From 3cf1d5021bd7de39a9ed205bc914a9c88a27aa45 Mon Sep 17 00:00:00 2001 From: shanyuhai123 <864299347@qq.com> Date: Sat, 14 Nov 2026 21:33:08 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E9=87=8D=E6=9E=84=20CS=20=E6=9E=B6?= =?UTF-8?q?=E6=9E=84=E5=B9=B6=E9=AA=8C=E8=AF=81=20Android=20=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/mobile-build.yml | 91 + .playwright/scripts/README.md | 9 + .../__tests__/attachment-publication.e2e.mjs | 62 + .../browser-platform-lifetime.e2e.mjs | 88 + .../browser-selection-references.e2e.mjs | 61 +- .../scripts/__tests__/commandCapacity.ts | 93 + .../scripts/__tests__/connections.e2e.mjs | 99 + .../context-panel-draft-adoption.e2e.mjs | 2 +- .../__tests__/extension-contributions.e2e.mjs | 38 +- .../__tests__/local-service-lifecycle.e2e.mjs | 328 ++++ .../__tests__/mobile-approvals.e2e.mjs | 235 +++ .../scripts/__tests__/mobile-branches.e2e.mjs | 360 ++++ .../scripts/__tests__/mobile-content.e2e.mjs | 98 + .../scripts/__tests__/mobile-desktop.e2e.mjs | 199 ++ .../scripts/__tests__/mobile-devices.e2e.mjs | 209 +++ .../scripts/__tests__/mobile-history.e2e.mjs | 190 ++ .../__tests__/mobile-interruption.e2e.mjs | 166 ++ .../__tests__/mobile-questions.e2e.mjs | 320 ++++ .../scripts/__tests__/mobile-recovery.e2e.mjs | 52 + .../__tests__/mobile-task-controls.e2e.mjs | 228 +++ .../__tests__/persistence-upgrade.e2e.mjs | 178 ++ .../__tests__/prepareProductProfile.ts | 15 + .../scripts/__tests__/product-desktop.e2e.mjs | 56 + .../scripts/__tests__/service-process.e2e.mjs | 275 +++ .playwright/scripts/__tests__/storage.e2e.mjs | 289 +-- .playwright/scripts/__tests__/storageCrash.ts | 400 ++++ .../scripts/__tests__/storageDurability.ts | 244 +++ .../scripts/__tests__/storageFixture.ts | 15 + .../scripts/__tests__/task-archive-profile.ts | 2 +- .../scripts/__tests__/task-archive.e2e.mjs | 493 +++-- .../scripts/__tests__/workbench.e2e.mjs | 111 +- .playwright/scripts/buddy-test.mjs | 39 +- .../fixtures/attachment-publication-crash.ts | 68 + .playwright/scripts/fixtures/electron.mjs | 10 +- .../fixtures/extension-contributions.mjs | 6 +- .../scripts/fixtures/local-service.mjs | 213 +++ .../scripts/fixtures/mobile-desktop.mjs | 18 + .../fixtures/runtime-network-utility.mjs | 7 +- .../fixtures/service-process-harness.ts | 62 + .playwright/scripts/fixtures/task-archive.mjs | 49 +- .../mobile/__tests__/process-recovery.e2e.mjs | 60 + .../mobile/capture-android-performance.mjs | 118 ++ .../mobile/capture-android-streaming.mjs | 88 + .../scripts/private-network-fixture.mjs | 135 ++ .playwright/scripts/storage-durability.mjs | 47 + apps/buddy/electron-builder.config.cjs | 14 +- apps/buddy/electron.vite.config.ts | 4 +- apps/buddy/electron/dev.mjs | 38 + .../electron/main/DesktopWindowManager.ts | 16 +- .../main/__tests__/conversationIpc.spec.ts | 123 ++ .../electron/main/app/DesktopApplication.ts | 174 +- .../electron/main/app/DesktopIntegrations.ts | 55 +- .../electron/main/app/DesktopRuntimeHost.ts | 223 ++- .../buddy/electron/main/app/DesktopStartup.ts | 7 + .../electron/main/app/DesktopWindowHost.ts | 2 + .../main/app/__tests__/DesktopStartup.spec.ts | 21 +- .../__tests__/desktopQuitLifecycle.spec.ts | 93 +- .../__tests__/requestDesktopTarget.spec.ts | 72 + .../electron/main/app/desktopQuitLifecycle.ts | 17 + .../electron/main/app/desktopServiceEntry.ts | 105 ++ apps/buddy/electron/main/app/environment.ts | 43 +- .../electron/main/app/requestDesktopTarget.ts | 78 + apps/buddy/electron/main/app/typing.ts | 4 +- .../buddy/electron/main/attachmentProtocol.ts | 8 +- .../electron/main/browser/BrowserHost.ts | 479 +---- .../main/browser/BrowserIntegration.ts | 52 +- .../main/browser/BrowserNavigation.ts | 477 +++++ .../main/browser/BrowserObservationStore.ts | 94 + .../main/browser/BrowserPresentationHost.ts | 167 ++ .../main/browser/SemanticBrowserDriver.ts | 104 +- .../__tests__/BrowserPresentationHost.spec.ts | 91 + .../main/browser/registerBrowserDesktopIpc.ts | 17 + .../electron/main/config/LexoraConfigStore.ts | 31 +- .../__tests__/initializeDesktopConfig.spec.ts | 24 +- .../main/config/initializeDesktopConfig.ts | 2 +- .../ExtensionTaskCleanupRuntime.spec.ts | 6 +- .../main/extensions/registerExtensionIpc.ts | 66 +- .../electron/main/extensions/runtime/host.js | 22 +- apps/buddy/electron/main/index.ts | 5 +- .../electron/main/local-chat/conversation.ts | 5 +- .../electron/main/local-chat/executions.ts | 1 + .../main/local-chat/privateNetwork.ts | 12 + .../electron/main/local-chat/registrar.ts | 4 + .../buddy/electron/main/local-chat/runtime.ts | 11 + apps/buddy/electron/main/localChatIpc.ts | 2 + .../productProtocol/DesktopProductCommands.ts | 120 ++ .../DesktopTaskSynchronization.ts | 83 + .../productProtocol/ProductDesktopGateway.ts | 384 ++++ .../productProtocol/ProductOutboxStore.ts | 199 ++ .../main/productProtocol/ProductRpcClient.ts | 161 ++ .../main/productProtocol/ProductSyncClient.ts | 181 ++ .../productProtocol/RendererDraftStore.ts | 86 + .../__tests__/DesktopProductCommands.spec.ts | 140 ++ .../__tests__/ProductRpcClient.spec.ts | 61 + .../__tests__/ProductSyncClient.spec.ts | 98 + .../__tests__/RendererDraftStore.spec.ts | 77 + .../registerDraftRecoveryIpc.ts | 41 + .../main/runtime/RemoteServiceClient.ts | 193 ++ .../main/runtime/buddyServiceEnvironment.ts | 65 +- .../main/runtime/platformHostBootstrap.ts | 26 + .../electron/main/runtime/serviceProcess.ts | 81 + .../main/sandbox/registerSandboxHostRpc.ts | 2 +- .../main}/sandbox/sandboxProcess.ts | 4 +- .../electron/main/secrets/CredentialVault.ts | 5 +- .../main/storage/StorageBackupService.ts | 10 +- .../main/storage/StorageMigrationWindow.ts | 13 +- .../storage/__tests__/profileWrites.spec.ts | 40 + .../requestPersistenceUpgrade.spec.ts | 37 + .../electron/main/storage/profileWrites.ts | 56 + .../main/storage/requestPersistenceUpgrade.ts | 23 + .../electron/main/storage/storageBootstrap.ts | 10 +- .../storage/storageMigrationDiagnostics.ts | 2 +- .../main/storage/storageMigrationPage.ts | 25 +- .../electron/main/updates/linuxInstaller.ts | 9 +- .../electron/main/updates/windowsInstaller.ts | 47 +- apps/buddy/electron/preload/browser.ts | 4 +- apps/buddy/electron/preload/desktop.ts | 18 +- .../electron/preload/local-chat/activity.ts | 8 +- .../preload/local-chat/conversation.ts | 29 +- .../preload/local-chat/draftRecovery.ts | 13 + .../electron/preload/local-chat/executions.ts | 1 + .../preload/local-chat/privateNetwork.ts | 14 + .../electron/preload/local-chat/runtime.ts | 3 + .../electron/preload/local-chat/taskMarks.ts | 4 +- apps/buddy/electron/preload/localChatApi.ts | 4 + apps/buddy/electron/shared/desktopApi.ts | 12 +- .../electron/shared/desktopApiSchemas.ts | 2 + apps/buddy/electron/shared/localChatApi.ts | 54 +- .../electron/shared/privateNetworkApi.ts | 1 + .../__tests__/dependency-boundaries.spec.mjs | 127 -- apps/buddy/eslint/dependency-boundaries.js | 14 +- apps/buddy/mobile-reader.vite.config.ts | 48 + apps/buddy/native/host/Cargo.toml | 8 + .../host/__tests__/process_identity_macos.rs | 33 + .../host/__tests__/runtime_guard.windows.mjs | 51 + .../native/host/__tests__/service_launcher.rs | 40 + .../native/host/src/bin/process_control.rs | 13 + apps/buddy/native/host/src/lib.rs | 2 + apps/buddy/native/host/src/linux_update.rs | 7 +- apps/buddy/native/host/src/process_control.rs | 8 +- .../native/host/src/process_control/macos.rs | 117 ++ apps/buddy/native/host/src/runtime_guard.rs | 92 +- .../buddy/native/host/src/service_launcher.rs | 99 + .../buddy/native/pet/src/native_pet/window.rs | 1134 +----------- .../native/pet/src/native_pet/window_draw.rs | 23 + .../pet/src/native_pet/window_pointer.rs | 522 ++++++ .../pet/src/native_pet/window_runtime.rs | 655 +++++++ apps/buddy/package.json | 18 +- .../platform/extensions/ExtensionDownloads.ts | 26 + .../extensions/ExtensionInferenceRequests.ts | 204 +++ .../extensions/ExtensionInstallations.ts | 4 +- .../extensions/ExtensionInvocationScope.ts | 21 + .../extensions/ExtensionPackageStore.ts | 50 +- .../extensions/ExtensionSelectedResources.ts | 130 ++ .../platform/extensions/ExtensionService.ts | 763 ++++---- .../platform/extensions/ExtensionUploads.ts | 55 + .../extensions/ExtensionViewExports.ts | 70 + .../__tests__/ExtensionCardActions.spec.ts | 48 +- .../__tests__/ExtensionCatalogService.spec.ts | 20 +- .../__tests__/ExtensionExecutionScope.spec.ts | 82 + .../__tests__/ExtensionLifetime.spec.ts | 194 ++ .../__tests__/ExtensionPackageStore.spec.ts | 47 +- .../__tests__/ExtensionTaskCleanup.spec.ts | 20 +- .../createExtensionTaskCleanup.spec.ts | 9 - .../ipc/__tests__/localTransport.spec.ts | 19 + apps/buddy/platform/ipc/localTransport.ts | 42 +- apps/buddy/platform/native/nodeRuntime.json | 45 + .../platform/process/serviceEnvironment.ts | 65 + apps/buddy/platform/service/HostConnection.ts | 89 + .../platform/service/LocalServiceServer.ts | 98 + .../platform/service/ServiceChildProcess.ts | 182 ++ .../platform/service/ServiceProfileOwner.ts | 97 + .../platform/service/ServiceRpcChannel.ts | 28 + .../service/SessionServiceRegistration.ts | 121 ++ apps/buddy/platform/service/SocketFrames.ts | 111 ++ .../__tests__/ServiceChildProcess.spec.ts | 36 + .../service/__tests__/localService.spec.ts | 141 ++ .../platform/service/connectLocalService.ts | 37 + .../platform/service/launchLocalService.ts | 98 + apps/buddy/platform/service/releaseSet.ts | 120 ++ apps/buddy/platform/service/serviceFiles.ts | 52 + .../service/serviceProcessIdentity.ts | 45 + .../platform/storage/StorageLocationStore.ts | 10 +- .../platform/storage/StorageRootLease.ts | 18 +- .../buddy/platform/storage/productIdentity.ts | 28 + .../platform/storage/storageOperationFiles.ts | 2 +- apps/buddy/service.vite.config.ts | 50 + apps/buddy/service/assets.d.ts | 9 + .../skills/plugin-creator/references/api.d.ts | 9 +- .../plugin-creator/references/lifecycle.md | 15 + .../plugin-creator/references/protocol.md | 8 + apps/buddy/service/src/BuddyService.ts | 800 +++++--- apps/buddy/service/src/LocalServiceDaemon.ts | 436 +++++ .../service/src/LocalServiceSupervisor.ts | 78 + .../src/__tests__/LocalServiceDaemon.spec.ts | 233 +++ .../buddyService.integration.spec.ts | 317 +++- .../__tests__/mutationConcurrencyFixture.ts | 22 +- .../agent/events/BufferedRunEventWriter.ts | 31 +- .../service/src/agent/events/PiEventBridge.ts | 1 + .../__tests__/BufferedRunEventWriter.spec.ts | 82 +- .../src/agent/execution/ActiveRunRegistry.ts | 12 +- .../src/agent/execution/BuddyAgentRunner.ts | 23 +- .../execution/BuddyRunExecutionPlanner.ts | 1 - .../src/agent/execution/BuddyTurnLauncher.ts | 13 +- .../ExecutionComposerInputService.ts | 2 +- .../agent/execution/ExecutionCoordinator.ts | 8 + .../execution/ExecutionPresentationService.ts | 22 +- .../execution/RuntimeExecutionLifecycle.ts | 35 + .../__tests__/BuddyAgentRunner.spec.ts | 102 +- .../__tests__/BuddyTurnLauncher.spec.ts | 68 +- .../__tests__/ExecutionCoordinator.spec.ts | 54 - .../ExecutionPresentationService.spec.ts | 48 +- .../agent/execution/registerExecutionRpc.ts | 1 + .../browserSessionExtensions.spec.ts | 2 +- .../src/agent/sessions/BuddySessionFactory.ts | 1 - .../agent/sessions/PiRequestMaterializer.ts | 198 ++ .../src/agent/sessions/PiSessionMailbox.ts | 112 ++ .../agent/sessions/bindRuntimePreferences.ts | 13 +- .../sessions/createReusableBuddySession.ts | 323 +--- .../__tests__/BuddyConversationTree.spec.ts | 72 +- .../sessions/withPiSessionStorageBoundary.ts | 18 + .../src/approvals/ApprovalCommandService.ts | 139 ++ .../service/src/approvals/ApprovalService.ts | 146 +- .../__tests__/ApprovalCommands.spec.ts | 227 +++ .../__tests__/ApprovalService.spec.ts | 219 +-- .../service/src/approvals/approvalReview.ts | 60 + .../src/approvals/registerApprovalRpc.ts | 22 +- .../src/attachments/AttachmentError.ts | 9 + .../AttachmentInputMaterializer.ts | 258 +++ .../src/attachments/AttachmentInputReader.ts | 14 + .../src/attachments/AttachmentService.ts | 762 ++++---- .../attachments/ComposerResourceService.ts | 72 +- .../src/attachments/InputMaterialProjector.ts | 121 ++ .../__tests__/AttachmentPersistence.spec.ts | 215 +++ .../src/attachments/attachmentMetadata.ts | 82 + .../src/attachments/registerAttachmentRpc.ts | 6 +- .../src/automations/AutomationScheduler.ts | 20 +- ...tomationOccurrenceLifecycleService.spec.ts | 143 +- .../__tests__/AutomationScheduler.spec.ts | 33 +- .../__tests__/registerAutomationRpc.spec.ts | 200 -- .../src/automations/registerAutomationRpc.ts | 13 +- .../service/src/chat/ChatCommandService.ts | 83 +- .../src/chat/ChatControlCommandService.ts | 127 ++ .../src/chat/ChatInputCommandService.ts | 221 +++ .../service/src/chat/ChatInputMaterializer.ts | 307 ++++ .../service/src/chat/ChatInputPreparation.ts | 121 ++ .../src/chat/ChatInputValidationService.ts | 2 +- .../service/src/chat/ChatQueueService.ts | 228 ++- .../buddy/service/src/chat/ChatTurnService.ts | 739 ++------ .../src/chat/ComposerDraftCommandService.ts | 223 +++ .../service/src/chat/ComposerDraftService.ts | 8 + .../src/chat/PreparedResourceSettlement.ts | 46 + .../src/chat/PreparedRunCommandService.ts | 112 ++ .../service/src/chat/QueueContinuation.ts | 2 +- .../service/src/chat/TurnRequestService.ts | 59 +- .../chat/__tests__/ChatCommandService.spec.ts | 14 - .../__tests__/ChatInputCommandService.spec.ts | 217 +++ .../chat/__tests__/ChatQueueService.spec.ts | 324 ++-- .../ComposerDraftCommandService.spec.ts | 139 ++ .../PreparedResourceSettlement.spec.ts | 73 + .../PreparedRunCommandService.spec.ts | 471 +++++ .../__tests__/persistPreparedTurn.spec.ts | 61 - .../sessionReferenceAuthorization.spec.ts | 21 +- .../service/src/chat/persistPreparedTurn.ts | 12 +- .../service/src/chat/resolveChatInput.ts | 24 + .../src/commands/CommandAdmissionError.ts | 27 + .../src/commands/CommandAdmissionService.ts | 187 ++ .../src/commands/DesktopOpenCommandService.ts | 34 + .../__tests__/CommandAdmissionService.spec.ts | 171 ++ .../DesktopOpenCommandService.spec.ts | 36 + .../commands/__tests__/commandIntent.spec.ts | 30 + .../service/src/commands/commandIntent.ts | 131 ++ .../src/connectors/mcp/McpConnectorService.ts | 269 +-- .../connectors/mcp/McpCredentialChanges.ts | 67 + .../__tests__/McpCredentialChanges.spec.ts | 80 + .../context/ContextUsageSnapshotService.ts | 2 +- .../ConversationArchiveService.ts | 137 +- .../ConversationCleanupService.ts | 11 +- .../ConversationCommandService.ts | 260 +++ .../ConversationLifecycleService.ts | 22 +- .../ConversationMetadataService.ts | 147 +- .../ConversationMutationGuard.ts | 4 - .../conversations/ConversationPurgeService.ts | 16 +- .../src/conversations/ExtensionTaskContext.ts | 12 +- .../conversations/SessionReferenceAccess.ts | 2 +- .../src/conversations/TaskMarkService.ts | 23 +- .../ConversationArchiveService.spec.ts | 55 +- .../ConversationCleanupService.spec.ts | 22 +- .../ConversationCommandService.spec.ts | 233 +++ .../ConversationMetadataService.spec.ts | 124 +- .../extensionReferenceCapabilities.spec.ts | 51 +- .../extensionTaskCapabilities.spec.ts | 23 +- .../__tests__/referenceSnapshots.spec.ts | 22 +- .../registerConversationArchiveRpc.spec.ts | 133 +- .../__tests__/registerConversationRpc.spec.ts | 55 +- .../registerConversationArchiveRpc.ts | 37 +- .../conversations/registerConversationRpc.ts | 14 +- .../sessionReferenceExtension.ts | 4 +- .../src/createBuddyCapabilityFactory.ts | 4 +- .../service/src/events/RunEventFailure.ts | 14 + apps/buddy/service/src/events/RunEventLog.ts | 11 +- .../buddy/service/src/events/RunEventPorts.ts | 2 - .../service/src/events/RunEventProjector.ts | 35 +- .../service/src/events/RunEventQueries.ts | 27 +- .../service/src/events/SqliteRunEventLog.ts | 247 +++ .../src/events/SqliteRunEventWriter.ts | 106 ++ .../src/events/__tests__/RunEventLog.spec.ts | 10 +- .../__tests__/RunEventMaintenance.spec.ts | 22 +- .../__tests__/SqliteRunEventLog.spec.ts | 188 ++ .../__tests__/SqliteRunEventWriter.spec.ts | 445 +++++ .../service/src/events/createRunEventLog.ts | 2 + .../service/src/events/prepareEventPayload.ts | 11 + .../src/images/createPluginImageBackends.ts | 2 +- apps/buddy/service/src/index.ts | 348 ++-- .../service/src/network/runtimeNetwork.ts | 60 +- .../buddy/service/src/pet/PetActionService.ts | 16 +- .../pet/__tests__/PetActionService.spec.ts | 64 +- .../src/platform/HostConnectorSecretStore.ts | 42 + .../service/src/platform/RuntimePlatform.ts | 75 + .../src/platform/RuntimePlatformLifecycle.ts | 58 + .../__tests__/runtimePlatform.spec.ts | 27 + .../src/platform/createRpcRuntimePlatform.ts | 179 ++ .../service/src/platform/nodeServiceConfig.ts | 50 + .../src/platform/prepareRuntimeEnvironment.ts | 10 + .../src/plugins/ExtensionAgentRuntime.ts | 35 +- .../src/plugins/PluginAuthoringService.ts | 20 +- apps/buddy/service/src/plugins/PluginHost.ts | 21 + .../__tests__/ExtensionActionService.spec.ts | 5 +- .../__tests__/ExtensionAgentRuntime.spec.ts | 12 +- .../pluginAuthoringCapability.spec.ts | 6 +- .../src/plugins/pluginAuthoringCapability.ts | 12 +- .../DeviceAuthorizationService.ts | 278 +++ .../PrivateNetworkController.ts | 156 ++ .../privateNetwork/PrivateNetworkServer.ts | 293 +++ .../PrivateNetworkTlsIdentity.ts | 83 + .../src/privateNetwork/RemoteAvatarService.ts | 35 + .../privateNetwork/RemoteResourceService.ts | 357 ++++ .../src/privateNetwork/RemoteUploadService.ts | 437 +++++ .../DeviceAuthorizationService.spec.ts | 188 ++ .../PrivateNetworkController.spec.ts | 69 + .../__tests__/PrivateNetworkServer.spec.ts | 114 ++ .../__tests__/RemoteAvatarService.spec.ts | 64 + .../__tests__/RemoteResourceService.spec.ts | 134 ++ .../__tests__/RemoteUploadService.spec.ts | 194 ++ .../__tests__/privateNetworkAddress.spec.ts | 20 + .../privateNetwork/privateNetworkAddress.ts | 13 + .../revokeRestoredDeviceGrants.ts | 19 + .../src/privateNetwork/uploadValidation.ts | 15 + .../ProductActorCommandScope.ts | 116 ++ .../productProtocol/ProductBuddyAvatars.ts | 32 + .../src/productProtocol/ProductConnection.ts | 180 ++ .../ProductContentReferences.ts | 70 + .../productProtocol/ProductDraftReadModel.ts | 46 + .../src/productProtocol/ProductPageReader.ts | 50 + .../src/productProtocol/ProductQueries.ts | 261 +++ .../productProtocol/ProductQueryRevisions.ts | 93 + .../src/productProtocol/ProductReadViews.ts | 372 ++++ .../ProductStreamingMessages.ts | 60 + .../productProtocol/ProductSyncPublisher.ts | 401 ++++ .../productProtocol/ProductTaskReadModel.ts | 88 + .../ConversationArchiveCommands.spec.ts | 284 +++ .../__tests__/ProductConnection.spec.ts | 51 + .../__tests__/ProductPersistence.spec.ts | 494 +++++ .../__tests__/ProductQueryRevisions.spec.ts | 90 + .../ProductServiceConnections.spec.ts | 146 ++ .../__tests__/ProductSyncPublisher.spec.ts | 99 + .../__tests__/ProductTaskReads.spec.ts | 43 + .../createProductCommandHandlers.ts | 171 ++ .../createProductModelQueries.ts | 55 + .../createProductSynchronization.ts | 197 ++ .../productProtocol/projectCommandReceipt.ts | 32 + .../registerLocalProductConnection.ts | 26 + .../src/providers/HostCredentialStore.ts | 34 +- .../src/providers/ProviderModelCatalog.ts | 306 +--- .../__tests__/BuiltinModelDiscovery.spec.ts | 3 +- .../__tests__/PluginModelRegistry.spec.ts | 82 +- .../providerModelReconciliation.spec.ts | 47 + .../src/providers/createProviderService.ts | 21 +- .../providers/providerModelReconciliation.ts | 86 + .../src/providers/providerModelSource.ts | 96 + .../src/providers/registerProviderRpc.ts | 25 +- .../src/questions/UserQuestionDelivery.ts | 6 +- .../src/questions/UserQuestionService.ts | 81 +- .../__tests__/ProductQuestionCommands.spec.ts | 348 ++++ .../__tests__/UserQuestionService.spec.ts | 49 - .../service/src/rpc/BuddyServiceRpcServer.ts | 58 +- .../src/rpc/ServiceClientConnections.ts | 100 + .../__tests__/BuddyServiceRpcServer.spec.ts | 34 +- .../ServiceClientConnections.spec.ts | 84 + .../rpc/__tests__/serviceConnectionHarness.ts | 33 + .../src/rpc/candidateLegacyRegistrar.ts | 41 + .../src/rpc/registerServiceManagementRpc.ts | 39 + .../service/src/runs/RunLifecycleService.ts | 4 +- .../runs/__tests__/recoverSqliteRuns.spec.ts | 319 ++++ apps/buddy/service/src/runs/publicRun.ts | 1 + .../service/src/runs/recoverSqliteRuns.ts | 70 + .../service/src/sandbox/ShellSandboxClient.ts | 4 +- apps/buddy/service/src/skills/SkillService.ts | 8 +- .../spaces/__tests__/registerSpaceRpc.spec.ts | 3 +- .../service/src/spaces/registerSpaceRpc.ts | 6 + apps/buddy/service/src/startServiceRuntime.ts | 171 ++ .../storage/ChatControlCommandRepository.ts | 132 ++ .../service/src/storage/CommandCommitter.ts | 146 ++ .../storage/ConversationCommandRepository.ts | 88 + .../src/storage/ExecutionResourceQueries.ts | 58 + .../service/src/storage/StorageIsolation.ts | 100 + .../service/src/storage/StorageMigration.ts | 63 +- .../service/src/storage/StorageSnapshots.ts | 17 +- .../ExecutionResourceQueries.spec.ts | 81 + .../__tests__/StorageIsolation.spec.ts | 86 + .../__tests__/StorageMigration.spec.ts | 171 +- .../storage/__tests__/archiveBackup.spec.ts | 74 - .../__tests__/archiveSchemaMigration.spec.ts | 77 +- .../__tests__/branchParallelExecution.spec.ts | 145 ++ .../__tests__/buddyIdentityRepository.spec.ts | 3 +- .../__tests__/chatQueueRepository.spec.ts | 11 +- .../__tests__/checkpointSqliteStorage.spec.ts | 102 ++ .../contentReferenceOwnership.spec.ts | 208 +++ .../conversationArchivePurge.spec.ts | 343 ++++ .../conversationArchiveRepository.spec.ts | 228 --- .../conversationHistoryRepositories.spec.ts | 119 -- .../__tests__/databaseTransaction.spec.ts | 180 -- .../draftOwnershipRepository.spec.ts | 57 + .../__tests__/executionRepository.spec.ts | 21 +- .../__tests__/executionRestart.spec.ts | 22 +- .../__tests__/legacyConversationPurge.spec.ts | 10 +- .../__tests__/persistenceBackup.spec.ts | 197 ++ .../__tests__/persistenceMigration.spec.ts | 256 +++ .../src/storage/__tests__/schema.spec.ts | 109 +- .../src/storage/__tests__/sqliteRunFixture.ts | 15 + .../storage/__tests__/sqliteStartup.spec.ts | 84 + .../__tests__/taskMarkRepository.spec.ts | 36 - .../__tests__/threadBindingRepository.spec.ts | 2 +- .../src/storage/__tests__/transaction.spec.ts | 246 +++ .../__tests__/turnRequestRepository.spec.ts | 34 +- .../usageAnalyticsRepository.spec.ts | 113 +- .../__tests__/verifyCommandReceipt.spec.ts | 95 + .../service/src/storage/approvalRepository.ts | 25 +- .../attachmentPersistenceRepository.ts | 191 ++ .../src/storage/attachmentRepository.ts | 37 +- .../service/src/storage/backupDatabase.ts | 65 +- .../src/storage/backupDatabasePolicy.ts | 4 +- .../src/storage/chatQueueRepository.ts | 35 +- .../src/storage/checkpointSqliteStorage.ts | 27 + .../src/storage/commandRequestRepository.ts | 56 +- .../src/storage/commitBackupRestore.ts | 36 +- .../src/storage/composerDraftRepository.ts | 47 +- .../src/storage/composerResourceRepository.ts | 8 +- .../storage/conversationArchiveRepository.ts | 7 +- .../storage/conversationDeletionRepository.ts | 38 +- .../storage/conversationHistoryRepository.ts | 7 - .../storage/conversationIndexRepository.ts | 9 +- .../storage/conversationPurgeRepository.ts | 94 +- .../service/src/storage/conversationRecord.ts | 14 +- apps/buddy/service/src/storage/database.ts | 302 +-- .../src/storage/draftOwnershipRepository.ts | 68 + .../src/storage/executionRepository.ts | 20 +- .../storage/extensionReferenceRepository.ts | 2 +- .../service/src/storage/mailboxRepository.ts | 2 +- .../migrations/approvalReviewsSchema.ts | 17 + .../migrations/branchExecutionSchema.ts | 15 + .../contentReferenceOwnershipSchema.ts | 91 + .../conversationArchiveManagedFilesSchema.ts | 19 + .../migrations/draftOwnershipSchema.ts | 43 + .../storage/migrations/managedFilesSchema.ts | 133 ++ .../migrations/metadataCommandsSchema.ts | 12 + .../migrations/persistenceCommandsSchema.ts | 60 + .../persistenceIdentitySchema.ts} | 2 +- .../migrations/privateNetworkSchema.ts | 71 + .../productContentReferencesSchema.ts | 26 + .../migrations/productQueryRevisionsSchema.ts | 132 ++ .../storage/migrations/remoteFilesSchema.ts | 46 + .../migrations/resourceExportsSchema.ts | 25 + .../src/storage/migrations/v43Persistence.ts | 28 + .../__tests__/persistenceCandidate.spec.ts | 196 ++ .../activatePersistenceCandidate.ts | 79 + .../importPersistenceCandidate.ts | 291 +++ .../persistenceMigration/legacyRunHistory.ts | 150 ++ .../persistenceSources.ts | 190 ++ .../service/src/storage/providerRepository.ts | 3 +- .../src/storage/relocateProfileReferences.ts | 18 +- .../service/src/storage/restoreBackup.ts | 106 +- .../service/src/storage/restorePolicy.ts | 3 +- apps/buddy/service/src/storage/schema.ts | 4 + .../service/src/storage/snapshotDatabase.ts | 91 + .../service/src/storage/sqliteAuthority.ts | 16 + .../service/src/storage/taskMarkRepository.ts | 12 +- .../src/storage/threadBindingRepository.ts | 4 +- apps/buddy/service/src/storage/transaction.ts | 185 ++ .../src/storage/turnRequestRepository.ts | 127 +- .../src/storage/verifyCommandReceipt.ts | 24 + .../service/src/validateServiceRelease.ts | 15 + .../service/src/web/WebSettingsService.ts | 24 +- .../web/__tests__/WebSettingsService.spec.ts | 20 +- .../service/src/web/observeWebDiagnostics.ts | 2 +- apps/buddy/service/src/web/webContent.ts | 2 +- .../buddy/shared/browser/browserDesktopApi.ts | 12 + .../shared/browser/browserDesktopSchemas.ts | 17 + .../__tests__/contentBehaviorVectors.spec.ts | 17 + apps/buddy/shared/conversation/actorThread.ts | 4 +- apps/buddy/shared/conversation/chatApi.ts | 2 +- .../buddy/shared/conversation/chatQueueApi.ts | 10 +- .../shared/conversation/composerDraft.ts | 1 + .../shared/conversation/conversationApi.ts | 36 +- .../conversation/conversationArchiveApi.ts | 1 - .../shared/conversation/draftRecovery.ts | 42 + .../shared/conversation/submittedInput.ts | 30 + apps/buddy/shared/conversation/taskMarkApi.ts | 10 +- .../diagnostics/credentialVaultFailure.ts | 2 +- .../executions/executionPresentationApi.ts | 6 + .../__tests__/extensionCompatibility.spec.ts | 18 - .../extensionRequestLifecycle.spec.ts | 11 - .../buddy/shared/extensions/extensionAgent.ts | 1 + apps/buddy/shared/extensions/extensionApi.ts | 1 + .../extensions/extensionCompatibility.ts | 2 + .../extensions/extensionInstallation.ts | 1 + .../shared/extensions/extensionManifest.ts | 20 +- .../shared/extensions/extensionResources.ts | 2 + .../shared/lifecycle/AsyncOperationScope.ts | 52 + .../__tests__/AsyncOperationScope.spec.ts | 34 + apps/buddy/shared/lifecycle/exitBehavior.ts | 5 + apps/buddy/shared/permissions/approvalApi.ts | 8 +- .../permissions/approvalReviewPayload.ts | 4 +- .../shared/permissions/conditionalApproval.ts | 14 + apps/buddy/shared/privateNetwork/limits.ts | 24 + .../buddy/shared/privateNetwork/management.ts | 23 + apps/buddy/shared/privateNetwork/pairing.ts | 40 + apps/buddy/shared/privateNetwork/resources.ts | 14 + apps/buddy/shared/privateNetwork/uploads.ts | 53 + .../__tests__/protocol.spec.ts | 52 + apps/buddy/shared/productProtocol/avatars.ts | 10 + .../content-behavior-vectors.json | 187 ++ apps/buddy/shared/productProtocol/content.ts | 63 + apps/buddy/shared/productProtocol/errors.ts | 74 + apps/buddy/shared/productProtocol/identity.ts | 42 + apps/buddy/shared/productProtocol/limits.ts | 24 + apps/buddy/shared/productProtocol/methods.ts | 87 + apps/buddy/shared/productProtocol/queries.ts | 66 + apps/buddy/shared/productProtocol/receipts.ts | 61 + apps/buddy/shared/productProtocol/scalars.ts | 41 + apps/buddy/shared/productProtocol/sync.ts | 35 + apps/buddy/shared/productProtocol/topics.ts | 56 + .../buddy/shared/productProtocol/transport.ts | 11 + .../buddy/shared/productProtocol/vectors.json | 264 +++ apps/buddy/shared/productProtocol/views.ts | 99 + apps/buddy/shared/runs/runApi.ts | 2 + .../shared/runtime/attachmentHostProtocol.ts | 7 + .../shared/runtime/credentialProtocol.ts | 2 +- apps/buddy/shared/runtime/localChatError.ts | 12 + apps/buddy/shared/runtime/productRecovery.ts | 16 + apps/buddy/shared/runtime/profile.ts | 2 + .../shared/service/localServiceProtocol.ts | 102 ++ apps/buddy/shared/settings/settingsCatalog.ts | 1 + apps/buddy/shared/storage/storageApi.ts | 2 +- .../workbench/workbenchContributionCatalog.ts | 2 +- .../src/app/bootstrap/DesktopAppProvider.vue | 6 +- .../__tests__/useDesktopNavigation.spec.ts | 14 +- .../src/app/bootstrap/useDesktopNavigation.ts | 17 +- .../src/app/shell/DesktopCommandRecovery.vue | 99 + apps/buddy/src/app/shell/DesktopShell.vue | 2 + .../src/app/workbench/ActiveTaskProjection.ts | 4 +- .../app/workbench/DesktopTaskContribution.vue | 33 +- .../src/app/workbench/TaskWorkspacePool.ts | 121 +- .../__tests__/TaskInputCleanup.spec.ts | 12 +- .../desktopSelectionReferences.spec.ts | 55 +- .../__tests__/instantTaskSwitch.spec.ts | 102 +- .../__tests__/prepareFileClose.spec.ts | 61 + .../registerResourceContributions.spec.ts | 70 - .../workbench/__tests__/taskResource.spec.ts | 17 + .../createDesktopSelectionReferences.ts | 4 +- .../src/app/workbench/prepareFileClose.ts | 60 + .../app/workbench/restoreTaskInputViews.ts | 6 +- apps/buddy/src/app/workbench/taskResource.ts | 36 + .../src/app/workbench/useDesktopWorkbench.ts | 411 +---- .../src/app/workbench/useTaskNavigation.ts | 152 ++ .../app/workbench/useTaskResourceSpaces.ts | 10 +- .../src/app/workbench/useWorkbenchDialogs.ts | 131 ++ .../buddy/src/i18n/locales/en-US/approvals.ts | 3 + apps/buddy/src/i18n/locales/en-US/chat.ts | 26 + apps/buddy/src/i18n/locales/en-US/settings.ts | 40 + .../buddy/src/i18n/locales/zh-CN/approvals.ts | 3 + apps/buddy/src/i18n/locales/zh-CN/chat.ts | 26 + apps/buddy/src/i18n/locales/zh-CN/settings.ts | 40 + .../src/mobile-reader/DocumentPreview.vue | 40 + .../buddy/src/mobile-reader/MessageAvatar.vue | 39 + apps/buddy/src/mobile-reader/MobileReader.vue | 79 + .../buddy/src/mobile-reader/ReaderMessage.vue | 77 + .../mobile-reader/ReaderMessageActions.vue | 34 + .../src/mobile-reader/ReaderTerminal.vue | 16 + .../src/mobile-reader/ReaderToolCall.vue | 78 + apps/buddy/src/mobile-reader/ReaderTurn.vue | 15 + .../src/mobile-reader/ReaderTurnContent.vue | 53 + .../__tests__/readerProjection.spec.ts | 26 + apps/buddy/src/mobile-reader/bootstrap.ts | 10 + .../src/mobile-reader/document-bootstrap.ts | 10 + apps/buddy/src/mobile-reader/document.html | 13 + apps/buddy/src/mobile-reader/document.ts | 2 + apps/buddy/src/mobile-reader/index.html | 13 + apps/buddy/src/mobile-reader/main.ts | 2 + .../src/mobile-reader/markdownStrings.ts | 47 + apps/buddy/src/mobile-reader/polyfills.ts | 8 + apps/buddy/src/mobile-reader/reader.css | 539 ++++++ apps/buddy/src/mobile-reader/readerBridge.ts | 99 + .../src/mobile-reader/readerProjection.ts | 35 + .../buddy/src/mobile-reader/readerProtocol.ts | 76 + apps/buddy/src/mobile-reader/readerStrings.ts | 62 + .../src/mobile-reader/useDocumentPreview.ts | 56 + .../src/mobile-reader/useReaderAvatars.ts | 9 + .../mobile-reader/useReaderCopyFeedback.ts | 5 + .../src/mobile-reader/useReaderResultRead.ts | 54 + .../buddy/src/mobile-reader/useReaderTurns.ts | 72 + .../src/mobile-reader/useReaderViewport.ts | 117 ++ apps/buddy/src/modules/buddies/reader.ts | 1 + .../state/__tests__/useExtensionState.spec.ts | 52 +- .../widgets/DesktopExtensionInstallReview.vue | 14 + .../DesktopExtensionFrameHost.spec.ts | 2 +- .../DesktopExtensionInstallReview.spec.ts | 172 +- .../model/__tests__/settingsRegistry.spec.ts | 41 +- .../modules/settings/model/builtinSettings.ts | 3 +- .../settings/model/settingsRegistry.ts | 2 +- .../pages/DesktopConnectionsSettingsView.vue | 10 + .../settings/pages/settingsPageRenderers.ts | 1 + .../app/DesktopGeneralSettingField.vue | 6 + .../widgets/devices/DesktopPairedDevices.vue | 44 + .../devices/DesktopPrivateNetworkSettings.vue | 89 + .../devices/usePrivateNetworkSettings.ts | 96 + .../navigation/DesktopSettingsSidebar.vue | 3 +- apps/buddy/src/modules/tasks/contracts.ts | 23 +- apps/buddy/src/modules/tasks/index.ts | 9 +- .../__tests__/approvalDecisions.spec.ts | 45 + .../model/approvals/approvalDecisions.ts | 26 + .../tasks/model/composer/actorInputTarget.ts | 8 + .../__tests__/chatMessageActions.spec.ts | 68 +- .../chatTranscriptProjection.spec.ts | 72 +- .../model/transcript/chatMessageActions.ts | 39 +- .../transcript/chatTranscriptPresentation.ts | 64 +- apps/buddy/src/modules/tasks/reader.ts | 9 + .../state/__tests__/useTaskCapability.spec.ts | 132 +- .../state/composer/useActorInputTarget.ts | 13 +- .../useAgentBrowserResources.spec.ts | 47 +- .../__tests__/useTaskResourcePanel.spec.ts | 60 +- .../useTaskResourcePanelClose.spec.ts | 8 +- .../context-panel/useTaskBrowserSessions.ts | 225 +++ .../context-panel/useTaskResourcePanel.ts | 204 +-- .../conversations/useChatBranchMutations.ts | 52 +- .../state/conversations/useTaskMembers.ts | 10 +- .../__tests__/createDraftRecovery.spec.ts | 117 ++ .../useTaskDraftModelBinding.spec.ts | 12 +- .../useTaskWorkspacePersistence.spec.ts | 87 +- .../tasks/state/drafts/createDraftRecovery.ts | 109 ++ .../src/modules/tasks/state/drafts/typing.ts | 3 + .../tasks/state/drafts/useChatDrafts.ts | 23 +- .../state/drafts/useChatPermissionSettings.ts | 2 +- .../tasks/state/drafts/useTaskActorModel.ts | 29 +- .../state/drafts/useTaskModelPersistence.ts | 4 +- .../drafts/useTaskWorkspacePersistence.ts | 44 +- .../__tests__/useChatExecutionTree.spec.ts | 1 + .../runs/__tests__/useExecutionDetail.spec.ts | 1 + .../tasks/state/runs/useChatApprovals.ts | 12 +- .../tasks/state/runs/useChatExecution.ts | 5 +- .../modules/tasks/state/runs/useChatQueue.ts | 7 +- .../tasks/state/runs/useChatTurnExecution.ts | 5 +- .../tasks/state/runs/useTaskContextUsage.ts | 52 + .../__tests__/useTaskActivities.spec.ts | 74 +- .../__tests__/useTaskHistory.spec.ts | 38 +- .../task-index/__tests__/useTaskIndex.spec.ts | 52 +- .../task-index/__tests__/useTaskMarks.spec.ts | 38 +- .../state/task-index/useTaskActivities.ts | 17 +- .../tasks/state/task-index/useTaskIndex.ts | 12 +- .../tasks/state/task-index/useTaskMarks.ts | 13 +- .../modules/tasks/state/useTaskCapability.ts | 128 +- .../modules/tasks/state/useTaskLifecycle.ts | 29 +- apps/buddy/src/modules/tasks/taskContext.ts | 2 +- apps/buddy/src/modules/tasks/ui.ts | 5 + .../widgets/task-index/DesktopTaskIndex.vue | 10 +- .../task-index/DesktopTaskMarkManager.vue | 21 +- .../DesktopTaskMoveToSpaceDialog.vue | 8 +- .../widgets/task-index/DesktopTaskRow.vue | 3 +- .../DesktopTaskArchiveManager.spec.ts | 65 +- .../DesktopTaskMoveToSpaceDialog.spec.ts | 77 +- .../task-index/useTaskIndexManagement.ts | 13 +- .../transcript/BuddyChatActionToolbar.vue | 4 +- .../transcript/BuddyChatMessageList.vue | 72 +- .../transcript/BuddyChatMessageRow.vue | 13 +- .../transcript/BuddyChatToolDetails.vue | 8 +- .../widgets/transcript/chatContentContext.ts | 2 + .../widgets/workspace/DesktopApprovalCard.vue | 329 +--- .../workspace/DesktopApprovalDetails.vue | 310 ++++ .../workspace/DesktopConversationBranches.vue | 78 + .../DesktopDraftRestorationNotice.vue | 17 + .../widgets/workspace/DesktopTaskEditor.vue | 6 + .../widgets/workspace/DesktopTaskNotices.vue | 1 + .../platform/browser/BrowserGuestCatalog.ts | 66 + .../browser/DesktopBrowserGuestHost.vue | 156 +- .../lib/__tests__/localChatError.spec.ts | 19 +- apps/buddy/src/shared/lib/localChatError.ts | 6 + .../shared/ui/markdown/ChatMarkdownBody.vue | 171 ++ .../ui/markdown/DesktopMarkdownContent.vue | 153 +- apps/buddy/src/workbench/common/workbench.ts | 4 +- apps/buddy/tsconfig.json | 3 +- apps/buddy/tsconfig.node.json | 7 +- apps/buddy/tsconfig.service.json | 9 + apps/mobile/.gitignore | 10 + apps/mobile/README.md | 16 + apps/mobile/android/build.gradle.kts | 138 ++ apps/mobile/android/proguard-rules.pro | 1 + .../mobile/android/release-certificate.sha256 | 1 + .../src/androidTest/AndroidManifest.xml | 12 + .../__tests__/FileReceiverActivity.java | 82 + .../__tests__/FileWorkflowProvider.java | 73 + .../__tests__/ApprovalUiInstrumentedTest.kt | 84 + .../AttachmentComposerInstrumentedTest.kt | 122 ++ .../__tests__/AvatarInstrumentedTest.kt | 158 ++ .../BranchParallelInstrumentedTest.kt | 430 +++++ .../ConnectionSessionInstrumentedTest.kt | 75 + .../__tests__/ContentInstrumentedTest.kt | 92 + .../DesktopApprovalInstrumentedTest.kt | 162 ++ .../DesktopWorkflowInstrumentedTest.kt | 142 ++ .../DeviceRegistryInstrumentedTest.kt | 78 + .../__tests__/DeviceSwitchInstrumentedTest.kt | 367 ++++ .../__tests__/DraftUiInstrumentedTest.kt | 156 ++ .../__tests__/FileWorkflowInstrumentedTest.kt | 146 ++ .../__tests__/HistoryInstrumentedTest.kt | 143 ++ .../__tests__/InterruptionInstrumentedTest.kt | 131 ++ .../mobile/__tests__/LegacyDraftFixture.kt | 50 + .../__tests__/LiveApprovalInstrumentedTest.kt | 113 ++ .../LiveAttachmentConflictInstrumentedTest.kt | 148 ++ .../LiveConnectionInstrumentedTest.kt | 43 + .../__tests__/LiveFilesInstrumentedTest.kt | 122 ++ .../__tests__/LiveLogoutInstrumentedTest.kt | 50 + .../__tests__/LiveServiceInstrumentedTest.kt | 83 + .../__tests__/LiveSpaceInstrumentedTest.kt | 64 + .../__tests__/LocalFilesInstrumentedTest.kt | 207 +++ .../__tests__/LocalInputInstrumentedTest.kt | 69 + .../__tests__/LocalizationInstrumentedTest.kt | 75 + .../__tests__/MaterialFlowInstrumentedTest.kt | 97 + .../__tests__/MobileFlowUiInstrumentedTest.kt | 58 + .../mobile/__tests__/MobileUiTestDriver.kt | 278 +++ .../__tests__/NativeCameraInstrumentedTest.kt | 73 + .../PairingLifecycleInstrumentedTest.kt | 60 + .../PaperWorkbenchInstrumentedTest.kt | 256 +++ .../PhotoRecoveryInstrumentedTest.kt | 87 + .../__tests__/PreviewInstrumentedTest.kt | 134 ++ .../ProcessRecoveryInstrumentedTest.kt | 137 ++ .../QuestionAnswerInstrumentedTest.kt | 283 +++ .../__tests__/RecoveryInstrumentedTest.kt | 173 ++ .../__tests__/SharedReaderInstrumentedTest.kt | 378 ++++ .../__tests__/SubscriptionInstrumentedTest.kt | 171 ++ .../__tests__/SystemFileInstrumentedTest.kt | 166 ++ .../__tests__/TaskControlsInstrumentedTest.kt | 198 ++ .../TaskOperationOwnershipInstrumentedTest.kt | 130 ++ .../android/src/main/AndroidManifest.xml | 32 + .../app/uselexora/mobile/MobileApplication.kt | 13 + .../uselexora/mobile/i18n/MobileStrings.kt | 44 + .../mobile/network/ApprovalDocuments.kt | 50 + .../mobile/network/MobileConnection.kt | 126 ++ .../uselexora/mobile/network/MobileFiles.kt | 283 +++ .../uselexora/mobile/network/PairingClient.kt | 67 + .../mobile/network/PinnedTransport.kt | 74 + .../uselexora/mobile/network/ProductClient.kt | 118 ++ .../mobile/preview/PreviewActivity.kt | 169 ++ .../uselexora/mobile/preview/PreviewClient.kt | 88 + .../mobile/preview/PreviewDocumentView.kt | 95 + .../uselexora/mobile/preview/PreviewPolicy.kt | 35 + .../mobile/preview/PreviewService.kt | 113 ++ .../uselexora/mobile/preview/ZoomImageView.kt | 51 + .../mobile/protocol/ApprovalReview.kt | 64 + .../mobile/protocol/CommandOutbox.kt | 43 + .../mobile/protocol/CommandRecovery.kt | 26 + .../mobile/protocol/ConversationTarget.kt | 43 + .../uselexora/mobile/protocol/DraftContext.kt | 41 + .../mobile/protocol/ExecutionSettings.kt | 30 + .../mobile/protocol/MobileContentAdapter.kt | 77 + .../mobile/protocol/MobileInputCommands.kt | 34 + .../protocol/MobileMessagePresentation.kt | 145 ++ .../mobile/protocol/ProductProjection.kt | 63 + .../mobile/protocol/ProductProtocol.kt | 44 + .../mobile/protocol/ProductSubscription.kt | 151 ++ .../mobile/protocol/ProtocolSchemas.kt | 92 + .../uselexora/mobile/protocol/ServiceTime.kt | 10 + .../mobile/session/MobileComposerSession.kt | 102 ++ .../mobile/session/MobileConnectionSession.kt | 171 ++ .../mobile/session/MobileOperations.kt | 40 + .../mobile/session/MobileTaskReadModel.kt | 171 ++ .../uselexora/mobile/storage/AvatarCache.kt | 53 + .../mobile/storage/DeviceRegistry.kt | 147 ++ .../mobile/storage/DocumentExport.kt | 26 + .../uselexora/mobile/storage/FileBudget.kt | 28 + .../uselexora/mobile/storage/LocalFiles.kt | 73 + .../uselexora/mobile/storage/LocalStore.kt | 207 +++ .../mobile/storage/MaterialDrafts.kt | 25 + .../uselexora/mobile/storage/PhotoStore.kt | 38 + .../app/uselexora/mobile/storage/ReadCache.kt | 46 + .../uselexora/mobile/storage/SecretStore.kt | 72 + .../uselexora/mobile/storage/TransferStore.kt | 17 + .../uselexora/mobile/ui/AppearanceSettings.kt | 64 + .../app/uselexora/mobile/ui/ApprovalDialog.kt | 104 ++ .../uselexora/mobile/ui/ComposerToolbar.kt | 83 + .../mobile/ui/ConversationMessageMenu.kt | 38 + .../mobile/ui/ConversationSettings.kt | 170 ++ .../uselexora/mobile/ui/DraftRecoveryPanel.kt | 52 + .../uselexora/mobile/ui/IncomingMaterial.kt | 32 + .../mobile/ui/IncomingMaterialPanel.kt | 53 + .../app/uselexora/mobile/ui/LexoraTheme.kt | 88 + .../uselexora/mobile/ui/LocalFilesPanel.kt | 73 + .../app/uselexora/mobile/ui/MainActivity.kt | 1627 +++++++++++++++++ .../uselexora/mobile/ui/MessageTimeline.kt | 279 +++ .../uselexora/mobile/ui/MobileAppearance.kt | 29 + .../mobile/ui/MobileFailurePresentation.kt | 44 + .../app/uselexora/mobile/ui/MobileMarkdown.kt | 205 +++ .../mobile/ui/MobileModelControls.kt | 38 + .../uselexora/mobile/ui/MobileQuestions.kt | 219 +++ .../mobile/ui/MobileReaderBenchmark.kt | 73 + .../mobile/ui/MobileReadingPolicy.kt | 100 + .../uselexora/mobile/ui/MobileReviewWindow.kt | 12 + .../mobile/ui/MobileRunTranscripts.kt | 76 + .../app/uselexora/mobile/ui/MobileShell.kt | 790 ++++++++ .../uselexora/mobile/ui/MobileTaskFilters.kt | 34 + .../app/uselexora/mobile/ui/MobileTheme.kt | 17 + .../app/uselexora/mobile/ui/MobileUiState.kt | 126 ++ .../app/uselexora/mobile/ui/PaintedDecor.kt | 64 + .../mobile/ui/PairingCaptureActivity.kt | 11 + .../app/uselexora/mobile/ui/PaperMaterial.kt | 244 +++ .../app/uselexora/mobile/ui/PaperWorkbench.kt | 281 +++ .../app/uselexora/mobile/ui/ReaderAvatars.kt | 119 ++ .../uselexora/mobile/ui/ReaderProjection.kt | 119 ++ .../app/uselexora/mobile/ui/ServiceIcon.kt | 13 + .../uselexora/mobile/ui/ServiceSwitcher.kt | 104 ++ .../app/uselexora/mobile/ui/TaskFilesPanel.kt | 57 + .../app/uselexora/mobile/ui/TaskOperations.kt | 214 +++ .../uselexora/mobile/ui/WebMessageTimeline.kt | 287 +++ .../main/res/drawable-nodpi/paper_fiber.webp | Bin 0 -> 119158 bytes .../drawable-nodpi/workbench_companion.png | Bin 0 -> 2021149 bytes .../src/main/res/drawable/ic_service.xml | 26 + .../main/res/drawable/ic_service_linux.xml | 3 + .../main/res/drawable/ic_service_macos.xml | 3 + .../main/res/drawable/ic_service_windows.xml | 3 + .../res/drawable/mobile_dialog_background.xml | 7 + .../src/main/res/values-night-v27/styles.xml | 6 + .../src/main/res/values-night/styles.xml | 28 + .../src/main/res/values-v27/styles.xml | 6 + .../src/main/res/values-zh/strings.xml | 772 ++++++++ .../android/src/main/res/values/attrs.xml | 6 + .../android/src/main/res/values/strings.xml | 786 ++++++++ .../android/src/main/res/values/styles.xml | 28 + .../main/res/xml/data_extraction_rules.xml | 4 + .../android/src/main/res/xml/file_paths.xml | 1 + .../mobile/__tests__/ApprovalReviewTest.kt | 85 + .../mobile/__tests__/AvatarCacheTest.kt | 43 + .../mobile/__tests__/ContentBehaviorTest.kt | 39 + .../mobile/__tests__/DraftContextTest.kt | 25 + .../mobile/__tests__/FileBudgetTest.kt | 36 + .../__tests__/MobileLocalizationTest.kt | 55 + .../MobileMessagePresentationTest.kt | 88 + .../mobile/__tests__/MobileOperationsTest.kt | 62 + .../mobile/__tests__/PinnedTransportTest.kt | 87 + .../mobile/__tests__/PreviewPolicyTest.kt | 29 + .../mobile/__tests__/ProductProjectionTest.kt | 42 + .../mobile/__tests__/ProductProtocolTest.kt | 66 + .../mobile/__tests__/ReadCacheTest.kt | 38 + .../mobile/__tests__/TestMobileStrings.kt | 29 + .../storage/__tests__/DeviceDirectoryTest.kt | 143 ++ .../ui/__tests__/MobileReadingPolicyTest.kt | 16 + .../ui/__tests__/ReaderProjectionTest.kt | 49 + apps/mobile/android/version.properties | 2 + apps/mobile/build.gradle.kts | 4 + apps/mobile/gradle.properties | 4 + apps/mobile/gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 45457 bytes .../gradle/wrapper/gradle-wrapper.properties | 8 + apps/mobile/gradlew | 248 +++ apps/mobile/gradlew.bat | 93 + apps/mobile/settings.gradle.kts | 7 + .../tools/__tests__/localization.spec.mjs | 48 + .../tools/__tests__/protocol-schema.spec.mjs | 9 + apps/mobile/tools/export-protocol.mjs | 55 + apps/mobile/tools/protocol-schema.mjs | 56 + apps/mobile/tools/protocol-source.ts | 15 + apps/website/src/en/plugins/permissions.md | 8 +- apps/website/src/plugins/permissions.md | 8 +- eslint.config.js | 2 +- package.json | 2 +- packages/assets/README.md | 1 + packaging/buddy/ci/run-gui-smoke.mjs | 1 + packaging/buddy/release/build-service.mjs | 99 + packaging/buddy/release/node-runtime.mjs | 81 + .../buddy/release/product-release-set.mjs | 106 ++ packaging/buddy/release/verify-package.mjs | 1 + packaging/buddy/windows/installer.nsh | 12 + pnpm-lock.yaml | 243 +++ pnpm-workspace.yaml | 1 + 890 files changed, 64139 insertions(+), 11360 deletions(-) create mode 100644 .github/workflows/mobile-build.yml create mode 100644 .playwright/scripts/__tests__/attachment-publication.e2e.mjs create mode 100644 .playwright/scripts/__tests__/browser-platform-lifetime.e2e.mjs create mode 100644 .playwright/scripts/__tests__/commandCapacity.ts create mode 100644 .playwright/scripts/__tests__/connections.e2e.mjs create mode 100644 .playwright/scripts/__tests__/local-service-lifecycle.e2e.mjs create mode 100644 .playwright/scripts/__tests__/mobile-approvals.e2e.mjs create mode 100644 .playwright/scripts/__tests__/mobile-branches.e2e.mjs create mode 100644 .playwright/scripts/__tests__/mobile-content.e2e.mjs create mode 100644 .playwright/scripts/__tests__/mobile-desktop.e2e.mjs create mode 100644 .playwright/scripts/__tests__/mobile-devices.e2e.mjs create mode 100644 .playwright/scripts/__tests__/mobile-history.e2e.mjs create mode 100644 .playwright/scripts/__tests__/mobile-interruption.e2e.mjs create mode 100644 .playwright/scripts/__tests__/mobile-questions.e2e.mjs create mode 100644 .playwright/scripts/__tests__/mobile-recovery.e2e.mjs create mode 100644 .playwright/scripts/__tests__/mobile-task-controls.e2e.mjs create mode 100644 .playwright/scripts/__tests__/persistence-upgrade.e2e.mjs create mode 100644 .playwright/scripts/__tests__/prepareProductProfile.ts create mode 100644 .playwright/scripts/__tests__/product-desktop.e2e.mjs create mode 100644 .playwright/scripts/__tests__/service-process.e2e.mjs create mode 100644 .playwright/scripts/__tests__/storageCrash.ts create mode 100644 .playwright/scripts/__tests__/storageDurability.ts create mode 100644 .playwright/scripts/__tests__/storageFixture.ts create mode 100644 .playwright/scripts/fixtures/attachment-publication-crash.ts create mode 100644 .playwright/scripts/fixtures/local-service.mjs create mode 100644 .playwright/scripts/fixtures/mobile-desktop.mjs create mode 100644 .playwright/scripts/fixtures/service-process-harness.ts create mode 100644 .playwright/scripts/mobile/__tests__/process-recovery.e2e.mjs create mode 100644 .playwright/scripts/mobile/capture-android-performance.mjs create mode 100644 .playwright/scripts/mobile/capture-android-streaming.mjs create mode 100644 .playwright/scripts/private-network-fixture.mjs create mode 100644 .playwright/scripts/storage-durability.mjs create mode 100644 apps/buddy/electron/dev.mjs create mode 100644 apps/buddy/electron/main/__tests__/conversationIpc.spec.ts create mode 100644 apps/buddy/electron/main/app/__tests__/requestDesktopTarget.spec.ts create mode 100644 apps/buddy/electron/main/app/desktopServiceEntry.ts create mode 100644 apps/buddy/electron/main/app/requestDesktopTarget.ts create mode 100644 apps/buddy/electron/main/browser/BrowserNavigation.ts create mode 100644 apps/buddy/electron/main/browser/BrowserObservationStore.ts create mode 100644 apps/buddy/electron/main/browser/BrowserPresentationHost.ts create mode 100644 apps/buddy/electron/main/browser/__tests__/BrowserPresentationHost.spec.ts create mode 100644 apps/buddy/electron/main/local-chat/privateNetwork.ts create mode 100644 apps/buddy/electron/main/productProtocol/DesktopProductCommands.ts create mode 100644 apps/buddy/electron/main/productProtocol/DesktopTaskSynchronization.ts create mode 100644 apps/buddy/electron/main/productProtocol/ProductDesktopGateway.ts create mode 100644 apps/buddy/electron/main/productProtocol/ProductOutboxStore.ts create mode 100644 apps/buddy/electron/main/productProtocol/ProductRpcClient.ts create mode 100644 apps/buddy/electron/main/productProtocol/ProductSyncClient.ts create mode 100644 apps/buddy/electron/main/productProtocol/RendererDraftStore.ts create mode 100644 apps/buddy/electron/main/productProtocol/__tests__/DesktopProductCommands.spec.ts create mode 100644 apps/buddy/electron/main/productProtocol/__tests__/ProductRpcClient.spec.ts create mode 100644 apps/buddy/electron/main/productProtocol/__tests__/ProductSyncClient.spec.ts create mode 100644 apps/buddy/electron/main/productProtocol/__tests__/RendererDraftStore.spec.ts create mode 100644 apps/buddy/electron/main/productProtocol/registerDraftRecoveryIpc.ts create mode 100644 apps/buddy/electron/main/runtime/RemoteServiceClient.ts create mode 100644 apps/buddy/electron/main/runtime/platformHostBootstrap.ts create mode 100644 apps/buddy/electron/main/runtime/serviceProcess.ts rename apps/buddy/{service/src => electron/main}/sandbox/sandboxProcess.ts (90%) create mode 100644 apps/buddy/electron/main/storage/__tests__/profileWrites.spec.ts create mode 100644 apps/buddy/electron/main/storage/__tests__/requestPersistenceUpgrade.spec.ts create mode 100644 apps/buddy/electron/main/storage/profileWrites.ts create mode 100644 apps/buddy/electron/main/storage/requestPersistenceUpgrade.ts create mode 100644 apps/buddy/electron/preload/local-chat/draftRecovery.ts create mode 100644 apps/buddy/electron/preload/local-chat/privateNetwork.ts create mode 100644 apps/buddy/electron/shared/privateNetworkApi.ts delete mode 100644 apps/buddy/eslint/__tests__/dependency-boundaries.spec.mjs create mode 100644 apps/buddy/mobile-reader.vite.config.ts create mode 100644 apps/buddy/native/host/__tests__/process_identity_macos.rs create mode 100644 apps/buddy/native/host/__tests__/runtime_guard.windows.mjs create mode 100644 apps/buddy/native/host/__tests__/service_launcher.rs create mode 100644 apps/buddy/native/host/src/process_control/macos.rs create mode 100644 apps/buddy/native/host/src/service_launcher.rs create mode 100644 apps/buddy/native/pet/src/native_pet/window_draw.rs create mode 100644 apps/buddy/native/pet/src/native_pet/window_pointer.rs create mode 100644 apps/buddy/native/pet/src/native_pet/window_runtime.rs create mode 100644 apps/buddy/platform/extensions/ExtensionDownloads.ts create mode 100644 apps/buddy/platform/extensions/ExtensionInferenceRequests.ts create mode 100644 apps/buddy/platform/extensions/ExtensionInvocationScope.ts create mode 100644 apps/buddy/platform/extensions/ExtensionSelectedResources.ts create mode 100644 apps/buddy/platform/extensions/ExtensionUploads.ts create mode 100644 apps/buddy/platform/extensions/ExtensionViewExports.ts create mode 100644 apps/buddy/platform/extensions/__tests__/ExtensionExecutionScope.spec.ts create mode 100644 apps/buddy/platform/extensions/__tests__/ExtensionLifetime.spec.ts create mode 100644 apps/buddy/platform/native/nodeRuntime.json create mode 100644 apps/buddy/platform/process/serviceEnvironment.ts create mode 100644 apps/buddy/platform/service/HostConnection.ts create mode 100644 apps/buddy/platform/service/LocalServiceServer.ts create mode 100644 apps/buddy/platform/service/ServiceChildProcess.ts create mode 100644 apps/buddy/platform/service/ServiceProfileOwner.ts create mode 100644 apps/buddy/platform/service/ServiceRpcChannel.ts create mode 100644 apps/buddy/platform/service/SessionServiceRegistration.ts create mode 100644 apps/buddy/platform/service/SocketFrames.ts create mode 100644 apps/buddy/platform/service/__tests__/ServiceChildProcess.spec.ts create mode 100644 apps/buddy/platform/service/__tests__/localService.spec.ts create mode 100644 apps/buddy/platform/service/connectLocalService.ts create mode 100644 apps/buddy/platform/service/launchLocalService.ts create mode 100644 apps/buddy/platform/service/releaseSet.ts create mode 100644 apps/buddy/platform/service/serviceFiles.ts create mode 100644 apps/buddy/platform/service/serviceProcessIdentity.ts create mode 100644 apps/buddy/platform/storage/productIdentity.ts create mode 100644 apps/buddy/service.vite.config.ts create mode 100644 apps/buddy/service/assets.d.ts create mode 100644 apps/buddy/service/src/LocalServiceDaemon.ts create mode 100644 apps/buddy/service/src/LocalServiceSupervisor.ts create mode 100644 apps/buddy/service/src/__tests__/LocalServiceDaemon.spec.ts create mode 100644 apps/buddy/service/src/agent/execution/RuntimeExecutionLifecycle.ts create mode 100644 apps/buddy/service/src/agent/sessions/PiRequestMaterializer.ts create mode 100644 apps/buddy/service/src/agent/sessions/PiSessionMailbox.ts create mode 100644 apps/buddy/service/src/agent/sessions/withPiSessionStorageBoundary.ts create mode 100644 apps/buddy/service/src/approvals/ApprovalCommandService.ts create mode 100644 apps/buddy/service/src/approvals/__tests__/ApprovalCommands.spec.ts create mode 100644 apps/buddy/service/src/approvals/approvalReview.ts create mode 100644 apps/buddy/service/src/attachments/AttachmentError.ts create mode 100644 apps/buddy/service/src/attachments/AttachmentInputMaterializer.ts create mode 100644 apps/buddy/service/src/attachments/AttachmentInputReader.ts create mode 100644 apps/buddy/service/src/attachments/InputMaterialProjector.ts create mode 100644 apps/buddy/service/src/attachments/__tests__/AttachmentPersistence.spec.ts create mode 100644 apps/buddy/service/src/attachments/attachmentMetadata.ts delete mode 100644 apps/buddy/service/src/automations/__tests__/registerAutomationRpc.spec.ts create mode 100644 apps/buddy/service/src/chat/ChatControlCommandService.ts create mode 100644 apps/buddy/service/src/chat/ChatInputCommandService.ts create mode 100644 apps/buddy/service/src/chat/ChatInputMaterializer.ts create mode 100644 apps/buddy/service/src/chat/ChatInputPreparation.ts create mode 100644 apps/buddy/service/src/chat/ComposerDraftCommandService.ts create mode 100644 apps/buddy/service/src/chat/PreparedResourceSettlement.ts create mode 100644 apps/buddy/service/src/chat/PreparedRunCommandService.ts create mode 100644 apps/buddy/service/src/chat/__tests__/ChatInputCommandService.spec.ts create mode 100644 apps/buddy/service/src/chat/__tests__/ComposerDraftCommandService.spec.ts create mode 100644 apps/buddy/service/src/chat/__tests__/PreparedResourceSettlement.spec.ts create mode 100644 apps/buddy/service/src/chat/__tests__/PreparedRunCommandService.spec.ts delete mode 100644 apps/buddy/service/src/chat/__tests__/persistPreparedTurn.spec.ts create mode 100644 apps/buddy/service/src/chat/resolveChatInput.ts create mode 100644 apps/buddy/service/src/commands/CommandAdmissionError.ts create mode 100644 apps/buddy/service/src/commands/CommandAdmissionService.ts create mode 100644 apps/buddy/service/src/commands/DesktopOpenCommandService.ts create mode 100644 apps/buddy/service/src/commands/__tests__/CommandAdmissionService.spec.ts create mode 100644 apps/buddy/service/src/commands/__tests__/DesktopOpenCommandService.spec.ts create mode 100644 apps/buddy/service/src/commands/__tests__/commandIntent.spec.ts create mode 100644 apps/buddy/service/src/commands/commandIntent.ts create mode 100644 apps/buddy/service/src/connectors/mcp/McpCredentialChanges.ts create mode 100644 apps/buddy/service/src/connectors/mcp/__tests__/McpCredentialChanges.spec.ts create mode 100644 apps/buddy/service/src/conversations/ConversationCommandService.ts create mode 100644 apps/buddy/service/src/conversations/__tests__/ConversationCommandService.spec.ts create mode 100644 apps/buddy/service/src/events/SqliteRunEventLog.ts create mode 100644 apps/buddy/service/src/events/SqliteRunEventWriter.ts create mode 100644 apps/buddy/service/src/events/__tests__/SqliteRunEventLog.spec.ts create mode 100644 apps/buddy/service/src/events/__tests__/SqliteRunEventWriter.spec.ts create mode 100644 apps/buddy/service/src/events/prepareEventPayload.ts create mode 100644 apps/buddy/service/src/platform/HostConnectorSecretStore.ts create mode 100644 apps/buddy/service/src/platform/RuntimePlatform.ts create mode 100644 apps/buddy/service/src/platform/RuntimePlatformLifecycle.ts create mode 100644 apps/buddy/service/src/platform/__tests__/runtimePlatform.spec.ts create mode 100644 apps/buddy/service/src/platform/createRpcRuntimePlatform.ts create mode 100644 apps/buddy/service/src/platform/nodeServiceConfig.ts create mode 100644 apps/buddy/service/src/platform/prepareRuntimeEnvironment.ts create mode 100644 apps/buddy/service/src/plugins/PluginHost.ts create mode 100644 apps/buddy/service/src/privateNetwork/DeviceAuthorizationService.ts create mode 100644 apps/buddy/service/src/privateNetwork/PrivateNetworkController.ts create mode 100644 apps/buddy/service/src/privateNetwork/PrivateNetworkServer.ts create mode 100644 apps/buddy/service/src/privateNetwork/PrivateNetworkTlsIdentity.ts create mode 100644 apps/buddy/service/src/privateNetwork/RemoteAvatarService.ts create mode 100644 apps/buddy/service/src/privateNetwork/RemoteResourceService.ts create mode 100644 apps/buddy/service/src/privateNetwork/RemoteUploadService.ts create mode 100644 apps/buddy/service/src/privateNetwork/__tests__/DeviceAuthorizationService.spec.ts create mode 100644 apps/buddy/service/src/privateNetwork/__tests__/PrivateNetworkController.spec.ts create mode 100644 apps/buddy/service/src/privateNetwork/__tests__/PrivateNetworkServer.spec.ts create mode 100644 apps/buddy/service/src/privateNetwork/__tests__/RemoteAvatarService.spec.ts create mode 100644 apps/buddy/service/src/privateNetwork/__tests__/RemoteResourceService.spec.ts create mode 100644 apps/buddy/service/src/privateNetwork/__tests__/RemoteUploadService.spec.ts create mode 100644 apps/buddy/service/src/privateNetwork/__tests__/privateNetworkAddress.spec.ts create mode 100644 apps/buddy/service/src/privateNetwork/privateNetworkAddress.ts create mode 100644 apps/buddy/service/src/privateNetwork/revokeRestoredDeviceGrants.ts create mode 100644 apps/buddy/service/src/privateNetwork/uploadValidation.ts create mode 100644 apps/buddy/service/src/productProtocol/ProductActorCommandScope.ts create mode 100644 apps/buddy/service/src/productProtocol/ProductBuddyAvatars.ts create mode 100644 apps/buddy/service/src/productProtocol/ProductConnection.ts create mode 100644 apps/buddy/service/src/productProtocol/ProductContentReferences.ts create mode 100644 apps/buddy/service/src/productProtocol/ProductDraftReadModel.ts create mode 100644 apps/buddy/service/src/productProtocol/ProductPageReader.ts create mode 100644 apps/buddy/service/src/productProtocol/ProductQueries.ts create mode 100644 apps/buddy/service/src/productProtocol/ProductQueryRevisions.ts create mode 100644 apps/buddy/service/src/productProtocol/ProductReadViews.ts create mode 100644 apps/buddy/service/src/productProtocol/ProductStreamingMessages.ts create mode 100644 apps/buddy/service/src/productProtocol/ProductSyncPublisher.ts create mode 100644 apps/buddy/service/src/productProtocol/ProductTaskReadModel.ts create mode 100644 apps/buddy/service/src/productProtocol/__tests__/ConversationArchiveCommands.spec.ts create mode 100644 apps/buddy/service/src/productProtocol/__tests__/ProductConnection.spec.ts create mode 100644 apps/buddy/service/src/productProtocol/__tests__/ProductPersistence.spec.ts create mode 100644 apps/buddy/service/src/productProtocol/__tests__/ProductQueryRevisions.spec.ts create mode 100644 apps/buddy/service/src/productProtocol/__tests__/ProductServiceConnections.spec.ts create mode 100644 apps/buddy/service/src/productProtocol/__tests__/ProductSyncPublisher.spec.ts create mode 100644 apps/buddy/service/src/productProtocol/__tests__/ProductTaskReads.spec.ts create mode 100644 apps/buddy/service/src/productProtocol/createProductCommandHandlers.ts create mode 100644 apps/buddy/service/src/productProtocol/createProductModelQueries.ts create mode 100644 apps/buddy/service/src/productProtocol/createProductSynchronization.ts create mode 100644 apps/buddy/service/src/productProtocol/projectCommandReceipt.ts create mode 100644 apps/buddy/service/src/productProtocol/registerLocalProductConnection.ts create mode 100644 apps/buddy/service/src/providers/__tests__/providerModelReconciliation.spec.ts create mode 100644 apps/buddy/service/src/providers/providerModelReconciliation.ts create mode 100644 apps/buddy/service/src/providers/providerModelSource.ts create mode 100644 apps/buddy/service/src/questions/__tests__/ProductQuestionCommands.spec.ts create mode 100644 apps/buddy/service/src/rpc/ServiceClientConnections.ts create mode 100644 apps/buddy/service/src/rpc/__tests__/ServiceClientConnections.spec.ts create mode 100644 apps/buddy/service/src/rpc/__tests__/serviceConnectionHarness.ts create mode 100644 apps/buddy/service/src/rpc/candidateLegacyRegistrar.ts create mode 100644 apps/buddy/service/src/rpc/registerServiceManagementRpc.ts create mode 100644 apps/buddy/service/src/runs/__tests__/recoverSqliteRuns.spec.ts create mode 100644 apps/buddy/service/src/runs/recoverSqliteRuns.ts create mode 100644 apps/buddy/service/src/startServiceRuntime.ts create mode 100644 apps/buddy/service/src/storage/ChatControlCommandRepository.ts create mode 100644 apps/buddy/service/src/storage/CommandCommitter.ts create mode 100644 apps/buddy/service/src/storage/ConversationCommandRepository.ts create mode 100644 apps/buddy/service/src/storage/ExecutionResourceQueries.ts create mode 100644 apps/buddy/service/src/storage/StorageIsolation.ts create mode 100644 apps/buddy/service/src/storage/__tests__/ExecutionResourceQueries.spec.ts create mode 100644 apps/buddy/service/src/storage/__tests__/StorageIsolation.spec.ts delete mode 100644 apps/buddy/service/src/storage/__tests__/archiveBackup.spec.ts create mode 100644 apps/buddy/service/src/storage/__tests__/branchParallelExecution.spec.ts create mode 100644 apps/buddy/service/src/storage/__tests__/checkpointSqliteStorage.spec.ts create mode 100644 apps/buddy/service/src/storage/__tests__/contentReferenceOwnership.spec.ts create mode 100644 apps/buddy/service/src/storage/__tests__/conversationArchivePurge.spec.ts delete mode 100644 apps/buddy/service/src/storage/__tests__/conversationArchiveRepository.spec.ts delete mode 100644 apps/buddy/service/src/storage/__tests__/databaseTransaction.spec.ts create mode 100644 apps/buddy/service/src/storage/__tests__/draftOwnershipRepository.spec.ts create mode 100644 apps/buddy/service/src/storage/__tests__/persistenceBackup.spec.ts create mode 100644 apps/buddy/service/src/storage/__tests__/persistenceMigration.spec.ts create mode 100644 apps/buddy/service/src/storage/__tests__/sqliteRunFixture.ts create mode 100644 apps/buddy/service/src/storage/__tests__/sqliteStartup.spec.ts create mode 100644 apps/buddy/service/src/storage/__tests__/transaction.spec.ts create mode 100644 apps/buddy/service/src/storage/__tests__/verifyCommandReceipt.spec.ts create mode 100644 apps/buddy/service/src/storage/attachmentPersistenceRepository.ts create mode 100644 apps/buddy/service/src/storage/checkpointSqliteStorage.ts create mode 100644 apps/buddy/service/src/storage/draftOwnershipRepository.ts create mode 100644 apps/buddy/service/src/storage/migrations/approvalReviewsSchema.ts create mode 100644 apps/buddy/service/src/storage/migrations/branchExecutionSchema.ts create mode 100644 apps/buddy/service/src/storage/migrations/contentReferenceOwnershipSchema.ts create mode 100644 apps/buddy/service/src/storage/migrations/conversationArchiveManagedFilesSchema.ts create mode 100644 apps/buddy/service/src/storage/migrations/draftOwnershipSchema.ts create mode 100644 apps/buddy/service/src/storage/migrations/managedFilesSchema.ts create mode 100644 apps/buddy/service/src/storage/migrations/metadataCommandsSchema.ts create mode 100644 apps/buddy/service/src/storage/migrations/persistenceCommandsSchema.ts rename apps/buddy/service/src/storage/{__tests__/experimentalPersistenceSchema.ts => migrations/persistenceIdentitySchema.ts} (98%) create mode 100644 apps/buddy/service/src/storage/migrations/privateNetworkSchema.ts create mode 100644 apps/buddy/service/src/storage/migrations/productContentReferencesSchema.ts create mode 100644 apps/buddy/service/src/storage/migrations/productQueryRevisionsSchema.ts create mode 100644 apps/buddy/service/src/storage/migrations/remoteFilesSchema.ts create mode 100644 apps/buddy/service/src/storage/migrations/resourceExportsSchema.ts create mode 100644 apps/buddy/service/src/storage/migrations/v43Persistence.ts create mode 100644 apps/buddy/service/src/storage/persistenceMigration/__tests__/persistenceCandidate.spec.ts create mode 100644 apps/buddy/service/src/storage/persistenceMigration/activatePersistenceCandidate.ts create mode 100644 apps/buddy/service/src/storage/persistenceMigration/importPersistenceCandidate.ts create mode 100644 apps/buddy/service/src/storage/persistenceMigration/legacyRunHistory.ts create mode 100644 apps/buddy/service/src/storage/persistenceMigration/persistenceSources.ts create mode 100644 apps/buddy/service/src/storage/snapshotDatabase.ts create mode 100644 apps/buddy/service/src/storage/sqliteAuthority.ts create mode 100644 apps/buddy/service/src/storage/transaction.ts create mode 100644 apps/buddy/service/src/storage/verifyCommandReceipt.ts create mode 100644 apps/buddy/service/src/validateServiceRelease.ts create mode 100644 apps/buddy/shared/conversation/__tests__/contentBehaviorVectors.spec.ts create mode 100644 apps/buddy/shared/conversation/draftRecovery.ts create mode 100644 apps/buddy/shared/conversation/submittedInput.ts delete mode 100644 apps/buddy/shared/extensions/__tests__/extensionCompatibility.spec.ts delete mode 100644 apps/buddy/shared/extensions/__tests__/extensionRequestLifecycle.spec.ts create mode 100644 apps/buddy/shared/lifecycle/AsyncOperationScope.ts create mode 100644 apps/buddy/shared/lifecycle/__tests__/AsyncOperationScope.spec.ts create mode 100644 apps/buddy/shared/lifecycle/exitBehavior.ts create mode 100644 apps/buddy/shared/permissions/conditionalApproval.ts create mode 100644 apps/buddy/shared/privateNetwork/limits.ts create mode 100644 apps/buddy/shared/privateNetwork/management.ts create mode 100644 apps/buddy/shared/privateNetwork/pairing.ts create mode 100644 apps/buddy/shared/privateNetwork/resources.ts create mode 100644 apps/buddy/shared/privateNetwork/uploads.ts create mode 100644 apps/buddy/shared/productProtocol/__tests__/protocol.spec.ts create mode 100644 apps/buddy/shared/productProtocol/avatars.ts create mode 100644 apps/buddy/shared/productProtocol/content-behavior-vectors.json create mode 100644 apps/buddy/shared/productProtocol/content.ts create mode 100644 apps/buddy/shared/productProtocol/errors.ts create mode 100644 apps/buddy/shared/productProtocol/identity.ts create mode 100644 apps/buddy/shared/productProtocol/limits.ts create mode 100644 apps/buddy/shared/productProtocol/methods.ts create mode 100644 apps/buddy/shared/productProtocol/queries.ts create mode 100644 apps/buddy/shared/productProtocol/receipts.ts create mode 100644 apps/buddy/shared/productProtocol/scalars.ts create mode 100644 apps/buddy/shared/productProtocol/sync.ts create mode 100644 apps/buddy/shared/productProtocol/topics.ts create mode 100644 apps/buddy/shared/productProtocol/transport.ts create mode 100644 apps/buddy/shared/productProtocol/vectors.json create mode 100644 apps/buddy/shared/productProtocol/views.ts create mode 100644 apps/buddy/shared/runtime/attachmentHostProtocol.ts create mode 100644 apps/buddy/shared/runtime/productRecovery.ts create mode 100644 apps/buddy/shared/service/localServiceProtocol.ts create mode 100644 apps/buddy/src/app/shell/DesktopCommandRecovery.vue create mode 100644 apps/buddy/src/app/workbench/__tests__/prepareFileClose.spec.ts delete mode 100644 apps/buddy/src/app/workbench/__tests__/registerResourceContributions.spec.ts create mode 100644 apps/buddy/src/app/workbench/__tests__/taskResource.spec.ts create mode 100644 apps/buddy/src/app/workbench/prepareFileClose.ts create mode 100644 apps/buddy/src/app/workbench/taskResource.ts create mode 100644 apps/buddy/src/app/workbench/useTaskNavigation.ts create mode 100644 apps/buddy/src/app/workbench/useWorkbenchDialogs.ts create mode 100644 apps/buddy/src/mobile-reader/DocumentPreview.vue create mode 100644 apps/buddy/src/mobile-reader/MessageAvatar.vue create mode 100644 apps/buddy/src/mobile-reader/MobileReader.vue create mode 100644 apps/buddy/src/mobile-reader/ReaderMessage.vue create mode 100644 apps/buddy/src/mobile-reader/ReaderMessageActions.vue create mode 100644 apps/buddy/src/mobile-reader/ReaderTerminal.vue create mode 100644 apps/buddy/src/mobile-reader/ReaderToolCall.vue create mode 100644 apps/buddy/src/mobile-reader/ReaderTurn.vue create mode 100644 apps/buddy/src/mobile-reader/ReaderTurnContent.vue create mode 100644 apps/buddy/src/mobile-reader/__tests__/readerProjection.spec.ts create mode 100644 apps/buddy/src/mobile-reader/bootstrap.ts create mode 100644 apps/buddy/src/mobile-reader/document-bootstrap.ts create mode 100644 apps/buddy/src/mobile-reader/document.html create mode 100644 apps/buddy/src/mobile-reader/document.ts create mode 100644 apps/buddy/src/mobile-reader/index.html create mode 100644 apps/buddy/src/mobile-reader/main.ts create mode 100644 apps/buddy/src/mobile-reader/markdownStrings.ts create mode 100644 apps/buddy/src/mobile-reader/polyfills.ts create mode 100644 apps/buddy/src/mobile-reader/reader.css create mode 100644 apps/buddy/src/mobile-reader/readerBridge.ts create mode 100644 apps/buddy/src/mobile-reader/readerProjection.ts create mode 100644 apps/buddy/src/mobile-reader/readerProtocol.ts create mode 100644 apps/buddy/src/mobile-reader/readerStrings.ts create mode 100644 apps/buddy/src/mobile-reader/useDocumentPreview.ts create mode 100644 apps/buddy/src/mobile-reader/useReaderAvatars.ts create mode 100644 apps/buddy/src/mobile-reader/useReaderCopyFeedback.ts create mode 100644 apps/buddy/src/mobile-reader/useReaderResultRead.ts create mode 100644 apps/buddy/src/mobile-reader/useReaderTurns.ts create mode 100644 apps/buddy/src/mobile-reader/useReaderViewport.ts create mode 100644 apps/buddy/src/modules/buddies/reader.ts create mode 100644 apps/buddy/src/modules/settings/pages/DesktopConnectionsSettingsView.vue create mode 100644 apps/buddy/src/modules/settings/widgets/devices/DesktopPairedDevices.vue create mode 100644 apps/buddy/src/modules/settings/widgets/devices/DesktopPrivateNetworkSettings.vue create mode 100644 apps/buddy/src/modules/settings/widgets/devices/usePrivateNetworkSettings.ts create mode 100644 apps/buddy/src/modules/tasks/model/approvals/__tests__/approvalDecisions.spec.ts create mode 100644 apps/buddy/src/modules/tasks/model/approvals/approvalDecisions.ts create mode 100644 apps/buddy/src/modules/tasks/model/composer/actorInputTarget.ts create mode 100644 apps/buddy/src/modules/tasks/reader.ts create mode 100644 apps/buddy/src/modules/tasks/state/context-panel/useTaskBrowserSessions.ts create mode 100644 apps/buddy/src/modules/tasks/state/drafts/__tests__/createDraftRecovery.spec.ts create mode 100644 apps/buddy/src/modules/tasks/state/drafts/createDraftRecovery.ts create mode 100644 apps/buddy/src/modules/tasks/state/runs/useTaskContextUsage.ts create mode 100644 apps/buddy/src/modules/tasks/widgets/workspace/DesktopApprovalDetails.vue create mode 100644 apps/buddy/src/modules/tasks/widgets/workspace/DesktopConversationBranches.vue create mode 100644 apps/buddy/src/platform/browser/BrowserGuestCatalog.ts create mode 100644 apps/buddy/src/shared/ui/markdown/ChatMarkdownBody.vue create mode 100644 apps/buddy/tsconfig.service.json create mode 100644 apps/mobile/.gitignore create mode 100644 apps/mobile/README.md create mode 100644 apps/mobile/android/build.gradle.kts create mode 100644 apps/mobile/android/proguard-rules.pro create mode 100644 apps/mobile/android/release-certificate.sha256 create mode 100644 apps/mobile/android/src/androidTest/AndroidManifest.xml create mode 100644 apps/mobile/android/src/androidTest/java/app/uselexora/mobile/__tests__/FileReceiverActivity.java create mode 100644 apps/mobile/android/src/androidTest/java/app/uselexora/mobile/__tests__/FileWorkflowProvider.java create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/ApprovalUiInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/AttachmentComposerInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/AvatarInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/BranchParallelInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/ConnectionSessionInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/ContentInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/DesktopApprovalInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/DesktopWorkflowInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/DeviceRegistryInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/DeviceSwitchInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/DraftUiInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/FileWorkflowInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/HistoryInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/InterruptionInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/LegacyDraftFixture.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/LiveApprovalInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/LiveAttachmentConflictInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/LiveConnectionInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/LiveFilesInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/LiveLogoutInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/LiveServiceInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/LiveSpaceInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/LocalFilesInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/LocalInputInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/LocalizationInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/MaterialFlowInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/MobileFlowUiInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/MobileUiTestDriver.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/NativeCameraInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/PairingLifecycleInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/PaperWorkbenchInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/PhotoRecoveryInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/PreviewInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/ProcessRecoveryInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/QuestionAnswerInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/RecoveryInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/SharedReaderInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/SubscriptionInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/SystemFileInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/TaskControlsInstrumentedTest.kt create mode 100644 apps/mobile/android/src/androidTest/kotlin/app/uselexora/mobile/__tests__/TaskOperationOwnershipInstrumentedTest.kt create mode 100644 apps/mobile/android/src/main/AndroidManifest.xml create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/MobileApplication.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/i18n/MobileStrings.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/network/ApprovalDocuments.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/network/MobileConnection.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/network/MobileFiles.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/network/PairingClient.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/network/PinnedTransport.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/network/ProductClient.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/preview/PreviewActivity.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/preview/PreviewClient.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/preview/PreviewDocumentView.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/preview/PreviewPolicy.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/preview/PreviewService.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/preview/ZoomImageView.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/protocol/ApprovalReview.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/protocol/CommandOutbox.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/protocol/CommandRecovery.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/protocol/ConversationTarget.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/protocol/DraftContext.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/protocol/ExecutionSettings.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/protocol/MobileContentAdapter.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/protocol/MobileInputCommands.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/protocol/MobileMessagePresentation.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/protocol/ProductProjection.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/protocol/ProductProtocol.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/protocol/ProductSubscription.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/protocol/ProtocolSchemas.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/protocol/ServiceTime.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/session/MobileComposerSession.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/session/MobileConnectionSession.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/session/MobileOperations.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/session/MobileTaskReadModel.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/storage/AvatarCache.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/storage/DeviceRegistry.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/storage/DocumentExport.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/storage/FileBudget.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/storage/LocalFiles.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/storage/LocalStore.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/storage/MaterialDrafts.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/storage/PhotoStore.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/storage/ReadCache.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/storage/SecretStore.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/storage/TransferStore.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/AppearanceSettings.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/ApprovalDialog.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/ComposerToolbar.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/ConversationMessageMenu.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/ConversationSettings.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/DraftRecoveryPanel.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/IncomingMaterial.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/IncomingMaterialPanel.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/LexoraTheme.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/LocalFilesPanel.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/MainActivity.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/MessageTimeline.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/MobileAppearance.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/MobileFailurePresentation.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/MobileMarkdown.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/MobileModelControls.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/MobileQuestions.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/MobileReaderBenchmark.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/MobileReadingPolicy.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/MobileReviewWindow.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/MobileRunTranscripts.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/MobileShell.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/MobileTaskFilters.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/MobileTheme.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/MobileUiState.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/PaintedDecor.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/PairingCaptureActivity.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/PaperMaterial.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/PaperWorkbench.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/ReaderAvatars.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/ReaderProjection.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/ServiceIcon.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/ServiceSwitcher.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/TaskFilesPanel.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/TaskOperations.kt create mode 100644 apps/mobile/android/src/main/kotlin/app/uselexora/mobile/ui/WebMessageTimeline.kt create mode 100644 apps/mobile/android/src/main/res/drawable-nodpi/paper_fiber.webp create mode 100644 apps/mobile/android/src/main/res/drawable-nodpi/workbench_companion.png create mode 100644 apps/mobile/android/src/main/res/drawable/ic_service.xml create mode 100644 apps/mobile/android/src/main/res/drawable/ic_service_linux.xml create mode 100644 apps/mobile/android/src/main/res/drawable/ic_service_macos.xml create mode 100644 apps/mobile/android/src/main/res/drawable/ic_service_windows.xml create mode 100644 apps/mobile/android/src/main/res/drawable/mobile_dialog_background.xml create mode 100644 apps/mobile/android/src/main/res/values-night-v27/styles.xml create mode 100644 apps/mobile/android/src/main/res/values-night/styles.xml create mode 100644 apps/mobile/android/src/main/res/values-v27/styles.xml create mode 100644 apps/mobile/android/src/main/res/values-zh/strings.xml create mode 100644 apps/mobile/android/src/main/res/values/attrs.xml create mode 100644 apps/mobile/android/src/main/res/values/strings.xml create mode 100644 apps/mobile/android/src/main/res/values/styles.xml create mode 100644 apps/mobile/android/src/main/res/xml/data_extraction_rules.xml create mode 100644 apps/mobile/android/src/main/res/xml/file_paths.xml create mode 100644 apps/mobile/android/src/test/kotlin/app/uselexora/mobile/__tests__/ApprovalReviewTest.kt create mode 100644 apps/mobile/android/src/test/kotlin/app/uselexora/mobile/__tests__/AvatarCacheTest.kt create mode 100644 apps/mobile/android/src/test/kotlin/app/uselexora/mobile/__tests__/ContentBehaviorTest.kt create mode 100644 apps/mobile/android/src/test/kotlin/app/uselexora/mobile/__tests__/DraftContextTest.kt create mode 100644 apps/mobile/android/src/test/kotlin/app/uselexora/mobile/__tests__/FileBudgetTest.kt create mode 100644 apps/mobile/android/src/test/kotlin/app/uselexora/mobile/__tests__/MobileLocalizationTest.kt create mode 100644 apps/mobile/android/src/test/kotlin/app/uselexora/mobile/__tests__/MobileMessagePresentationTest.kt create mode 100644 apps/mobile/android/src/test/kotlin/app/uselexora/mobile/__tests__/MobileOperationsTest.kt create mode 100644 apps/mobile/android/src/test/kotlin/app/uselexora/mobile/__tests__/PinnedTransportTest.kt create mode 100644 apps/mobile/android/src/test/kotlin/app/uselexora/mobile/__tests__/PreviewPolicyTest.kt create mode 100644 apps/mobile/android/src/test/kotlin/app/uselexora/mobile/__tests__/ProductProjectionTest.kt create mode 100644 apps/mobile/android/src/test/kotlin/app/uselexora/mobile/__tests__/ProductProtocolTest.kt create mode 100644 apps/mobile/android/src/test/kotlin/app/uselexora/mobile/__tests__/ReadCacheTest.kt create mode 100644 apps/mobile/android/src/test/kotlin/app/uselexora/mobile/__tests__/TestMobileStrings.kt create mode 100644 apps/mobile/android/src/test/kotlin/app/uselexora/mobile/storage/__tests__/DeviceDirectoryTest.kt create mode 100644 apps/mobile/android/src/test/kotlin/app/uselexora/mobile/ui/__tests__/MobileReadingPolicyTest.kt create mode 100644 apps/mobile/android/src/test/kotlin/app/uselexora/mobile/ui/__tests__/ReaderProjectionTest.kt create mode 100644 apps/mobile/android/version.properties create mode 100644 apps/mobile/build.gradle.kts create mode 100644 apps/mobile/gradle.properties create mode 100644 apps/mobile/gradle/wrapper/gradle-wrapper.jar create mode 100644 apps/mobile/gradle/wrapper/gradle-wrapper.properties create mode 100755 apps/mobile/gradlew create mode 100644 apps/mobile/gradlew.bat create mode 100644 apps/mobile/settings.gradle.kts create mode 100644 apps/mobile/tools/__tests__/localization.spec.mjs create mode 100644 apps/mobile/tools/__tests__/protocol-schema.spec.mjs create mode 100644 apps/mobile/tools/export-protocol.mjs create mode 100644 apps/mobile/tools/protocol-schema.mjs create mode 100644 apps/mobile/tools/protocol-source.ts create mode 100644 packaging/buddy/release/build-service.mjs create mode 100644 packaging/buddy/release/node-runtime.mjs create mode 100644 packaging/buddy/release/product-release-set.mjs diff --git a/.github/workflows/mobile-build.yml b/.github/workflows/mobile-build.yml new file mode 100644 index 000000000..fc9836328 --- /dev/null +++ b/.github/workflows/mobile-build.yml @@ -0,0 +1,91 @@ +name: Android package + +on: + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: android-package-${{ github.ref }} + cancel-in-progress: false + +jobs: + build: + name: Signed Android APK + runs-on: ubuntu-24.04 + environment: android-signing + timeout-minutes: 30 + steps: + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Setup pnpm + uses: pnpm/action-setup@0977fd99725f1db4007ccb2928dbb4e90d06cc86 # v6.0.10 + + - name: Setup Node + uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0 + with: + node-version-file: .node-version + + - name: Setup Java + uses: actions/setup-java@b6effb05e454b25005698d916606bdc6ffcbf961 # v5 + with: + distribution: temurin + java-version: '21' + + - name: Install workspace dependencies + uses: ./.github/actions/install-workspace-dependencies + with: + packages: '@uselexora/lexora @uselexora/lexora-buddy...' + + - name: Prepare Android SDK + run: sdkmanager 'platforms;android-36' 'build-tools;36.0.0' + + - name: Verify Android client + working-directory: apps/mobile + run: ./gradlew --no-daemon :android:testDebugUnitTest :android:lintRelease + + - name: Build signed APK + working-directory: apps/mobile + env: + LEXORA_ANDROID_KEYSTORE_BASE64: ${{ secrets.LEXORA_ANDROID_KEYSTORE_BASE64 }} + LEXORA_ANDROID_STORE_PASSWORD: ${{ secrets.LEXORA_ANDROID_STORE_PASSWORD }} + LEXORA_ANDROID_KEY_ALIAS: ${{ secrets.LEXORA_ANDROID_KEY_ALIAS }} + LEXORA_ANDROID_KEY_PASSWORD: ${{ secrets.LEXORA_ANDROID_KEY_PASSWORD }} + shell: bash + run: | + set -euo pipefail + : "${LEXORA_ANDROID_KEYSTORE_BASE64:?Missing signing keystore}" + : "${LEXORA_ANDROID_STORE_PASSWORD:?Missing keystore password}" + : "${LEXORA_ANDROID_KEY_ALIAS:?Missing signing alias}" + : "${LEXORA_ANDROID_KEY_PASSWORD:?Missing key password}" + umask 077 + export LEXORA_ANDROID_KEYSTORE="$RUNNER_TEMP/lexora-android-release.p12" + trap 'rm -f "$LEXORA_ANDROID_KEYSTORE"' EXIT + printf '%s' "$LEXORA_ANDROID_KEYSTORE_BASE64" | base64 --decode > "$LEXORA_ANDROID_KEYSTORE" + unset LEXORA_ANDROID_KEYSTORE_BASE64 + ./gradlew --no-daemon :android:assembleRelease + "$ANDROID_HOME/build-tools/36.0.0/apksigner" verify --verbose --print-certs android/build/outputs/apk/release/android-release.apk | tee android/build/outputs/apk/release/signature.txt + expected="$(cat android/release-certificate.sha256)" + grep -Fx "Signer #1 certificate SHA-256 digest: $expected" android/build/outputs/apk/release/signature.txt + cd android/build/outputs/apk/release + sha256sum android-release.apk > android-release.apk.sha256 + + - name: Remove signing material + if: always() + run: rm -f "$RUNNER_TEMP/lexora-android-release.p12" + + - name: Upload signed package + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: lexora-android-apk + path: | + apps/mobile/android/build/outputs/apk/release/android-release.apk + apps/mobile/android/build/outputs/apk/release/android-release.apk.sha256 + apps/mobile/android/build/outputs/apk/release/output-metadata.json + apps/mobile/android/build/outputs/apk/release/signature.txt + if-no-files-found: error + retention-days: 14 diff --git a/.playwright/scripts/README.md b/.playwright/scripts/README.md index 20375acbc..f5331e1d3 100644 --- a/.playwright/scripts/README.md +++ b/.playwright/scripts/README.md @@ -11,3 +11,12 @@ pnpm test:e2e `pnpm test` 执行无 GUI 的 Vitest 测试。`pnpm test:e2e` 构建当前 Electron 应用并执行 Playwright Test;可追加测试文件名或 `--grep` 筛选场景。Linux 需要 `Xvfb`,Electron 必须能启用 Chromium sandbox。 Electron 测试位于 `__tests__/*.e2e.mjs`,复用 `fixtures/electron.mjs` 的 `test`、`expect`;辅助逻辑测试使用 `__tests__/*.spec.mjs`。 + +Android 已安装调试 APK 和测试 APK、并通过 `adb` 连接后,可运行双端审批、草稿、附件、历史、进程恢复与任务控制联测: + +```sh +LEXORA_TEST_ANDROID_SERIAL=emulator-5554 pnpm test:e2e mobile-desktop.e2e.mjs mobile-approvals.e2e.mjs mobile-task-controls.e2e.mjs mobile-history.e2e.mjs mobile-recovery.e2e.mjs mobile-content.e2e.mjs mobile-interruption.e2e.mjs +# 普通 Node Core 与 Electron Host 联测需先构建配套服务 +pnpm --filter @uselexora/lexora-buddy build:desktop +LEXORA_TEST_MOBILE_SERVICE_MODE=daemon LEXORA_TEST_ANDROID_SERIAL=emulator-5554 pnpm exec playwright test --config .playwright/scripts/playwright.config.mjs mobile-desktop.e2e.mjs mobile-approvals.e2e.mjs mobile-task-controls.e2e.mjs mobile-history.e2e.mjs mobile-recovery.e2e.mjs mobile-content.e2e.mjs mobile-interruption.e2e.mjs +``` diff --git a/.playwright/scripts/__tests__/attachment-publication.e2e.mjs b/.playwright/scripts/__tests__/attachment-publication.e2e.mjs new file mode 100644 index 000000000..a16d61d94 --- /dev/null +++ b/.playwright/scripts/__tests__/attachment-publication.e2e.mjs @@ -0,0 +1,62 @@ +import { execFile } from 'node:child_process' +import fs from 'node:fs/promises' +import { createRequire, isBuiltin } from 'node:module' +import os from 'node:os' +import path from 'node:path' +import process from 'node:process' +import { fileURLToPath, pathToFileURL } from 'node:url' +import { promisify } from 'node:util' +import { expect, test } from '@playwright/test' + +const execute = promisify(execFile) +const root = fileURLToPath(new URL('../../../', import.meta.url)) +const requireBuddy = createRequire(path.join(root, 'apps/buddy/package.json')) +const checkpoints = ['prepared', 'file-synced', 'renamed', 'directory-synced', 'published', 'bound'] + +test('interrupted file publication recovers only durable attachment ownership', async () => { + const directory = await fs.mkdtemp(path.join(os.tmpdir(), 'lexora-attachment-publication-')) + const report = [] + try { + const { build } = await import(pathToFileURL(requireBuddy.resolve('vite')).href) + await build({ configFile: false, logLevel: 'error', build: { + ssr: path.join(root, '.playwright/scripts/fixtures/attachment-publication-crash.ts'), + outDir: path.join(directory, 'harness'), + target: 'node26', + rolldownOptions: { external: isBuiltin, output: { entryFileNames: 'publication.mjs', format: 'es' } }, + } }) + const entry = path.join(directory, 'harness/publication.mjs') + for (const checkpoint of checkpoints) { + const profile = path.join(directory, checkpoint) + const unknown = path.join(profile, 'profile/drafts/unknown/keep.txt') + await fs.mkdir(path.dirname(unknown), { recursive: true }) + await fs.writeFile(unknown, 'unknown files are preserved') + let interrupted + try { + await execute(process.execPath, [entry, 'publish', profile, checkpoint], { timeout: 30000 }) + } + catch (error) { interrupted = error } + expect(interrupted, checkpoint).toBeTruthy() + expect(JSON.parse(interrupted.stdout.trim())).toEqual({ checkpoint }) + const result = await execute(process.execPath, [entry, 'recover', profile], { timeout: 30000 }) + const recovery = JSON.parse(result.stdout.trim()) + const bound = checkpoint === 'bound' + expect(recovery.files).toHaveLength(1) + expect(recovery.files[0].state).toBe(bound ? 'ready' : 'deleted') + expect(recovery.references).toEqual(bound ? [{ kind: 'draft', owner_id: 'crash-draft' }] : []) + expect(recovery.bytes).toEqual(bound ? ['durable attachment bytes'] : []) + expect(recovery.preparations).toEqual([{ state: bound ? 'settled' : 'aborted', reserved_bytes: 0 }]) + expect(await fs.readFile(unknown, 'utf8')).toBe('unknown files are preserved') + const repeat = JSON.parse((await execute(process.execPath, [entry, 'recover', profile], { timeout: 30000 })).stdout.trim()) + expect(repeat.files).toEqual(recovery.files) + expect(repeat.references).toEqual(recovery.references) + expect(repeat.recovered.removedOrphanFiles).toBe(0) + report.push({ checkpoint, ...recovery }) + } + } + finally { + const reportPath = test.info().outputPath('attachment-publication.json') + await fs.writeFile(reportPath, JSON.stringify(report, null, 2)) + await test.info().attach('attachment-publication.json', { path: reportPath, contentType: 'application/json' }) + await fs.rm(directory, { recursive: true, force: true }) + } +}) diff --git a/.playwright/scripts/__tests__/browser-platform-lifetime.e2e.mjs b/.playwright/scripts/__tests__/browser-platform-lifetime.e2e.mjs new file mode 100644 index 000000000..3f97e30d1 --- /dev/null +++ b/.playwright/scripts/__tests__/browser-platform-lifetime.e2e.mjs @@ -0,0 +1,88 @@ +import { Buffer } from 'node:buffer' +import { createServer } from 'node:http' +import { expect, test } from '../fixtures/electron.mjs' + +test('platform browser survives workbench destruction, rejects stale presentation and reattaches the same interactive page', async ({ buddy }, testInfo) => { + const server = createServer((_request, response) => { + response.setHeader('Content-Type', 'text/html; charset=utf-8') + response.end('Background browser fixture
') + }) + await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)) + try { + const url = `http://127.0.0.1:${server.address().port}/` + const instance = await buddy.createInstance('browser-background') + const { app, page } = await instance.launch() + const state = await page.evaluate(async (url) => { + const state = await window.lexoraDesktop.browser.ensureSession(null, 'lifetime-test') + return window.lexoraDesktop.browser.navigate(state.sessionId, url) + }, url) + const pageId = await app.evaluate(({ webContents }, url) => webContents.getAllWebContents().find(contents => contents.getURL() === url)?.id, url) + expect(typeof pageId).toBe('number') + const bounds = { x: 40, y: 100, width: 620, height: 300 } + const oldToken = await page.evaluate(() => window.lexoraDesktop.browser.acquirePresentation()) + const presentation = { presentationId: oldToken, sessionId: state.sessionId, pageId: state.pageId, visible: true, bounds, clip: bounds } + await page.evaluate(input => window.lexoraDesktop.browser.present(input), presentation) + const initial = await app.evaluate(async ({ webContents }, id) => { + const contents = webContents.fromId(id) + contents.focus() + await contents.executeJavaScript('document.querySelector("#text").focus()') + contents.insertText(' retained') + return { text: await contents.executeJavaScript('document.querySelector("#text").value'), ticks: await contents.executeJavaScript('window.ticks'), zoom: contents.getZoomFactor(), size: (await contents.capturePage()).getSize() } + }, pageId) + expect(initial.text).toContain('retained') + expect(initial.size.width).toBeGreaterThan(0) + await page.evaluate(async sessionId => window.lexoraDesktop.browser.setZoomFactor(sessionId, 1.25), state.sessionId) + await page.evaluate(input => window.lexoraDesktop.browser.present(input), { ...presentation, visible: false }) + await app.evaluate(({ BrowserWindow }) => { + const window = BrowserWindow.getAllWindows().find(window => window.webContents.getURL().startsWith('lexora-app:')) + if (!window) + throw new Error('Workbench is missing') + setImmediate(() => window.destroy()) + }) + await expect.poll(() => app.evaluate(async ({ webContents }, id) => (await webContents.fromId(id)?.debugger.sendCommand('Runtime.evaluate', { expression: 'window.ticks', returnByValue: true }))?.result?.value, pageId)).toBeGreaterThan(initial.ticks + 2) + const offline = await app.evaluate(async ({ webContents }, id) => { + const contents = webContents.fromId(id) + await contents.debugger.sendCommand('Runtime.evaluate', { expression: 'document.querySelector("button").click()' }) + const snapshot = await contents.capturePage(undefined, { stayHidden: true }) + return { clicks: await contents.executeJavaScript('window.clicks'), text: await contents.executeJavaScript('document.querySelector("#text").value'), screenshotBytes: snapshot.toPNG().length } + }, pageId) + expect(offline).toMatchObject({ clicks: 1, text: initial.text }) + expect(offline.screenshotBytes).toBeGreaterThan(100) + const newWindow = app.waitForEvent('window') + await app.evaluate(({ app }) => app.emit('activate')) + const reopened = await newWindow + await expect(reopened.locator('.desktop-workbench-area__tasks .tiptap:visible').first()).toBeVisible() + const stale = await reopened.evaluate(async (input) => { + try { + await window.lexoraDesktop.browser.present(input) + return false + } + catch { return true } + }, presentation) + expect(stale).toBe(true) + const guests = await reopened.evaluate(() => window.lexoraDesktop.browser.listGuests()) + expect(guests).toContainEqual(expect.objectContaining({ sessionId: state.sessionId, pageId: state.pageId })) + const nextToken = await reopened.evaluate(() => window.lexoraDesktop.browser.acquirePresentation()) + await reopened.evaluate(input => window.lexoraDesktop.browser.present(input), { ...presentation, presentationId: nextToken }) + const resumed = await app.evaluate(async ({ webContents }, id) => { + const contents = webContents.fromId(id) + const button = await contents.executeJavaScript('(()=>{const r=document.querySelector("button").getBoundingClientRect();return {x:r.x+r.width/2,y:r.y+r.height/2}})()') + const point = { x: Math.round(button.x * contents.getZoomFactor()), y: Math.round(button.y * contents.getZoomFactor()) } + contents.sendInputEvent({ type: 'mouseDown', button: 'left', clickCount: 1, ...point }) + contents.sendInputEvent({ type: 'mouseUp', button: 'left', clickCount: 1, ...point }) + return { id: contents.id, zoom: contents.getZoomFactor(), text: await contents.executeJavaScript('document.querySelector("#text").value') } + }, pageId) + expect(resumed).toEqual({ id: pageId, zoom: 1.25, text: initial.text }) + await expect.poll(() => app.evaluate(({ webContents }, id) => webContents.fromId(id).executeJavaScript('window.clicks'), pageId)).toBe(2) + await expect.poll(() => app.evaluate(({ BrowserWindow }) => BrowserWindow.getAllWindows().filter(window => window.webContents.getURL().startsWith('lexora-app:')).length)).toBe(1) + const image = await app.evaluate(async ({ desktopCapturer }) => { + const [screen] = await desktopCapturer.getSources({ types: ['screen'], thumbnailSize: { width: 1500, height: 950 } }) + return screen.thumbnail.toPNG().toString('base64') + }) + await testInfo.attach('reattached-browser', { body: Buffer.from(image, 'base64'), contentType: 'image/png' }) + } + finally { + server.closeAllConnections() + await new Promise(resolve => server.close(resolve)) + } +}) diff --git a/.playwright/scripts/__tests__/browser-selection-references.e2e.mjs b/.playwright/scripts/__tests__/browser-selection-references.e2e.mjs index 3a7ed7349..121fde881 100644 --- a/.playwright/scripts/__tests__/browser-selection-references.e2e.mjs +++ b/.playwright/scripts/__tests__/browser-selection-references.e2e.mjs @@ -26,7 +26,7 @@ test('browser button picks a typed element without clicking the page, cancels sa const cards = page.locator('.desktop-chat-composer-wrap .resource-quote-card') async function nativePoint(selector, click = true) { return app.evaluate(async ({ webContents }, { url, selector, click }) => { - const guest = webContents.getAllWebContents().find(contents => contents.getType() === 'webview' && contents.getURL() === url) + const guest = webContents.getAllWebContents().find(contents => contents.getURL() === url) if (!guest) throw new Error('Fixture guest missing') const rect = await guest.executeJavaScript(`(() => { const r = document.querySelector(${JSON.stringify(selector)}).getBoundingClientRect(); return { x:r.x+r.width/2, y:r.y+r.height/2, width:innerWidth, height:innerHeight }; })()`) @@ -36,7 +36,6 @@ test('browser button picks a typed element without clicking the page, cancels sa guest.sendInputEvent({ type: 'mouseDown', button: 'left', clickCount: 1, ...point }) guest.sendInputEvent({ type: 'mouseUp', button: 'left', clickCount: 1, ...point }) } - return { x: point.x / guest.getZoomFactor() / rect.width, y: point.y / guest.getZoomFactor() / rect.height } }, { url, selector, click }) } async function waitPicker() { @@ -49,7 +48,7 @@ test('browser button picks a typed element without clicking the page, cancels sa await expect(cards.first()).toContainText('Frozen link excerpt') await expect(input).toContainText('Keep my latest question.') await expect(address).toHaveValue(url) - expect(await app.evaluate(async ({ webContents }, url) => webContents.getAllWebContents().find(contents => contents.getType() === 'webview' && contents.getURL() === url).executeJavaScript('Boolean(window.clicked)'), url)).toBe(false) + expect(await app.evaluate(async ({ webContents }, url) => webContents.getAllWebContents().find(contents => contents.getURL() === url).executeJavaScript('Boolean(window.clicked)'), url)).toBe(false) await cards.first().locator('.chat-reference-card__link').click() await expect(page.locator('.resource-quote-preview:visible')).toContainText('Frozen link excerpt') await page.getByRole('button', { name: '定位来源', exact: true }).click() @@ -73,7 +72,7 @@ test('browser button picks a typed element without clicking the page, cancels sa await waitPicker() await nativePoint('button') await expect(cards).toHaveCount(2) - expect(await app.evaluate(async ({ webContents }, url) => webContents.getAllWebContents().find(contents => contents.getType() === 'webview' && contents.getURL() === url).executeJavaScript('Boolean(window.submitted)'), url)).toBe(false) + expect(await app.evaluate(async ({ webContents }, url) => webContents.getAllWebContents().find(contents => contents.getURL() === url).executeJavaScript('Boolean(window.submitted)'), url)).toBe(false) await page.evaluate(async () => { const [guest] = await window.lexoraDesktop.browser.listGuests() await window.lexoraDesktop.browser.setZoomFactor(guest.sessionId, 1) @@ -84,7 +83,7 @@ test('browser button picks a typed element without clicking the page, cancels sa await expect(cards).toHaveCount(3) await expect(cards.last()).not.toContainText('must-not-capture') await app.evaluate(async ({ webContents }, url) => { - const guest = webContents.getAllWebContents().find(contents => contents.getType() === 'webview' && contents.getURL() === url) + const guest = webContents.getAllWebContents().find(contents => contents.getURL() === url) await guest.executeJavaScript('document.querySelector("#large").textContent="x".repeat(32769); document.querySelector("#large").style="height:24px;overflow:hidden"') }, url) await pick.click() @@ -103,63 +102,19 @@ test('browser button picks a typed element without clicking the page, cancels sa await input.click() await expect(pick).toBeVisible() await app.evaluate(async ({ webContents }, url) => { - const guest = webContents.getAllWebContents().find(contents => contents.getType() === 'webview' && contents.getURL() === url) + const guest = webContents.getAllWebContents().find(contents => contents.getURL() === url) await guest.executeJavaScript('document.querySelector("#target").textContent="New snapshot for one target"') }, url) await pick.click() await waitPicker() - const anchor = await nativePoint('#target') + await nativePoint('#target') const menu = page.locator('.resource-selection-menu.n-dropdown:visible') - const surface = page.getByTestId('browser-guest-surface') - async function checkMenuPosition(point) { - const viewport = await surface.boundingBox() - const box = await menu.boundingBox() - const windowSize = await page.evaluate(() => ({ width: innerWidth, height: innerHeight })) - const x = viewport.x + point.x * viewport.width - const y = viewport.y + point.y * viewport.height - expect(box.x).toBeGreaterThanOrEqual(0) - expect(box.y).toBeGreaterThanOrEqual(0) - expect(box.x + box.width).toBeLessThanOrEqual(windowSize.width + 1) - expect(box.y + box.height).toBeLessThanOrEqual(windowSize.height + 1) - if (x + box.width + 8 < windowSize.width) { - expect(Math.abs(box.x - x)).toBeLessThan(8) - } - else { - expect(box.x).toBeLessThanOrEqual(x + 8) - expect(box.x + box.width).toBeGreaterThanOrEqual(x - 8) - } - expect(Math.min(Math.abs(box.y - y), Math.abs(box.y + box.height - y))).toBeLessThan(16) - } await expect(menu).toBeVisible() - await checkMenuPosition(anchor) await menu.getByRole('menuitem', { name: '引用到其他对话', exact: true }).hover() const targets = page.locator('.resource-selection-menu:visible').filter({ has: page.locator('.resource-selection-menu-option__label').filter({ hasText: /^分屏 2/ }) }) await targets.getByRole('menuitem').filter({ hasText: /^分屏 2/ }).click() await expect(page.locator('.resource-quote-card').filter({ hasText: 'New snapshot for one target' })).toHaveCount(1) await expect(inputs.first()).toContainText('Keep my latest question.') - await page.evaluate(async () => { - const [guest] = await window.lexoraDesktop.browser.listGuests() - await window.lexoraDesktop.browser.setZoomFactor(guest.sessionId, 1.5) - }) - await pick.click() - await waitPicker() - const zoomAnchor = await nativePoint('#target') - await expect(menu).toBeVisible() - await checkMenuPosition(zoomAnchor) - await page.keyboard.press('Escape') - await expect(menu).not.toBeVisible() - await app.evaluate(async ({ webContents }, url) => { - const guest = webContents.getAllWebContents().find(contents => contents.getType() === 'webview' && contents.getURL() === url) - await guest.executeJavaScript('document.querySelector("#target").style="position:fixed;bottom:12px;right:12px;width:80px;height:32px;padding:0;margin:0;overflow:hidden"') - }, url) - await pick.click() - await waitPicker() - const edgeAnchor = await nativePoint('#target') - await expect(menu).toBeVisible() - await checkMenuPosition(edgeAnchor) - await page.keyboard.press('Escape') - await expect(menu).not.toBeVisible() - await expect(page.locator('.resource-quote-card').filter({ hasText: 'New snapshot for one target' })).toHaveCount(1) await pick.click() await waitPicker() await address.fill(`${url}?next`) @@ -194,7 +149,7 @@ test('native element identity survives scrolling and shadow roots, rejects repla await address.press('Enter') await expect(page.getByTestId('browser-pick-element')).toBeEnabled() const guestEval = expression => app.evaluate(async ({ webContents }, { url, expression }) => { - const guest = webContents.getAllWebContents().find(contents => contents.getType() === 'webview' && contents.getURL() === url) + const guest = webContents.getAllWebContents().find(contents => contents.getURL() === url) return guest.executeJavaScript(expression) }, { url, expression }) await expect.poll(() => guestEval('Boolean(document.querySelector("iframe")?.contentDocument?.querySelector("button"))')).toBe(true) @@ -207,7 +162,7 @@ test('native element identity survives scrolling and shadow roots, rejects repla } async function point(target, click = false) { await app.evaluate(async ({ webContents }, { url, target, click }) => { - const guest = webContents.getAllWebContents().find(contents => contents.getType() === 'webview' && contents.getURL() === url) + const guest = webContents.getAllWebContents().find(contents => contents.getURL() === url) const point = await guest.executeJavaScript(`(() => { const target = ${JSON.stringify(target)}; const frame = document.querySelector('iframe'); diff --git a/.playwright/scripts/__tests__/commandCapacity.ts b/.playwright/scripts/__tests__/commandCapacity.ts new file mode 100644 index 000000000..31f3a3d37 --- /dev/null +++ b/.playwright/scripts/__tests__/commandCapacity.ts @@ -0,0 +1,93 @@ +import assert from 'node:assert/strict' +import { stat, writeFile } from 'node:fs/promises' +import { join } from 'node:path' +import { performance } from 'node:perf_hooks' +import process from 'node:process' +import { setImmediate as yieldLoop } from 'node:timers/promises' +import { ComposerDraftCommandService } from '../../../apps/buddy/service/src/chat/ComposerDraftCommandService' +import { ComposerDraftService } from '../../../apps/buddy/service/src/chat/ComposerDraftService' +import { CommandAdmissionService, createLocalCommandPrincipal } from '../../../apps/buddy/service/src/commands/CommandAdmissionService' +import { createComposerDraftRepository } from '../../../apps/buddy/service/src/storage/composerDraftRepository' +import { openBuddyMigrationCandidate } from '../../../apps/buddy/service/src/storage/database' +import { createBuddyUserContent } from '../../../apps/buddy/shared/conversation/buddyUserContent' + +export async function runCommandCapacity(root: string, bundleSha256: string) { + const path = join(root, 'capacity.sqlite3') + const database = openBuddyMigrationCandidate({ databasePath: path }) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + const repository = createComposerDraftRepository(database) + const executionConfig = { approvalPolicy: 'policy', executionProfile: 'read_only' } as const + repository.open({ draftId: 'capacity', scope: { kind: 'task', draftId: 'capacity', spaceId: null }, initialContent: createBuddyUserContent(), initialExecutionConfig: executionConfig, initialModelSelection: null, now: new Date().toISOString() }) + const drafts = new ComposerDraftService(repository, async () => {}) + const principal = createLocalCommandPrincipal(database) + const admission = new CommandAdmissionService({ database, principal, sourceInstanceId: 'capacity-instance' }) + const commands = new ComposerDraftCommandService({ + database, + actorId: principal.actorId, + admission, + drafts, + prepareAttachments: async () => { throw new Error('This workload does not copy files') }, + releaseResources: async () => {}, + onCleanupError: (error) => { throw error }, + }) + const samples: number[] = [] + const dataGeneration = String(database.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation) + const count = 6000 + const report = { + platform: process.platform, + node: process.version, + bundleSha256, + commands: count, + inputCharacters: 16_384, + continuousTypingEquivalentSeconds: count * 1.5, + intermittentTypingEquivalentSeconds: count * 0.3, + limits: { commitP99Ms: 25, bytesPerCommand: 2048, receiptPayloadBytes: 1024 }, + commitP99Ms: 0, + databaseBytes: 0, + bytesPerCommand: 0, + maxReceiptPayloadBytes: 0, + backupBytes: 0, + passed: false, + } + try { + const baseline = Number(database.prepare('PRAGMA page_count').get()!.page_count) * Number(database.prepare('PRAGMA page_size').get()!.page_size) + for (let index = 0; index < count; index++) { + const started = performance.now() + const result = await commands.execute('composerDrafts.save', { + dataGeneration, + commandId: `save-${index}`, + expectedServiceInstanceId: 'capacity-instance', + intent: { draftId: 'capacity', expectedRevision: index, content: createBuddyUserContent(`${index}:`.padEnd(report.inputCharacters, 'x')), executionConfig, modelSelection: null }, + }) + assert.equal(result.receipt.decision, 'accepted') + samples.push(performance.now() - started) + if (index % 32 === 0) + await yieldLoop() + } + const sorted = samples.toSorted((a, b) => a - b) + report.commitP99Ms = sorted[Math.ceil(sorted.length * 0.99) - 1]! + assert.equal(database.prepare('SELECT COUNT(*) AS count FROM command_receipts').get()!.count, count) + report.maxReceiptPayloadBytes = Number(database.prepare('SELECT MAX(length(CAST(target_json AS BLOB)) + length(CAST(result_json AS BLOB))) AS value FROM command_receipts').get()!.value) + database.exec('PRAGMA wal_checkpoint(TRUNCATE)') + report.databaseBytes = (await stat(path)).size + report.bytesPerCommand = (report.databaseBytes - baseline) / count + const backup = join(root, 'capacity-backup.sqlite3') + database.prepare('VACUUM INTO ?').run(backup) + report.backupBytes = (await stat(backup)).size + repository.discard({ draftId: 'capacity', expectedRevision: count }) + assert.equal(admission.lookup({ dataGeneration, commandId: 'save-0' }).lookup, 'decided') + assert.equal(admission.lookup({ dataGeneration, commandId: `save-${count - 1}` }).lookup, 'decided') + assert(report.commitP99Ms <= report.limits.commitP99Ms) + assert(report.bytesPerCommand <= report.limits.bytesPerCommand) + assert(report.maxReceiptPayloadBytes <= report.limits.receiptPayloadBytes) + report.passed = true + process.stdout.write(`Command capacity: ${count} decisions, ${report.bytesPerCommand.toFixed(1)} bytes/command, commit P99 ${report.commitP99Ms.toFixed(2)}ms\n`) + } + finally { + await commands.dispose() + await admission.dispose() + await drafts.dispose() + database.close() + await writeFile(join(root, 'report.json'), `${JSON.stringify(report, null, 2)}\n`) + } +} diff --git a/.playwright/scripts/__tests__/connections.e2e.mjs b/.playwright/scripts/__tests__/connections.e2e.mjs new file mode 100644 index 000000000..c50052673 --- /dev/null +++ b/.playwright/scripts/__tests__/connections.e2e.mjs @@ -0,0 +1,99 @@ +import { Buffer } from 'node:buffer' +import { createHash, X509Certificate } from 'node:crypto' +import { once } from 'node:events' +import { request } from 'node:https' +import { createServer } from 'node:net' +import { connect } from 'node:tls' +import { expect, test, useSyntheticCredentialStorage } from '../fixtures/electron.mjs' + +test('mobile connections start without manual addressing and show centered empty or paired devices', async ({ buddy }, testInfo) => { + const instance = await buddy.createInstance('connections-settings') + const desktop = await instance.launch({ storageAuthority: 'sqlite' }) + await useSyntheticCredentialStorage(desktop) + const { page } = desktop + const reservation = createServer().listen(0, '127.0.0.1') + await once(reservation, 'listening') + const port = reservation.address().port + await new Promise(resolve => reservation.close(resolve)) + await page.evaluate(port => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: false, port }), port) + expect(await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.status({}))).toMatchObject({ enabled: false, bindAddress: null }) + await page.locator('.desktop-app-sidebar').getByRole('button', { name: '设置', exact: true }).click() + await page.getByRole('link', { name: '连接', exact: true }).click() + const settings = page.getByTestId('private-network-settings') + const toggle = settings.getByRole('switch', { name: '允许移动设备连接' }) + await expect(toggle).toBeEnabled() + await expect(settings.getByRole('textbox')).toHaveCount(0) + await expect(settings.getByRole('spinbutton')).toHaveCount(0) + const area = page.getByTestId('paired-devices-empty-area') + const empty = page.getByTestId('paired-devices-empty') + await expect(empty).toBeVisible() + async function assertCentered() { + const outer = await area.boundingBox() + const inner = await empty.boundingBox() + expect(Math.abs(outer.x + outer.width / 2 - inner.x - inner.width / 2)).toBeLessThanOrEqual(2) + expect(Math.abs(outer.y + outer.height / 2 - inner.y - inner.height / 2)).toBeLessThanOrEqual(2) + } + await assertCentered() + await page.screenshot({ path: testInfo.outputPath('connections-empty-light.png') }) + await page.evaluate(() => window.lexoraDesktop.themes.request({ action: 'preference', preference: { id: 'lexora.themes.classic-dark' } })) + await assertCentered() + await page.screenshot({ path: testInfo.outputPath('connections-empty-dark.png') }) + const browserWindow = await desktop.app.browserWindow(page) + await browserWindow.evaluate(window => window.setSize(900, 650)) + await assertCentered() + await page.screenshot({ path: testInfo.outputPath('connections-empty-narrow.png') }) + expect(await settings.evaluate(element => element.scrollWidth <= element.clientWidth)).toBe(true) + await browserWindow.evaluate(window => window.setSize(1440, 900)) + await page.evaluate(() => window.lexoraDesktop.themes.request({ action: 'preference', preference: { id: 'lexora.themes.classic-light' } })) + await toggle.click() + await expect(toggle).toBeChecked() + await expect.poll(() => page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.status({}))).toMatchObject({ state: 'listening', port }) + await settings.getByRole('button', { name: '配对新设备', exact: true }).click() + await expect(page.getByTestId('device-pairing-code').locator('canvas')).toBeVisible() + await page.screenshot({ path: testInfo.outputPath('connections-pairing.png') }) + await page.keyboard.press('Escape') + await expect(page.getByTestId('device-pairing-code')).toBeHidden() + const qr = await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.issuePairing({})) + const { serviceId, profileId, issuerInstanceId, ticketId, ticketSecret } = qr + const context = { serviceId, profileId, issuerInstanceId, ticketId, ticketSecret } + const call = await pairedTransport(qr) + const prepared = await call('/v1/pairing/prepare', { ...context, deviceName: '测试手机' }) + const credentials = await call('/v1/pairing/redeem', { ...context, attemptId: prepared.attemptId, recoverySecret: prepared.recoverySecret }) + await call('/v1/pairing/confirm', {}, `${credentials.credentialId}.${credentials.credentialSecret}`) + const list = page.getByTestId('paired-devices-list') + await expect(list.getByText('测试手机', { exact: true })).toBeVisible({ timeout: 15000 }) + await expect(list).toContainText('已授权') + await expect(empty).toBeHidden() + await page.screenshot({ path: testInfo.outputPath('connections-paired.png') }) + await list.getByRole('button', { name: '撤销访问' }).click() + await page.getByRole('button', { name: '确认', exact: true }).click() + await expect(list).toContainText('已撤销') + await toggle.click() + await expect(toggle).not.toBeChecked() + await expect.poll(() => page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.status({}))).toMatchObject({ enabled: false, state: 'disabled', endpoint: null }) + expect(desktop.diagnostics.console.filter(item => item.type === 'pageerror')).toEqual([]) +}) + +async function pairedTransport(qr) { + const url = new URL(qr.endpoint) + const socket = connect({ host: url.hostname, port: Number(url.port), rejectUnauthorized: false }) + await once(socket, 'secureConnect') + const certificate = new X509Certificate(socket.getPeerCertificate().raw) + socket.destroy() + const pin = `sha256/${createHash('sha256').update(certificate.publicKey.export({ type: 'spki', format: 'der' })).digest('base64')}` + expect(pin).toBe(qr.spkiPin) + return (path, body, bearer) => new Promise((resolve, reject) => { + const payload = JSON.stringify(body) + const req = request(`${qr.endpoint}${path}`, { method: 'POST', ca: certificate.toString(), headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(payload), ...(bearer ? { Authorization: `Bearer ${bearer}` } : {}) } }, (response) => { + const chunks = [] + response.on('data', chunk => chunks.push(chunk)) + response.on('end', () => { + if (response.statusCode !== 200) + reject(new Error(`Pairing failed: ${response.statusCode}`)) + else resolve(JSON.parse(Buffer.concat(chunks).toString())) + }) + }) + req.on('error', reject) + req.end(payload) + }) +} diff --git a/.playwright/scripts/__tests__/context-panel-draft-adoption.e2e.mjs b/.playwright/scripts/__tests__/context-panel-draft-adoption.e2e.mjs index d0ce8626a..7c0038f05 100644 --- a/.playwright/scripts/__tests__/context-panel-draft-adoption.e2e.mjs +++ b/.playwright/scripts/__tests__/context-panel-draft-adoption.e2e.mjs @@ -80,7 +80,7 @@ test('first send with a browser reference preserves the open resource panel and await pick.click() await expect(pick).toHaveAttribute('aria-pressed', 'true') await app.evaluate(async ({ webContents }, url) => { - const guest = webContents.getAllWebContents().find(contents => contents.getType() === 'webview' && contents.getURL() === url) + const guest = webContents.getAllWebContents().find(contents => contents.getURL() === url) const point = await guest.executeJavaScript('(() => { const r = document.querySelector("#picked").getBoundingClientRect(); return { x:r.x+r.width/2, y:r.y+r.height/2 }; })()') const x = Math.round(point.x * guest.getZoomFactor()) const y = Math.round(point.y * guest.getZoomFactor()) diff --git a/.playwright/scripts/__tests__/extension-contributions.e2e.mjs b/.playwright/scripts/__tests__/extension-contributions.e2e.mjs index 49a6e3211..e4cec717c 100644 --- a/.playwright/scripts/__tests__/extension-contributions.e2e.mjs +++ b/.playwright/scripts/__tests__/extension-contributions.e2e.mjs @@ -51,26 +51,6 @@ test('plugin-owned preferences activate contributions, yield native content and const alpha = await pluginFrame('alpha', 'footer') await expect(alpha.getByText('tests.alpha content')).toBeVisible() await expect(nativeFooter()).toHaveCount(0) - const identity = await alpha.evaluate(() => window.fixture.instance) - const hostColor = name => page.evaluate((name) => { - const style = document.createElement('span').style - style.color = getComputedStyle(document.documentElement).getPropertyValue(`--buddy-${name}`) - return style.color - }, name) - await page.evaluate(() => window.lexoraDesktop.settings.update({ desktop: { theme: { id: 'lexora.themes.violet-dark' } } })) - await expect(page.locator('html')).toHaveAttribute('data-buddy-theme-id', 'lexora.themes.violet-dark') - await expect.poll(() => alpha.evaluate(() => getComputedStyle(document.querySelector('main')).backgroundColor)).toBe(await hostColor('accent-solid')) - await page.getByTestId('context-panel-toggle').click() - await page.getByTestId('context-panel-more').click() - await page.getByRole('menuitem', { name: '移到左侧', exact: true }).click() - await page.getByTestId('context-panel-maximize').click() - await expect.poll(() => visible('alpha')).toBe(false) - await page.getByTestId('context-panel-maximize').click() - await expect.poll(() => visible('alpha')).toBe(true) - await page.getByTestId('context-panel-more').click() - await page.getByRole('menuitem', { name: '移到右侧', exact: true }).click() - await page.getByTestId('context-panel-more').click() - await page.getByRole('menuitem', { name: '关闭', exact: true }).click() const alphaActions = await pluginFrame('alpha', 'actions') const actionFrame = await alphaActions.frameElement() await actionFrame.waitForElementState('stable') @@ -79,7 +59,6 @@ test('plugin-owned preferences activate contributions, yield native content and await expect(nativeFooter()).toBeVisible() await alphaActions.getByRole('checkbox', { name: 'Content tests.alpha' }).check() await expect(alpha.getByText('tests.alpha content')).toBeVisible() - expect(await alpha.evaluate(() => window.fixture.instance)).toBe(identity) await installPlugin(instance, app, page, 'beta') await preference('beta', true) await expect.poll(() => visible('alpha')).toBe(true) @@ -103,7 +82,7 @@ test('plugin-owned preferences activate contributions, yield native content and await expect(nativeFooter()).toBeVisible() }) -test('command failures distinguish execution, malformed replies and timeouts in the real plugin host', async ({ buddy }) => { +test('command failures preserve diagnostics and responsive pending calls settle when the plugin is disabled', async ({ buddy }) => { const instance = await buddy.createInstance('plugin-failures') const { app, page } = await instance.launch() const id = await installPlugin(instance, app, page, 'failures') @@ -123,10 +102,17 @@ test('command failures distinguish execution, malformed replies and timeouts in expect(await status()).toMatchObject({ state: 'active', error: null, logs: expect.arrayContaining([ expect.objectContaining({ code: 'EXTENSION_INVALID_RESULT', diagnostic: expect.objectContaining({ method: 'command', stage: 'result' }) }), ]) }) - expect(await execute('timeout')).toContain('EXTENSION_REQUEST_TIMEOUT') - await expect.poll(status).toMatchObject({ state: 'failed', generation: null, error: 'EXTENSION_REQUEST_TIMEOUT', logs: expect.arrayContaining([ - expect.objectContaining({ event: 'host.failed', code: 'EXTENSION_REQUEST_TIMEOUT', diagnostic: { method: 'command', stage: 'timeout' } }), - ]) }) + let settled = false + const pending = execute('wait').then((error) => { + settled = true + return error + }) + expect(await execute('read')).toBeNull() + expect(settled).toBe(false) + expect(await status()).toMatchObject({ state: 'active', error: null }) + await page.evaluate(id => window.lexoraDesktop.extensions.enable(id, false), id) + expect(await pending).toContain('EXTENSION_AGENT_CANCELLED') + await expect.poll(status).toMatchObject({ state: 'disabled', generation: null, error: null }) expect(JSON.stringify(await status())).not.toContain('Private plugin error') }) diff --git a/.playwright/scripts/__tests__/local-service-lifecycle.e2e.mjs b/.playwright/scripts/__tests__/local-service-lifecycle.e2e.mjs new file mode 100644 index 000000000..2297ad758 --- /dev/null +++ b/.playwright/scripts/__tests__/local-service-lifecycle.e2e.mjs @@ -0,0 +1,328 @@ +import { execFile, spawn } from 'node:child_process' +import { createHash, randomUUID } from 'node:crypto' +import { once } from 'node:events' +import fs from 'node:fs/promises' +import { createRequire, isBuiltin } from 'node:module' +import path from 'node:path' +import process from 'node:process' +import { DatabaseSync } from 'node:sqlite' +import { pathToFileURL } from 'node:url' +import { promisify } from 'node:util' +import { chromium } from '@playwright/test' +import { startDisplay } from '../buddy-test.mjs' +import { expect, test } from '../fixtures/electron.mjs' +import { processAlive as live, connectLocalService as management, readJson } from '../fixtures/local-service.mjs' +import { ROOT } from '../shared.mjs' + +const buddyRoot = path.join(ROOT, 'apps/buddy') +const require = createRequire(path.join(buddyRoot, 'package.json')) + +const execute = promisify(execFile) +const cases = [{ storageAuthority: 'legacy_jsonl' }, { storageAuthority: 'sqlite' }] +if (process.env.LEXORA_TEST_SESSION_SERVICE === '1') + cases.push({ storageAuthority: 'sqlite', sessionService: true }) + +for (const { storageAuthority, sessionService } of cases) { + test(`independent Core maintains ${storageAuthority} storage${sessionService ? ', registered by systemd' : ''}, survives Desktop exit, and fully stops its host`, async ({ buddy }, testInfo) => { + test.skip(process.platform !== 'linux', 'The POSIX process-group acceptance runs on Linux') + test.setTimeout(180000) + const instance = await buddy.createInstance('independent-service') + let preparationModule + if (storageAuthority === 'sqlite') { + const preparation = testInfo.outputPath('profile-preparation') + const { build } = await import(pathToFileURL(require.resolve('vite')).href) + await build({ configFile: false, logLevel: 'error', build: { + ssr: path.join(ROOT, '.playwright/scripts/__tests__/prepareProductProfile.ts'), + outDir: preparation, + target: 'node26', + rolldownOptions: { external: isBuiltin, output: { entryFileNames: 'prepare.mjs', format: 'es' } }, + } }) + preparationModule = await import(pathToFileURL(path.join(preparation, 'prepare.mjs')).href) + } + const display = await startDisplay() + const output = path.join(buddyRoot, '.output') + const release = JSON.parse(await fs.readFile(path.join(output, 'release-set.json'), 'utf8')) + const boot = (await fs.readFile('/proc/sys/kernel/random/boot_id', 'utf8')).trim() + let runtimeDirectory = path.join(instance.home, '.runtime') + await fs.mkdir(runtimeDirectory, { mode: 0o700 }) + const config = { + version: 1, + profile: 'test', + profileRoot: instance.home, + resourcesRoot: path.join(output, path.dirname(release.core.entry), 'resources'), + nodeExecutable: path.join(output, release.node.entry), + coreEntry: path.join(output, release.core.entry), + hostExecutable: require('electron'), + hostArguments: ['--ozone-platform=x11', '--disable-setuid-sandbox', '--disable-gpu', '--remote-debugging-port=0', buddyRoot], + releaseSetPath: path.join(output, 'release-set.json'), + buildId: release.buildId, + sessionId: `${boot}:${process.env.XDG_SESSION_ID}`, + userId: String(process.getuid()), + leaseDirectory: path.join(path.dirname(instance.home), '.leases'), + storageAuthority, + } + const launchFile = path.join(runtimeDirectory, 'launch.json') + await fs.writeFile(launchFile, JSON.stringify(config), { mode: 0o600 }) + let stderr = '' + const core = sessionService ? null : spawn(config.nodeExecutable, [config.coreEntry, '--config', launchFile], { cwd: ROOT, env: { ...process.env, DISPLAY: display.address, WAYLAND_DISPLAY: '', LEXORA_BUDDY_PROFILE: 'test', LEXORA_HOME: instance.home }, stdio: ['ignore', 'pipe', 'pipe'] }) + core?.stderr.on('data', (value) => { + stderr += value + }) + core?.stdout.on('data', (value) => { + stderr += value + }) + let browser + let client + let product + let hostPid + let corePid = core?.pid + let unit + try { + if (sessionService) { + const launcher = preparationModule.resolveBuddyProcessControl({ appPath: config.resourcesRoot, resourcesPath: config.resourcesRoot, isPackaged: true }) + expect(launcher).toBeTruthy() + const registration = new preparationModule.SessionServiceRegistration(config, launchFile, launcher) + unit = `${registration.name}.service` + const previousDisplay = process.env.DISPLAY + const previousWayland = process.env.WAYLAND_DISPLAY + process.env.DISPLAY = display.address + process.env.WAYLAND_DISPLAY = '' + try { + await registration.start() + } + finally { + if (previousDisplay === undefined) + delete process.env.DISPLAY + else process.env.DISPLAY = previousDisplay + if (previousWayland === undefined) + delete process.env.WAYLAND_DISPLAY + else process.env.WAYLAND_DISPLAY = previousWayland + } + } + await expect.poll(async () => { + const discovery = await readJson(path.join(runtimeDirectory, 'service.json')) + return discovery?.phase + }, { timeout: 120000 }).toBe('ready') + let discovery = await readJson(path.join(runtimeDirectory, 'service.json')) + if (unit) { + corePid = Number((await execute('/usr/bin/systemctl', ['--user', 'show', unit, '--property=MainPID', '--value'])).stdout.trim()) + stderr = (await execute('/usr/bin/journalctl', ['--user', '-u', unit, '-o', 'cat', '--no-pager'], { maxBuffer: 4 * 1024 * 1024 })).stdout + } + expect(discovery.process.pid).toBe(corePid) + let credentials = await readJson(path.join(runtimeDirectory, 'local-client.json')) + client = await management(discovery, credentials.managementSecret) + await expect.poll(() => stderr.match(/DevTools listening on (ws:\/\/\S+)/)?.[1]).toBeTruthy() + browser = await chromium.connectOverCDP(stderr.match(/DevTools listening on (ws:\/\/\S+)/)[1]) + expect(browser.contexts().flatMap(context => context.pages()).filter(page => page.url().startsWith('lexora-app:'))).toHaveLength(0) + const children = (await fs.readFile(`/proc/${corePid}/task/${corePid}/children`, 'utf8')).trim().split(/\s+/).filter(Boolean).map(Number) + const executable = await fs.realpath(config.hostExecutable) + const hosts = [] + for (const pid of children) { + if (await fs.readlink(`/proc/${pid}/exe`).catch(() => null) === executable) + hosts.push(pid) + } + expect(hosts).toHaveLength(1) + hostPid = hosts[0] + const opened = await client.request('service.openDesktop', { expectedServiceInstanceId: discovery.serviceInstanceId, intent: { kind: 'tasks' } }) + expect(opened).toMatchObject({ accepted: true, uiReady: true, targetVisible: true, visible: true }) + let page = browser.contexts().flatMap(context => context.pages()).find(page => page.url().startsWith('lexora-app:')) + expect(page).toBeTruthy() + await expect.poll(() => page.evaluate(() => window.lexoraDesktop.app.startup.getState())).toMatchObject({ status: 'ready' }) + const info = await page.evaluate(() => window.lexoraDesktop.app.getInfo()) + expect(path.dirname(info.configPath)).toBe(instance.home) + const firstGeneration = await page.evaluate(async () => (await window.lexoraDesktop.app.startup.getState()).generation) + const snapshot = await page.evaluate(() => window.lexoraDesktop.storage.snapshot({ scopes: ['tasks', 'settings'], credentials: false })) + expect(snapshot.id).toBeTruthy() + await expect.poll(() => page.evaluate(() => window.lexoraDesktop.app.startup.getState())).toMatchObject({ status: 'ready', maintenance: null }) + const backupGeneration = await page.evaluate(async () => (await window.lexoraDesktop.app.startup.getState()).generation) + expect(backupGeneration).not.toBe(firstGeneration) + expect((await client.request('service.status', {})).process.pid).toBe(corePid) + await page.evaluate(() => window.lexoraDesktop.localChat.runtime.restart()) + await expect.poll(() => page.evaluate(() => window.lexoraDesktop.app.startup.getState())).toMatchObject({ status: 'ready', maintenance: null }) + expect(await page.evaluate(async () => (await window.lexoraDesktop.app.startup.getState()).generation)).not.toBe(backupGeneration) + expect((await client.request('service.status', {})).process.pid).toBe(corePid) + await expect(client.request('service.beginMaintenance', { expectedServiceInstanceId: discovery.serviceInstanceId, kind: 'backup' })).rejects.toThrow() + const restart = async (expectedRoot = instance.home) => { + const previousHost = hostPid + const previousService = discovery.serviceInstanceId + await page.evaluate(() => window.lexoraDesktop.storage.restart()) + runtimeDirectory = path.join(expectedRoot, '.runtime') + await expect.poll(async () => { + const next = await readJson(path.join(runtimeDirectory, 'service.json')) + return next?.phase === 'ready' && next.serviceInstanceId !== previousService + }, { timeout: 120000 }).toBe(true) + expect(await live(previousHost)).toBe(false) + discovery = await readJson(path.join(runtimeDirectory, 'service.json')) + credentials = await readJson(path.join(runtimeDirectory, 'local-client.json')) + client.close() + client = await management(discovery, credentials.managementSecret) + expect(discovery.process.pid).toBe(corePid) + if (unit) + stderr = (await execute('/usr/bin/journalctl', ['--user', '-u', unit, '-o', 'cat', '--no-pager'], { maxBuffer: 4 * 1024 * 1024 })).stdout + await browser.close().catch(() => {}) + const endpoint = [...stderr.matchAll(/DevTools listening on (ws:\/\/\S+)/g)].at(-1)?.[1] + browser = await chromium.connectOverCDP(endpoint) + page = browser.contexts().flatMap(context => context.pages()).find(candidate => candidate.url().startsWith('lexora-app:')) + await expect.poll(() => page?.evaluate(() => window.lexoraDesktop.app.startup.getState())).toMatchObject({ status: 'ready' }) + expect(path.dirname((await page.evaluate(() => window.lexoraDesktop.app.getInfo())).configPath)).toBe(expectedRoot) + const children = (await fs.readFile(`/proc/${corePid}/task/${corePid}/children`, 'utf8')).trim().split(/\s+/).filter(Boolean).map(Number) + const nextHosts = [] + for (const pid of children) { + if (await fs.readlink(`/proc/${pid}/exe`).catch(() => null) === executable) + nextHosts.push(pid) + } + expect(nextHosts).toHaveLength(1) + hostPid = nextHosts[0] + } + await restart() + if (storageAuthority === 'sqlite') { + if (sessionService) { + await page.evaluate(id => window.lexoraDesktop.storage.restore(id, { scopes: ['tasks', 'settings'], credentials: false }), snapshot.id) + await restart() + } + else { + const moved = `${instance.home}-moved` + await page.evaluate(target => window.lexoraDesktop.storage.relocate(target), moved) + await restart(moved) + } + } + await page.evaluate(() => window.lexoraDesktop.settings.update({ desktop: { exitBehavior: 'keep_service' } })) + await page.screenshot({ path: testInfo.outputPath('independent-workbench.png'), animations: 'disabled', timeout: 10000 }) + const closed = page.waitForEvent('close') + await page.evaluate(() => window.lexoraDesktop.commands.execute('app.quit')) + await closed + expect((await client.request('service.status', {})).process.pid).toBe(corePid) + expect(await live(hostPid)).toBe(true) + if (storageAuthority === 'sqlite') { + product = await management(discovery, credentials.productSecret, 'product') + const initialized = await product.request('initialize', { protocol: { major: 1, minMinor: 0, maxMinor: 0 }, client: { kind: 'mobile', version: 'test' }, supportedContentKinds: ['buddy-user-content-v1'], supportedReviewKinds: [] }) + const command = { dataGeneration: initialized.dataGeneration, commandId: randomUUID(), expectedServiceInstanceId: initialized.serviceInstanceId, intent: { kind: 'settings' } } + const result = await product.request('desktop.open', command) + expect(result.receipt).toMatchObject({ decision: 'accepted', result: { desktop: { uiReady: true, targetVisible: true, visible: true } } }) + expect(await product.request('desktop.open', command)).toEqual(result) + expect(await product.request('commands.get', { dataGeneration: command.dataGeneration, commandId: command.commandId })).toEqual({ lookup: 'decided', receipt: result.receipt }) + } + else { + expect(await client.request('service.openDesktop', { expectedServiceInstanceId: discovery.serviceInstanceId, intent: { kind: 'settings' } })).toMatchObject({ targetVisible: true }) + } + const reopened = browser.contexts().flatMap(context => context.pages()).find(page => page.url().startsWith('lexora-app:')) + expect(reopened).not.toBe(page) + expect(reopened.url()).toContain('#/settings/general') + await expect.poll(() => reopened.evaluate(() => window.lexoraDesktop.app.startup.getState())).toMatchObject({ status: 'ready' }) + expect((await reopened.evaluate(() => window.lexoraDesktop.settings.get())).desktop.exitBehavior).toBe('keep_service') + const exited = core ? once(core, 'exit') : null + if (storageAuthority === 'sqlite' && !sessionService) { + await reopened.evaluate(async () => { + await window.lexoraDesktop.settings.update({ desktop: { exitBehavior: 'stop_service' } }) + void window.lexoraDesktop.commands.execute('app.quit') + }) + } + else { + expect(await client.request('service.stop', { expectedServiceInstanceId: discovery.serviceInstanceId, commandId: randomUUID(), reason: 'explicit' })).toEqual({ accepted: true }) + } + if (exited) + await exited + else await expect.poll(() => live(corePid)).toBe(false) + expect(await live(hostPid)).toBe(false) + expect(await readJson(path.join(runtimeDirectory, 'service.json'))).toBeNull() + } + finally { + product?.close() + client?.close() + await browser?.close().catch(() => {}) + if (core && core.exitCode === null && core.signalCode === null) { + const exited = once(core, 'exit') + core.kill('SIGTERM') + await Promise.race([exited, new Promise(resolve => setTimeout(resolve, 10000))]) + } + if (unit) { + await execute('/usr/bin/systemctl', ['--user', 'stop', unit]).catch(() => {}) + stderr = (await execute('/usr/bin/journalctl', ['--user', '-u', unit, '-o', 'cat', '--no-pager'], { maxBuffer: 4 * 1024 * 1024 })).stdout + } + await display.stop() + await fs.writeFile(testInfo.outputPath('service.log'), stderr) + await testInfo.attach('service-log', { path: testInfo.outputPath('service.log'), contentType: 'text/plain' }) + } + }) +} + +for (const legacy of [false, true]) { + test(`desktop launcher ${legacy ? 'upgrades legacy data and' : 'initializes SQLite and'} starts one session service, reuses it and honors installer shutdown`, async ({ buddy }, testInfo) => { + test.skip(process.platform !== 'linux' || process.env.LEXORA_TEST_SESSION_SERVICE !== '1', 'Requires an explicit Linux user-session service acceptance') + test.setTimeout(180000) + const instance = await buddy.createInstance('service-launcher', { executablePath: legacy ? process.env.LEXORA_TEST_LEGACY_EXECUTABLE : undefined }) + let original + let draftId + if (legacy) { + const { page } = await instance.launch() + const editor = page.locator('.desktop-workbench-area__tasks .tiptap:visible').first() + await editor.fill('启动升级保留草稿 😀') + await expect.poll(() => page.evaluate(async () => (await window.lexoraDesktop.localChat.composerDrafts.list()).find(draft => JSON.stringify(draft.content).includes('启动升级保留草稿 😀'))?.draftId)).toBeTruthy() + draftId = await page.evaluate(async () => (await window.lexoraDesktop.localChat.composerDrafts.list()).find(draft => JSON.stringify(draft.content).includes('启动升级保留草稿 😀')).draftId) + await instance.stop() + original = await fs.readFile(path.join(instance.home, 'buddy/buddy.sqlite3')) + } + const display = await startDisplay() + const environment = Object.fromEntries(Object.entries(process.env).filter(([key]) => !/^(?:LEXORA_|ELECTRON_|PI_)/.test(key))) + Object.assign(environment, { DISPLAY: display.address, WAYLAND_DISPLAY: '', XDG_SESSION_TYPE: 'x11', LEXORA_BUDDY_PROFILE: 'test', LEXORA_HOME: instance.home, LEXORA_BUDDY_SERVICE_MODE: 'daemon' }) + const packaged = process.env.LEXORA_TEST_SERVICE_EXECUTABLE + if (packaged) + delete environment.LEXORA_BUDDY_SERVICE_MODE + const launch = async (...args) => { + const result = await execute(packaged ?? require('electron'), ['--ozone-platform=x11', '--disable-setuid-sandbox', '--disable-gpu', ...(packaged ? [] : [buddyRoot]), ...args], { cwd: ROOT, env: environment, timeout: 130000, maxBuffer: 1024 * 1024 }) + await fs.appendFile(testInfo.outputPath('launcher.log'), result.stderr) + } + const directory = path.join(instance.home, '.runtime') + const launchFile = path.join(path.dirname(instance.home), '.lexora-locations', createHash('sha256').update(instance.home).digest('hex').slice(0, 24), 'service', 'launch.json') + let unit + let client + let corePid + try { + await launch('--background') + await expect.poll(() => readJson(launchFile), { timeout: 120000 }).not.toBeNull() + const config = await readJson(launchFile) + unit = `lexora-core-${createHash('sha256').update(`${config.defaultRoot ?? config.profileRoot}\0${config.sessionId}`).digest('hex').slice(0, 24)}.service` + expect(config.hostArguments).toEqual(expect.arrayContaining(['--ozone-platform=x11', '--disable-gpu'])) + await expect.poll(() => readJson(path.join(directory, 'service.json')), { timeout: 120000 }).toMatchObject({ phase: 'ready' }) + const discovery = await readJson(path.join(directory, 'service.json')) + expect(discovery.phase).toBe('ready') + corePid = discovery.process.pid + const credentials = await readJson(path.join(directory, 'local-client.json')) + client = await management(discovery, credentials.managementSecret) + const opened = await client.request('service.openDesktop', { expectedServiceInstanceId: discovery.serviceInstanceId, intent: { kind: 'tasks' } }) + expect(opened).toMatchObject({ accepted: true, uiReady: true, targetVisible: true, visible: true }) + await launch() + expect((await readJson(path.join(directory, 'service.json'))).serviceInstanceId).toBe(discovery.serviceInstanceId) + expect((await client.request('service.status', {})).process.pid).toBe(corePid) + await launch('--quit-for-install') + expect(await live(corePid)).toBe(false) + await expect.poll(() => readJson(path.join(directory, 'service.json'))).toBeNull() + await launch('--quit-for-install') + const location = await readJson(path.join(path.dirname(path.dirname(launchFile)), 'location.json')) + expect(discovery.profileId).toBe(location.identity.dataId) + const database = new DatabaseSync(path.join(instance.home, 'buddy/buddy.sqlite3'), { readOnly: true }) + try { + expect(database.prepare('SELECT authority, migration_state FROM storage_profile').get()).toMatchObject({ authority: 'sqlite', migration_state: 'ready' }) + if (legacy) { + expect(database.prepare('SELECT content_json FROM composer_drafts WHERE id = ?').get(draftId).content_json).toContain('启动升级保留草稿 😀') + const upgrade = location.history.find(operation => operation.kind === 'upgrade') + expect(await fs.readFile(path.join(upgrade.retainedTarget, 'buddy/buddy.sqlite3'))).toEqual(original) + } + } + finally { database.close() } + expect(await readJson(path.join(directory, 'service.json'))).toBeNull() + } + finally { + client?.close() + const config = await readJson(launchFile) + if (!unit && config) + unit = `lexora-core-${createHash('sha256').update(`${config.defaultRoot ?? config.profileRoot}\0${config.sessionId}`).digest('hex').slice(0, 24)}.service` + if (unit) { + await execute('/usr/bin/systemctl', ['--user', 'stop', unit]).catch(() => {}) + const log = await execute('/usr/bin/journalctl', ['--user', '-u', unit, '-o', 'cat', '--no-pager'], { maxBuffer: 4 * 1024 * 1024 }) + await fs.writeFile(testInfo.outputPath('launcher-service.log'), log.stdout) + } + await display.stop() + } + }) +} diff --git a/.playwright/scripts/__tests__/mobile-approvals.e2e.mjs b/.playwright/scripts/__tests__/mobile-approvals.e2e.mjs new file mode 100644 index 000000000..50f27d149 --- /dev/null +++ b/.playwright/scripts/__tests__/mobile-approvals.e2e.mjs @@ -0,0 +1,235 @@ +import { Buffer } from 'node:buffer' +import { execFile } from 'node:child_process' +import { randomUUID } from 'node:crypto' +import { once } from 'node:events' +import fs from 'node:fs/promises' +import { createServer } from 'node:http' +import { createServer as createListener } from 'node:net' +import path from 'node:path' +import process from 'node:process' +import { DatabaseSync } from 'node:sqlite' +import { promisify } from 'node:util' +import { expect, test, useSyntheticCredentialStorage } from '../fixtures/electron.mjs' +import { prepareMobileDesktopProfile } from '../fixtures/mobile-desktop.mjs' +import { selectComposerPermission } from '../fixtures/model-controls.mjs' + +const execute = promisify(execFile) +const serial = process.env.LEXORA_TEST_ANDROID_SERIAL +const serviceMode = process.env.LEXORA_TEST_MOBILE_SERVICE_MODE ?? 'utility' + +test('Desktop and Android converge on approval winners, original receipts and cancelled runs', async ({ buddy }) => { + test.skip(!serial, 'Requires an isolated Android device with the debug and instrumentation APKs installed') + test.setTimeout(240000) + const adb = (...args) => execute(process.env.ADB ?? 'adb', ['-s', serial, ...args], { timeout: 210000, maxBuffer: 8 * 1024 * 1024 }) + const token = randomUUID() + const appPackage = 'app.uselexora.mobile.dev' + const remoteConfig = `/data/local/tmp/lexora-approval-${token}.json` + const configuration = test.info().outputPath('pairing.json') + const requests = [] + const provider = createServer((request, response) => { + void (async () => { + const chunks = [] + for await (const chunk of request) chunks.push(chunk) + const body = JSON.parse(Buffer.concat(chunks).toString('utf8')) + requests.push(body) + const user = body.messages.findLastIndex(message => message.role === 'user') + const phase = JSON.stringify(body.messages[user]?.content).match(/Approval competition (\d)/)?.[1] ?? '0' + const write = !body.messages.slice(user + 1).some(message => message.role === 'tool') + const delta = write + ? { role: 'assistant', tool_calls: [{ index: 0, id: `approval-write-${phase}`, type: 'function', function: { name: 'write', arguments: JSON.stringify({ path: `approval-${phase}.txt`, content: `Approved exactly once ${phase} 😀` }) } }] } + : { role: 'assistant', content: `双端审批完成 ${phase}` } + const common = { id: `approval-${requests.length}`, model: 'fixture-model', object: 'chat.completion.chunk', created: 1 } + response.writeHead(200, { 'content-type': 'text/event-stream' }) + response.write(`data: ${JSON.stringify({ ...common, choices: [{ index: 0, delta, finish_reason: null }] })}\n\n`) + response.write(`data: ${JSON.stringify({ ...common, choices: [{ index: 0, delta: {}, finish_reason: write ? 'tool_calls' : 'stop' }] })}\n\n`) + response.end('data: [DONE]\n\n') + })().catch(() => response.destroy()) + }) + provider.listen(0, '127.0.0.1') + await once(provider, 'listening') + const instance = await buddy.createInstance('mobile-approvals') + await prepareMobileDesktopProfile(instance.home, test.info().outputPath('profile-preparation')) + const desktop = await instance.launch({ storageAuthority: 'sqlite', serviceMode }) + await useSyntheticCredentialStorage(desktop) + let { page } = desktop + const read = (query) => { + const database = new DatabaseSync(path.join(instance.home, 'buddy/buddy.sqlite3'), { readOnly: true }) + try { + return query(database) + } + finally { database.close() } + } + const listener = createListener() + await new Promise(resolve => listener.listen(0, '127.0.0.1', resolve)) + const port = listener.address().port + await new Promise(resolve => listener.close(resolve)) + let instrument + const files = [0, 1, 2, 3].flatMap(phase => ['ready', 'done', 'resume'].map(stage => `files/approval-${token}-${phase}-${stage}.json`)) + files.push(...['0', '2', 'final'].map(phase => `files/approval-${token}-${phase}.png`)) + const checkpoint = async (phase, stage) => { + const file = `files/approval-${token}-${phase}-${stage}.json` + files.push(file) + let value + await expect.poll(async () => { + try { + value = JSON.parse((await adb('shell', 'run-as', appPackage, 'cat', file)).stdout) + return true + } + catch { return false } + }, { timeout: 60000 }).toBe(true) + return value + } + try { + await page.evaluate(async (baseUrl) => { + await window.lexoraDesktop.settings.update({ runtime: { codemode: 'off', modelRetryLimit: 0 } }) + const providers = window.lexoraDesktop.localChat.providers + await providers.upsertCustom({ id: 'approval-fixture', displayName: 'Approval fixture', api: 'openai-completions', baseUrl, enabled: true, models: [{ id: 'fixture-model', name: 'Approval model', input: ['text'], reasoning: false, contextWindow: 128000, maxTokens: 1024 }] }) + const stop = providers.onAuthChallenge((challenge) => { + if (challenge.providerId === 'approval-fixture' && challenge.type === 'secret') + void providers.respondToAuth(challenge.challengeId, 'offline-fixture-key') + }) + try { + await providers.login('approval-fixture', 'api_key') + } + finally { stop() } + await providers.setDefaultModel({ providerId: 'approval-fixture', modelId: 'fixture-model', reasoning: null }) + }, `http://127.0.0.1:${provider.address().port}/v1`) + await page.reload() + await selectComposerPermission(page, '人工审批') + await page.evaluate(port => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: true, bindAddress: '127.0.0.1', port }), port) + await adb('reverse', `tcp:${port}`, `tcp:${port}`) + const evidence = [] + for (let phase = 0; phase < 5; phase++) { + await page.locator('.desktop-chat-composer__prosemirror:visible').fill(`Approval competition ${phase}`) + await page.getByRole('button', { name: '发送消息', exact: true }).click() + const card = page.locator('.desktop-approval-card').filter({ has: page.getByRole('button', { name: '允许一次', exact: true }) }) + await expect(card).toBeVisible({ timeout: 45000 }) + const pending = read(db => db.prepare('SELECT a.id, a.run_id, r.conversation_id FROM approvals a JOIN runs r ON a.run_id = r.id WHERE a.status = ?').get('pending')) + if (phase === 0) { + const pairing = await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.issuePairing({})) + await fs.writeFile(configuration, JSON.stringify({ pairing, conversationId: pending.conversation_id, desktopLifecycle: serviceMode === 'daemon' }), { mode: 0o600 }) + await adb('push', configuration, remoteConfig) + await fs.rm(configuration) + instrument = adb('shell', 'am', 'instrument', '-w', '-r', '-e', 'class', 'app.uselexora.mobile.DesktopApprovalInstrumentedTest', '-e', 'desktopApprovalToken', token, `${appPackage}.test/androidx.test.runner.AndroidJUnitRunner`).then(result => ({ result }), error => ({ error })) + } + const ready = await checkpoint(phase, 'ready') + expect(ready.approvalId).toBe(pending.id) + await page.screenshot({ path: test.info().outputPath(`desktop-pending-${phase}.png`) }) + if (phase === 0) { + await card.getByRole('button', { name: '拒绝', exact: true }).click() + } + else if (phase === 1) { + if (serviceMode === 'daemon') { + await page.evaluate(() => window.lexoraDesktop.settings.update({ desktop: { exitBehavior: 'keep_service' } })) + const closed = page.waitForEvent('close') + await page.evaluate(() => window.lexoraDesktop.commands.execute('app.quit')) + await closed + expect(desktop.app.process().exitCode).toBeNull() + expect(desktop.app.context().pages().filter(candidate => candidate.url().startsWith('lexora-app:'))).toHaveLength(0) + } + const reopened = serviceMode === 'daemon' ? desktop.app.context().waitForEvent('page', { timeout: 60000 }) : null + const resume = `files/approval-${token}-${phase}-resume.json` + files.push(resume) + await adb('shell', 'run-as', appPackage, 'touch', resume) + if (reopened) { + page = await reopened + await expect.poll(() => page.evaluate(() => window.lexoraDesktop.app.startup.getState())).toMatchObject({ status: 'ready' }) + await expect(page.locator('.buddy-chat-message').filter({ hasText: '双端审批完成 1' })).toBeVisible() + } + } + else if (phase === 2) { + await page.getByRole('button', { name: '停止运行', exact: true }).click() + } + else if (phase === 3) { + await page.evaluate(() => window.lexoraDesktop.localChat.runtime.restart()) + await expect.poll(async () => (await page.evaluate(() => window.lexoraDesktop.localChat.runtime.getStatus())).status, { timeout: 45000 }).toBe('ready') + await adb('shell', 'run-as', appPackage, 'touch', `files/approval-${token}-${phase}-resume.json`) + } + else { + const resume = `files/approval-${token}-${phase}-resume.json` + files.push(resume) + await adb('shell', 'run-as', appPackage, 'touch', resume) + } + const expected = ['denied', 'approved', 'cancelled', 'cancelled', 'denied'][phase] + await expect.poll(() => read(db => db.prepare('SELECT status FROM approvals WHERE id = ?').get(pending.id).status)).toBe(expected) + await expect(page.locator('.desktop-approval-card').getByRole('button', { name: '允许一次', exact: true })).toHaveCount(0) + await expect.poll(() => read(db => db.prepare('SELECT status FROM runs WHERE id = ?').get(pending.run_id).status)).not.toMatch(/^(queued|running)$/) + if (phase < 2 || phase === 4) + await expect(page.locator('.buddy-chat-message').filter({ hasText: `双端审批完成 ${phase}` })).toBeVisible() + const receipts = await checkpoint(phase, 'done') + const decision = read(db => db.prepare('SELECT * FROM approval_decisions WHERE approval_id = ?').get(pending.id)) + expect(decision.resolution).toBe(phase === 2 || phase === 3 ? 'cancelled' : phase === 1 ? 'approved' : 'denied') + expect(read(db => db.prepare('SELECT COUNT(*) AS count FROM run_events WHERE run_id = ? AND event_type = \'approval.resolved\'').get(pending.run_id).count)).toBe(1) + const review = JSON.parse(read(db => db.prepare('SELECT review_json FROM approval_reviews WHERE approval_id = ?').get(pending.id).review_json)) + const output = path.resolve(review.body.cwd, review.body.arguments.path) + expect(output.startsWith(`${instance.home}${path.sep}`)).toBe(true) + if (phase === 1) { + expect(await fs.readFile(output, 'utf8')).toBe('Approved exactly once 1 😀') + const rejected = await page.evaluate(async ({ approvalId, runId, reviewVersion }) => { + try { + await window.lexoraDesktop.localChat.approvals.deny(approvalId, { runId, reviewVersion }) + return null + } + catch (error) { return String(error) } + }, { approvalId: pending.id, runId: pending.run_id, reviewVersion: review.reviewVersion }) + expect(rejected).toContain('APPROVAL_ALREADY_RESOLVED') + expect(read(db => db.prepare('SELECT * FROM approval_decisions WHERE approval_id = ?').get(pending.id))).toEqual(decision) + expect(decision.actor_id).not.toBe(read(db => db.prepare('SELECT local_actor_id FROM storage_profile WHERE id = 1').get().local_actor_id)) + } + else { + await expect(fs.stat(output)).rejects.toMatchObject({ code: 'ENOENT' }) + } + if (phase === 4) { + expect(receipts[phase].receipt.decision).toBe('accepted') + expect(decision.actor_id).not.toBe(read(db => db.prepare('SELECT local_actor_id FROM storage_profile WHERE id = 1').get().local_actor_id)) + } + evidence.push({ phase, approvalId: pending.id, runId: pending.run_id, decision, receipt: receipts[phase].receipt }) + } + if (serviceMode === 'daemon') { + await checkpoint('final', 'ready') + await page.screenshot({ path: test.info().outputPath('desktop-final.png') }) + await desktop.app.detachMain() + await page.evaluate(() => window.lexoraDesktop.settings.update({ desktop: { exitBehavior: 'stop_service' } })) + const closed = page.waitForEvent('close') + await page.evaluate(() => { + void window.lexoraDesktop.commands.execute('app.quit') + }) + await closed + expect(await checkpoint('final', 'done')).toEqual({ disconnected: true, outboxUnchanged: true }) + await expect.poll(() => desktop.app.process().exitCode).toBe(0) + } + const outcome = await instrument + await fs.writeFile(test.info().outputPath('android-instrumentation.txt'), outcome.result?.stdout ?? String(outcome.error)) + expect(outcome.error).toBeUndefined() + expect(outcome.result.stdout).toMatch(/OK \(1 test\)/) + expect(outcome.result.stdout).not.toMatch(/INSTRUMENTATION_STATUS_CODE: -[234]|FAILURES/) + for (const phase of ['0', '2', 'final']) { + const file = `files/approval-${token}-${phase}.png` + files.push(file) + const capture = await execute(process.env.ADB ?? 'adb', ['-s', serial, 'exec-out', 'run-as', appPackage, 'cat', file], { encoding: 'buffer', maxBuffer: 8 * 1024 * 1024 }) + await fs.writeFile(test.info().outputPath(`android-${phase}.png`), capture.stdout) + } + await fs.writeFile(test.info().outputPath('approval-evidence.json'), JSON.stringify({ evidence, modelRequests: requests.length, coreAdapter: serviceMode, syntheticProvider: true }, null, 2)) + if (!page.isClosed()) + await page.screenshot({ path: test.info().outputPath('desktop-final.png') }) + } + finally { + await adb('shell', 'am', 'force-stop', appPackage) + const interrupted = await instrument + if (interrupted) + await fs.writeFile(test.info().outputPath('android-instrumentation.txt'), interrupted.result?.stdout ?? String(interrupted.error)) + await adb('shell', 'rm', '-f', remoteConfig) + if (files.length) + await adb('shell', 'run-as', appPackage, 'rm', '-f', ...files) + await adb('reverse', '--remove', `tcp:${port}`) + await fs.rm(configuration, { force: true }) + for (const device of page.isClosed() ? [] : await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.devices({}))) { + if (device.state === 'active') + await page.evaluate(deviceId => window.lexoraDesktop.localChat.privateNetwork.revoke({ deviceId }), device.deviceId) + } + if (!page.isClosed()) + await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: false })) + provider.closeAllConnections() + await new Promise(resolve => provider.close(resolve)) + } +}) diff --git a/.playwright/scripts/__tests__/mobile-branches.e2e.mjs b/.playwright/scripts/__tests__/mobile-branches.e2e.mjs new file mode 100644 index 000000000..b4c7d0010 --- /dev/null +++ b/.playwright/scripts/__tests__/mobile-branches.e2e.mjs @@ -0,0 +1,360 @@ +import { Buffer } from 'node:buffer' +import { execFile } from 'node:child_process' +import { createHash, randomUUID } from 'node:crypto' +import { once } from 'node:events' +import fs from 'node:fs/promises' +import { createServer as createProvider } from 'node:http' +import { createServer } from 'node:net' +import path from 'node:path' +import process from 'node:process' +import { DatabaseSync } from 'node:sqlite' +import { promisify } from 'node:util' +import { expect, test, useSyntheticCredentialStorage } from '../fixtures/electron.mjs' +import { prepareMobileDesktopProfile } from '../fixtures/mobile-desktop.mjs' + +const execute = promisify(execFile) +const serial = process.env.LEXORA_TEST_ANDROID_SERIAL +const serviceMode = process.env.LEXORA_TEST_MOBILE_SERVICE_MODE ?? 'utility' +const bindAddress = process.env.LEXORA_TEST_MOBILE_BIND_ADDRESS ?? '127.0.0.1' + +test('Android and Desktop browse and run separate branches concurrently without mixing drafts, questions or queues', async ({ buddy }, testInfo) => { + test.skip(!serial, 'Requires an Android device with current debug and instrumentation APKs') + test.setTimeout(420000) + const adb = (...args) => execute(process.env.ADB ?? 'adb', ['-s', serial, ...args], { timeout: 360000, maxBuffer: 8 * 1024 * 1024 }) + const bytes = async (...args) => (await execute(process.env.ADB ?? 'adb', ['-s', serial, ...args], { timeout: 15000, encoding: 'buffer', maxBuffer: 8 * 1024 * 1024 })).stdout + const token = randomUUID() + const appPackage = 'app.uselexora.mobile.dev' + const remoteConfig = `/data/local/tmp/lexora-branches-${token}.json` + const configuration = testInfo.outputPath('pairing.json') + const identityBackup = `no_backup/branch-parallel-${token}-identity` + const files = new Set() + const marker = stage => `files/branches-${token}-${stage}.json` + const gates = Object.fromEntries(['A-initial', 'B-initial', 'A-result', 'B-result'].map(key => [key, Promise.withResolvers()])) + const requests = [] + const cleanupFailures = [] + let desktop + let port + let reversed = false + let started = false + let instrument + let outcome + const provider = createProvider((request, response) => { + void (async () => { + const chunks = [] + for await (const chunk of request) chunks.push(chunk) + const body = JSON.parse(Buffer.concat(chunks).toString('utf8')) + const input = body.messages.filter(message => message.role === 'user').map(message => JSON.stringify(message.content)).join('\n') + const markers = [...input.matchAll(/Parallel (baseline [AB]|edited A|branch ([AB]) main)|Queue branch ([AB])/g)] + const selected = markers.at(-1) + const branch = selected?.[2] + const queueBranch = selected?.[3] + const agentRequest = body.tools?.some(tool => tool.function?.name === 'lexora_ask_user') === true + const calls = body.messages.flatMap(message => message.tool_calls ?? []) + const ask = calls.find(call => call.function?.name === 'lexora_ask_user') + const waited = calls.some(call => call.function?.name === 'lexora_wait_user') + const phase = branch && agentRequest ? !ask ? 'initial' : !waited ? 'wait' : 'result' : queueBranch ? 'queue' : 'baseline' + const record = { branch: branch ?? queueBranch ?? null, phase, receivedAt: Date.now(), closedAt: null, body } + requests.push(record) + response.once('close', () => { + record.closedAt = Date.now() + }) + const common = { id: `branch-${requests.length}`, model: 'fixture-model', object: 'chat.completion.chunk', created: 1 } + response.writeHead(200, { 'content-type': 'text/event-stream' }) + const emit = (delta, finish = null) => response.write(`data: ${JSON.stringify({ ...common, choices: [{ index: 0, delta, finish_reason: finish }] })}\n\n`) + const hold = async key => Promise.race([gates[key].promise.then(() => true), once(response, 'close').then(() => false)]) + let tool + if (phase === 'initial') { + emit({ role: 'assistant', content: `${branch} 并行输出开始 ${token}\n` }) + if (!await hold(`${branch}-initial`)) + return + tool = { id: `branch-${branch}-ask`, name: 'lexora_ask_user', arguments: JSON.stringify({ questions: [{ id: `answer-${branch}`, question: `只回答分支 ${branch} 的问题`, kind: 'text', options: [] }] }) } + } + else if (phase === 'wait') { + const result = body.messages.find(message => message.role === 'tool' && message.tool_call_id === ask.id) + const content = typeof result?.content === 'string' ? result.content : result?.content?.map(item => item.text ?? '').join('') ?? '' + const requestId = content.match(/"requestId"\s*:\s*"([^"]+)"/)?.[1] + if (!requestId) + throw new Error('Actual ask_user receipt did not contain requestId') + tool = { id: `branch-${branch}-wait`, name: 'lexora_wait_user', arguments: JSON.stringify({ requestId }) } + } + else if (phase === 'result') { + if (!JSON.stringify(body.messages).includes(`分支 ${branch} 的回答 ${token}`)) + throw new Error('Answer was delivered to the wrong branch') + emit({ role: 'assistant', content: `${branch} 已收到本分支回答 ${token}\n` }) + if (!await hold(`${branch}-result`)) + return + emit({ content: `${branch} 并行完成 ${token}` }) + } + else { + const text = queueBranch ? `${queueBranch} 队列独立完成 ${token}` : selected?.[1] === 'edited A' ? `编辑 A 的新分支结果 ${token}` : `历史 ${selected?.[1]?.endsWith('B') ? 'B' : 'A'} 原文 ${token}` + emit({ role: 'assistant', content: text }) + } + if (tool) + emit({ role: 'assistant', tool_calls: [{ index: 0, id: tool.id, type: 'function', function: { name: tool.name, arguments: tool.arguments } }] }) + emit({}, tool ? 'tool_calls' : 'stop') + response.end('data: [DONE]\n\n') + })().catch(error => response.destroy(error)) + }) + const checkpoint = async (stage) => { + files.add(marker(stage)) + let value + await expect.poll(async () => { + try { + value = JSON.parse((await adb('shell', 'run-as', appPackage, 'cat', marker(stage))).stdout) + return true + } + catch { + if (outcome) + throw new Error(outcome.result?.stdout ?? String(outcome.error)) + return false + } + }, { timeout: 90000 }).toBe(true) + return value + } + const signal = async (stage) => { + files.add(marker(stage)) + await adb('shell', 'run-as', appPackage, 'touch', marker(stage)) + } + const restorePhone = async () => { + if (!started) + return + await adb('shell', 'am', 'force-stop', appPackage) + let manifest + for (const suffix of ['.json', '.json.bak']) { + manifest = await adb('shell', 'run-as', appPackage, 'cat', `${identityBackup}${suffix}`).then(value => JSON.parse(value.stdout), () => null) + if (manifest) + break + } + if (!manifest) + return + let failure + try { + const result = await adb('shell', 'am', 'instrument', '-w', '-r', '-e', 'class', 'app.uselexora.mobile.BranchParallelInstrumentedTest', '-e', 'branchParallelToken', token, '-e', 'branchParallelRestoreOnly', 'true', `${appPackage}.test/androidx.test.runner.AndroidJUnitRunner`) + await fs.writeFile(testInfo.outputPath('android-restoration.txt'), result.stdout + result.stderr) + expect(result.stdout).toMatch(/OK \(1 test\)/) + expect(result.stdout).not.toMatch(/INSTRUMENTATION_STATUS_CODE: -[234]|FAILURES/) + } + catch (error) { failure = error } + finally { + await adb('shell', 'am', 'force-stop', appPackage) + const original = await bytes('exec-out', 'run-as', appPackage, 'cat', `${identityBackup}.bin`) + expect(createHash('sha256').update(original).digest('hex')).toBe(manifest.sha256) + if (manifest.present) { + await adb('shell', 'run-as', appPackage, 'cp', `${identityBackup}.bin`, `${identityBackup}.restore`) + await adb('shell', 'run-as', appPackage, 'mv', `${identityBackup}.restore`, 'no_backup/device-identity.bin') + await adb('shell', 'run-as', appPackage, 'rm', '-f', 'no_backup/device-identity.bin.bak', 'no_backup/device-identity.bin.new') + expect(createHash('sha256').update(await bytes('exec-out', 'run-as', appPackage, 'cat', 'no_backup/device-identity.bin')).digest('hex')).toBe(manifest.sha256) + } + else { + await adb('shell', 'run-as', appPackage, 'rm', '-f', 'no_backup/device-identity.bin', 'no_backup/device-identity.bin.bak', 'no_backup/device-identity.bin.new') + } + } + if (failure) + throw failure + await adb('shell', 'run-as', appPackage, 'rm', '-f', `${identityBackup}.bin`, `${identityBackup}.json`, `${identityBackup}.json.bak`) + } + try { + provider.listen(0, '127.0.0.1') + await once(provider, 'listening') + const instance = await buddy.createInstance('mobile-branches') + await prepareMobileDesktopProfile(instance.home, testInfo.outputPath('profile-preparation')) + desktop = await instance.launch({ storageAuthority: 'sqlite', serviceMode }) + await useSyntheticCredentialStorage(desktop) + const { page } = desktop + const read = (query) => { + const database = new DatabaseSync(path.join(instance.home, 'buddy/buddy.sqlite3'), { readOnly: true }) + try { + return query(database) + } + finally { database.close() } + } + await page.evaluate(async (baseUrl) => { + await window.lexoraDesktop.settings.update({ runtime: { codemode: 'off', modelRetryLimit: 0 }, desktop: { chat: { followUpBehavior: 'queue' } } }) + const providers = window.lexoraDesktop.localChat.providers + await providers.upsertCustom({ id: 'branches-fixture', displayName: 'Branch verification', api: 'openai-completions', baseUrl, enabled: true, models: [{ id: 'fixture-model', name: 'Branch verification', input: ['text'], reasoning: false, contextWindow: 128000, maxTokens: 1024 }] }) + const stop = providers.onAuthChallenge((challenge) => { + if (challenge.providerId === 'branches-fixture' && challenge.type === 'secret') + void providers.respondToAuth(challenge.challengeId, 'offline-fixture-key') + }) + try { + await providers.login('branches-fixture', 'api_key') + } + finally { stop() } + await providers.setDefaultModel({ providerId: 'branches-fixture', modelId: 'fixture-model', reasoning: null }) + }, `http://127.0.0.1:${provider.address().port}/v1`) + await page.reload() + const composer = page.locator('.desktop-chat-composer__prosemirror:visible') + await composer.fill(`Parallel baseline A ${token}`) + await page.getByRole('button', { name: '发送消息', exact: true }).click() + await expect.poll(() => read(db => db.prepare('SELECT COUNT(*) AS count FROM runs WHERE status = \'completed\'').get().count)).toBe(1) + const first = read(db => db.prepare('SELECT id, conversation_id, branch_id, triggering_message_id FROM runs ORDER BY rowid LIMIT 1').get()) + const conversationId = first.conversation_id + const branchA = first.branch_id + const originalRecords = read(db => db.prepare('SELECT id, content_json FROM messages WHERE branch_id = ? ORDER BY rowid').all(branchA)) + const title = `分支并行验收 ${token.slice(0, 8)}` + const fork = await page.evaluate(async ({ conversationId, branchId, userMessageId, token, title }) => { + const api = window.lexoraDesktop.localChat + const current = await api.conversations.get(conversationId) + await api.conversations.rename(conversationId, title, current.titleRevision) + const draft = await api.composerDrafts.open({ draftId: crypto.randomUUID(), scope: { kind: 'message_edit', conversationId, branchId, userMessageId }, initialContent: { version: 1, body: [{ type: 'paragraph', content: [{ type: 'text', text: `Parallel baseline B ${token}` }] }], panelResourceIds: [] }, initialModelSelection: null, initialExecutionConfig: { executionProfile: current.executionProfile, approvalPolicy: current.approvalPolicy } }) + return api.chat.editUserMessage({ conversationId, userMessageId, draftId: draft.draftId, expectedRevision: draft.revision, requestId: crypto.randomUUID() }) + }, { conversationId, branchId: branchA, userMessageId: first.triggering_message_id, token, title }) + const branchB = fork.branchId + await expect.poll(() => read(db => db.prepare('SELECT status FROM runs WHERE id = ?').get(fork.runId).status)).toBe('completed') + expect(branchB).not.toBe(branchA) + await page.locator('[data-testid="conversation-branches"]:visible').click() + await page.getByTestId(`conversation-branch-${branchB}`).click() + const branchView = page.locator('[data-testid="conversation-branches"]:visible') + await expect(branchView).toHaveAttribute('data-branch-id', branchB) + const listener = createServer() + await new Promise(resolve => listener.listen(0, bindAddress, resolve)) + port = listener.address().port + await new Promise(resolve => listener.close(resolve)) + await page.evaluate(value => window.lexoraDesktop.localChat.privateNetwork.configure(value), { enabled: true, bindAddress, port }) + if (bindAddress === '127.0.0.1') { + await adb('reverse', `tcp:${port}`, `tcp:${port}`) + reversed = true + } + const pairing = await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.issuePairing({})) + await fs.writeFile(configuration, JSON.stringify({ pairing, title, conversationId, branchA, branchB }), { mode: 0o600 }) + await adb('push', configuration, remoteConfig) + await fs.rm(configuration) + started = true + instrument = adb('shell', 'am', 'instrument', '-w', '-r', '-e', 'class', 'app.uselexora.mobile.BranchParallelInstrumentedTest', '-e', 'branchParallelToken', token, `${appPackage}.test/androidx.test.runner.AndroidJUnitRunner`).then(result => ({ result }), error => ({ error })).then((value) => { + outcome = value + return value + }) + const phone = await checkpoint('phone-started') + await expect(branchView).toHaveAttribute('data-branch-id', branchB) + await composer.fill(`Parallel branch B main ${token}`) + await page.getByRole('button', { name: '发送消息', exact: true }).click() + await expect.poll(() => requests.filter(value => value.phase === 'initial').map(value => value.branch).sort(), { timeout: 45000 }).toEqual(['A', 'B']) + const runB = read(db => db.prepare('SELECT id FROM runs WHERE branch_id = ? AND status = \'running\'').get(branchB)).id + const concurrent = read(db => db.prepare('SELECT id, branch_id, buddy_id FROM runs WHERE id IN (?, ?) AND status = \'running\'').all(phone.runId, runB)) + expect(concurrent).toHaveLength(2) + expect(new Set(concurrent.map(run => run.buddy_id)).size).toBe(1) + expect(requests.filter(value => value.phase === 'initial').every(value => value.closedAt === null)).toBe(true) + await expect(page.getByText(`B 并行输出开始 ${token}`, { exact: false })).toBeVisible() + await expect(page.getByText(`A 并行输出开始 ${token}`, { exact: false })).toHaveCount(0) + await signal('both-running') + await expect(composer).toHaveText('') + const queueText = `Queue branch B ${token}` + await composer.fill(queueText) + await page.locator('.desktop-chat-composer__actions:visible').getByRole('button', { name: '加入队列', exact: true }).click() + await expect(composer).toHaveText('') + const queuedRow = page.locator('.chat-message-queue__row:visible').filter({ hasText: queueText }) + await expect(queuedRow).toBeVisible() + const queuedBranchB = await page.evaluate(value => window.lexoraDesktop.localChat.chat.listQueue(value), { conversationId, branchId: branchB }) + expect(queuedBranchB).toHaveLength(1) + const queueB = queuedBranchB[0].id + await expect(queuedRow).toHaveAttribute('data-queued-message', queueB) + expect(queuedBranchB[0]).toMatchObject({ conversationId, branchId: branchB, state: 'waiting', delivery: { mode: 'next' }, content: { userContent: { body: [{ type: 'paragraph', content: [{ type: 'text', text: queueText }] }] } } }) + const queuedA = await checkpoint('queue-a') + const desktopDraft = `电脑 B 独立草稿 ${token}` + await composer.fill(desktopDraft) + for (const branch of ['A', 'B']) gates[`${branch}-initial`].resolve() + const questions = branchId => page.evaluate(value => window.lexoraDesktop.localChat.questions.list(value), { conversationId, branchId }) + await expect.poll(() => questions(branchA), { timeout: 45000 }).toMatchObject([{ status: 'pending', branchId: branchA }]) + await expect.poll(() => questions(branchB), { timeout: 45000 }).toMatchObject([{ status: 'pending', branchId: branchB }]) + await signal('questions-ready') + expect(await checkpoint('views-isolated')).toMatchObject({ recreated: true }) + await expect(branchView).toHaveAttribute('data-branch-id', branchB) + await expect(composer).toHaveText(desktopDraft) + await page.reload() + await expect(branchView).toHaveAttribute('data-branch-id', branchB) + await expect(composer).toHaveText(desktopDraft) + await signal('answer-a') + await checkpoint('answered-a') + await expect.poll(() => requests.some(value => value.phase === 'result' && value.branch === 'A'), { timeout: 45000 }).toBe(true) + await signal('stop-a') + await checkpoint('stopped-a') + expect(read(db => db.prepare('SELECT status FROM runs WHERE id = ?').get(runB).status)).toBe('running') + expect((await questions(branchB))[0].status).toBe('pending') + expect(read(db => db.prepare('SELECT state FROM chat_queue WHERE id = ?').get(queuedA.queueId).state)).toBe('paused') + const queuedB = await page.evaluate(value => window.lexoraDesktop.localChat.chat.listQueue(value), { conversationId, branchId: branchB }) + expect(queuedB).toHaveLength(1) + expect(queuedB[0]).toMatchObject({ id: queueB, branchId: branchB, state: 'waiting' }) + const questionB = (await questions(branchB))[0] + await page.evaluate(value => window.lexoraDesktop.localChat.questions.answer(value), { conversationId, branchId: branchB, requestId: questionB.id, answers: [{ questionId: 'answer-B', selectedOptionIds: [], text: `分支 B 的回答 ${token}` }] }) + await expect.poll(() => requests.some(value => value.phase === 'result' && value.branch === 'B'), { timeout: 45000 }).toBe(true) + await signal('edit-a') + const edited = await checkpoint('edited-a') + expect(edited.branchId).not.toBe(branchA) + expect(edited.branchId).not.toBe(branchB) + expect(read(db => db.prepare('SELECT status FROM runs WHERE id = ?').get(runB).status)).toBe('running') + expect(read(db => db.prepare(`SELECT id, content_json FROM messages WHERE id IN (${originalRecords.map(() => '?').join(',')}) ORDER BY rowid`).all(...originalRecords.map(value => value.id)))).toEqual(originalRecords) + await expect(branchView).toHaveAttribute('data-branch-id', branchB) + await expect(composer).toHaveText(desktopDraft) + await signal('restore-a') + expect(await checkpoint('reconnected-a')).toMatchObject({ draft: `手机 A 独立草稿😀 ${token}` }) + await page.reload() + await expect(branchView).toHaveAttribute('data-branch-id', branchB) + await expect(composer).toHaveText(desktopDraft) + gates['B-result'].resolve() + await expect.poll(() => read(db => db.prepare('SELECT status FROM runs WHERE id = ?').get(runB).status)).toBe('completed') + await expect.poll(() => requests.some(value => value.phase === 'queue' && value.branch === 'B')).toBe(true) + await expect.poll(() => read(db => db.prepare('SELECT COUNT(*) AS count FROM runs WHERE branch_id = ? AND status IN (\'running\',\'queued\')').get(branchB).count)).toBe(0) + await signal('b-finished') + const done = await checkpoint('done') + const result = await instrument + await fs.writeFile(testInfo.outputPath('android-instrumentation.txt'), result.result?.stdout ?? String(result.error)) + expect(result.error).toBeUndefined() + expect(result.result.stdout).toMatch(/OK \(1 test\)/) + expect(result.result.stdout).not.toMatch(/INSTRUMENTATION_STATUS_CODE: -[234]|FAILURES/) + const evidence = JSON.parse((await adb('shell', 'run-as', appPackage, 'cat', marker('done'))).stdout) + expect(evidence).toMatchObject({ ...done, originalIdentityRestored: true, independentDrafts: true, noGlobalActivation: true, landscapeImeVisible: true }) + const text = branch => read(db => db.prepare('SELECT content_json FROM messages WHERE branch_id = ? ORDER BY rowid').all(branch)).map(value => value.content_json).join('\n') + expect(text(branchA)).not.toContain('B 并行') + expect(text(branchB)).not.toContain('A 并行') + expect(text(edited.branchId)).not.toContain('B 并行') + expect(read(db => db.prepare('SELECT active_branch_id FROM conversations WHERE id = ?').get(conversationId).active_branch_id)).toBe(branchA) + expect((await questions(branchA))[0]).toMatchObject({ status: 'answered', response: { answers: [{ text: `分支 A 的回答 ${token}` }] } }) + expect((await questions(branchB))[0]).toMatchObject({ status: 'answered', response: { answers: [{ text: `分支 B 的回答 ${token}` }] } }) + await fs.writeFile(testInfo.outputPath('branch-evidence.json'), JSON.stringify({ ...evidence, concurrent, queueA: queuedA.queueId, queueB, runB, edited, simultaneousProviderRequests: true, desktopBranchRestored: branchB, syntheticProvider: true, coreAdapter: serviceMode }, null, 2)) + await page.screenshot({ path: testInfo.outputPath('desktop-branch-b.png') }) + } + finally { + for (const stage of ['isolated', 'restored', 'landscape-ime']) { + const file = `files/branches-${token}-${stage}.png` + files.add(file) + const image = await bytes('exec-out', 'run-as', appPackage, 'cat', file).catch(() => null) + if (image?.subarray(0, 8).equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]))) + await fs.writeFile(testInfo.outputPath(`android-${stage}.png`), image) + } + const touchFile = `files/branches-${token}-landscape-touch.json` + files.add(touchFile) + const touch = await adb('shell', 'run-as', appPackage, 'cat', touchFile).then(result => JSON.parse(result.stdout)).catch(() => null) + if (touch) + await fs.writeFile(testInfo.outputPath('android-landscape-touch.json'), JSON.stringify(touch, null, 2)) + await restorePhone().catch(error => cleanupFailures.push(error)) + const result = await instrument + if (result) + await fs.writeFile(testInfo.outputPath('android-instrumentation.txt'), result.result?.stdout ?? String(result.error)) + for (const gate of Object.values(gates)) gate.resolve() + provider.closeAllConnections() + const cleanup = await Promise.allSettled([ + adb('shell', 'rm', '-f', remoteConfig), + adb('shell', 'run-as', appPackage, 'rm', '-f', ...files), + fs.rm(configuration, { force: true }), + new Promise((resolve, reject) => provider.close(error => error ? reject(error) : resolve())), + (async () => { + try { + if (desktop) { + for (const device of await desktop.page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.devices({}))) { + if (device.state === 'active') + await desktop.page.evaluate(deviceId => window.lexoraDesktop.localChat.privateNetwork.revoke({ deviceId }), device.deviceId) + } + await desktop.page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: false })) + } + } + finally { + if (reversed) + await adb('reverse', '--remove', `tcp:${port}`) + } + })(), + ]) + cleanupFailures.push(...cleanup.filter(value => value.status === 'rejected').map(value => value.reason)) + } + if (cleanupFailures.length) + throw new AggregateError(cleanupFailures, 'Could not restore the isolated mobile branches test') +}) diff --git a/.playwright/scripts/__tests__/mobile-content.e2e.mjs b/.playwright/scripts/__tests__/mobile-content.e2e.mjs new file mode 100644 index 000000000..aea305757 --- /dev/null +++ b/.playwright/scripts/__tests__/mobile-content.e2e.mjs @@ -0,0 +1,98 @@ +import { execFile } from 'node:child_process' +import { randomUUID } from 'node:crypto' +import fs from 'node:fs/promises' +import { createServer } from 'node:net' +import process from 'node:process' +import { promisify } from 'node:util' +import { expect, test, useSyntheticCredentialStorage } from '../fixtures/electron.mjs' +import { prepareMobileDesktopProfile } from '../fixtures/mobile-desktop.mjs' + +const execute = promisify(execFile) +const serial = process.env.LEXORA_TEST_ANDROID_SERIAL +const serviceMode = process.env.LEXORA_TEST_MOBILE_SERVICE_MODE ?? 'utility' + +test('Legacy local Android content remains intact and read only while unsupported actions remain blocked', async ({ buddy }, testInfo) => { + test.skip(!serial, 'Requires an isolated Android device with debug and instrumentation APKs') + test.setTimeout(180000) + const adb = (...args) => execute(process.env.ADB ?? 'adb', ['-s', serial, ...args], { timeout: 120000, maxBuffer: 8 * 1024 * 1024 }) + const token = randomUUID() + const appPackage = 'app.uselexora.mobile.dev' + const remoteConfig = `/data/local/tmp/lexora-content-${token}.json` + const configuration = testInfo.outputPath('pairing.json') + const instance = await buddy.createInstance('mobile-content') + await prepareMobileDesktopProfile(instance.home, testInfo.outputPath('profile-preparation')) + const desktop = await instance.launch({ storageAuthority: 'sqlite', serviceMode }) + const { page } = desktop + const editor = page.locator('.desktop-workbench-area__tasks .tiptap:visible').first() + await expect(editor).toBeVisible() + await useSyntheticCredentialStorage(desktop) + await expect(editor).toBeVisible() + const original = '跨端中文😀e\u0301' + await editor.fill(original) + const getDraft = () => page.evaluate(async text => (await window.lexoraDesktop.localChat.composerDrafts.list()).find(draft => JSON.stringify(draft.content).includes(text)), original) + await expect.poll(getDraft).toBeTruthy() + const draft = await getDraft() + const content = { + version: 1, + body: [{ type: 'paragraph', content: [ + { type: 'text', text: original }, + { type: 'hard_break' }, + { type: 'actor_ref', actorId: 'fixture-reader', name: '阅读者' }, + { type: 'quote_ref', quoteKind: 'message', quoteId: 'frozen-quote' }, + { type: 'session_ref', sessionId: 'historical-session' }, + { type: 'prompt_directive', directive: 'slash_command', commandMode: 'prompt', value: '/review' }, + ] }], + panelResourceIds: [], + quotes: [{ id: 'frozen-quote', text: '引用原文😀', textOffset: 2, source: { conversationId: 'archived-task', branchId: 'archived-branch', messageId: 'archived-reply', role: 'assistant', runId: null } }], + sessionReferences: [{ id: 'historical-session', title: '原来的任务' }], + } + const saved = await page.evaluate(({ draft, content }) => window.lexoraDesktop.localChat.composerDrafts.save({ draftId: draft.draftId, expectedRevision: draft.revision, content, modelSelection: draft.modelSelection, executionConfig: draft.executionConfig, participants: draft.participants ?? null }), { draft, content }) + const large = await page.evaluate(async (executionConfig) => { + const id = crypto.randomUUID() + return window.lexoraDesktop.localChat.composerDrafts.open({ draftId: id, scope: { kind: 'task', draftId: id, spaceId: null }, initialContent: { version: 1, body: [{ type: 'paragraph', content: [{ type: 'text', text: '大段内容'.repeat(30000) }] }], panelResourceIds: [] }, initialModelSelection: null, initialExecutionConfig: executionConfig }) + }, draft.executionConfig) + const listener = createServer() + await new Promise(resolve => listener.listen(0, '127.0.0.1', resolve)) + const port = listener.address().port + await new Promise(resolve => listener.close(resolve)) + let reversed = false + let instrument + try { + await page.evaluate(port => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: true, bindAddress: '127.0.0.1', port }), port) + const pairing = await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.issuePairing({})) + await fs.writeFile(configuration, JSON.stringify({ pairing, draftId: draft.draftId, largeDraftId: large.draftId }), { mode: 0o600 }) + await adb('reverse', `tcp:${port}`, `tcp:${port}`) + reversed = true + await adb('push', configuration, remoteConfig) + await fs.rm(configuration) + instrument = adb('shell', 'am', 'instrument', '-w', '-r', '-e', 'class', 'app.uselexora.mobile.ContentInstrumentedTest', '-e', 'contentWorkflowToken', token, `${appPackage}.test/androidx.test.runner.AndroidJUnitRunner`).then(result => ({ result }), error => ({ error })) + const outcome = await instrument + await fs.writeFile(testInfo.outputPath('android-instrumentation.txt'), outcome.result?.stdout ?? String(outcome.error)) + expect(outcome.error).toBeUndefined() + expect(outcome.result.stdout).toMatch(/OK \(1 test\)/) + expect(outcome.result.stdout).not.toMatch(/INSTRUMENTATION_STATUS_CODE: -[234]|FAILURES/) + const result = JSON.parse((await adb('shell', 'run-as', appPackage, 'cat', `files/content-${token}-result.json`)).stdout) + expect(result).toMatchObject({ richInputReadOnly: true, unsupportedInputPreserved: true, forbiddenActionRejected: true, noImplicitSend: true }) + expect(result.content).toEqual(saved.content) + expect((await page.evaluate(id => window.lexoraDesktop.localChat.composerDrafts.get(id), draft.draftId)).content).toEqual(saved.content) + const screenshot = await execute(process.env.ADB ?? 'adb', ['-s', serial, 'exec-out', 'run-as', appPackage, 'cat', `files/content-${token}.png`], { encoding: 'buffer', maxBuffer: 8 * 1024 * 1024 }) + await fs.writeFile(testInfo.outputPath('android-content.png'), screenshot.stdout) + await fs.writeFile(testInfo.outputPath('roundtrip.json'), JSON.stringify({ ...result, originalDesktopPreserved: true, coreAdapter: serviceMode }, null, 2)) + } + finally { + await adb('shell', 'am', 'force-stop', appPackage) + const outcome = await instrument + if (outcome) + await fs.writeFile(testInfo.outputPath('android-instrumentation.txt'), outcome.result?.stdout ?? String(outcome.error)) + await adb('shell', 'rm', '-f', remoteConfig) + await adb('shell', 'run-as', appPackage, 'rm', '-f', `files/content-${token}-result.json`, `files/content-${token}.png`) + if (reversed) + await adb('reverse', '--remove', `tcp:${port}`) + await fs.rm(configuration, { force: true }) + for (const device of await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.devices({}))) { + if (device.state === 'active') + await page.evaluate(deviceId => window.lexoraDesktop.localChat.privateNetwork.revoke({ deviceId }), device.deviceId) + } + await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: false })) + } +}) diff --git a/.playwright/scripts/__tests__/mobile-desktop.e2e.mjs b/.playwright/scripts/__tests__/mobile-desktop.e2e.mjs new file mode 100644 index 000000000..e1d63f3c7 --- /dev/null +++ b/.playwright/scripts/__tests__/mobile-desktop.e2e.mjs @@ -0,0 +1,199 @@ +import { Buffer } from 'node:buffer' +import { execFile } from 'node:child_process' +import { createHash, randomUUID } from 'node:crypto' +import { once } from 'node:events' +import fs from 'node:fs/promises' +import { createServer as createProvider } from 'node:http' +import { createServer } from 'node:net' +import path from 'node:path' +import process from 'node:process' +import { DatabaseSync } from 'node:sqlite' +import { promisify } from 'node:util' +import { expect, test, useSyntheticCredentialStorage } from '../fixtures/electron.mjs' +import { prepareMobileDesktopProfile } from '../fixtures/mobile-desktop.mjs' + +const execute = promisify(execFile) +const serial = process.env.LEXORA_TEST_ANDROID_SERIAL +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' + +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') + test.setTimeout(180000) + expect(!networkRecovery || !useAdbReverse, 'Wi-Fi recovery requires a real private-network address').toBe(true) + const adb = (...args) => execute(process.env.ADB ?? 'adb', ['-s', serial, ...args], { timeout: 90000, maxBuffer: 8 * 1024 * 1024 }) + const token = randomUUID() + const appPackage = 'app.uselexora.mobile.dev' + const remoteConfig = `/data/local/tmp/lexora-desktop-${token}.json` + const ready = `files/desktop-${token}-ready.json` + const resume = `files/desktop-${token}-resume.json` + const instance = await buddy.createInstance('mobile-desktop') + await prepareMobileDesktopProfile(instance.home, test.info().outputPath('profile-preparation')) + const desktop = await instance.launch({ storageAuthority: 'sqlite', serviceMode }) + await useSyntheticCredentialStorage(desktop) + const { page } = desktop + const avatar = await page.evaluate(async () => { + const buddies = window.lexoraDesktop.localChat.buddies + const buddy = await buddies.get('default') + const canvas = document.createElement('canvas') + canvas.width = canvas.height = 64 + const context = canvas.getContext('2d') + context.fillStyle = '#1d9076' + context.fillRect(0, 0, 64, 64) + context.fillStyle = '#f2d97d' + context.fillRect(16, 16, 32, 32) + const avatar = canvas.toDataURL('image/png') + await buddies.update({ buddyId: buddy.id, expectedRevision: buddy.revision, avatar }) + return avatar + }) + const avatarHash = createHash('sha256').update(Buffer.from(avatar.split(',')[1], 'base64')).digest('hex') + const requests = [] + const provider = createProvider((request, response) => { + void (async () => { + const chunks = [] + for await (const chunk of request) chunks.push(chunk) + requests.push(JSON.parse(Buffer.concat(chunks).toString('utf8'))) + const common = { id: 'local-input', model: 'fixture-model', object: 'chat.completion.chunk', created: 1 } + response.writeHead(200, { 'content-type': 'text/event-stream' }) + response.write(`data: ${JSON.stringify({ ...common, choices: [{ index: 0, delta: { role: 'assistant', content: '输入和附件已收到' }, finish_reason: null }] })}\n\n`) + response.write(`data: ${JSON.stringify({ ...common, choices: [{ index: 0, delta: {}, finish_reason: 'stop' }] })}\n\n`) + response.end('data: [DONE]\n\n') + })().catch(() => response.destroy()) + }) + provider.listen(0, '127.0.0.1') + await once(provider, 'listening') + await page.evaluate(async (baseUrl) => { + await window.lexoraDesktop.settings.update({ runtime: { codemode: 'off', modelRetryLimit: 0 } }) + const providers = window.lexoraDesktop.localChat.providers + await providers.upsertCustom({ id: 'input-fixture', displayName: 'Input fixture', api: 'openai-completions', baseUrl, enabled: true, models: [{ id: 'fixture-model', name: 'Input model', input: ['text'], reasoning: false, contextWindow: 128000, maxTokens: 1024 }] }) + const stop = providers.onAuthChallenge((challenge) => { + if (challenge.providerId === 'input-fixture' && challenge.type === 'secret') + void providers.respondToAuth(challenge.challengeId, 'offline-fixture-key') + }) + try { + await providers.login('input-fixture', 'api_key') + } + finally { + stop() + } + await providers.setDefaultModel({ providerId: 'input-fixture', modelId: 'fixture-model', reasoning: null }) + }, `http://127.0.0.1:${provider.address().port}/v1`) + await page.reload() + const read = (query) => { + const database = new DatabaseSync(path.join(instance.home, 'buddy/buddy.sqlite3'), { readOnly: true }) + try { + return query(database) + } + finally { + database.close() + } + } + const editor = page.locator('.desktop-workbench-area__tasks .tiptap:visible').first() + await expect(editor).toBeVisible() + const originalText = 'Desktop 原始输入 😀' + await editor.fill(originalText) + const getDraft = () => page.evaluate(async text => (await window.lexoraDesktop.localChat.composerDrafts.list()).find(draft => JSON.stringify(draft.content).includes(text)), originalText) + await expect.poll(getDraft).toBeTruthy() + const draft = await getDraft() + const listener = createServer() + await new Promise(resolve => listener.listen(0, bindAddress, resolve)) + const port = listener.address().port + await new Promise(resolve => listener.close(resolve)) + const configuration = test.info().outputPath('pairing.json') + let instrument + 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 }) + 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 })) + let checkpoint + await expect.poll(async () => { + try { + checkpoint = JSON.parse((await adb('shell', 'run-as', appPackage, 'cat', ready)).stdout) + return checkpoint.attachmentCount + } + catch { return null } + }, { timeout: 45000 }).toBe(1) + await expect(editor).toContainText(originalText) + const updatedText = 'Desktop 并发编辑保留 😀' + await editor.fill(updatedText) + await expect.poll(() => page.evaluate(async id => JSON.stringify((await window.lexoraDesktop.localChat.composerDrafts.get(id)).content), draft.draftId)).toContain(updatedText) + expect(read(db => db.prepare('SELECT COUNT(*) AS count FROM composer_drafts WHERE owner_actor_id = ?').get(checkpoint.ownerDeviceId).count)).toBe(0) + expect(read(db => db.prepare('SELECT COUNT(*) AS count FROM remote_uploads WHERE actor_id = ? AND input_id = ? AND state = \'ready\'').get(checkpoint.ownerDeviceId, checkpoint.inputId).count)).toBe(1) + const database = new DatabaseSync(path.join(instance.home, 'buddy/buddy.sqlite3')) + try { + database.prepare('UPDATE remote_uploads SET state = \'expired\' WHERE actor_id = ? AND input_id = ? AND state = \'ready\'').run(checkpoint.ownerDeviceId, checkpoint.inputId) + } + finally { + database.close() + } + const continuation = test.info().outputPath('resume.json') + 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) { + 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 } + }, { timeout: 45000 }).toBe(true) + expect((await adb('shell', 'settings', 'get', 'global', 'wifi_on')).stdout.trim()).toBe('1') + await waitMarker('network-ready') + await adb('shell', 'svc', 'wifi', 'disable') + try { + await waitMarker('offline') + } + finally { 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)) + expect(outcome.error).toBeUndefined() + expect(outcome.result.stdout).toMatch(/OK \(1 test\)/) + expect(outcome.result.stdout).not.toMatch(/INSTRUMENTATION_STATUS_CODE: -[234]|FAILURES/) + await expect(editor).toContainText(updatedText) + expect(read(db => db.prepare('SELECT COUNT(*) AS count FROM composer_drafts WHERE owner_actor_id = ?').get(checkpoint.ownerDeviceId).count)).toBe(0) + expect(read(db => db.prepare('SELECT COUNT(*) AS count FROM command_receipts WHERE actor_id = ? AND method = \'chat.submitInput\' AND decision = \'accepted\'').get(checkpoint.ownerDeviceId).count)).toBe(1) + expect(JSON.stringify(requests)).toContain('Android 本地输入与附件') + expect(JSON.stringify(requests)).toContain('附件内容完整保留') + const screenshot = await execute(process.env.ADB ?? 'adb', ['-s', serial, 'exec-out', 'run-as', appPackage, 'cat', `files/desktop-${token}.png`], { encoding: 'buffer', maxBuffer: 8 * 1024 * 1024 }) + await fs.writeFile(test.info().outputPath('android-draft.png'), screenshot.stdout) + await page.screenshot({ path: test.info().outputPath('desktop-draft.png') }) + const target = read(db => JSON.parse(db.prepare('SELECT target_json FROM command_receipts WHERE actor_id = ? AND method = \'chat.submitInput\' AND decision = \'accepted\'').get(checkpoint.ownerDeviceId).target_json)) + await page.locator(`[data-task-id="${target.conversationId}"]`).first().click() + await page.getByRole('button', { name: '分屏打开', exact: true }).click() + await expect(editor).toContainText(updatedText) + await expect(page.locator('.desktop-workbench-area__tasks').getByText('输入和附件已收到', { exact: true })).toBeVisible() + 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)) + } + finally { + if (networkRecovery) + await adb('shell', 'svc', 'wifi', 'enable') + await adb('shell', 'am', 'force-stop', appPackage) + const interrupted = await instrument + if (interrupted) + await fs.writeFile(test.info().outputPath('android-instrumentation.txt'), interrupted.result?.stdout ?? String(interrupted.error)) + await adb('shell', 'rm', '-f', remoteConfig, `/data/local/tmp/lexora-desktop-${token}-resume.json`) + await adb('shell', 'run-as', appPackage, 'rm', '-f', ready, resume, `files/desktop-${token}.png`, `files/desktop-${token}-network-ready.json`, `files/desktop-${token}-offline.json`) + if (useAdbReverse) + await adb('reverse', '--remove', `tcp:${port}`) + await fs.rm(configuration, { force: true }) + for (const device of await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.devices({}))) { + if (device.state === 'active') + await page.evaluate(deviceId => window.lexoraDesktop.localChat.privateNetwork.revoke({ deviceId }), device.deviceId) + } + await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: false })) + provider.closeAllConnections() + await new Promise(resolve => provider.close(resolve)) + } +}) diff --git a/.playwright/scripts/__tests__/mobile-devices.e2e.mjs b/.playwright/scripts/__tests__/mobile-devices.e2e.mjs new file mode 100644 index 000000000..ba628f4d1 --- /dev/null +++ b/.playwright/scripts/__tests__/mobile-devices.e2e.mjs @@ -0,0 +1,209 @@ +import { Buffer } from 'node:buffer' +import { execFile } from 'node:child_process' +import { createHash, randomUUID } from 'node:crypto' +import { once } from 'node:events' +import fs from 'node:fs/promises' +import { createServer as createProvider } from 'node:http' +import { createServer } from 'node:net' +import process from 'node:process' +import { promisify } from 'node:util' +import { expect, test, useSyntheticCredentialStorage } from '../fixtures/electron.mjs' +import { prepareMobileDesktopProfile } from '../fixtures/mobile-desktop.mjs' + +const execute = promisify(execFile) +const serial = process.env.LEXORA_TEST_ANDROID_SERIAL +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' + +test('Android switches between two real computers without mixing credentials, scopes or local input', async ({ buddy }, testInfo) => { + test.skip(!serial, 'Requires an Android device with the debug and instrumentation APKs installed') + test.setTimeout(360000) + const adb = (...args) => execute(process.env.ADB ?? 'adb', ['-s', serial, ...args], { timeout: 300000, maxBuffer: 8 * 1024 * 1024 }) + const bytes = async (...args) => (await execute(process.env.ADB ?? 'adb', ['-s', serial, ...args], { timeout: 15000, encoding: 'buffer', maxBuffer: 8 * 1024 * 1024 })).stdout + const token = randomUUID() + const appPackage = 'app.uselexora.mobile.dev' + const remoteConfig = `/data/local/tmp/lexora-devices-${token}.json` + const resultFile = `files/devices-${token}.json` + const configuration = testInfo.outputPath('pairing.json') + const computers = [] + const captures = ['computer-switch', 'workbench', 'spaces', 'unassigned', 'space-tasks', 'new-space-task', 'conversation', 'restored-second-input'] + const identityBackup = `no_backup/device-switch-${token}-identity` + const requests = [] + const provider = createProvider((request, response) => { + void (async () => { + const chunks = [] + for await (const chunk of request) chunks.push(chunk) + requests.push(JSON.parse(Buffer.concat(chunks).toString('utf8'))) + const common = { id: `devices-${requests.length}`, model: 'fixture-model', object: 'chat.completion.chunk', created: 1 } + response.writeHead(200, { 'content-type': 'text/event-stream' }) + response.write(`data: ${JSON.stringify({ ...common, choices: [{ index: 0, delta: { role: 'assistant', content: '空间验收回复:已收到这台电脑的任务。' }, finish_reason: null }] })}\n\n`) + response.write(`data: ${JSON.stringify({ ...common, choices: [{ index: 0, delta: {}, finish_reason: 'stop' }] })}\n\n`) + response.end('data: [DONE]\n\n') + })().catch(() => response.destroy()) + }) + const cleanupFailures = [] + let instrumentStarted = false + const restorePhone = async () => { + if (!instrumentStarted) + return + await adb('shell', 'am', 'force-stop', appPackage) + let manifest + for (const suffix of ['.json', '.json.bak']) { + manifest = await adb('shell', 'run-as', appPackage, 'cat', `${identityBackup}${suffix}`).then(result => JSON.parse(result.stdout), () => null) + if (manifest) + break + } + if (!manifest) + return + let cleanupError + try { + const cleanup = await adb('shell', 'am', 'instrument', '-w', '-r', '-e', 'class', 'app.uselexora.mobile.DeviceSwitchInstrumentedTest', '-e', 'deviceSwitchToken', token, '-e', 'deviceSwitchRestoreOnly', 'true', `${appPackage}.test/androidx.test.runner.AndroidJUnitRunner`) + await fs.writeFile(testInfo.outputPath('android-restoration.txt'), cleanup.stdout + cleanup.stderr) + expect(cleanup.stdout).toMatch(/OK \(1 test\)/) + expect(cleanup.stdout).not.toMatch(/INSTRUMENTATION_STATUS_CODE: -[234]|FAILURES/) + } + catch (error) { cleanupError = error } + finally { + await adb('shell', 'am', 'force-stop', appPackage) + const original = await bytes('exec-out', 'run-as', appPackage, 'cat', `${identityBackup}.bin`) + expect(createHash('sha256').update(original).digest('hex')).toBe(manifest.sha256) + if (manifest.present) { + await adb('shell', 'run-as', appPackage, 'cp', `${identityBackup}.bin`, `${identityBackup}.restore`) + await adb('shell', 'run-as', appPackage, 'mv', `${identityBackup}.restore`, 'no_backup/device-identity.bin') + await adb('shell', 'run-as', appPackage, 'rm', '-f', 'no_backup/device-identity.bin.bak', 'no_backup/device-identity.bin.new') + const restored = await bytes('exec-out', 'run-as', appPackage, 'cat', 'no_backup/device-identity.bin') + expect(createHash('sha256').update(restored).digest('hex')).toBe(manifest.sha256) + } + else { + await adb('shell', 'run-as', appPackage, 'rm', '-f', 'no_backup/device-identity.bin', 'no_backup/device-identity.bin.bak', 'no_backup/device-identity.bin.new') + } + } + if (cleanupError) + throw cleanupError + await adb('shell', 'run-as', appPackage, 'rm', '-f', `${identityBackup}.json`, `${identityBackup}.json.bak`, `${identityBackup}.json.new`, `${identityBackup}.bin`) + await fs.writeFile(testInfo.outputPath('phone-restoration.json'), JSON.stringify({ originalIdentityRestored: true, testPartitionsRemoved: manifest.partitions.length, originalKeystorePreserved: true })) + } + try { + provider.listen(0, '127.0.0.1') + await once(provider, 'listening') + for (const label of ['first', 'second']) { + const instance = await buddy.createInstance(`mobile-devices-${label}`) + await prepareMobileDesktopProfile(instance.home, testInfo.outputPath(`profile-preparation-${label}`)) + const desktop = await instance.launch({ storageAuthority: 'sqlite', serviceMode }) + await useSyntheticCredentialStorage(desktop) + const computer = { label, desktop, reversed: false } + computers.push(computer) + const display = label === 'first' ? 'A' : 'B' + const spaceName = `验收空间 ${display}` + const space = await desktop.page.evaluate(name => window.lexoraDesktop.localChat.spaces.create({ name, memoryScope: 'space_only', primaryDirectory: null }), spaceName) + await desktop.page.evaluate(async (baseUrl) => { + await window.lexoraDesktop.settings.update({ runtime: { codemode: 'off', modelRetryLimit: 0 } }) + const providers = window.lexoraDesktop.localChat.providers + await providers.upsertCustom({ id: 'devices-fixture', displayName: 'Devices verification', api: 'openai-completions', baseUrl, enabled: true, models: [{ id: 'fixture-model', name: 'Devices verification', input: ['text'], reasoning: false, contextWindow: 128000, maxTokens: 1024 }] }) + const stop = providers.onAuthChallenge((challenge) => { + if (challenge.providerId === 'devices-fixture' && challenge.type === 'secret') + void providers.respondToAuth(challenge.challengeId, 'offline-fixture-key') + }) + try { + await providers.login('devices-fixture', 'api_key') + } + finally { stop() } + await providers.setDefaultModel({ providerId: 'devices-fixture', modelId: 'fixture-model', reasoning: null }) + }, `http://127.0.0.1:${provider.address().port}/v1`) + computer.tasks = [] + for (const assigned of [true, false]) { + const title = `电脑 ${display} ${assigned ? '空间任务' : '未分配任务'}` + const task = await desktop.page.evaluate(async ({ spaceId, title }) => { + const api = window.lexoraDesktop.localChat + const draftId = crypto.randomUUID() + const draft = await api.composerDrafts.open({ draftId, scope: { kind: 'task', draftId, spaceId }, initialContent: { version: 1, body: [{ type: 'paragraph', content: [{ type: 'text', text: title }] }], panelResourceIds: [] }, initialModelSelection: null, initialExecutionConfig: { executionProfile: 'read_only', approvalPolicy: 'policy' } }) + const turn = await api.chat.startTurn({ draftId, expectedRevision: draft.revision, requestId: crypto.randomUUID() }) + return { conversationId: turn.conversationId, branchId: turn.branchId, runId: turn.runId } + }, { spaceId: assigned ? space.id : null, title }) + await expect.poll(() => desktop.page.evaluate(id => window.lexoraDesktop.localChat.runs.get(id), task.runId), { timeout: 30000 }).toMatchObject({ status: 'completed' }) + await desktop.page.evaluate(async ({ id, title }) => { + const api = window.lexoraDesktop.localChat.conversations + const current = await api.get(id) + return api.rename(id, title, current.titleRevision) + }, { id: task.conversationId, title }) + computer.tasks.push({ ...task, title, assigned }) + } + const listener = createServer() + await new Promise(resolve => listener.listen(0, bindAddress, resolve)) + computer.port = listener.address().port + await new Promise(resolve => listener.close(resolve)) + await desktop.page.evaluate(({ bindAddress, port }) => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: true, bindAddress, port }), { bindAddress, port: computer.port }) + computer.spaceId = space.id + computer.spaceName = spaceName + if (useAdbReverse) { + await adb('reverse', `tcp:${computer.port}`, `tcp:${computer.port}`) + computer.reversed = true + } + } + const config = {} + for (const computer of computers) { + const pairing = await computer.desktop.page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.issuePairing({})) + config[computer.label] = { pairing, spaceId: computer.spaceId, spaceName: computer.spaceName, assignedTitle: computer.tasks.find(task => task.assigned).title, unassignedTitle: computer.tasks.find(task => !task.assigned).title } + } + expect(config.first.pairing.serviceId).not.toBe(config.second.pairing.serviceId) + expect(config.first.pairing.profileId).not.toBe(config.second.pairing.profileId) + await fs.writeFile(configuration, JSON.stringify(config), { mode: 0o600 }) + await adb('push', configuration, remoteConfig) + await fs.rm(configuration) + instrumentStarted = true + const outcome = await adb('shell', 'am', 'instrument', '-w', '-r', '-e', 'class', 'app.uselexora.mobile.DeviceSwitchInstrumentedTest', '-e', 'deviceSwitchToken', token, `${appPackage}.test/androidx.test.runner.AndroidJUnitRunner`) + await fs.writeFile(testInfo.outputPath('android-instrumentation.txt'), outcome.stdout + outcome.stderr) + expect(outcome.stdout).toMatch(/OK \(1 test\)/) + expect(outcome.stdout).not.toMatch(/INSTRUMENTATION_STATUS_CODE: -[234]|FAILURES/) + const checkpoint = JSON.parse((await adb('shell', 'run-as', appPackage, 'cat', resultFile)).stdout) + expect(checkpoint).toMatchObject({ deviceCount: 2, separateScopes: true, localDraftIsolation: true, staleIdentityRejected: true, previousConnectionClosed: true, forgotSelectedWithoutFallback: true, remainingDeviceReconnects: true, originalIdentityRestored: true, ui: { topBarDeviceSwitch: true, spaceNavigation: true, allAndUnassignedFilters: true, newTaskInheritsSpace: true, localInputIsolation: true, inputSurvivesRecreation: true } }) + expect(new Set(checkpoint.partitions).size).toBe(2) + expect(checkpoint.firstServiceId).toBe(config.first.pairing.serviceId) + expect(checkpoint.secondServiceId).toBe(config.second.pairing.serviceId) + expect(await computers[0].desktop.page.evaluate(id => window.lexoraDesktop.localChat.conversations.get(id), checkpoint.ui.createdConversationId)).toMatchObject({ spaceId: config.first.spaceId }) + expect(await computers[1].desktop.page.evaluate(() => window.lexoraDesktop.localChat.conversations.list())).toHaveLength(2) + const userInputs = JSON.stringify(requests.flatMap(request => request.messages.filter(message => message.role === 'user'))) + expect(userInputs).toContain(`服务 A 空间草稿😀 ${token}`) + expect(userInputs).not.toContain(`服务 B 空间草稿😀 ${token}`) + for (const computer of computers) { + const devices = await computer.desktop.page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.devices({})) + expect(devices.filter(device => device.state === 'active').map(device => device.deviceId)).toEqual([checkpoint[`${computer.label}DeviceId`]]) + await computer.desktop.page.screenshot({ path: testInfo.outputPath(`desktop-${computer.label}.png`) }) + } + await fs.writeFile(testInfo.outputPath('devices.json'), JSON.stringify({ ...checkpoint, coreAdapter: serviceMode, transport: useAdbReverse ? 'adb-reverse' : 'private-network', syntheticProvider: true, productDataFromCore: true }, null, 2)) + } + finally { + for (const capture of captures) { + const image = await bytes('exec-out', 'run-as', appPackage, 'cat', `files/devices-${token}-${capture}.png`).catch(() => null) + if (image) + await fs.writeFile(testInfo.outputPath(`android-${capture}.png`), image) + } + await restorePhone().catch(error => cleanupFailures.push(error)) + const cleanup = await Promise.allSettled([ + adb('shell', 'rm', '-f', remoteConfig), + adb('shell', 'run-as', appPackage, 'rm', '-f', resultFile, ...captures.map(capture => `files/devices-${token}-${capture}.png`)), + fs.rm(configuration, { force: true }), + (async () => { + provider.closeAllConnections() + await new Promise((resolve, reject) => provider.close(error => error ? reject(error) : resolve())) + })(), + ...computers.map(async (computer) => { + try { + for (const device of await computer.desktop.page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.devices({}))) { + if (device.state === 'active') + await computer.desktop.page.evaluate(deviceId => window.lexoraDesktop.localChat.privateNetwork.revoke({ deviceId }), device.deviceId) + } + await computer.desktop.page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: false })) + } + finally { + if (computer.reversed) + await adb('reverse', '--remove', `tcp:${computer.port}`) + } + }), + ]) + cleanupFailures.push(...cleanup.filter(result => result.status === 'rejected').map(result => result.reason)) + } + if (cleanupFailures.length) + throw new AggregateError(cleanupFailures, 'Could not clean up the isolated mobile device-switch test') +}) diff --git a/.playwright/scripts/__tests__/mobile-history.e2e.mjs b/.playwright/scripts/__tests__/mobile-history.e2e.mjs new file mode 100644 index 000000000..103d1ddf5 --- /dev/null +++ b/.playwright/scripts/__tests__/mobile-history.e2e.mjs @@ -0,0 +1,190 @@ +import { Buffer } from 'node:buffer' +import { execFile } from 'node:child_process' +import { randomUUID } from 'node:crypto' +import { once } from 'node:events' +import fs from 'node:fs/promises' +import { createServer } from 'node:http' +import { createServer as createListener } from 'node:net' +import path from 'node:path' +import process from 'node:process' +import { DatabaseSync } from 'node:sqlite' +import { promisify } from 'node:util' +import { expect, test, useSyntheticCredentialStorage } from '../fixtures/electron.mjs' +import { prepareMobileDesktopProfile } from '../fixtures/mobile-desktop.mjs' + +const execute = promisify(execFile) +const serial = process.env.LEXORA_TEST_ANDROID_SERIAL +const serviceMode = process.env.LEXORA_TEST_MOBILE_SERVICE_MODE ?? 'utility' + +test('Android historical followup, edit and regeneration preserve branches, restored input and exact source targets', async ({ buddy }, testInfo) => { + test.skip(!serial, 'Requires an isolated Android device with debug and instrumentation APKs') + test.setTimeout(240000) + const adb = (...args) => execute(process.env.ADB ?? 'adb', ['-s', serial, ...args], { timeout: 210000, maxBuffer: 8 * 1024 * 1024 }) + const token = randomUUID() + const appPackage = 'app.uselexora.mobile.dev' + const remoteConfig = `/data/local/tmp/lexora-history-${token}.json` + const configuration = testInfo.outputPath('pairing.json') + const requests = [] + const provider = createServer((request, response) => { + void (async () => { + const chunks = [] + for await (const chunk of request) chunks.push(chunk) + const number = requests.push(JSON.parse(Buffer.concat(chunks).toString('utf8'))) + const common = { id: `history-${number}`, model: 'fixture-model', object: 'chat.completion.chunk', created: 1 } + response.writeHead(200, { 'content-type': 'text/event-stream' }) + response.write(`data: ${JSON.stringify({ ...common, choices: [{ index: 0, delta: { role: 'assistant', content: `历史回复 ${number} 😀` }, finish_reason: null }] })}\n\n`) + response.write(`data: ${JSON.stringify({ ...common, choices: [{ index: 0, delta: {}, finish_reason: 'stop' }] })}\n\n`) + response.end('data: [DONE]\n\n') + })().catch(() => response.destroy()) + }) + provider.listen(0, '127.0.0.1') + await once(provider, 'listening') + const instance = await buddy.createInstance('mobile-history') + await prepareMobileDesktopProfile(instance.home, testInfo.outputPath('profile-preparation')) + const desktop = await instance.launch({ storageAuthority: 'sqlite', serviceMode }) + await useSyntheticCredentialStorage(desktop) + const { page } = desktop + const read = (query) => { + const database = new DatabaseSync(path.join(instance.home, 'buddy/buddy.sqlite3'), { readOnly: true }) + try { + return query(database) + } + finally { database.close() } + } + const listener = createListener() + await new Promise(resolve => listener.listen(0, '127.0.0.1', resolve)) + const port = listener.address().port + await new Promise(resolve => listener.close(resolve)) + let instrument + let outcome + let reversed = false + const marker = stage => `files/history-${token}-${stage}.json` + const files = new Set([`files/history-${token}-branches.png`]) + const signal = async (stage) => { + files.add(marker(stage)) + await adb('shell', 'run-as', appPackage, 'touch', marker(stage)) + } + const checkpoint = async (stage) => { + files.add(marker(stage)) + let value + await expect.poll(async () => { + try { + value = JSON.parse((await adb('shell', 'run-as', appPackage, 'cat', marker(stage))).stdout) + return true + } + catch { + if (outcome) + throw new Error(outcome.result?.stdout ?? String(outcome.error)) + return false + } + }, { timeout: 60000 }).toBe(true) + return value + } + try { + await page.evaluate(async (baseUrl) => { + await window.lexoraDesktop.settings.update({ runtime: { codemode: 'off', modelRetryLimit: 0 } }) + const providers = window.lexoraDesktop.localChat.providers + await providers.upsertCustom({ id: 'history-fixture', displayName: 'History fixture', api: 'openai-completions', baseUrl, enabled: true, models: [{ id: 'fixture-model', name: 'History model', input: ['text'], reasoning: false, contextWindow: 128000, maxTokens: 1024 }] }) + const stop = providers.onAuthChallenge((challenge) => { + if (challenge.providerId === 'history-fixture' && challenge.type === 'secret') + void providers.respondToAuth(challenge.challengeId, 'offline-fixture-key') + }) + try { + await providers.login('history-fixture', 'api_key') + } + finally { stop() } + await providers.setDefaultModel({ providerId: 'history-fixture', modelId: 'fixture-model', reasoning: null }) + }, `http://127.0.0.1:${provider.address().port}/v1`) + await page.reload() + for (const [index, input] of ['历史基线输入', '后续输入不应进入历史续写'].entries()) { + await page.locator('.desktop-chat-composer__prosemirror:visible').fill(input) + await page.getByRole('button', { name: '发送消息', exact: true }).click() + await expect.poll(() => read(db => db.prepare('SELECT COUNT(*) AS count FROM runs WHERE status = \'completed\'').get().count)).toBe(index + 1) + } + const original = read(db => db.prepare('SELECT id, conversation_id, branch_id FROM runs ORDER BY started_at LIMIT 1').get()) + await page.evaluate(() => { + location.hash = '/settings/general' + }) + await page.evaluate(port => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: true, bindAddress: '127.0.0.1', port }), port) + const pairing = await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.issuePairing({})) + await fs.writeFile(configuration, JSON.stringify({ pairing, conversationId: original.conversation_id, originalBranchId: original.branch_id }), { mode: 0o600 }) + await adb('reverse', `tcp:${port}`, `tcp:${port}`) + reversed = true + await adb('push', configuration, remoteConfig) + await fs.rm(configuration) + instrument = adb('shell', 'am', 'instrument', '-w', '-r', '-e', 'class', 'app.uselexora.mobile.HistoryInstrumentedTest', '-e', 'historyToken', token, `${appPackage}.test/androidx.test.runner.AndroidJUnitRunner`) + .then(result => ({ result }), error => ({ error })) + .then((result) => { + outcome = result + return result + }) + const regenerated = await checkpoint('regenerated') + expect(requests).toHaveLength(5) + for (const index of [2, 3, 4]) { + const input = JSON.stringify(requests[index].messages) + expect(input).toContain('历史回复 1') + expect(input).not.toContain('后续输入不应进入历史续写') + expect(input).not.toContain('历史回复 2') + } + expect(JSON.stringify(requests[2].messages)).toContain('从第一条回复继续') + expect(JSON.stringify(requests[3].messages)).toContain('编辑后的接续输入') + expect(JSON.stringify(requests[4].messages)).toContain('编辑后的接续输入') + const activate = branchId => page.evaluate(async ({ conversationId, branchId }) => { + const api = window.lexoraDesktop.localChat.conversations + const current = await api.get(conversationId) + await api.activateBranch({ conversationId, branchId, expectedRevision: current.metadataRevision }) + }, { conversationId: original.conversation_id, branchId }) + await activate(original.branch_id) + await signal('original-active') + await checkpoint('source-edit-ready') + await activate(regenerated.branchId) + await signal('branch-changed') + const independentEdit = await checkpoint('delete-ready') + expect(independentEdit.branchId).not.toBe(original.branch_id) + expect(independentEdit.branchId).not.toBe(regenerated.branchId) + expect(requests).toHaveLength(6) + expect(JSON.stringify(requests[5].messages)).toContain('默认分支改变仍从原来源编辑') + expect(JSON.stringify(requests[5].messages)).not.toContain('编辑后的接续输入') + expect(read(db => db.prepare('SELECT COUNT(*) AS count FROM runs').get().count)).toBe(6) + expect(read(db => db.prepare('SELECT COUNT(*) AS count FROM messages WHERE branch_id = ?').get(original.branch_id).count)).toBe(4) + expect(read(db => db.prepare('SELECT active_branch_id FROM conversations WHERE id = ?').get(original.conversation_id).active_branch_id)).toBe(regenerated.branchId) + await page.evaluate(async (id) => { + const api = window.lexoraDesktop.localChat.conversations + const current = await api.get(id) + if (!await api.delete(id, current.metadataRevision)) + throw new Error('Fixture task was not deleted') + }, original.conversation_id) + await signal('task-deleted') + const evidence = await checkpoint('done') + expect(evidence).toMatchObject({ independentBranchEditAccepted: true, deletedSourceRejection: { decision: 'rejected', errorCode: 'SOURCE_CHANGED' } }) + const result = await instrument + expect(result.error).toBeUndefined() + expect(result.result.stdout).toMatch(/OK \(1 test\)/) + expect(result.result.stdout).not.toMatch(/INSTRUMENTATION_STATUS_CODE: -[234]|FAILURES/) + expect(requests).toHaveLength(6) + expect(read(db => db.prepare('SELECT COUNT(*) AS count FROM runs').get().count)).toBe(0) + expect(read(db => db.prepare('SELECT COUNT(*) AS count FROM messages').get().count)).toBe(0) + expect(read(db => db.prepare('SELECT id FROM conversations WHERE id = ?').get(original.conversation_id))).toBeUndefined() + const capture = await execute(process.env.ADB ?? 'adb', ['-s', serial, 'exec-out', 'run-as', appPackage, 'cat', `files/history-${token}-branches.png`], { encoding: 'buffer', maxBuffer: 8 * 1024 * 1024 }) + await fs.writeFile(testInfo.outputPath('android-history-branches.png'), capture.stdout) + await fs.writeFile(testInfo.outputPath('history-evidence.json'), JSON.stringify({ ...evidence, coreAdapter: serviceMode, modelRequests: requests.length, syntheticProvider: true, originalBranchPreservedBeforeDeletion: true, deletedTaskPurged: true, laterContextExcluded: true }, null, 2)) + } + finally { + await adb('shell', 'am', 'force-stop', appPackage) + const result = await instrument + if (result) + await fs.writeFile(testInfo.outputPath('android-instrumentation.txt'), result.result?.stdout ?? String(result.error)) + await adb('shell', 'rm', '-f', remoteConfig) + await adb('shell', 'run-as', appPackage, 'rm', '-f', ...files) + if (reversed) + await adb('reverse', '--remove', `tcp:${port}`) + await fs.rm(configuration, { force: true }) + for (const device of await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.devices({}))) { + if (device.state === 'active') + await page.evaluate(deviceId => window.lexoraDesktop.localChat.privateNetwork.revoke({ deviceId }), device.deviceId) + } + await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: false })) + provider.closeAllConnections() + await new Promise(resolve => provider.close(resolve)) + } +}) diff --git a/.playwright/scripts/__tests__/mobile-interruption.e2e.mjs b/.playwright/scripts/__tests__/mobile-interruption.e2e.mjs new file mode 100644 index 000000000..8701777ee --- /dev/null +++ b/.playwright/scripts/__tests__/mobile-interruption.e2e.mjs @@ -0,0 +1,166 @@ +import { Buffer } from 'node:buffer' +import { execFile } from 'node:child_process' +import { randomUUID } from 'node:crypto' +import { once } from 'node:events' +import fs from 'node:fs/promises' +import { createServer } from 'node:http' +import { createServer as createListener } from 'node:net' +import path from 'node:path' +import process from 'node:process' +import { DatabaseSync } from 'node:sqlite' +import { promisify } from 'node:util' +import { expect, test, useSyntheticCredentialStorage } from '../fixtures/electron.mjs' +import { prepareMobileDesktopProfile } from '../fixtures/mobile-desktop.mjs' + +const execute = promisify(execFile) +const serial = process.env.LEXORA_TEST_ANDROID_SERIAL +const serviceMode = process.env.LEXORA_TEST_MOBILE_SERVICE_MODE ?? 'utility' + +test('Android recovers the original receipt after a Core runtime restart and explicitly starts a separate successor', async ({ buddy }, testInfo) => { + test.skip(!serial, 'Requires an isolated Android device with debug and instrumentation APKs') + test.setTimeout(180000) + const adb = (...args) => execute(process.env.ADB ?? 'adb', ['-s', serial, ...args], { timeout: 150000, maxBuffer: 8 * 1024 * 1024 }) + const token = randomUUID() + const appPackage = 'app.uselexora.mobile.dev' + const remoteConfig = `/data/local/tmp/lexora-interruption-${token}.json` + const configuration = testInfo.outputPath('pairing.json') + const requests = [] + let pendingStream + const provider = createServer((request, response) => { + void (async () => { + const chunks = [] + for await (const chunk of request) chunks.push(chunk) + const number = requests.push(JSON.parse(Buffer.concat(chunks).toString('utf8'))) + const common = { id: `interruption-${number}`, model: 'fixture-model', object: 'chat.completion.chunk', created: 1 } + response.writeHead(200, { 'content-type': 'text/event-stream' }) + response.write(`data: ${JSON.stringify({ ...common, choices: [{ index: 0, delta: { role: 'assistant', content: number === 1 ? '中断前的部分回复' : '中断后新回复' }, finish_reason: null }] })}\n\n`) + if (number === 1) { + pendingStream = response + return + } + response.write(`data: ${JSON.stringify({ ...common, choices: [{ index: 0, delta: {}, finish_reason: 'stop' }] })}\n\n`) + response.end('data: [DONE]\n\n') + })().catch(() => response.destroy()) + }) + provider.listen(0, '127.0.0.1') + await once(provider, 'listening') + const instance = await buddy.createInstance('mobile-interruption') + await prepareMobileDesktopProfile(instance.home, testInfo.outputPath('profile-preparation')) + const desktop = await instance.launch({ storageAuthority: 'sqlite', serviceMode }) + await useSyntheticCredentialStorage(desktop) + const { page } = desktop + const read = (query) => { + const db = new DatabaseSync(path.join(instance.home, 'buddy/buddy.sqlite3'), { readOnly: true }) + try { + return query(db) + } + finally { db.close() } + } + const listener = createListener() + await new Promise(resolve => listener.listen(0, '127.0.0.1', resolve)) + const port = listener.address().port + await new Promise(resolve => listener.close(resolve)) + let instrument + let outcome + let reversed = false + const files = new Set(['live', 'completed'].map(stage => `files/interruption-${token}-${stage}.png`)) + const marker = stage => `files/interruption-${token}-${stage}.json` + const signal = async (stage) => { + files.add(marker(stage)) + await adb('shell', 'run-as', appPackage, 'touch', marker(stage)) + } + const checkpoint = async (stage) => { + files.add(marker(stage)) + let value + await expect.poll(async () => { + try { + value = JSON.parse((await adb('shell', 'run-as', appPackage, 'cat', marker(stage))).stdout) + return true + } + catch { + if (outcome) + throw new Error(outcome.result?.stdout ?? String(outcome.error)) + return false + } + }, { timeout: 60000 }).toBe(true) + return value + } + try { + await page.evaluate(async (baseUrl) => { + await window.lexoraDesktop.settings.update({ runtime: { codemode: 'off', modelRetryLimit: 0 } }) + const providers = window.lexoraDesktop.localChat.providers + await providers.upsertCustom({ id: 'interruption-fixture', displayName: 'Interruption fixture', api: 'openai-completions', baseUrl, enabled: true, models: [{ id: 'fixture-model', name: 'Interruption model', input: ['text'], reasoning: false, contextWindow: 128000, maxTokens: 1024 }] }) + const stop = providers.onAuthChallenge((challenge) => { + if (challenge.providerId === 'interruption-fixture' && challenge.type === 'secret') + void providers.respondToAuth(challenge.challengeId, 'offline-fixture-key') + }) + try { + await providers.login('interruption-fixture', 'api_key') + } + finally { stop() } + await providers.setDefaultModel({ providerId: 'interruption-fixture', modelId: 'fixture-model', reasoning: null }) + }, `http://127.0.0.1:${provider.address().port}/v1`) + await page.evaluate(port => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: true, bindAddress: '127.0.0.1', port }), port) + const pairing = await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.issuePairing({})) + await fs.writeFile(configuration, JSON.stringify({ pairing }), { mode: 0o600 }) + await adb('reverse', `tcp:${port}`, `tcp:${port}`) + reversed = true + await adb('push', configuration, remoteConfig) + await fs.rm(configuration) + instrument = adb('shell', 'am', 'instrument', '-w', '-r', '-e', 'class', 'app.uselexora.mobile.InterruptionInstrumentedTest', '-e', 'interruptionToken', token, `${appPackage}.test/androidx.test.runner.AndroidJUnitRunner`) + .then(result => ({ result }), error => ({ error })) + .then((result) => { + outcome = result + return result + }) + await checkpoint('stream-visible') + expect(pendingStream).toBeTruthy() + pendingStream.write(`data: ${JSON.stringify({ id: 'interruption-1', model: 'fixture-model', object: 'chat.completion.chunk', created: 1, choices: [{ index: 0, delta: { content: '接着输出' }, finish_reason: null }] })}\n\n`) + const original = await checkpoint('started') + expect(requests).toHaveLength(1) + expect(read(db => db.prepare('SELECT status FROM runs WHERE id = ?').get(original.runId).status)).toBe('running') + await page.evaluate(() => window.lexoraDesktop.localChat.runtime.restart()) + await expect.poll(async () => (await page.evaluate(() => window.lexoraDesktop.localChat.runtime.getStatus())).status, { timeout: 45000 }).toBe('ready') + await signal('restarted') + const recovered = await checkpoint('reconciled') + expect(recovered).toMatchObject({ oldTarget: original, originalReceiptRecovered: true }) + expect(['cancelled', 'failed']).toContain(recovered.oldStatus) + expect(requests).toHaveLength(1) + expect(read(db => db.prepare('SELECT COUNT(*) AS count FROM runs').get().count)).toBe(1) + await signal('allow-new-intent') + const evidence = await checkpoint('done') + const result = await instrument + expect(result.error).toBeUndefined() + expect(result.result.stdout).toMatch(/OK \(1 test\)/) + expect(result.result.stdout).not.toMatch(/INSTRUMENTATION_STATUS_CODE: -[234]|FAILURES/) + expect(requests).toHaveLength(2) + expect(read(db => db.prepare('SELECT COUNT(*) AS count FROM runs').get().count)).toBe(2) + expect(read(db => db.prepare('SELECT status FROM runs WHERE id = ?').get(original.runId).status)).toBe(recovered.oldStatus) + await expect.poll(() => read(db => db.prepare('SELECT status FROM runs WHERE id = ?').get(evidence.successor.runId).status)).toBe('completed') + expect(JSON.stringify(requests[1].messages)).toContain('服务中断后等待我明确重新生成') + for (const stage of ['live', 'completed']) { + const capture = await execute(process.env.ADB ?? 'adb', ['-s', serial, 'exec-out', 'run-as', appPackage, 'cat', `files/interruption-${token}-${stage}.png`], { encoding: 'buffer', maxBuffer: 8 * 1024 * 1024 }) + await fs.writeFile(testInfo.outputPath(`android-${stage}.png`), capture.stdout) + } + await fs.writeFile(testInfo.outputPath('interruption-evidence.json'), JSON.stringify({ ...evidence, coreAdapter: serviceMode, controlledRuntimeRestart: true, liveAndRestoredPartialOutput: true, automaticSuccessors: 0, modelRequests: requests.length }, null, 2)) + } + finally { + await adb('shell', 'am', 'force-stop', appPackage) + const result = await instrument + if (result) + await fs.writeFile(testInfo.outputPath('android-instrumentation.txt'), result.result?.stdout ?? String(result.error)) + await adb('shell', 'rm', '-f', remoteConfig) + if (files.size) + await adb('shell', 'run-as', appPackage, 'rm', '-f', ...files) + if (reversed) + await adb('reverse', '--remove', `tcp:${port}`) + await fs.rm(configuration, { force: true }) + for (const device of await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.devices({}))) { + if (device.state === 'active') + await page.evaluate(deviceId => window.lexoraDesktop.localChat.privateNetwork.revoke({ deviceId }), device.deviceId) + } + await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: false })) + provider.closeAllConnections() + await new Promise(resolve => provider.close(resolve)) + } +}) diff --git a/.playwright/scripts/__tests__/mobile-questions.e2e.mjs b/.playwright/scripts/__tests__/mobile-questions.e2e.mjs new file mode 100644 index 000000000..50f38c94a --- /dev/null +++ b/.playwright/scripts/__tests__/mobile-questions.e2e.mjs @@ -0,0 +1,320 @@ +import { Buffer } from 'node:buffer' +import { execFile } from 'node:child_process' +import { createHash, randomUUID } from 'node:crypto' +import { once } from 'node:events' +import fs from 'node:fs/promises' +import { createServer as createProvider } from 'node:http' +import { createServer } from 'node:net' +import path from 'node:path' +import process from 'node:process' +import { DatabaseSync } from 'node:sqlite' +import { setTimeout as delay } from 'node:timers/promises' +import { promisify } from 'node:util' +import { expect, test, useSyntheticCredentialStorage } from '../fixtures/electron.mjs' +import { prepareMobileDesktopProfile } from '../fixtures/mobile-desktop.mjs' + +const execute = promisify(execFile) +const serial = process.env.LEXORA_TEST_ANDROID_SERIAL +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 prompts = [ + [{ id: 'coverage', question: '这次要验证哪些能力?', kind: 'multiple', options: [{ id: 'connection', label: '连接恢复', recommended: true }, { id: 'files', label: '文件操作' }, { id: 'source', label: '仅审查源码' }] }], + [{ id: 'winner', question: '本次以哪一端的实际回答为准?', kind: 'single', options: [{ id: 'desktop', label: '由电脑提交回答' }, { id: 'mobile', label: '手机先填但不提交' }] }], +] + +test('real Agent questions accept native Android choices, retain unfinished answers and close a stale dialog after Desktop answers', async ({ buddy }, testInfo) => { + test.skip(!serial, 'Requires an Android device with the debug and instrumentation APKs installed') + test.setTimeout(360000) + const adb = (...args) => execute(process.env.ADB ?? 'adb', ['-s', serial, ...args], { timeout: 300000, maxBuffer: 8 * 1024 * 1024 }) + const bytes = async (...args) => (await execute(process.env.ADB ?? 'adb', ['-s', serial, ...args], { timeout: 15000, encoding: 'buffer', maxBuffer: 8 * 1024 * 1024 })).stdout + const token = randomUUID() + const appPackage = 'app.uselexora.mobile.dev' + const remoteConfig = `/data/local/tmp/lexora-questions-${token}.json` + const resultFile = `files/questions-${token}.json` + const configuration = testInfo.outputPath('pairing.json') + const identityBackup = `no_backup/question-answer-${token}-identity` + const captures = ['filled', 'restored-answer', 'delivered', 'awaiting-desktop', 'desktop-winner'] + const requests = [] + const cleanupFailures = [] + let instrumentStarted = false + let instrument + let instrumentProcess + let instrumentOutcome + let desktop + let port + let reversed = false + const provider = createProvider((request, response) => { + void (async () => { + const chunks = [] + for await (const chunk of request) chunks.push(chunk) + const body = JSON.parse(Buffer.concat(chunks).toString('utf8')) + requests.push(body) + const serialized = JSON.stringify(body.messages) + const phase = Number(serialized.match(/Question mobile phase ([01])/)?.[1] ?? '0') + const calls = body.messages.flatMap(message => message.tool_calls ?? []) + const agentRequest = body.tools?.some(tool => tool.function?.name === 'lexora_ask_user') === true + const ask = calls.find(call => call.function?.name === 'lexora_ask_user') + const waited = calls.some(call => call.function?.name === 'lexora_wait_user') + let tool + if (agentRequest && !ask) { + tool = { name: 'lexora_ask_user', arguments: JSON.stringify({ questions: prompts[phase] }) } + } + else if (agentRequest && !waited) { + const result = body.messages.find(message => message.role === 'tool' && message.tool_call_id === ask.id) + const text = typeof result?.content === 'string' ? result.content : result?.content?.map(item => item.text ?? '').join('') ?? '' + const requestId = text.match(/"requestId"\s*:\s*"([^"]+)"/)?.[1] + if (!requestId) + throw new Error('The actual ask_user tool did not return a request receipt') + tool = { name: 'lexora_wait_user', arguments: JSON.stringify({ requestId }) } + } + const reply = phase === 0 ? `手机选择与补充😀 ${token}` : `桌面抢先回答 ${token}` + const delta = tool + ? { role: 'assistant', tool_calls: [{ index: 0, id: `question-${phase}-${tool.name}`, type: 'function', function: tool }] } + : { role: 'assistant', content: `问题验收完成 ${phase}:${serialized.includes(reply) ? reply : '未收到实际答案'}` } + const common = { id: `question-${requests.length}`, model: 'fixture-model', object: 'chat.completion.chunk', created: 1 } + response.writeHead(200, { 'content-type': 'text/event-stream' }) + response.write(`data: ${JSON.stringify({ ...common, choices: [{ index: 0, delta, finish_reason: null }] })}\n\n`) + response.write(`data: ${JSON.stringify({ ...common, choices: [{ index: 0, delta: {}, finish_reason: tool ? 'tool_calls' : 'stop' }] })}\n\n`) + response.end('data: [DONE]\n\n') + })().catch(error => response.destroy(error)) + }) + const restorePhone = async () => { + if (!instrumentStarted) + return + await adb('shell', 'am', 'force-stop', appPackage) + let manifest + for (const suffix of ['.json', '.json.bak']) { + manifest = await adb('shell', 'run-as', appPackage, 'cat', `${identityBackup}${suffix}`).then(result => JSON.parse(result.stdout), () => null) + if (manifest) + break + } + if (!manifest) + return + let cleanupError + try { + const cleanup = await adb('shell', 'am', 'instrument', '-w', '-r', '-e', 'class', 'app.uselexora.mobile.QuestionAnswerInstrumentedTest', '-e', 'questionAnswerToken', token, '-e', 'questionAnswerRestoreOnly', 'true', `${appPackage}.test/androidx.test.runner.AndroidJUnitRunner`) + await fs.writeFile(testInfo.outputPath('android-restoration.txt'), cleanup.stdout + cleanup.stderr) + expect(cleanup.stdout).toMatch(/OK \(1 test\)/) + expect(cleanup.stdout).not.toMatch(/INSTRUMENTATION_STATUS_CODE: -[234]|FAILURES/) + } + catch (error) { cleanupError = error } + finally { + await adb('shell', 'am', 'force-stop', appPackage) + const original = await bytes('exec-out', 'run-as', appPackage, 'cat', `${identityBackup}.bin`) + expect(createHash('sha256').update(original).digest('hex')).toBe(manifest.sha256) + if (manifest.present) { + await adb('shell', 'run-as', appPackage, 'cp', `${identityBackup}.bin`, `${identityBackup}.restore`) + await adb('shell', 'run-as', appPackage, 'mv', `${identityBackup}.restore`, 'no_backup/device-identity.bin') + await adb('shell', 'run-as', appPackage, 'rm', '-f', 'no_backup/device-identity.bin.bak', 'no_backup/device-identity.bin.new') + const restored = await bytes('exec-out', 'run-as', appPackage, 'cat', 'no_backup/device-identity.bin') + expect(createHash('sha256').update(restored).digest('hex')).toBe(manifest.sha256) + } + else { + await adb('shell', 'run-as', appPackage, 'rm', '-f', 'no_backup/device-identity.bin', 'no_backup/device-identity.bin.bak', 'no_backup/device-identity.bin.new') + } + } + if (cleanupError) + throw cleanupError + await adb('shell', 'run-as', appPackage, 'rm', '-f', `${identityBackup}.json`, `${identityBackup}.json.bak`, `${identityBackup}.json.new`, `${identityBackup}.bin`) + await fs.writeFile(testInfo.outputPath('phone-restoration.json'), JSON.stringify({ originalIdentityRestored: true, testPartitionsRemoved: manifest.partitions.length, originalKeystorePreserved: true })) + } + const checkpoints = [] + const instrumentationText = outcome => [outcome?.result?.stdout ?? outcome?.error?.stdout, outcome?.result?.stderr ?? outcome?.error?.stderr, outcome?.error?.stack].filter(Boolean).join('\n') + const assertInstrumentation = (outcome) => { + const output = instrumentationText(outcome) + if (outcome.error || !/OK \(1 test\)/.test(output) || /INSTRUMENTATION_STATUS_CODE: -[234]|FAILURES/.test(output)) + throw new Error(`Android question instrumentation failed:\n${output}`, { cause: outcome.error }) + } + const checkpoint = async (phase, stage) => { + const file = `files/questions-${token}-${phase}-${stage}.json` + checkpoints.push(file) + const deadline = performance.now() + 75000 + while (performance.now() < deadline) { + let value + try { + value = JSON.parse((await adb('shell', 'run-as', appPackage, 'cat', file)).stdout) + } + catch {} + if (instrumentOutcome) { + assertInstrumentation(instrumentOutcome) + if (value === undefined) + throw new Error(`Android instrumentation completed before question checkpoint ${phase}/${stage}:\n${instrumentationText(instrumentOutcome)}`) + } + if (value !== undefined) + return value + await Promise.race([delay(250), instrument]) + } + throw new Error(`Android question checkpoint ${phase}/${stage} was not reached within 75000 ms`) + } + const resume = async (phase) => { + const file = `files/questions-${token}-${phase}-resume.json` + checkpoints.push(file) + await adb('shell', 'run-as', appPackage, 'touch', file) + } + try { + provider.listen(0, '127.0.0.1') + await once(provider, 'listening') + const instance = await buddy.createInstance('mobile-questions') + await prepareMobileDesktopProfile(instance.home, testInfo.outputPath('profile-preparation')) + desktop = await instance.launch({ storageAuthority: 'sqlite', serviceMode }) + await useSyntheticCredentialStorage(desktop) + const page = desktop.page + const read = (query) => { + const database = new DatabaseSync(path.join(instance.home, 'buddy/buddy.sqlite3'), { readOnly: true }) + try { + return query(database) + } + finally { database.close() } + } + await page.evaluate(async (baseUrl) => { + await window.lexoraDesktop.settings.update({ runtime: { codemode: 'off', modelRetryLimit: 0 } }) + const providers = window.lexoraDesktop.localChat.providers + await providers.upsertCustom({ id: 'questions-fixture', displayName: 'Question verification', api: 'openai-completions', baseUrl, enabled: true, models: [{ id: 'fixture-model', name: 'Question verification', input: ['text'], reasoning: false, contextWindow: 128000, maxTokens: 1024 }] }) + const stop = providers.onAuthChallenge((challenge) => { + if (challenge.providerId === 'questions-fixture' && challenge.type === 'secret') + void providers.respondToAuth(challenge.challengeId, 'offline-fixture-key') + }) + try { + await providers.login('questions-fixture', 'api_key') + } + finally { stop() } + await providers.setDefaultModel({ providerId: 'questions-fixture', modelId: 'fixture-model', reasoning: null }) + }, `http://127.0.0.1:${provider.address().port}/v1`) + const cases = [] + for (let phase = 0; phase < 2; phase++) { + const title = phase === 0 ? '手机问答与恢复验收' : '桌面抢先回答验收' + const turn = await page.evaluate(async (phase) => { + const api = window.lexoraDesktop.localChat + const draftId = crypto.randomUUID() + const draft = await api.composerDrafts.open({ draftId, scope: { kind: 'task', draftId, spaceId: null }, initialContent: { version: 1, body: [{ type: 'paragraph', content: [{ type: 'text', text: `Question mobile phase ${phase}` }] }], panelResourceIds: [] }, initialModelSelection: null, initialExecutionConfig: { executionProfile: 'read_only', approvalPolicy: 'policy' } }) + const turn = await api.chat.startTurn({ draftId, expectedRevision: draft.revision, requestId: crypto.randomUUID() }) + return { conversationId: turn.conversationId, branchId: turn.branchId, runId: turn.runId } + }, phase) + const scope = { conversationId: turn.conversationId, branchId: turn.branchId } + await expect.poll(() => page.evaluate(scope => window.lexoraDesktop.localChat.questions.list(scope), scope), { timeout: 45000 }).toMatchObject([{ status: 'pending', questions: prompts[phase] }]) + const request = (await page.evaluate(scope => window.lexoraDesktop.localChat.questions.list(scope), scope))[0] + await page.evaluate(async ({ id, title }) => { + const api = window.lexoraDesktop.localChat.conversations + const current = await api.get(id) + await api.rename(id, title, current.titleRevision) + }, { id: turn.conversationId, title }) + cases.push({ ...turn, requestId: request.id, title }) + } + const listener = createServer() + await new Promise(resolve => listener.listen(0, bindAddress, resolve)) + port = listener.address().port + await new Promise(resolve => listener.close(resolve)) + await page.evaluate(({ bindAddress, port }) => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: true, bindAddress, port }), { bindAddress, port }) + if (useAdbReverse) { + await adb('reverse', `tcp:${port}`, `tcp:${port}`) + reversed = true + } + const pairing = await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.issuePairing({})) + await fs.writeFile(configuration, JSON.stringify({ pairing, cases }), { mode: 0o600 }) + await adb('push', configuration, remoteConfig) + await fs.rm(configuration) + instrumentStarted = true + instrumentProcess = adb('shell', 'am', 'instrument', '-w', '-r', '-e', 'class', 'app.uselexora.mobile.QuestionAnswerInstrumentedTest', '-e', 'questionAnswerToken', token, `${appPackage}.test/androidx.test.runner.AndroidJUnitRunner`) + instrument = instrumentProcess.then(result => ({ result }), error => ({ error })).then((outcome) => { + instrumentOutcome = outcome + return outcome + }) + const phone = await checkpoint(0, 'done') + expect(phone).toMatchObject({ requestId: cases[0].requestId, activityRecreated: true, selectedOptionIds: ['connection', 'files'], text: `手机选择与补充😀 ${token}` }) + const scopeOf = item => ({ conversationId: item.conversationId, branchId: item.branchId }) + const currentQuestion = item => page.evaluate(scope => window.lexoraDesktop.localChat.questions.list(scope), scopeOf(item)) + await expect.poll(() => currentQuestion(cases[0])).toMatchObject([{ status: 'answered', response: { answers: [{ questionId: 'coverage', selectedOptionIds: ['connection', 'files'], text: phone.text }] }, delivery: { state: 'delivered' } }]) + await expect.poll(() => page.evaluate(id => window.lexoraDesktop.localChat.runs.get(id), cases[0].runId)).toMatchObject({ status: 'completed' }) + await resume(0) + const ready = await checkpoint(1, 'ready') + expect(ready).toMatchObject({ requestId: cases[1].requestId, dialogOpen: true }) + await page.evaluate(async ({ scope, requestId, text }) => { + await window.lexoraDesktop.localChat.questions.answer({ ...scope, requestId, answers: [{ questionId: 'winner', selectedOptionIds: ['desktop'], text }] }) + }, { scope: scopeOf(cases[1]), requestId: cases[1].requestId, text: `桌面抢先回答 ${token}` }) + await expect.poll(() => currentQuestion(cases[1])).toMatchObject([{ status: 'answered', response: { answers: [{ selectedOptionIds: ['desktop'], text: `桌面抢先回答 ${token}` }] }, delivery: { state: 'delivered' } }]) + await expect.poll(() => page.evaluate(id => window.lexoraDesktop.localChat.runs.get(id), cases[1].runId)).toMatchObject({ status: 'completed' }) + await resume(1) + expect(await checkpoint(1, 'done')).toMatchObject({ staleDialogClosed: true, noStaleCommand: true }) + const outcome = await instrument + assertInstrumentation(outcome) + const evidence = JSON.parse((await adb('shell', 'run-as', appPackage, 'cat', resultFile)).stdout) + expect(evidence).toMatchObject({ activityRecreated: true, multipleChoicesAndText: true, staleDialogClosed: true, noStaleCommand: true, originalIdentityRestored: true }) + for (const [phase, item] of cases.entries()) { + const events = read(db => db.prepare('SELECT event_type, payload_json FROM run_events WHERE run_id = ? ORDER BY sequence').all(item.runId)) + const toolNames = events.filter(event => event.event_type === 'tool.started').map(event => JSON.parse(event.payload_json).toolName) + expect(toolNames.filter(name => name === 'lexora_ask_user')).toHaveLength(1) + expect(toolNames.filter(name => name === 'lexora_wait_user')).toHaveLength(1) + expect(read(db => db.prepare('SELECT COUNT(*) AS count FROM execution_mailbox_sources WHERE id = ?').get(`user-question:${item.requestId}`).count)).toBe(1) + const reply = read(db => db.prepare('SELECT content_json FROM messages WHERE run_id = ? AND role = \'assistant\'').all(item.runId)).map(row => JSON.parse(row.content_json).text ?? '').join('\n') + expect(reply).toContain(phase === 0 ? phone.text : `桌面抢先回答 ${token}`) + } + const receipts = read(db => db.prepare('SELECT decision, target_json FROM command_receipts WHERE method = \'questions.answer\'').all()) + expect(receipts).toHaveLength(2) + expect(new Set(receipts.map(receipt => JSON.parse(receipt.target_json).questionId))).toEqual(new Set(cases.map(item => item.requestId))) + expect(receipts.every(receipt => receipt.decision === 'accepted')).toBe(true) + expect(JSON.stringify(requests)).not.toContain(`此手机旧回答不得投递 ${token}`) + await fs.writeFile(testInfo.outputPath('questions.json'), JSON.stringify({ ...evidence, cases, syntheticProvider: true, toolExecutionFromCore: true, mailboxInputs: 2, productAnswerReceipts: 2, desktopWinnerUsesProductGateway: true }, null, 2)) + await page.screenshot({ path: testInfo.outputPath('desktop-questions.png') }) + } + finally { + if (instrument && !instrumentOutcome) { + await adb('shell', 'am', 'force-stop', appPackage).catch(error => cleanupFailures.push(error)) + await Promise.race([instrument, delay(10000)]) + if (!instrumentOutcome) { + instrumentProcess.child.kill('SIGTERM') + await instrument + } + } + const output = instrumentationText(instrumentOutcome) + if (instrumentStarted) + await fs.writeFile(testInfo.outputPath('android-instrumentation.txt'), output || 'Instrumentation did not return output').catch(error => cleanupFailures.push(error)) + const pngHeader = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]) + for (const capture of captures) { + const image = await bytes('exec-out', 'run-as', appPackage, 'cat', `files/questions-${token}-${capture}.png`).catch(() => null) + if (image?.subarray(0, 8).equals(pngHeader)) + await fs.writeFile(testInfo.outputPath(`android-${capture}.png`), image).catch(error => cleanupFailures.push(error)) + } + for (const name of new Set(output.match(/mobile-ui-failure-\d+\.(?:json|png)/g) ?? [])) { + const file = `files/${name}` + checkpoints.push(file) + const value = await bytes('exec-out', 'run-as', appPackage, 'cat', file).catch(() => null) + if (!value) + continue + try { + if (name.endsWith('.png') ? !value.subarray(0, 8).equals(pngHeader) : !Array.isArray(JSON.parse(value.toString('utf8')))) + continue + } + catch { continue } + await fs.writeFile(testInfo.outputPath(name), value).catch(error => cleanupFailures.push(error)) + } + await restorePhone().catch(error => cleanupFailures.push(error)) + const cleanup = await Promise.allSettled([ + adb('shell', 'rm', '-f', remoteConfig), + adb('shell', 'run-as', appPackage, 'rm', '-f', resultFile, ...checkpoints, ...captures.map(capture => `files/questions-${token}-${capture}.png`)), + fs.rm(configuration, { force: true }), + (async () => { + provider.closeAllConnections() + await new Promise((resolve, reject) => provider.close(error => error ? reject(error) : resolve())) + })(), + (async () => { + try { + if (desktop) { + for (const device of await desktop.page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.devices({}))) { + if (device.state === 'active') + await desktop.page.evaluate(deviceId => window.lexoraDesktop.localChat.privateNetwork.revoke({ deviceId }), device.deviceId) + } + await desktop.page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: false })) + } + } + finally { + if (reversed) + await adb('reverse', '--remove', `tcp:${port}`) + } + })(), + ]) + cleanupFailures.push(...cleanup.filter(result => result.status === 'rejected').map(result => result.reason)) + } + if (cleanupFailures.length) + throw new AggregateError(cleanupFailures, 'Could not clean up the isolated mobile questions test') +}) diff --git a/.playwright/scripts/__tests__/mobile-recovery.e2e.mjs b/.playwright/scripts/__tests__/mobile-recovery.e2e.mjs new file mode 100644 index 000000000..75c6d4104 --- /dev/null +++ b/.playwright/scripts/__tests__/mobile-recovery.e2e.mjs @@ -0,0 +1,52 @@ +import { execFile } from 'node:child_process' +import fs from 'node:fs/promises' +import { createServer } from 'node:net' +import process from 'node:process' +import { promisify } from 'node:util' +import { expect, test, useSyntheticCredentialStorage } from '../fixtures/electron.mjs' +import { prepareMobileDesktopProfile } from '../fixtures/mobile-desktop.mjs' + +const execute = promisify(execFile) +const serial = process.env.LEXORA_TEST_ANDROID_SERIAL +const serviceMode = process.env.LEXORA_TEST_MOBILE_SERVICE_MODE ?? 'utility' + +test('Android process termination preserves drafts, original receipts and resumable attachment uploads with real Core', async ({ buddy }, testInfo) => { + test.skip(!serial, 'Requires an isolated Android device with debug and instrumentation APKs') + test.setTimeout(180000) + const adb = (...args) => execute(process.env.ADB ?? 'adb', ['-s', serial, ...args], { timeout: 60000 }) + const instance = await buddy.createInstance('mobile-recovery') + await prepareMobileDesktopProfile(instance.home, testInfo.outputPath('profile-preparation')) + const desktop = await instance.launch({ storageAuthority: 'sqlite', serviceMode }) + await useSyntheticCredentialStorage(desktop) + const { page } = desktop + const listener = createServer() + await new Promise(resolve => listener.listen(0, '127.0.0.1', resolve)) + const port = listener.address().port + await new Promise(resolve => listener.close(resolve)) + const configuration = testInfo.outputPath('pairing.json') + let reversed = false + try { + await page.evaluate(port => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: true, bindAddress: '127.0.0.1', port }), port) + const pairing = await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.issuePairing({})) + await fs.writeFile(configuration, JSON.stringify({ pairing }), { mode: 0o600 }) + await adb('reverse', `tcp:${port}`, `tcp:${port}`) + reversed = true + const result = await execute(process.execPath, ['.playwright/scripts/mobile/__tests__/process-recovery.e2e.mjs', '--serial', serial, '--adb', process.env.ADB ?? 'adb', '--pairing', configuration, '--output', testInfo.outputPath('recovery')], { timeout: 150000, maxBuffer: 8 * 1024 * 1024 }) + await fs.writeFile(testInfo.outputPath('runner.log'), result.stdout + result.stderr) + const recovered = JSON.parse(await fs.readFile(testInfo.outputPath('recovery/result.json'), 'utf8')) + expect(recovered).toMatchObject({ passed: true, nativeDraftRestored: true, currentInputPreserved: true, executionSettingsPreserved: true, duplicateAdoptionPrevented: true }) + await expect(page.locator('.desktop-workbench-area__tasks .tiptap:visible').first()).toBeVisible() + await page.screenshot({ path: testInfo.outputPath('desktop-after-recovery.png') }) + } + finally { + await adb('shell', 'am', 'force-stop', 'app.uselexora.mobile.dev') + if (reversed) + await adb('reverse', '--remove', `tcp:${port}`) + await fs.rm(configuration, { force: true }) + for (const device of await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.devices({}))) { + if (device.state === 'active') + await page.evaluate(deviceId => window.lexoraDesktop.localChat.privateNetwork.revoke({ deviceId }), device.deviceId) + } + await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: false })) + } +}) diff --git a/.playwright/scripts/__tests__/mobile-task-controls.e2e.mjs b/.playwright/scripts/__tests__/mobile-task-controls.e2e.mjs new file mode 100644 index 000000000..a70f4d4f3 --- /dev/null +++ b/.playwright/scripts/__tests__/mobile-task-controls.e2e.mjs @@ -0,0 +1,228 @@ +import { Buffer } from 'node:buffer' +import { execFile } from 'node:child_process' +import { randomUUID } from 'node:crypto' +import { once } from 'node:events' +import fs from 'node:fs/promises' +import { createServer } from 'node:http' +import { createServer as createListener } from 'node:net' +import path from 'node:path' +import process from 'node:process' +import { DatabaseSync } from 'node:sqlite' +import { promisify } from 'node:util' +import { expect, test, useSyntheticCredentialStorage } from '../fixtures/electron.mjs' +import { prepareMobileDesktopProfile } from '../fixtures/mobile-desktop.mjs' + +const execute = promisify(execFile) +const serial = process.env.LEXORA_TEST_ANDROID_SERIAL +const serviceMode = process.env.LEXORA_TEST_MOBILE_SERVICE_MODE ?? 'utility' + +test('Android direct stop, queue, branch switching and read state preserve exact targets and local input', async ({ buddy }, testInfo) => { + test.skip(!serial, 'Requires an isolated Android device with debug and instrumentation APKs') + test.setTimeout(300000) + const adb = (...args) => execute(process.env.ADB ?? 'adb', ['-s', serial, ...args], { timeout: 280000, maxBuffer: 8 * 1024 * 1024 }) + const token = randomUUID() + const appPackage = 'app.uselexora.mobile.dev' + const remoteConfig = `/data/local/tmp/lexora-controls-${token}.json` + const configuration = testInfo.outputPath('pairing.json') + const gates = Array.from({ length: 3 }, () => Promise.withResolvers()) + const requests = [] + const provider = createServer((request, response) => { + void (async () => { + const chunks = [] + for await (const chunk of request) chunks.push(chunk) + const body = JSON.parse(Buffer.concat(chunks).toString('utf8')) + const index = requests.push(body) - 1 + if (gates[index] && !await Promise.race([gates[index].promise.then(() => true), once(response, 'close').then(() => false)])) + return + const common = { id: `controls-${index}`, model: 'fixture-model', object: 'chat.completion.chunk', created: 1 } + response.writeHead(200, { 'content-type': 'text/event-stream' }) + response.write(`data: ${JSON.stringify({ ...common, choices: [{ index: 0, delta: { role: 'assistant', content: index === 0 ? '首段输出结束' : index < 3 ? '恢复排队任务完成' : '历史分支验证回复' }, finish_reason: null }] })}\n\n`) + response.write(`data: ${JSON.stringify({ ...common, choices: [{ index: 0, delta: {}, finish_reason: 'stop' }] })}\n\n`) + response.end('data: [DONE]\n\n') + })().catch(() => response.destroy()) + }) + provider.listen(0, '127.0.0.1') + await once(provider, 'listening') + const instance = await buddy.createInstance('mobile-task-controls') + await prepareMobileDesktopProfile(instance.home, testInfo.outputPath('profile-preparation')) + const desktop = await instance.launch({ storageAuthority: 'sqlite', serviceMode }) + await useSyntheticCredentialStorage(desktop) + const { page } = desktop + const read = (query) => { + const database = new DatabaseSync(path.join(instance.home, 'buddy/buddy.sqlite3'), { readOnly: true }) + try { + return query(database) + } + finally { database.close() } + } + const listener = createListener() + await new Promise(resolve => listener.listen(0, '127.0.0.1', resolve)) + const port = listener.address().port + await new Promise(resolve => listener.close(resolve)) + let reversed = false + let started = false + let failure + let instrument + let outcome + const files = new Set(['stopped.png', 'final.png'].map(name => `files/controls-${token}-${name}`)) + const marker = stage => `files/controls-${token}-${stage}.json` + const signal = async (stage) => { + files.add(marker(stage)) + await adb('shell', 'run-as', appPackage, 'touch', marker(stage)) + } + const checkpoint = async (stage) => { + files.add(marker(stage)) + let value + await expect.poll(async () => { + try { + value = JSON.parse((await adb('shell', 'run-as', appPackage, 'cat', marker(stage))).stdout) + return true + } + catch { + if (outcome) + throw new Error(outcome.result?.stdout ?? String(outcome.error)) + return false + } + }, { timeout: 60000 }).toBe(true) + return value + } + try { + await page.evaluate(async (baseUrl) => { + await window.lexoraDesktop.settings.update({ runtime: { codemode: 'off', modelRetryLimit: 0 } }) + const providers = window.lexoraDesktop.localChat.providers + await providers.upsertCustom({ id: 'controls-fixture', displayName: 'Controls fixture', api: 'openai-completions', baseUrl, enabled: true, models: [{ id: 'fixture-model', name: 'Controls model', input: ['text'], reasoning: false, contextWindow: 128000, maxTokens: 1024 }] }) + const stop = providers.onAuthChallenge((challenge) => { + if (challenge.providerId === 'controls-fixture' && challenge.type === 'secret') + void providers.respondToAuth(challenge.challengeId, 'offline-fixture-key') + }) + try { + await providers.login('controls-fixture', 'api_key') + } + finally { stop() } + await providers.setDefaultModel({ providerId: 'controls-fixture', modelId: 'fixture-model', reasoning: null }) + }, `http://127.0.0.1:${provider.address().port}/v1`) + await page.reload() + await page.locator('.desktop-chat-composer__prosemirror:visible').fill('Mobile controls baseline') + await page.getByRole('button', { name: '发送消息', exact: true }).click() + await expect.poll(() => requests.length).toBe(1) + const first = read(db => db.prepare('SELECT id, conversation_id, branch_id FROM runs WHERE status = \'running\'').get()) + expect(first).toBeTruthy() + await page.evaluate(() => { + location.hash = '/settings/general' + }) + await expect(page).toHaveURL(/#\/settings\/general/) + await page.evaluate(port => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: true, bindAddress: '127.0.0.1', port }), port) + const pairing = await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.issuePairing({})) + await fs.writeFile(configuration, JSON.stringify({ pairing, conversationId: first.conversation_id }), { mode: 0o600 }) + await adb('reverse', `tcp:${port}`, `tcp:${port}`) + reversed = true + await adb('push', configuration, remoteConfig) + await fs.rm(configuration) + started = true + instrument = adb('shell', 'am', 'instrument', '-w', '-r', '-e', 'class', 'app.uselexora.mobile.TaskControlsInstrumentedTest', '-e', 'taskControlsToken', token, `${appPackage}.test/androidx.test.runner.AndroidJUnitRunner`) + .then(result => ({ result }), error => ({ error })) + .then((result) => { + outcome = result + return result + }) + const stale = await checkpoint('stale-ready') + expect(await page.evaluate(item => window.lexoraDesktop.localChat.chat.cancelQueued({ id: item.id, conversationId: item.conversationId, branchId: item.branchId, expectedRevision: Number(item.revision) }), stale)).toBe(true) + await signal('stale-removed') + expect((await checkpoint('steered')).runId).toBe(first.id) + gates[0].resolve() + await expect.poll(() => requests.length).toBe(2) + expect(JSON.stringify(requests[1].messages)).toContain('手机补充当前执行') + expect(read(db => db.prepare('SELECT COUNT(*) AS count FROM runs').get().count)).toBe(1) + await signal('steer-delivered') + const paused = await checkpoint('paused') + expect(read(db => db.prepare('SELECT state FROM chat_queue WHERE id = ?').get(paused.queueId).state)).toBe('paused') + expect(read(db => db.prepare('SELECT status FROM runs WHERE id = ?').get(first.id).status)).toBe('cancelled') + expect(requests).toHaveLength(2) + await signal('continue') + await expect.poll(() => requests.length).toBe(3) + await signal('next-running') + const replay = await checkpoint('old-stop-replayed') + expect(replay.nextRunId).not.toBe(first.id) + expect(read(db => db.prepare('SELECT status FROM runs WHERE id = ?').get(replay.nextRunId).status)).toBe('running') + gates[2].resolve() + const phoneRead = await checkpoint('phone-read') + expect(phoneRead.unread).toBe(false) + const states = () => page.evaluate(id => window.lexoraDesktop.localChat.taskMarks.states([id]), first.conversation_id) + await expect.poll(states).toMatchObject([{ unread: false, resultRunId: replay.nextRunId }]) + const setRead = read => page.evaluate(async ({ id, read }) => { + const marks = window.lexoraDesktop.localChat.taskMarks + const [state] = await marks.states([id]) + return marks.setRead({ conversationId: id, resultRunId: state.resultRunId, readRevision: state.readRevision, read }) + }, { id: first.conversation_id, read }) + expect(await setRead(false)).toMatchObject({ unread: true }) + await signal('desktop-unread') + await checkpoint('background') + expect(await setRead(true)).toMatchObject({ unread: false }) + await signal('desktop-read') + await checkpoint('branches-ready') + const fork = await page.evaluate(({ conversationId, sourceRunId, requestId }) => window.lexoraDesktop.localChat.chat.regenerateAssistant({ conversationId, sourceRunId, requestId }), { conversationId: first.conversation_id, sourceRunId: replay.nextRunId, requestId: randomUUID() }) + expect(fork.branchId).not.toBe(first.branch_id) + await expect.poll(() => read(db => db.prepare('SELECT status FROM runs WHERE id = ?').get(fork.runId).status)).toBe('completed') + await page.evaluate(async ({ conversationId, branchId }) => { + const conversations = window.lexoraDesktop.localChat.conversations + const current = await conversations.get(conversationId) + await conversations.activateBranch({ conversationId, branchId, expectedRevision: current.metadataRevision }) + }, { conversationId: first.conversation_id, branchId: first.branch_id }) + await signal('branches-created') + const evidence = await checkpoint('done') + expect(evidence).toMatchObject({ directStopRetainedInput: true, branchDraftIsolation: true, forkBranchId: fork.branchId, originalBranchId: first.branch_id }) + expect(read(db => db.prepare('SELECT active_branch_id FROM conversations WHERE id = ?').get(first.conversation_id).active_branch_id)).toBe(first.branch_id) + await expect(page).toHaveURL(/#\/settings\/general/) + const result = await instrument + await fs.writeFile(testInfo.outputPath('android-instrumentation.txt'), result.result?.stdout ?? String(result.error)) + expect(result.error).toBeUndefined() + expect(result.result.stdout).toMatch(/OK \(1 test\)/) + expect(result.result.stdout).not.toMatch(/INSTRUMENTATION_STATUS_CODE: -[234]|FAILURES/) + expect(read(db => db.prepare('SELECT COUNT(*) AS count FROM runs').get().count)).toBe(3) + expect(requests).toHaveLength(4) + for (const stage of ['stopped', 'final']) { + const capture = await execute(process.env.ADB ?? 'adb', ['-s', serial, 'exec-out', 'run-as', appPackage, 'cat', `files/controls-${token}-${stage}.png`], { encoding: 'buffer', maxBuffer: 8 * 1024 * 1024 }) + await fs.writeFile(testInfo.outputPath(`android-${stage}.png`), capture.stdout) + } + await fs.writeFile(testInfo.outputPath('task-control-evidence.json'), JSON.stringify({ ...evidence, coreAdapter: serviceMode, modelRequests: requests.length, syntheticProvider: true }, null, 2)) + } + catch (error) { failure = error } + finally { + const cleanupErrors = [] + const cleanup = async (action) => { + try { + await action() + } + catch (error) { cleanupErrors.push(error) } + } + if (started) + await cleanup(() => adb('shell', 'am', 'force-stop', appPackage)) + const result = await instrument + if (result) + await cleanup(() => fs.writeFile(testInfo.outputPath('android-instrumentation.txt'), result.result?.stdout ?? String(result.error))) + await cleanup(() => adb('shell', 'rm', '-f', remoteConfig)) + if (started) + await cleanup(() => adb('shell', 'run-as', appPackage, 'rm', '-f', ...files)) + if (reversed) + await cleanup(() => adb('reverse', '--remove', `tcp:${port}`)) + await cleanup(() => fs.rm(configuration, { force: true })) + await cleanup(async () => { + for (const device of await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.devices({}))) { + if (device.state === 'active') + await page.evaluate(deviceId => window.lexoraDesktop.localChat.privateNetwork.revoke({ deviceId }), device.deviceId) + } + await page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.configure({ enabled: false })) + }) + for (const gate of gates) gate.resolve() + provider.closeAllConnections() + await cleanup(() => new Promise(resolve => provider.close(resolve))) + if (cleanupErrors.length) { + if (!failure) + failure = new AggregateError(cleanupErrors, 'Mobile task control cleanup failed') + else + await cleanup(() => fs.writeFile(testInfo.outputPath('cleanup-errors.txt'), cleanupErrors.map(error => String(error)).join('\n'))) + } + } + if (failure) + throw failure +}) diff --git a/.playwright/scripts/__tests__/persistence-upgrade.e2e.mjs b/.playwright/scripts/__tests__/persistence-upgrade.e2e.mjs new file mode 100644 index 000000000..87dab8898 --- /dev/null +++ b/.playwright/scripts/__tests__/persistence-upgrade.e2e.mjs @@ -0,0 +1,178 @@ +import { Buffer } from 'node:buffer' +import { createHash, randomUUID } from 'node:crypto' +import fs from 'node:fs/promises' +import { createServer } from 'node:http' +import { createRequire, isBuiltin } from 'node:module' +import path from 'node:path' +import process from 'node:process' +import { DatabaseSync } from 'node:sqlite' +import { pathToFileURL } from 'node:url' +import { expect, test } from '../fixtures/electron.mjs' +import { ROOT } from '../shared.mjs' + +const requireBuddy = createRequire(path.join(ROOT, 'apps/buddy/package.json')) +const contentText = '旧版迁移附件 😀 完整字节保留。\n' + +test.use({ passwordStore: process.env.LEXORA_TEST_PASSWORD_STORE }) + +test('installed legacy data migrates with attachments and accepts new work without changing the source', async ({ buddy }, testInfo) => { + test.skip(!process.env.LEXORA_TEST_LEGACY_EXECUTABLE, 'Set the installed legacy executable to validate its actual data') + test.setTimeout(240000) + const requests = [] + const server = createServer(async (request, response) => { + if (request.method !== 'POST' || request.url !== '/v1/chat/completions') { + response.writeHead(404).end() + return + } + const chunks = [] + for await (const chunk of request) chunks.push(chunk) + requests.push(JSON.parse(Buffer.concat(chunks).toString('utf8'))) + const common = { id: `migration-${requests.length}`, model: 'migration-fixture', object: 'chat.completion.chunk', created: 1 } + response.writeHead(200, { 'content-type': 'text/event-stream' }) + response.write(`data: ${JSON.stringify({ ...common, choices: [{ index: 0, delta: { role: 'assistant', content: '迁移验收固定输出 😀' }, finish_reason: null }] })}\n\n`) + response.write(`data: ${JSON.stringify({ ...common, choices: [{ index: 0, delta: {}, finish_reason: 'stop' }], usage: { prompt_tokens: 32, completion_tokens: 8, total_tokens: 40 } })}\n\n`) + response.end('data: [DONE]\n\n') + }) + await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)) + const source = await buddy.createInstance('installed-legacy', { executablePath: process.env.LEXORA_TEST_LEGACY_EXECUTABLE }) + const candidate = await buddy.createInstance('migration-candidate') + let success = false + try { + const { page } = await source.launch() + const info = await page.evaluate(() => window.lexoraDesktop.app.getInfo()) + await page.evaluate(async (baseUrl) => { + const providers = window.lexoraDesktop.localChat.providers + await providers.upsertCustom({ id: 'migration-fixture', displayName: 'Migration fixture', api: 'openai-completions', baseUrl, enabled: true, models: [{ id: 'migration-fixture', name: 'Migration fixture', input: ['text'], reasoning: false, contextWindow: 128000, maxTokens: 1024 }] }) + const stop = providers.onAuthChallenge((challenge) => { + if (challenge.providerId === 'migration-fixture' && challenge.type === 'secret') + void providers.respondToAuth(challenge.challengeId, 'isolated-migration-fixture-key') + }) + try { + await providers.login('migration-fixture', 'api_key') + } + finally { stop() } + await providers.setDefaultModel({ providerId: 'migration-fixture', modelId: 'migration-fixture', reasoning: null }) + await window.lexoraDesktop.settings.update({ desktop: { notificationsEnabled: false } }) + }, `http://127.0.0.1:${server.address().port}/v1`) + await page.reload() + const editor = page.locator('.desktop-workbench-area__tasks .tiptap:visible').first() + await expect(editor).toBeVisible() + await editor.fill('由已安装旧版创建的输入 😀') + await expect.poll(() => page.evaluate(async () => (await window.lexoraDesktop.localChat.composerDrafts.list()).some(draft => JSON.stringify(draft.content).includes('由已安装旧版创建')))).toBe(true) + const editorDraftId = await page.evaluate(async () => (await window.lexoraDesktop.localChat.composerDrafts.list()).find(draft => JSON.stringify(draft.content).includes('由已安装旧版创建')).draftId) + const oldDraft = await createAttachmentDraft(page, '由已安装旧版创建的输入 😀', contentText, editorDraftId) + const turn = await page.evaluate(async (draftId) => { + const api = window.lexoraDesktop.localChat + const draft = await api.composerDrafts.get(draftId) + return api.chat.startTurn({ draftId, expectedRevision: draft.revision, requestId: crypto.randomUUID() }) + }, (await createAttachmentDraft(page, '旧版带附件任务', contentText)).draftId) + await expect.poll(() => readRows(source.home, 'SELECT status FROM runs WHERE id = ?', turn.run.id)[0]?.status, { timeout: 45000 }).toBe('completed') + expect(JSON.stringify(requests)).toContain('旧版迁移附件') + const originalDraft = await page.evaluate(id => window.lexoraDesktop.localChat.composerDrafts.get(id), oldDraft.draftId) + await source.stop() + const reopened = await source.launch() + expect(await reopened.page.evaluate(id => window.lexoraDesktop.localChat.composerDrafts.get(id), oldDraft.draftId)).toEqual(originalDraft) + await source.stop() + await fs.writeFile(path.join(source.home, 'unknown-preserved.bin'), Buffer.from([0, 1, 127, 255])) + const sourceRows = readRows(source.home, 'SELECT id, conversation_id, branch_id, status, pi_session_file FROM runs ORDER BY id') + expect(sourceRows.some(row => row.pi_session_file)).toBe(true) + const original = await fingerprints(source.home) + const output = testInfo.outputPath('migration-module') + const { build } = await import(pathToFileURL(requireBuddy.resolve('vite')).href) + await build({ configFile: false, logLevel: 'error', build: { ssr: path.join(ROOT, '.playwright/scripts/__tests__/prepareProductProfile.ts'), outDir: output, target: 'node26', rolldownOptions: { external: isBuiltin, output: { entryFileNames: 'prepare.mjs', format: 'es' } } } }) + const { activatePersistenceCandidate, importPersistenceCandidate, openBuddySqliteDatabase } = await import(pathToFileURL(path.join(output, 'prepare.mjs')).href) + expect(() => openBuddySqliteDatabase({ databasePath: path.join(source.home, 'buddy/buddy.sqlite3') })).toThrow('explicit persistence migration required') + expect(await fingerprints(source.home)).toEqual(original) + const input = { sourceRoot: source.home, candidateRoot: path.join(candidate.home, 'migration'), leaseDirectory: path.join(buddy.directory, 'leases'), signal: new AbortController().signal } + for (const point of ['copied', 'run', 'files', 'verified']) { + await expect(importPersistenceCandidate({ ...input, checkpoint: async (phase) => { + if (phase === point) + throw new Error(`Interrupted at ${point}`) + } })).rejects.toThrow(`Interrupted at ${point}`) + expect(await fingerprints(source.home)).toEqual(original) + } + const migrated = await activatePersistenceCandidate(input) + expect(await activatePersistenceCandidate(input)).toEqual(migrated) + expect(await fingerprints(source.home)).toEqual(original) + expect(readRows(migrated.profileRoot, 'SELECT authority, migration_state FROM storage_profile')).toEqual([{ authority: 'sqlite', migration_state: 'ready' }]) + expect(readRows(migrated.profileRoot, 'SELECT id, conversation_id, branch_id, status, pi_session_file FROM runs ORDER BY id')).toEqual(sourceRows.map(row => ({ ...row, pi_session_file: row.pi_session_file?.replace(source.home, migrated.profileRoot) ?? null }))) + expect(await fs.readFile(path.join(migrated.profileRoot, 'unknown-preserved.bin'))).toEqual(Buffer.from([0, 1, 127, 255])) + const migratedLaunch = await candidate.launch({ profileRoot: migrated.profileRoot, storageAuthority: 'sqlite' }) + expect(await migratedLaunch.page.evaluate(id => window.lexoraDesktop.localChat.composerDrafts.get(id), oldDraft.draftId)).toMatchObject(originalDraft) + const attachment = await migratedLaunch.page.evaluate(async (id) => { + const api = window.lexoraDesktop.localChat + const resource = (await api.composerResources.list(id))[0] + return api.composerResources.readText({ draftId: id, resourceId: resource.resourceId }) + }, oldDraft.draftId) + expect(attachment).toBe(contentText) + const nextDraft = await createAttachmentDraft(migratedLaunch.page, '迁移后新工作', contentText) + const next = await migratedLaunch.page.evaluate(async (draftId) => { + const api = window.lexoraDesktop.localChat + const draft = await api.composerDrafts.get(draftId) + const command = { draftId, expectedRevision: draft.revision, requestId: crypto.randomUUID() } + const first = await api.chat.startTurn(command) + const retry = await api.chat.startTurn(command) + return { first, retry } + }, nextDraft.draftId) + expect(next.retry.run.id).toBe(next.first.run.id) + await expect.poll(() => readRows(migrated.profileRoot, 'SELECT status FROM runs WHERE id = ?', next.first.run.id)[0]?.status, { timeout: 45000 }).toBe('completed') + await candidate.stop() + const databaseBytes = await fs.readFile(path.join(migrated.profileRoot, 'buddy/buddy.sqlite3')) + const rejected = await source.launch({ profileRoot: migrated.profileRoot, startupStatus: 'failed' }) + let rejection + await expect.poll(async () => { + rejection = await rejected.page.evaluate(() => window.lexoraDesktop.app.startup.getState()) + return rejection.status + }).toBe('failed') + expect(rejection.stages).toEqual(expect.arrayContaining([expect.objectContaining({ stage: 'runtime.database', status: 'failed' })])) + await source.stop() + expect(await fs.readFile(path.join(migrated.profileRoot, 'buddy/buddy.sqlite3'))).toEqual(databaseBytes) + expect(await fingerprints(source.home)).toEqual(original) + await fs.writeFile(testInfo.outputPath('migration-result.json'), JSON.stringify({ installedVersion: info.version, installedExecutable: process.env.LEXORA_TEST_LEGACY_EXECUTABLE, sourceRuns: sourceRows.length, migrated, oldRun: turn.run.id, newRun: next.first.run.id, sourceUnchanged: true, migrationCheckpoints: ['copied', 'run', 'files', 'verified'], oldBinaryRejection: rejection }, null, 2)) + success = true + } + finally { + await source.stop() + await candidate.stop() + server.closeAllConnections() + await new Promise(resolve => server.close(resolve)) + if (!success) + await fs.writeFile(testInfo.outputPath('provider-requests.json'), JSON.stringify(requests, null, 2)) + } +}) + +async function createAttachmentDraft(page, text, attachment, draftId = randomUUID()) { + return page.evaluate(async ({ text, attachment, draftId, resourceId }) => { + const api = window.lexoraDesktop.localChat + const content = { version: 1, body: [{ type: 'paragraph', content: [{ type: 'text', text }] }], panelResourceIds: [] } + let draft = await api.composerDrafts.open({ draftId, scope: { kind: 'task', draftId, spaceId: null }, initialContent: content, initialExecutionConfig: { approvalPolicy: 'policy', executionProfile: 'workspace_write' }, initialModelSelection: { providerId: 'migration-fixture', modelId: 'migration-fixture', reasoning: null, serviceTier: null } }) + const bytes = new TextEncoder().encode(attachment) + await api.composerResources.accept({ draftId, resources: [{ resourceId, name: '迁移附件.txt', mimeType: 'text/plain', sizeBytes: bytes.length }] }) + await api.composerResources.complete({ draftId, resourceId, bytes }) + draft = await api.composerDrafts.get(draftId) + return api.composerDrafts.save({ draftId, expectedRevision: draft.revision, content: { ...content, panelResourceIds: [resourceId] }, modelSelection: draft.modelSelection, executionConfig: draft.executionConfig }) + }, { text, attachment, draftId, resourceId: randomUUID() }) +} + +function readRows(profileRoot, sql, ...params) { + const database = new DatabaseSync(path.join(profileRoot, 'buddy/buddy.sqlite3'), { readOnly: true }) + try { + return database.prepare(sql).all(...params).map(row => ({ ...row })) + } + finally { database.close() } +} + +async function fingerprints(root) { + const result = {} + for (const entry of await fs.readdir(root, { withFileTypes: true, recursive: true })) { + const absolute = path.join(entry.parentPath, entry.name) + const relative = path.relative(root, absolute) + if (relative.startsWith('.runtime/') || relative === 'buddy/buddy.sqlite3-shm') + continue + if (entry.isFile()) + result[relative] = createHash('sha256').update(await fs.readFile(absolute)).digest('hex') + else if (entry.isSymbolicLink()) + result[relative] = await fs.readlink(absolute) + } + return result +} diff --git a/.playwright/scripts/__tests__/prepareProductProfile.ts b/.playwright/scripts/__tests__/prepareProductProfile.ts new file mode 100644 index 000000000..1f4248aba --- /dev/null +++ b/.playwright/scripts/__tests__/prepareProductProfile.ts @@ -0,0 +1,15 @@ +import { join } from 'node:path' +import { openBuddySqliteDatabase } from '../../../apps/buddy/service/src/storage/database' + +export function prepareProductProfile(profileRoot: string) { + openBuddySqliteDatabase({ databasePath: join(profileRoot, 'buddy/buddy.sqlite3') }).close() +} + +export { resolveBuddyProcessControl } from '../../../apps/buddy/platform/native/nativeHost' +export { SessionServiceRegistration } from '../../../apps/buddy/platform/service/SessionServiceRegistration' + +export { openBuddySqliteDatabase } from '../../../apps/buddy/service/src/storage/database' + +export { activatePersistenceCandidate } from '../../../apps/buddy/service/src/storage/persistenceMigration/activatePersistenceCandidate' + +export { importPersistenceCandidate } from '../../../apps/buddy/service/src/storage/persistenceMigration/importPersistenceCandidate' diff --git a/.playwright/scripts/__tests__/product-desktop.e2e.mjs b/.playwright/scripts/__tests__/product-desktop.e2e.mjs new file mode 100644 index 000000000..40ef75dac --- /dev/null +++ b/.playwright/scripts/__tests__/product-desktop.e2e.mjs @@ -0,0 +1,56 @@ +import fs from 'node:fs/promises' +import { createRequire, isBuiltin } from 'node:module' +import path from 'node:path' +import { fileURLToPath, pathToFileURL } from 'node:url' +import { expect, test } from '../fixtures/electron.mjs' + +const root = fileURLToPath(new URL('../../../', import.meta.url)) +const requireBuddy = createRequire(path.join(root, 'apps/buddy/package.json')) + +test('candidate Desktop persists inputs and recovers stale saves through the product protocol', async ({ buddy }) => { + const instance = await buddy.createInstance('product-desktop') + const output = test.info().outputPath('profile-preparation') + const { build } = await import(pathToFileURL(requireBuddy.resolve('vite')).href) + await build({ configFile: false, logLevel: 'error', build: { + ssr: path.join(root, '.playwright/scripts/__tests__/prepareProductProfile.ts'), + outDir: output, + target: 'node26', + rolldownOptions: { external: isBuiltin, output: { entryFileNames: 'prepare.mjs', format: 'es' } }, + } }) + const { prepareProductProfile } = await import(pathToFileURL(path.join(output, 'prepare.mjs')).href) + prepareProductProfile(instance.home) + let { page } = await instance.launch({ storageAuthority: 'sqlite' }) + const editor = page.locator('.desktop-workbench-area__tasks .tiptap:visible').first() + await expect(editor).toBeVisible() + await editor.fill('Persist the candidate input 中文 🚀') + await expect.poll(() => page.evaluate(async () => (await window.lexoraDesktop.localChat.composerDrafts.list()).map(draft => JSON.stringify(draft.content)).join('\n'))).toContain('Persist the candidate input') + const proof = await page.evaluate(async () => { + const api = window.lexoraDesktop.localChat + const draft = (await api.composerDrafts.list()).find(draft => JSON.stringify(draft.content).includes('Persist the candidate input')) + const content = { version: 1, body: [{ type: 'paragraph', content: [{ type: 'text', text: 'Recover unconfirmed desktop input' }] }], panelResourceIds: [] } + let rejected = false + try { + await api.composerDrafts.save({ draftId: draft.draftId, expectedRevision: draft.revision + 9, content, modelSelection: draft.modelSelection, executionConfig: draft.executionConfig }) + } + catch { rejected = true } + return { rejected, originalDraftId: draft.draftId } + }) + expect(proof.rejected).toBe(true) + await expect.poll(() => page.evaluate(() => window.lexoraDesktop.localChat.runtime.listRecovery())).toMatchObject([expect.objectContaining({ state: 'rejected', canRestore: true })]) + await instance.stop() + const reopened = await instance.launch({ storageAuthority: 'sqlite' }) + page = reopened.page + await expect(page.locator('.desktop-workbench-area__tasks .tiptap:visible').first()).toContainText('Persist the candidate input') + await page.getByRole('button', { name: '查看', exact: true }).click() + await expect(page.getByText('Recover unconfirmed desktop input', { exact: true })).toBeVisible() + await page.getByRole('button', { name: '保存为新草稿', exact: true }).click() + await expect(page.locator('.tiptap:visible').filter({ hasText: 'Recover unconfirmed desktop input' })).toBeVisible() + const result = await page.evaluate(async () => { + const api = window.lexoraDesktop.localChat + return { drafts: (await api.composerDrafts.list()).map(draft => ({ draftId: draft.draftId, content: draft.content })), recovery: await api.runtime.listRecovery() } + }) + expect(result.recovery).toEqual([]) + expect(result.drafts.find(draft => draft.draftId === proof.originalDraftId)?.content.body[0].content[0].text).toContain('Persist the candidate input') + await fs.writeFile(test.info().outputPath('product-desktop.json'), JSON.stringify({ ...proof, ...result }, null, 2)) + await page.screenshot({ path: test.info().outputPath('product-desktop.png') }) +}) diff --git a/.playwright/scripts/__tests__/service-process.e2e.mjs b/.playwright/scripts/__tests__/service-process.e2e.mjs new file mode 100644 index 000000000..54fbc83db --- /dev/null +++ b/.playwright/scripts/__tests__/service-process.e2e.mjs @@ -0,0 +1,275 @@ +import assert from 'node:assert/strict' +import { Buffer } from 'node:buffer' +import { execFile, fork } from 'node:child_process' +import { createHash, randomUUID } from 'node:crypto' +import { once } from 'node:events' +import fs from 'node:fs/promises' +import { createServer } from 'node:http' +import { createRequire, isBuiltin } from 'node:module' +import os from 'node:os' +import path from 'node:path' +import process from 'node:process' +import { setTimeout as delay } from 'node:timers/promises' +import { fileURLToPath, pathToFileURL } from 'node:url' +import { promisify } from 'node:util' +import { expect, test } from '@playwright/test' + +const execute = promisify(execFile) +const root = fileURLToPath(new URL('../../../', import.meta.url)) +const buddyRoot = path.join(root, 'apps/buddy') +const requireBuddy = createRequire(path.join(buddyRoot, 'package.json')) + +test('standalone Node artifact keeps accepted work across client disconnect and service restart', async () => { + const testInfo = test.info() + const directory = await fs.mkdtemp(path.join(os.tmpdir(), 'lexora-service-process-')) + const candidate = path.join(directory, 'candidate') + const coreHome = path.join(directory, 'profile') + const work = path.join(directory, 'work') + const file = path.join(work, 'fixture.txt') + const report = { requests: [], results: {} } + const source = { PATH: process.env.PATH, HOME: directory, TMPDIR: directory, TEMP: directory, SystemRoot: process.env.SystemRoot, COMSPEC: process.env.COMSPEC } + const children = [] + let finishStream + const server = createServer((request, response) => { + void (async () => { + const chunks = [] + for await (const chunk of request) chunks.push(chunk) + const payload = JSON.parse(Buffer.concat(chunks).toString()) + assert.equal(request.headers.authorization, 'Bearer process-fixture-key') + const user = JSON.stringify(payload.messages.findLast(message => message.role === 'user')) + const toolResult = payload.messages.findLast(message => message.role === 'tool') + const mode = user.includes('FIXTURE_FILE') ? 'file' : user.includes('FIXTURE_STREAM') ? 'stream' : 'text' + report.requests.push({ mode, toolReadMatched: Boolean(toolResult && JSON.stringify(toolResult).includes('isolated-node-file')) }) + response.writeHead(200, { 'content-type': 'text/event-stream' }) + const common = { id: randomUUID(), model: 'fixture-model', object: 'chat.completion.chunk', created: 1 } + const send = (delta, finishReason = null) => response.write(`data: ${JSON.stringify({ ...common, choices: [{ index: 0, delta, finish_reason: finishReason }] })}\n\n`) + send({ role: 'assistant', content: '' }) + if (mode === 'stream') { + send({ content: 'fixture-stream-started' }) + finishStream = () => { + send({}, 'stop') + response.end('data: [DONE]\n\n') + } + return + } + if (mode === 'file' && !toolResult) { + send({ tool_calls: [{ index: 0, id: 'fixture-read', type: 'function', function: { name: 'read', arguments: JSON.stringify({ path: file }) } }] }) + send({}, 'tool_calls') + } + else { + send({ content: mode === 'file' ? 'fixture-file-ok' : 'fixture-text-ok' }) + send({}, 'stop') + } + response.end('data: [DONE]\n\n') + })().catch((error) => { + report.serverError = error.message + response.writeHead(500) + response.end('Fixture failed') + }) + }) + try { + await fs.cp(path.join(buddyRoot, '.output/build/service'), candidate, { recursive: true, verbatimSymlinks: true }) + await fs.mkdir(work) + await fs.writeFile(file, 'isolated-node-file\n') + server.listen(0, '127.0.0.1') + await once(server, 'listening') + const baseUrl = `http://127.0.0.1:${server.address().port}` + report.closure = await auditCandidate(candidate) + const manifest = JSON.parse(await fs.readFile(path.join(candidate, 'resources.json'), 'utf8')) + for (const resource of [...manifest.bundled, ...manifest.resources]) + expect(await hashFile(path.join(candidate, resource.path)), resource.path).toBe(resource.sha256) + const runtime = path.join(directory, process.platform === 'win32' ? 'node.exe' : 'node') + await fs.copyFile(process.execPath, runtime) + await fs.chmod(runtime, 0o700) + const inventory = await execute(runtime, ['--input-type=module', '-e', ` + import { createRequire } from 'node:module'; + import { DatabaseSync } from 'node:sqlite'; + const require = createRequire(import.meta.resolve('@earendil-works/pi-coding-agent')); + const photon = require('@silvia-odwyer/photon-node'); + const image = new photon.PhotonImage(new Uint8Array([255, 0, 0, 255]), 1, 1); + const db = new DatabaseSync(':memory:'); + console.log(JSON.stringify({ versions: process.versions, execPath: process.execPath, sqlite: db.prepare('select sqlite_version() as version').get(), wasm: { width: image.get_width(), height: image.get_height() }, modules: [require.resolve('@silvia-odwyer/photon-node'), import.meta.resolve('unpdf')] })); + image.free(); db.close(); await import('unpdf'); + `], { cwd: candidate, env: source }) + report.inventory = JSON.parse(inventory.stdout) + expect(report.inventory.versions.electron).toBeUndefined() + expect(report.inventory.execPath).toBe(runtime) + expect(report.inventory.wasm).toEqual({ width: 1, height: 1 }) + for (const module of report.inventory.modules) { + const resolved = module.startsWith('file:') ? fileURLToPath(module) : module + expect(path.relative(candidate, resolved).startsWith('..'), module).toBe(false) + } + const { build } = await import(pathToFileURL(requireBuddy.resolve('vite')).href) + await build({ configFile: false, logLevel: 'error', build: { + ssr: path.join(root, '.playwright/scripts/fixtures/service-process-harness.ts'), + outDir: path.join(directory, 'harness'), + target: 'node26', + rolldownOptions: { external: isBuiltin, output: { entryFileNames: 'harness.mjs', format: 'es' } }, + } }) + const { RuntimeRpcPeer, createBuddyServiceEnvironment, BUDDY_SERVICE_PROTOCOL_VERSION, DEFAULT_RUNTIME_PREFERENCES, registerFixtureHost } = await import(pathToFileURL(path.join(directory, 'harness/harness.mjs')).href) + function startCore(resourcesRoot = path.join(candidate, 'resources')) { + const child = fork(path.join(candidate, 'service.mjs'), [], { execPath: runtime, execArgv: [], cwd: work, env: createBuddyServiceEnvironment(source, coreHome), stdio: ['ignore', 'pipe', 'pipe', 'ipc'] }) + children.push(child) + let stderr = '' + child.stderr.on('data', (value) => { + stderr = `${stderr}${value}`.slice(-32768) + }) + child.stdout.resume() + const messages = [] + child.on('message', message => messages.push(message)) + const peers = new Map() + function channel(id) { + const peer = new RuntimeRpcPeer({ transport: { postMessage: message => child.send({ type: 'rpc', channel: id, message }), subscribe(listener) { + const receive = (message) => { + if (message.type === 'rpc' && message.channel === id) + listener(message.message) + } + child.on('message', receive) + return () => child.off('message', receive) + } } }) + peers.set(id, peer) + return peer + } + registerFixtureHost(channel('host')) + const exited = new Promise(resolve => child.once('exit', (code, signal) => { + for (const peer of peers.values()) peer.close(new Error('Service exited')) + resolve({ code, signal, stderr }) + })) + const connect = (id, role) => { + const peer = channel(id) + child.send({ type: 'connect', channel: id, role }) + return peer + } + child.send({ type: 'connect', channel: 'too-early', role: 'product' }) + child.send({ type: 'start', protocolVersion: BUDDY_SERVICE_PROTOCOL_VERSION, config: { profile: 'test', buddyHome: coreHome, resourcesRoot, proxy: { httpProxy: '', httpsProxy: '', noProxy: '' }, preferences: { ...DEFAULT_RUNTIME_PREFERENCES, codemode: 'off', modelRetryLimit: 0 }, display: false } }) + return { + child, + messages, + connect, + exited, + async ready() { + await expect.poll(() => messages.find(message => ['ready', 'failed'].includes(message.type)), { timeout: 30000 }).toBeTruthy() + expect(messages.find(message => ['ready', 'failed'].includes(message.type)), stderr).toEqual({ type: 'ready', protocolVersion: BUDDY_SERVICE_PROTOCOL_VERSION }) + }, + close(id) { + child.send({ type: 'disconnect', channel: id }) + peers.get(id)?.close(new Error('Client disconnected')) + }, + } + } + let core = startCore(path.join(candidate, 'missing')) + report.results.missingResources = await core.exited + expect(report.results.missingResources.code).toBe(1) + await expect(fs.stat(path.join(coreHome, 'buddy.sqlite3'))).rejects.toThrow() + core = startCore() + await core.ready() + expect(core.messages).toContainEqual({ type: 'rejected', channel: 'too-early', code: 'SERVICE_NOT_READY' }) + const management = core.connect('management', 'management') + let product = core.connect('first', 'product') + const second = core.connect('second', 'product') + await expect(management.request('runtime.status', {})).resolves.toMatchObject({ ready: true }) + await expect(product.request('runtime.shutdown', {})).rejects.toMatchObject({ code: -32601 }) + await expect(product.request('extensions.agent.request', {})).rejects.toMatchObject({ code: -32601 }) + await expect(product.request('providers.upsertCustom', {})).rejects.toMatchObject({ code: -32601 }) + await management.request('providers.upsertCustom', { id: 'process-fixture', displayName: 'Isolated process fixture', api: 'openai-completions', baseUrl: `${baseUrl}/v1`, enabled: true, models: [{ id: 'fixture-model', name: 'Fixture', input: ['text'], reasoning: false, contextWindow: 128000, maxTokens: 1024 }] }) + const space = await management.request('spaces.create', { name: 'Isolated workspace', memoryScope: 'space_only', primaryDirectory: { id: null, root: work }, primaryDirectorySelectionVerified: true }) + report.results.search = await product.request('spaces.searchFiles', { spaceId: space.id, query: 'fixture', deepSearch: true }) + expect(report.results.search.some(entry => entry.path === 'fixture.txt' || entry.path === file)).toBe(true) + report.results.nativeRead = await management.request('spaceFiles.readDocument', { spaceId: space.id, directoryId: space.primaryDirectory.id, revision: space.primaryDirectory.revision, path: 'fixture.txt' }) + expect(report.results.nativeRead.text).toBe('isolated-node-file\n') + async function startTask(text) { + const draftId = randomUUID() + const content = { version: 1, body: [{ type: 'paragraph', content: [{ type: 'text', text }] }], panelResourceIds: [] } + const executionConfig = { approvalPolicy: 'policy', executionProfile: 'workspace_write' } + const modelSelection = { providerId: 'process-fixture', modelId: 'fixture-model', reasoning: null, serviceTier: null } + const opened = await product.request('composerDrafts.open', { draftId, scope: { kind: 'task', draftId, spaceId: space.id }, initialContent: content, initialExecutionConfig: executionConfig, initialModelSelection: modelSelection }) + const draft = await product.request('composerDrafts.save', { draftId: opened.draftId, expectedRevision: opened.revision, content, executionConfig, modelSelection }) + return product.request('chat.startTurn', { draftId: draft.draftId, expectedRevision: draft.revision, requestId: randomUUID() }) + } + async function terminal(runId) { + let result + await expect.poll(async () => { + result = await product.request('runs.get', { runId }) + return result.status + }, { timeout: 30000 }).toMatch(/^(?:completed|cancelled|failed)$/) + expect(result.status, JSON.stringify(result)).toBe('completed') + return result + } + report.results.text = await terminal((await startTask('FIXTURE_TEXT')).runId) + report.results.file = await terminal((await startTask('FIXTURE_FILE')).runId) + expect(report.requests.some(value => value.toolReadMatched)).toBe(true) + const continued = await startTask('FIXTURE_STREAM') + await expect.poll(() => Boolean(finishStream)).toBe(true) + await expect(management.request('runtime.shutdown', { onlyIfIdle: true })).resolves.toEqual({ accepted: false }) + core.close('first') + product = second + await expect(second.request('runs.get', { runId: continued.runId })).resolves.toMatchObject({ status: 'running' }) + finishStream() + report.results.disconnected = await terminal(continued.runId) + const third = core.connect('third', 'product') + core.child.send({ type: 'rpc', channel: 'second', message: { invalid: true } }) + await expect(third.request('runs.get', { runId: continued.runId })).resolves.toMatchObject({ status: 'completed' }) + core.child.send({ type: 'connect', channel: 'first', role: 'product' }) + await expect.poll(() => core.messages.find(message => message.type === 'rejected' && message.channel === 'first')).toEqual({ type: 'rejected', channel: 'first', code: 'SERVICE_CHANNEL_EXISTS' }) + report.results.requestCount = report.requests.length + await expect(management.request('runtime.shutdown', {})).resolves.toEqual({ accepted: true }) + report.results.stopped = await core.exited + expect(report.results.stopped.code).toBe(0) + core = startCore() + await core.ready() + const restored = core.connect('restored', 'product') + const admin = core.connect('admin', 'management') + await expect(restored.request('runs.list', {})).resolves.toHaveLength(3) + await expect(restored.request('runs.get', { runId: continued.runId })).resolves.toMatchObject({ status: 'completed' }) + expect(report.requests.length).toBe(report.results.requestCount) + await expect(admin.request('runtime.shutdown', { onlyIfIdle: true })).resolves.toEqual({ accepted: true }) + expect((await core.exited).code).toBe(0) + core = startCore() + await core.ready() + core.child.disconnect() + report.results.ownerDisconnected = await core.exited + expect(report.results.ownerDisconnected.code).toBe(1) + expect(report.serverError).toBeUndefined() + } + finally { + finishStream?.() + for (const child of children) { + if (child.exitCode !== null || child.signalCode !== null) + continue + child.kill('SIGTERM') + await Promise.race([once(child, 'exit'), delay(5000)]) + if (child.exitCode === null && child.signalCode === null) { + child.kill('SIGKILL') + await once(child, 'exit') + } + } + server.closeAllConnections() + await new Promise(resolve => server.close(resolve)) + const reportPath = testInfo.outputPath('service-process.json') + await fs.writeFile(reportPath, JSON.stringify(report, null, 2)) + await testInfo.attach('service-process.json', { path: reportPath, contentType: 'application/json' }) + await fs.rm(directory, { recursive: true, force: true }) + } +}) + +async function hashFile(file) { + return createHash('sha256').update(await fs.readFile(file)).digest('hex') +} + +async function auditCandidate(root) { + let files = 0 + let symlinks = 0 + for (const entry of await fs.readdir(root, { recursive: true, withFileTypes: true })) { + if (entry.isFile()) + files++ + if (!entry.isSymbolicLink()) + continue + symlinks++ + const resolved = await fs.realpath(path.join(entry.parentPath, entry.name)) + const relative = path.relative(root, resolved) + assert(relative && relative !== '..' && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative), 'Artifact symlink must stay inside the service package') + } + expect(files).toBeGreaterThan(0) + expect(symlinks).toBeGreaterThan(0) + return { files, symlinks } +} diff --git a/.playwright/scripts/__tests__/storage.e2e.mjs b/.playwright/scripts/__tests__/storage.e2e.mjs index c2cdc821a..ad9c47bed 100644 --- a/.playwright/scripts/__tests__/storage.e2e.mjs +++ b/.playwright/scripts/__tests__/storage.e2e.mjs @@ -1,23 +1,6 @@ import { Buffer } from 'node:buffer' import { expect, test } from '../fixtures/electron.mjs' -test('legacy caches and diagnostic logs alone do not trigger a startup migration', async ({ buddy }) => { - const { mkdir, readFile, writeFile } = await import('node:fs/promises') - const { join, dirname } = await import('node:path') - const instance = await buddy.createInstance('cache-only') - const files = ['.runtime/cache/chromium/Cache/data', '.runtime/cache/chromium/Code Cache/data', '.runtime/state/logs/old.log', '.runtime/state/crashes/old-report'] - for (const file of files) { - await mkdir(dirname(join(instance.home, file)), { recursive: true }) - await writeFile(join(instance.home, file), 'retained-fixture') - } - const desktop = await instance.launch() - const summary = await desktop.page.evaluate(() => window.lexoraDesktop.storage.summary()) - expect(summary.pending).toBeNull() - expect(summary.history).toEqual([]) - for (const file of files) - expect(await readFile(join(instance.home, file), 'utf8')).toBe('retained-fixture') -}) - test('stopping initial consolidation exits with source data intact and offers retry on next launch', async ({ buddy }) => { const { createHash } = await import('node:crypto') const { mkdir, open, readFile, stat } = await import('node:fs/promises') @@ -168,48 +151,50 @@ test('local backup limits defer deletion until confirmation or the next successf await expect(page.getByRole('button', { name: '查看备份 (1)', exact: true })).toBeVisible() }) -test('preboot migration is visible, keeps runtime stopped, and a second launch uses the verified root', async ({ buddy }) => { - const { access, readFile, rename, writeFile } = await import('node:fs/promises') - const { createHash } = await import('node:crypto') - const { dirname, join } = await import('node:path') - const instance = await buddy.createInstance('migration') - const first = await instance.launch() - const target = `${instance.home}-moved` - await first.page.evaluate(target => window.lexoraDesktop.storage.relocate(target), target) - await instance.stop() - const locator = join(dirname(instance.home), '.lexora-locations', createHash('sha256').update(instance.home).digest('hex').slice(0, 24), 'location.json') - const pending = JSON.parse(await readFile(locator, 'utf8')) - pending.pending.phase = 'failed' - pending.pending.error = 'STORAGE_WORKER_INTERRUPTED' - await writeFile(locator, JSON.stringify(pending)) - const migration = await instance.launch({ maintenance: true }) - await expect(migration.page.getByRole('heading', { name: '正在搬迁数据' })).toBeVisible() - expect(await migration.app.evaluate(({ app }) => app.getAppMetrics().some(metric => metric.name === 'Buddy Service'))).toBe(false) - await migration.app.evaluate(({ app }) => { - app.relaunch = () => {} - }) - await migration.page.getByRole('link', { name: '重试', exact: true }).click() - await expect.poll(async () => JSON.parse(await readFile(locator, 'utf8')).activeRoot).toBe(target) - await instance.stop() - const restarted = await instance.launch({ expectedRoot: target }) - await restarted.page.evaluate(() => { - location.hash = '#/settings/storage' +for (const storageAuthority of ['legacy_jsonl', 'sqlite']) { + test(`preboot migration is visible, keeps runtime stopped, and a second launch uses the verified root (${storageAuthority})`, async ({ buddy }) => { + const { access, readFile, rename, writeFile } = await import('node:fs/promises') + const { createHash } = await import('node:crypto') + const { dirname, join } = await import('node:path') + const instance = await buddy.createInstance('migration') + const first = await instance.launch({ storageAuthority }) + const target = `${instance.home}-moved` + await first.page.evaluate(target => window.lexoraDesktop.storage.relocate(target), target) + await instance.stop() + const locator = join(dirname(instance.home), '.lexora-locations', createHash('sha256').update(instance.home).digest('hex').slice(0, 24), 'location.json') + const pending = JSON.parse(await readFile(locator, 'utf8')) + pending.pending.phase = 'failed' + pending.pending.error = 'STORAGE_WORKER_INTERRUPTED' + await writeFile(locator, JSON.stringify(pending)) + const migration = await instance.launch({ storageAuthority, maintenance: true }) + await expect(migration.page.getByRole('heading', { name: '正在搬迁数据' })).toBeVisible() + expect(await migration.app.evaluate(({ app }) => app.getAppMetrics().some(metric => metric.name === 'Buddy Service'))).toBe(false) + await migration.app.evaluate(({ app }) => { + app.relaunch = () => {} + }) + await migration.page.getByRole('link', { name: '重试', exact: true }).click() + await expect.poll(async () => JSON.parse(await readFile(locator, 'utf8')).activeRoot).toBe(target) + await instance.stop() + const restarted = await instance.launch({ storageAuthority, expectedRoot: target }) + await restarted.page.evaluate(() => { + location.hash = '#/settings/storage' + }) + await expect(restarted.page.getByTestId('storage-current-root')).toHaveText(target) + expect(JSON.parse(await readFile(locator, 'utf8')).pending).toBeNull() + await instance.stop() + await instance.launch({ storageAuthority, expectedRoot: target }) + expect(JSON.parse(await readFile(locator, 'utf8')).history).toHaveLength(1) + await instance.stop() + await rename(target, `${target}-offline`) + const unavailable = await instance.launch({ storageAuthority, maintenance: true }) + await expect(unavailable.page.getByRole('heading', { name: '暂时无法打开原有数据' })).toBeVisible() + expect(await unavailable.app.evaluate(({ app }) => app.getAppMetrics().some(metric => metric.name === 'Buddy Service'))).toBe(false) + await expect(access(target)).rejects.toMatchObject({ code: 'ENOENT' }) + await instance.stop() + await rename(`${target}-offline`, target) + await instance.launch({ storageAuthority, expectedRoot: target }) }) - await expect(restarted.page.getByTestId('storage-current-root')).toHaveText(target) - expect(JSON.parse(await readFile(locator, 'utf8')).pending).toBeNull() - await instance.stop() - await instance.launch({ expectedRoot: target }) - expect(JSON.parse(await readFile(locator, 'utf8')).history).toHaveLength(1) - await instance.stop() - await rename(target, `${target}-offline`) - const unavailable = await instance.launch({ maintenance: true }) - await expect(unavailable.page.getByRole('heading', { name: '暂时无法打开原有数据' })).toBeVisible() - expect(await unavailable.app.evaluate(({ app }) => app.getAppMetrics().some(metric => metric.name === 'Buddy Service'))).toBe(false) - await expect(access(target)).rejects.toMatchObject({ code: 'ENOENT' }) - await instance.stop() - await rename(`${target}-offline`, target) - await instance.launch({ expectedRoot: target }) -}) +} test('a failed move can return to the original data without retrying', async ({ buddy }) => { const { mkdir, writeFile } = await import('node:fs/promises') @@ -430,78 +415,134 @@ test('a missing initial data root stays absent while recovery is shown', async ( await instance.launch() }) -test('exports an encrypted selective backup and imports it into the configured root with unselected data preserved', async ({ buddy }) => { +for (const storageAuthority of ['legacy_jsonl', 'sqlite']) { + test(`exports an encrypted selective backup and imports it into the configured root with unselected data preserved (${storageAuthority})`, async ({ buddy }) => { + const { createHash, randomUUID } = await import('node:crypto') + const { mkdir, readFile, writeFile } = await import('node:fs/promises') + const { dirname, join } = await import('node:path') + const { useSyntheticCredentialStorage } = await import('../fixtures/electron.mjs') + const source = await buddy.createInstance('backup-export') + const desktop = await source.launch({ storageAuthority }) + await useSyntheticCredentialStorage(desktop) + const connection = { id: randomUUID(), credentialId: randomUUID(), adapter: 'webdav', name: 'Portable connection', endpoint: 'https://example.invalid/backup/', username: 'fixture' } + await mkdir(join(source.home, 'buddy/secrets/sync'), { recursive: true }) + await writeFile(join(source.home, 'device/sync.json'), JSON.stringify({ version: 1, enabled: false, selectedAdapter: 'webdav', activeConnectionId: connection.id, connections: [connection] })) + await writeFile(join(source.home, 'buddy/secrets/sync', `${createHash('sha256').update(connection.credentialId).digest('hex')}.credential`), `offline-fixture:${JSON.stringify({ id: connection.credentialId, value: 'synthetic-key-only', version: 1 })}`) + const snapshot = await desktop.page.evaluate(() => window.lexoraDesktop.storage.snapshot({ scopes: ['connections'], credentials: true, password: 'fixture-password' })) + expect(snapshot).toMatchObject({ scopes: ['connections'], credentials: true }) + const exportParent = join(source.home, 'exported') + await mkdir(exportParent, { recursive: true }) + await desktop.app.evaluate(({ dialog }, selected) => { + dialog.showOpenDialog = async () => ({ canceled: false, filePaths: [selected] }) + }, exportParent) + const exported = await desktop.page.evaluate(id => window.lexoraDesktop.storage.export(id), snapshot.id) + const destination = await buddy.createInstance('backup-import') + const receiving = await destination.launch({ storageAuthority }) + await useSyntheticCredentialStorage(receiving) + const space = await receiving.page.evaluate(() => window.lexoraDesktop.localChat.spaces.create({ name: 'Keep my current space', memoryScope: 'space_only', primaryDirectory: null })) + await receiving.app.evaluate(({ dialog }, selected) => { + dialog.showOpenDialog = async () => ({ canceled: false, filePaths: [selected] }) + }, exported) + expect(await receiving.page.evaluate(() => window.lexoraDesktop.storage.import())).toEqual(snapshot) + const wrong = await receiving.page.evaluate(async (id) => { + try { + await window.lexoraDesktop.storage.restore(id, { scopes: ['connections'], credentials: true, password: 'incorrect-password' }) + } + catch (error) { return String(error) } + }, snapshot.id) + expect(wrong).toContain('SNAPSHOT_PASSWORD_INVALID') + expect((await receiving.page.evaluate(() => window.lexoraDesktop.storage.summary())).pending).toBeNull() + await receiving.page.getByRole('button', { name: '设置', exact: true }).click() + await receiving.page.getByRole('link', { name: '数据存储', exact: true }).click() + await receiving.page.getByRole('button', { name: /^查看备份/ }).click() + await receiving.page.getByRole('button', { name: '恢复备份', exact: true }).click() + await expect(receiving.page.getByRole('checkbox', { name: /^工具与连接配置/ })).toBeChecked() + await expect(receiving.page.getByRole('checkbox', { name: /^任务与空间/ })).toBeDisabled() + await expect(receiving.page.locator('.n-modal')).toHaveCount(1) + await receiving.page.keyboard.press('Escape') + await receiving.page.evaluate(id => window.lexoraDesktop.storage.restore(id, { scopes: ['connections'], credentials: true, password: 'fixture-password' }), snapshot.id) + await destination.stop() + const locator = join(dirname(destination.home), '.lexora-locations', createHash('sha256').update(destination.home).digest('hex').slice(0, 24), 'location.json') + const state = JSON.parse(await readFile(locator, 'utf8')) + state.pending.phase = 'failed' + state.pending.error = 'STORAGE_WORKER_INTERRUPTED' + await writeFile(locator, JSON.stringify(state)) + const migration = await destination.launch({ storageAuthority, maintenance: true }) + await migration.app.evaluate(({ app }) => { + app.relaunch = () => {} + }) + await migration.page.getByRole('link', { name: '重试', exact: true }).click() + await expect.poll(async () => JSON.parse(await readFile(locator, 'utf8')).pending).toBeNull() + await destination.stop() + const restored = await destination.launch({ storageAuthority }) + await useSyntheticCredentialStorage(restored) + const connections = (await restored.page.evaluate(() => window.lexoraDesktop.sync.state())).connections + expect(connections).toHaveLength(1) + expect(connections[0]).toMatchObject({ name: 'Portable connection' }) + const credentialRestored = await restored.app.evaluate(({ safeStorage }, root) => { + const fs = process.getBuiltinModule('node:fs') + const path = process.getBuiltinModule('node:path') + const crypto = process.getBuiltinModule('node:crypto') + const connection = JSON.parse(fs.readFileSync(path.join(root, 'device/sync.json'), 'utf8')).connections[0] + const file = path.join(root, 'buddy/secrets/sync', `${crypto.createHash('sha256').update(connection.credentialId).digest('hex')}.credential`) + return JSON.parse(safeStorage.decryptString(fs.readFileSync(file))).value === 'synthetic-key-only' + }, destination.home) + expect(credentialRestored).toBe(true) + expect(await restored.page.evaluate(() => window.lexoraDesktop.localChat.spaces.list())).toEqual(expect.arrayContaining([expect.objectContaining({ id: space.id })])) + expect((await restored.page.evaluate(() => window.lexoraDesktop.storage.summary())).root).toBe(destination.home) + }) +} + +test('in-place persistence upgrade retries with visible progress and preserves independent stores', async ({ buddy }, testInfo) => { const { createHash, randomUUID } = await import('node:crypto') - const { mkdir, readFile, writeFile } = await import('node:fs/promises') + const { lstat, mkdir, readFile, writeFile } = await import('node:fs/promises') const { dirname, join } = await import('node:path') - const { useSyntheticCredentialStorage } = await import('../fixtures/electron.mjs') - const source = await buddy.createInstance('backup-export') - const desktop = await source.launch() - await useSyntheticCredentialStorage(desktop) - const connection = { id: randomUUID(), credentialId: randomUUID(), adapter: 'webdav', name: 'Portable connection', endpoint: 'https://example.invalid/backup/', username: 'fixture' } - await mkdir(join(source.home, 'buddy/secrets/sync'), { recursive: true }) - await writeFile(join(source.home, 'device/sync.json'), JSON.stringify({ version: 1, enabled: false, selectedAdapter: 'webdav', activeConnectionId: connection.id, connections: [connection] })) - await writeFile(join(source.home, 'buddy/secrets/sync', `${createHash('sha256').update(connection.credentialId).digest('hex')}.credential`), `offline-fixture:${JSON.stringify({ id: connection.credentialId, value: 'synthetic-key-only', version: 1 })}`) - const snapshot = await desktop.page.evaluate(() => window.lexoraDesktop.storage.snapshot({ scopes: ['connections'], credentials: true, password: 'fixture-password' })) - expect(snapshot).toMatchObject({ scopes: ['connections'], credentials: true }) - const exportParent = join(source.home, 'exported') - await mkdir(exportParent, { recursive: true }) - await desktop.app.evaluate(({ dialog }, selected) => { - dialog.showOpenDialog = async () => ({ canceled: false, filePaths: [selected] }) - }, exportParent) - const exported = await desktop.page.evaluate(id => window.lexoraDesktop.storage.export(id), snapshot.id) - const destination = await buddy.createInstance('backup-import') - const receiving = await destination.launch() - await useSyntheticCredentialStorage(receiving) - const space = await receiving.page.evaluate(() => window.lexoraDesktop.localChat.spaces.create({ name: 'Keep my current space', memoryScope: 'space_only', primaryDirectory: null })) - await receiving.app.evaluate(({ dialog }, selected) => { - dialog.showOpenDialog = async () => ({ canceled: false, filePaths: [selected] }) - }, exported) - expect(await receiving.page.evaluate(() => window.lexoraDesktop.storage.import())).toEqual(snapshot) - const wrong = await receiving.page.evaluate(async (id) => { - try { - await window.lexoraDesktop.storage.restore(id, { scopes: ['connections'], credentials: true, password: 'incorrect-password' }) - } - catch (error) { return String(error) } - }, snapshot.id) - expect(wrong).toContain('SNAPSHOT_PASSWORD_INVALID') - expect((await receiving.page.evaluate(() => window.lexoraDesktop.storage.summary())).pending).toBeNull() - await receiving.page.getByRole('button', { name: '设置', exact: true }).click() - await receiving.page.getByRole('link', { name: '数据存储', exact: true }).click() - await receiving.page.getByRole('button', { name: /^查看备份/ }).click() - await receiving.page.getByRole('button', { name: '恢复备份', exact: true }).click() - await expect(receiving.page.getByRole('checkbox', { name: /^工具与连接配置/ })).toBeChecked() - await expect(receiving.page.getByRole('checkbox', { name: /^任务与空间/ })).toBeDisabled() - await expect(receiving.page.locator('.n-modal')).toHaveCount(1) - await receiving.page.keyboard.press('Escape') - await receiving.page.evaluate(id => window.lexoraDesktop.storage.restore(id, { scopes: ['connections'], credentials: true, password: 'fixture-password' }), snapshot.id) - await destination.stop() - const locator = join(dirname(destination.home), '.lexora-locations', createHash('sha256').update(destination.home).digest('hex').slice(0, 24), 'location.json') + const instance = await buddy.createInstance('upgrade-scope') + const first = await instance.launch({ storageAuthority: 'legacy_jsonl' }) + const editor = first.page.locator('.desktop-workbench-area__tasks .tiptap:visible').first() + await editor.fill('升级前保留的草稿') + await expect.poll(() => first.page.evaluate(async () => JSON.stringify(await window.lexoraDesktop.localChat.composerDrafts.list()))).toContain('升级前保留的草稿') + await instance.stop() + const markers = ['buddy/extensions/inference/model.bin', 'buddy/backups/previous.bin', 'device/chromium/Cache/marker'] + for (const path of markers) { + await mkdir(dirname(join(instance.home, path)), { recursive: true }) + await writeFile(join(instance.home, path), 'independent data') + } + const identities = await Promise.all(markers.map(path => lstat(join(instance.home, path)))) + const locator = join(dirname(instance.home), '.lexora-locations', createHash('sha256').update(instance.home).digest('hex').slice(0, 24), 'location.json') const state = JSON.parse(await readFile(locator, 'utf8')) - state.pending.phase = 'failed' - state.pending.error = 'STORAGE_WORKER_INTERRUPTED' + state.pending = { id: randomUUID(), kind: 'upgrade', source: instance.home, target: instance.home, phase: 'copying', createdAt: new Date().toISOString(), error: null } await writeFile(locator, JSON.stringify(state)) - const migration = await destination.launch({ maintenance: true }) + const migration = await instance.launch({ storageAuthority: 'default', maintenance: true }) + await expect(migration.page.getByRole('heading', { name: '正在升级本地数据' })).toBeVisible() + await expect(migration.page.locator('#detail')).toContainText('上次整理被中断') await migration.app.evaluate(({ app }) => { app.relaunch = () => {} }) + const progress = [] + migration.page.on('console', (message) => { + const text = message.text() + if (text.startsWith('upgrade-progress:')) + progress.push(JSON.parse(text.slice('upgrade-progress:'.length))) + }) + await migration.page.evaluate(() => { + new MutationObserver(() => console.info(`upgrade-progress:${JSON.stringify({ phase: document.body.dataset.phase, detail: document.getElementById('detail').textContent, amount: document.getElementById('amount').textContent })}`)) + .observe(document.body, { subtree: true, childList: true, attributes: true }) + }) + const process = migration.app.process() await migration.page.getByRole('link', { name: '重试', exact: true }).click() - await expect.poll(async () => JSON.parse(await readFile(locator, 'utf8')).pending).toBeNull() - await destination.stop() - const restored = await destination.launch() - await useSyntheticCredentialStorage(restored) - const connections = (await restored.page.evaluate(() => window.lexoraDesktop.sync.state())).connections - expect(connections).toHaveLength(1) - expect(connections[0]).toMatchObject({ name: 'Portable connection' }) - const credentialRestored = await restored.app.evaluate(({ safeStorage }, root) => { - const fs = process.getBuiltinModule('node:fs') - const path = process.getBuiltinModule('node:path') - const crypto = process.getBuiltinModule('node:crypto') - const connection = JSON.parse(fs.readFileSync(path.join(root, 'device/sync.json'), 'utf8')).connections[0] - const file = path.join(root, 'buddy/secrets/sync', `${crypto.createHash('sha256').update(connection.credentialId).digest('hex')}.credential`) - return JSON.parse(safeStorage.decryptString(fs.readFileSync(file))).value === 'synthetic-key-only' - }, destination.home) - expect(credentialRestored).toBe(true) - expect(await restored.page.evaluate(() => window.lexoraDesktop.localChat.spaces.list())).toEqual(expect.arrayContaining([expect.objectContaining({ id: space.id })])) - expect((await restored.page.evaluate(() => window.lexoraDesktop.storage.summary())).root).toBe(destination.home) + await expect.poll(() => process.exitCode, { timeout: 45000 }).toBe(0) + await instance.stop() + await writeFile(testInfo.outputPath('upgrade-progress.json'), JSON.stringify(progress, null, 2)) + expect(progress.some(update => update.phase === 'copying' && /已复制 [1-9]/.test(update.detail))).toBe(true) + expect(progress.some(update => update.phase === 'verifying' && /已校验 [1-9]/.test(update.detail))).toBe(true) + expect(JSON.parse(await readFile(locator, 'utf8')).pending).toBeNull() + for (const [index, path] of markers.entries()) { + expect(await lstat(join(instance.home, path))).toMatchObject({ ino: identities[index].ino, mtimeMs: identities[index].mtimeMs }) + expect(await readFile(join(instance.home, path), 'utf8')).toBe('independent data') + } + const restarted = await instance.launch({ storageAuthority: 'default' }) + await expect(restarted.page.locator('.desktop-workbench-area__tasks .tiptap:visible').first()).toContainText('升级前保留的草稿') + expect(await restarted.page.evaluate(() => window.lexoraDesktop.localChat.privateNetwork.status({}))).toMatchObject({ enabled: false }) + await restarted.page.screenshot({ path: testInfo.outputPath('after-upgrade.png') }) }) diff --git a/.playwright/scripts/__tests__/storageCrash.ts b/.playwright/scripts/__tests__/storageCrash.ts new file mode 100644 index 000000000..0da4249ae --- /dev/null +++ b/.playwright/scripts/__tests__/storageCrash.ts @@ -0,0 +1,400 @@ +import type { ChildProcess } from 'node:child_process' +import type { Readable } from 'node:stream' +import assert from 'node:assert/strict' +import { spawn } from 'node:child_process' +import { createHash, randomUUID } from 'node:crypto' +import { mkdirSync, readFileSync, rmSync, writeFileSync, writeSync } from 'node:fs' +import { join } from 'node:path' +import process from 'node:process' +import { DatabaseSync } from 'node:sqlite' +import { pathToFileURL } from 'node:url' +import { PreparedRunCommandService } from '../../../apps/buddy/service/src/chat/PreparedRunCommandService' +import { recoverSqliteRuns } from '../../../apps/buddy/service/src/runs/recoverSqliteRuns' +import { createAttachmentRepository } from '../../../apps/buddy/service/src/storage/attachmentRepository' +import { createComposerDraftRepository } from '../../../apps/buddy/service/src/storage/composerDraftRepository' +import { openBuddyMigrationCandidate } from '../../../apps/buddy/service/src/storage/database' +import { guardStorageDatabase } from '../../../apps/buddy/service/src/storage/StorageIsolation' +import { verifyCommandReceipt } from '../../../apps/buddy/service/src/storage/verifyCommandReceipt' +import { createBuddyUserContent } from '../../../apps/buddy/shared/conversation/buddyUserContent' + +export const STORAGE_CRASH_POINTS = [ + 'before-begin', + 'after-input', + 'after-draft', + 'after-receipt', + 'before-commit', + 'after-commit', + 'before-ack', + 'partial-ack', + 'after-ack', +] as const +type CrashPoint = typeof STORAGE_CRASH_POINTS[number] +const committedPoints = new Set(['after-commit', 'before-ack', 'partial-ack', 'after-ack']) +const transactionPoints = new Set(['after-input', 'after-draft', 'after-receipt', 'before-commit']) +const now = '2026-09-30T00:00:00.000Z' +const resource = 'Synthetic attachment retained across process interruption.\n' +const resourceHash = createHash('sha256').update(resource).digest('hex') + +export interface StorageCrashFixture { + directory: string + dataGeneration: string +} + +interface Checkpoint { + point: CrashPoint + inTransaction: boolean +} + +function command(fixture: StorageCrashFixture) { + return { dataGeneration: fixture.dataGeneration, actorId: 'desktop-local', commandId: 'command-1', fingerprintVersion: 1, fingerprint: 'fixture-intent-1' } +} + +function input(fixture: StorageCrashFixture) { + return { + attachmentBindings: [{ id: 'file-1', sourceAttachmentId: 'file-1', sourceDraftId: 'draft-1', messageId: 'message-1', storedPath: join(fixture.directory, 'resource.txt'), createdAt: now }], + branchId: 'branch-1', + conversationId: 'conversation-1', + createdAt: now, + approvalPolicy: 'policy' as const, + executionProfile: 'workspace_write' as const, + draft: { draftId: 'draft-1', expectedRevision: 0 }, + model: 'fixture', + provider: 'fixture', + spaceId: null, + runInput: { attachmentIds: ['file-1'], contextItems: [], prompt: 'Materialized fixture input', reasoning: null, serviceTier: null }, + runId: 'run-1', + title: 'Fixture', + userMessageContent: { text: 'Keep original input' }, + userMessageId: 'message-1', + } +} + +export function createStorageCrashFixture(directory: string): StorageCrashFixture { + mkdirSync(directory) + writeFileSync(join(directory, 'resource.txt'), resource) + const database = openBuddyMigrationCandidate({ databasePath: join(directory, 'candidate.sqlite3') }) + try { + guardStorageDatabase(database) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + createComposerDraftRepository(database).open({ + draftId: 'draft-1', + scope: { kind: 'global' }, + initialContent: createBuddyUserContent('Keep original input'), + initialExecutionConfig: { approvalPolicy: 'policy', executionProfile: 'workspace_write' }, + initialModelSelection: { modelId: 'fixture', providerId: 'fixture', reasoning: null, serviceTier: null }, + now, + }) + const attachments = createAttachmentRepository(database) + attachments.persistence?.adopt({ path: join(directory, 'resource.txt'), sizeBytes: resource.length, sha256: resourceHash }) + attachments.create({ + id: 'file-1', + draftId: 'draft-1', + messageId: null, + conversationId: null, + name: 'resource.txt', + mimeType: 'text/plain', + sizeBytes: resource.length, + storedPath: join(directory, 'resource.txt'), + createdAt: now, + }) + const fixture = { directory, dataGeneration: String(database.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation) } + writeFileSync(join(directory, 'fixture.json'), `${JSON.stringify(fixture)}\n`) + return fixture + } + finally { + database.close() + } +} + +export function acceptAtStorageCheckpoint( + fixture: StorageCrashFixture, + point: CrashPoint, + checkpoint: (value: Checkpoint) => void, + respond: (data: string) => void, +): void { + const database = openBuddyMigrationCandidate({ databasePath: join(fixture.directory, 'candidate.sqlite3') }) + guardStorageDatabase(database) + const owner = new PreparedRunCommandService({ database, sourceInstanceId: 'before-restart' }) + const exec = database.exec.bind(database) + const stop = (candidate: CrashPoint) => { + if (candidate === point) + checkpoint({ point, inTransaction: database.isTransaction }) + } + try { + assert.equal(database.prepare('PRAGMA synchronous').get()!.synchronous, 2) + assert.equal(database.prepare('PRAGMA journal_mode').get()!.journal_mode, 'wal') + database.function('fixture_checkpoint', (candidate) => { + stop(candidate as CrashPoint) + return null + }) + database.exec(` + CREATE TEMP TRIGGER fixture_input AFTER INSERT ON run_inputs + BEGIN SELECT fixture_checkpoint('after-input'); END; + CREATE TEMP TRIGGER fixture_draft AFTER UPDATE ON composer_drafts + BEGIN SELECT fixture_checkpoint('after-draft'); END; + CREATE TEMP TRIGGER fixture_receipt AFTER INSERT ON command_receipts + BEGIN SELECT fixture_checkpoint('after-receipt'); END; + `) + database.exec = (sql) => { + if (sql === 'COMMIT') + stop('before-commit') + exec(sql) + if (sql === 'COMMIT') + stop('after-commit') + } + stop('before-begin') + const accepted = owner.start(command(fixture), input(fixture)) + stop('before-ack') + const response = `${JSON.stringify({ commandId: 'command-1', receipt: accepted.receipt })}\n` + if (point === 'partial-ack') { + respond(response.slice(0, Math.floor(response.length / 2))) + stop('partial-ack') + } + else { + respond(response) + stop('after-ack') + } + throw new Error(`Storage checkpoint was not interrupted: ${point}`) + } + finally { + database.exec = exec + owner.dispose() + database.close() + } +} + +export async function verifyStorageCrash(fixture: StorageCrashFixture, point: CrashPoint, response: string) { + const databasePath = join(fixture.directory, 'candidate.sqlite3') + const committed = committedPoints.has(point) + const originalReceipt = verifyCommandReceipt({ databasePath, identity: command(fixture) }) + assert.equal(originalReceipt !== null, committed) + const reader = new DatabaseSync(databasePath, { readOnly: true }) + try { + for (const table of ['conversations', 'conversation_branches', 'messages', 'runs', 'run_inputs', 'turn_requests', 'command_receipts', 'run_events']) + assert.equal(reader.prepare(`SELECT count(*) AS count FROM ${table}`).get()!.count, Number(committed), table) + assert.equal(reader.prepare('SELECT revision FROM composer_drafts').get()!.revision, committed ? 1 : 0) + assert.deepEqual({ ...reader.prepare('SELECT draft_id, message_id FROM attachments').get() }, committed + ? { draft_id: null, message_id: 'message-1' } + : { draft_id: 'draft-1', message_id: null }) + if (committed) { + assert.deepEqual(JSON.parse(JSON.stringify(originalReceipt!.target)), { runId: 'run-1', conversationId: 'conversation-1', branchId: 'branch-1' }) + assert.equal(originalReceipt!.sourceInstanceId, 'before-restart') + assert.equal(originalReceipt!.method, 'chat.startTurn') + assert.deepEqual(JSON.parse(JSON.stringify(originalReceipt!.result)), { draftReceipt: { draftId: 'draft-1', sourceRevision: 0, committedRevision: 1 } }) + assert.deepEqual(JSON.parse(String(reader.prepare('SELECT content_json FROM messages').get()!.content_json)), { text: 'Keep original input' }) + assert.deepEqual({ ...reader.prepare('SELECT prompt, attachment_ids_json FROM run_inputs').get() }, { prompt: 'Materialized fixture input', attachment_ids_json: '["file-1"]' }) + assert.deepEqual({ ...reader.prepare('SELECT id, conversation_id, branch_id, status, next_event_sequence FROM runs').get() }, { + id: 'run-1', + conversation_id: 'conversation-1', + branch_id: 'branch-1', + status: 'queued', + next_event_sequence: 2, + }) + } + assert.equal(reader.prepare('PRAGMA integrity_check').get()!.integrity_check, 'ok') + assert.deepEqual(reader.prepare('PRAGMA foreign_key_check').all(), []) + } + finally { + reader.close() + } + if (point === 'after-ack') { + assert.deepEqual(JSON.parse(response), JSON.parse(JSON.stringify({ commandId: 'command-1', receipt: originalReceipt }))) + } + else if (point === 'partial-ack') { + assert(response.length > 0 && !response.endsWith('\n')) + assert.throws(() => JSON.parse(response)) + } + else { + assert.equal(response, '') + } + + const database = openBuddyMigrationCandidate({ databasePath }) + guardStorageDatabase(database) + const owner = new PreparedRunCommandService({ database, sourceInstanceId: 'after-restart' }) + try { + const accepted = owner.start(command(fixture), input(fixture)) + assert.equal(accepted.created, !committed) + if (committed) + assert.deepEqual(accepted.receipt, originalReceipt) + assert.deepEqual(owner.start(command(fixture), input(fixture)), { created: false, receipt: accepted.receipt }) + const conversationsDirectory = join(fixture.directory, 'conversations') + assert.equal(await recoverSqliteRuns({ database, conversationsDirectory }), 1) + assert.equal(await recoverSqliteRuns({ database, conversationsDirectory }), 0) + assert.deepEqual(owner.find(command(fixture)), accepted.receipt) + assert.equal(owner.start(command(fixture), input(fixture)).created, false) + assert.equal(database.prepare('SELECT count(*) AS count FROM runs').get()!.count, 1) + assert.deepEqual({ ...database.prepare('SELECT status, error_code FROM runs').get() }, { status: 'failed', error_code: 'RUNTIME_RESTARTED' }) + assert.equal(database.prepare('SELECT count(*) AS count FROM command_receipts').get()!.count, 1) + assert.equal(database.prepare('SELECT revision FROM composer_drafts').get()!.revision, 1) + assert.equal(createHash('sha256').update(readFileSync(join(fixture.directory, 'resource.txt'))).digest('hex'), resourceHash) + return { point, committedBeforeRestart: committed, receipt: accepted.receipt, recoveredRuns: 1, finalRuns: 1, resourceHash } + } + finally { + owner.dispose() + database.close() + } +} + +export function runStorageCrashChild(directory: string, point: CrashPoint, nonce: string): void { + assert(STORAGE_CRASH_POINTS.includes(point)) + const fixture = JSON.parse(readFileSync(join(directory, 'fixture.json'), 'utf8')) as StorageCrashFixture + assert.equal(fixture.directory, directory) + acceptAtStorageCheckpoint(fixture, point, (value) => { + writeSync(3, `${JSON.stringify({ ...value, nonce })}\n`) + Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 30000) + throw new Error('Storage crash parent did not terminate the paused child') + }, data => writeSync(1, data)) +} + +interface ChildResult { + stdout: string + stderr: string + code: number | null + signal: NodeJS.Signals | null + error: { message: string, code?: string } | null + checkpoint: Checkpoint | null +} + +const children = new Set() + +function runChild(args: string[], expected?: { point: CrashPoint, nonce: string }): Promise { + return new Promise((resolve) => { + const result: ChildResult = { stdout: '', stderr: '', code: null, signal: null, error: null, checkpoint: null } + const child = spawn(process.execPath, args, { stdio: ['ignore', 'pipe', 'pipe', 'pipe'] }) + children.add(child) + const fail = (error: unknown) => { + result.error ??= { message: String(error), ...((error as NodeJS.ErrnoException)?.code ? { code: (error as NodeJS.ErrnoException).code } : {}) } + child.kill('SIGKILL') + } + const timeout = setTimeout(() => fail(new Error('Storage child timed out before verified exit')), 10000) + for (const [name, stream] of [['stdout', child.stdout], ['stderr', child.stderr]] as const) { + stream!.setEncoding('utf8') + stream!.on('data', (chunk: string) => { + if (result[name].length + chunk.length > 65536) { + fail(new Error(`Storage child exceeded ${name} limit`)) + return + } + result[name] += chunk + }) + } + let control = '' + const channel = child.stdio[3] as Readable + channel.setEncoding('utf8') + channel.on('data', (chunk: string) => { + try { + assert(expected, 'Unexpected storage child checkpoint') + assert(control.length + chunk.length <= 4096, 'Storage child checkpoint exceeded limit') + control += chunk + if (!control.endsWith('\n')) + return + const message = JSON.parse(control) as Checkpoint & { nonce: string } + assert.deepEqual(message, { ...expected, inTransaction: transactionPoints.has(expected.point) }) + assert.equal(result.checkpoint, null, 'Repeated storage child checkpoint') + result.checkpoint = { point: message.point, inTransaction: message.inTransaction } + assert(child.kill('SIGKILL'), 'Storage child could not be terminated') + } + catch (error) { + fail(error) + } + }) + child.on('error', fail) + child.once('close', (code, signal) => { + clearTimeout(timeout) + children.delete(child) + result.code = code + result.signal = signal + resolve(result) + }) + }) +} + +export async function runStorageCrashes(root: string, bundle: string, bundleSha256: string): Promise { + const cases: Array> & { child: ChildResult }> = [] + const report: { + startedAt: string + node: string + platform: string + arch: string + bundleSha256: string + status: 'running' | 'blocked' | 'failed' | 'passed' + blockedReason?: string + retainedFixture?: string + preflight?: ChildResult + cases: typeof cases + error?: string + } = { startedAt: new Date().toISOString(), node: process.version, platform: process.platform, arch: process.arch, bundleSha256, status: 'running', cases } + const stopChildren = () => { + for (const child of children) + child.kill('SIGKILL') + } + let interrupted = false + const interrupt = () => { + interrupted = true + stopChildren() + } + process.once('SIGINT', interrupt) + process.once('SIGTERM', interrupt) + try { + const token = randomUUID() + report.preflight = await runChild(['--input-type=module', '-e', 'process.stdout.write(process.argv[1])', token]) + assert(!interrupted, 'Storage process interruption acceptance was cancelled') + if (report.preflight.error || report.preflight.code !== 0 || report.preflight.stdout !== token || process.platform === 'win32') { + report.status = 'blocked' + report.blockedReason = process.platform === 'win32' + ? 'SIGKILL acceptance requires a POSIX host' + : report.preflight.error + ? `Child process error: ${report.preflight.error.message}` + : report.preflight.code !== 0 + ? `Child process exited with code ${report.preflight.code}` + : 'Child process exited without the required nonce response' + process.exitCode = 1 + return + } + for (const point of STORAGE_CRASH_POINTS) { + const directory = join(root, point) + let verified = false + report.retainedFixture = directory + try { + const fixture = createStorageCrashFixture(directory) + const nonce = randomUUID() + const child = await runChild([ + '--input-type=module', + '-e', + 'const { runStorageCrashChild } = await import(process.argv[1]); runStorageCrashChild(process.argv[2], process.argv[3], process.argv[4]);', + pathToFileURL(bundle).href, + directory, + point, + nonce, + ], { point, nonce }) + writeFileSync(join(root, `${point}.child.json`), `${JSON.stringify(child, null, 2)}\n`) + assert(!interrupted, 'Storage process interruption acceptance was cancelled') + assert.equal(child.error, null, JSON.stringify(child)) + assert.equal(child.signal, 'SIGKILL', JSON.stringify(child)) + assert.equal(child.checkpoint?.point, point, JSON.stringify(child)) + cases.push({ ...await verifyStorageCrash(fixture, point, child.stdout), child }) + assert(!interrupted, 'Storage process interruption acceptance was cancelled') + verified = true + process.stdout.write(`Storage process interruption verified: ${point}\n`) + } + finally { + if (verified) { + rmSync(directory, { recursive: true, force: true }) + delete report.retainedFixture + } + } + } + report.status = 'passed' + } + catch (error) { + report.status = 'failed' + report.error = String(error) + throw error + } + finally { + stopChildren() + process.removeListener('SIGINT', interrupt) + process.removeListener('SIGTERM', interrupt) + writeFileSync(join(root, 'report.json'), `${JSON.stringify(report, null, 2)}\n`) + process.stdout.write(`Storage process interruption status: ${report.status}; verified ${cases.length}/${STORAGE_CRASH_POINTS.length}\n`) + } +} diff --git a/.playwright/scripts/__tests__/storageDurability.ts b/.playwright/scripts/__tests__/storageDurability.ts new file mode 100644 index 000000000..bd9bc93ae --- /dev/null +++ b/.playwright/scripts/__tests__/storageDurability.ts @@ -0,0 +1,244 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { AppendBuddyRunEventInput } from '../../../apps/buddy/service/src/events/BuddyRunEvent' +import assert from 'node:assert/strict' +import { existsSync, mkdirSync, rmSync, statSync, writeFileSync } from 'node:fs' +import { join } from 'node:path' +import { monitorEventLoopDelay, performance } from 'node:perf_hooks' +import process from 'node:process' +import { setTimeout as delay, setImmediate as yieldLoop } from 'node:timers/promises' +import { BufferedRunEventWriter } from '../../../apps/buddy/service/src/agent/events/BufferedRunEventWriter' +import { SqliteRunEventLog } from '../../../apps/buddy/service/src/events/SqliteRunEventLog' +import { checkpointSqliteStorage } from '../../../apps/buddy/service/src/storage/checkpointSqliteStorage' +import { CommandCommitter } from '../../../apps/buddy/service/src/storage/CommandCommitter' +import { createComposerDraftRepository } from '../../../apps/buddy/service/src/storage/composerDraftRepository' +import { createConversationRepository } from '../../../apps/buddy/service/src/storage/conversationRepository' +import { openBuddyMigrationCandidate } from '../../../apps/buddy/service/src/storage/database' +import { guardStorageDatabase } from '../../../apps/buddy/service/src/storage/StorageIsolation' +import { withTransaction } from '../../../apps/buddy/service/src/storage/transaction' +import { createBuddyUserContent } from '../../../apps/buddy/shared/conversation/buddyUserContent' +import { seedSqliteRun } from './storageFixture' + +const time = '2026-09-29T00:00:00.000Z' +const executionConfig = { approvalPolicy: 'policy', executionProfile: 'workspace_write' } as const +const modes = ['full-1', 'full-20', 'buffered'] as const +type Mode = typeof modes[number] + +function distribution(values: number[]) { + assert(values.length > 0) + const sorted = values.toSorted((a, b) => a - b) + return { + count: sorted.length, + p95: sorted[Math.ceil(sorted.length * 0.95) - 1]!, + p99: sorted[Math.ceil(sorted.length * 0.99) - 1]!, + max: sorted.at(-1)!, + } +} + +function seed(database: DatabaseSync) { + withTransaction(database, () => { + const insert = database.prepare(`INSERT INTO messages (id, conversation_id, branch_id, run_id, role, content_json, created_at) + VALUES (?, 'task-1', 'branch-task-1', ?, 'assistant', ?, ?)`) + for (let run = 0; run < 1000; run++) { + seedSqliteRun(database, `history-${run}`) + for (let message = 0; message < 9; message++) { + insert.run(`history-${run}-${message}`, `history-${run}`, JSON.stringify({ text: 'x'.repeat(256) }), new Date(Date.parse(time) + run * 9 + message).toISOString()) + } + } + database.exec('UPDATE runs SET status = \'completed\'') + seedSqliteRun(database, 'stream-run') + }) + const drafts = createComposerDraftRepository(database) + drafts.open({ + draftId: 'draft', + scope: { kind: 'global' }, + initialContent: createBuddyUserContent('Draft'), + initialExecutionConfig: executionConfig, + initialModelSelection: null, + now: time, + }) + return { drafts, commands: new CommandCommitter(database), conversations: createConversationRepository(database) } +} + +function storageSize(databasePath: string) { + return { + database: statSync(databasePath).size, + wal: existsSync(`${databasePath}-wal`) ? statSync(`${databasePath}-wal`).size : 0, + } +} + +async function sample(root: string, mode: Mode, repetition: number) { + const directory = join(root, `${mode}-${repetition}`) + mkdirSync(directory) + const databasePath = join(directory, 'candidate.sqlite3') + const database = openBuddyMigrationCandidate({ databasePath }) + guardStorageDatabase(database) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + const log = new SqliteRunEventLog({ database }) + const histogram = monitorEventLoopDelay({ resolution: 1 }) + try { + assert.deepEqual({ ...database.prepare('PRAGMA synchronous').get() }, { synchronous: 2 }) + assert.deepEqual({ ...database.prepare('PRAGMA journal_mode').get() }, { journal_mode: 'wal' }) + const { drafts, commands, conversations } = seed(database) + const beforeSize = storageSize(databasePath) + const dataGeneration = String(database.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation) + const commitTimes: number[] = [] + const appendBatch = async (events: readonly AppendBuddyRunEventInput[]) => { + const started = performance.now() + const result = await log.appendBatch(events) + commitTimes.push(performance.now() - started) + return result + } + const buffered = new BufferedRunEventWriter({ appendBatch }, 'stream-run', () => time) + const batchSize = mode === 'full-1' ? 1 : 20 + histogram.enable() + await delay(10) + const cpuBefore = process.cpuUsage() + const started = performance.now() + let peakRss = process.memoryUsage().rss + for (let index = 0; index < 4000; index += batchSize) { + const events = Array.from({ length: batchSize }, () => ({ + runId: 'stream-run', + type: 'message.delta' as const, + payload: { messageId: 'stream', delta: 's'.repeat(128) }, + createdAt: time, + })) + if (mode === 'buffered') + buffered.appendBatch(events) + else + await appendBatch(events) + peakRss = Math.max(peakRss, process.memoryUsage().rss) + await yieldLoop() + } + await buffered.drain() + const elapsedMs = performance.now() - started + const cpu = process.cpuUsage(cpuBefore) + await delay(10) + histogram.disable() + const eventLoopMs = { p95: histogram.percentile(95) / 1e6, p99: histogram.percentile(99) / 1e6, max: histogram.max / 1e6 } + const facts = await log.read('stream-run') + assert.equal(facts.map(event => (event.payload as { delta: string }).delta).join(''), 's'.repeat(4000 * 128)) + const queries: Record = { legacyOffset: [], messageTail: [], messageOld: [], eventOld: [] } + const reads = { + legacyOffset: () => database.prepare('SELECT * FROM messages WHERE conversation_id = \'task-1\' AND branch_id = \'branch-task-1\' ORDER BY created_at DESC LIMIT 50 OFFSET 9000').all(), + messageTail: () => conversations.listMessagePage('task-1', 'branch-task-1', { limit: 50 }).items, + messageOld: () => conversations.listMessagePage('task-1', 'branch-task-1', { limit: 50, beforeMessageId: 'history-100-0' }).items, + eventOld: () => log.list('stream-run', { limit: 50, afterSequence: 0 }), + } + for (let index = 0; index < 200; index++) { + for (const [name, read] of Object.entries(reads)) { + const begin = performance.now() + const rows = await read() + queries[name]!.push(performance.now() - begin) + assert.equal(rows.length, 50) + } + await yieldLoop() + } + const terminalStart = performance.now() + await log.appendBatch([ + { runId: 'stream-run', type: 'message.completed', payload: { + messageId: 'stream', + role: 'assistant', + content: { text: 's'.repeat(4000 * 128) }, + stopReason: 'completed', + }, createdAt: time }, + { runId: 'stream-run', type: 'run.completed', payload: null, createdAt: time }, + ]) + const terminalMs = performance.now() - terminalStart + const compactStart = performance.now() + const removedEvents = await log.compactTerminalRun('stream-run') + const compactionMs = performance.now() - compactStart + assert.equal(removedEvents, facts.length) + assert.equal((await log.read('stream-run')).length, 2) + assert.equal(database.prepare('SELECT next_event_sequence FROM runs WHERE id = \'stream-run\'').get()!.next_event_sequence, facts.length + 3) + const draftTimes: number[] = [] + histogram.reset() + histogram.enable() + await delay(10) + for (let revision = 0; revision < 1000; revision++) { + const begin = performance.now() + const result = commands.accept({ + dataGeneration, + actorId: 'desktop-local', + commandId: `save-${revision}`, + method: 'draft.save', + fingerprintVersion: 1, + fingerprint: `draft-${revision}`, + sourceInstanceId: 'cost-fixture', + }, () => { + const draft = drafts.save({ draftId: 'draft', expectedRevision: revision, content: createBuddyUserContent('d'.repeat(512)), executionConfig, modelSelection: null, now: time }) + return { target: { draftId: draft.draftId }, result: { revision: draft.revision } } + }) + draftTimes.push(performance.now() - begin) + assert.equal(result.receipt.result.revision, revision + 1) + await yieldLoop() + } + await delay(10) + histogram.disable() + const draftEventLoopMs = { p99: histogram.percentile(99) / 1e6, max: histogram.max / 1e6 } + assert.equal(drafts.findById('draft')?.revision, 1000) + assert.equal(database.prepare('SELECT count(*) AS count FROM command_receipts').get()!.count, 1000) + const beforeCheckpoint = storageSize(databasePath) + const checkpointStart = performance.now() + const checkpoint = { ...checkpointSqliteStorage(database), elapsedMs: performance.now() - checkpointStart } + const afterCheckpoint = storageSize(databasePath) + const result = { + mode, + repetition, + historyRuns: 1000, + historyMessages: 10000, + inputEvents: 4000, + inputCharacters: 128, + persistedEvents: facts.length, + elapsedMs, + cpu, + peakRss, + commitMs: distribution(commitTimes), + draftCommitMs: distribution(draftTimes), + eventLoopMs, + draftEventLoopMs, + queriesMs: Object.fromEntries(Object.entries(queries).map(([name, values]) => [name, distribution(values)])), + beforeSize, + beforeCheckpoint, + afterCheckpoint, + checkpoint, + terminalMs, + compactionMs, + removedEvents, + sqlite: database.prepare('SELECT sqlite_version() AS version').get()!.version, + } + writeFileSync(join(root, `${mode}-${repetition}.json`), `${JSON.stringify(result, null, 2)}\n`) + assert.equal(checkpoint.busy, 0) + assert.equal(afterCheckpoint.wal, 0) + for (const p99 of [result.commitMs.p99, result.draftCommitMs.p99, eventLoopMs.p99, draftEventLoopMs.p99, ...Object.values(result.queriesMs).map(query => query.p99)]) + assert(p99 <= 25, `Storage cost P99 ${p99.toFixed(2)}ms exceeds 25ms`) + return result + } + finally { + histogram.disable() + try { + await log.close() + } + finally { + database.close() + rmSync(directory, { recursive: true }) + } + } +} + +export async function runStorageDurability(root: string, bundleSha256: string) { + const samples: Awaited>[] = [] + const report = { startedAt: new Date().toISOString(), platform: process.platform, arch: process.arch, node: process.version, bundleSha256, samples, passed: false } + try { + for (let repetition = 1; repetition <= 3; repetition++) { + for (const mode of modes) { + const result = await sample(root, mode, repetition) + samples.push(result) + process.stdout.write(`${mode} ${repetition}/3: commit P99 ${result.commitMs.p99.toFixed(2)}ms, loop P99 ${result.eventLoopMs.p99.toFixed(2)}ms, old page P99 ${result.queriesMs.messageOld!.p99.toFixed(2)}ms\n`) + } + } + report.passed = true + } + finally { + writeFileSync(join(root, 'report.json'), `${JSON.stringify(report, null, 2)}\n`) + } +} diff --git a/.playwright/scripts/__tests__/storageFixture.ts b/.playwright/scripts/__tests__/storageFixture.ts new file mode 100644 index 000000000..bd543cbc7 --- /dev/null +++ b/.playwright/scripts/__tests__/storageFixture.ts @@ -0,0 +1,15 @@ +import type { DatabaseSync } from 'node:sqlite' + +export const sqliteRunFixtureTime = '2026-09-29T00:00:00.000Z' + +export function seedSqliteRun(database: DatabaseSync, runId: string, options: { conversationId?: string, nextSequence?: number | null } = {}) { + const conversationId = options.conversationId ?? 'task-1' + const branchId = `branch-${conversationId}` + database.prepare('INSERT OR IGNORE INTO conversations (id, created_at, updated_at) VALUES (?, ?, ?)').run(conversationId, sqliteRunFixtureTime, sqliteRunFixtureTime) + database.prepare('INSERT OR IGNORE INTO conversation_branches (id, conversation_id, created_at) VALUES (?, ?, ?)').run(branchId, conversationId, sqliteRunFixtureTime) + database.prepare('UPDATE conversations SET active_branch_id = ? WHERE id = ?').run(branchId, conversationId) + database.prepare(`INSERT INTO messages (id, conversation_id, branch_id, role, content_json, created_at) + VALUES (?, ?, ?, 'user', ?, ?)`).run(`input-${runId}`, conversationId, branchId, '{"text":"Fixture input"}', sqliteRunFixtureTime) + database.prepare(`INSERT INTO runs (id, conversation_id, branch_id, triggering_message_id, provider, model, purpose, status, started_at, next_event_sequence) + VALUES (?, ?, ?, ?, 'fixture', 'fixture', 'chat', 'queued', ?, ?)`).run(runId, conversationId, branchId, `input-${runId}`, sqliteRunFixtureTime, options.nextSequence === undefined ? 1 : options.nextSequence) +} diff --git a/.playwright/scripts/__tests__/task-archive-profile.ts b/.playwright/scripts/__tests__/task-archive-profile.ts index 50b88a820..53ea60bb4 100644 --- a/.playwright/scripts/__tests__/task-archive-profile.ts +++ b/.playwright/scripts/__tests__/task-archive-profile.ts @@ -1 +1 @@ -export { openBuddyDatabase } from '../../../apps/buddy/service/src/storage/database' +export { openBuddyDatabase, openBuddySqliteDatabase } from '../../../apps/buddy/service/src/storage/database' diff --git a/.playwright/scripts/__tests__/task-archive.e2e.mjs b/.playwright/scripts/__tests__/task-archive.e2e.mjs index e31853aa4..607819682 100644 --- a/.playwright/scripts/__tests__/task-archive.e2e.mjs +++ b/.playwright/scripts/__tests__/task-archive.e2e.mjs @@ -2,7 +2,7 @@ import fs from 'node:fs/promises' import path from 'node:path' import process from 'node:process' import { expect, test } from '../fixtures/electron.mjs' -import { prepareTaskArchiveProfile, readTaskArchiveStorage, taskExists } from '../fixtures/task-archive.mjs' +import { prepareTaskArchiveProfile, taskExists } from '../fixtures/task-archive.mjs' const pluginId = 'p95e8d9a8d120433d9ba2058e1c844ed0.legacy-task-cleanup' @@ -22,7 +22,6 @@ async function archiveTask(page, id) { async function openManager(page) { await page.locator('.desktop-app-sidebar').getByRole('button', { name: '设置', exact: true }).click() const link = page.locator('.desktop-settings-sidebar').getByRole('link', { name: '任务归档', exact: true }) - await expect(link.locator('..').getByRole('heading', { name: '个人', exact: true })).toBeVisible() await link.click() await expect(page.locator('.desktop-settings-page__title')).toHaveText('任务归档') const manager = page.getByTestId('task-archive-manager') @@ -30,12 +29,8 @@ async function openManager(page) { return manager } -async function expectStorage(app, databasePath) { - expect(await readTaskArchiveStorage(app, databasePath)).toEqual({ version: 42, tables: [] }) -} - -async function launchArchiveInstance(instance) { - const session = await instance.launch() +async function launchArchiveInstance(instance, authority) { + const session = await instance.launch({ storageAuthority: authority }) await expect.poll(() => session.page.evaluate(() => window.lexoraDesktop.localChat.runtime.getStatus())).toMatchObject({ status: 'ready' }) return session } @@ -46,272 +41,224 @@ async function capture(page, testInfo, name) { await testInfo.attach(name, { path: screenshot, contentType: 'image/png' }) } -test('archive manager preserves read-only history, explicit restoration and truthful cleanup results', async ({ buddy }, testInfo) => { - test.setTimeout(180000) - const instance = await buddy.createInstance('task-archive-legacy') - const fixture = await prepareTaskArchiveProfile(instance.home, testInfo.outputPath('profile-preparation')) - const { app, page } = await launchArchiveInstance(instance) - await expectStorage(app, fixture.databasePath) - const row = page.locator('[data-task-id="archive-active"]') - await expect(row).toBeVisible() - await row.locator('.desktop-task-sidebar__task').click() - await expect(page.locator('.desktop-chat-page:visible')).toBeVisible() - await archiveTask(page, 'archive-active') - await page.getByRole('button', { name: '撤销归档', exact: true }).click() - await expect(row).toBeVisible() - await archiveTask(page, 'archive-active') - let manager = await openManager(page) - const archived = manager.locator('[data-archive-id="archive-active"]') - await expect(archived).toBeVisible() - await archived.getByRole('button', { name: '可归档的历史任务', exact: true }).click() - await expect(manager).toBeHidden() - await expect(page.getByText('此任务已归档,仅可阅读历史。取消归档不会自动恢复执行或权限。')).toBeVisible() - await expect(page.locator('.desktop-chat-page:visible .tiptap:visible')).toHaveCount(0) - await expect(page.locator('.desktop-chat-page:visible')).toContainText('保留的历史内容 archive-active') - await expect(row).toBeHidden() - expect(await page.locator('.n-message--error-type').allTextContents()).toEqual([]) - await capture(page, testInfo, 'legacy-archived-read-only') - await page.getByRole('button', { name: '取消归档', exact: true }).click() - await expect(row).toBeVisible() - await expect(page.locator('.desktop-chat-page:visible .tiptap:visible')).toBeVisible() - expect(await page.evaluate(() => window.lexoraDesktop.localChat.conversations.get('archive-active'))).toMatchObject({ archivedAt: null }) - manager = await openManager(page) - const select = async (label) => { - await manager.locator('#archive-cleanup-policy .n-base-selection').click() - const menu = page.locator('.n-base-select-menu:visible') - await expect(menu.locator('.n-base-select-option')).toHaveText(['不清理', '3 天', '7 天', '30 天']) - await menu.getByText(label, { exact: true }).click() - } - await expect(manager.locator('#archive-cleanup-policy')).toContainText('不清理') - await select('3 天') - let risk = page.getByTestId('task-archive-policy-confirm') - await expect(risk).toContainText('不可恢复') - await expect(risk).toContainText('旧版已移除的任务不自动清理') - await risk.getByRole('button', { name: '取消', exact: true }).click() - expect(await page.evaluate(() => window.lexoraDesktop.localChat.conversations.getArchivePolicy())).toEqual({ days: 0 }) - await select('7 天') - risk = page.getByTestId('task-archive-policy-confirm') - await risk.getByRole('button', { name: '保存', exact: true }).click() - await expect.poll(() => page.evaluate(() => window.lexoraDesktop.localChat.conversations.getArchivePolicy())).toEqual({ days: 7 }) - await select('3 天') - await expect(risk).toContainText('归档满 3 天') - await risk.getByRole('button', { name: '取消', exact: true }).click() - await select('30 天') - await expect.poll(() => page.evaluate(() => window.lexoraDesktop.localChat.conversations.getArchivePolicy())).toEqual({ days: 30 }) - await select('不清理') - await expect.poll(() => page.evaluate(() => window.lexoraDesktop.localChat.conversations.getArchivePolicy())).toEqual({ days: 0 }) - const failed = manager.locator('[data-archive-id="archive-failed"]') - await failed.getByRole('button', { name: '清理失败', exact: true }).click() - await expect(page.getByText('FILE_ACCESS_DENIED', { exact: true })).toBeVisible() - await failed.getByRole('button', { name: '清理失败', exact: true }).click() - await expect(failed).toContainText('清理失败') - await failed.getByRole('button', { name: '删除', exact: true }).click() - const deletion = page.getByTestId('task-archive-delete-confirm') - await expect(deletion).toContainText('永久删除') - await expect(deletion).toContainText('任务独占的 workspace 和附件副本') - await expect(deletion).toContainText('外部目录及其中的文件不受影响') - await deletion.getByRole('button', { name: '删除', exact: true }).click() - await expect(deletion).toBeHidden() - await expect.poll(() => taskExists(app, fixture.databasePath, 'archive-failed')).toBe(false) - await expect(failed).toHaveCount(0) - expect(await taskExists(app, fixture.databasePath, 'archive-legacy')).toBe(true) - await expectStorage(app, fixture.databasePath) - await capture(page, testInfo, 'legacy-archive-cleanup-result') -}) +for (const authority of ['legacy_jsonl', 'sqlite']) { + test.describe(authority, () => { + test('archived history stays read-only until explicit restoration', async ({ buddy }, testInfo) => { + test.setTimeout(180000) + const instance = await buddy.createInstance(`task-archive-legacy-${authority}`) + await prepareTaskArchiveProfile(instance.home, testInfo.outputPath('profile-preparation'), authority) + const { page } = await launchArchiveInstance(instance, authority) + const row = page.locator('[data-task-id="archive-active"]') + await expect(row).toBeVisible() + await row.locator('.desktop-task-sidebar__task').click() + await expect(page.locator('.desktop-chat-page:visible')).toBeVisible() + await archiveTask(page, 'archive-active') + await page.getByRole('button', { name: '撤销归档', exact: true }).click() + await expect(row).toBeVisible() + await archiveTask(page, 'archive-active') + const manager = await openManager(page) + const archived = manager.locator('[data-archive-id="archive-active"]') + await expect(archived).toBeVisible() + await archived.getByRole('button', { name: '可归档的历史任务', exact: true }).click() + await expect(manager).toBeHidden() + await expect(page.getByText('此任务已归档,仅可阅读历史。取消归档不会自动恢复执行或权限。')).toBeVisible() + await expect(page.locator('.desktop-chat-page:visible .tiptap:visible')).toHaveCount(0) + await expect(page.locator('.desktop-chat-page:visible')).toContainText('保留的历史内容 archive-active') + await expect(row).toBeHidden() + await page.getByRole('button', { name: '取消归档', exact: true }).click() + await expect(row).toBeVisible() + await expect(page.locator('.desktop-chat-page:visible .tiptap:visible')).toBeVisible() + expect(await page.evaluate(() => window.lexoraDesktop.localChat.conversations.get('archive-active'))).toMatchObject({ archivedAt: null }) + }) -test('active task deletion requires confirmation and permanently removes only task-owned data', async ({ buddy }, testInfo) => { - test.setTimeout(180000) - const instance = await buddy.createInstance('active-task-delete') - const fixture = await prepareTaskArchiveProfile(instance.home, testInfo.outputPath('profile-preparation')) - const { app, page } = await launchArchiveInstance(instance) - await expectStorage(app, fixture.databasePath) - const row = page.locator('[data-task-id="archive-active"]') - await expect(row).toBeVisible() - const before = await page.evaluate(() => window.lexoraDesktop.localChat.conversations.get('archive-active')) - expect(before).toMatchObject({ archivedAt: null, deletedAt: null }) - expect(Number.isSafeInteger(before.metadataRevision)).toBe(true) - const openDeletion = async () => { - await row.click({ button: 'right' }) - await page.locator('.n-dropdown-menu:visible').getByText('删除', { exact: true }).click() - const confirmation = page.getByRole('dialog').filter({ hasText: '删除任务?' }) - await expect(confirmation).toContainText('将永久删除“可归档的历史任务”') - await expect(confirmation).toContainText('不可恢复') - await expect(confirmation).toContainText('外部目录及其中的文件不受影响') - return confirmation - } - let confirmation = await openDeletion() - await confirmation.getByRole('button', { name: '取消', exact: true }).click() - await expect(confirmation).toBeHidden() - await expect(row).toBeVisible() - expect(await taskExists(app, fixture.databasePath, 'archive-active')).toBe(true) - expect(await page.evaluate(() => window.lexoraDesktop.localChat.conversations.get('archive-active'))).toMatchObject({ deletedAt: null, metadataRevision: before.metadataRevision }) - expect(await fs.readFile(path.join(fixture.activeOwned, 'owned.txt'), 'utf8')).toBe('Task-owned fixture') - confirmation = await openDeletion() - await capture(page, testInfo, 'active-task-delete-confirmation') - await confirmation.getByRole('button', { name: '删除', exact: true }).click() - await expect(confirmation).toBeHidden() - await expect(row).toBeHidden() - await expect.poll(() => taskExists(app, fixture.databasePath, 'archive-active')).toBe(false) - await expect.poll(() => fs.access(fixture.activeOwned).then(() => true, () => false)).toBe(false) - expect(await fs.readFile(path.join(fixture.external, 'keep.txt'), 'utf8')).toBe('External fixture must survive') - expect(await taskExists(app, fixture.databasePath, 'archive-existing')).toBe(true) - expect(await taskExists(app, fixture.databasePath, 'archive-legacy')).toBe(true) - await expectStorage(app, fixture.databasePath) - await capture(page, testInfo, 'active-task-deleted') -}) + test('active task deletion requires confirmation and permanently removes only task-owned data', async ({ buddy }, testInfo) => { + test.setTimeout(180000) + const instance = await buddy.createInstance(`active-task-delete-${authority}`) + const fixture = await prepareTaskArchiveProfile(instance.home, testInfo.outputPath('profile-preparation'), authority) + const { app, page } = await launchArchiveInstance(instance, authority) + const row = page.locator('[data-task-id="archive-active"]') + await expect(row).toBeVisible() + const before = await page.evaluate(() => window.lexoraDesktop.localChat.conversations.get('archive-active')) + expect(before).toMatchObject({ archivedAt: null, deletedAt: null }) + const openDeletion = async () => { + await row.click({ button: 'right' }) + await page.locator('.n-dropdown-menu:visible').getByText('删除', { exact: true }).click() + const confirmation = page.getByRole('dialog').filter({ hasText: '删除任务?' }) + await expect(confirmation).toContainText('将永久删除“可归档的历史任务”') + await expect(confirmation).toContainText('不可恢复') + await expect(confirmation).toContainText('外部目录及其中的文件不受影响') + return confirmation + } + let confirmation = await openDeletion() + await confirmation.getByRole('button', { name: '取消', exact: true }).click() + await expect(confirmation).toBeHidden() + await expect(row).toBeVisible() + expect(await taskExists(app, fixture.databasePath, 'archive-active')).toBe(true) + expect(await page.evaluate(() => window.lexoraDesktop.localChat.conversations.get('archive-active'))).toMatchObject({ deletedAt: null, metadataRevision: before.metadataRevision }) + expect(await fs.readFile(path.join(fixture.activeOwned, 'owned.txt'), 'utf8')).toBe('Task-owned fixture') + confirmation = await openDeletion() + await confirmation.getByRole('button', { name: '删除', exact: true }).click() + await expect(confirmation).toBeHidden() + await expect(row).toBeHidden() + await expect.poll(() => taskExists(app, fixture.databasePath, 'archive-active')).toBe(false) + await expect.poll(() => fs.access(fixture.activeOwned).then(() => true, () => false)).toBe(false) + expect(await fs.readFile(path.join(fixture.external, 'keep.txt'), 'utf8')).toBe('External fixture must survive') + expect(await taskExists(app, fixture.databasePath, 'archive-existing')).toBe(true) + expect(await taskExists(app, fixture.databasePath, 'archive-legacy')).toBe(true) + }) -test('official legacy cleanup plugin owns confirmation and preserves external files', async ({ buddy }, testInfo) => { - test.skip(!process.env.LEXORA_TEST_LEGACY_CLEANUP_ARCHIVE, 'Provide the separately built legacy cleanup plugin archive') - test.setTimeout(180000) - const instance = await buddy.createInstance('legacy-task-cleanup') - const fixture = await prepareTaskArchiveProfile(instance.home, testInfo.outputPath('profile-preparation'), 'legacy_jsonl', { legacyTasks: 32, legacyFiles: 64 }) - const { app, page } = await launchArchiveInstance(instance) - await expectStorage(app, fixture.databasePath) - await page.locator('.desktop-app-sidebar').getByRole('button', { name: '插件', exact: true }).click() - await app.evaluate(({ dialog }, archive) => { - dialog.showOpenDialog = async () => ({ canceled: false, filePaths: [archive] }) - }, process.env.LEXORA_TEST_LEGACY_CLEANUP_ARCHIVE) - await page.getByTestId('extension-install-options').click() - await page.getByTestId('extension-install').click() - const review = page.getByTestId('extension-install-review') - await expect(review).toBeVisible() - await expect(review.locator('[data-permission="legacyTaskCleanup"]')).toContainText('不允许删除活动任务或任意文件') - await review.getByTestId('extension-confirm-install').click() - await expect(review).toBeHidden() - await expect.poll(() => page.evaluate(async id => (await window.lexoraDesktop.extensions.list()).find(item => item.manifest.id === id)?.enabled, pluginId)).toBe(true) - const card = page.locator(`article[data-extension-id="${pluginId}"]`) - await expect(card.getByRole('button', { name: '打开', exact: true })).toHaveCount(0) - await expect(page.locator('.desktop-app-sidebar').getByRole('button', { name: '旧版任务清理', exact: true })).toHaveCount(0) - const runCleanup = async () => { - await card.getByTestId('extension-card-more').click() - await page.getByTestId('extension-actions').hover() - const action = page.locator(`[data-extension-action="${pluginId}.action"]`) - await expect(action).toHaveText('清理任务残留…') - await action.click() - } - const pluginFrame = async () => { - let frame - await expect.poll(async () => { - for (const candidate of page.frames()) { - if (await candidate.getByTestId('legacy-task-cleanup-panel').count().catch(() => 0)) { - frame = candidate - return true - } + test('official legacy cleanup plugin owns confirmation and preserves external files', async ({ buddy }, testInfo) => { + test.skip(!process.env.LEXORA_TEST_LEGACY_CLEANUP_ARCHIVE, 'Provide the separately built legacy cleanup plugin archive') + test.setTimeout(180000) + const instance = await buddy.createInstance(`legacy-task-cleanup-${authority}`) + const fixture = await prepareTaskArchiveProfile(instance.home, testInfo.outputPath('profile-preparation'), authority, { legacyTasks: 32, legacyFiles: 64 }) + const { app, page } = await launchArchiveInstance(instance, authority) + await page.locator('.desktop-app-sidebar').getByRole('button', { name: '插件', exact: true }).click() + await app.evaluate(({ dialog }, archive) => { + dialog.showOpenDialog = async () => ({ canceled: false, filePaths: [archive] }) + }, process.env.LEXORA_TEST_LEGACY_CLEANUP_ARCHIVE) + await page.getByTestId('extension-install-options').click() + await page.getByTestId('extension-install').click() + const review = page.getByTestId('extension-install-review') + await expect(review).toBeVisible() + await expect(review.locator('[data-permission="legacyTaskCleanup"]')).toContainText('不允许删除活动任务或任意文件') + await review.getByTestId('extension-confirm-install').click() + await expect(review).toBeHidden() + await expect.poll(() => page.evaluate(async id => (await window.lexoraDesktop.extensions.list()).find(item => item.manifest.id === id)?.enabled, pluginId)).toBe(true) + const card = page.locator(`article[data-extension-id="${pluginId}"]`) + await expect(card.getByRole('button', { name: '打开', exact: true })).toHaveCount(0) + await expect(page.locator('.desktop-app-sidebar').getByRole('button', { name: '旧版任务清理', exact: true })).toHaveCount(0) + const runCleanup = async () => { + await card.getByTestId('extension-card-more').click() + await page.getByTestId('extension-actions').hover() + const action = page.locator(`[data-extension-action="${pluginId}.action"]`) + await expect(action).toHaveText('清理任务残留…') + await action.click() } - return false - }).toBe(true) - return frame - } - const showResult = async () => { - await card.getByTestId('extension-card-more').click() - await page.getByTestId('extension-actions').hover() - await page.locator(`[data-extension-action="${pluginId}.result-action"]`).click() - return pluginFrame() - } - const lifecycleWindow = await app.browserWindow(page) - await lifecycleWindow.evaluate(window => window.focus()) - await runCleanup() - let frame = await pluginFrame() - const confirmation = () => frame.getByTestId('cleanup-preview') - await expect(confirmation()).toBeVisible() - await expect(frame.getByTestId('cleanup-preview-total')).toHaveText('32') - await expect(confirmation()).toContainText('不可恢复') - await expect(confirmation()).toContainText('不处理活动或已归档任务') - await expect(confirmation()).toContainText('不删除用户工作区及外部文件') - await expect(frame.getByTestId('legacy-task-cleanup-panel')).toHaveAttribute('data-phase', 'confirming') - for (const appearance of ['light', 'dark']) { - await page.evaluate(() => window.lexoraDesktop.themes.request({ action: 'preference', preference: { id: 'system' } })) - await app.evaluate(({ nativeTheme }, appearance) => { - Object.defineProperty(nativeTheme, 'shouldUseDarkColors', { configurable: true, get: () => appearance === 'dark' }) - nativeTheme.emit('updated') - }, appearance) - await expect(page.locator('html')).toHaveAttribute('data-buddy-theme', appearance) - await capture(page, testInfo, `legacy-cleanup-confirm-${appearance}`) - } - const originalBounds = await lifecycleWindow.evaluate(window => window.getBounds()) - await lifecycleWindow.evaluate(window => window.setBounds({ width: 980, height: 640 })) - await expect.poll(() => page.evaluate(() => window.innerWidth)).toBe(980) - await expect(frame.getByTestId('cleanup-confirm-delete')).toBeInViewport() - await capture(page, testInfo, 'legacy-cleanup-confirm-minimum') - await lifecycleWindow.evaluate((window, bounds) => window.setBounds(bounds), originalBounds) - await frame.getByTestId('cleanup-cancel').click() - await expect(confirmation()).toBeHidden() - await expect(frame.getByTestId('legacy-task-cleanup-panel')).toHaveAttribute('data-phase', 'cancelled') - expect(await taskExists(app, fixture.databasePath, 'archive-legacy')).toBe(true) - expect(await fs.readFile(path.join(fixture.owned, 'owned.txt'), 'utf8')).toBe('Task-owned fixture') - await frame.getByRole('button', { name: '关闭', exact: true }).click() - await runCleanup() - frame = await pluginFrame() - await expect(confirmation()).toBeVisible() - await lifecycleWindow.evaluate(window => window.hide()) - await expect(frame.getByTestId('legacy-task-cleanup-panel')).toHaveAttribute('data-phase', 'cancelled') - await lifecycleWindow.evaluate((window) => { - window.show() - window.focus() - }) - await expect(confirmation()).toBeHidden() - expect(await taskExists(app, fixture.databasePath, 'archive-legacy')).toBe(true) - await frame.getByRole('button', { name: '关闭', exact: true }).click() - await runCleanup() - frame = await pluginFrame() - await expect(confirmation()).toBeVisible() - await frame.evaluate(() => { - window.cleanupProgressSnapshots = [] - const panel = document.querySelector('[data-testid="legacy-task-cleanup-panel"]') - new MutationObserver(() => { - if (panel.dataset.phase === 'running') - window.cleanupProgressSnapshots.push(Number(panel.querySelector('[data-testid="cleanup-processed"]').textContent)) - }).observe(panel, { subtree: true, attributes: true, childList: true, characterData: true }) - }) - await frame.getByTestId('cleanup-confirm-delete').click() - await expect(confirmation()).toBeHidden() - await expect(frame.getByTestId('legacy-task-cleanup-panel')).toHaveAttribute('data-phase', 'running') - await expect.poll(() => frame.evaluate(() => window.cleanupProgressSnapshots.some(count => count > 0 && count < 32))).toBe(true) - await capture(page, testInfo, 'legacy-cleanup-plugin-progress') - await frame.getByRole('button', { name: /^(后台继续|关闭)$/ }).click() - await expect(card.getByTestId('extension-card-more')).toBeEnabled() - await page.locator('.desktop-app-sidebar').getByRole('button', { name: '设置', exact: true }).click() - await expect.poll(() => taskExists(app, fixture.databasePath, 'archive-legacy-31')).toBe(false) - await page.locator('.desktop-app-sidebar').getByRole('button', { name: '插件', exact: true }).click() - frame = await showResult() - await expect(frame.getByTestId('legacy-task-cleanup-panel')).toHaveAttribute('data-phase', 'completed') - await expect(frame.getByTestId('cleanup-deleted')).toHaveText('32') - await expect(frame.getByTestId('cleanup-pending')).toHaveText('0') - await expect(frame.getByTestId('cleanup-failed')).toHaveText('0') - await capture(page, testInfo, 'legacy-cleanup-plugin-result-dark') - await app.evaluate(({ nativeTheme }) => { - Object.defineProperty(nativeTheme, 'shouldUseDarkColors', { configurable: true, get: () => false }) - nativeTheme.emit('updated') + const pluginFrame = async () => { + let frame + await expect.poll(async () => { + for (const candidate of page.frames()) { + if (await candidate.getByTestId('legacy-task-cleanup-panel').count().catch(() => 0)) { + frame = candidate + return true + } + } + return false + }).toBe(true) + return frame + } + const showResult = async () => { + await card.getByTestId('extension-card-more').click() + await page.getByTestId('extension-actions').hover() + await page.locator(`[data-extension-action="${pluginId}.result-action"]`).click() + return pluginFrame() + } + const lifecycleWindow = await app.browserWindow(page) + await lifecycleWindow.evaluate(window => window.focus()) + await runCleanup() + let frame = await pluginFrame() + const confirmation = () => frame.getByTestId('cleanup-preview') + await expect(confirmation()).toBeVisible() + await expect(frame.getByTestId('cleanup-preview-total')).toHaveText('32') + await expect(confirmation()).toContainText('不可恢复') + await expect(confirmation()).toContainText('不处理活动或已归档任务') + await expect(confirmation()).toContainText('不删除用户工作区及外部文件') + await expect(frame.getByTestId('legacy-task-cleanup-panel')).toHaveAttribute('data-phase', 'confirming') + for (const appearance of ['light', 'dark']) { + await page.evaluate(() => window.lexoraDesktop.themes.request({ action: 'preference', preference: { id: 'system' } })) + await app.evaluate(({ nativeTheme }, appearance) => { + Object.defineProperty(nativeTheme, 'shouldUseDarkColors', { configurable: true, get: () => appearance === 'dark' }) + nativeTheme.emit('updated') + }, appearance) + await expect(page.locator('html')).toHaveAttribute('data-buddy-theme', appearance) + await capture(page, testInfo, `legacy-cleanup-confirm-${appearance}`) + } + const originalBounds = await lifecycleWindow.evaluate(window => window.getBounds()) + await lifecycleWindow.evaluate(window => window.setBounds({ width: 980, height: 640 })) + await expect.poll(() => page.evaluate(() => window.innerWidth)).toBe(980) + await expect(frame.getByTestId('cleanup-confirm-delete')).toBeInViewport() + await capture(page, testInfo, 'legacy-cleanup-confirm-minimum') + await lifecycleWindow.evaluate((window, bounds) => window.setBounds(bounds), originalBounds) + await frame.getByTestId('cleanup-cancel').click() + await expect(confirmation()).toBeHidden() + await expect(frame.getByTestId('legacy-task-cleanup-panel')).toHaveAttribute('data-phase', 'cancelled') + expect(await taskExists(app, fixture.databasePath, 'archive-legacy')).toBe(true) + expect(await fs.readFile(path.join(fixture.owned, 'owned.txt'), 'utf8')).toBe('Task-owned fixture') + await frame.getByRole('button', { name: '关闭', exact: true }).click() + await runCleanup() + frame = await pluginFrame() + await expect(confirmation()).toBeVisible() + await lifecycleWindow.evaluate(window => window.hide()) + await expect(frame.getByTestId('legacy-task-cleanup-panel')).toHaveAttribute('data-phase', 'cancelled') + await lifecycleWindow.evaluate((window) => { + window.show() + window.focus() + }) + await expect(confirmation()).toBeHidden() + expect(await taskExists(app, fixture.databasePath, 'archive-legacy')).toBe(true) + await frame.getByRole('button', { name: '关闭', exact: true }).click() + await runCleanup() + frame = await pluginFrame() + await expect(confirmation()).toBeVisible() + await frame.evaluate(() => { + window.cleanupProgressSnapshots = [] + const panel = document.querySelector('[data-testid="legacy-task-cleanup-panel"]') + new MutationObserver(() => { + if (panel.dataset.phase === 'running') + window.cleanupProgressSnapshots.push(Number(panel.querySelector('[data-testid="cleanup-processed"]').textContent)) + }).observe(panel, { subtree: true, attributes: true, childList: true, characterData: true }) + }) + await frame.getByTestId('cleanup-confirm-delete').click() + await expect(confirmation()).toBeHidden() + await expect(frame.getByTestId('legacy-task-cleanup-panel')).toHaveAttribute('data-phase', 'running') + await expect.poll(() => frame.evaluate(() => window.cleanupProgressSnapshots.some(count => count > 0 && count < 32))).toBe(true) + await capture(page, testInfo, 'legacy-cleanup-plugin-progress') + await frame.getByRole('button', { name: /^(后台继续|关闭)$/ }).click() + await expect(card.getByTestId('extension-card-more')).toBeEnabled() + await page.locator('.desktop-app-sidebar').getByRole('button', { name: '设置', exact: true }).click() + await expect.poll(() => taskExists(app, fixture.databasePath, 'archive-legacy-31')).toBe(false) + await page.locator('.desktop-app-sidebar').getByRole('button', { name: '插件', exact: true }).click() + frame = await showResult() + await expect(frame.getByTestId('legacy-task-cleanup-panel')).toHaveAttribute('data-phase', 'completed') + await expect(frame.getByTestId('cleanup-deleted')).toHaveText('32') + await expect(frame.getByTestId('cleanup-pending')).toHaveText('0') + await expect(frame.getByTestId('cleanup-failed')).toHaveText('0') + await capture(page, testInfo, 'legacy-cleanup-plugin-result-dark') + await app.evaluate(({ nativeTheme }) => { + Object.defineProperty(nativeTheme, 'shouldUseDarkColors', { configurable: true, get: () => false }) + nativeTheme.emit('updated') + }) + await expect(page.locator('html')).toHaveAttribute('data-buddy-theme', 'light') + await capture(page, testInfo, 'legacy-cleanup-plugin-result-light') + await lifecycleWindow.evaluate(window => window.setBounds({ width: 980, height: 640 })) + await expect(frame.getByRole('button', { name: '关闭', exact: true })).toBeInViewport() + await capture(page, testInfo, 'legacy-cleanup-plugin-result-minimum') + await lifecycleWindow.evaluate((window, bounds) => window.setBounds(bounds), originalBounds) + await lifecycleWindow.dispose() + for (const id of fixture.legacyTaskIds) + expect(await taskExists(app, fixture.databasePath, id)).toBe(false) + await expect.poll(() => fs.access(fixture.owned).then(() => true, () => false)).toBe(false) + expect(await fs.readFile(path.join(fixture.external, 'keep.txt'), 'utf8')).toBe('External fixture must survive') + expect(await taskExists(app, fixture.databasePath, 'archive-active')).toBe(true) + expect(await taskExists(app, fixture.databasePath, 'archive-existing')).toBe(true) + await frame.getByRole('button', { name: '关闭', exact: true }).click() + await runCleanup() + frame = await pluginFrame() + await expect(frame.getByTestId('legacy-task-cleanup-panel')).toHaveAttribute('data-phase', 'completed') + await expect(frame.getByTestId('cleanup-total')).toHaveText('0') + await expect(frame.getByTestId('legacy-task-cleanup-panel')).toContainText('没有可清理的旧版任务') + await capture(page, testInfo, 'legacy-cleanup-plugin-empty') + await frame.getByRole('button', { name: '关闭', exact: true }).click() + await expect(page.locator('.n-message')).toHaveCount(0, { timeout: 15000 }) + await card.getByTestId('extension-card-more').click() + await page.getByTestId('extension-actions').hover() + await capture(page, testInfo, 'legacy-cleanup-plugin-menu') + await page.keyboard.press('Escape') + await card.getByTestId('extension-disable').click() + await expect(card.getByTestId('extension-enable')).toBeVisible() + await card.getByTestId('extension-card-more').click() + await expect(page.getByTestId('extension-actions')).toHaveCount(0) + }) }) - await expect(page.locator('html')).toHaveAttribute('data-buddy-theme', 'light') - await capture(page, testInfo, 'legacy-cleanup-plugin-result-light') - await lifecycleWindow.evaluate(window => window.setBounds({ width: 980, height: 640 })) - await expect(frame.getByRole('button', { name: '关闭', exact: true })).toBeInViewport() - await capture(page, testInfo, 'legacy-cleanup-plugin-result-minimum') - await lifecycleWindow.evaluate((window, bounds) => window.setBounds(bounds), originalBounds) - await lifecycleWindow.dispose() - for (const id of fixture.legacyTaskIds) - expect(await taskExists(app, fixture.databasePath, id)).toBe(false) - await expect.poll(() => fs.access(fixture.owned).then(() => true, () => false)).toBe(false) - expect(await fs.readFile(path.join(fixture.external, 'keep.txt'), 'utf8')).toBe('External fixture must survive') - expect(await taskExists(app, fixture.databasePath, 'archive-active')).toBe(true) - expect(await taskExists(app, fixture.databasePath, 'archive-existing')).toBe(true) - await expectStorage(app, fixture.databasePath) - await frame.getByRole('button', { name: '关闭', exact: true }).click() - await runCleanup() - frame = await pluginFrame() - await expect(frame.getByTestId('legacy-task-cleanup-panel')).toHaveAttribute('data-phase', 'completed') - await expect(frame.getByTestId('cleanup-total')).toHaveText('0') - await expect(frame.getByTestId('legacy-task-cleanup-panel')).toContainText('没有可清理的旧版任务') - await capture(page, testInfo, 'legacy-cleanup-plugin-empty') - await frame.getByRole('button', { name: '关闭', exact: true }).click() - await expect(page.locator('.n-message')).toHaveCount(0, { timeout: 15000 }) - await card.getByTestId('extension-card-more').click() - await page.getByTestId('extension-actions').hover() - await capture(page, testInfo, 'legacy-cleanup-plugin-menu') - await page.keyboard.press('Escape') - await card.getByTestId('extension-disable').click() - await expect(card.getByTestId('extension-enable')).toBeVisible() - await card.getByTestId('extension-card-more').click() - await expect(page.getByTestId('extension-actions')).toHaveCount(0) -}) +} diff --git a/.playwright/scripts/__tests__/workbench.e2e.mjs b/.playwright/scripts/__tests__/workbench.e2e.mjs index 3e1f6c69b..093cdd0bd 100644 --- a/.playwright/scripts/__tests__/workbench.e2e.mjs +++ b/.playwright/scripts/__tests__/workbench.e2e.mjs @@ -2,7 +2,7 @@ import fs from 'node:fs/promises' import path from 'node:path' import { expect, test } from '../fixtures/electron.mjs' -test('working copies and sidebar preserve drafts, veto unsaved closure and recover across restart', async ({ buddy }) => { +test('working copies preserve drafts, veto unsaved closure and recover across restart', async ({ buddy }) => { const instance = await buddy.createInstance('working-copy') const directory = path.join(instance.home, 'documents') const document = path.join(directory, 'README.md') @@ -47,28 +47,6 @@ test('working copies and sidebar preserve drafts, veto unsaved closure and recov await expect.poll(readBackup).toBe(beforeUndo) await expect(editor.locator('.view-lines')).toContainText('Second edit while dirty.') expect(await fs.readFile(document, 'utf8')).toBe('Original document.\n') - const sidebar = page.locator('.desktop-workbench-layout__sidebar') - const width = () => sidebar.evaluate(element => element.getBoundingClientRect().width) - const initialWidth = await width() - await page.getByTestId('workbench-sidebar-resizer').focus() - await page.keyboard.press('ArrowRight') - const savedWidth = () => page.evaluate(async () => (await window.lexoraDesktop.settings.get()).desktop.taskSidebar.width) - await expect.poll(savedWidth).toBeGreaterThan(initialWidth) - const expandedWidth = await savedWidth() - await expect.poll(width).toBe(expandedWidth) - const toggle = page.getByTestId('workbench-sidebar-toggle') - await page.getByTestId('workbench-sidebar-resizer').hover() - await toggle.click() - await expect(toggle).toHaveAttribute('aria-expanded', 'false') - await expect(sidebar).toHaveAttribute('inert', '') - await expect.poll(width).toBe(0) - await expect(composer).toHaveText(draft) - await page.locator('.desktop-workbench-layout__sidebar-collapsed-boundary').hover() - await toggle.click() - await expect(toggle).toHaveAttribute('aria-expanded', 'true') - await expect(sidebar).not.toHaveAttribute('inert') - await expect.poll(width).toBe(expandedWidth) - await expect(composer).toHaveText(draft) await page.getByTestId('task-context-panel').getByRole('tab', { name: '文件', exact: true }).click({ button: 'right' }) await page.locator('.n-dropdown-menu:visible').getByText('关闭全部', { exact: true }).click() const close = page.locator('.n-dialog').filter({ hasText: '关闭前保存更改?' }) @@ -77,8 +55,6 @@ test('working copies and sidebar preserve drafts, veto unsaved closure and recov await expect(editor.locator('.view-lines')).toContainText('Second edit while dirty.') await instance.stop() ;({ app, page, diagnostics } = await instance.launch()) - await expect.poll(savedWidth).toBe(expandedWidth) - await expect.poll(() => page.locator('.desktop-workbench-layout__sidebar').evaluate(element => element.getBoundingClientRect().width)).toBe(expandedWidth) await expect(page.locator('.desktop-chat-composer__prosemirror:visible')).toHaveText(draft) const panelToggle = page.getByTestId('context-panel-toggle') if (await panelToggle.getAttribute('aria-expanded') === 'false') @@ -224,6 +200,17 @@ async function sendNotificationTarget(window) { await window.evaluate(window => window.webContents.send('lexora:app:open-target', { conversationId: 'b', runId: 'notification-run' })) } +async function expectNotificationBranch(page, pane) { + await expect(pane.locator('[data-message-id="notification-message"]')).toBeAttached() + const viewId = await pane.locator('.workbench__view[data-resource-id="b"]').getAttribute('data-view-id') + await expect.poll(() => page.evaluate(async id => (await window.lexoraDesktop.workbench.read())?.layout.views[id]?.resource, viewId)).toMatchObject({ + scheme: 'task', + id: 'b', + data: { branchId: 'notification-branch' }, + }) + expect(await page.evaluate(async () => (await window.lexoraDesktop.localChat.conversations.get('b')).activeBranchId)).toBe('branch-b') +} + async function expectNotificationMessage(page, pane) { const message = pane.locator('[data-message-id="notification-message"]') await expect(message).toContainText('The notification points to this earlier message.') @@ -231,7 +218,7 @@ async function expectNotificationMessage(page, pane) { const laterMessages = await page.evaluate(() => window.lexoraDesktop.localChat.conversations.listMessages({ conversationId: 'b', branchId: 'notification-branch', limit: 100 })) expect(laterMessages.items.map(item => item.id)).toContain('later-message-24') await expect(pane.locator('[data-message-id="later-message-24"]')).not.toBeInViewport() - await expect.poll(() => page.evaluate(async () => (await window.lexoraDesktop.localChat.conversations.get('b')).activeBranchId)).toBe('notification-branch') + await expectNotificationBranch(page, pane) await expect(pane.locator('[data-message-id="message-b"]')).toHaveCount(0) } @@ -285,7 +272,7 @@ test('notification navigation: app:open-target opens an unopened task while hidd await expect.poll(() => window.evaluate(window => ({ visible: window.isVisible(), focused: window.isFocused() }))).toEqual({ visible: false, focused: false }) await sendNotificationTarget(window) - await expect.poll(() => page.evaluate(async () => (await window.lexoraDesktop.localChat.conversations.get('b')).activeBranchId)).toBe('notification-branch') + await expectNotificationBranch(page, notificationPane(page, 'b')) await expect(notificationTask(page, 'b')).toHaveClass(/is-active/) expect(await window.evaluate(window => window.isVisible())).toBe(false) await window.evaluate(window => window.showInactive()) @@ -297,73 +284,3 @@ test('notification navigation: app:open-target opens an unopened task while hidd await expect(notificationTask(page, 'b')).toHaveClass(/is-active/) expect(diagnostics.console.filter(item => item.type === 'pageerror')).toEqual([]) }) - -test('resource tab menus pin, close by position and preserve pins across restart', async ({ buddy }) => { - const instance = await buddy.createInstance('resource-tab-menu') - let { page, diagnostics } = await instance.launch() - await expect(page.getByRole('textbox', { name: '对话消息', exact: true })).toBeVisible() - await page.keyboard.press('Control+Shift+P') - await page.getByPlaceholder('输入命令名称').fill('浏览器') - await page.getByPlaceholder('输入命令名称').press('Enter') - const panel = () => page.getByTestId('task-context-panel') - const tabs = () => panel().getByRole('tab') - const menu = () => page.locator('.n-dropdown-menu:visible') - async function add() { - const count = await tabs().count() - await page.getByTestId('context-add-tab').click() - await page.getByRole('menuitem', { name: '浏览器', exact: true }).click() - await expect(tabs()).toHaveCount(count + 1) - } - async function action(index, label) { - await tabs().nth(index).click({ button: 'right' }) - await expect(menu().getByText(label, { exact: true }).locator('..')).not.toHaveClass(/disabled/) - await menu().getByText(label, { exact: true }).click() - } - await expect(tabs()).toHaveCount(1) - await add() - await add() - await add() - const ids = await tabs().evaluateAll(elements => elements.map(element => element.id)) - await action(1, '固定') - await expect(tabs().first()).toHaveAttribute('id', ids[1]) - await expect(panel().locator('[data-pinned="true"]')).toHaveCount(1) - await expect(tabs().last()).toHaveAttribute('aria-selected', 'true') - await tabs().nth(2).click({ button: 'right' }) - await expect(menu().getByText('关闭左侧', { exact: true }).locator('..')).not.toHaveClass(/disabled/) - await menu().getByText('关闭左侧', { exact: true }).click() - await expect(tabs()).toHaveCount(3) - expect(await tabs().evaluateAll(elements => elements.map(element => element.id))).toEqual([ids[1], ids[2], ids[3]]) - await action(1, '关闭右侧') - await expect(tabs()).toHaveCount(2) - expect(await tabs().evaluateAll(elements => elements.map(element => element.id))).toEqual([ids[1], ids[2]]) - await add() - await action(1, '关闭其他') - await expect(tabs()).toHaveCount(2) - await action(1, '关闭全部') - await expect(tabs()).toHaveCount(1) - await expect(tabs().first()).toHaveAttribute('id', ids[1]) - await tabs().first().focus() - await page.keyboard.press('Shift+F10') - await expect(menu().getByText('取消固定', { exact: true })).toBeVisible() - for (const label of ['关闭其他', '关闭左侧', '关闭右侧', '关闭全部']) - await expect(menu().getByText(label, { exact: true }).locator('..')).toHaveClass(/disabled/) - await page.keyboard.press('Escape') - await expect(menu()).toBeHidden() - expect(diagnostics.console.filter(item => item.type === 'pageerror')).toEqual([]) - await instance.stop() - ;({ page, diagnostics } = await instance.launch()) - const toggle = page.getByTestId('context-panel-toggle') - await expect(toggle).toBeVisible() - if (await toggle.getAttribute('aria-expanded') === 'false') - await toggle.click() - await expect(panel().locator('[data-pinned="true"]')).toHaveCount(1) - await action(0, '取消固定') - await expect(panel().locator('[data-pinned="true"]')).toHaveCount(0) - await action(0, '固定') - await panel().getByRole('button', { name: '取消固定', exact: true }).click() - await expect(tabs()).toHaveCount(1) - await expect(panel().locator('[data-pinned="true"]')).toHaveCount(0) - await panel().getByRole('button', { name: '关闭新标签页', exact: true }).click() - await expect(tabs()).toHaveCount(0) - expect(diagnostics.console.filter(item => item.type === 'pageerror')).toEqual([]) -}) diff --git a/.playwright/scripts/buddy-test.mjs b/.playwright/scripts/buddy-test.mjs index ed2f65fab..536568c6a 100644 --- a/.playwright/scripts/buddy-test.mjs +++ b/.playwright/scripts/buddy-test.mjs @@ -8,6 +8,7 @@ import { createServer } from 'node:net' import { homedir } from 'node:os' import path from 'node:path' import process from 'node:process' +import { launchDaemonDesktop } from './fixtures/local-service.mjs' import { attachPageDiagnostics, ROOT, serializeError } from './shared.mjs' const repoRoot = ROOT @@ -38,7 +39,7 @@ export async function createBuddyTestRun({ appPath = process.env.LEXORA_TEST_APP const pending = new Set() const records = [] let disposed = false - async function createInstance(label) { + async function createInstance(label, { executablePath: instanceExecutablePath = executablePath } = {}) { assert(/^[\w-]{1,64}$/.test(label), 'Invalid test instance name') const home = await fs.mkdtemp(path.join(instancesRoot, `${label}-`)) const identity = await fs.stat(home) @@ -83,11 +84,17 @@ export async function createBuddyTestRun({ appPath = process.env.LEXORA_TEST_APP instances.push(instance) return instance - async function launch({ maintenance = false, expectedRoot = home, startupStatus = 'ready' } = {}) { + async function launch({ maintenance = false, profileRoot = home, expectedRoot = profileRoot, startupStatus = 'ready', storageAuthority = 'legacy_jsonl', serviceMode = 'utility' } = {}) { assert(['ready', 'failed'].includes(startupStatus), 'Unsupported startup expectation') assert(contains(instancesRoot, await canonicalPath(expectedRoot)), 'Expected root must stay in this isolated test run') - const { _electron: electron } = await import('@playwright/test') - const environment = createTestEnvironment(home) + const { _electron: electron, expect } = await import('@playwright/test') + assert(['legacy_jsonl', 'sqlite', 'default'].includes(storageAuthority), 'Unsupported storage authority') + assert(contains(instancesRoot, await canonicalPath(profileRoot)), 'Profile must stay in this isolated test run') + assert(['utility', 'daemon'].includes(serviceMode), 'Unsupported service mode') + assert(serviceMode !== 'daemon' || (!maintenance && !instanceExecutablePath && !rendererUrl && storageAuthority === 'sqlite'), 'Daemon fixture requires the SQLite development application') + const environment = createTestEnvironment(profileRoot) + if (storageAuthority !== 'default') + environment.LEXORA_BUDDY_STORAGE_AUTHORITY = storageAuthority if (rendererUrl) environment.ELECTRON_RENDERER_URL = rendererUrl if (process.platform === 'linux') { @@ -99,9 +106,11 @@ export async function createBuddyTestRun({ appPath = process.env.LEXORA_TEST_APP launchNumber += 1 try { const args = [...(process.platform === 'linux' ? ['--ozone-platform=x11', '--disable-setuid-sandbox'] : []), ...(passwordStore ? [`--password-store=${passwordStore}`] : []), '--disable-gpu'] - application = executablePath - ? await launchInstalled(executablePath, args, environment, { maintenance, artifactDirectory, launchNumber }) - : await electron.launch({ executablePath: require('electron'), args: [...args, path.resolve(appPath)], chromiumSandbox: true, cwd: repoRoot, env: environment, timeout: 45000 }) + application = serviceMode === 'daemon' + ? await launchDaemonDesktop({ home: profileRoot, buddyRoot, hostExecutable: require('electron'), hostArguments: args, environment, artifactDirectory }) + : instanceExecutablePath + ? await launchInstalled(instanceExecutablePath, args, environment, { maintenance, artifactDirectory, launchNumber }) + : await electron.launch({ executablePath: require('electron'), args: [...args, path.resolve(appPath)], chromiumSandbox: true, cwd: repoRoot, env: environment, timeout: 45000 }) applicationProcess = application.process() launchRecord.pid = applicationProcess.pid applicationProcess.stderr?.on('data', (chunk) => { @@ -117,10 +126,10 @@ export async function createBuddyTestRun({ appPath = process.env.LEXORA_TEST_APP return { app: application, page, diagnostics: launchRecord.diagnostics } } assert.equal(rendererUrl ? new URL(page.url()).origin : new URL(page.url()).protocol, rendererUrl ? new URL(rendererUrl).origin : 'lexora-app:') - await page.waitForFunction(async status => (await window.lexoraDesktop?.app.startup.getState())?.status === status, startupStatus, { timeout: 45000 }) + await expect.poll(() => page.evaluate(async () => (await window.lexoraDesktop?.app.startup.getState())?.status), { timeout: 45000 }).toBe(startupStatus) const info = await page.evaluate(() => window.lexoraDesktop.app.getInfo()) assert.equal(info.runtimeProfile, 'test') - if (executablePath) + if (instanceExecutablePath) assert.equal(info.isPackaged, true) assert.equal(await fs.realpath(path.dirname(info.configPath)), await canonicalPath(expectedRoot)) const { runtimeProfile, isPackaged, version, platform, configPath } = info @@ -137,7 +146,7 @@ export async function createBuddyTestRun({ appPath = process.env.LEXORA_TEST_APP if (application) { const current = application const child = applicationProcess - if (executablePath || (child.exitCode === null && child.signalCode === null)) { + if (instanceExecutablePath || current.serviceMode === 'daemon' || (child.exitCode === null && child.signalCode === null)) { await page?.screenshot({ path: path.join(artifactDirectory, `launch-${launchNumber}.png`), animations: 'disabled', timeout: 5000 }).catch(() => {}) await withTimeout(current.context().tracing.stop({ path: path.join(artifactDirectory, `trace-${launchNumber}.zip`) }), 10000, 'Trace export timed out').catch(() => {}) try { @@ -169,9 +178,9 @@ export async function createBuddyTestRun({ appPath = process.env.LEXORA_TEST_APP artifacts, runId, records, - async createInstance(label = 'desktop') { + async createInstance(label = 'desktop', options = {}) { assert(!disposed, 'Test run has been disposed') - const operation = createInstance(label) + const operation = createInstance(label, options) pending.add(operation) try { return await operation @@ -197,10 +206,10 @@ export async function createBuddyTestRun({ appPath = process.env.LEXORA_TEST_APP } } -function createTestEnvironment(home) { +export function createTestEnvironment(home) { const environment = Object.fromEntries(Object.entries(process.env).filter(([key, value]) => value !== undefined && !/^(?:LEXORA_|ELECTRON_|PI_|XDG_|WAYLAND_DISPLAY$)/.test(key))) - return { ...environment, LEXORA_BUDDY_PROFILE: 'test', LEXORA_HOME: home } + return { ...environment, LEXORA_BUDDY_PROFILE: 'test', LEXORA_HOME: home, LEXORA_BUDDY_SERVICE_MODE: 'utility' } } function contains(root, candidate) { @@ -225,7 +234,7 @@ async function canonicalPath(value) { } } -async function startDisplay() { +export async function startDisplay() { const child = spawn('Xvfb', ['-displayfd', '3', '-screen', '0', '1500x950x24', '-nolisten', 'tcp'], { stdio: ['ignore', 'ignore', 'pipe', 'pipe'] }) let stderr = '' child.stderr.on('data', (chunk) => { diff --git a/.playwright/scripts/fixtures/attachment-publication-crash.ts b/.playwright/scripts/fixtures/attachment-publication-crash.ts new file mode 100644 index 000000000..ce7676b83 --- /dev/null +++ b/.playwright/scripts/fixtures/attachment-publication-crash.ts @@ -0,0 +1,68 @@ +import { Buffer } from 'node:buffer' +import { writeSync } from 'node:fs' +import { readFile } from 'node:fs/promises' +import { join } from 'node:path' +import process from 'node:process' +import { fileStorage } from '../../../apps/buddy/platform/filesystem/fileStorage' +import { AttachmentService } from '../../../apps/buddy/service/src/attachments/AttachmentService' +import { createAttachmentRepository } from '../../../apps/buddy/service/src/storage/attachmentRepository' +import { BuddyDataPaths } from '../../../apps/buddy/service/src/storage/BuddyDataPaths' +import { openBuddyMigrationCandidate } from '../../../apps/buddy/service/src/storage/database' + +const [mode, directory, point] = process.argv.slice(2) +if (!directory || !['publish', 'recover'].includes(mode!)) + throw new Error('Invalid attachment crash fixture arguments') +const database = openBuddyMigrationCandidate({ databasePath: join(directory, 'data.sqlite3') }) +database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') +const repository = createAttachmentRepository(database) +const service = new AttachmentService({ paths: new BuddyDataPaths(join(directory, 'profile')), repository }) +function checkpoint(name: string) { + if (name !== point) + return + writeSync(1, `${JSON.stringify({ checkpoint: name })}\n`) + process.kill(process.pid, 'SIGKILL') +} +if (mode === 'publish') { + const persistence = repository.persistence! + const prepare = persistence.prepare.bind(persistence) + persistence.prepare = (input) => { + const result = prepare(input) + checkpoint('prepared') + return result + } + const replace = fileStorage.replace + fileStorage.replace = async (...args) => { + checkpoint('file-synced') + await replace(...args) + checkpoint('renamed') + } + const syncDirectory = fileStorage.syncDirectory + fileStorage.syncDirectory = async (...args) => { + await syncDirectory(...args) + checkpoint('directory-synced') + } + const create = repository.create + repository.create = (record) => { + const result = create(record) + checkpoint('bound') + return result + } + service.onDidChange((event) => { + if (event.kind === 'file-published') + checkpoint('published') + }) + await service.registerUploads('crash-draft', [{ name: 'source.txt', mimeType: 'text/plain', bytes: Buffer.from('durable attachment bytes') }]) + throw new Error('Requested publication checkpoint was not reached') +} +else { + const before = database.prepare('SELECT state FROM managed_files').get() + const recovered = await service.reconcileStorage() + const records = repository.listAll() + const bytes = await Promise.all(records.map(record => readFile(record.storedPath, 'utf8'))) + const files = database.prepare('SELECT state, sha256, durability FROM managed_files').all() + const references = database.prepare('SELECT kind, owner_id FROM file_references').all() + const preparations = database.prepare('SELECT state, reserved_bytes FROM file_preparations').all() + await service.dispose() + database.close() + writeSync(1, `${JSON.stringify({ before, recovered, files, references, preparations, bytes })}\n`) +} diff --git a/.playwright/scripts/fixtures/electron.mjs b/.playwright/scripts/fixtures/electron.mjs index 9e154fd28..19f954ff5 100644 --- a/.playwright/scripts/fixtures/electron.mjs +++ b/.playwright/scripts/fixtures/electron.mjs @@ -62,7 +62,7 @@ export const test = base.extend({ for (const file of await fs.readdir(record.artifactDirectory)) { await testInfo.attach(`${path.basename(record.home)}-${file}`, { path: path.join(record.artifactDirectory, file), - contentType: file.endsWith('.png') ? 'image/png' : 'application/zip', + contentType: file.endsWith('.png') ? 'image/png' : file.endsWith('.zip') ? 'application/zip' : file.endsWith('.json') ? 'application/json' : 'text/plain', }) } } @@ -74,7 +74,8 @@ export const test = base.extend({ export { expect } from '@playwright/test' export async function useSyntheticCredentialStorage({ app, page }) { - await base.expect.poll(async () => (await page.evaluate(() => window.lexoraDesktop.localChat.runtime.getStatus())).status, { timeout: 45000 }).toBe('ready') + await base.expect.poll(() => page.evaluate(async () => (await window.lexoraDesktop.app.startup.getState()).status), { timeout: 45000 }).toBe('ready') + const generation = await page.evaluate(async () => (await window.lexoraDesktop.app.startup.getState()).generation) await app.evaluate(({ app, safeStorage }) => { if (app.getName() !== 'Lexora Buddy Test') throw new Error('Synthetic credentials require an isolated test instance') @@ -95,5 +96,8 @@ export async function useSyntheticCredentialStorage({ app, page }) { }) }) await page.evaluate(() => window.lexoraDesktop.localChat.runtime.restart()) - await base.expect.poll(async () => (await page.evaluate(() => window.lexoraDesktop.localChat.runtime.getStatus())).status, { timeout: 45000 }).toBe('ready') + await base.expect.poll(async () => { + const state = await page.evaluate(() => window.lexoraDesktop.app.startup.getState()) + return { ready: state.status === 'ready', restarted: state.generation !== generation } + }, { timeout: 45000 }).toEqual({ ready: true, restarted: true }) } diff --git a/.playwright/scripts/fixtures/extension-contributions.mjs b/.playwright/scripts/fixtures/extension-contributions.mjs index 9c7b4df97..fd27ad848 100644 --- a/.playwright/scripts/fixtures/extension-contributions.mjs +++ b/.playwright/scripts/fixtures/extension-contributions.mjs @@ -15,7 +15,7 @@ export async function writeContributionPlugin(directory, name, { uno = false, me entry: 'host.js', ...(media ? { permissions: { network: ['https://media.example.com'], themeManagement: true } } : {}), contributes: { - commands: ['read', 'save', 'failure', 'invalid', 'timeout', ...(media ? ['download'] : [])].map(command => ({ id: `${id}.${command}`, title: command, hidden: true })), + commands: ['read', 'save', 'failure', 'invalid', 'wait', ...(media ? ['download'] : [])].map(command => ({ id: `${id}.${command}`, title: command, hidden: true })), views: [ { id: `${id}.settings`, title: `Settings ${name}`, entry: 'settings.js', resource: 'none', location: 'page' }, { id: `${id}.footer`, title: `Footer ${name}`, entry: 'footer.js', resource: 'none' }, @@ -55,7 +55,7 @@ async function activate(media, context) { throw new Error('Private plugin error') })) context.subscriptions.add(context.commands.register(`${id}.invalid`, () => Number.NaN)) - context.subscriptions.add(context.commands.register(`${id}.timeout`, () => new Promise(() => {}))) + context.subscriptions.add(context.commands.register(`${id}.wait`, () => new Promise(() => {}))) context.subscriptions.add(context.commands.register(`${id}.save`, async ({ arguments: patch }) => { state = { ...state, ...patch } await context.storage.set(state) @@ -78,7 +78,7 @@ async function settings(pluginId, context, container) { async function footer(pluginId, uno, context, container) { container.dataset.contributionFixture = `${pluginId}.footer` - window.fixture = { instance: crypto.randomUUID(), visible: context.visible } + window.fixture = { visible: context.visible } context.onVisibilityChange(visible => window.fixture.visible = visible) if (uno) container.className = 'h-full grid place-items-center rounded-control bg-accent text-on-accent text-[14px] font-sans transition-colors duration-[var(--lexora-motion-state-duration)]' diff --git a/.playwright/scripts/fixtures/local-service.mjs b/.playwright/scripts/fixtures/local-service.mjs new file mode 100644 index 000000000..da77d4782 --- /dev/null +++ b/.playwright/scripts/fixtures/local-service.mjs @@ -0,0 +1,213 @@ +import assert from 'node:assert/strict' +import { spawn } from 'node:child_process' +import { randomUUID } from 'node:crypto' +import { once } from 'node:events' +import fs from 'node:fs/promises' +import net from 'node:net' +import path from 'node:path' +import process from 'node:process' +import { chromium, expect } from '@playwright/test' + +export async function connectLocalService(discovery, secret, role = 'management') { + const socket = net.createConnection(role === 'product' ? discovery.productEndpoint : discovery.managementEndpoint) + const rpc = createRequests(frame => socket.write(`${JSON.stringify({ jsonrpc: '2.0', ...frame })}\n`), randomUUID) + let buffer = '' + let welcome + const ready = new Promise((resolve, reject) => { + welcome = { resolve, reject } + }) + socket.on('error', (error) => { + welcome.reject(error) + rpc.reject(error) + }) + socket.on('close', () => { + const error = new Error('SERVICE_CLOSED') + welcome.reject(error) + rpc.reject(error) + }) + socket.on('data', (chunk) => { + buffer += chunk + while (buffer.includes('\n')) { + const end = buffer.indexOf('\n') + const frame = JSON.parse(buffer.slice(0, end)) + buffer = buffer.slice(end + 1) + if (frame.type === 'welcome') + welcome.resolve(frame) + else rpc.receive(frame) + } + }) + const { protocol, profileId, serviceInstanceId, buildId, sessionId, userId } = discovery + socket.write(`${JSON.stringify({ protocol, profileId, serviceInstanceId, buildId, sessionId, userId, type: 'hello', role, secret })}\n`) + try { + expect(await ready).toMatchObject({ serviceInstanceId, buildId, sessionId, userId }) + return { request: rpc.request, close: () => socket.destroy() } + } + catch (error) { + socket.destroy() + throw error + } +} + +export async function launchDaemonDesktop({ home, buddyRoot, hostExecutable, hostArguments, environment, artifactDirectory }) { + assert.equal(process.platform, 'linux', 'Daemon Desktop fixture currently requires Linux') + assert.equal(environment.LEXORA_BUDDY_PROFILE, 'test') + environment = { ...environment, XDG_SESSION_ID: process.env.XDG_SESSION_ID } + const output = path.join(buddyRoot, '.output') + const release = await readJson(path.join(output, 'release-set.json')) + const boot = (await fs.readFile('/proc/sys/kernel/random/boot_id', 'utf8')).trim() + const runtimeDirectory = path.join(home, '.runtime') + await fs.mkdir(runtimeDirectory, { recursive: true, mode: 0o700 }) + const config = { + version: 1, + profile: 'test', + profileRoot: home, + resourcesRoot: path.join(output, path.dirname(release.core.entry), 'resources'), + nodeExecutable: path.join(output, release.node.entry), + coreEntry: path.join(output, release.core.entry), + hostExecutable, + hostArguments: [...hostArguments, '--inspect=127.0.0.1:0', '--remote-debugging-port=0', buddyRoot], + releaseSetPath: path.join(output, 'release-set.json'), + buildId: release.buildId, + sessionId: `${boot}:${process.env.XDG_SESSION_ID}`, + userId: String(process.getuid()), + leaseDirectory: path.join(path.dirname(home), '.leases'), + storageAuthority: 'sqlite', + } + const launchFile = path.join(runtimeDirectory, 'launch.json') + await fs.writeFile(launchFile, JSON.stringify(config), { mode: 0o600 }) + const core = spawn(config.nodeExecutable, [config.coreEntry, '--config', launchFile], { cwd: buddyRoot, env: environment, stdio: ['ignore', 'pipe', 'pipe'] }) + let log = '' + for (const stream of [core.stdout, core.stderr]) { + stream.on('data', (chunk) => { + log = `${log}${chunk}`.slice(-1024 * 1024) + }) + } + let browser + let inspector + let client + let hostPid + const stopped = once(core, 'exit') + const close = async () => { + await inspector?.close() + await browser?.close().catch(() => {}) + try { + if (core.exitCode === null && core.signalCode === null) { + const discovery = await readJson(path.join(runtimeDirectory, 'service.json')) + if (client && discovery) + await client.request('service.stop', { expectedServiceInstanceId: discovery.serviceInstanceId, commandId: randomUUID(), reason: 'explicit' }).catch(() => {}) + else core.kill('SIGTERM') + await expect.poll(() => core.exitCode !== null || core.signalCode !== null, { timeout: 20000 }).toBe(true) + } + await stopped + if (hostPid) + await expect.poll(() => processAlive(hostPid)).toBe(false) + } + finally { + client?.close() + if (core.exitCode === null && core.signalCode === null) + core.kill('SIGTERM') + await fs.writeFile(path.join(artifactDirectory, 'daemon.log'), log, { mode: 0o600 }) + } + } + try { + await expect.poll(async () => { + assert(core.exitCode === null && core.signalCode === null, `Node service exited: ${log}`) + return (await readJson(path.join(runtimeDirectory, 'service.json')))?.phase + }, { timeout: 120000 }).toBe('ready') + const discovery = await readJson(path.join(runtimeDirectory, 'service.json')) + expect(discovery.process.pid).toBe(core.pid) + expect(await fs.realpath(`/proc/${core.pid}/exe`)).toBe(await fs.realpath(config.nodeExecutable)) + const credentials = await readJson(path.join(runtimeDirectory, 'local-client.json')) + client = await connectLocalService(discovery, credentials.managementSecret) + await expect.poll(() => /DevTools listening on ws:\/\/\S+/.test(log)).toBe(true) + browser = await chromium.connectOverCDP(log.match(/DevTools listening on (ws:\/\/\S+)/)[1]) + await expect.poll(() => /Debugger listening on ws:\/\/\S+/.test(log)).toBe(true) + inspector = await connectInspector(log.match(/Debugger listening on (ws:\/\/\S+)/)[1]) + hostPid = await inspector.evaluate(() => process.pid) + expect(hostPid).not.toBe(core.pid) + const opened = await client.request('service.openDesktop', { expectedServiceInstanceId: discovery.serviceInstanceId, intent: { kind: 'tasks' } }) + expect(opened).toMatchObject({ accepted: true, uiReady: true, targetVisible: true, visible: true }) + const page = browser.contexts().flatMap(context => context.pages()).find(page => page.url().startsWith('lexora-app:')) + assert(page, 'Service did not open Desktop') + await fs.writeFile(path.join(artifactDirectory, 'daemon.json'), JSON.stringify({ corePid: core.pid, hostPid, buildId: release.buildId, nodeExecutable: config.nodeExecutable }, null, 2)) + return { serviceMode: 'daemon', context: () => page.context(), firstWindow: async () => page, process: () => core, evaluate: inspector.evaluate, detachMain: () => inspector.close(), close } + } + catch (error) { + await close() + throw error + } +} + +async function connectInspector(endpoint) { + const socket = new WebSocket(endpoint) + const rpc = createRequests(frame => socket.send(JSON.stringify(frame))) + socket.addEventListener('message', event => rpc.receive(JSON.parse(event.data))) + socket.addEventListener('close', () => rpc.reject(new Error('INSPECTOR_CLOSED'))) + await once(socket, 'open') + return { + async evaluate(callback, argument) { + const result = await rpc.request('Runtime.evaluate', { expression: `(${callback.toString()})(process.mainModule.require('electron'), ${JSON.stringify(argument) ?? 'undefined'})`, awaitPromise: true, returnByValue: true }) + if (result.exceptionDetails) + throw new Error(result.exceptionDetails.exception?.description ?? result.exceptionDetails.text) + return result.result.value + }, + async close() { + if (socket.readyState === WebSocket.OPEN) { + const closed = once(socket, 'close') + socket.close() + await closed + } + }, + } +} + +function createRequests(send, nextId) { + const pending = new Map() + let sequence = 0 + return { + request(method, params) { + const id = nextId ? nextId() : ++sequence + return new Promise((resolve, reject) => { + const timeout = setTimeout(() => { + pending.delete(id) + reject(new Error(`Request timed out: ${method}`)) + }, 45000) + pending.set(id, { resolve, reject, timeout }) + send({ id, method, params }) + }) + }, + receive(frame) { + const request = pending.get(frame.id) + if (!request) + return + pending.delete(frame.id) + clearTimeout(request.timeout) + if (frame.error) + request.reject(new Error(frame.error.message)) + else request.resolve(frame.result) + }, + reject(error) { + for (const request of pending.values()) { + clearTimeout(request.timeout) + request.reject(error) + } + pending.clear() + }, + } +} + +export async function readJson(file) { + try { + return JSON.parse(await fs.readFile(file, 'utf8')) + } + catch (error) { + if (error.code === 'ENOENT') + return null + throw error + } +} + +export async function processAlive(pid) { + const stat = await fs.readFile(`/proc/${pid}/stat`, 'utf8').catch(() => null) + return stat !== null && !['Z', 'X'].includes(stat.slice(stat.lastIndexOf(')') + 2).split(' ')[0]) +} diff --git a/.playwright/scripts/fixtures/mobile-desktop.mjs b/.playwright/scripts/fixtures/mobile-desktop.mjs new file mode 100644 index 000000000..d12d6ca22 --- /dev/null +++ b/.playwright/scripts/fixtures/mobile-desktop.mjs @@ -0,0 +1,18 @@ +import { createRequire, isBuiltin } from 'node:module' +import path from 'node:path' +import { fileURLToPath, pathToFileURL } from 'node:url' + +const root = fileURLToPath(new URL('../../../', import.meta.url)) + +export async function prepareMobileDesktopProfile(home, output) { + const requireBuddy = createRequire(path.join(root, 'apps/buddy/package.json')) + const { build } = await import(pathToFileURL(requireBuddy.resolve('vite')).href) + await build({ configFile: false, logLevel: 'error', build: { + ssr: path.join(root, '.playwright/scripts/__tests__/prepareProductProfile.ts'), + outDir: output, + target: 'node26', + rolldownOptions: { external: isBuiltin, output: { entryFileNames: 'prepare.mjs', format: 'es' } }, + } }) + const { prepareProductProfile } = await import(pathToFileURL(path.join(output, 'prepare.mjs')).href) + prepareProductProfile(home) +} diff --git a/.playwright/scripts/fixtures/runtime-network-utility.mjs b/.playwright/scripts/fixtures/runtime-network-utility.mjs index c9b757fe8..ca113f51f 100644 --- a/.playwright/scripts/fixtures/runtime-network-utility.mjs +++ b/.playwright/scripts/fixtures/runtime-network-utility.mjs @@ -1,9 +1,10 @@ import process from 'node:process' import { setTimeout as delay } from 'node:timers/promises' -import { startRuntimeNetwork } from '../../../apps/buddy/service/src/network/runtimeNetwork.ts' +import { RuntimeNetwork } from '../../../apps/buddy/service/src/network/runtimeNetwork.ts' async function run() { - const stop = startRuntimeNetwork() + const network = new RuntimeNetwork() + await network.replace({ httpProxy: process.env.HTTP_PROXY, httpsProxy: process.env.HTTPS_PROXY, noProxy: '' }) const start = performance.now() const before = process.cpuUsage() let failure @@ -49,7 +50,7 @@ async function run() { catch (error) { cancelled = error.name === 'AbortError' } return { versions: { electron: process.versions.electron, node: process.versions.node, undici: process.versions.undici }, failure, settleMs: settled - start, totalCpuMs: (total.user + total.system) / 1000, idleCpuMs: (idleUsage.user + idleUsage.system) / 1000, stream, responseCompatible: response instanceof Response, websocket, cancelled } } - finally { await stop() } + finally { await network.dispose() } } run().then(result => process.parentPort.postMessage(result), error => process.parentPort.postMessage({ error: error.message })) diff --git a/.playwright/scripts/fixtures/service-process-harness.ts b/.playwright/scripts/fixtures/service-process-harness.ts new file mode 100644 index 000000000..39fbfc2e3 --- /dev/null +++ b/.playwright/scripts/fixtures/service-process-harness.ts @@ -0,0 +1,62 @@ +import type { RuntimeRpcPeerContract } from '../../../apps/buddy/shared/runtime/rpcPeer' +import { openBuddyMigrationCandidate } from '../../../apps/buddy/service/src/storage/database' +import { extensionAgentRpc } from '../../../apps/buddy/shared/extensions/extensionAgent' +import { extensionCapabilitiesRpc } from '../../../apps/buddy/shared/extensions/extensionCapabilities' +import { extensionChatRpc } from '../../../apps/buddy/shared/extensions/extensionChat' +import { extensionImageRpc } from '../../../apps/buddy/shared/extensions/extensionImage' +import { extensionImageReadingRpc } from '../../../apps/buddy/shared/extensions/extensionImageReading' +import { extensionShellRpc } from '../../../apps/buddy/shared/extensions/extensionShell' +import { extensionSkillsRpc } from '../../../apps/buddy/shared/extensions/extensionSkills' +import { imageRpc } from '../../../apps/buddy/shared/images/imagePreferences' +import { DEFAULT_NETWORK_PERMISSIONS } from '../../../apps/buddy/shared/permissions/networkPermissions' +import { networkPermissionsRpc } from '../../../apps/buddy/shared/permissions/networkPermissionsRpc' +import { HOST_SPEECH_MODELS } from '../../../apps/buddy/shared/speech/speechApi' + +export { RuntimeRpcPeer } from '../../../apps/buddy/platform/ipc/runtimeRpcPeer' +export { createBuddyServiceEnvironment } from '../../../apps/buddy/platform/process/serviceEnvironment' +export { DEFAULT_RUNTIME_PREFERENCES } from '../../../apps/buddy/shared/runtime/runtimePreferences' +export { BUDDY_SERVICE_PROTOCOL_VERSION } from '../../../apps/buddy/shared/runtime/runtimeProtocol' + +export function registerFixtureHost(peer: RuntimeRpcPeerContract, options: { secrets?: Map, persistSecrets?: () => Promise } = {}): void { + const credentials = new Map([['process-fixture', { type: 'api_key', key: 'process-fixture-key' }]]) + peer.onRequest('host.credentials.list', () => ({ ok: true, providers: [...credentials.keys()].map(providerId => ({ providerId, type: 'api_key' })) })) + peer.onRequest('host.credentials.read', raw => ({ ok: true, value: credentials.get((raw as { providerId: string }).providerId) ?? null })) + peer.onRequest('host.credentials.write', (raw) => { + const { providerId, credential } = raw as { providerId: string, credential: unknown } + credentials.set(providerId, credential) + return { ok: true } + }) + peer.onRequest('host.credentials.delete', (raw) => { + credentials.delete((raw as { providerId: string }).providerId) + return { ok: true } + }) + const secrets = options.secrets ?? new Map() + peer.onRequest('host.secrets.read', (raw) => { + const { namespace, id } = raw as { namespace: string, id: string } + return { ok: true, value: secrets.get(`${namespace}:${id}`) ?? null } + }) + peer.onRequest('host.secrets.write', async (raw) => { + const { namespace, id, value } = raw as { namespace: string, id: string, value: unknown } + secrets.set(`${namespace}:${id}`, value) + await options.persistSecrets?.() + return { ok: true } + }) + peer.onRequest('host.secrets.delete', async (raw) => { + const { namespace, id } = raw as { namespace: string, id: string } + secrets.delete(`${namespace}:${id}`) + await options.persistSecrets?.() + return { ok: true } + }) + for (const method of [extensionAgentRpc.list, extensionCapabilitiesRpc.list, extensionChatRpc.list, extensionImageRpc.list, extensionImageReadingRpc.list, extensionShellRpc.list, extensionSkillsRpc.list, HOST_SPEECH_MODELS]) + peer.onRequest(method, () => []) + peer.onRequest(imageRpc.preferences, () => ({})) + peer.onRequest(networkPermissionsRpc.get, () => structuredClone(DEFAULT_NETWORK_PERMISSIONS)) +} + +export function createCandidateFixtureDatabase(path: string): void { + const database = openBuddyMigrationCandidate({ databasePath: path }) + try { + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + } + finally { database.close() } +} diff --git a/.playwright/scripts/fixtures/task-archive.mjs b/.playwright/scripts/fixtures/task-archive.mjs index 091a76324..57edf8ea5 100644 --- a/.playwright/scripts/fixtures/task-archive.mjs +++ b/.playwright/scripts/fixtures/task-archive.mjs @@ -7,8 +7,8 @@ import { fileURLToPath, pathToFileURL } from 'node:url' const root = fileURLToPath(new URL('../../../', import.meta.url)) -export async function prepareTaskArchiveProfile(home, output, authority = 'legacy_jsonl', { legacyTasks = 1, legacyFiles = 1 } = {}) { - assert.equal(authority, 'legacy_jsonl') +export async function prepareTaskArchiveProfile(home, output, authority, { legacyTasks = 1, legacyFiles = 1 } = {}) { + assert.ok(['legacy_jsonl', 'sqlite'].includes(authority), 'Archive fixture requires an explicit storage authority') assert(Number.isInteger(legacyTasks) && legacyTasks >= 1 && legacyTasks <= 100) assert(Number.isInteger(legacyFiles) && legacyFiles >= 1 && legacyFiles <= 64) const legacyTaskIds = Array.from({ length: legacyTasks }, (_, index) => index ? `archive-legacy-${index}` : 'archive-legacy') @@ -20,32 +20,28 @@ export async function prepareTaskArchiveProfile(home, output, authority = 'legac target: 'node26', rolldownOptions: { external: isBuiltin, output: { entryFileNames: 'prepare.mjs', format: 'es' } }, } }) - const { openBuddyDatabase } = await import(pathToFileURL(path.join(output, 'prepare.mjs')).href) + const { openBuddyDatabase, openBuddySqliteDatabase } = await import(pathToFileURL(path.join(output, 'prepare.mjs')).href) const databasePath = path.join(home, 'buddy/buddy.sqlite3') - const database = openBuddyDatabase({ databasePath }) + const open = authority === 'legacy_jsonl' ? openBuddyDatabase : openBuddySqliteDatabase + const database = open({ databasePath }) try { - assert.equal(database.prepare('PRAGMA user_version').get().user_version, 42) - assert.deepEqual(database.prepare('SELECT name FROM sqlite_master WHERE type = \'table\' AND name IN (\'storage_profile\', \'managed_files\') ORDER BY name').all().map(row => row.name), []) + assert.equal(database.prepare('PRAGMA user_version').get().user_version, authority === 'legacy_jsonl' ? 42 : 43) + assert.deepEqual(database.prepare('SELECT name FROM sqlite_master WHERE type = \'table\' AND name IN (\'storage_profile\', \'managed_files\') ORDER BY name').all().map(row => row.name), authority === 'legacy_jsonl' ? [] : ['managed_files', 'storage_profile']) + if (authority === 'sqlite') + assert.equal(database.prepare('SELECT authority FROM storage_profile WHERE id = 1').get().authority, 'sqlite') const now = new Date().toISOString() - for (const [id, name, revoked] of [['archive-space-design', '设计空间', null], ['archive-space-old', '历史研究空间', now]]) - database.prepare('INSERT INTO spaces (id, name, memory_scope, revoked_at, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)').run(id, name, 'space_only', revoked, now, now) const tasks = [ ['archive-active', '可归档的历史任务', null, null], ['archive-existing', '已经归档的历史任务', now, null], - ['archive-notes', '整理桌面工作台交互细节与后续改进记录,保留完整上下文供下次继续', now, null, 'archive-space-design'], - ['archive-review', '检查本地数据的保留范围与恢复流程', now, null, 'archive-space-old'], - ['archive-space-active', '设计空间中的活动任务', null, null, 'archive-space-design'], - ['archive-failed', '清理失败的历史任务', now, now], ...legacyTaskIds.map((id, index) => [id, `模拟旧版已移除任务 ${index + 1}`, null, now]), ] - for (const [id, title, archived, deleted, spaceId] of tasks) { - database.prepare('INSERT INTO conversations (id, title, buddy_id, active_branch_id, created_at, updated_at, archived_at, deleted_at, space_id) VALUES (?, ?, (SELECT default_buddy_id FROM buddy_catalog WHERE id = 1), ?, ?, ?, ?, ?, ?)').run(id, title, `branch-${id}`, now, now, archived, deleted, spaceId ?? null) + for (const [id, title, archived, deleted] of tasks) { + database.prepare('INSERT INTO conversations (id, title, buddy_id, active_branch_id, created_at, updated_at, archived_at, deleted_at) VALUES (?, ?, (SELECT default_buddy_id FROM buddy_catalog WHERE id = 1), ?, ?, ?, ?, ?)').run(id, title, `branch-${id}`, now, now, archived, deleted) database.prepare('INSERT INTO conversation_members (conversation_id, actor_id, joined_at) SELECT ?, default_buddy_id, ? FROM buddy_catalog WHERE id = 1').run(id, now) database.prepare('INSERT INTO task_attention (conversation_id) VALUES (?)').run(id) database.prepare('INSERT INTO conversation_branches (id, conversation_id, created_at) VALUES (?, ?, ?)').run(`branch-${id}`, id, now) database.prepare('INSERT INTO messages (id, conversation_id, branch_id, role, content_json, created_at) VALUES (?, ?, ?, ?, ?, ?)').run(`message-${id}`, id, `branch-${id}`, 'user', JSON.stringify({ text: `保留的历史内容 ${id}` }), now) } - database.prepare('INSERT INTO conversation_deletion_jobs (conversation_id, status, error_code, created_at, updated_at) VALUES (\'archive-failed\', \'failed\', \'FILE_ACCESS_DENIED\', ?, ?)').run(now, now) } finally { database.close() } const owned = path.join(home, 'buddy/conversations/archive-legacy/workspace') @@ -56,32 +52,13 @@ export async function prepareTaskArchiveProfile(home, output, authority = 'legac await Promise.all(Array.from({ length: legacyFiles }, (_, index) => fs.writeFile(path.join(directory, index ? `owned-${index}.txt` : 'owned.txt'), 'Task-owned fixture'))) } const external = path.join(home, 'external') - for (const directory of [owned, activeOwned]) { - await fs.mkdir(directory, { recursive: true }) - await fs.writeFile(path.join(directory, 'owned.txt'), 'Task-owned fixture') - } + await fs.mkdir(activeOwned, { recursive: true }) await fs.mkdir(external, { recursive: true }) + await fs.writeFile(path.join(activeOwned, 'owned.txt'), 'Task-owned fixture') await fs.writeFile(path.join(external, 'keep.txt'), 'External fixture must survive') return { databasePath, owned, activeOwned, external, legacyTaskIds } } -export async function readTaskArchiveStorage(app, databasePath) { - return app.evaluate(({ app }, databasePath) => { - if (app.getName() !== 'Lexora Buddy Test') - throw new Error('Archive assertions require an isolated test profile') - const { DatabaseSync } = process.getBuiltinModule('node:sqlite') - const database = new DatabaseSync(databasePath, { readOnly: true }) - try { - const tables = database.prepare('SELECT name FROM sqlite_master WHERE type = \'table\' AND name IN (\'storage_profile\', \'managed_files\') ORDER BY name').all().map(row => row.name) - return { - version: database.prepare('PRAGMA user_version').get().user_version, - tables, - } - } - finally { database.close() } - }, databasePath) -} - export async function taskExists(app, databasePath, id) { return app.evaluate(({ app }, { databasePath, id }) => { if (app.getName() !== 'Lexora Buddy Test') diff --git a/.playwright/scripts/mobile/__tests__/process-recovery.e2e.mjs b/.playwright/scripts/mobile/__tests__/process-recovery.e2e.mjs new file mode 100644 index 000000000..b0a46d72d --- /dev/null +++ b/.playwright/scripts/mobile/__tests__/process-recovery.e2e.mjs @@ -0,0 +1,60 @@ +import assert from 'node:assert/strict' +import { execFile } from 'node:child_process' +import { randomUUID } from 'node:crypto' +import { mkdir, writeFile } from 'node:fs/promises' +import { join } from 'node:path' +import process from 'node:process' +import { setTimeout } from 'node:timers/promises' +import { promisify } from 'node:util' + +const execute = promisify(execFile) +const args = new Map(process.argv.slice(2).reduce((entries, value, index, values) => value.startsWith('--') ? [...entries, [value.slice(2), values[index + 1]]] : entries, [])) +for (const key of ['serial', 'output']) assert(args.get(key), `Missing --${key}`) +const output = args.get('output') +await mkdir(output, { recursive: true }) +const packageName = 'app.uselexora.mobile.dev' +const token = randomUUID() +const marker = `files/process-recovery-${token}.json` +const adb = (...parameters) => execute(args.get('adb') ?? 'adb', ['-s', args.get('serial'), ...parameters], { timeout: 60000, maxBuffer: 4 * 1024 * 1024 }) +const instrument = phase => adb('shell', 'am', 'instrument', '-w', '-r', '-e', 'class', 'app.uselexora.mobile.ProcessRecoveryInstrumentedTest', '-e', 'processRecoveryToken', token, '-e', 'processRecoveryPhase', phase, '-e', 'processRecoveryPairing', String(Boolean(args.get('pairing'))), `${packageName}.test/androidx.test.runner.AndroidJUnitRunner`) +let preparing +try { + if (args.get('pairing')) + await adb('push', args.get('pairing'), `/data/local/tmp/lexora-recovery-${token}.json`) + let preparationFinished = false + preparing = instrument('prepare').then(result => ({ result }), error => ({ error })).then(async (outcome) => { + await writeFile(join(output, 'preparation.txt'), outcome.result?.stdout ?? String(outcome.error)) + preparationFinished = true + return outcome + }) + let checkpoint + const deadline = Date.now() + 30000 + while (Date.now() < deadline) { + assert(!preparationFinished, 'Preparation ended before its checkpoint; see preparation.txt') + try { + checkpoint = JSON.parse((await adb('shell', 'run-as', packageName, 'cat', marker)).stdout) + break + } + catch {} + await setTimeout(200) + } + assert(checkpoint?.pid > 0, 'Preparation did not persist its checkpoint') + assert.equal((await adb('shell', 'pidof', packageName)).stdout.trim(), String(checkpoint.pid)) + await adb('shell', 'am', 'force-stop', packageName) + const interrupted = await preparing + await writeFile(join(output, 'interrupted.txt'), interrupted.result?.stdout ?? String(interrupted.error)) + assert.match(interrupted.result?.stdout ?? '', /Process crashed|INSTRUMENTATION_FAILED|shortMsg=Process crashed/, 'Instrumentation was not interrupted by process termination') + const recovered = await instrument('verify') + await writeFile(join(output, 'recovered.txt'), recovered.stdout) + assert.match(recovered.stdout, /OK \(1 test\)/) + assert.doesNotMatch(recovered.stdout, /INSTRUMENTATION_STATUS_CODE: -[234]|FAILURES/) + const screenshot = await execute(args.get('adb') ?? 'adb', ['-s', args.get('serial'), 'exec-out', 'run-as', packageName, 'cat', `files/process-recovery-${token}.png`], { encoding: 'buffer', maxBuffer: 8 * 1024 * 1024 }) + await writeFile(join(output, 'android-recovered.png'), screenshot.stdout) + await writeFile(join(output, 'result.json'), JSON.stringify({ passed: true, action: 'am force-stop', terminatedPid: checkpoint.pid, inputPreserved: true, nativeDraftRestored: true, currentInputPreserved: true, executionSettingsPreserved: true, originalCommandReconciled: true, serverPrefixResumed: true, duplicateAdoptionPrevented: true }, null, 2)) +} +finally { + await adb('shell', 'am', 'force-stop', packageName) + await preparing + await adb('shell', 'rm', '-f', `/data/local/tmp/lexora-recovery-${token}.json`) + await adb('shell', 'run-as', packageName, 'rm', '-f', marker, `files/process-recovery-${token}.png`) +} diff --git a/.playwright/scripts/mobile/capture-android-performance.mjs b/.playwright/scripts/mobile/capture-android-performance.mjs new file mode 100644 index 000000000..37f119a38 --- /dev/null +++ b/.playwright/scripts/mobile/capture-android-performance.mjs @@ -0,0 +1,118 @@ +import assert from 'node:assert/strict' +import { execFile } from 'node:child_process' +import { createHash, randomUUID } from 'node:crypto' +import { mkdir, readFile, writeFile } from 'node:fs/promises' +import { join } from 'node:path' +import process from 'node:process' +import { promisify } from 'node:util' + +const execute = promisify(execFile) +const args = new Map(process.argv.slice(2).reduce((entries, value, index, values) => value.startsWith('--') ? [...entries, [value.slice(2), values[index + 1]]] : entries, [])) +for (const key of ['serial', 'package', 'activity', 'apk', 'output']) assert(args.get(key), `Missing --${key}`) +const output = args.get('output') +const recoveryMarker = `Recovery${randomUUID().replaceAll('-', '')}` +await mkdir(output, { recursive: true }) +const adb = args.get('adb') ?? 'adb' +const invoke = async (...parameters) => (await execute(adb, ['-s', args.get('serial'), ...parameters], { maxBuffer: 8 * 1024 * 1024 })).stdout +const shell = (...parameters) => invoke('shell', ...parameters) +const start = () => shell('am', 'start', '-W', '-n', `${args.get('package')}/${args.get('activity')}`, ...(args.has('fixture') ? ['--ez', 'fixture', 'true'] : [])) +async function dump() { + await shell('uiautomator', 'dump', '/sdcard/lexora-acceptance.xml') + const xml = await shell('cat', '/sdcard/lexora-acceptance.xml') + assert(xml.includes(`package="${args.get('package')}"`), 'Another application owns the focused UI') + return xml +} +const bounds = node => [...node.matchAll(/bounds="\[(\d+),(\d+)\]\[(\d+),(\d+)\]"/g)][0]?.slice(1).map(Number) +const center = value => [Math.round((value[0] + value[2]) / 2), Math.round((value[1] + value[3]) / 2)] +const device = { + emulator: (await shell('getprop', 'ro.kernel.qemu')).trim() === '1', + model: (await shell('getprop', 'ro.product.model')).trim(), + android: (await shell('getprop', 'ro.build.version.release')).trim(), + api: Number((await shell('getprop', 'ro.build.version.sdk')).trim()), + abi: (await shell('getprop', 'ro.product.cpu.abi')).trim(), + display: (await shell('wm', 'size')).trim(), + density: (await shell('wm', 'density')).trim(), +} +const budgets = { environment: device.emulator ? 'emulator' : 'physical', coldStartP95Ms: 2500, frameP95Ms: 64, pssKiB: 262144, apkBytes: 40 * 1024 * 1024 } +await writeFile(join(output, 'device.json'), JSON.stringify(device, null, 2)) +await writeFile(join(output, 'budgets.json'), JSON.stringify({ ...budgets, frozenAt: new Date().toISOString() }, null, 2)) +await invoke('install', '-r', args.get('apk')) +const times = [] +for (let index = 0; index < 5; index++) { + await shell('am', 'force-stop', args.get('package')) + const result = await start() + await writeFile(join(output, `cold-start-${index + 1}.txt`), result) + const value = /TotalTime:\s*(\d+)/.exec(result) + assert(value, 'No successful Activity startup measurement') + times.push(Number(value[1])) + assert((await shell('pidof', args.get('package'))).trim(), 'Application exited during startup') + const xml = await dump() + assert(xml.includes('Lexora'), 'Application did not render its interface') +} +async function openTasks() { + let xml = await dump() + const search = xml.match(/]+content-desc="搜索任务"[^>]*>/)?.[0] + if (search) { + await shell('input', 'tap', ...center(bounds(search)).map(String)) + xml = await dump() + } + return xml +} +async function openFixtureTask() { + const xml = await openTasks() + const task = xml.match(/]+(?:text|content-desc)="任务 1 ·[^>]*>/)?.[0] + assert(task, 'First fixture task unavailable') + await shell('input', 'tap', ...center(bounds(task)).map(String)) + return dump() +} +await writeFile(join(output, 'initial.xml'), await openTasks()) +await shell('dumpsys', 'gfxinfo', args.get('package'), 'reset') +const taskList = (await dump()).match(/]+scrollable="true"[^>]*>/)?.[0] +assert(taskList, 'Task list unavailable') +const [left, top, right, bottom] = bounds(taskList) +const x = Math.round((left + right) / 2) +const upper = Math.round(top + (bottom - top) * 0.2) +const lower = Math.round(top + (bottom - top) * 0.8) +for (let index = 0; index < 10; index++) await shell('input', 'swipe', String(x), String(index % 2 ? upper : lower), String(x), String(index % 2 ? lower : upper), '350') +const frames = await shell('dumpsys', 'gfxinfo', args.get('package'), 'framestats') +const memory = await shell('dumpsys', 'meminfo', args.get('package')) +const flutterTimings = args.get('renderer') === 'flutter' ? await shell('cat', `/sdcard/Android/data/${args.get('package')}/files/frame-times.json`) : null +await writeFile(join(output, 'frames.txt'), frames) +await writeFile(join(output, 'memory.txt'), memory) +const editing = await openFixtureTask() +const input = editing.match(/]+class="android.widget.EditText"[^>]*>/)?.[0] +assert(input, 'Native text input unavailable') +await shell('input', 'tap', ...center(bounds(input)).map(String)) +await shell('input', 'keyevent', 'KEYCODE_MOVE_END') +await shell('input', 'text', recoveryMarker) +const keyboard = await shell('dumpsys', 'input_method') +if (/mInputShown=true|mIsInputViewShown=true/.test(keyboard)) + await shell('input', 'keyevent', 'KEYCODE_BACK') +assert((await dump()).includes(recoveryMarker), 'Typed input missing') +await shell('am', 'force-stop', args.get('package')) +await start() +let recovered = await dump() +if (!recovered.includes(recoveryMarker)) + recovered = await openFixtureTask() +assert(recovered.includes(recoveryMarker), 'Process death lost the locally saved draft') +await writeFile(join(output, 'recovered.xml'), recovered) +const screenshot = await execute(adb, ['-s', args.get('serial'), 'exec-out', 'screencap', '-p'], { encoding: 'buffer', maxBuffer: 8 * 1024 * 1024 }) +await writeFile(join(output, '01-input-recovered.png'), screenshot.stdout) +const apk = await readFile(args.get('apk')) +const p95 = times.toSorted((a, b) => a - b).at(-1) +let frameP95 = Number(/95th percentile:\s*(\d+)ms/.exec(frames)?.[1] ?? NaN) +let frameMetric = 'Android HWUI' +if (args.get('renderer') === 'flutter') { + const timings = flutterTimings + await writeFile(join(output, 'flutter-frame-times.json'), timings) + const values = JSON.parse(timings).map(value => value / 1000).sort((a, b) => a - b) + assert(values.length >= 30, 'Insufficient Flutter FrameTiming samples') + frameP95 = values[Math.ceil(values.length * 0.95) - 1] + frameMetric = 'Flutter FrameTiming totalSpan (separate renderer instrumentation)' +} +const pss = Number(/^\s*TOTAL(?: PSS)?:\s*(\d+)/m.exec(memory)?.[1] ?? NaN) +const result = { package: args.get('package'), apkSha256: createHash('sha256').update(apk).digest('hex'), apkBytes: apk.length, coldStartMs: times, coldStartP95Ms: p95, frameP95Ms: frameP95, frameMetric, pssKiB: pss, recovery: 'passed', emulatorOnly: device.emulator, device, scope: 'Five cold starts, 1000-task list scrolling, main-process memory snapshot and draft recovery; excludes WebView process memory, sustained streaming, peak memory and energy', budgets, pass: p95 <= budgets.coldStartP95Ms && frameP95 <= budgets.frameP95Ms && pss <= budgets.pssKiB && apk.length <= budgets.apkBytes } +await writeFile(join(output, 'measurements.json'), JSON.stringify(result, null, 2)) +await shell('rm', '-f', '/sdcard/lexora-acceptance.xml') +process.stdout.write(`${JSON.stringify(result)}\n`) +assert(result.pass, 'Android baseline performance budget exceeded') diff --git a/.playwright/scripts/mobile/capture-android-streaming.mjs b/.playwright/scripts/mobile/capture-android-streaming.mjs new file mode 100644 index 000000000..ebf52ded5 --- /dev/null +++ b/.playwright/scripts/mobile/capture-android-streaming.mjs @@ -0,0 +1,88 @@ +import assert from 'node:assert/strict' +import { execFile } from 'node:child_process' +import { createHash } from 'node:crypto' +import { mkdir, readFile, writeFile } from 'node:fs/promises' +import path from 'node:path' +import process from 'node:process' +import { setTimeout as delay } from 'node:timers/promises' +import { promisify } from 'node:util' + +const execute = promisify(execFile) +const args = new Map(process.argv.slice(2).reduce((entries, value, index, values) => value.startsWith('--') ? [...entries, [value.slice(2), values[index + 1]]] : entries, [])) +for (const key of ['serial', 'package', 'apk', 'output']) assert(args.get(key), `Missing --${key}`) +const appPackage = args.get('package') +assert(appPackage.endsWith('.benchmark'), 'Streaming measurement requires the isolated benchmark application') +const seconds = Number(args.get('seconds') ?? 60) +assert(Number.isInteger(seconds) && seconds >= 10 && seconds <= 180) +const output = args.get('output') +await mkdir(output, { recursive: true }) +const adb = (...parameters) => execute(args.get('adb') ?? 'adb', ['-s', args.get('serial'), ...parameters], { maxBuffer: 8 * 1024 * 1024, timeout: 20000 }) +const shell = async (...parameters) => (await adb('shell', ...parameters)).stdout +const apkSha256 = createHash('sha256').update(await readFile(args.get('apk'))).digest('hex') +const installed = (await shell('pm', 'path', appPackage)).trim().replace(/^package:/, '') +assert((await shell('sha256sum', installed)).startsWith(apkSha256), 'Installed APK does not match the supplied artifact') +const budgets = { rafFrameP95Ms: 64, totalPssKiB: 262144, minimumFrameSamples: 100 } +await writeFile(path.join(output, 'budgets.json'), JSON.stringify({ ...budgets, frozenAt: new Date().toISOString() }, null, 2)) +const remoteResult = `/sdcard/Android/data/${appPackage}/files/reader-benchmark.json` +await shell('am', 'force-stop', appPackage) +await shell('rm', '-f', remoteResult) +const start = await shell('am', 'start', '-W', '-n', `${appPackage}/app.uselexora.mobile.ui.MainActivity`, '--ez', 'fixture', 'true', '--ei', 'readerBenchmarkSeconds', String(seconds)) +await writeFile(path.join(output, 'start.txt'), start) +const samples = [] +let metrics +let completedAt +const started = Date.now() +while (Date.now() - started < (seconds * 2 + 40) * 1000) { + const main = (await shell('pidof', appPackage)).trim().split(/\s+/).filter(Boolean) + assert(main.length, 'Benchmark application exited') + const services = await shell('dumpsys', 'activity', 'services') + const processes = new Map(main.map(pid => [pid, appPackage])) + for (const block of services.split(/(?= {2}\* ServiceRecord\{)/)) { + if (!block.includes('SandboxedProcessService') || !block.includes(appPackage)) + continue + const linked = /app=ProcessRecord\{[^ ]+ (\d+):([^/}]+)/.exec(block) + if (linked) + processes.set(linked[1], linked[2]) + } + const memory = [] + for (const [pid, name] of processes) { + const value = await shell('dumpsys', 'meminfo', pid) + const pssKiB = Number(/^\s*TOTAL(?: PSS)?:\s*(\d+)/m.exec(value)?.[1]) + assert(Number.isFinite(pssKiB), `Missing process memory: ${name}`) + memory.push({ pid, name, pssKiB }) + } + const battery = await shell('dumpsys', 'battery') + samples.push({ elapsedMs: Date.now() - started, processes: memory, totalPssKiB: memory.reduce((sum, item) => sum + item.pssKiB, 0), battery }) + metrics ??= await shell('cat', remoteResult).then(value => JSON.parse(value), () => null) + if (metrics) { + completedAt ??= Date.now() + if (Date.now() - completedAt >= 10000) + break + } + await delay(2000) +} +const peakPssKiB = Math.max(...samples.map(sample => sample.totalPssKiB)) +const device = { + model: (await shell('getprop', 'ro.product.model')).trim(), + api: Number((await shell('getprop', 'ro.build.version.sdk')).trim()), + emulator: (await shell('getprop', 'ro.kernel.qemu')).trim() === '1', +} +const result = { + device, + package: appPackage, + apkSha256, + metrics, + peakPssKiB, + settledPssKiB: samples.at(-1)?.totalPssKiB, + budgets, + samples, + energy: { measured: false, reason: 'USB power and charge state are recorded; these are not a battery-discharge energy measurement' }, + scope: 'Foreground 500-message history and sustained output; total PSS includes WebView processes explicitly bound to this application', + passed: Boolean(metrics?.completed && metrics.frameSamples >= budgets.minimumFrameSamples && metrics.rafFrameP95Ms <= budgets.rafFrameP95Ms && peakPssKiB <= budgets.totalPssKiB && samples.some(sample => sample.processes.length > 1)), +} +await writeFile(path.join(output, 'measurements.json'), JSON.stringify(result, null, 2)) +const image = await execute(args.get('adb') ?? 'adb', ['-s', args.get('serial'), 'exec-out', 'screencap', '-p'], { encoding: 'buffer', maxBuffer: 8 * 1024 * 1024 }) +assert(image.stdout.length > 0, 'No visible device screenshot') +await writeFile(path.join(output, 'stream-completed.png'), image.stdout) +process.stdout.write(`${JSON.stringify({ ...result, samples: result.samples.length })}\n`) +assert(result.passed, 'Streaming or complete process memory budget exceeded') diff --git a/.playwright/scripts/private-network-fixture.mjs b/.playwright/scripts/private-network-fixture.mjs new file mode 100644 index 000000000..2ad55776d --- /dev/null +++ b/.playwright/scripts/private-network-fixture.mjs @@ -0,0 +1,135 @@ +import { Buffer } from 'node:buffer' +import { fork } from 'node:child_process' +import { once } from 'node:events' +import * as fs from 'node:fs/promises' +import { createServer } from 'node:http' +import { createRequire, isBuiltin } from 'node:module' +import os from 'node:os' +import path from 'node:path' +import process from 'node:process' +import { fileURLToPath, pathToFileURL } from 'node:url' + +export async function main() { + const root = fileURLToPath(new URL('../../', import.meta.url)) + const buddy = path.join(root, 'apps/buddy') + const requireBuddy = createRequire(path.join(buddy, 'package.json')) + const keep = Boolean(process.env.LEXORA_FIXTURE_DIRECTORY) + const directory = keep ? path.resolve(process.env.LEXORA_FIXTURE_DIRECTORY) : await fs.mkdtemp(path.join(os.tmpdir(), 'lexora-native-network-')) + await fs.mkdir(directory, { recursive: true, mode: 0o700 }) + const profile = path.join(directory, 'profile') + const qrPath = path.join(directory, 'pairing.json') + const endpointPath = path.join(directory, 'connection.json') + const port = Number(process.env.LEXORA_FIXTURE_PORT ?? 19443) + const candidate = process.env.LEXORA_FIXTURE_SERVICE ? path.resolve(process.env.LEXORA_FIXTURE_SERVICE) : path.join(buddy, '.output/build/service') + const { build } = await import(pathToFileURL(requireBuddy.resolve('vite')).href) + await build({ configFile: false, logLevel: 'error', build: { ssr: path.join(root, '.playwright/scripts/fixtures/service-process-harness.ts'), outDir: path.join(directory, 'harness'), target: 'node26', rolldownOptions: { external: isBuiltin, output: { entryFileNames: 'harness.mjs', format: 'es' } } } }) + const { RuntimeRpcPeer, createBuddyServiceEnvironment, BUDDY_SERVICE_PROTOCOL_VERSION, DEFAULT_RUNTIME_PREFERENCES, registerFixtureHost, createCandidateFixtureDatabase } = await import(pathToFileURL(path.join(directory, 'harness/harness.mjs')).href) + await fs.mkdir(profile, { recursive: true, mode: 0o700 }) + createCandidateFixtureDatabase(path.join(profile, 'buddy.sqlite3')) + const provider = createServer((request, response) => { + void (async () => { + let size = 0 + const chunks = [] + for await (const chunk of request) { + size += chunk.byteLength + chunks.push(chunk) + if (size > 1_048_576) + throw new Error('Fixture request exceeds limit') + } + const body = JSON.parse(Buffer.concat(chunks).toString('utf8')) + const approval = JSON.stringify(body.messages).includes('Android approval verification') && !body.messages.some(message => message.role === 'tool') + response.writeHead(200, { 'Content-Type': 'text/event-stream' }) + response.write(`data: ${JSON.stringify({ id: 'native-fixture', model: 'fixture-model', object: 'chat.completion.chunk', created: 1, choices: [{ index: 0, delta: approval ? { role: 'assistant', tool_calls: [{ index: 0, id: 'mobile-approval-call', type: 'function', function: { name: 'write', arguments: JSON.stringify({ path: 'mobile-approval.txt', content: JSON.stringify(body.messages).includes('large approval document') ? '中😀'.repeat(30000) : 'Isolated Android approval fixture' }) } }] } : { role: 'assistant', content: '隔离服务已收到输入。你好,Android。😀' }, finish_reason: null }] })}\n\n`) + response.write(`data: ${JSON.stringify({ id: 'native-fixture', model: 'fixture-model', object: 'chat.completion.chunk', created: 1, choices: [{ index: 0, delta: {}, finish_reason: approval ? 'tool_calls' : 'stop' }] })}\n\n`) + response.end('data: [DONE]\n\n') + })().catch(() => response.destroy()) + }) + provider.listen(0, '127.0.0.1') + await once(provider, 'listening') + const child = fork(path.join(candidate, 'service.mjs'), [], { execArgv: [], cwd: directory, env: createBuddyServiceEnvironment({ PATH: process.env.PATH, HOME: directory, TMPDIR: directory, TEMP: directory, SystemRoot: process.env.SystemRoot, COMSPEC: process.env.COMSPEC }, profile), stdio: ['ignore', 'ignore', 'pipe', 'ipc'] }) + const peers = new Map() + function channel(id) { + const peer = new RuntimeRpcPeer({ transport: { postMessage: message => child.send({ type: 'rpc', channel: id, message }), subscribe(listener) { + const receive = (message) => { + if (message.type === 'rpc' && message.channel === id) + listener(message.message) + } + child.on('message', receive) + return () => child.off('message', receive) + } } }) + peers.set(id, peer) + return peer + } + const secretsPath = path.join(directory, 'synthetic-secrets.json') + let secrets + try { + secrets = new Map(JSON.parse(await fs.readFile(secretsPath, 'utf8'))) + } + catch (error) { + if (error.code !== 'ENOENT') + throw error + secrets = new Map() + } + registerFixtureHost(channel('host'), { secrets, persistSecrets: async () => { + const temporary = `${secretsPath}.part` + await fs.writeFile(temporary, JSON.stringify([...secrets]), { mode: 0o600, flush: true }) + await fs.rename(temporary, secretsPath) + } }) + let stopped = false + async function stop() { + if (stopped) + return + stopped = true + const exited = child.exitCode === null && child.signalCode === null ? once(child, 'exit') : Promise.resolve() + child.kill('SIGTERM') + await exited + for (const peer of peers.values()) peer.close(new Error('Fixture stopped')) + provider.closeAllConnections() + provider.close() + if (!keep) + await fs.rm(directory, { recursive: true, force: true }) + } + process.once('SIGINT', () => void stop()) + process.once('SIGTERM', () => void stop()) + child.once('exit', () => void stop()) + child.stderr.on('data', value => process.stderr.write(value)) + const ready = new Promise((resolve, reject) => { + child.on('message', (message) => { + if (message.type === 'ready') + resolve() + if (message.type === 'failed') + reject(new Error(message.code)) + }) + child.once('exit', () => reject(new Error('Fixture service exited before ready'))) + }) + child.send({ type: 'start', protocolVersion: BUDDY_SERVICE_PROTOCOL_VERSION, config: { profile: 'test', buddyHome: profile, resourcesRoot: path.join(candidate, 'resources'), storageAuthority: 'sqlite', proxy: { httpProxy: '', httpsProxy: '', noProxy: '' }, preferences: { ...DEFAULT_RUNTIME_PREFERENCES, cacheWarming: 'off', codemode: 'off', modelRetryLimit: 0 }, display: false } }) + await ready + const management = channel('management') + child.send({ type: 'connect', channel: 'management', role: 'management' }) + await management.request('providers.upsertCustom', { id: 'process-fixture', displayName: 'Isolated Android fixture', api: 'openai-completions', baseUrl: `http://127.0.0.1:${provider.address().port}/v1`, enabled: true, models: [{ id: 'fixture-model', name: 'Fixture', input: ['text'], reasoning: false, contextWindow: 128000, maxTokens: 1024 }] }) + const product = channel('product') + child.send({ type: 'connect', channel: 'product', role: 'product' }) + const spaces = await product.request('spaces.list', {}) + if (!spaces.some(space => space.name === 'Android verification')) + await management.request('spaces.create', { name: 'Android verification', memoryScope: 'space_only', primaryDirectory: null, primaryDirectorySelectionVerified: false }) + let status = await management.request('privateNetwork.configure', { enabled: true, bindAddress: '127.0.0.1', port }) + if (process.env.LEXORA_FIXTURE_RESET_TLS === '1') + status = await management.request('privateNetwork.resetIdentity', {}) + if (status.state !== 'listening') + throw new Error(`Fixture listener failed: ${status.errorCode}`) + await fs.writeFile(endpointPath, JSON.stringify(status), { mode: 0o600 }) + async function issue() { + const qr = await management.request('privateNetwork.issuePairing', {}) + await fs.writeFile(qrPath, JSON.stringify(qr), { mode: 0o600 }) + process.stdout.write(`${JSON.stringify({ endpoint: status.endpoint, qrPath, endpointPath, fixture: directory })}\n`) + } + await issue() + process.on('SIGUSR1', () => void issue()) +} + +if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + main().catch((error) => { + process.stderr.write(`${error instanceof Error ? error.message : 'FIXTURE_FAILED'}\n`) + process.exitCode = 1 + }) +} diff --git a/.playwright/scripts/storage-durability.mjs b/.playwright/scripts/storage-durability.mjs new file mode 100644 index 000000000..08a9c87ed --- /dev/null +++ b/.playwright/scripts/storage-durability.mjs @@ -0,0 +1,47 @@ +import assert from 'node:assert/strict' +import { createHash } from 'node:crypto' +import fs from 'node:fs/promises' +import { createRequire } from 'node:module' +import path from 'node:path' +import process from 'node:process' +import { fileURLToPath, pathToFileURL } from 'node:url' + +const workspace = fileURLToPath(new URL('../../', import.meta.url)) +const buddy = path.join(workspace, 'apps/buddy') +const requireBuddy = createRequire(path.join(buddy, 'package.json')) +const mode = process.argv[2] ?? 'cost' +assert(process.argv.length <= 3 && ['cost', 'crash', 'capacity'].includes(mode), 'Usage: node .playwright/scripts/storage-durability.mjs [cost|crash|capacity]') +const root = path.join(workspace, '.playwright/runs', `storage-durability-${new Date().toISOString().replaceAll(/[:.]/g, '-')}`) +await fs.mkdir(root, { recursive: true }) +await fs.symlink(path.join(buddy, 'node_modules'), path.join(root, 'node_modules'), 'junction') +try { + const { build } = await import(pathToFileURL(requireBuddy.resolve('vite'))) + await build({ + configFile: false, + logLevel: 'error', + build: { + ssr: fileURLToPath(new URL(`./__tests__/${mode === 'crash' ? 'storageCrash' : mode === 'capacity' ? 'commandCapacity' : 'storageDurability'}.ts`, import.meta.url)), + outDir: root, + emptyOutDir: false, + target: 'node26', + minify: false, + rollupOptions: { + external: id => !id.startsWith('.') && !path.isAbsolute(id) && !id.startsWith('\0'), + output: { entryFileNames: 'storage.mjs' }, + }, + }, + }) + const bundle = path.join(root, 'storage.mjs') + const bundleSha256 = createHash('sha256').update(await fs.readFile(bundle)).digest('hex') + const api = await import(pathToFileURL(bundle)) + if (mode === 'crash') + await api.runStorageCrashes(root, bundle, bundleSha256) + else if (mode === 'capacity') + await api.runCommandCapacity(root, bundleSha256) + else + await api.runStorageDurability(root, bundleSha256) +} +finally { + await fs.unlink(path.join(root, 'node_modules')) + process.stdout.write(`Storage durability artifacts: ${root}\n`) +} diff --git a/apps/buddy/electron-builder.config.cjs b/apps/buddy/electron-builder.config.cjs index c13116718..84f3b9227 100644 --- a/apps/buddy/electron-builder.config.cjs +++ b/apps/buddy/electron-builder.config.cjs @@ -10,7 +10,11 @@ const { resolveInferenceEnvironment } = require('./shared/extensions/inferencePl const { OPERATING_SYSTEM } = require('./shared/platform/identifiers.ts') const target = resolveBuildTarget() -const resources = platformResources(target) +const resources = [ + ...platformResources(target), + { from: '.output/build/service', to: 'lexora-service' }, + { from: `.output/resources/node-runtime/${target.id}`, to: 'lexora-runtime' }, +] const packageFormat = process.env.LEXORA_BUDDY_PACKAGE_FORMAT ?? target.packageFormats[0] const { version, sourceDateEpoch } = require('./buddy.version.json') const { desktopName, productName: displayName } = require('./package.json') @@ -29,6 +33,11 @@ const signedMacos = macosSigningMode() === 'developer-id' module.exports = withDesktopNativeVersion({ appId: desktopName, + afterPack: async context => (await import('../../packaging/buddy/release/product-release-set.mjs')).writePackagedReleaseSet(context), + afterSign: async (context) => { + if (target.platform === OPERATING_SYSTEM.Windows) + await (await import('../../packaging/buddy/release/product-release-set.mjs')).writePackagedReleaseSet(context) + }, extraMetadata: { desktopDistribution: { schemaVersion: 1, format: packageFormat === 'macos-zip' ? 'dmg' : packageFormat, architecture: target.architecture, signing: target.platform === OPERATING_SYSTEM.MacOS ? (signedMacos ? 'developer-id' : 'ad-hoc') : 'none' }, }, @@ -58,6 +67,7 @@ module.exports = withDesktopNativeVersion({ ...excludedDependencyFiles(target), ], mac: { + sign: async (options, packager) => (await import('../../packaging/buddy/release/product-release-set.mjs')).signMacosReleaseSet(options, packager), extraResources: (target.platform === OPERATING_SYSTEM.MacOS ? resources : []), target: [{ target: 'dmg', arch: ['arm64'] }, { target: 'zip', arch: ['arm64'] }], artifactName: target.platform === OPERATING_SYSTEM.MacOS ? desktopArtifactName(target, 'dmg', macro('version')).replace(/\.dmg$/, `.${macro('ext')}`) : undefined, @@ -73,7 +83,7 @@ module.exports = withDesktopNativeVersion({ binaries: (target.platform === OPERATING_SYSTEM.MacOS ? resources : []) .filter(resource => resource.to.startsWith('native-')) .map(resource => `Contents/Resources/${resource.to}`) - .concat(['Contents/Resources/search-tools/fd', 'Contents/Resources/search-tools/rg']), + .concat(['Contents/Resources/search-tools/fd', 'Contents/Resources/search-tools/rg', 'Contents/Resources/lexora-runtime/node']), }, dmg: { sign: signedMacos, diff --git a/apps/buddy/electron.vite.config.ts b/apps/buddy/electron.vite.config.ts index 46829b1dc..ba8e67630 100644 --- a/apps/buddy/electron.vite.config.ts +++ b/apps/buddy/electron.vite.config.ts @@ -39,11 +39,11 @@ export default defineConfig({ externalizeDeps: { exclude: ['typescript'] }, outDir: join(electronOutputRoot, 'main'), rollupOptions: { - external: ['electron', '@silvia-odwyer/photon-node', 'fflate/node', /^@earendil-works\/pi-/], + external: ['electron', 'original-fs', '@silvia-odwyer/photon-node', 'fflate/node', /^@earendil-works\/pi-/], input: { 'index': fileURLToPath(new URL('./electron/main/index.ts', import.meta.url)), 'storage-worker': fileURLToPath(new URL('./service/src/storage/storageWorker.ts', import.meta.url)), - 'buddy-service': fileURLToPath(new URL('./service/src/index.ts', import.meta.url)), + 'buddy-service': fileURLToPath(new URL('./electron/main/runtime/serviceProcess.ts', import.meta.url)), 'extension-package-worker': fileURLToPath(new URL('./electron/main/extensions/packageWorker.ts', import.meta.url)), }, output: { diff --git a/apps/buddy/electron/dev.mjs b/apps/buddy/electron/dev.mjs new file mode 100644 index 000000000..e491c02f9 --- /dev/null +++ b/apps/buddy/electron/dev.mjs @@ -0,0 +1,38 @@ +import { spawn } from 'node:child_process' +import { dirname, join } from 'node:path' +import process from 'node:process' +import { fileURLToPath } from 'node:url' +import { BUDDY_DEVELOPMENT_RESTART_EXIT_CODE } from '../shared/runtime/profile.ts' + +const cli = join(dirname(fileURLToPath(import.meta.resolve('electron-vite/package.json'))), 'bin/electron-vite.js') +let child +let stopping = false +for (const signal of ['SIGINT', 'SIGTERM', ...(process.platform === 'win32' ? [] : ['SIGHUP'])]) { + process.on(signal, () => { + stopping = true + child?.kill(signal) + }) +} +async function run() { + let restart = true + while (restart) { + if (stopping) + return + child = spawn(process.execPath, [cli, 'dev', ...process.argv.slice(2)], { + stdio: 'inherit', + env: { ...process.env, LEXORA_BUDDY_DEV_LAUNCHER: '1' }, + }) + const code = await new Promise((resolve, reject) => { + child.once('error', reject) + child.once('close', (code, signal) => resolve(code ?? (signal ? 1 : 0))) + }) + child = undefined + restart = !stopping && code === BUDDY_DEVELOPMENT_RESTART_EXIT_CODE + if (!restart) + process.exitCode = code + } +} +void run().catch((error) => { + console.error(error) + process.exitCode = 1 +}) diff --git a/apps/buddy/electron/main/DesktopWindowManager.ts b/apps/buddy/electron/main/DesktopWindowManager.ts index 651b129cd..78844f538 100644 --- a/apps/buddy/electron/main/DesktopWindowManager.ts +++ b/apps/buddy/electron/main/DesktopWindowManager.ts @@ -3,6 +3,7 @@ import type { DesktopWindowHandle } from './window' export interface DesktopWindowManagerOptions { createWindow: () => DesktopWindowHandle + onBeforeDestroy?: (window: BrowserWindow) => void onRecoveryExhausted?: () => void onRecoveryRebuilding?: () => void onRecoveryStarted?: () => void @@ -15,6 +16,7 @@ interface ManagedDesktopWindow { } export class DesktopWindowManager { + readonly #onBeforeDestroy?: (window: BrowserWindow) => void readonly #createWindow: () => DesktopWindowHandle readonly #onRecoveryExhausted?: () => void readonly #onRecoveryRebuilding?: () => void @@ -26,6 +28,7 @@ export class DesktopWindowManager { #disposed = false constructor(options: DesktopWindowManagerOptions) { + this.#onBeforeDestroy = options.onBeforeDestroy this.#createWindow = options.createWindow this.#onRecoveryExhausted = options.onRecoveryExhausted this.#onRecoveryRebuilding = options.onRecoveryRebuilding @@ -61,7 +64,7 @@ export class DesktopWindowManager { if (this.#managedWindow === managedWindow) { this.#managedWindow = null if (!managedWindow.handle.window.isDestroyed()) - managedWindow.handle.window.destroy() + this.#destroy(managedWindow.handle.window) } throw error }) @@ -96,14 +99,19 @@ export class DesktopWindowManager { const managedWindow = this.#managedWindow this.#managedWindow = null if (managedWindow && !managedWindow.handle.window.isDestroyed()) - managedWindow.handle.window.destroy() + this.#destroy(managedWindow.handle.window) + } + + #destroy(window: BrowserWindow): void { + this.#onBeforeDestroy?.(window) + window.destroy() } #replaceWindow(): ManagedDesktopWindow { const previousWindow = this.#managedWindow this.#managedWindow = null if (previousWindow && !previousWindow.handle.window.isDestroyed()) - previousWindow.handle.window.destroy() + this.#destroy(previousWindow.handle.window) const handle = this.#createWindow() const managedWindow: ManagedDesktopWindow = { @@ -188,7 +196,7 @@ export class DesktopWindowManager { const managedWindow = this.#managedWindow this.#managedWindow = null if (managedWindow && !managedWindow.handle.window.isDestroyed()) - managedWindow.handle.window.destroy() + this.#destroy(managedWindow.handle.window) this.#onRecoveryExhausted?.() } } diff --git a/apps/buddy/electron/main/__tests__/conversationIpc.spec.ts b/apps/buddy/electron/main/__tests__/conversationIpc.spec.ts new file mode 100644 index 000000000..07d481f3e --- /dev/null +++ b/apps/buddy/electron/main/__tests__/conversationIpc.spec.ts @@ -0,0 +1,123 @@ +import type { BrowserWindow, IpcMainInvokeEvent } from 'electron' +import { describe, expect, it, vi } from 'vitest' +import { conversationsRpc } from '../../../shared/conversation/conversationApi' +import { conversationArchiveRpc } from '../../../shared/conversation/conversationArchiveApi' +import { createConversationApi } from '../../preload/local-chat/conversation' +import { LOCAL_CHAT_IPC_CHANNELS } from '../../shared/localChatApi' +import { registerConversationIpc } from '../local-chat/conversation' +import { createLocalChatIpcContext } from '../local-chat/registrar' + +const electron = vi.hoisted(() => { + const handlers = new Map Promise>() + const webContents = { mainFrame: {} } + return { + handlers, + webContents, + invoke: async (channel: string, input: unknown) => { + const handler = handlers.get(channel) + if (!handler) + throw new Error('IPC handler is missing') + return handler({ sender: webContents, senderFrame: webContents.mainFrame } as unknown as IpcMainInvokeEvent, input) + }, + } +}) + +vi.mock('electron', () => ({ + ipcMain: { + handle: (channel: string, handler: (event: IpcMainInvokeEvent, input: unknown) => Promise) => electron.handlers.set(channel, handler), + removeHandler: (channel: string) => electron.handlers.delete(channel), + }, + ipcRenderer: { invoke: electron.invoke }, +})) + +describe('conversation archive IPC contract', () => { + it.each([ + ['archive', LOCAL_CHAT_IPC_CHANNELS.conversationsArchive], + ['restoreArchive', LOCAL_CHAT_IPC_CHANNELS.conversationsRestoreArchive], + ['deleteArchived', LOCAL_CHAT_IPC_CHANNELS.conversationsDeleteArchived], + ] as const)('preserves the fixed revision and refuses missing CAS for %s', async (method, channel) => { + const received: unknown[] = [] + const context = createLocalChatIpcContext({ + getLanguage: () => 'zh-CN', + getWindow: () => ({ webContents: electron.webContents } as unknown as BrowserWindow), + openModelSnapshotDirectory: async () => {}, + readWebCredential: async () => null, + runtime: { + state: {}, + onNotification: () => () => {}, + onStateChange: () => () => {}, + restart: async () => {}, + request: async (name, input) => { + expect(name).toBe(conversationArchiveRpc[method].method) + received.push(conversationArchiveRpc[method].input.parse(input)) + return true + }, + }, + }) + registerConversationIpc(context) + const api = createConversationApi() + try { + await expect(api.conversations[method]('task-1', 0)).resolves.toBe(true) + await expect(api.conversations[method]('task-1', 7)).resolves.toBe(true) + for (const expectedRevision of [undefined, -1, 1.5, '7', Number.MAX_SAFE_INTEGER + 1]) { + await expect(electron.invoke(channel, { conversationId: 'task-1', expectedRevision })).rejects.toThrow('VALIDATION_FAILED') + } + expect(received).toEqual([ + { conversationId: 'task-1', expectedRevision: 0 }, + { conversationId: 'task-1', expectedRevision: 7 }, + ]) + } + finally { context.dispose() } + }) +}) + +describe('conversation deletion IPC contract', () => { + it('preserves the preload revision condition through strict main validation', async () => { + const received: unknown[] = [] + const context = createLocalChatIpcContext({ + getLanguage: () => 'zh-CN', + getWindow: () => ({ webContents: electron.webContents } as unknown as BrowserWindow), + openModelSnapshotDirectory: async () => {}, + readWebCredential: async () => null, + runtime: { + state: {}, + onNotification: () => () => {}, + onStateChange: () => () => {}, + restart: async () => {}, + request: async (method, input) => { + expect(method).toBe(conversationsRpc.delete.method) + received.push(conversationsRpc.delete.input.parse(input)) + return true + }, + }, + }) + registerConversationIpc(context) + const api = createConversationApi() + try { + await expect(api.conversations.delete('task-1', 0)).resolves.toBe(true) + await expect(api.conversations.delete('task-1', 7)).resolves.toBe(true) + await expect(api.conversations.delete('task-1')).resolves.toBe(true) + expect(received).toEqual([ + { conversationId: 'task-1', expectedRevision: 0 }, + { conversationId: 'task-1', expectedRevision: 7 }, + { conversationId: 'task-1' }, + ]) + + for (const revision of [-1, 1.5, '7']) { + await expect(electron.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsDelete, { + conversationId: 'task-1', + expectedRevision: revision, + })).rejects.toThrow('VALIDATION_FAILED') + } + await expect(electron.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsDelete, { + conversationId: 'task-1', + expectedRevision: 7, + unexpected: true, + })).rejects.toThrow('VALIDATION_FAILED') + expect(received).toHaveLength(3) + } + finally { + context.dispose() + } + }) +}) diff --git a/apps/buddy/electron/main/app/DesktopApplication.ts b/apps/buddy/electron/main/app/DesktopApplication.ts index 39f1d175a..11adc57be 100644 --- a/apps/buddy/electron/main/app/DesktopApplication.ts +++ b/apps/buddy/electron/main/app/DesktopApplication.ts @@ -1,7 +1,10 @@ -import type { Event } from 'electron' +import type { BrowserWindow, Event } from 'electron' +import type { z } from 'zod' +import type { desktopOpenIntentSchema } from '../../../shared/service/localServiceProtocol' import type { DesktopEnvironment } from './typing' import process from 'node:process' -import { app, nativeTheme } from 'electron' +import { app, nativeTheme, powerMonitor } from 'electron' +import { z as schema } from 'zod' import { currentPlatform } from '../../../platform/currentPlatform' import { localTransports } from '../../../platform/ipc/localTransport' import { DEFAULT_BROWSER_PREFERENCES } from '../../../shared/browser/browserPreferences' @@ -11,6 +14,8 @@ import { ServiceLifecycleSource } from '../../../shared/lifecycle/ServiceLifecyc import { observeLifecycleDiagnostics } from '../../../shared/observability/lifecycleDiagnostics' import { runsRpc } from '../../../shared/runs/runApi' import { BrowserIntegration } from '../browser/BrowserIntegration' +import { connectPlatformHost } from '../runtime/platformHostBootstrap' +import { RemoteServiceClient } from '../runtime/RemoteServiceClient' import { INSTALLER_EXIT_ARGUMENT, isInstallerExitRequest, resolveDesktopLaunchIntent } from '../startupIntent' import { showStorageMigration } from '../storage/StorageMigrationWindow' import { showStorageUnavailable, StorageUnavailableError } from '../storage/storageUnavailable' @@ -26,7 +31,8 @@ import { DesktopRuntimeHost } from './DesktopRuntimeHost' import { checkDesktopSmokeBridge } from './desktopSmokeCheck' import { checkDesktopDirectories, DesktopInstanceLockUnavailable } from './desktopStorage' import { DesktopWindowHost } from './DesktopWindowHost' -import { initializeDesktopEnvironment, prepareDesktopEnvironment, prepareDesktopReady } from './environment' +import { initializeDesktopEnvironment, prepareDesktopEnvironment, prepareDesktopReady, registerDesktopSchemes } from './environment' +import { requestDesktopTarget } from './requestDesktopTarget' class DesktopApplication { readonly #environment: DesktopEnvironment @@ -39,6 +45,9 @@ class DesktopApplication { readonly #quit: ReturnType #disposePromise: Promise | null = null #rendererRecoveryPrompt: Promise | null = null + #openingDesktop: Promise | undefined + #closingDesktop: Promise | undefined + #locked = false constructor(environment: DesktopEnvironment) { this.#environment = environment @@ -52,13 +61,25 @@ class DesktopApplication { report: event => environment.events.publish(event), isTaskLinked: () => this.#runtime.config?.desktop.contextPanelMode === 'task', onActivityError: () => environment.events.publish({ level: 'warn', event: 'browser.activity.failed', errorCode: 'BROWSER_ACTIVITY_FAILED' }), - endpoint: localTransports[currentPlatform.transport](environment.paths.browserAdapterSocket), + endpoint: localTransports[currentPlatform.transport](environment.paths.browserAdapterSocket, { ownsProfileLease: () => true }), getPreferences: () => this.#runtime.config?.browser ?? DEFAULT_BROWSER_PREFERENCES, testBrokerSocketPath: environment.paths.profile === 'test' ? process.env.LEXORA_BUDDY_BROWSER_ADAPTER_TEST_BROKER_SOCKET : undefined, }) this.#runtime = new DesktopRuntimeHost(environment, this.#windows, this.#browser) + this.#runtime.openDesktop = (intent, signal) => this.#openDesktopTarget(intent, signal) this.#quit = createDesktopQuitLifecycle({ events: environment.events, + ...(environment.platformHost + ? { + getExitBehavior: () => this.#runtime.config?.desktop.exitBehavior ?? 'stop_service', + closeDesktop: () => this.#closeDesktop(), + restartDesktop: async () => { + if (!(this.#runtime.service instanceof RemoteServiceClient)) + throw new Error('SERVICE_NOT_READY') + await this.#runtime.service.restartDesktop() + }, + } + : {}), confirm: options => confirmDesktopQuit({ getWindow: () => this.#windows.window, getLanguage: () => this.#runtime.language, @@ -70,9 +91,13 @@ class DesktopApplication { }, options), dispose: async () => { try { + if (this.#runtime.service instanceof RemoteServiceClient) + await this.#runtime.service.stop() await this.#dispose() } - finally { await closeDesktopDiagnostics(environment.diagnostics) } + finally { + await closeDesktopDiagnostics(environment.diagnostics) + } }, quit: (restart) => { if (restart) @@ -126,8 +151,30 @@ class DesktopApplication { if (resolveDesktopLaunchIntent(argv) === 'foreground') this.#windows.show() }) - app.on('activate', () => this.#windows.show()) + app.on('activate', () => { + void this.#openDesktop().catch(() => {}) + }) app.on('window-all-closed', () => {}) + void app.whenReady().then(() => { + powerMonitor.on('lock-screen', () => { + this.#locked = true + }) + powerMonitor.on('unlock-screen', () => { + this.#locked = false + }) + }) + const platformHost = this.#environment.platformHost + if (platformHost) { + platformHost.peer.onRequest('host.stop', async (raw) => { + schema.object({}).strict().parse(raw) + await this.#dispose() + setImmediate(() => app.exit(0)) + return { stopped: true } + }) + platformHost.peer.onDidClose(() => { + void this.#dispose().finally(() => app.exit(1)) + }) + } } async start(): Promise { @@ -150,27 +197,103 @@ class DesktopApplication { defer(() => this.#integrations.stopSubscriptions()) await this.#integrations.start() }, ['desktop.runtime']) + await this.#runtime.publishHostReady() + if (this.#environment.platformHost) + return null return host.start('desktop.window', ({ defer }) => { defer(() => this.#windows.close()) defer(() => this.#browser.closeWindow()) - return this.#windows.initialize({ - onWindowCreated: window => this.#browser.bindWindow(window), - isQuitting: () => this.#quit.quitting, - onHidden: () => { void showBackgroundCloseNotice(this.#runtime.configStore) }, - onRecoveryExhausted: () => this.#presentRendererRecovery(), - }) + return this.#openDesktop() }, ['desktop.integrations']) }) - if (this.#runtime.windowsPowerShell?.endsWith('\\powershell.exe') && !this.#environment.isSmokeTest) + if (window && this.#runtime.windowsPowerShell?.endsWith('\\powershell.exe') && !this.#environment.isSmokeTest) showLegacyPowerShellNotice(window, () => this.#runtime.language, this.#environment) - if (this.#environment.isSmokeTest) { + if (this.#environment.isSmokeTest && window) { await checkDesktopSmokeBridge(window) await this.#runtime.verifyInstallation() await this.#quit.request() } } + async #openDesktopTarget(intent: z.infer, signal?: AbortSignal) { + const available = () => !signal?.aborted && !this.#locked && powerMonitor.getSystemIdleState(1) !== 'locked' && !this.#quit.quitting && !this.#disposePromise && !this.#closingDesktop + const unavailable = { accepted: false, uiReady: false, targetVisible: false, visible: false, focused: false } + if (!available()) + return unavailable + try { + const window = await this.#openDesktop(intent) + const targetVisible = await requestDesktopTarget(window, this.#environment.startup, intent, undefined, { signal, available }) + if (!available() || window.isDestroyed()) + return unavailable + return { accepted: true, uiReady: this.#environment.startup.state.status === 'ready', targetVisible, visible: window.isVisible(), focused: window.isFocused() } + } + catch { + return unavailable + } + } + + async #openDesktop(intent?: z.infer): Promise { + if (this.#quit.quitting || this.#disposePromise || this.#closingDesktop || this.#locked) + throw new Error('DESKTOP_OPEN_UNAVAILABLE') + if (intent?.kind === 'tasks' && intent.conversationId) { + const task = await this.#runtime.service.request('conversations.get', { conversationId: intent.conversationId }) + if (!task) + throw new Error('CONVERSATION_NOT_FOUND') + } + const window = await (this.#openingDesktop ??= (async () => { + await this.#runtime.setDesktopAvailable(true) + this.#integrations.attachDesktop() + if (this.#windows.window) { + this.#windows.show() + return this.#windows.window + } + const opened = await this.#windows.initialize({ + onWindowCreated: (window) => { + this.#browser.bindWindow(window) + window.on('query-session-end', () => { + void this.#quit.request({ reason: 'session_end', discardDraftsOnFailure: true }) + }) + }, + onWindowReleasing: () => { + this.#environment.startup.detachRenderer() + this.#browser.closeWindow() + }, + isQuitting: () => this.#quit.quitting, + onHidden: () => { + void showBackgroundCloseNotice(this.#runtime.configStore) + }, + onRecoveryExhausted: () => this.#presentRendererRecovery(), + }) + if (this.#environment.platformHost) + this.#windows.show() + return opened + })().finally(() => { + this.#openingDesktop = undefined + })) + if (this.#quit.quitting || this.#closingDesktop || window.isDestroyed()) + throw new Error('DESKTOP_OPEN_CANCELLED') + return window + } + + #closeDesktop(): Promise { + return this.#closingDesktop ??= (async () => { + await this.#openingDesktop + await this.#integrations.detachDesktop() + this.#browser.closeWindow() + this.#windows.close() + })().finally(() => { + this.#closingDesktop = undefined + }) + } + async handleStartupFailure(error: unknown): Promise { + if (this.#environment.platformHost) { + this.#environment.startup.failed(error) + await this.#dispose().catch(() => {}) + await closeDesktopDiagnostics(this.#environment.diagnostics) + app.exit(1) + return + } if (this.#environment.isSmokeTest) process.stderr.write(`Desktop installation verification failed: ${error instanceof Error ? error.stack : String(error)}\n`) this.#environment.startup.failed(error) @@ -187,7 +310,9 @@ class DesktopApplication { try { await closeDesktopDiagnostics(this.#environment.diagnostics) } - finally { app.exit(1) } + finally { + app.exit(1) + } } } } @@ -231,6 +356,8 @@ class DesktopApplication { this.#disposePromise ??= (async () => { this.#environment.startup.stopping() const failures: unknown[] = [] + this.#browser.closeWindow() + this.#windows.close() try { await this.#host.stop() } @@ -252,15 +379,23 @@ export async function startDesktopApplication(): Promise { let environment: DesktopEnvironment | undefined let recoveryRecorded = false try { + registerDesktopSchemes() if (isDesktopInstallationActive()) { app.quit() return } - environment = prepareDesktopEnvironment() + const platformHost = process.argv.includes('--lexora-platform-host') ? await connectPlatformHost() : undefined + process.env.LEXORA_BUDDY_STORAGE_AUTHORITY ??= 'sqlite' + environment = await prepareDesktopEnvironment(platformHost) if (environment.storage.state.pending) { await showStorageMigration(environment.storage, environment.diagnostics) return } + if (process.argv.includes('--lexora-storage-maintenance')) { + await closeDesktopDiagnostics(environment.diagnostics) + app.exit(0) + return + } initializeDesktopEnvironment(environment) if (process.argv.includes(INSTALLER_EXIT_ARGUMENT)) { await closeDesktopDiagnostics(environment.diagnostics) @@ -278,6 +413,13 @@ export async function startDesktopApplication(): Promise { void application.start().catch(error => application.handleStartupFailure(error)) } catch (cause) { + if (process.argv.includes('--lexora-platform-host')) { + environment?.startup.failed(cause) + if (environment) + await closeDesktopDiagnostics(environment.diagnostics) + app.exit(1) + return + } let error = cause if (error instanceof StorageUnavailableError) { await showStorageUnavailable(error) diff --git a/apps/buddy/electron/main/app/DesktopIntegrations.ts b/apps/buddy/electron/main/app/DesktopIntegrations.ts index 7fead5dd4..2b5f71663 100644 --- a/apps/buddy/electron/main/app/DesktopIntegrations.ts +++ b/apps/buddy/electron/main/app/DesktopIntegrations.ts @@ -7,7 +7,7 @@ import type { DesktopRuntimeHost } from './DesktopRuntimeHost' import type { DesktopWindowHost } from './DesktopWindowHost' import type { DesktopEnvironment } from './typing' import { homedir } from 'node:os' -import { basename } from 'node:path' +import { basename, join } from 'node:path' import process from 'node:process' import { app, nativeTheme, Notification, shell } from 'electron' import { z } from 'zod' @@ -50,6 +50,8 @@ import { NotificationSoundLibrary } from '../notifications/NotificationSoundLibr import { NotificationSoundService } from '../notifications/NotificationSoundService' import { registerDesktopNotificationIpc } from '../notifications/registerDesktopNotificationIpc' import { registerNotificationSoundIpc } from '../notifications/registerNotificationSoundIpc' +import { ProductDesktopGateway } from '../productProtocol/ProductDesktopGateway' +import { registerDraftRecoveryIpc } from '../productProtocol/registerDraftRecoveryIpc' import { createProviderSpeechBackend } from '../speech/createProviderSpeechBackend' import { registerSpeechIpc } from '../speech/registerSpeechIpc' import { registerStorageIpc } from '../storage/registerStorageIpc' @@ -81,6 +83,7 @@ export class DesktopIntegrations { readonly #requestInstall: (prepare: () => Promise<(() => void) | void>) => Promise readonly #subscriptions: Array<() => void | Promise> = [] #tray: DesktopTrayController | null = null + #detachDesktop: (() => Promise) | undefined constructor(environment: DesktopEnvironment, runtime: DesktopRuntimeHost, windows: DesktopWindowHost, browser: BrowserIntegration, requestQuit: () => void, requestRestart: () => void, requestInstall: (prepare: () => Promise<(() => void) | void>) => Promise) { this.#environment = environment @@ -107,8 +110,31 @@ export class DesktopIntegrations { await this.#browser.host?.updateActivity() } + attachDesktop(): void { + if (this.#tray) + return + const { paths, trayIconPath } = this.#environment + const runtime = this.#runtime + const windows = this.#windows + this.#tray = createDesktopTray({ + appName: paths.appName, + iconPath: trayIconPath, + profile: paths.profile, + language: runtime.language, + onOpenDesktop: () => windows.show(), + onQuit: this.#requestQuit, + onRestart: this.#requestRestart, + }) + } + + async detachDesktop(): Promise { + this.destroyTray() + await this.#detachDesktop?.() + await this.#runtime.setDesktopAvailable(false) + } + async start(): Promise { - const { paths, trayIconPath, diagnostics } = this.#environment + const { paths, diagnostics } = this.#environment const runtime = this.#runtime const windows = this.#windows const service = runtime.service @@ -252,6 +278,7 @@ export class DesktopIntegrations { await extensions.themesReady.catch(() => {}) this.#subscriptions.push(registerThemeIpc(themes, () => windows.window, (color, dark, system) => windows.setThemeAppearance(color, dark, system))) this.#subscriptions.push(service.onStateChange(() => extensions.conditions.invalidate({ inputs: ['runtime.models', 'runtime.task'] }))) + this.#detachDesktop = extensions.detachDesktop runtime.inspectExtension = extensions.inspect runtime.extensionAgent = extensions.agent runtime.extensionStorage = extensions @@ -283,15 +310,8 @@ export class DesktopIntegrations { this.#subscriptions.push(registerStartupIpc(this.#environment.startup, () => windows.window)) this.#subscriptions.push(registerApplicationLogIpc(new ApplicationLogReader(paths.logs, diagnostics.launchId, homedir()), () => windows.window, event => diagnostics.record(event), () => diagnostics.flushWithin(1000))) this.#subscriptions.push(registerPerformanceIpc(runtime, () => windows.window, this.#environment.events.publish)) - this.#tray = createDesktopTray({ - appName: paths.appName, - iconPath: trayIconPath, - profile: paths.profile, - language: runtime.language, - onOpenDesktop: () => windows.show(), - onQuit: this.#requestQuit, - onRestart: this.#requestRestart, - }) + if (!this.#environment.platformHost) + this.attachDesktop() const player = new NotificationAudioPlayer() const soundLibrary = new NotificationSoundLibrary({ home: paths.buddyHome, configStore: runtime.configStore, applyConfig: config => this.applyConfig(config) }) const sounds = new NotificationSoundService({ player, library: soundLibrary, getPreferences: () => runtime.config!.desktop.notificationSound }) @@ -388,6 +408,7 @@ export class DesktopIntegrations { }) this.#subscriptions.push(() => configNotifications.dispose()) this.#subscriptions.push(registerBrowserDesktopIpc({ + presentation: this.#browser, data: this.#browser.data, screenshots: this.#browser.screenshots, getHost: () => this.#browser.host, @@ -395,6 +416,16 @@ export class DesktopIntegrations { resolveArtifactEntry: async input => browserArtifactEntrySchema.parse(await service.request('artifacts.resolveBrowserEntry', input)), resolveFileEntry: async input => taskFilesRpc.resolveBrowser.response.parse(await service.request(taskFilesRpc.resolveBrowser.method, input)), })) + const productGateway = process.env.LEXORA_BUDDY_STORAGE_AUTHORITY === 'sqlite' + ? new ProductDesktopGateway(service, join(paths.lexoraHome, 'device/product-outbox'), buddyVersion.version) + : null + if (productGateway) + this.#subscriptions.push(() => productGateway.dispose()) + this.#subscriptions.push(registerDraftRecoveryIpc({ + directory: join(paths.lexoraHome, 'device/renderer-drafts'), + getWindow: () => windows.window, + identity: async () => productGateway ? productGateway.getIdentity() : null, + })) this.#subscriptions.push(registerLocalChatIpc({ recordDiagnostic: this.#environment.events.scope({ component: 'desktop.gateway' }).publish, readWebCredential: () => runtime.readWebCredential(), @@ -417,7 +448,7 @@ export class DesktopIntegrations { throw error } }, - runtime: service, + runtime: productGateway ?? service, })) } diff --git a/apps/buddy/electron/main/app/DesktopRuntimeHost.ts b/apps/buddy/electron/main/app/DesktopRuntimeHost.ts index 57524a3b9..b1c72d931 100644 --- a/apps/buddy/electron/main/app/DesktopRuntimeHost.ts +++ b/apps/buddy/electron/main/app/DesktopRuntimeHost.ts @@ -1,4 +1,6 @@ import type { ExtensionInspection } from '../../../shared/extensions/extensionAuthoring' +import type { RuntimeRpcPeerContract } from '../../../shared/runtime/rpcPeer' +import type { DesktopOpenIntent, DesktopOpenResult } from '../../../shared/service/localServiceProtocol' import type { SpeechProvider } from '../../../shared/speech/speechApi' import type { LexoraConfig } from '../../shared/desktopApi' import type { BrowserIntegration } from '../browser/BrowserIntegration' @@ -10,6 +12,7 @@ import type { DesktopEnvironment } from './typing' import { join } from 'node:path' import process from 'node:process' import { app, powerMonitor } from 'electron' +import { z } from 'zod' import { currentPlatform } from '../../../platform/currentPlatform' import { createBuddyNativeEnvironment, resolveBuddyProcessControl, resolveBuddyShellSandbox } from '../../../platform/native/nativeHost' import shellSandbox from '../../../platform/native/shellSandbox.json' @@ -28,6 +31,7 @@ import { ocrRpc } from '../../../shared/images/ocrService' import { networkPermissionGrantSchema, networkPermissionsRpc } from '../../../shared/permissions/networkPermissionsRpc' import { isLinux } from '../../../shared/platform/identifiers' import { runtimePreferencesRpc } from '../../../shared/runtime/runtimePreferences' +import { desktopOpenIntentSchema } from '../../../shared/service/localServiceProtocol' import { HOST_SPEECH_MODELS } from '../../../shared/speech/speechApi' import { installAttachmentProtocol } from '../attachmentProtocol' import { registerBrowserHostRpc } from '../browser/registerBrowserHostRpc' @@ -49,12 +53,15 @@ import { installRendererProtocol } from '../rendererProtocol' import { createBuddyServiceEnvironment, resolveBuddySearchToolsDirectory } from '../runtime/buddyServiceEnvironment' import { forkBuddyServiceProcess } from '../runtime/buddyServiceProcess' import { BuddyServiceSupervisor } from '../runtime/BuddyServiceSupervisor' +import { RemoteServiceClient } from '../runtime/RemoteServiceClient' import { registerSandboxHostRpc } from '../sandbox/registerSandboxHostRpc' import { verifySandboxInstallation } from '../sandbox/verifySandboxInstallation' import { createCredentialVault } from '../secrets/CredentialVault' import { registerCredentialHostRpc } from '../secrets/registerCredentialHostRpc' +import { withProfileWritesStopped } from '../storage/profileWrites' export class DesktopRuntimeHost { + openDesktop: ((intent: DesktopOpenIntent, signal?: AbortSignal) => Promise) | null = null readonly performance: DesktopPerformanceMonitor inspectExtension: ((id: string) => Promise) | null = null extensionAgent: ReturnType['agent'] | null = null @@ -71,7 +78,8 @@ export class DesktopRuntimeHost { #credentials: CredentialVault | null = null #config: LexoraConfig | null = null #features: DesktopFeature[] = [] - #service: BuddyServiceSupervisor | null = null + #service: Pick | null = null + #desktopAvailable = true #network: DesktopNetwork | null = null #webHost: HostWebService | null = null #windowsPowerShell: string | undefined @@ -83,6 +91,7 @@ export class DesktopRuntimeHost { constructor(environment: DesktopEnvironment, windows: DesktopWindowHost, browser: BrowserIntegration) { this.#environment = environment + this.#desktopAvailable = !environment.platformHost this.#windows = windows this.#browser = browser this.performance = new DesktopPerformanceMonitor(() => ({ metrics: app.getAppMetrics(), proxy: this.network.activity }), environment.events.publish) @@ -101,7 +110,8 @@ export class DesktopRuntimeHost { } withStoppedStorage(operation: () => Promise): Promise { - return this.service.withStoppedStorage(() => this.extensionStorage ? this.extensionStorage.withStoppedStorage(operation) : operation()) + const snapshot = () => withProfileWritesStopped(this.#environment.paths.lexoraHome, operation) + return this.service.withStoppedStorage(() => this.extensionStorage ? this.extensionStorage.withStoppedStorage(snapshot) : snapshot()) } get language(): LexoraConfig['desktop']['language'] { @@ -160,7 +170,7 @@ export class DesktopRuntimeHost { } } - get service(): BuddyServiceSupervisor { + get service(): Pick { if (!this.#service) throw new Error('Desktop Runtime is not prepared') return this.#service @@ -217,91 +227,103 @@ export class DesktopRuntimeHost { paths: environment.paths, }) this.#features = composition.features - this.#service = new BuddyServiceSupervisor({ - onDiagnostic: (event) => { - environment.diagnostics.record({ ...event, scope: 'local-service' }) - }, - bindPeer: (peer) => { - const sandbox = registerSandboxHostRpc(peer, { - buddyHome: environment.paths.buddyHome, - protectedRoots: environment.storage.protection.sandboxRoots, - protectionScope: environment.storage.bootstrapRoot, - proxyUrl: this.#network!.sandboxProxyUrl, - ...this.#sandboxOptions(), - }) - const prepareSandbox = () => { - sandbox.prepare() - peer.notify('host.sandbox.ready', {}) - } - this.#prepareSandbox = prepareSandbox - sandbox.onDidChange(event => environment.events.publish({ - event: `sandbox.supervisor.${event.kind}`, - level: event.kind === 'cleanup_failed' ? 'warn' : 'info', - requestId: event.requestId, - sandboxProcess: { phase: event.kind, pid: event.pid, exitCode: event.exitCode }, - })) - const stopAuthoring = registerExtensionAuthoringRpc(peer, (id) => { - if (!this.inspectExtension) + const bindPeer = (peer: RuntimeRpcPeerContract) => { + const sandbox = registerSandboxHostRpc(peer, { + buddyHome: environment.paths.buddyHome, + protectedRoots: environment.storage.protection.sandboxRoots, + protectionScope: environment.storage.bootstrapRoot, + proxyUrl: this.#network!.sandboxProxyUrl, + ...this.#sandboxOptions(), + }) + const prepareSandbox = () => { + sandbox.prepare() + peer.notify('host.sandbox.ready', {}) + } + this.#prepareSandbox = prepareSandbox + sandbox.onDidChange(event => environment.events.publish({ + event: `sandbox.supervisor.${event.kind}`, + level: event.kind === 'cleanup_failed' ? 'warn' : 'info', + requestId: event.requestId, + sandboxProcess: { phase: event.kind, pid: event.pid, exitCode: event.exitCode }, + })) + const stopAuthoring = registerExtensionAuthoringRpc(peer, (id) => { + if (!this.inspectExtension) + throw new Error('EXTENSION_SERVICE_UNAVAILABLE') + return this.inspectExtension(id) + }, () => this.#config?.desktop.pluginAuthor ?? '') + const disposers = [ + peer.onRequest('host.openDesktop', (raw, signal) => { + if (!this.openDesktop) + throw new Error('DESKTOP_UNAVAILABLE') + return this.openDesktop(desktopOpenIntentSchema.parse(raw), signal) + }), + peer.onRequest(imageRpc.preferences, async () => (await this.configStore.read()).desktop.images ?? {}), + peer.onRequest(ocrRpc.read, (raw, signal) => this.ocr.read(raw, signal ?? new AbortController().signal)), + peer.onRequest(HOST_SPEECH_MODELS, () => this.speechModels?.() ?? []), + registerExtensionImageReadingRpc(peer, () => this.extensionInference), + registerExtensionImageRpc(peer, () => this.extensionInference), + registerExtensionModelRpc(peer, () => this.extensionInference), + registerExtensionShellRpc(peer, () => this.extensionShells), + peer.onRequest(extensionAgentRpc.list, () => this.extensionAgent?.list() ?? []), + peer.onRequest(extensionAgentRpc.releaseRun, (input) => { + const { runId } = z.object({ runId: z.string().min(1).max(256) }).strict().parse(input) + this.extensionAgent?.releaseRun(runId) + return {} + }), + peer.onRequest(extensionSkillsRpc.list, () => this.extensionSkills?.list() ?? []), + peer.onRequest(extensionAgentRpc.resource, (input, signal) => { + if (!this.extensionAgent) + throw new Error('EXTENSION_AGENT_UNAVAILABLE') + return this.extensionAgent.readResource(extensionAgentResourceRequestSchema.parse(input), signal ?? new AbortController().signal) + }), + // Unlike skills, an empty answer here would read as "everything was uninstalled", so an unready service must fail instead. + peer.onRequest(extensionCapabilitiesRpc.list, () => { + if (!this.extensionCapabilities) throw new Error('EXTENSION_SERVICE_UNAVAILABLE') - return this.inspectExtension(id) - }, () => this.#config?.desktop.pluginAuthor ?? '') - const disposers = [ - peer.onRequest(imageRpc.preferences, async () => (await this.configStore.read()).desktop.images ?? {}), - peer.onRequest(ocrRpc.read, (raw, signal) => this.ocr.read(raw, signal ?? new AbortController().signal)), - peer.onRequest(HOST_SPEECH_MODELS, () => this.speechModels?.() ?? []), - registerExtensionImageReadingRpc(peer, () => this.extensionInference), - registerExtensionImageRpc(peer, () => this.extensionInference), - registerExtensionModelRpc(peer, () => this.extensionInference), - registerExtensionShellRpc(peer, () => this.extensionShells), - peer.onRequest(extensionAgentRpc.list, () => this.extensionAgent?.list() ?? []), - peer.onRequest(extensionSkillsRpc.list, () => this.extensionSkills?.list() ?? []), - peer.onRequest(extensionAgentRpc.resource, (input, signal) => { - if (!this.extensionAgent) - throw new Error('EXTENSION_AGENT_UNAVAILABLE') - return this.extensionAgent.readResource(extensionAgentResourceRequestSchema.parse(input), signal ?? new AbortController().signal) - }), - // Unlike skills, an empty answer here would read as "everything was uninstalled", so an unready service must fail instead. - peer.onRequest(extensionCapabilitiesRpc.list, () => { - if (!this.extensionCapabilities) - throw new Error('EXTENSION_SERVICE_UNAVAILABLE') - return this.extensionCapabilities.list() - }), - peer.onRequest(extensionAgentRpc.invoke, (input, signal) => { - if (!this.extensionAgent) - throw new Error('EXTENSION_AGENT_UNAVAILABLE') - return this.extensionAgent.invoke(extensionAgentInvocationSchema.parse(input), signal ?? new AbortController().signal) - }), - peer.onRequest(runtimePreferencesRpc.get, () => this.#config!.runtime), - peer.onRequest(networkPermissionsRpc.get, async () => (await this.configStore.read()).browser.networkPermissions), - peer.onRequest(networkPermissionsRpc.allow, async (params, signal) => { - const input = networkPermissionGrantSchema.parse(params) - await this.configStore.allowNetworkPermission(input.host, input.operation, () => signal?.throwIfAborted()) - return {} - }), - peer.onRequest(contextPanelRpc.presentBrowser, (params) => { - const source = contextPanelSourceSchema.parse(params) - return this.contextPanel.execute({ action: 'open', target: { kind: 'browser', source } }, 'harness') - }), - registerWebHostRpc(peer, this.#webHost!), - registerBrowserHostRpc(peer, { - createAdapterLease: input => this.#browser.adapter.issueLease(input), - getHost: () => this.#browser.host, - }), - registerCredentialHostRpc(peer, credentials), - ...this.#features.map(feature => feature.bindPeer(peer)), - ] - return () => { - if (this.#prepareSandbox === prepareSandbox) - this.#prepareSandbox = undefined - const cleanup = Promise.allSettled([sandbox.dispose(), stopAuthoring()]).then((results) => { - const failures = results.filter(result => result.status === 'rejected').map(result => result.reason) - if (failures.length) - throw new AggregateError(failures, 'RUNTIME_CLEANUP_FAILED') - }).finally(() => this.#sandboxCleanup.delete(cleanup)) - this.#sandboxCleanup.add(cleanup) - disposers.forEach(dispose => dispose()) - } + return this.extensionCapabilities.list() + }), + peer.onRequest(extensionAgentRpc.invoke, (input, signal) => { + if (!this.extensionAgent) + throw new Error('EXTENSION_AGENT_UNAVAILABLE') + return this.extensionAgent.invoke(extensionAgentInvocationSchema.parse(input), signal ?? new AbortController().signal) + }), + peer.onRequest(runtimePreferencesRpc.get, () => this.#config!.runtime), + peer.onRequest(networkPermissionsRpc.get, async () => (await this.configStore.read()).browser.networkPermissions), + peer.onRequest(networkPermissionsRpc.allow, async (params, signal) => { + const input = networkPermissionGrantSchema.parse(params) + await this.configStore.allowNetworkPermission(input.host, input.operation, () => signal?.throwIfAborted()) + return {} + }), + peer.onRequest(contextPanelRpc.presentBrowser, (params) => { + const source = contextPanelSourceSchema.parse(params) + return this.contextPanel.execute({ action: 'open', target: { kind: 'browser', source } }, 'harness') + }), + registerWebHostRpc(peer, this.#webHost!), + registerBrowserHostRpc(peer, { + createAdapterLease: input => this.#browser.adapter.issueLease(input), + getHost: () => this.#browser.host, + }), + registerCredentialHostRpc(peer, credentials), + ...this.#features.map(feature => feature.bindPeer(peer)), + ] + return () => { + this.extensionAgent?.releaseAllRuns() + if (this.#prepareSandbox === prepareSandbox) + this.#prepareSandbox = undefined + const cleanup = Promise.allSettled([sandbox.dispose(), stopAuthoring()]).then((results) => { + const failures = results.filter(result => result.status === 'rejected').map(result => result.reason) + if (failures.length) + throw new AggregateError(failures, 'RUNTIME_CLEANUP_FAILED') + }).finally(() => this.#sandboxCleanup.delete(cleanup)) + this.#sandboxCleanup.add(cleanup) + disposers.forEach(dispose => dispose()) + } + } + const supervisorOptions = { + onDiagnostic: (event: Parameters[0]['onDiagnostic']>>[0]) => { + environment.diagnostics.record({ ...event, scope: 'local-service' }) }, + bindPeer, diagnosticOutput: environment.diagnostics.createWritable('local-service', { event: 'runtime.supervisor', level: 'error' }), spawnService: (onFatalError, sourceId) => forkBuddyServiceProcess({ env: { @@ -314,20 +336,28 @@ export class DesktopRuntimeHost { onFatalError, captureStderr: output => environment.diagnostics.captureOutput('local-service', output, sourceId), }), - }) + } satisfies ConstructorParameters[0] + if (environment.platformHost) { + this.#subscriptions.push(bindPeer(environment.platformHost.peer)) + this.#service = new RemoteServiceClient(environment.platformHost.bootstrap, environment.platformHost.peer) + } + else { + this.#service = new BuddyServiceSupervisor(supervisorOptions) + } this.#subscriptions.push(environment.startup.bindRuntime(this.#service)) return config } async applyConfig(config: LexoraConfig): Promise { await this.#network?.apply(config.proxy) + const previousLaunchAtLogin = this.#config?.desktop.launchAtLogin const previous = this.#config?.runtime this.#config = config if (previous?.cacheWarming !== config.runtime.cacheWarming || previous?.codemode !== config.runtime.codemode || previous?.modelRetryLimit !== config.runtime.modelRetryLimit || previous?.codemodeWorkerLimit !== config.runtime.codemodeWorkerLimit || previous?.codemodeOverflow !== config.runtime.codemodeOverflow) this.#service?.notify(runtimePreferencesRpc.changed, config.runtime) - const featureConfig = config.desktop.onboardingCompleted ? config : { ...config, pet: { ...config.pet, enabled: false } } + const featureConfig = this.#desktopAvailable && config.desktop.onboardingCompleted ? config : { ...config, pet: { ...config.pet, enabled: false } } await Promise.all(this.#features.map(feature => feature.applyConfig(featureConfig))) - if (app.isPackaged && this.#environment.paths.profile === 'stable') + if (app.isPackaged && this.#environment.paths.profile === 'stable' && previousLaunchAtLogin !== undefined && previousLaunchAtLogin !== config.desktop.launchAtLogin) await this.#environment.setAutostart(config.desktop.launchAtLogin) } @@ -372,13 +402,26 @@ export class DesktopRuntimeHost { return this.#credentials.read('web', 'tavily') } + async setDesktopAvailable(value: boolean): Promise { + this.#desktopAvailable = value + if (this.#config) + await this.applyConfig(this.#config) + } + + async publishHostReady(): Promise { + const host = this.#environment.platformHost + if (!host) + return + await host.peer.request('host.ready', { hostInstanceId: host.bootstrap.hostInstanceId, preferences: this.#config!.runtime, display: true, proxy: { httpProxy: this.network.proxyUrl, httpsProxy: this.network.proxyUrl, noProxy: '' } }, 120000) + } + async stop(): Promise { this.performance.stop() const failures: unknown[] = [] for (const cleanup of [ () => this.contextPanel.dispose(), () => this.configStore.dispose(), - () => this.#service?.stop(), + () => this.#service instanceof RemoteServiceClient ? this.#service.disconnect() : this.#service?.stop(), () => Promise.all(this.#sandboxCleanup), () => this.#webHost?.dispose(), () => this.#network?.stop(), diff --git a/apps/buddy/electron/main/app/DesktopStartup.ts b/apps/buddy/electron/main/app/DesktopStartup.ts index d13f7fc3a..dd60e28d0 100644 --- a/apps/buddy/electron/main/app/DesktopStartup.ts +++ b/apps/buddy/electron/main/app/DesktopStartup.ts @@ -130,6 +130,13 @@ export class DesktopStartup { return true } + detachRenderer(): void { + if (this.#renderer) + this.#retiredRenderers.add(this.#renderer.sourceId) + this.#renderer = null + this.#update() + } + stopping(): void { if (this.#terminalStatus) return diff --git a/apps/buddy/electron/main/app/DesktopWindowHost.ts b/apps/buddy/electron/main/app/DesktopWindowHost.ts index 94cb9cda6..29c1ff060 100644 --- a/apps/buddy/electron/main/app/DesktopWindowHost.ts +++ b/apps/buddy/electron/main/app/DesktopWindowHost.ts @@ -15,6 +15,7 @@ interface WindowBindings { onHidden: () => void onRecoveryExhausted: () => void onWindowCreated: (window: BrowserWindow) => void + onWindowReleasing?: (window: BrowserWindow) => void } export class DesktopWindowHost { @@ -45,6 +46,7 @@ export class DesktopWindowHost { screen.getAllDisplays().map(display => display.bounds), ) const manager = new DesktopWindowManager({ + onBeforeDestroy: bindings.onWindowReleasing, onRecoveryStarted: () => environment.events.publish({ level: 'warn', event: 'renderer.recovery.started', attempt: 1 }), onRecoveryRebuilding: () => environment.events.publish({ level: 'info', event: 'renderer.recovery.rebuilding', attempt: 1 }), onRecoveryExhausted: () => { diff --git a/apps/buddy/electron/main/app/__tests__/DesktopStartup.spec.ts b/apps/buddy/electron/main/app/__tests__/DesktopStartup.spec.ts index d19ed755a..64345bc94 100644 --- a/apps/buddy/electron/main/app/__tests__/DesktopStartup.spec.ts +++ b/apps/buddy/electron/main/app/__tests__/DesktopStartup.spec.ts @@ -80,9 +80,6 @@ describe('application lifecycle snapshot', () => { expect(startup.state.status).toBe('ready') expect(startup.state.hasBeenReady).toBe(true) expect(events.filter(event => event.event === 'app.ready')).toHaveLength(1) - const snapshot = startup.state - Object.assign(snapshot.stages[0]!, { status: 'failed' }) - expect(startup.state.stages[0]?.status).toBe('completed') }) it('rejects delayed renderer reports after the Runtime generation changes while retaining their diagnostics', () => { @@ -117,11 +114,17 @@ describe('application lifecycle snapshot', () => { expect(startup.state.stages.some(stage => stage.progress)).toBe(false) }) - it('reconstructs completed managed stages when subscribed after initialization', async () => { - const host = new ServiceHost() - await host.start('desktop', () => true) - const startup = new DesktopStartup() - startup.bindDesktop(host.lifecycle) - expect(startup.state.stages).toContainEqual(expect.objectContaining({ stage: 'desktop', status: 'completed' })) + it('requires a fresh renderer after closing the desktop and rejects late reports from the old page', () => { + const { startup, complete, send, connect } = fixture() + complete('desktop') + connect('runtime-1') + complete('renderer') + expect(startup.state.status).toBe('ready') + startup.detachRenderer() + expect(startup.state.status).toBe('starting') + send('renderer', 'ready') + expect(startup.state.status).toBe('starting') + complete('renderer') + expect(startup.state.status).toBe('ready') }) }) diff --git a/apps/buddy/electron/main/app/__tests__/desktopQuitLifecycle.spec.ts b/apps/buddy/electron/main/app/__tests__/desktopQuitLifecycle.spec.ts index 3f1195581..dcd8b13c2 100644 --- a/apps/buddy/electron/main/app/__tests__/desktopQuitLifecycle.spec.ts +++ b/apps/buddy/electron/main/app/__tests__/desktopQuitLifecycle.spec.ts @@ -3,6 +3,57 @@ import { describe, expect, it } from 'vitest' import { createDesktopQuitLifecycle } from '../desktopQuitLifecycle' describe('desktop quit lifecycle', () => { + it('captures the committed exit preference once and keeps Core alive when only Desktop exits', async () => { + const saving = deferred() + const operations: string[] = [] + let preference: 'keep_service' | 'stop_service' = 'keep_service' + const lifecycle = createDesktopQuitLifecycle({ + getExitBehavior: () => preference, + closeDesktop: async () => { operations.push('desktop.closed') }, + confirm: () => saving.promise, + dispose: async () => { operations.push('service.stopped') }, + quit: () => { operations.push('host.quit') }, + exit: () => { operations.push('host.forced') }, + }) + const first = lifecycle.request() + preference = 'stop_service' + expect(lifecycle.request()).toBe(first) + saving.resolve(true) + await first + expect(operations).toEqual(['desktop.closed']) + expect(lifecycle.committed).toBe(false) + await lifecycle.request({ reason: 'service_stop' }) + expect(operations).toEqual(['desktop.closed', 'service.stopped', 'host.quit']) + }) + + it('hands a confirmed restart to Core and retries a rejected handoff without disposing Desktop', async () => { + const accepted = deferred() + let failure = true + let requests = 0 + const lifecycle = createDesktopQuitLifecycle({ + getExitBehavior: () => 'keep_service', + confirm: async () => true, + restartDesktop: async () => { + requests++ + if (failure) + throw new Error('SERVICE_NOT_READY') + await accepted.promise + }, + dispose: async () => { throw new Error('Core owns restart cleanup') }, + quit: () => { throw new Error('Core owns the next Desktop') }, + exit: () => { throw new Error('A rejected handoff must keep Desktop alive') }, + }) + await expect(lifecycle.request({ restart: true })).rejects.toThrow('SERVICE_NOT_READY') + expect(lifecycle.quitting).toBe(false) + failure = false + const request = lifecycle.request({ restart: true }) + expect(lifecycle.request({ restart: true })).toBe(request) + accepted.resolve() + await request + expect(requests).toBe(2) + expect(lifecycle.committed).toBe(true) + }) + it('shares concurrent requests and commits only after saving and cleanup', async () => { const save = deferred() const stopped = deferred() @@ -59,20 +110,7 @@ describe('desktop quit lifecycle', () => { expect(events).toEqual(['cleanup', 'restart']) }) - it('preserves explicit discard-on-failure for signal shutdown', async () => { - const events: string[] = [] - const lifecycle = createDesktopQuitLifecycle({ - exit: () => { throw new Error('Unexpected forced exit') }, - confirm: async options => options.discardDraftsOnFailure === true, - dispose: async () => { events.push('cleanup') }, - quit: () => { events.push('quit') }, - }) - await lifecycle.request({ discardDraftsOnFailure: true }) - expect(events).toEqual(['cleanup', 'quit']) - expect(lifecycle.committed).toBe(true) - }) - - it.each(['quit', 'installation'] as const)('exits after cleanup fails during %s without committing or running the completion action', async (reason) => { + it('exits after cleanup fails without committing or running the installation', async () => { const operations: string[] = [] const lifecycle = createDesktopQuitLifecycle({ confirm: async () => true, @@ -83,36 +121,15 @@ describe('desktop quit lifecycle', () => { quit: () => { operations.push('quit') }, exit: () => { operations.push('exit') }, }) - const input = reason === 'quit' ? {} : { reason: 'installation' as const } - const prepare = reason === 'installation' - ? async () => () => { operations.push('install') } - : undefined - const pending = lifecycle.request(input, prepare) + const pending = lifecycle.request({ reason: 'installation' }, async () => () => { + operations.push('install') + }) await expect(pending).rejects.toThrow('Cleanup failed') expect(operations).toEqual(['dispose', 'exit']) expect(lifecycle.quitting).toBe(true) expect(lifecycle.committed).toBe(false) }) - it('hands installation off only after confirmation and before releasing application resources', async () => { - const operations: string[] = [] - let allowed = false - const lifecycle = createDesktopQuitLifecycle({ - exit: () => { throw new Error('Unexpected forced exit') }, - confirm: async () => allowed, - dispose: async () => { operations.push('dispose') }, - quit: () => { operations.push('quit') }, - }) - const prepare = async () => { - operations.push('installer') - } - await lifecycle.request({ reason: 'installation' }, prepare) - expect(operations).toEqual([]) - allowed = true - await lifecycle.request({ reason: 'installation' }, prepare) - expect(operations).toEqual(['installer', 'dispose', 'quit']) - }) - it('leaves the application running if installer launch fails', async () => { const operations: string[] = [] const lifecycle = createDesktopQuitLifecycle({ diff --git a/apps/buddy/electron/main/app/__tests__/requestDesktopTarget.spec.ts b/apps/buddy/electron/main/app/__tests__/requestDesktopTarget.spec.ts new file mode 100644 index 000000000..1208b8ebb --- /dev/null +++ b/apps/buddy/electron/main/app/__tests__/requestDesktopTarget.spec.ts @@ -0,0 +1,72 @@ +import type { BrowserWindow, IpcMain } from 'electron' +import { EventEmitter } from 'node:events' +import { afterEach, expect, it, vi } from 'vitest' +import { DESKTOP_IPC_CHANNELS } from '../../../shared/desktopApi' +import { requestDesktopTarget } from '../requestDesktopTarget' +import { createStartupFixture } from './startupFixture' + +vi.mock('electron', () => ({ ipcMain: {} })) +afterEach(() => vi.useRealTimers()) + +function fixture() { + const { startup, complete, connect } = createStartupFixture() + complete('desktop') + connect('runtime-1') + complete('renderer') + const ipc = new EventEmitter() + let request!: { requestId: string } + const webContents = { mainFrame: {}, isDestroyed: () => false, send: (_channel: string, input: typeof request) => { + request = input + } } + const window = Object.assign(new EventEmitter(), { isDestroyed: () => false, webContents }) + const open = (options = {}) => requestDesktopTarget(window as unknown as BrowserWindow, startup, { kind: 'tasks' }, ipc as unknown as IpcMain, options) + const ack = (sender = webContents, senderFrame = webContents.mainFrame, requestId = request.requestId) => ipc.emit(DESKTOP_IPC_CHANNELS.appOpenTargetAck, { sender, senderFrame }, { requestId, opened: true }) + return { open, ack, window, webContents, ipc } +} + +it('acknowledges only the requested target from the current main frame', async () => { + vi.useFakeTimers() + const f = fixture() + const opening = f.open() + let settled = false + void opening.then(() => settled = true) + await vi.advanceTimersByTimeAsync(0) + f.ack({ ...f.webContents }) + f.ack(f.webContents, {}) + f.ack(f.webContents, f.webContents.mainFrame, '00000000-0000-4000-8000-000000000000') + await vi.advanceTimersByTimeAsync(100) + expect(settled).toBe(false) + f.ack() + await expect(opening).resolves.toBe(true) + expect(f.ipc.listenerCount(DESKTOP_IPC_CHANNELS.appOpenTargetAck)).toBe(0) +}) + +it.each(['timeout', 'closed'])('does not report a visible target after %s', async (reason) => { + vi.useFakeTimers() + const f = fixture() + const opening = f.open() + await vi.advanceTimersByTimeAsync(0) + if (reason === 'closed') + f.window.emit('closed') + else + await vi.advanceTimersByTimeAsync(20000) + await expect(opening).resolves.toBe(false) + expect(f.ipc.listenerCount(DESKTOP_IPC_CHANNELS.appOpenTargetAck)).toBe(0) +}) + +it('does not accept a target acknowledgement after locking or cancellation', async () => { + vi.useFakeTimers() + const f = fixture() + let available = true + const opening = f.open({ available: () => available }) + await vi.advanceTimersByTimeAsync(0) + available = false + f.ack() + await expect(opening).resolves.toBe(false) + const controller = new AbortController() + const cancelled = f.open({ signal: controller.signal }) + await vi.advanceTimersByTimeAsync(0) + controller.abort() + await expect(cancelled).resolves.toBe(false) + expect(f.ipc.listenerCount(DESKTOP_IPC_CHANNELS.appOpenTargetAck)).toBe(0) +}) diff --git a/apps/buddy/electron/main/app/desktopQuitLifecycle.ts b/apps/buddy/electron/main/app/desktopQuitLifecycle.ts index 6f125b542..dfb4dcc1c 100644 --- a/apps/buddy/electron/main/app/desktopQuitLifecycle.ts +++ b/apps/buddy/electron/main/app/desktopQuitLifecycle.ts @@ -1,8 +1,12 @@ +import type { DesktopExitBehavior } from '../../../shared/lifecycle/exitBehavior' import type { ApplicationEvents } from '../../../shared/observability/ApplicationEvents' import type { DesktopQuitOptions } from './typing' interface DesktopQuitLifecycleOptions { events?: ApplicationEvents + getExitBehavior?: () => DesktopExitBehavior + closeDesktop?: () => Promise + restartDesktop?: () => Promise confirm: (options: DesktopQuitOptions) => Promise dispose: () => Promise quit: (restart: boolean) => void @@ -20,6 +24,7 @@ export function createDesktopQuitLifecycle(options: DesktopQuitLifecycleOptions) request(input: DesktopQuitOptions = {}, prepare?: () => Promise<(() => void) | void>): Promise { if (pending) return pending + const behavior = input.reason || input.restart || prepare ? 'stop_service' : options.getExitBehavior?.() ?? 'stop_service' pending = Promise.resolve().then(async () => { options.events?.publish({ event: 'app.quit_requested', level: 'info' }) if (!(await options.confirm(input))) { @@ -27,6 +32,18 @@ export function createDesktopQuitLifecycle(options: DesktopQuitLifecycleOptions) return } options.events?.publish({ event: 'app.quit_confirmed', level: 'info' }) + if (behavior === 'keep_service') { + if (!options.closeDesktop) + throw new Error('DESKTOP_EXIT_SCOPE_UNAVAILABLE') + await options.closeDesktop() + return + } + if (input.restart && options.restartDesktop && !prepare) { + await options.restartDesktop() + quitting = true + committed = true + return + } const finish = prepare ? await prepare() : undefined quitting = true await options.dispose() diff --git a/apps/buddy/electron/main/app/desktopServiceEntry.ts b/apps/buddy/electron/main/app/desktopServiceEntry.ts new file mode 100644 index 000000000..17ea6a8b5 --- /dev/null +++ b/apps/buddy/electron/main/app/desktopServiceEntry.ts @@ -0,0 +1,105 @@ +import type { LocalServiceLaunchOptions } from '../../../platform/service/launchLocalService' +import { randomUUID } from 'node:crypto' +import { homedir, tmpdir } from 'node:os' +import { dirname, join } from 'node:path' +import process from 'node:process' +import { app, dialog } from 'electron' +import originalFs from 'original-fs' +import buddyPackage from '../../../package.json' +import { ensurePrivateDirectoriesSync } from '../../../platform/filesystem/privateDirectories' +import { resolveBuddyPrivateDirectories } from '../../../platform/native/nativeHost' +import { launchLocalService } from '../../../platform/service/launchLocalService' +import { acquireStorageRootLease } from '../../../platform/storage/StorageRootLease' +import { resolveBuddyRuntimePaths } from '../paths' +import { INSTALLER_EXIT_ARGUMENT, resolveDesktopLaunchIntent } from '../startupIntent' +import { requestPersistenceUpgrade } from '../storage/requestPersistenceUpgrade' +import { prepareStorageBootstrap, readStorageBootstrap } from '../storage/storageBootstrap' + +export async function startDesktopServiceEntry(): Promise { + if (process.argv.includes('--lexora-platform-host') || process.argv.includes('--lexora-storage-maintenance') || process.env.LEXORA_BUDDY_SERVICE_MODE === 'utility' || (!app.isPackaged && process.env.LEXORA_BUDDY_SERVICE_MODE !== 'daemon')) + return false + try { + const paths = resolveBuddyRuntimePaths({ + defaultUserData: app.getPath('userData'), + desktopName: buddyPackage.desktopName, + isPackaged: app.isPackaged, + localAppData: process.env.LOCALAPPDATA, + lexoraHomeOverride: process.env.LEXORA_HOME, + profileOverride: process.env.LEXORA_BUDDY_PROFILE, + smokeTest: process.env.LEXORA_DESKTOP_SMOKE_TEST === '1', + temporaryDirectory: tmpdir(), + userDataOverride: app.commandLine.hasSwitch('user-data-dir') ? app.commandLine.getSwitchValue('user-data-dir') : undefined, + userHome: homedir(), + userId: process.geteuid?.() ?? 0, + xdgCacheHome: process.env.XDG_CACHE_HOME, + xdgConfigHome: process.env.XDG_CONFIG_HOME, + xdgRuntimeDirectory: process.env.XDG_RUNTIME_DIR, + xdgStateHome: process.env.XDG_STATE_HOME, + }) + const storage = readStorageBootstrap(paths.lexoraHome) + const previousUserData = app.getPath('userData') + const previousSessionData = app.getPath('sessionData') + const launcherData = join(storage.bootstrapRoot, 'launcher') + ensurePrivateDirectoriesSync([launcherData], resolveBuddyPrivateDirectories({ appPath: app.getAppPath(), resourcesPath: process.resourcesPath, isPackaged: app.isPackaged })) + app.setName(paths.appName) + app.setPath('userData', launcherData) + app.setPath('sessionData', launcherData) + const platformArguments = ['ozone-platform', 'password-store', 'user-data-dir', 'disable-gpu', 'disable-setuid-sandbox'].flatMap((name) => { + if (!app.commandLine.hasSwitch(name)) + return [] + const value = app.commandLine.getSwitchValue(name) + return [value ? `--${name}=${value}` : `--${name}`] + }) + const options: LocalServiceLaunchOptions = { + releaseFileSystem: originalFs, + releaseSetPath: app.isPackaged ? join(process.resourcesPath, 'release-set.json') : join(app.getAppPath(), '.output/release-set.json'), + profileRoot: storage.state.activeRoot, + defaultRoot: paths.lexoraHome, + profile: paths.profile, + hostExecutable: process.execPath, + hostArguments: [...platformArguments, ...app.isPackaged ? [] : [app.getAppPath()]], + leaseDirectory: join(paths.profile === 'test' ? dirname(paths.lexoraHome) : homedir(), '.lexora-locations', 'locks'), + storageAuthority: 'sqlite', + } + let connection = await launchLocalService({ ...options, startIfMissing: false }) + if (!connection && !process.argv.includes(INSTALLER_EXIT_ARGUMENT)) { + const release = acquireStorageRootLease(storage.state.activeRoot, options.leaseDirectory, resolveBuddyPrivateDirectories({ appPath: app.getAppPath(), resourcesPath: process.resourcesPath, isPackaged: app.isPackaged })) + let maintenance: boolean + try { + const prepared = prepareStorageBootstrap(paths, resolveBuddyPrivateDirectories({ appPath: app.getAppPath(), resourcesPath: process.resourcesPath, isPackaged: app.isPackaged })) + await requestPersistenceUpgrade(prepared) + maintenance = !!(await prepared.store.read()).pending + } + finally { release() } + if (maintenance) { + app.setPath('userData', previousUserData) + app.setPath('sessionData', previousSessionData) + return false + } + connection = await launchLocalService(options) + } + if (!connection) { + app.exit(0) + return true + } + try { + if (process.argv.includes(INSTALLER_EXIT_ARGUMENT)) { + await connection.peer.request('service.stop', { expectedServiceInstanceId: connection.discovery.serviceInstanceId, commandId: randomUUID(), reason: 'maintenance' }) + await connection.waitForExit() + } + else if (resolveDesktopLaunchIntent(process.argv, process.platform === 'darwin' && app.getLoginItemSettings().wasOpenedAtLogin) === 'foreground') { + await connection.peer.request('service.openDesktop', { expectedServiceInstanceId: connection.discovery.serviceInstanceId, intent: { kind: 'tasks' } }) + } + } + finally { connection.peer.close(new Error('SERVICE_LAUNCHER_FINISHED')) } + app.exit(0) + } + catch (error) { + await app.whenReady() + const code = error instanceof Error && /^[A-Z_]+$/.test(error.message) ? error.message : 'SERVICE_START_FAILED' + process.stderr.write(`${code}\n`) + dialog.showErrorBox('Lexora', `无法启动后台服务。请保留当前数据并重试。\n${code}`) + app.exit(1) + } + return true +} diff --git a/apps/buddy/electron/main/app/environment.ts b/apps/buddy/electron/main/app/environment.ts index 481940000..8f1a5429e 100644 --- a/apps/buddy/electron/main/app/environment.ts +++ b/apps/buddy/electron/main/app/environment.ts @@ -1,5 +1,5 @@ import type { DesktopEnvironment } from './typing' -import { mkdirSync } from 'node:fs' +import { mkdirSync, realpathSync } from 'node:fs' import { homedir, tmpdir } from 'node:os' import { dirname, join } from 'node:path' import process from 'node:process' @@ -18,6 +18,7 @@ import { resolveBuddyRuntimePaths } from '../paths' import { desktopHosts } from '../platform/desktopHost' import { rendererSchemePrivileges } from '../rendererProtocol' import { resolveDesktopLaunchIntent } from '../startupIntent' +import { requestPersistenceUpgrade } from '../storage/requestPersistenceUpgrade' import { prepareStorageBootstrap, readStorageBootstrap, unifiedStoragePaths } from '../storage/storageBootstrap' import { fileIconSchemePrivileges } from '../themes/registerFileIconIpc' import { themeSchemePrivileges } from '../themes/registerThemeIpc' @@ -27,7 +28,7 @@ import { DesktopStartup } from './DesktopStartup' import { checkDesktopDirectories, criticalDesktopDirectories, DesktopInstanceLockUnavailable, prepareDesktopPrivateStorage } from './desktopStorage' import { observeStartupDiagnostics } from './startupDiagnostics' -export function prepareDesktopEnvironment(): DesktopEnvironment { +export async function prepareDesktopEnvironment(platformHost?: DesktopEnvironment['platformHost']): Promise { if (currentPlatform.id === OPERATING_SYSTEM.Linux) app.commandLine.appendSwitch('enable-speech-dispatcher') const desktopHost = desktopHosts[currentPlatform.id] @@ -63,8 +64,27 @@ export function prepareDesktopEnvironment(): DesktopEnvironment { if (!app.requestSingleInstanceLock({ executablePath: process.execPath })) throw new DesktopInstanceLockUnavailable(legacyPaths.userData) const storage = prepareStorageBootstrap(legacyPaths, privateDirectoriesExecutable()) - const releaseStorage = acquireStorageRootLease(storage.state.activeRoot, storage.leaseDirectory, storage.privateDirectoriesExecutable) - app.once('will-quit', releaseStorage) + if (platformHost) { + if (platformHost.peer.closed || storage.state.pending || realpathSync(storage.state.activeRoot) !== realpathSync(platformHost.bootstrap.profileRoot) || legacyPaths.profile !== platformHost.bootstrap.profile) + throw new Error('PLATFORM_HOST_PROFILE_CHANGED') + } + else if (!storage.state.pending) { + const releaseStorage = acquireStorageRootLease(storage.state.activeRoot, storage.leaseDirectory, storage.privateDirectoriesExecutable) + try { + if (!process.argv.includes('--lexora-storage-maintenance') && process.env.LEXORA_BUDDY_STORAGE_AUTHORITY === 'sqlite') { + await requestPersistenceUpgrade(storage) + storage.state = await storage.store.read() + } + } + catch (error) { + releaseStorage() + throw error + } + if (storage.state.pending) + releaseStorage() + else + app.once('will-quit', releaseStorage) + } const paths = unifiedStoragePaths(legacyPaths, storage.state.activeRoot) if (process.env.LEXORA_BUDDY_PET_STATE_PATH) paths.nativePetState = legacyPaths.nativePetState @@ -84,6 +104,7 @@ export function prepareDesktopEnvironment(): DesktopEnvironment { events.subscribe(event => diagnostics.record({ ...event, scope: 'desktop' })) observeStartupDiagnostics(startup, events) return { + ...(platformHost ? { platformHost } : {}), diagnostics, events, startup, @@ -131,9 +152,7 @@ export function initializeDesktopEnvironment(environment: DesktopEnvironment): v environment.diagnostics.record({ scope: 'desktop', level: 'warn', event: 'crash_reporter.unavailable', ...readDiagnosticError(error) }) } } - bootstrapStep('register_protocols', () => { - protocol.registerSchemesAsPrivileged([...attachmentSchemePrivileges, rendererSchemePrivileges, extensionSchemePrivileges, fileIconSchemePrivileges, themeSchemePrivileges]) - }) + registerDesktopSchemes() bootstrapStep('desktop_identity', () => desktopHosts[currentPlatform.id].setIdentity?.(paths.desktopName)) } @@ -163,3 +182,13 @@ export async function prepareDesktopReady(environment: DesktopEnvironment): Prom app.setAppUserModelId(paths.desktopName) Menu.setApplicationMenu(null) } + +let desktopSchemesRegistered = false +export function registerDesktopSchemes(): void { + if (desktopSchemesRegistered) + return + bootstrapStep('register_protocols', () => { + protocol.registerSchemesAsPrivileged([...attachmentSchemePrivileges, rendererSchemePrivileges, extensionSchemePrivileges, fileIconSchemePrivileges, themeSchemePrivileges]) + }) + desktopSchemesRegistered = true +} diff --git a/apps/buddy/electron/main/app/requestDesktopTarget.ts b/apps/buddy/electron/main/app/requestDesktopTarget.ts new file mode 100644 index 000000000..0905a1afd --- /dev/null +++ b/apps/buddy/electron/main/app/requestDesktopTarget.ts @@ -0,0 +1,78 @@ +import type { BrowserWindow, IpcMain, IpcMainEvent } from 'electron' +import type { z } from 'zod' +import type { desktopOpenIntentSchema } from '../../../shared/service/localServiceProtocol' +import type { DesktopStartup } from './DesktopStartup' +import { randomUUID } from 'node:crypto' +import { ipcMain } from 'electron' +import { desktopTargetAckSchema } from '../../../shared/service/localServiceProtocol' +import { DESKTOP_IPC_CHANNELS } from '../../shared/desktopApi' + +export async function requestDesktopTarget(window: BrowserWindow, startup: DesktopStartup, intent: z.infer, ipc: Pick = ipcMain, options: { signal?: AbortSignal, available?: () => boolean } = {}): Promise { + const deadline = Date.now() + 20000 + const available = () => !options.signal?.aborted && (options.available?.() ?? true) + if (!available()) + return false + await new Promise((resolve, reject) => { + let stop = () => {} + let timeout: ReturnType + function close() { + finish(new Error('DESKTOP_CLOSED')) + } + function finish(error?: Error) { + clearTimeout(timeout) + stop() + window.off('closed', close) + options.signal?.removeEventListener('abort', close) + if (error) + reject(error) + else resolve() + } + timeout = setTimeout(() => finish(new Error('DESKTOP_RENDERER_NOT_READY')), 20000) + const check = () => { + const state = startup.state + if (window.isDestroyed()) + close() + else if (state.status === 'ready') + finish() + else if (state.status === 'failed' || state.status === 'stopped') + finish(new Error('DESKTOP_RENDERER_NOT_READY')) + } + stop = startup.onStateChange(check) + window.once('closed', close) + options.signal?.addEventListener('abort', close, { once: true }) + check() + }) + if (!available() || window.isDestroyed() || window.webContents.isDestroyed()) + return false + const requestId = randomUUID() + return new Promise((resolve) => { + let timeout: ReturnType + function finish(opened: boolean) { + clearTimeout(timeout) + ipc.off(DESKTOP_IPC_CHANNELS.appOpenTargetAck, receive) + window.off('closed', close) + options.signal?.removeEventListener('abort', close) + resolve(opened) + } + function close() { + finish(false) + } + function receive(event: IpcMainEvent, raw: unknown) { + if (window.isDestroyed() || event.sender !== window.webContents || event.senderFrame !== window.webContents.mainFrame) + return + const result = desktopTargetAckSchema.safeParse(raw) + if (result.success && result.data.requestId === requestId) + finish(available() && result.data.opened) + } + timeout = setTimeout(finish, Math.max(0, deadline - Date.now()), false) + ipc.on(DESKTOP_IPC_CHANNELS.appOpenTargetAck, receive) + window.once('closed', close) + options.signal?.addEventListener('abort', close, { once: true }) + try { + window.webContents.send(DESKTOP_IPC_CHANNELS.appOpenTargetRequest, { requestId, intent }) + } + catch { + finish(false) + } + }) +} diff --git a/apps/buddy/electron/main/app/typing.ts b/apps/buddy/electron/main/app/typing.ts index 2369ff12a..96b0e9c85 100644 --- a/apps/buddy/electron/main/app/typing.ts +++ b/apps/buddy/electron/main/app/typing.ts @@ -3,11 +3,13 @@ import type { ApplicationEvents } from '../../../shared/observability/Applicatio import type { LexoraConfig } from '../../shared/desktopApi' import type { DesktopDiagnosticLogger } from '../desktopDiagnostics' import type { BuddyRuntimePaths } from '../paths' +import type { PlatformHostConnection } from '../runtime/platformHostBootstrap' import type { DesktopLaunchIntent } from '../startupIntent' import type { DesktopStorage } from '../storage/storageBootstrap' import type { DesktopStartup } from './DesktopStartup' export interface DesktopEnvironment { + platformHost?: PlatformHostConnection events: ApplicationEvents startup: DesktopStartup diagnostics: DesktopDiagnosticLogger @@ -22,7 +24,7 @@ export interface DesktopEnvironment { } export interface DesktopQuitOptions { - reason?: 'installation' + reason?: 'installation' | 'session_end' | 'service_stop' discardDraftsOnFailure?: boolean restart?: boolean } diff --git a/apps/buddy/electron/main/attachmentProtocol.ts b/apps/buddy/electron/main/attachmentProtocol.ts index 4e221689d..f1f97f1de 100644 --- a/apps/buddy/electron/main/attachmentProtocol.ts +++ b/apps/buddy/electron/main/attachmentProtocol.ts @@ -1,4 +1,5 @@ import type { BuddyServiceSupervisor } from './runtime/BuddyServiceSupervisor' +import { Buffer } from 'node:buffer' import { readFile } from 'node:fs/promises' import { dirname } from 'node:path' import process from 'node:process' @@ -8,6 +9,7 @@ import { resolveBuddyFileReader } from '../../platform/native/nativeHost' import { artifactsRequestSchemas, artifactsResponseSchemas } from '../../shared/artifacts/artifactApi' import { attachmentsRequestSchemas } from '../../shared/conversation/attachmentApi' import { composerResourcesRpc } from '../../shared/conversation/composerApi' +import { attachmentPreviewBytesSchema } from '../../shared/runtime/attachmentHostProtocol' import { handleMediaRequest } from './mediaProtocol' import { resolveDevelopmentRendererUrl } from './security/navigationPolicy' @@ -20,7 +22,7 @@ export const attachmentSchemePrivileges: Electron.CustomScheme[] = [ATTACHMENT_P privileges: { secure: true, standard: true, stream: true, supportFetchAPI: true, corsEnabled: scheme === ARTIFACT_PROTOCOL || scheme === ATTACHMENT_PROTOCOL }, })) -export function installAttachmentProtocol(runtime: BuddyServiceSupervisor): () => void { +export function installAttachmentProtocol(runtime: Pick): () => void { const fileReader = resolveBuddyFileReader({ appPath: app.getAppPath(), isPackaged: app.isPackaged, resourcesPath: process.resourcesPath }) const developmentUrl = resolveDevelopmentRendererUrl(process.env.ELECTRON_RENDERER_URL, app.isPackaged) const rendererOrigin = developmentUrl ? new URL(developmentUrl).origin : 'lexora-app://renderer' @@ -70,6 +72,10 @@ export function installAttachmentProtocol(runtime: BuddyServiceSupervisor): () = try { const input = schema.parse({ [idKey]: id }) + if (scheme === ATTACHMENT_PROTOCOL) { + const preview = attachmentPreviewBytesSchema.parse(await runtime.request('attachments.readPreview', input)) + return new Response(Buffer.from(preview.data, 'base64'), { headers: { 'cache-control': 'private, max-age=300', 'content-type': preview.mimeType, 'x-content-type-options': 'nosniff' } }) + } const preview = artifactsResponseSchemas.artifactPreview.parse( await runtime.request(method, input), ) diff --git a/apps/buddy/electron/main/browser/BrowserHost.ts b/apps/buddy/electron/main/browser/BrowserHost.ts index 66d23016e..934c31296 100644 --- a/apps/buddy/electron/main/browser/BrowserHost.ts +++ b/apps/buddy/electron/main/browser/BrowserHost.ts @@ -15,7 +15,6 @@ import type { BrowserWaitSpec, } from '../../../shared/browser' import type { - DesktopBrowserError, DesktopBrowserGuestDescriptor, DesktopBrowserProfileMode, DesktopBrowserSetSurfaceInput, @@ -24,6 +23,7 @@ import type { import type { BrowserLocateElementInput, BrowserPickResult } from '../../../shared/browser/browserSelection' import type { NetworkAuthorizer } from '../../../shared/network/networkAuthorization' import type { BrowserHostChange, BrowserHostFact } from './BrowserHostEvents' +import type { BrowserLocalFileGrant } from './BrowserNavigation' import type { BrowserPage } from './BrowserPageSession' import type { BrowserSessionTeardownReason } from './BrowserSessionRegistry' import type { SemanticBrowserDriver, SemanticBrowserScreenshot, SemanticBrowserScreenshotReference } from './SemanticBrowserDriver' @@ -32,11 +32,9 @@ import { BROWSER_WAIT_DEFAULT_QUIET_MS, browserAgentTabId } from '../../../share import { Emitter } from '../../../shared/events/Emitter' import { copyEventSnapshot } from '../../../shared/events/eventSnapshot' import { BrowserHostError } from './BrowserHostError' +import { BrowserNavigation } from './BrowserNavigation' import { BrowserOperationGuard } from './BrowserOperationGuard' import { BrowserPageSession, snapshot } from './BrowserPageSession' -import { - BrowserSecurityPolicyError, -} from './BrowserSecurityPolicy' import { BrowserSessionRegistry, BrowserSessionRegistryError, @@ -47,8 +45,6 @@ import { export const BROWSER_DEFAULT_PARTITION = 'persist:buddy-browser-default-v1' const BROWSER_GUEST_ATTACH_TIMEOUT_MS = 10_000 -const BROWSER_NAVIGATION_WAIT_TIMEOUT_MS = 30_000 -const BROWSER_FAILED_NAVIGATION_SETTLE_MS = 200 const BROWSER_NAVIGATION_SETTLE_TIMEOUT_MS = 15_000 const BROWSER_ACTION_NAVIGATION_DETECTION_MS = 50 const BROWSER_WAIT_STATE_INTERVAL_MS = 50 @@ -64,17 +60,12 @@ interface BrowserHostOptions { createPage?: (descriptor: DesktopBrowserGuestDescriptor) => BrowserPage requestGuestAttachment?: () => void revokeSession?: (sessionId: string) => void - window: BrowserWindow + window?: BrowserWindow } type BrowserWaitAction = Extract type BrowserWaitRequest = BrowserWaitAction | BrowserWaitSpec -export interface BrowserLocalFileGrant { - entryPath: string - rootPath: string -} - export interface BrowserScreenshotReadInput extends SemanticBrowserScreenshotReference { owner?: BrowserPageOwner pageId: string @@ -103,11 +94,11 @@ export class BrowserHost { readonly #createId: () => string readonly #createPage: ((descriptor: DesktopBrowserGuestDescriptor) => BrowserPage) | null readonly #evictedConversationIds = new Set() - readonly #failedNavigationTargets = new WeakMap() + readonly #navigation: BrowserNavigation readonly #requestGuestAttachment: () => void readonly #revokeSession: (sessionId: string) => void readonly #sessions: BrowserSessionRegistry - readonly #window: BrowserWindow + readonly #window: BrowserWindow | undefined readonly #windowClosedListener: () => void readonly #willAttachWebviewListener: ( event: ElectronEvent, @@ -133,13 +124,23 @@ export class BrowserHost { createId: this.#createId, maxSessions: options.maxSessions ?? 16, }) + this.#navigation = new BrowserNavigation({ + sessions: this.#sessions, + operations: this.#operations, + requireSession: id => this.#requireSession(id), + waitForPage: session => this.#waitForPage(session), + assertCurrentPage: (session, pageId, operation) => this.#assertCurrentPage(session, pageId, operation), + notFound: id => this.#sessionNotFound(id), + onDidChange: this.onDidChange, + recordTiming: (session, phase, startedAt) => this.#recordTiming(session, phase, startedAt), + }) this.#window = options.window this.#windowClosedListener = () => this.dispose() this.#willAttachWebviewListener = (event, webPreferences, params) => { this.#configureGuestAttachment(event, webPreferences, params) } - this.#window.once('closed', this.#windowClosedListener) - this.#window.webContents.on('will-attach-webview', this.#willAttachWebviewListener) + this.#window?.once('closed', this.#windowClosedListener) + this.#window?.webContents.on('will-attach-webview', this.#willAttachWebviewListener) } get isDisposed(): boolean { @@ -296,7 +297,7 @@ export class BrowserHost { listGuests(): DesktopBrowserGuestDescriptor[] { this.#assertActive() - return this.#sessions.values().map(session => ({ ...session.descriptor })) + return this.#sessions.values().map(session => ({ ...session.descriptor, pageId: session.state.pageId })) } attachGuest(sessionId: string, page: BrowserPage): void { @@ -493,6 +494,7 @@ export class BrowserHost { this.#operations.assertCanMutate() this.assertOwner(input.sessionId, input.owner) const queuedSession = this.#requireSession(input.sessionId) + this.assertControl(input) const predecessor = queuedSession.actionTail let releaseQueue!: () => void queuedSession.actionTail = new Promise((resolve) => { @@ -639,376 +641,16 @@ export class BrowserHost { } } - async navigate(sessionId: string, rawUrl: string, readiness: 'load' | 'document-ready' = 'load', assertControl: () => void = () => {}): Promise { - assertControl() - this.#operations.assertCanMutate() - const session = this.#requireSession(sessionId) - const startedAt = performance.now() - const navigationSequence = this.#beginPageAction(session) - let url: string - try { - await this.#waitForPage(session) - this.#requireSession(sessionId) - if (!this.#shouldContinuePageAction(session, navigationSequence)) - return snapshot(session.state) - this.#recordTiming(session, 'guest_ready', startedAt) - this.#assertNavigationControl(session, navigationSequence, assertControl) - this.#operations.assertCanMutate() - const authorizationStartedAt = performance.now() - url = await session.securityPolicy?.authorizeNavigation(rawUrl) ?? '' - this.#recordTiming(session, 'authorized', authorizationStartedAt) - } - catch (error) { - this.#requireSession(sessionId) - if (error instanceof BrowserHostError && error.code === 'BROWSER_CONTROL_REQUIRED') - throw error - if (session.state.error?.code === 'BROWSER_PAGE_UNRESPONSIVE') - throw new BrowserHostError('BROWSER_PAGE_UNRESPONSIVE', session.state.error.message) - if (!this.#shouldContinuePageAction(session, navigationSequence)) - return snapshot(session.state) - this.#failedNavigationTargets.set(session, rawUrl) - const failure: DesktopBrowserError = error instanceof BrowserSecurityPolicyError - ? { code: error.code, message: error.message, reason: error.reason } - : { code: 'BROWSER_PAGE_FAILED', message: browserLoadErrorMessage(error) } - session.state.error = failure - session.state.status = 'error' - this.#finishPageAction(session, navigationSequence) - this.#publish(session) - throw error instanceof BrowserHostError - ? error - : new BrowserHostError(failure.code, failure.message, failure.reason) - } - if (!this.#shouldContinuePageAction(session, navigationSequence)) - return snapshot(session.state) - - this.#assertNavigationControl(session, navigationSequence, assertControl) - return this.#loadPage(sessionId, session, navigationSequence, url, readiness, assertControl) + navigate(sessionId: string, rawUrl: string, readiness: 'load' | 'document-ready' = 'load', assertControl: () => void = () => {}): Promise { + return this.#navigation.navigate(sessionId, rawUrl, readiness, assertControl) } - async #loadPage( - sessionId: string, - session: BrowserPageSession, - navigationSequence: number, - url: string, - readiness: 'load' | 'document-ready', - assertControl: () => void, - ): Promise { - return session.runWhileActive(async () => { - const startedAt = performance.now() - const page = session.requirePage() - const pageId = session.state.pageId - let hasCommittedDocument = false - let resolveWait!: (state: DesktopBrowserState) => void - let rejectWait!: (error: unknown) => void - const waiting = new Promise((resolve, reject) => { - resolveWait = resolve - rejectWait = reject - }) - const checkInterrupted = (): boolean => { - try { - this.#requireSession(sessionId) - this.#assertCurrentPage(session, pageId, 'while waiting for navigation') - assertControl() - if (!this.#shouldContinuePageAction(session, navigationSequence)) { - resolveWait(snapshot(session.state)) - return true - } - const error = session.state.error - if (error?.code === 'BROWSER_PAGE_CRASHED' || error?.code === 'BROWSER_PAGE_UNRESPONSIVE') - throw new BrowserHostError(error.code, error.message, error.reason) - return false - } - catch (error) { - rejectWait(error) - return true - } - } - const onDocumentCommitted = () => { - if (this.#shouldContinuePageAction(session, navigationSequence)) - hasCommittedDocument = true - } - const onDocumentReady = () => { - if (checkInterrupted() || readiness !== 'document-ready' || session.mainFrameCommitSequence !== navigationSequence) - return - this.#recordTiming(session, 'document_ready', startedAt) - resolveWait(snapshot(session.state)) - } - const subscription = this.onDidChange((change) => { - if ((change.kind === 'state' || change.kind === 'session') && change.state.sessionId === sessionId) - checkInterrupted() - }) - page.on('did-navigate', onDocumentCommitted) - page.on('dom-ready', onDocumentReady) - const timer = setTimeout(() => { - if (checkInterrupted()) - return - const failure = session.state.error - if (failure) { - rejectWait(new BrowserHostError(failure.code, failure.message, failure.reason)) - return - } - if (hasCommittedDocument && session.mainFrameCommitSequence === navigationSequence) { - resolveWait(snapshot(session.state)) - return - } - const message = 'Browser navigation did not commit before its deadline' - rejectWait(new BrowserHostError('BROWSER_PAGE_FAILED', message)) - session.navigationSequence += 1 - this.#finishPageAction(session, navigationSequence) - this.#failedNavigationTargets.set(session, url) - session.state.error = { code: 'BROWSER_PAGE_FAILED', message } - session.state.status = 'error' - try { - page.stop() - } - catch {} - this.#publish(session) - }, BROWSER_NAVIGATION_WAIT_TIMEOUT_MS) - try { - return await Promise.race([ - waiting, - this.#loadPageToCompletion(sessionId, session, navigationSequence, url, assertControl), - ]) - } - finally { - clearTimeout(timer) - subscription.dispose() - page.off('did-navigate', onDocumentCommitted) - page.off('dom-ready', onDocumentReady) - } - }, { timeoutMs: null }) - } - - async #loadPageToCompletion( - sessionId: string, - session: BrowserPageSession, - navigationSequence: number, - url: string, - assertControl: () => void, - ): Promise { - const startedAt = performance.now() - session.state.url = url - this.#publish(session) - try { - const page = await this.#waitForPage(session) - if (!this.#shouldContinuePageAction(session, navigationSequence)) - return snapshot(session.state) - this.#assertNavigationControl(session, navigationSequence, assertControl) - await page.loadURL(url) - this.#recordTiming(session, 'load_complete', startedAt) - if (!this.#sessions.get(sessionId)) - throw this.#sessionNotFound(sessionId) - if (!this.#isCurrentPageAction(session, navigationSequence)) - return snapshot(session.state) - session.stoppedNavigationSequence = null - this.#finishPageAction(session, navigationSequence) - session.removeBootstrapHistory() - session.refreshPageState() - session.state.status = 'ready' - this.#publish(session) - return snapshot(session.state) - } - catch (error) { - if (error instanceof BrowserHostError) - throw error - if (!this.#sessions.get(sessionId)) - throw this.#sessionNotFound(sessionId) - if (!this.#isCurrentPageAction(session, navigationSequence)) - return snapshot(session.state) - if (session.stoppedNavigationSequence === navigationSequence) { - session.stoppedNavigationSequence = null - this.#finishPageAction(session, navigationSequence) - session.state.error = null - session.state.status = session.state.url === 'about:blank' ? 'idle' : 'ready' - session.refreshPageState() - this.#publish(session) - return snapshot(session.state) - } - return this.#waitForNavigationSettlement( - sessionId, - session, - navigationSequence, - error, - ) - } - } - - async #waitForNavigationSettlement( - sessionId: string, - session: BrowserPageSession, - navigationSequence: number, - loadError: unknown, - ): Promise { - const startedAt = Date.now() - const deadline = startedAt + BROWSER_NAVIGATION_SETTLE_TIMEOUT_MS - const isAbortedLoad = isNavigationAborted(loadError) - const loadFailureDeadline = isAbortedLoad - ? null - : Math.min(deadline, startedAt + BROWSER_FAILED_NAVIGATION_SETTLE_MS) - let pageFailureDeadline: number | null = null - while (true) { - if (this.#sessions.get(sessionId) !== session) - throw this.#sessionNotFound(sessionId) - if (!this.#isCurrentPageAction(session, navigationSequence)) - return snapshot(session.state) - if (session.stoppedNavigationSequence === navigationSequence) { - session.stoppedNavigationSequence = null - this.#finishPageAction(session, navigationSequence) - session.state.error = null - session.state.status = session.state.url === 'about:blank' ? 'idle' : 'ready' - session.refreshPageState() - this.#publish(session) - return snapshot(session.state) - } - const hasReplacementCommit = session.mainFrameCommitSequence === navigationSequence - const pageFailure = session.state.error - const now = Date.now() - if (pageFailure) { - pageFailureDeadline ??= Math.min( - deadline, - now + BROWSER_FAILED_NAVIGATION_SETTLE_MS, - ) - } - else { - pageFailureDeadline = null - } - if ( - pageFailure - && ( - session.activeNavigationSequence !== navigationSequence - || now >= (pageFailureDeadline ?? deadline) - ) - ) { - this.#finishPageAction(session, navigationSequence) - throw new BrowserHostError( - pageFailure.code, - pageFailure.message, - pageFailure.reason ?? null, - ) - } - if ( - session.state.status === 'ready' - && (isAbortedLoad || hasReplacementCommit) - ) { - return snapshot(session.state) - } - if (loadFailureDeadline !== null && now >= loadFailureDeadline) { - const message = browserLoadErrorMessage(loadError) - this.#finishPageAction(session, navigationSequence) - session.state.error = { code: 'BROWSER_PAGE_FAILED', message } - session.state.status = 'error' - this.#publish(session) - throw new BrowserHostError('BROWSER_PAGE_FAILED', message) - } - const remainingMs = Math.min( - pageFailureDeadline ?? deadline, - loadFailureDeadline ?? deadline, - ) - now - if (remainingMs <= 0) { - const message = 'Browser navigation did not settle after the initial load was replaced' - this.#finishPageAction(session, navigationSequence) - session.state.error = { code: 'BROWSER_PAGE_FAILED', message } - session.state.status = 'error' - this.#publish(session) - throw new BrowserHostError('BROWSER_PAGE_FAILED', message) - } - await wait(Math.min(remainingMs, 50)) - } - } - - goBack(sessionId: string): void { - this.#operations.assertCanMutate() - const session = this.#requireSession(sessionId) - const history = session.requirePage().navigationHistory - if (!history.canGoBack()) - return - this.#beginPageAction(session) - history.goBack() - } - - goForward(sessionId: string): void { - this.#operations.assertCanMutate() - const session = this.#requireSession(sessionId) - const history = session.requirePage().navigationHistory - if (!history.canGoForward()) - return - this.#beginPageAction(session) - history.goForward() - } - - async reload(sessionId: string, assertControl: () => void = () => {}): Promise { - assertControl() - this.#operations.assertCanMutate() - const session = this.#requireSession(sessionId) - const failedTarget = this.#failedNavigationTargets.get(session) - if (session.state.status === 'error' && failedTarget !== undefined) { - await this.navigate(sessionId, failedTarget, 'load', assertControl) - return - } - if (session.state.url === 'about:blank') - return - this.#beginPageAction(session) - session.requirePage().reload() - } - - stop(sessionId: string): void { - const session = this.#requireSession(sessionId) - if (session.state.status !== 'loading') - return - session.stoppedNavigationSequence = session.navigationSequence - this.#finishPageAction(session, session.navigationSequence) - if (session.page && !session.page.isDestroyed()) - session.page.stop() - session.refreshPageState() - session.state.error = null - session.state.status = session.state.url === 'about:blank' ? 'idle' : 'ready' - this.#publish(session) - } - - async openLocalFile( - sessionId: string, - grant: BrowserLocalFileGrant, - readiness: 'load' | 'document-ready' = 'load', - assertControl: () => void = () => {}, - ): Promise { - assertControl() - this.#operations.assertCanMutate() - const session = this.#requireSession(sessionId) - const startedAt = performance.now() - await this.#waitForPage(session) - this.#recordTiming(session, 'guest_ready', startedAt) - this.#operations.assertCanMutate() - const navigationSequence = this.#beginPageAction(session) - const authorizationStartedAt = performance.now() - let url: string - try { - url = await session.securityPolicy?.authorizeLocalFile( - grant.entryPath, - grant.rootPath, - ) ?? '' - this.#recordTiming(session, 'authorized', authorizationStartedAt) - } - catch (error) { - if (session.state.error?.code === 'BROWSER_PAGE_UNRESPONSIVE') - throw new BrowserHostError('BROWSER_PAGE_UNRESPONSIVE', session.state.error.message) - if (!(error instanceof BrowserSecurityPolicyError)) - throw error - if (!this.#shouldContinuePageAction(session, navigationSequence)) - return snapshot(session.state) - session.state.error = { - code: error.code, - message: error.message, - reason: error.reason, - } - session.state.status = 'error' - this.#finishPageAction(session, navigationSequence) - this.#publish(session) - throw new BrowserHostError(error.code, error.message, error.reason) - } - if (!this.#shouldContinuePageAction(session, navigationSequence)) - return snapshot(session.state) - this.#assertNavigationControl(session, navigationSequence, assertControl) - return this.#loadPage(sessionId, session, navigationSequence, url, readiness, assertControl) + goBack(sessionId: string): void { this.#navigation.goBack(sessionId) } + goForward(sessionId: string): void { this.#navigation.goForward(sessionId) } + reload(sessionId: string, assertControl: () => void = () => {}): Promise { return this.#navigation.reload(sessionId, assertControl) } + stop(sessionId: string): void { this.#navigation.stop(sessionId) } + openLocalFile(sessionId: string, grant: BrowserLocalFileGrant, readiness: 'load' | 'document-ready' = 'load', assertControl: () => void = () => {}): Promise { + return this.#navigation.openLocalFile(sessionId, grant, readiness, assertControl) } async #replaceSessionProfile( @@ -1230,8 +872,8 @@ export class BrowserHost { if (this.#disposed) return this.#disposed = true - this.#window.off('closed', this.#windowClosedListener) - this.#window.webContents.off('will-attach-webview', this.#willAttachWebviewListener) + this.#window?.off('closed', this.#windowClosedListener) + this.#window?.webContents.off('will-attach-webview', this.#willAttachWebviewListener) const pending = this.#sessions.values().map(session => session.actionTail) this.#sessions.dispose() this.#evictedConversationIds.clear() @@ -1306,7 +948,7 @@ export class BrowserHost { session.state.status = 'error' } session.state.visible = false - this.#failedNavigationTargets.delete(session) + this.#navigation.release(session) this.#revokeSession(session.state.sessionId) session.releasePage() const cleanup = session.whenIdle() @@ -1327,49 +969,6 @@ export class BrowserHost { } } - #beginPageAction(session: BrowserPageSession): number { - this.#failedNavigationTargets.delete(session) - this.#sessions.touch(session.state.sessionId) - session.navigationSequence += 1 - session.activeNavigationSequence = session.navigationSequence - session.semanticDriver?.invalidateDocument() - session.stoppedNavigationSequence = null - session.state.error = null - session.state.status = 'loading' - this.#publish(session) - return session.navigationSequence - } - - #isCurrentPageAction(session: BrowserPageSession, navigationSequence: number): boolean { - return session.navigationSequence === navigationSequence - } - - #finishPageAction(session: BrowserPageSession, navigationSequence: number): void { - if (session.activeNavigationSequence === navigationSequence) - session.activeNavigationSequence = null - } - - #assertNavigationControl(session: BrowserPageSession, navigationSequence: number, assertControl: () => void): void { - try { - assertControl() - } - catch (error) { - if (this.#shouldContinuePageAction(session, navigationSequence)) { - this.#finishPageAction(session, navigationSequence) - session.refreshPageState() - session.state.error = null - session.state.status = session.page?.isLoading() ? 'loading' : session.state.url === 'about:blank' ? 'idle' : 'ready' - this.#publish(session) - } - throw error - } - } - - #shouldContinuePageAction(session: BrowserPageSession, navigationSequence: number): boolean { - return this.#isCurrentPageAction(session, navigationSequence) - && session.stoppedNavigationSequence !== navigationSequence - } - #publish(session: BrowserPageSession): void { session.publish() } @@ -1510,22 +1109,6 @@ export class BrowserHost { } } -function isNavigationAborted(error: unknown): boolean { - if (!error || typeof error !== 'object') - return false - const candidate = error as { code?: unknown, errno?: unknown } - if (candidate.code === 'ERR_ABORTED' || candidate.errno === -3) - return true - return error instanceof Error - && /^ERR_ABORTED(?: \(-3\))?(?: |$)/.test(error.message) -} - -function browserLoadErrorMessage(error: unknown): string { - if (error instanceof Error && error.message.trim()) - return error.message.slice(0, 1_024) - return 'Browser page failed to load' -} - function swallowDriverProbeFailure(error: unknown): false { if (error instanceof SemanticBrowserDriverError && error.code === 'BROWSER_PAGE_FAILED') return false @@ -1555,3 +1138,5 @@ function wait(durationMs: number): Promise { } export { BrowserHostError } from './BrowserHostError' + +export type { BrowserLocalFileGrant } from './BrowserNavigation' diff --git a/apps/buddy/electron/main/browser/BrowserIntegration.ts b/apps/buddy/electron/main/browser/BrowserIntegration.ts index 5a365f048..b3f5c1c84 100644 --- a/apps/buddy/electron/main/browser/BrowserIntegration.ts +++ b/apps/buddy/electron/main/browser/BrowserIntegration.ts @@ -1,5 +1,6 @@ import type { BrowserWindow } from 'electron' import type { LocalEndpoint } from '../../../platform/ipc/localTransport' +import type { DesktopBrowserPresentationInput } from '../../../shared/browser/browserDesktopApi' import type { BrowserPreferences } from '../../../shared/browser/browserPreferences' import type { ApplicationDiagnosticReporter } from '../../../shared/diagnostics/applicationDiagnostic' import { DESKTOP_IPC_CHANNELS } from '../../shared/desktopApi' @@ -8,6 +9,7 @@ import { BrowserAdapterTestLeasePublisher } from './BrowserAdapterTestLeasePubli import { BrowserDataService } from './BrowserDataService' import { BrowserHost } from './BrowserHost' import { BrowserOperationGuard } from './BrowserOperationGuard' +import { BrowserPresentationHost } from './BrowserPresentationHost' import { BrowserScreenshotService } from './BrowserScreenshotService' interface BrowserIntegrationOptions { @@ -23,6 +25,8 @@ export class BrowserIntegration { readonly adapter: BrowserAdapterServer readonly data: BrowserDataService readonly screenshots: BrowserScreenshotService + readonly #presentation = new BrowserPresentationHost((sessionId, visible) => this.#host?.setSurface({ sessionId, visible })) + #window: BrowserWindow | null = null readonly #operations = new BrowserOperationGuard() #host: BrowserHost | null = null #adapterStarted = false @@ -43,7 +47,23 @@ export class BrowserIntegration { } bindWindow(window: BrowserWindow): void { - this.closeWindow() + this.#window = window + this.#presentation.bindWindow(window) + } + + acquirePresentation(window: BrowserWindow): string { + return this.#presentation.acquire(window) + } + + async present(window: BrowserWindow, input: DesktopBrowserPresentationInput): Promise { + if (this.host?.getState(input.sessionId).pageId !== input.pageId) + throw new Error('BROWSER_PAGE_FAILED') + return this.#presentation.present(window, input) + } + + #startHost(): void { + if (this.host) + return this.#host = new BrowserHost({ getFreezeDelay: (visible) => { if (!this.#options.isTaskLinked?.()) @@ -54,22 +74,24 @@ export class BrowserIntegration { : null }, onActivityError: this.#options.onActivityError, - window, + createPage: descriptor => this.#presentation.createPage(descriptor), operations: this.#operations, getDefaultZoomFactor: () => this.#options.getPreferences().defaultZoomFactor, requestGuestAttachment: () => { - if (!window.isDestroyed()) - window.webContents.send(DESKTOP_IPC_CHANNELS.browserGuestsChanged) + this.#send(DESKTOP_IPC_CHANNELS.browserGuestsChanged) }, revokeSession: id => this.adapter.revokeSession(id), }) this.#host.onDidChange((change) => { - if ((change.kind === 'session' || (change.kind === 'guest' && change.status === 'detached')) && !window.isDestroyed()) - window.webContents.send(DESKTOP_IPC_CHANNELS.browserGuestsChanged) + if (change.kind === 'session' || (change.kind === 'guest' && change.status === 'detached')) + this.#send(DESKTOP_IPC_CHANNELS.browserGuestsChanged) }) this.#host.onDidChange((change) => { - if (change.kind === 'state' && !window.isDestroyed()) - window.webContents.send(DESKTOP_IPC_CHANNELS.browserStateChanged, change.state) + if (change.kind === 'state') { + if (!change.state.visible) + this.#presentation.hide(change.state.sessionId) + this.#send(DESKTOP_IPC_CHANNELS.browserStateChanged, change.state) + } }) this.#host.onDidChange((change) => { if (change.kind === 'state') @@ -93,6 +115,17 @@ export class BrowserIntegration { } closeWindow(): void { + this.#presentation.detachWindow() + this.#window = null + } + + #send(channel: string, value?: unknown): void { + const window = this.#window + if (window && !window.isDestroyed()) + window.webContents.send(channel, value) + } + + #stopHost(): void { const host = this.#host host?.dispose() if (host) { @@ -103,6 +136,7 @@ export class BrowserIntegration { } async startAdapter(): Promise { + this.#startHost() await this.adapter.start() this.#adapterStarted = true if (this.#options.testBrokerSocketPath) { @@ -114,6 +148,7 @@ export class BrowserIntegration { } async stopAdapter(): Promise { + this.#stopHost() await Promise.all([...this.#closingHosts]) this.#testLeasePublisher?.dispose() this.#testLeasePublisher = null @@ -121,5 +156,6 @@ export class BrowserIntegration { this.#adapterStarted = false await this.adapter.dispose() } + this.#presentation.dispose() } } diff --git a/apps/buddy/electron/main/browser/BrowserNavigation.ts b/apps/buddy/electron/main/browser/BrowserNavigation.ts new file mode 100644 index 000000000..f474b4a77 --- /dev/null +++ b/apps/buddy/electron/main/browser/BrowserNavigation.ts @@ -0,0 +1,477 @@ +import type { DesktopBrowserError, DesktopBrowserState } from '../../../shared/browser/browserDesktopApi' +import type { Event } from '../../../shared/events/Emitter' +import type { BrowserHostChange } from './BrowserHostEvents' +import type { BrowserOperationGuard } from './BrowserOperationGuard' +import type { BrowserPage, BrowserPageSession } from './BrowserPageSession' +import type { BrowserSessionRegistry } from './BrowserSessionRegistry' +import { BrowserHostError } from './BrowserHostError' +import { snapshot } from './BrowserPageSession' +import { BrowserSecurityPolicyError } from './BrowserSecurityPolicy' + +const BROWSER_NAVIGATION_WAIT_TIMEOUT_MS = 30_000 +const BROWSER_FAILED_NAVIGATION_SETTLE_MS = 200 +const BROWSER_NAVIGATION_SETTLE_TIMEOUT_MS = 15_000 + +export interface BrowserLocalFileGrant { + entryPath: string + rootPath: string +} + +interface BrowserNavigationOptions { + sessions: Pick, 'get' | 'touch'> + operations: Pick + requireSession: (id: string) => BrowserPageSession + waitForPage: (session: BrowserPageSession) => Promise + assertCurrentPage: (session: BrowserPageSession, pageId: string, operation: string) => void + notFound: (id: string) => BrowserHostError + onDidChange: Event + recordTiming: (session: BrowserPageSession, phase: 'guest_ready' | 'authorized' | 'document_ready' | 'load_complete', startedAt: number) => void +} + +export class BrowserNavigation { + readonly #options: BrowserNavigationOptions + readonly #failedNavigationTargets = new WeakMap() + + constructor(options: BrowserNavigationOptions) { + this.#options = options + } + + release(session: BrowserPageSession): void { + this.#failedNavigationTargets.delete(session) + } + + async navigate(sessionId: string, rawUrl: string, readiness: 'load' | 'document-ready' = 'load', assertControl: () => void = () => {}): Promise { + assertControl() + this.#options.operations.assertCanMutate() + const session = this.#options.requireSession(sessionId) + const startedAt = performance.now() + const navigationSequence = this.#beginPageAction(session) + let url: string + try { + await this.#options.waitForPage(session) + this.#options.requireSession(sessionId) + if (!this.#shouldContinuePageAction(session, navigationSequence)) + return snapshot(session.state) + this.#options.recordTiming(session, 'guest_ready', startedAt) + this.#assertNavigationControl(session, navigationSequence, assertControl) + this.#options.operations.assertCanMutate() + const authorizationStartedAt = performance.now() + url = await session.securityPolicy?.authorizeNavigation(rawUrl) ?? '' + this.#options.recordTiming(session, 'authorized', authorizationStartedAt) + } + catch (error) { + this.#options.requireSession(sessionId) + if (error instanceof BrowserHostError && error.code === 'BROWSER_CONTROL_REQUIRED') + throw error + if (session.state.error?.code === 'BROWSER_PAGE_UNRESPONSIVE') + throw new BrowserHostError('BROWSER_PAGE_UNRESPONSIVE', session.state.error.message) + if (!this.#shouldContinuePageAction(session, navigationSequence)) + return snapshot(session.state) + this.#failedNavigationTargets.set(session, rawUrl) + const failure: DesktopBrowserError = error instanceof BrowserSecurityPolicyError + ? { code: error.code, message: error.message, reason: error.reason } + : { code: 'BROWSER_PAGE_FAILED', message: browserLoadErrorMessage(error) } + session.state.error = failure + session.state.status = 'error' + this.#finishPageAction(session, navigationSequence) + session.publish() + throw error instanceof BrowserHostError + ? error + : new BrowserHostError(failure.code, failure.message, failure.reason) + } + if (!this.#shouldContinuePageAction(session, navigationSequence)) + return snapshot(session.state) + + this.#assertNavigationControl(session, navigationSequence, assertControl) + return this.#loadPage(sessionId, session, navigationSequence, url, readiness, assertControl) + } + + async #loadPage( + sessionId: string, + session: BrowserPageSession, + navigationSequence: number, + url: string, + readiness: 'load' | 'document-ready', + assertControl: () => void, + ): Promise { + return session.runWhileActive(async () => { + const startedAt = performance.now() + const page = session.requirePage() + const pageId = session.state.pageId + let hasCommittedDocument = false + let resolveWait!: (state: DesktopBrowserState) => void + let rejectWait!: (error: unknown) => void + const waiting = new Promise((resolve, reject) => { + resolveWait = resolve + rejectWait = reject + }) + const checkInterrupted = (): boolean => { + try { + this.#options.requireSession(sessionId) + this.#options.assertCurrentPage(session, pageId, 'while waiting for navigation') + assertControl() + if (!this.#shouldContinuePageAction(session, navigationSequence)) { + resolveWait(snapshot(session.state)) + return true + } + const error = session.state.error + if (error?.code === 'BROWSER_PAGE_CRASHED' || error?.code === 'BROWSER_PAGE_UNRESPONSIVE') + throw new BrowserHostError(error.code, error.message, error.reason) + return false + } + catch (error) { + rejectWait(error) + return true + } + } + const onDocumentCommitted = () => { + if (this.#shouldContinuePageAction(session, navigationSequence)) + hasCommittedDocument = true + } + const onDocumentReady = () => { + if (checkInterrupted() || readiness !== 'document-ready' || session.mainFrameCommitSequence !== navigationSequence) + return + this.#options.recordTiming(session, 'document_ready', startedAt) + resolveWait(snapshot(session.state)) + } + const subscription = this.#options.onDidChange((change) => { + if ((change.kind === 'state' || change.kind === 'session') && change.state.sessionId === sessionId) + checkInterrupted() + }) + page.on('did-navigate', onDocumentCommitted) + page.on('dom-ready', onDocumentReady) + const timer = setTimeout(() => { + if (checkInterrupted()) + return + const failure = session.state.error + if (failure) { + rejectWait(new BrowserHostError(failure.code, failure.message, failure.reason)) + return + } + if (hasCommittedDocument && session.mainFrameCommitSequence === navigationSequence) { + resolveWait(snapshot(session.state)) + return + } + const message = 'Browser navigation did not commit before its deadline' + rejectWait(new BrowserHostError('BROWSER_PAGE_FAILED', message)) + session.navigationSequence += 1 + this.#finishPageAction(session, navigationSequence) + this.#failedNavigationTargets.set(session, url) + session.state.error = { code: 'BROWSER_PAGE_FAILED', message } + session.state.status = 'error' + try { + page.stop() + } + catch {} + session.publish() + }, BROWSER_NAVIGATION_WAIT_TIMEOUT_MS) + try { + return await Promise.race([ + waiting, + this.#loadPageToCompletion(sessionId, session, navigationSequence, url, assertControl), + ]) + } + finally { + clearTimeout(timer) + subscription.dispose() + page.off('did-navigate', onDocumentCommitted) + page.off('dom-ready', onDocumentReady) + } + }, { timeoutMs: null }) + } + + async #loadPageToCompletion( + sessionId: string, + session: BrowserPageSession, + navigationSequence: number, + url: string, + assertControl: () => void, + ): Promise { + const startedAt = performance.now() + session.state.url = url + session.publish() + try { + const page = await this.#options.waitForPage(session) + if (!this.#shouldContinuePageAction(session, navigationSequence)) + return snapshot(session.state) + this.#assertNavigationControl(session, navigationSequence, assertControl) + await page.loadURL(url) + this.#options.recordTiming(session, 'load_complete', startedAt) + if (!this.#options.sessions.get(sessionId)) + throw this.#options.notFound(sessionId) + if (!this.#isCurrentPageAction(session, navigationSequence)) + return snapshot(session.state) + session.stoppedNavigationSequence = null + this.#finishPageAction(session, navigationSequence) + session.removeBootstrapHistory() + session.refreshPageState() + session.state.status = 'ready' + session.publish() + return snapshot(session.state) + } + catch (error) { + if (error instanceof BrowserHostError) + throw error + if (!this.#options.sessions.get(sessionId)) + throw this.#options.notFound(sessionId) + if (!this.#isCurrentPageAction(session, navigationSequence)) + return snapshot(session.state) + if (session.stoppedNavigationSequence === navigationSequence) { + session.stoppedNavigationSequence = null + this.#finishPageAction(session, navigationSequence) + session.state.error = null + session.state.status = session.state.url === 'about:blank' ? 'idle' : 'ready' + session.refreshPageState() + session.publish() + return snapshot(session.state) + } + return this.#waitForNavigationSettlement( + sessionId, + session, + navigationSequence, + error, + ) + } + } + + async #waitForNavigationSettlement( + sessionId: string, + session: BrowserPageSession, + navigationSequence: number, + loadError: unknown, + ): Promise { + const startedAt = Date.now() + const deadline = startedAt + BROWSER_NAVIGATION_SETTLE_TIMEOUT_MS + const isAbortedLoad = isNavigationAborted(loadError) + const loadFailureDeadline = isAbortedLoad + ? null + : Math.min(deadline, startedAt + BROWSER_FAILED_NAVIGATION_SETTLE_MS) + let pageFailureDeadline: number | null = null + while (true) { + if (this.#options.sessions.get(sessionId) !== session) + throw this.#options.notFound(sessionId) + if (!this.#isCurrentPageAction(session, navigationSequence)) + return snapshot(session.state) + if (session.stoppedNavigationSequence === navigationSequence) { + session.stoppedNavigationSequence = null + this.#finishPageAction(session, navigationSequence) + session.state.error = null + session.state.status = session.state.url === 'about:blank' ? 'idle' : 'ready' + session.refreshPageState() + session.publish() + return snapshot(session.state) + } + const hasReplacementCommit = session.mainFrameCommitSequence === navigationSequence + const pageFailure = session.state.error + const now = Date.now() + if (pageFailure) { + pageFailureDeadline ??= Math.min( + deadline, + now + BROWSER_FAILED_NAVIGATION_SETTLE_MS, + ) + } + else { + pageFailureDeadline = null + } + if ( + pageFailure + && ( + session.activeNavigationSequence !== navigationSequence + || now >= (pageFailureDeadline ?? deadline) + ) + ) { + this.#finishPageAction(session, navigationSequence) + throw new BrowserHostError( + pageFailure.code, + pageFailure.message, + pageFailure.reason ?? null, + ) + } + if ( + session.state.status === 'ready' + && (isAbortedLoad || hasReplacementCommit) + ) { + return snapshot(session.state) + } + if (loadFailureDeadline !== null && now >= loadFailureDeadline) { + const message = browserLoadErrorMessage(loadError) + this.#finishPageAction(session, navigationSequence) + session.state.error = { code: 'BROWSER_PAGE_FAILED', message } + session.state.status = 'error' + session.publish() + throw new BrowserHostError('BROWSER_PAGE_FAILED', message) + } + const remainingMs = Math.min( + pageFailureDeadline ?? deadline, + loadFailureDeadline ?? deadline, + ) - now + if (remainingMs <= 0) { + const message = 'Browser navigation did not settle after the initial load was replaced' + this.#finishPageAction(session, navigationSequence) + session.state.error = { code: 'BROWSER_PAGE_FAILED', message } + session.state.status = 'error' + session.publish() + throw new BrowserHostError('BROWSER_PAGE_FAILED', message) + } + await wait(Math.min(remainingMs, 50)) + } + } + + goBack(sessionId: string): void { + this.#options.operations.assertCanMutate() + const session = this.#options.requireSession(sessionId) + const history = session.requirePage().navigationHistory + if (!history.canGoBack()) + return + this.#beginPageAction(session) + history.goBack() + } + + goForward(sessionId: string): void { + this.#options.operations.assertCanMutate() + const session = this.#options.requireSession(sessionId) + const history = session.requirePage().navigationHistory + if (!history.canGoForward()) + return + this.#beginPageAction(session) + history.goForward() + } + + async reload(sessionId: string, assertControl: () => void = () => {}): Promise { + assertControl() + this.#options.operations.assertCanMutate() + const session = this.#options.requireSession(sessionId) + const failedTarget = this.#failedNavigationTargets.get(session) + if (session.state.status === 'error' && failedTarget !== undefined) { + await this.navigate(sessionId, failedTarget, 'load', assertControl) + return + } + if (session.state.url === 'about:blank') + return + this.#beginPageAction(session) + session.requirePage().reload() + } + + stop(sessionId: string): void { + const session = this.#options.requireSession(sessionId) + if (session.state.status !== 'loading') + return + session.stoppedNavigationSequence = session.navigationSequence + this.#finishPageAction(session, session.navigationSequence) + if (session.page && !session.page.isDestroyed()) + session.page.stop() + session.refreshPageState() + session.state.error = null + session.state.status = session.state.url === 'about:blank' ? 'idle' : 'ready' + session.publish() + } + + async openLocalFile( + sessionId: string, + grant: BrowserLocalFileGrant, + readiness: 'load' | 'document-ready' = 'load', + assertControl: () => void = () => {}, + ): Promise { + assertControl() + this.#options.operations.assertCanMutate() + const session = this.#options.requireSession(sessionId) + const startedAt = performance.now() + await this.#options.waitForPage(session) + this.#options.recordTiming(session, 'guest_ready', startedAt) + this.#options.operations.assertCanMutate() + const navigationSequence = this.#beginPageAction(session) + const authorizationStartedAt = performance.now() + let url: string + try { + url = await session.securityPolicy?.authorizeLocalFile( + grant.entryPath, + grant.rootPath, + ) ?? '' + this.#options.recordTiming(session, 'authorized', authorizationStartedAt) + } + catch (error) { + if (session.state.error?.code === 'BROWSER_PAGE_UNRESPONSIVE') + throw new BrowserHostError('BROWSER_PAGE_UNRESPONSIVE', session.state.error.message) + if (!(error instanceof BrowserSecurityPolicyError)) + throw error + if (!this.#shouldContinuePageAction(session, navigationSequence)) + return snapshot(session.state) + session.state.error = { + code: error.code, + message: error.message, + reason: error.reason, + } + session.state.status = 'error' + this.#finishPageAction(session, navigationSequence) + session.publish() + throw new BrowserHostError(error.code, error.message, error.reason) + } + if (!this.#shouldContinuePageAction(session, navigationSequence)) + return snapshot(session.state) + this.#assertNavigationControl(session, navigationSequence, assertControl) + return this.#loadPage(sessionId, session, navigationSequence, url, readiness, assertControl) + } + + #beginPageAction(session: BrowserPageSession): number { + this.#failedNavigationTargets.delete(session) + this.#options.sessions.touch(session.state.sessionId) + session.navigationSequence += 1 + session.activeNavigationSequence = session.navigationSequence + session.semanticDriver?.invalidateDocument() + session.stoppedNavigationSequence = null + session.state.error = null + session.state.status = 'loading' + session.publish() + return session.navigationSequence + } + + #isCurrentPageAction(session: BrowserPageSession, navigationSequence: number): boolean { + return session.navigationSequence === navigationSequence + } + + #finishPageAction(session: BrowserPageSession, navigationSequence: number): void { + if (session.activeNavigationSequence === navigationSequence) + session.activeNavigationSequence = null + } + + #assertNavigationControl(session: BrowserPageSession, navigationSequence: number, assertControl: () => void): void { + try { + assertControl() + } + catch (error) { + if (this.#shouldContinuePageAction(session, navigationSequence)) { + this.#finishPageAction(session, navigationSequence) + session.refreshPageState() + session.state.error = null + session.state.status = session.page?.isLoading() ? 'loading' : session.state.url === 'about:blank' ? 'idle' : 'ready' + session.publish() + } + throw error + } + } + + #shouldContinuePageAction(session: BrowserPageSession, navigationSequence: number): boolean { + return this.#isCurrentPageAction(session, navigationSequence) + && session.stoppedNavigationSequence !== navigationSequence + } +} + +function isNavigationAborted(error: unknown): boolean { + if (!error || typeof error !== 'object') + return false + const candidate = error as { code?: unknown, errno?: unknown } + if (candidate.code === 'ERR_ABORTED' || candidate.errno === -3) + return true + return error instanceof Error + && /^ERR_ABORTED(?: \(-3\))?(?: |$)/.test(error.message) +} + +function browserLoadErrorMessage(error: unknown): string { + if (error instanceof Error && error.message.trim()) + return error.message.slice(0, 1_024) + return 'Browser page failed to load' +} + +function wait(durationMs: number): Promise { + return new Promise(resolve => setTimeout(resolve, durationMs)) +} diff --git a/apps/buddy/electron/main/browser/BrowserObservationStore.ts b/apps/buddy/electron/main/browser/BrowserObservationStore.ts new file mode 100644 index 000000000..5eae77e32 --- /dev/null +++ b/apps/buddy/electron/main/browser/BrowserObservationStore.ts @@ -0,0 +1,94 @@ +import type { BrowserScreenshotRef } from '../../../shared/browser' +import type { SemanticBrowserTarget } from './browserObservationProjection' +import { BrowserHostError } from './BrowserHostError' + +interface BrowserObservationEntry { + documentRevision: number + screenshot?: { bytes: Uint8Array, ref: BrowserScreenshotRef } + targets: ReadonlyMap +} + +interface StoredObservation extends BrowserObservationEntry { + expiresAt: number + requiresHumanInput: boolean +} + +interface Options { + now: () => number + ttlMs: number + maxObservations?: number +} + +const MAX_SCREENSHOT_CACHE_BYTES = 32 * 1_024 * 1_024 + +export class BrowserObservationStore { + readonly #observations = new Map() + readonly #options: Options + #expiryTimer: ReturnType | null = null + #disposed = false + + constructor(options: Options) { + this.#options = options + } + + get(id: string): StoredObservation | undefined { + this.#prune() + return this.#observations.get(id) + } + + store(id: string, observation: BrowserObservationEntry): void { + if (this.#disposed) + throw new BrowserHostError('BROWSER_PAGE_FAILED', 'Browser observation store is disposed') + this.#prune() + this.#observations.delete(id) + this.#observations.set(id, { + ...observation, + expiresAt: this.#options.now() + this.#options.ttlMs, + requiresHumanInput: [...observation.targets.values()].some(target => target.inputMode === 'human'), + }) + let bytes = [...this.#observations.values()].reduce((total, entry) => total + (entry.screenshot?.bytes.byteLength ?? 0), 0) + while (this.#observations.size > (this.#options.maxObservations ?? 8) || bytes > MAX_SCREENSHOT_CACHE_BYTES) { + const oldest = this.#observations.keys().next().value + if (oldest === undefined) + break + bytes -= this.#observations.get(oldest)?.screenshot?.bytes.byteLength ?? 0 + this.#observations.delete(oldest) + } + this.#scheduleExpiry() + } + + clear(): void { + if (this.#expiryTimer) + clearTimeout(this.#expiryTimer) + this.#expiryTimer = null + this.#observations.clear() + } + + dispose(): void { + this.#disposed = true + this.clear() + } + + #prune(): void { + const now = this.#options.now() + for (const [id, observation] of this.#observations) { + if (observation.expiresAt <= now) + this.#observations.delete(id) + } + } + + #scheduleExpiry(): void { + if (this.#expiryTimer) + clearTimeout(this.#expiryTimer) + this.#expiryTimer = null + if (!this.#observations.size) + return + const expiresAt = Math.min(...[...this.#observations.values()].map(observation => observation.expiresAt)) + this.#expiryTimer = setTimeout(() => { + this.#expiryTimer = null + this.#prune() + this.#scheduleExpiry() + }, Math.max(1, expiresAt - this.#options.now())) + this.#expiryTimer.unref() + } +} diff --git a/apps/buddy/electron/main/browser/BrowserPresentationHost.ts b/apps/buddy/electron/main/browser/BrowserPresentationHost.ts new file mode 100644 index 000000000..5e24f1e9e --- /dev/null +++ b/apps/buddy/electron/main/browser/BrowserPresentationHost.ts @@ -0,0 +1,167 @@ +import type { BrowserWindow as ElectronWindow, WebContents } from 'electron' +import type { DesktopBrowserGuestDescriptor, DesktopBrowserPresentationInput } from '../../../shared/browser/browserDesktopApi' +import { randomUUID } from 'node:crypto' +import { BrowserWindow, View, WebContentsView } from 'electron' + +interface OwnedPage { + view: WebContentsView + clip: View + displayed: boolean +} + +export class BrowserPresentationHost { + readonly #pages = new Map() + #parking: ElectronWindow | null = null + #window: ElectronWindow | null = null + #token: string | null = null + #unbind: (() => void) | null = null + readonly #setSurface: (sessionId: string, visible: boolean) => void + + constructor(setSurface: (sessionId: string, visible: boolean) => void) { + this.#setSurface = setSurface + } + + createPage(descriptor: DesktopBrowserGuestDescriptor): WebContents { + if (this.#pages.has(descriptor.sessionId)) + throw new Error('Browser page already exists') + this.#parking ??= new BrowserWindow({ show: false, width: 1280, height: 800, webPreferences: { sandbox: true, contextIsolation: true, nodeIntegration: false, backgroundThrottling: false } }) + const view = new WebContentsView({ webPreferences: { partition: descriptor.partition, sandbox: true, contextIsolation: true, nodeIntegration: false, nodeIntegrationInSubFrames: false, nodeIntegrationInWorker: false, webSecurity: true, allowRunningInsecureContent: false, webviewTag: false, devTools: false, backgroundThrottling: false } }) + const clip = new View() + clip.addChildView(view) + clip.setBounds({ x: 0, y: 0, width: 1280, height: 800 }) + view.setBounds({ x: 0, y: 0, width: 1280, height: 800 }) + this.#parking.contentView.addChildView(clip) + const page = { view, clip, displayed: false } + this.#pages.set(descriptor.sessionId, page) + view.webContents.once('destroyed', () => { + if (this.#pages.get(descriptor.sessionId) !== page) + return + this.#remove(page) + this.#pages.delete(descriptor.sessionId) + }) + return view.webContents + } + + bindWindow(window: ElectronWindow): void { + this.detachWindow() + this.#window = window + const contents = window.webContents + const invalidate = () => this.#revoke() + const closed = () => { + if (this.#window !== window) + return + this.#token = null + this.#unbind?.() + this.#unbind = null + this.#window = null + for (const page of this.#pages.values()) + page.displayed = false + } + const navigation = (_event: unknown, _url: string, inPlace: boolean, mainFrame: boolean) => { + if (mainFrame && !inPlace) + invalidate() + } + const closing = (event: { defaultPrevented: boolean }) => { + if (!event.defaultPrevented) + this.detachWindow() + } + window.on('close', closing) + window.once('closed', closed) + contents.on('did-start-navigation', navigation) + contents.on('render-process-gone', invalidate) + this.#unbind = () => { + window.off('close', closing) + window.off('closed', closed) + contents.off('did-start-navigation', navigation) + contents.off('render-process-gone', invalidate) + } + } + + acquire(window: ElectronWindow): string { + if (this.#window !== window || window.isDestroyed()) + throw new Error('DESKTOP_REQUIRED') + this.#revoke() + this.#token = randomUUID() + return this.#token + } + + present(window: ElectronWindow, input: DesktopBrowserPresentationInput): Promise { + if (this.#window !== window || window.isDestroyed() || this.#token !== input.presentationId) + throw new Error('BROWSER_PRESENTATION_EXPIRED') + const page = this.#pages.get(input.sessionId) + if (!page || page.view.webContents.isDestroyed()) + throw new Error('BROWSER_PAGE_FAILED') + if (!input.visible) { + const capture = page.displayed ? page.view.webContents.capturePage() : null + this.hide(input.sessionId) + return (capture ?? Promise.resolve(null)).catch(() => null).then(snapshot => this.#token === input.presentationId && this.#pages.get(input.sessionId) === page ? snapshot?.toDataURL() ?? null : null) + } + const zoom = window.webContents.getZoomFactor() + const rounded = (value: number) => Math.round(value * zoom) + const { bounds, clip } = input + const content = window.getContentBounds() + const x = Math.max(0, rounded(clip.x)) + const y = Math.max(0, rounded(clip.y)) + const width = Math.min(rounded(clip.width), content.width - x) + const height = Math.min(rounded(clip.height), content.height - y) + if (width <= 0 || height <= 0) { + this.hide(input.sessionId) + return Promise.resolve(null) + } + if (!page.displayed) + this.#remove(page) + page.clip.setBounds({ x, y, width, height }) + page.view.setBounds({ x: rounded(bounds.x) - x, y: rounded(bounds.y) - y, width: Math.max(1, rounded(bounds.width)), height: Math.max(1, rounded(bounds.height)) }) + if (!page.displayed) + window.contentView.addChildView(page.clip) + page.displayed = true + this.#setSurface(input.sessionId, true) + return Promise.resolve(null) + } + + hide(sessionId: string): void { + const page = this.#pages.get(sessionId) + if (!page) + return + this.#remove(page) + if (this.#parking && !this.#parking.isDestroyed()) + this.#parking.contentView.addChildView(page.clip) + page.displayed = false + this.#setSurface(sessionId, false) + } + + detachWindow(): void { + this.#revoke() + this.#unbind?.() + this.#unbind = null + this.#window = null + } + + dispose(): void { + this.detachWindow() + for (const page of this.#pages.values()) { + this.#remove(page) + if (!page.view.webContents.isDestroyed()) + page.view.webContents.close({ waitForBeforeUnload: false }) + } + this.#pages.clear() + this.#parking?.destroy() + this.#parking = null + } + + #remove(page: OwnedPage): void { + if (this.#window && !this.#window.isDestroyed()) { + if (page.displayed && page.view.webContents.isFocused()) + this.#window.webContents.focus() + this.#window.contentView.removeChildView(page.clip) + } + if (this.#parking && !this.#parking.isDestroyed()) + this.#parking.contentView.removeChildView(page.clip) + } + + #revoke(): void { + this.#token = null + for (const sessionId of this.#pages.keys()) + this.hide(sessionId) + } +} diff --git a/apps/buddy/electron/main/browser/SemanticBrowserDriver.ts b/apps/buddy/electron/main/browser/SemanticBrowserDriver.ts index 162efa06b..80389beca 100644 --- a/apps/buddy/electron/main/browser/SemanticBrowserDriver.ts +++ b/apps/buddy/electron/main/browser/SemanticBrowserDriver.ts @@ -25,6 +25,7 @@ import { BrowserDebugger } from './BrowserDebugger' import { collectBrowserRegionNodes, resolveBrowserRegion } from './browserDomRegion' import { BrowserHostError } from './BrowserHostError' import { collectDomFieldMetadata, collectFrameIds, collectViewportNodeIds, containsSensitiveFieldMetadata, createScreenshotRef, createTruncation, deduplicateAccessibilityNodes, hasPngSignature, MAX_OBSERVED_FRAMES, normalizeElementLimit, projectElements, scopeAccessibilityNodes } from './browserObservationProjection' +import { BrowserObservationStore } from './BrowserObservationStore' import { projectBrowserObservedValue, redactBrowserRuntimeUrl, @@ -96,17 +97,6 @@ export interface SemanticBrowserScreenshot { export type { SemanticBrowserTarget } from './browserObservationProjection' -interface StoredObservation { - documentRevision: number - expiresAt: number - requiresHumanInput: boolean - screenshot?: { - bytes: Uint8Array - ref: BrowserScreenshotRef - } - targets: Map -} - type SemanticBrowserDriverErrorCode = Extract< BrowserErrorCode, | 'BROWSER_HUMAN_INPUT_REQUIRED' @@ -114,9 +104,7 @@ type SemanticBrowserDriverErrorCode = Extract< | 'BROWSER_TARGET_STALE' > -const DEFAULT_MAX_OBSERVATIONS = 8 const DEFAULT_OBSERVATION_TTL_MS = 30_000 -const MAX_SCREENSHOT_CACHE_BYTES = 32 * 1_024 * 1_024 const BROWSER_ACTION_OBJECT_GROUP = 'lexora-browser-action' const BROWSER_TARGET_ACTIONABILITY_FUNCTION = `async function (checkClickability) { const element = this @@ -264,10 +252,8 @@ export class SemanticBrowserDriverError extends Error { export class SemanticBrowserDriver { readonly #createId: () => string readonly #createScreenshotId: () => string - readonly #maxObservations: number readonly #now: () => number - readonly #observations = new Map() - #observationExpiryTimer: ReturnType | null = null + readonly #observations: BrowserObservationStore readonly #observationTtlMs: number readonly #contentReader: BrowserContentReader readonly #page: SemanticBrowserPage @@ -279,10 +265,10 @@ export class SemanticBrowserDriver { constructor(options: SemanticBrowserDriverOptions) { this.#createId = options.createId ?? randomUUID this.#createScreenshotId = options.createScreenshotId ?? randomUUID - this.#maxObservations = options.maxObservations ?? DEFAULT_MAX_OBSERVATIONS this.#now = options.now ?? Date.now this.#observationTtlMs = options.observationTtlMs ?? DEFAULT_OBSERVATION_TTL_MS this.#contentReader = new BrowserContentReader(this.#now, this.#observationTtlMs) + this.#observations = new BrowserObservationStore({ now: this.#now, ttlMs: this.#observationTtlMs, maxObservations: options.maxObservations }) this.#page = options.page this.#connection = options.connection ?? new BrowserDebugger(options.page.debugger) this.#ownsConnection = !options.connection @@ -477,16 +463,14 @@ export class SemanticBrowserDriver { projected.targets.delete(removed.ref) } - this.#storeObservation( - observation.observationId, - input.mode === 'screenshot' ? new Map() : projected.targets, - screenshot && observation.screenshot - ? { - bytes: screenshot.bytes, - ref: observation.screenshot, - } + this.#assertActive() + this.#observations.store(observation.observationId, { + documentRevision: revision, + targets: input.mode === 'screenshot' ? new Map() : projected.targets, + screenshot: screenshot && observation.screenshot + ? { bytes: screenshot.bytes, ref: observation.screenshot } : undefined, - ) + }) return observation } catch (error) { @@ -614,7 +598,6 @@ export class SemanticBrowserDriver { observationId: string }): void { this.#assertActive() - this.#pruneObservations(this.#now()) const observation = this.#observations.get(reference.observationId) if ( reference.documentRevision !== this.#documentRevision @@ -640,7 +623,6 @@ export class SemanticBrowserDriver { reference: SemanticBrowserScreenshotReference, ): SemanticBrowserScreenshot { this.#assertActive() - this.#pruneObservations(this.#now()) const observation = this.#observations.get(reference.observationId) const screenshot = observation?.screenshot if ( @@ -662,7 +644,7 @@ export class SemanticBrowserDriver { throw new SemanticBrowserDriverError() this.#documentRevision += 1 this.#contentReader.clear() - this.#clearObservations() + this.#observations.clear() } async isTextVisible(text: string): Promise { @@ -753,7 +735,7 @@ export class SemanticBrowserDriver { return this.#disposed = true this.#contentReader.clear() - this.#clearObservations() + this.#observations.dispose() if (this.#ownsConnection) this.#connection.dispose() } @@ -985,68 +967,6 @@ export class SemanticBrowserDriver { width, } } - - #pruneObservations(now: number): void { - for (const [observationId, observation] of this.#observations) { - if ( - observation.expiresAt <= now - || observation.documentRevision !== this.#documentRevision - ) { - this.#observations.delete(observationId) - } - } - } - - #clearObservations(): void { - if (this.#observationExpiryTimer) - clearTimeout(this.#observationExpiryTimer) - this.#observationExpiryTimer = null - this.#observations.clear() - } - - #scheduleObservationExpiry(): void { - if (this.#observationExpiryTimer) - clearTimeout(this.#observationExpiryTimer) - this.#observationExpiryTimer = null - if (!this.#observations.size) - return - const expiresAt = Math.min(...[...this.#observations.values()].map(observation => observation.expiresAt)) - this.#observationExpiryTimer = setTimeout(() => { - this.#observationExpiryTimer = null - this.#pruneObservations(this.#now()) - this.#scheduleObservationExpiry() - }, Math.max(1, expiresAt - this.#now())) - this.#observationExpiryTimer.unref() - } - - #storeObservation( - observationId: string, - targets: Map, - screenshot?: StoredObservation['screenshot'], - ): void { - this.#assertActive() - const now = this.#now() - this.#pruneObservations(now) - this.#observations.delete(observationId) - this.#observations.set(observationId, { - documentRevision: this.#documentRevision, - expiresAt: now + this.#observationTtlMs, - requiresHumanInput: [...targets.values()].some( - target => target.inputMode === 'human', - ), - ...(screenshot ? { screenshot } : {}), - targets, - }) - let screenshotBytes = [...this.#observations.values()].reduce((total, observation) => total + (observation.screenshot?.bytes.byteLength ?? 0), 0) - while (this.#observations.size > this.#maxObservations || screenshotBytes > MAX_SCREENSHOT_CACHE_BYTES) { - const oldestObservationId = this.#observations.keys().next().value - if (oldestObservationId === undefined) - break - screenshotBytes -= this.#observations.get(oldestObservationId)?.screenshot?.bytes.byteLength ?? 0 - this.#observations.delete(oldestObservationId) - } - this.#scheduleObservationExpiry() - } } function supportsTargetAction( diff --git a/apps/buddy/electron/main/browser/__tests__/BrowserPresentationHost.spec.ts b/apps/buddy/electron/main/browser/__tests__/BrowserPresentationHost.spec.ts new file mode 100644 index 000000000..c5b3309b0 --- /dev/null +++ b/apps/buddy/electron/main/browser/__tests__/BrowserPresentationHost.spec.ts @@ -0,0 +1,91 @@ +import { BrowserWindow } from 'electron' +import { describe, expect, it, vi } from 'vitest' +import { BrowserPresentationHost } from '../BrowserPresentationHost' + +vi.mock('electron', async () => { + const { EventEmitter } = await import('node:events') + class View { + children: View[] = [] + bounds = { x: 0, y: 0, width: 0, height: 0 } + addChildView(view: View) { + this.removeChildView(view) + this.children.push(view) + } + + removeChildView(view: View) { this.children = this.children.filter(child => child !== view) } + setBounds(bounds: typeof this.bounds) { this.bounds = bounds } + getBounds() { return this.bounds } + } + class Contents extends EventEmitter { + destroyed = false + isDestroyed() { return this.destroyed } + isFocused() { return false } + focus() {} + getZoomFactor() { return 1.25 } + capturePage() { return Promise.resolve({ toDataURL: () => 'data:image/png;base64,c2NyZWVuc2hvdA==' }) } + close() { + this.destroyed = true + this.emit('destroyed') + } + } + class Window extends EventEmitter { + readonly contentView = new View() + readonly webContents = new Contents() + destroyed = false + isDestroyed() { return this.destroyed } + getContentBounds() { return { x: 0, y: 0, width: 1000, height: 800 } } + destroy() { + this.destroyed = true + this.emit('closed') + } + } + return { BrowserWindow: Window, View, WebContentsView: class extends View { readonly webContents = new Contents() } } +}) + +function input(presentationId: string) { + return { presentationId, sessionId: 'session', pageId: 'page', visible: true, bounds: { x: 10, y: 20, width: 400, height: 300 }, clip: { x: 20, y: 30, width: 300, height: 200 } } +} + +describe('browser presentation ownership', () => { + it('preserves the page through window close and rejects the old display token in a new window', async () => { + const host = new BrowserPresentationHost(() => {}) + const page = host.createPage({ sessionId: 'session', partition: 'persist:isolated-fixture' }) + const first = new BrowserWindow() + host.bindWindow(first) + const token = host.acquire(first) + await host.present(first, input(token)) + expect(first.contentView.children).toHaveLength(1) + first.destroy() + expect(page.isDestroyed()).toBe(false) + const second = new BrowserWindow() + host.bindWindow(second) + expect(() => host.present(second, input(token))).toThrow('BROWSER_PRESENTATION_EXPIRED') + await host.present(second, input(host.acquire(second))) + expect(second.contentView.children).toHaveLength(1) + expect(page.isDestroyed()).toBe(false) + host.dispose() + expect(page.isDestroyed()).toBe(true) + }) + + it('revokes renderer generations without closing pages and preserves clipped viewport geometry', async () => { + const host = new BrowserPresentationHost(() => {}) + const page = host.createPage({ sessionId: 'session', partition: 'persist:isolated-fixture' }) + const window = new BrowserWindow() + host.bindWindow(window) + const token = host.acquire(window) + await host.present(window, input(token)) + const clip = window.contentView.children[0]! + expect(clip.getBounds()).toEqual({ x: 25, y: 38, width: 375, height: 250 }) + expect(clip.children[0]!.getBounds()).toEqual({ x: -12, y: -13, width: 500, height: 375 }) + window.webContents.emit('did-start-navigation', {}, 'lexora-app://desktop/index.html', false, true) + expect(window.contentView.children).toHaveLength(0) + expect(page.isDestroyed()).toBe(false) + expect(() => host.present(window, input(token))).toThrow('BROWSER_PRESENTATION_EXPIRED') + const next = host.acquire(window) + await host.present(window, input(next)) + expect(await host.present(window, { ...input(next), visible: false })).toMatch(/^data:image\/png/) + expect(window.contentView.children).toHaveLength(0) + expect(page.isDestroyed()).toBe(false) + host.dispose() + }) +}) diff --git a/apps/buddy/electron/main/browser/registerBrowserDesktopIpc.ts b/apps/buddy/electron/main/browser/registerBrowserDesktopIpc.ts index f4437a1d0..3c06fcd1e 100644 --- a/apps/buddy/electron/main/browser/registerBrowserDesktopIpc.ts +++ b/apps/buddy/electron/main/browser/registerBrowserDesktopIpc.ts @@ -2,6 +2,7 @@ import type { BrowserWindow, IpcMainInvokeEvent } from 'electron' import type { TaskFileTarget } from '../../../shared/files/taskFileApi' import type { BrowserDataService } from './BrowserDataService' import type { BrowserHost } from './BrowserHost' +import type { BrowserIntegration } from './BrowserIntegration' import type { BrowserScreenshotService } from './BrowserScreenshotService' import { fileURLToPath } from 'node:url' import { ipcMain, session, shell, webContents } from 'electron' @@ -12,6 +13,7 @@ import { browserNavigateInputSchema, browserOpenArtifactInputSchema, browserOpenFileInputSchema, + browserPresentationInputSchema, browserSessionInputSchema, browserSetProfileModeInputSchema, browserSetSurfaceInputSchema, @@ -24,6 +26,7 @@ import { DESKTOP_IPC_CHANNELS } from '../../shared/desktopApi' import { assertTrustedSender } from '../ipc' export interface RegisterBrowserDesktopIpcOptions { + presentation?: Pick data: Pick screenshots: Pick getHost: () => BrowserHost | null @@ -51,6 +54,18 @@ export function registerBrowserDesktopIpc( }) } + handle(DESKTOP_IPC_CHANNELS.browserAcquirePresentation, (_host, _input, event) => { + const window = options.getWindow() + if (!window || !options.presentation || event.senderFrame !== event.sender.mainFrame) + throw new Error('DESKTOP_REQUIRED') + return options.presentation.acquirePresentation(window) + }) + handle(DESKTOP_IPC_CHANNELS.browserPresent, (_host, input, event) => { + const window = options.getWindow() + if (!window || !options.presentation || event.senderFrame !== event.sender.mainFrame) + throw new Error('DESKTOP_REQUIRED') + return options.presentation.present(window, browserPresentationInputSchema.parse(input)) + }) handle(DESKTOP_IPC_CHANNELS.browserPickElement, async (host, input, event) => { if (event.senderFrame !== event.sender.mainFrame) throw new Error('Element picking requires the trusted main frame') @@ -69,6 +84,8 @@ export function registerBrowserDesktopIpc( return host.locateElement(browserLocateElementInputSchema.parse(input)) }) handle(DESKTOP_IPC_CHANNELS.browserAttachGuest, (host, input, event) => { + if (options.presentation) + throw new Error('Browser pages are owned by the platform host') const { sessionId, webContentsId } = browserAttachGuestInputSchema.parse(input) const guest = webContents.fromId(webContentsId) if ( diff --git a/apps/buddy/electron/main/config/LexoraConfigStore.ts b/apps/buddy/electron/main/config/LexoraConfigStore.ts index e1b9f89ee..9124553dd 100644 --- a/apps/buddy/electron/main/config/LexoraConfigStore.ts +++ b/apps/buddy/electron/main/config/LexoraConfigStore.ts @@ -17,6 +17,7 @@ import { copyEventSnapshot } from '../../../shared/events/eventSnapshot' import { extensionAuthorSchema } from '../../../shared/extensions/extensionIdentity' import { BUILTIN_FILE_ICON_THEME, fileIconPreferenceSchema } from '../../../shared/icons/fileIconTheme' import { imagePreferencesSchema } from '../../../shared/images/imagePreferences' +import { DEFAULT_DESKTOP_EXIT_BEHAVIOR, desktopExitBehaviorSchema } from '../../../shared/lifecycle/exitBehavior' import { downloadSettingsSchema } from '../../../shared/network/downloadSettings' import { DEFAULT_PROXY_SETTINGS, proxySettingsSchema } from '../../../shared/network/proxySettings' import { DEFAULT_NOTIFICATION_SOUND_PREFERENCES, notificationSoundAssetIdSchema, notificationSoundAssetSchema, notificationSoundPreferencesPatchSchema, notificationSoundPreferencesSchema } from '../../../shared/notifications/notificationSound' @@ -30,11 +31,12 @@ import { DEFAULT_LOCAL_BACKUP_LIMIT, localBackupLimitSchema } from '../../../sha import { DESKTOP_ACCENT_COLORS, legacyThemePreferenceSchema, migrateThemePreference, themePreferenceSchema } from '../../../shared/theme/themePreferences' import { DEFAULT_TYPOGRAPHY, typographyPreferencesSchema } from '../../../shared/typography/typographyPreferences' import { DEFAULT_DESKTOP_CHAT_PREFERENCES, DEFAULT_DESKTOP_CHAT_WELCOME_ITEMS, DESKTOP_CHAT_OUTLINE_POSITIONS, DESKTOP_CHAT_READING_WIDTHS, DESKTOP_PROFILE_AVATAR_MAX_DATA_URL_LENGTH, DESKTOP_TASK_SIDEBAR_SECTIONS } from '../../shared/desktopApi' - import { desktopChatWelcomeItemsSchema } from '../../shared/desktopApiSchemas' + import { isPresetWelcomeId, normalizeDesktopChatWelcomePreference } from '../../shared/desktopChatWelcome' import { desktopUpdateChannelSchema } from '../../shared/desktopUpdates' import { isWelcomeCompanionImageDataUrl, welcomeCompanionImageSchema, welcomeCompanionImagesSchema } from '../../shared/welcomeCompanionImage' +import { runProfileWrite } from '../storage/profileWrites' function migrateWelcomeItems(value: unknown): unknown { if (!Array.isArray(value)) @@ -120,6 +122,7 @@ const desktopConfigSchema = z.object({ }), developer_tools_enabled: z.boolean().default(false), language: z.enum(['zh-CN', 'en-US']).default('zh-CN'), + exit_behavior: desktopExitBehaviorSchema.default(DEFAULT_DESKTOP_EXIT_BEHAVIOR), launch_at_login: z.boolean().default(false), notifications_enabled: z.boolean().default(true), notification_sound: notificationSoundPreferencesSchema.prefault({}), @@ -165,6 +168,7 @@ const desktopConfigSchema = z.object({ profile: { user_name: '', device_name: '', avatar: '' }, developer_tools_enabled: false, language: 'zh-CN', + exit_behavior: DEFAULT_DESKTOP_EXIT_BEHAVIOR, launch_at_login: false, notifications_enabled: true, notification_sound: DEFAULT_NOTIFICATION_SOUND_PREFERENCES, @@ -244,12 +248,29 @@ export class LexoraConfigStore { } async read(): Promise { - await this.#writeQueue const config = decodeConfig(await this.#readFile()) this.#persisted = structuredClone(config) return config } + initializeExitBehavior(): Promise { + return runProfileWrite(dirname(this.#configPath), () => this.#initializeExitBehavior()) + } + + #initializeExitBehavior(): Promise { + const operation = this.#writeQueue.then(async () => { + const raw = await this.#readFile() as Record + const config = decodeConfig(raw) + const desktop = raw.desktop as Record | undefined + if (desktop?.exit_behavior === undefined) + await this.#write({ ...raw, desktop: { ...desktop, exit_behavior: DEFAULT_DESKTOP_EXIT_BEHAVIOR } }, () => {}, () => {}) + this.#persisted = structuredClone(config) + return config + }) + this.#writeQueue = operation.then(() => undefined, () => undefined) + return operation + } + get snapshot() { return copyEventSnapshot({ revision: this.#revision, persisted: this.#persisted, applied: this.#applied }) } update(patch: LexoraConfigPatch, apply?: (config: LexoraConfig) => Promise | void, assertCurrent: () => void = () => {}): Promise { @@ -300,6 +321,10 @@ export class LexoraConfigStore { } #update(transform: (current: LexoraConfig) => LexoraConfig, apply: ((config: LexoraConfig) => Promise | void) | undefined, assertCurrent: () => void): Promise { + return runProfileWrite(dirname(this.#configPath), () => this.#updateWritable(transform, apply, assertCurrent)) + } + + #updateWritable(transform: (current: LexoraConfig) => LexoraConfig, apply: ((config: LexoraConfig) => Promise | void) | undefined, assertCurrent: () => void): Promise { if (this.#disposing) return Promise.reject(new Error('CONFIG_STORE_STOPPED')) const operation = this.#writeQueue.then(async () => { @@ -472,6 +497,7 @@ function decodeConfig(value: unknown): LexoraConfig { }, developerToolsEnabled: config.desktop.developer_tools_enabled, language: config.desktop.language, + exitBehavior: config.desktop.exit_behavior, launchAtLogin: config.desktop.launch_at_login, notificationsEnabled: config.desktop.notifications_enabled, notificationSound: config.desktop.notification_sound, @@ -555,6 +581,7 @@ function encodeConfig(config: LexoraConfig) { }, developer_tools_enabled: config.desktop.developerToolsEnabled, language: config.desktop.language, + exit_behavior: config.desktop.exitBehavior, launch_at_login: config.desktop.launchAtLogin, notifications_enabled: config.desktop.notificationsEnabled, notification_sound: config.desktop.notificationSound, diff --git a/apps/buddy/electron/main/config/__tests__/initializeDesktopConfig.spec.ts b/apps/buddy/electron/main/config/__tests__/initializeDesktopConfig.spec.ts index e528d02ff..50fad7c4c 100644 --- a/apps/buddy/electron/main/config/__tests__/initializeDesktopConfig.spec.ts +++ b/apps/buddy/electron/main/config/__tests__/initializeDesktopConfig.spec.ts @@ -1,8 +1,9 @@ import { mkdir, readFile, writeFile } from 'node:fs/promises' import { join } from 'node:path' import { createTemporaryDirectory } from '@buddy-tests/temporaryDirectories' +import { parse } from 'smol-toml' import { expect, it } from 'vitest' -import { initializeDesktopConfig, resolveSystemLanguage } from '../initializeDesktopConfig' +import { initializeDesktopConfig } from '../initializeDesktopConfig' import { LexoraConfigStore } from '../LexoraConfigStore' async function fixture() { @@ -20,18 +21,18 @@ it('initializes only fresh profiles and persists language, incomplete setup and expect(await initializeDesktopConfig(new LexoraConfigStore(options), options)).toMatchObject({ desktop: { language: 'zh-CN', onboardingCompleted: true, navigationLayout: 'immersive' }, pet: { enabled: false } }) }) -it('does not enroll or rewrite existing profiles, including empty configs and data without a config', async () => { +it('persists the exit default without enrolling existing profiles or dropping unknown preferences', async () => { for (const content of ['', '[desktop]\nlanguage="zh-CN"\n[pet]\nenabled=false\n']) { const { options, store } = await fixture() await writeFile(options.configPath, content) expect((await initializeDesktopConfig(store, options)).desktop).toMatchObject({ language: 'zh-CN', onboardingCompleted: true }) - expect(await readFile(options.configPath, 'utf8')).toBe(content) + expect(parse(await readFile(options.configPath, 'utf8'))).toMatchObject({ ...parse(content), desktop: { ...(parse(content).desktop as object ?? {}), exit_behavior: 'stop_service' } }) } const { options, store } = await fixture() await mkdir(options.buddyHome) await writeFile(join(options.buddyHome, 'buddy.sqlite3'), 'existing data') expect((await initializeDesktopConfig(store, options)).desktop.onboardingCompleted).toBe(true) - await expect(readFile(options.configPath)).rejects.toMatchObject({ code: 'ENOENT' }) + expect(parse(await readFile(options.configPath, 'utf8'))).toEqual({ desktop: { exit_behavior: 'stop_service' } }) }) it('preserves corrupt configuration for recovery instead of resetting onboarding', async () => { @@ -42,10 +43,13 @@ it('preserves corrupt configuration for recovery instead of resetting onboarding expect(await readFile(options.configPath, 'utf8')).toBe(content) }) -it.each([ - [['ja-JP', 'zh-Hant-TW', 'en-US'], 'zh-CN'], - [['en_AU', 'zh-CN'], 'en-US'], - [['fr-FR'], 'en-US'], -] as const)('matches supported system languages in priority order: %j', (languages, expected) => { - expect(resolveSystemLanguage(languages)).toBe(expected) +it('preserves an explicit exit choice across repeated initialization and partial updates', async () => { + const { options, store } = await fixture() + await writeFile(options.configPath, '[desktop]\nexit_behavior="keep_service"\nfuture=true\n') + const before = await readFile(options.configPath, 'utf8') + expect((await initializeDesktopConfig(store, options)).desktop.exitBehavior).toBe('keep_service') + expect(await readFile(options.configPath, 'utf8')).toBe(before) + await Promise.all([store.initializeExitBehavior(), store.update({ desktop: { language: 'en-US' } })]) + expect((await new LexoraConfigStore(options).initializeExitBehavior()).desktop.exitBehavior).toBe('keep_service') + expect(parse(await readFile(options.configPath, 'utf8'))).toMatchObject({ desktop: { exit_behavior: 'keep_service', future: true } }) }) diff --git a/apps/buddy/electron/main/config/initializeDesktopConfig.ts b/apps/buddy/electron/main/config/initializeDesktopConfig.ts index 51b6b3cdc..230d16ddc 100644 --- a/apps/buddy/electron/main/config/initializeDesktopConfig.ts +++ b/apps/buddy/electron/main/config/initializeDesktopConfig.ts @@ -23,7 +23,7 @@ export async function initializeDesktopConfig(store: LexoraConfigStore, options: readdir(options.buddyHome).catch(missing), ]) if (config || data?.length) - return store.read() + return store.initializeExitBehavior() return store.update({ desktop: { onboardingCompleted: false, language: resolveSystemLanguage(options.languages) } }) } diff --git a/apps/buddy/electron/main/extensions/__tests__/ExtensionTaskCleanupRuntime.spec.ts b/apps/buddy/electron/main/extensions/__tests__/ExtensionTaskCleanupRuntime.spec.ts index c2988e44f..060d5e18f 100644 --- a/apps/buddy/electron/main/extensions/__tests__/ExtensionTaskCleanupRuntime.spec.ts +++ b/apps/buddy/electron/main/extensions/__tests__/ExtensionTaskCleanupRuntime.spec.ts @@ -34,6 +34,9 @@ async function runtime(execute: (context: { taskCleanup?: CleanupCommand }) => P AbortController, Worker: class {}, createExtensionWorkers: () => ({}), + createExtensionResourceClient: () => ({}), + createExtensionNetworkClient: () => ({}), + createExtensionInferenceClient: () => ({}), ExtensionHostEvents: class { events = {} }, addEventListener: (_name: string, callback: () => void) => { pagehide = callback }, }) @@ -66,7 +69,6 @@ it('routes by command token, isolates callback failures and releases listeners b expect(await run).toEqual({ success: true, result }) await f.progress('current', { ...progress, phase: 'scanning', total: null, processed: 0 }) expect(received).toHaveLength(2) - expect(result.summary).toEqual(summary) }) it('keeps concurrent command callbacks isolated and removes rejected and unloaded listeners', async () => { @@ -108,7 +110,7 @@ it('requires confirm before sending a request and does not expose cleanup outsid await expect(captured!.run({ confirm: () => true })).rejects.toThrow('EXTENSION_METHOD_DENIED') }) -it.each([true, false, 'true', 1, null, 'throw'] as const)('requires a boolean confirmation response: %s', async (response) => { +it.each([true, false, 'true', 'throw'] as const)('requires a boolean confirmation response: %s', async (response) => { const f = await runtime(async ({ taskCleanup }) => taskCleanup!.run({ confirm: (preview, signal) => { expect(preview).toEqual({ total: 2, skipped: 1 }) expect(Object.isFrozen(preview)).toBe(true) diff --git a/apps/buddy/electron/main/extensions/registerExtensionIpc.ts b/apps/buddy/electron/main/extensions/registerExtensionIpc.ts index c3544a5f7..92d1923ec 100644 --- a/apps/buddy/electron/main/extensions/registerExtensionIpc.ts +++ b/apps/buddy/electron/main/extensions/registerExtensionIpc.ts @@ -74,7 +74,7 @@ export function registerExtensionIpc(options: { record?: ApplicationDiagnosticReporter conditionRuntime?: (input: { models: boolean, task: boolean, taskId: string | null, runId: string | null }, signal: AbortSignal) => Promise agentRequest?: (input: { invocationId: string, method: string, params: JsonValue }, signal: AbortSignal) => Promise -}): { speech?: SpeechBackend, shells: { list: ExtensionService['shellContributions'] }, skills: { list: ExtensionService['skillContributions'] }, capabilities: { list: ExtensionService['capabilityContributions'] }, inference: { imageReaders: ExtensionService['inferenceImageReaders'], imageModels: ExtensionService['inferenceImageModels'], models: ExtensionService['inferenceModels'], list: ExtensionService['inferenceContributions'], prepare: ExtensionService['prepareInference'], invoke: (extensionId: string, revision: string, input: InferenceInvocation) => Promise, reset: () => Promise }, withStoppedStorage: ExtensionService['withStoppedStorage'], themesReady: Promise, conditions: ExtensionService['conditions'], dispose: () => Promise, reviewPackage: (path: string) => Promise, inspect: (id: string) => Promise, agent: { readResource: ExtensionService['readAgentResource'], list: () => Promise, invoke: (input: ExtensionAgentInvocation, signal: AbortSignal) => Promise } } { +}): { detachDesktop: () => Promise, speech?: SpeechBackend, shells: { list: ExtensionService['shellContributions'] }, skills: { list: ExtensionService['skillContributions'] }, capabilities: { list: ExtensionService['capabilityContributions'] }, inference: { imageReaders: ExtensionService['inferenceImageReaders'], imageModels: ExtensionService['inferenceImageModels'], models: ExtensionService['inferenceModels'], list: ExtensionService['inferenceContributions'], prepare: ExtensionService['prepareInference'], invoke: (extensionId: string, revision: string, input: InferenceInvocation) => Promise, reset: () => Promise }, withStoppedStorage: ExtensionService['withStoppedStorage'], themesReady: Promise, conditions: ExtensionService['conditions'], dispose: () => Promise, reviewPackage: (path: string) => Promise, inspect: (id: string) => Promise, agent: { releaseAllRuns: ExtensionService['releaseAllRuns'], releaseRun: ExtensionService['releaseRun'], readResource: ExtensionService['readAgentResource'], list: () => Promise, invoke: (input: ExtensionAgentInvocation, signal: AbortSignal) => Promise } } { const store = new ExtensionPackageStore(join(options.home, 'extensions'), options.version, processExtensionPackageInBackground) async function reviewResult(operation: () => Promise): Promise { try { @@ -90,7 +90,8 @@ export function registerExtensionIpc(options: { const stopProtocol = protocol.install(session.defaultSession, 'view') const hosts = new Set() const replies = new Map void }>() - const bound = new Set() + const bound = new Map void, reset: () => void, navigate: (event: Electron.Event) => void }>() + let desktopOwner: BrowserWindow | null = null let resourcePickerOpen = false let initializing = true const vault = createCredentialVault({ buddyHome: options.home, reportDiagnostic: options.record }) @@ -105,6 +106,7 @@ export function registerExtensionIpc(options: { } const speechListeners = new Set<() => void>() const service = new ExtensionService(store, { + desktopAvailable: () => false, documents: options.documents, clipboard: createExtensionClipboard(options.getWindow), taskCleanup: options.conversationCleanup ? createExtensionTaskCleanup(options.conversationCleanup) : undefined, @@ -255,27 +257,37 @@ export function registerExtensionIpc(options: { }), observeExtensionDiagnostics(service, event => options.record?.(event)), ] - const resetHosts = () => { + const resetHosts = (owner: BrowserWindow) => { + if (desktopOwner !== owner) + return + desktopOwner = null options.themes.releaseOwner('desktop') - void service.resetHosts().catch(() => {}) - } - const cancelUnconfirmedTaskCleanup = () => service.cancelUnconfirmedTaskCleanup() - const resetOnNavigation = (event: Electron.Event) => { - if (event.isMainFrame && !event.isSameDocument) - resetHosts() + void service.detachDesktop().catch(() => {}) } function window(): BrowserWindow | null { const current = options.getWindow() if (current && !current.isDestroyed() && !bound.has(current)) { - bound.add(current) - current.on('hide', cancelUnconfirmedTaskCleanup) - current.webContents.on('will-frame-navigate', guardNavigation) - current.webContents.on('did-start-navigation', resetOnNavigation) - current.webContents.on('render-process-gone', resetHosts) - current.webContents.once('destroyed', () => { + const reset = () => resetHosts(current) + const navigate = (event: Electron.Event) => { + if (event.isMainFrame && !event.isSameDocument) + reset() + } + const hide = () => { + if (desktopOwner === current) + service.cancelUnconfirmedTaskCleanup() + } + bound.set(current, { hide, reset, navigate }) + current.on('hide', hide) + const contents = current.webContents + contents.on('will-frame-navigate', guardNavigation) + contents.on('did-start-navigation', navigate) + contents.on('render-process-gone', reset) + contents.once('destroyed', () => { bound.delete(current) - for (const reply of replies.values()) reply.finish(null) - resetHosts() + if (desktopOwner === current) { + for (const reply of replies.values()) reply.finish(null) + reset() + } }) } return current @@ -307,7 +319,10 @@ export function registerExtensionIpc(options: { powerMonitor.on('suspend', suspend) powerMonitor.on('resume', resume) ipcMain.handle(EXTENSION_IPC.request, async (event, raw: unknown) => { - assertTrustedSender(event, window()) + const current = window() + assertTrustedSender(event, current) + desktopOwner = current + service.attachDesktop() try { const input = extensionManagementSchema.parse(raw) await prepared @@ -406,13 +421,13 @@ export function registerExtensionIpc(options: { finally { for (const subscription of subscriptions) subscription.dispose() for (const reply of replies.values()) reply.finish(null) - for (const current of bound) { + for (const [current, binding] of bound) { if (current.isDestroyed()) continue - current.off('hide', cancelUnconfirmedTaskCleanup) + current.off('hide', binding.hide) current.webContents.off('will-frame-navigate', guardNavigation) - current.webContents.off('did-start-navigation', resetOnNavigation) - current.webContents.off('render-process-gone', resetHosts) + current.webContents.off('did-start-navigation', binding.navigate) + current.webContents.off('render-process-gone', binding.reset) } stopProtocol() ipcMain.removeHandler(EXTENSION_IPC.request) @@ -427,6 +442,11 @@ export function registerExtensionIpc(options: { owner.webContents.send(EXTENSION_IPC.conditionsChanged, event) })) return { + async detachDesktop() { + desktopOwner = null + options.themes.releaseOwner('desktop') + await service.detachDesktop() + }, skills: { list: async () => { await prepared return service.skillContributions() @@ -490,6 +510,8 @@ export function registerExtensionIpc(options: { conditions: service.conditions, dispose, agent: { + releaseAllRuns: () => service.releaseAllRuns(), + releaseRun: runId => service.releaseRun(runId), readResource: (input, signal) => service.readAgentResource(input, signal), list: async () => { await prepared diff --git a/apps/buddy/electron/main/extensions/runtime/host.js b/apps/buddy/electron/main/extensions/runtime/host.js index 320dab8a7..27370fe64 100644 --- a/apps/buddy/electron/main/extensions/runtime/host.js +++ b/apps/buddy/electron/main/extensions/runtime/host.js @@ -32,6 +32,23 @@ let activated = false const interactions = new Map() const code = (error, method) => /^EXTENSION_[A-Z_]+$/.test(error?.message) ? error.message : method === 'activate' ? 'EXTENSION_ACTIVATION_FAILED' : 'EXTENSION_OPERATION_FAILED' const request = (method, params = null) => bridge.request(method, params) +function executionCapabilities(invocationId, signal) { + const scoped = (method, params = null) => { + signal.throwIfAborted() + return request('invocation.request', { invocationId, method, params }) + } + return Object.freeze({ + resources: createExtensionResourceClient(scoped), + network: createExtensionNetworkClient(scoped), + inference: createExtensionInferenceClient(scoped, events), + storage: Object.freeze({ get: () => scoped('storage.get'), set: value => scoped('storage.set', { value, version: manifest.dataVersion }) }), + window: Object.freeze({ styles: Object.freeze({ set: (id, css) => scoped('window.styles.set', { id, css }), remove: id => scoped('window.styles.remove', { id }) }) }), + views: Object.freeze({ broadcast: message => scoped('views.broadcast', message), open: (type, options = {}) => scoped('views.open', { type, resource: options.resource ?? null, state: options.state ?? {} }) }), + placements: Object.freeze({ show: (id, options) => scoped('placements.show', { id, ...(typeof options === 'string' ? { instanceId: options } : options ?? {}) }), hide: (id, options) => scoped('placements.hide', { id, ...(typeof options === 'string' ? { instanceId: options } : options ?? {}) }) }), + notifications: Object.freeze({ show: notification => scoped('notifications.show', notification) }), + }) +} + function cleanupCommand(token, signal) { let used = false return Object.freeze({ run: async (options) => { @@ -115,7 +132,7 @@ bridge.subscribe(async ({ id, method, params }) => { context = Object.freeze({ signal: lifetime.signal, workers, - extension: Object.freeze({ id: manifest.id, version: manifest.version, apiVersion: manifest.apiVersion }), + extension: Object.freeze({ id: manifest.id, version: manifest.version, apiVersion: manifest.apiVersion, executionScope: manifest.executionScope ?? 'desktop' }), events, themes: createThemeClient(input => request('themes.request', input), (listener) => { themeListeners.add(listener) @@ -282,6 +299,7 @@ bridge.subscribe(async ({ id, method, params }) => { try { const invocation = Object.freeze({ signal: controller.signal, + capabilities: executionCapabilities(params.invocationId, controller.signal), task: Object.freeze({ get: () => call('task.get'), messages: () => call('task.messages'), rename: value => call('task.rename', value) }), models: Object.freeze({ generateText: value => call('models.generateText', value) }), ...!params.action ? { scope: Object.freeze(await call('task.scope')), artifacts: Object.freeze({ publish: input => call('artifacts.publish', { ...input, resource: { id: input.resource.id } }) }), results: Object.freeze({ addImage: input => call('results.addImage', { resource: { id: input.resource.id } }) }), references: Object.freeze({ list: () => call('references.list'), resolve: (referenceId, options = {}) => call('references.resolve', { ...options, referenceId }) }) } : {}, @@ -301,7 +319,7 @@ bridge.subscribe(async ({ id, method, params }) => { throw new Error('EXTENSION_COMMAND_UNAVAILABLE') const controller = new AbortController() try { - result = await commands.get(params.command)(Object.freeze({ resource: params.resource, arguments: params.arguments ?? null, invocation: params.invocation ?? null, taskCleanup: params.cleanupToken ? cleanupCommand(params.cleanupToken, controller.signal) : undefined })) ?? null + result = await commands.get(params.command)(Object.freeze({ signal: controller.signal, capabilities: executionCapabilities(params.scopeId, controller.signal), resource: params.resource, arguments: params.arguments ?? null, invocation: params.invocation ?? null, taskCleanup: params.cleanupToken ? cleanupCommand(params.cleanupToken, controller.signal) : undefined })) ?? null } finally { controller.abort() } } diff --git a/apps/buddy/electron/main/index.ts b/apps/buddy/electron/main/index.ts index 20fe52cc5..31185c40e 100644 --- a/apps/buddy/electron/main/index.ts +++ b/apps/buddy/electron/main/index.ts @@ -1,3 +1,6 @@ import { startDesktopApplication } from './app/DesktopApplication' +import { startDesktopServiceEntry } from './app/desktopServiceEntry' +import { registerDesktopSchemes } from './app/environment' -void startDesktopApplication() +registerDesktopSchemes() +void startDesktopServiceEntry().then(handled => handled ? undefined : startDesktopApplication()) diff --git a/apps/buddy/electron/main/local-chat/conversation.ts b/apps/buddy/electron/main/local-chat/conversation.ts index 5303081a1..618aa5380 100644 --- a/apps/buddy/electron/main/local-chat/conversation.ts +++ b/apps/buddy/electron/main/local-chat/conversation.ts @@ -37,7 +37,6 @@ export function registerConversationIpc(context: LocalChatIpcContext): void { handle(LOCAL_CHAT_IPC_CHANNELS.conversationsGet, (_event, input) => request(conversationsRpc.get, conversationRequestSchemas.conversationId.parse(input))) handle(LOCAL_CHAT_IPC_CHANNELS.conversationsGetModelControls, (_event, input) => request(conversationsRpc.getModelControls, conversationRequestSchemas.modelControls.parse(input))) - handle(LOCAL_CHAT_IPC_CHANNELS.conversationsDeletePermanently, (_event, input) => request(conversationArchiveRpc.deletePermanently, conversationArchiveRpc.deletePermanently.input.parse(input))) handle(LOCAL_CHAT_IPC_CHANNELS.conversationsArchive, (_event, input) => request(conversationArchiveRpc.archive, conversationArchiveRpc.archive.input.parse(input))) handle(LOCAL_CHAT_IPC_CHANNELS.conversationsRestoreArchive, (_event, input) => request(conversationArchiveRpc.restoreArchive, conversationArchiveRpc.restoreArchive.input.parse(input))) handle(LOCAL_CHAT_IPC_CHANNELS.conversationsDeleteArchived, (_event, input) => request(conversationArchiveRpc.deleteArchived, conversationArchiveRpc.deleteArchived.input.parse(input))) @@ -45,7 +44,7 @@ export function registerConversationIpc(context: LocalChatIpcContext): void { handle(LOCAL_CHAT_IPC_CHANNELS.conversationsGetArchivePolicy, () => request(conversationArchiveRpc.getArchivePolicy, {})) handle(LOCAL_CHAT_IPC_CHANNELS.conversationsSetArchivePolicy, (_event, input) => request(conversationArchiveRpc.setArchivePolicy, conversationArchiveRpc.setArchivePolicy.input.parse(input))) - handle(LOCAL_CHAT_IPC_CHANNELS.conversationsDelete, (_event, input) => request(conversationsRpc.delete, conversationRequestSchemas.conversationId.parse(input))) + handle(LOCAL_CHAT_IPC_CHANNELS.conversationsDelete, (_event, input) => request(conversationsRpc.delete, conversationsRpc.delete.input.parse(input))) handle(LOCAL_CHAT_IPC_CHANNELS.conversationsActivateBranch, (_event, input) => request(conversationsRpc.activateBranch, conversationRequestSchemas.conversationBranchActivation.parse(input))) @@ -77,5 +76,5 @@ export function registerConversationIpc(context: LocalChatIpcContext): void { handle(LOCAL_CHAT_IPC_CHANNELS.chatRegenerateAssistant, (_event, input) => request(chatRpc.regenerateAssistant, chatRequestSchemas.regenerateAssistant.parse(input))) - handle(LOCAL_CHAT_IPC_CHANNELS.chatCancel, (_event, input) => request(chatRpc.cancel, runsRequestSchemas.runId.parse(input))) + handle(LOCAL_CHAT_IPC_CHANNELS.chatCancel, (_event, input) => request(chatRpc.cancel, runsRequestSchemas.cancel.parse(input))) } diff --git a/apps/buddy/electron/main/local-chat/executions.ts b/apps/buddy/electron/main/local-chat/executions.ts index 2110a4193..d3555dcce 100644 --- a/apps/buddy/electron/main/local-chat/executions.ts +++ b/apps/buddy/electron/main/local-chat/executions.ts @@ -3,6 +3,7 @@ import { executionPresentationRpc } from '../../../shared/executions/executionPr import { LOCAL_CHAT_IPC_CHANNELS } from '../../shared/localChatApi' export function registerExecutionsIpc({ handle, request }: LocalChatIpcContext): void { + handle(LOCAL_CHAT_IPC_CHANNELS.executionsActivity, (_event, input) => request(executionPresentationRpc.activity, executionPresentationRpc.activity.input.parse(input))) handle(LOCAL_CHAT_IPC_CHANNELS.executionsList, (_event, input) => request(executionPresentationRpc.list, executionPresentationRpc.list.input.parse(input))) handle(LOCAL_CHAT_IPC_CHANNELS.executionsDetail, (_event, input) => request(executionPresentationRpc.detail, executionPresentationRpc.detail.input.parse(input))) handle(LOCAL_CHAT_IPC_CHANNELS.executionsInputs, (_event, input) => request(executionPresentationRpc.inputs, executionPresentationRpc.inputs.input.parse(input))) diff --git a/apps/buddy/electron/main/local-chat/privateNetwork.ts b/apps/buddy/electron/main/local-chat/privateNetwork.ts new file mode 100644 index 000000000..8805df28b --- /dev/null +++ b/apps/buddy/electron/main/local-chat/privateNetwork.ts @@ -0,0 +1,12 @@ +import type { LocalChatIpcContext } from './registrar' +import { privateNetworkManagement } from '../../../shared/privateNetwork/management' +import { PRIVATE_NETWORK_IPC_PREFIX } from '../../shared/privateNetworkApi' + +export function registerPrivateNetworkIpc({ handle, options }: LocalChatIpcContext): void { + for (const [name, contract] of Object.entries(privateNetworkManagement)) { + handle(`${PRIVATE_NETWORK_IPC_PREFIX}${name}`, async (_event, input) => { + const value = contract.input.parse(input) + return contract.response.parse(await options.runtime.request(contract.method, value)) + }) + } +} diff --git a/apps/buddy/electron/main/local-chat/registrar.ts b/apps/buddy/electron/main/local-chat/registrar.ts index d0fa1669f..2726b4fb9 100644 --- a/apps/buddy/electron/main/local-chat/registrar.ts +++ b/apps/buddy/electron/main/local-chat/registrar.ts @@ -2,6 +2,7 @@ import type { BrowserWindow, IpcMainInvokeEvent } from 'electron' import type { ApplicationDiagnostic, ApplicationDiagnosticReporter } from '../../../shared/diagnostics/applicationDiagnostic' import type { RuntimeRequestContract, RuntimeRequestInput, RuntimeRequestResult } from '../../../shared/runtime/apiContract' import type { LocalChatErrorCode } from '../../../shared/runtime/localChatError' +import type { ProductRecoveryItem } from '../../../shared/runtime/productRecovery' import type { LexoraConfig } from '../../shared/desktopApi' import { randomUUID } from 'node:crypto' import { ipcMain } from 'electron' @@ -12,6 +13,9 @@ import { formatLocalChatPublicError, readLocalChatErrorCode } from '../../../sha import { assertTrustedSender } from '../ipc' export interface DesktopRuntimeGateway { + recover?: () => Promise + acknowledgeRecovery?: (id: string) => Promise + restoreRecovery?: (id: string) => Promise readonly state: unknown onNotification: ( listener: (notification: { method: string, params: unknown }) => void, diff --git a/apps/buddy/electron/main/local-chat/runtime.ts b/apps/buddy/electron/main/local-chat/runtime.ts index c95574600..711daee6c 100644 --- a/apps/buddy/electron/main/local-chat/runtime.ts +++ b/apps/buddy/electron/main/local-chat/runtime.ts @@ -1,10 +1,21 @@ import type { LocalChatIpcContext } from './registrar' +import { productRecoveryListSchema, productRecoveryTargetSchema } from '../../../shared/runtime/productRecovery' import { runtimeResponseSchemas } from '../../../shared/runtime/serviceState' import { LOCAL_CHAT_IPC_CHANNELS } from '../../shared/localChatApi' export function registerRuntimeIpc(context: LocalChatIpcContext): void { const { handle, options } = context + handle(LOCAL_CHAT_IPC_CHANNELS.runtimeRecoveryList, async () => productRecoveryListSchema.parse(await options.runtime.recover?.() ?? [])) + handle(LOCAL_CHAT_IPC_CHANNELS.runtimeRecoveryAcknowledge, async (_event, input) => { + await options.runtime.acknowledgeRecovery?.(productRecoveryTargetSchema.parse(input).id) + }) + handle(LOCAL_CHAT_IPC_CHANNELS.runtimeRecoveryRestore, async (_event, input) => { + if (!options.runtime.restoreRecovery) + throw new Error('Recovery unavailable') + return options.runtime.restoreRecovery(productRecoveryTargetSchema.parse(input).id) + }) + handle(LOCAL_CHAT_IPC_CHANNELS.runtimeStatus, () => ( runtimeResponseSchemas.runtimeState.parse(options.runtime.state) )) diff --git a/apps/buddy/electron/main/localChatIpc.ts b/apps/buddy/electron/main/localChatIpc.ts index a9709eb5c..215175263 100644 --- a/apps/buddy/electron/main/localChatIpc.ts +++ b/apps/buddy/electron/main/localChatIpc.ts @@ -7,6 +7,7 @@ import { registerConnectorsIpc } from './local-chat/connectors' import { registerConversationIpc } from './local-chat/conversation' import { registerExecutionsIpc } from './local-chat/executions' import { registerLocalChatNotifications } from './local-chat/notifications' +import { registerPrivateNetworkIpc } from './local-chat/privateNetwork' import { registerPromptsIpc } from './local-chat/prompts' import { registerProvidersIpc } from './local-chat/providers' import { registerQuestionsIpc } from './local-chat/questions' @@ -26,6 +27,7 @@ export function registerLocalChatIpc(options: RegisterLocalChatIpcOptions): () = registerExecutionsIpc(context) registerQuestionsIpc(context) registerRuntimeIpc(context) + registerPrivateNetworkIpc(context) registerActivityIpc(context) registerAutomationsIpc(context) registerPromptsIpc(context) diff --git a/apps/buddy/electron/main/productProtocol/DesktopProductCommands.ts b/apps/buddy/electron/main/productProtocol/DesktopProductCommands.ts new file mode 100644 index 000000000..ad35481a9 --- /dev/null +++ b/apps/buddy/electron/main/productProtocol/DesktopProductCommands.ts @@ -0,0 +1,120 @@ +import type { ProductIdentity } from '../../../shared/productProtocol/identity' +import type { ProductCommandMethod } from '../../../shared/productProtocol/methods' +import type { ProductOutboxRecord, ProductOutboxStore, ProductRecoveryInput } from './ProductOutboxStore' +import { productCommandLookupSchema, productCommandResultSchema } from '../../../shared/productProtocol/receipts' +import { isProductOutboxOwner } from './ProductOutboxStore' + +export interface DesktopProductConnection { + readonly identity: ProductIdentity + request: (method: string, params: unknown) => Promise +} +export interface DesktopCommandOutcome { + state: 'decided' | 'not_accepted' | 'unknown' | 'trust_changed' | 'connection_replaced' + record: ProductOutboxRecord +} + +export class DesktopProductCommands { + #connection: DesktopProductConnection | null = null + #commands = new Map>() + + readonly outbox: ProductOutboxStore + + constructor(outbox: ProductOutboxStore) { + this.outbox = outbox + } + + attach(connection: DesktopProductConnection): () => void { + this.#connection = connection + return () => { + if (this.#connection === connection) + this.#connection = null + } + } + + execute(input: { method: ProductCommandMethod, commandId: string, intent: unknown, recovery?: ProductRecoveryInput }): Promise { + const connection = this.#connection + if (!connection) + return Promise.reject(new Error('PRODUCT_CONNECTION_UNAVAILABLE')) + const key = commandKey(connection.identity, input.commandId) + const prior = this.#commands.get(key) + const run = () => this.#execute(connection, input) + const next = prior ? prior.then(run, run) : run() + this.#commands.set(key, next) + void next.finally(() => { + if (this.#commands.get(key) === next) + this.#commands.delete(key) + }).catch(() => {}) + return next + } + + async #execute(connection: DesktopProductConnection, input: { method: ProductCommandMethod, commandId: string, intent: unknown, recovery?: ProductRecoveryInput }): Promise { + const { created, record } = await this.outbox.create({ + identity: connection.identity, + method: input.method, + envelope: { commandId: input.commandId, dataGeneration: connection.identity.dataGeneration, expectedServiceInstanceId: connection.identity.serviceInstanceId, intent: input.intent }, + recovery: input.recovery, + }) + if (!created) + return this.#reconcile(record) + if (this.#connection !== connection) + return { state: 'connection_replaced', record } + const uncertain = await this.outbox.markUncertain(record) + if (this.#connection !== connection) + return { state: 'connection_replaced', record: uncertain } + try { + const result = productCommandResultSchema.parse(await connection.request(record.method, record.envelope)) + const decided = await this.outbox.decide(uncertain, result.receipt) + return { state: this.#connection === connection ? 'decided' : 'connection_replaced', record: decided } + } + catch { + return { state: this.#connection === connection ? 'unknown' : 'connection_replaced', record: uncertain } + } + } + + async reconcile(record: ProductOutboxRecord): Promise { + const pending = this.#commands.get(commandKey(record.owner, record.commandId)) + if (pending) + await pending.catch(() => {}) + return this.#reconcile(await this.outbox.read(record.owner, record.commandId) ?? record) + } + + async #reconcile(record: ProductOutboxRecord): Promise { + const connection = this.#connection + if (!connection) + return { state: 'unknown', record } + if (!isProductOutboxOwner(record.owner, connection.identity)) + return { state: 'trust_changed', record } + if (record.state === 'decided') + return { state: 'decided', record } + try { + const result = productCommandLookupSchema.parse(await connection.request('commands.get', { dataGeneration: record.owner.dataGeneration, commandId: record.commandId })) + if (this.#connection !== connection) + return { state: 'connection_replaced', record } + if (result.lookup === 'decided') { + const decided = await this.outbox.decide(record, result.receipt) + return { state: this.#connection === connection ? 'decided' : 'connection_replaced', record: decided } + } + if (result.lookup === 'not_found') { + const absent = await this.outbox.confirmAbsent(record) + return { state: this.#connection === connection ? 'not_accepted' : 'connection_replaced', record: absent } + } + return { state: 'unknown', record } + } + catch { + return { state: this.#connection === connection ? 'unknown' : 'connection_replaced', record } + } + } + + async recover(): Promise { + const outcomes: DesktopCommandOutcome[] = [] + for (const record of await this.outbox.list()) { + if (record.envelope || record.recovery) + outcomes.push(await this.reconcile(record)) + } + return outcomes + } +} + +function commandKey(owner: Pick, commandId: string): string { + return JSON.stringify([owner.serviceId, owner.profileId, owner.dataGeneration, owner.deviceId, commandId]) +} diff --git a/apps/buddy/electron/main/productProtocol/DesktopTaskSynchronization.ts b/apps/buddy/electron/main/productProtocol/DesktopTaskSynchronization.ts new file mode 100644 index 000000000..ae88b71c6 --- /dev/null +++ b/apps/buddy/electron/main/productProtocol/DesktopTaskSynchronization.ts @@ -0,0 +1,83 @@ +import type { z } from 'zod' +import type { ProductRpcClient } from './ProductRpcClient' +import { CONVERSATION_CHANGED, conversationSchema } from '../../../shared/conversation/conversationApi' +import { productTaskSummarySchema } from '../../../shared/productProtocol/queries' +import { decodeSafeProductRevision } from '../../../shared/productProtocol/scalars' +import { productTaskIndexChangeSchema } from '../../../shared/productProtocol/topics' +import { ProductSyncClient } from './ProductSyncClient' + +type Task = z.infer + +export class DesktopTaskSynchronization { + readonly #sync: ProductSyncClient + readonly #notify: (notification: { method: string, params: unknown }) => void + #closed = false + #tasks = new Map() + #retry: ReturnType | undefined + + constructor(client: ProductRpcClient, notify: (notification: { method: string, params: unknown }) => void) { + this.#sync = new ProductSyncClient(client) + this.#notify = notify + this.#subscribe() + } + + close(): void { + this.#closed = true + clearTimeout(this.#retry) + this.#sync.close() + this.#tasks.clear() + } + + #subscribe(): void { + if (this.#closed) + return + let staged = new Map() + const handle = this.#sync.subscribe('taskIndex', { + item: productTaskSummarySchema, + change: productTaskIndexChangeSchema, + beginSnapshot: () => { staged = new Map() }, + appendSnapshot: (tasks) => { for (const task of tasks) staged.set(task.id, task) }, + commitSnapshot: () => { + if (this.#closed) + return + for (const [id, task] of this.#tasks) { + if (!staged.has(id)) + this.#publish(task, true) + } + this.#tasks = staged + for (const task of staged.values()) this.#publish(task) + }, + applyChanges: (changes) => { + if (this.#closed) + return + const next = new Map(this.#tasks) + for (const change of changes) { + if (change.kind === 'remove') { + const previous = next.get(change.id) + next.delete(change.id) + if (previous) + this.#publish(previous, true) + } + else { + next.set(change.value.id, change.value) + this.#publish(change.value) + } + } + this.#tasks = next + }, + invalidate: () => { + if (!this.#closed) { + clearTimeout(this.#retry) + this.#retry = setTimeout(() => this.#subscribe(), 1000) + } + }, + }) + void handle.ready.catch(() => {}) + } + + #publish(task: Task, deleted = false): void { + const fields = Object.fromEntries(Object.keys(conversationSchema.shape).filter(key => key in task).map(key => [key, task[key as keyof Task]])) + const value = conversationSchema.parse({ ...fields, metadataRevision: decodeSafeProductRevision(task.revision), titleRevision: decodeSafeProductRevision(task.titleRevision), ...(deleted ? { deletedAt: new Date().toISOString() } : {}) }) + this.#notify({ method: CONVERSATION_CHANGED, params: value }) + } +} diff --git a/apps/buddy/electron/main/productProtocol/ProductDesktopGateway.ts b/apps/buddy/electron/main/productProtocol/ProductDesktopGateway.ts new file mode 100644 index 000000000..be18e9615 --- /dev/null +++ b/apps/buddy/electron/main/productProtocol/ProductDesktopGateway.ts @@ -0,0 +1,384 @@ +import type { z } from 'zod' +import type { ProductCommandMethod } from '../../../shared/productProtocol/methods' +import type { productCommandReceiptSchema } from '../../../shared/productProtocol/receipts' +import type { ProductRecoveryItem } from '../../../shared/runtime/productRecovery' +import type { DesktopRuntimeGateway } from '../local-chat/registrar' +import type { ProductOutboxRecord, ProductRecoveryInput } from './ProductOutboxStore' +import { randomUUID } from 'node:crypto' +import { buddyComposerDraftSchema } from '../../../shared/conversation/composerDraft' +import { conversationRequestSchemas, conversationResponseSchemas } from '../../../shared/conversation/conversationApi' +import { conversationArchivePolicySchema } from '../../../shared/conversation/conversationArchiveApi' +import { APPROVAL_REVIEW_KINDS } from '../../../shared/permissions/approvalReviewPayload' +import { productCommandIntents } from '../../../shared/productProtocol/methods' +import { productConversationArchivePageSchema } from '../../../shared/productProtocol/queries' +import { decodeSafeProductRevision, encodeProductRevision } from '../../../shared/productProtocol/scalars' +import { desktopProductContentProject } from '../../../shared/productProtocol/transport' +import { userQuestionRpc } from '../../../shared/questions/userQuestionApi' +import { runsResponseSchemas } from '../../../shared/runs/runApi' +import { DesktopProductCommands } from './DesktopProductCommands' +import { DesktopTaskSynchronization } from './DesktopTaskSynchronization' +import { isProductOutboxOwner, productOutboxRecordId, ProductOutboxStore } from './ProductOutboxStore' +import { ProductRpcClient } from './ProductRpcClient' + +const turnMethods = new Set(['chat.startTurn', 'chat.editUserMessage', 'chat.regenerateAssistant', 'chat.executeCommand']) +const draftMethods = new Set(['composerDrafts.open', 'composerDrafts.save']) +const questionMethods = new Set(['questions.answer', 'questions.cancel', 'questions.retry']) +type Receipt = z.infer + +export class ProductDesktopGateway implements DesktopRuntimeGateway { + readonly #runtime: DesktopRuntimeGateway + readonly #version: string + readonly #commands: DesktopProductCommands + readonly #stopState: () => void + readonly #notifications = new Set<(notification: { method: string, params: unknown }) => void>() + #synchronization: DesktopTaskSynchronization | null = null + #client: Promise | null = null + #current: ProductRpcClient | null = null + #generation = 0 + #detach: (() => void) | null = null + + constructor(runtime: DesktopRuntimeGateway, directory: string, version: string) { + this.#runtime = runtime + this.#version = version + this.#commands = new DesktopProductCommands(new ProductOutboxStore(directory)) + this.#stopState = runtime.onStateChange((state) => { + if (!state || typeof state !== 'object' || !('status' in state) || state.status !== 'ready') + this.#reset() + }) + } + + get state(): unknown { + return this.#runtime.state + } + + onNotification: DesktopRuntimeGateway['onNotification'] = (listener) => { + const stop = this.#runtime.onNotification(listener) + this.#notifications.add(listener) + return () => { + stop() + this.#notifications.delete(listener) + } + } + + onStateChange: DesktopRuntimeGateway['onStateChange'] = listener => this.#runtime.onStateChange(listener) + restart: DesktopRuntimeGateway['restart'] = () => this.#runtime.restart() + + async request(method: string, params: unknown, options?: { timeoutMs?: number, requestId?: string }): Promise { + if (method === 'conversations.listArchived' || method === 'conversations.getArchivePolicy') { + const client = await this.#connect() + const result = await client.request(method, params) + if (this.#current !== client) + throw gatewayError('COMMAND_RESULT_UNKNOWN') + if (method === 'conversations.getArchivePolicy') + return conversationArchivePolicySchema.parse(result) + const page = productConversationArchivePageSchema.parse(result) + return { ...page, items: page.items.map(({ revision, ...item }) => ({ ...item, metadataRevision: decodeSafeProductRevision(revision) })) } + } + const productMethod = method === 'approvals.approve' || method === 'approvals.deny' ? 'approvals.resolve' : method + if (!Object.hasOwn(productCommandIntents, productMethod)) + return this.#runtime.request(method, params, options) + const client = await this.#connect() + const input = objectInput(params) + const intent = { ...input } + const commandId = !questionMethods.has(method) && typeof intent.requestId === 'string' ? intent.requestId : options?.requestId ?? randomUUID() + if (!questionMethods.has(method)) + delete intent.requestId + if (method === 'composerDrafts.open') { + const current = objectInput(await client.request('composerDrafts.find', { scope: input.scope })) + if (current.draft) { + const draft = objectInput(current.draft) + return this.#runtime.request('composerDrafts.get', { draftId: draft.draftId }, options) + } + intent.expectedSlotRevision = current.slotRevision + } + if (draftMethods.has(method)) { + const field = method === 'composerDrafts.open' ? 'initialContent' : 'content' + intent[field] = desktopProductContentProject.response.parse(await this.#runtime.request(desktopProductContentProject.method, { content: input[field] }, options)).content + } + if (method === 'composerDrafts.copy' && intent.recoveredInput) { + const recovered = objectInput(intent.recoveredInput) + intent.recoveredInput = { ...recovered, content: desktopProductContentProject.response.parse(await this.#runtime.request(desktopProductContentProject.method, { content: recovered.content }, options)).content } + } + if (productMethod === 'approvals.resolve') { + const conditions = objectInput(input.conditions) + intent.runId = conditions.runId + intent.reviewVersion = conditions.reviewVersion + intent.decision = method === 'approvals.deny' ? 'denied' : input.scope === 'once' ? 'approved' : input.scope === 'persistent' ? 'approved_persistently' : `approved_for_${input.scope}` + delete intent.conditions + delete intent.scope + } + const actualMethod = productMethod === 'chat.queue.steer' && intent.targetRunId === null ? 'chat.queue.continue' : productMethod + if (actualMethod === 'chat.queue.continue') { + delete intent.targetRunId + delete intent.expectedRunGeneration + } + for (const key of ['expectedRevision', 'expectedSourceRevision', 'expectedSlotRevision', 'expectedRunGeneration', 'expectedTitleRevision', 'expectedAssignmentRevision', 'readRevision']) { + if (typeof intent[key] === 'number') + intent[key] = encodeProductRevision(intent[key]) + } + if (intent.expectedActiveRun && typeof intent.expectedActiveRun === 'object') { + const active = objectInput(intent.expectedActiveRun) + intent.expectedActiveRun = { ...active, generation: encodeProductRevision(Number(active.generation)) } + } + const recovery = await this.#recoveryInput(method, input, client, commandId) + const outcome = await this.#commands.execute({ method: actualMethod as ProductCommandMethod, commandId, intent, recovery }) + if (outcome.state !== 'decided' || !outcome.record.receipt) + throw gatewayError(outcome.state === 'trust_changed' ? 'AUTHENTICATION_REQUIRED' : 'COMMAND_RESULT_UNKNOWN') + const receipt = outcome.record.receipt + if (receipt.decision === 'rejected') + throw gatewayError(receipt.errorCode ?? 'LOCAL_CHAT_OPERATION_FAILED') + if (this.#current !== client) + throw gatewayError('COMMAND_RESULT_UNKNOWN') + const result = await this.#projectResult(method, input, receipt, options) + if (this.#current !== client) + throw gatewayError('COMMAND_RESULT_UNKNOWN') + void this.#commands.outbox.archive(outcome.record).catch(() => {}) + return result + } + + async getIdentity() { + return (await this.#connect()).identity + } + + async recover(): Promise { + const client = await this.#connect() + const outcomes = await this.#commands.recover() + return outcomes.map(({ state, record }) => { + const status = state === 'decided' ? record.receipt!.decision : state === 'connection_replaced' ? 'unknown' : state + return { + id: productOutboxRecordId(record), + commandId: record.commandId, + method: record.method, + createdAt: record.createdAt, + state: status, + conversationId: record.receipt?.target.conversationId ?? null, + content: record.recovery?.content ?? null, + canRestore: (status === 'rejected' || status === 'not_accepted') && Boolean(record.recovery?.executionConfig) && isProductOutboxOwner(record.owner, client.identity), + } + }) + } + + async acknowledgeRecovery(id: string): Promise { + const record = await this.#recoveryRecord(id) + await this.#commands.outbox.archive(record) + } + + async restoreRecovery(id: string): Promise { + const client = await this.#connect() + const outcome = await this.#commands.reconcile(await this.#recoveryRecord(id)) + const source = outcome.record.recovery + if (!isProductOutboxOwner(outcome.record.owner, client.identity) || !source?.executionConfig + || !(outcome.state === 'not_accepted' || (outcome.state === 'decided' && outcome.record.receipt?.decision === 'rejected'))) { + throw gatewayError('COMMAND_RESULT_UNKNOWN') + } + const commandId = `recovery-${id}` + const previous = await this.#commands.outbox.read(outboxOwner(client), commandId) + if (previous) { + const recovered = await this.#commands.reconcile(previous) + if (recovered.state !== 'decided' || recovered.record.receipt?.decision !== 'accepted' || !recovered.record.receipt.target.draftId) + throw gatewayError('COMMAND_RESULT_UNKNOWN') + await this.#commands.outbox.archive(outcome.record) + return recovered.record.receipt.target.draftId + } + const current = await client.request('composerDrafts.get', { draftId: source.draftId }) as { revision: string } | null + let draftId: string + if (current) { + const draft = buddyComposerDraftSchema.parse(await this.request('composerDrafts.copy', { + sourceDraftId: source.draftId, + expectedSourceRevision: current.revision, + targetScope: { kind: 'task', spaceId: null }, + expectedSlotRevision: '0', + recoveredInput: { content: source.content, executionConfig: source.executionConfig, modelSelection: source.modelSelection ?? null, ...(source.participants ? { participants: source.participants } : {}) }, + }, { requestId: commandId })) + draftId = draft.draftId + } + else { + draftId = `recovered-${id.slice(0, 32)}` + await this.request('composerDrafts.open', { + draftId, + scope: { kind: 'task', draftId, spaceId: null }, + initialContent: source.content, + initialExecutionConfig: source.executionConfig, + initialModelSelection: source.modelSelection ?? null, + ...(source.participants ? { initialParticipants: source.participants } : {}), + }, { requestId: commandId }) + } + await this.#commands.outbox.archive(outcome.record) + return draftId + } + + async #recoveryRecord(id: string): Promise { + const record = (await this.#commands.outbox.list()).find(record => productOutboxRecordId(record) === id) + if (!record) + throw gatewayError('OBJECT_DELETED') + return record + } + + async #recoveryInput(method: string, input: Record, client: ProductRpcClient, commandId: string): Promise { + const existing = await this.#commands.outbox.read(outboxOwner(client), commandId) + if (existing) + return existing.recovery ?? undefined + if (method === 'composerDrafts.open' || method === 'composerDrafts.save') { + const opening = method === 'composerDrafts.open' + const draft = buddyComposerDraftSchema.parse({ + draftId: input.draftId, + content: input[opening ? 'initialContent' : 'content'], + modelSelection: input[opening ? 'initialModelSelection' : 'modelSelection'], + executionConfig: input[opening ? 'initialExecutionConfig' : 'executionConfig'], + participants: input[opening ? 'initialParticipants' : 'participants'], + scope: input.scope ?? { kind: 'global' }, + revision: input.expectedRevision ?? 0, + updatedAt: new Date().toISOString(), + }) + return { draftId: draft.draftId, editSequence: String(draft.revision), content: draft.content, executionConfig: draft.executionConfig, modelSelection: draft.modelSelection, participants: draft.participants } + } + if (typeof input.draftId === 'string' && (turnMethods.has(method) || method === 'chat.queue.enqueue')) { + const draft = buddyComposerDraftSchema.parse(await this.#runtime.request('composerDrafts.get', { draftId: input.draftId })) + if (draft.revision === input.expectedRevision) + return { draftId: draft.draftId, editSequence: String(draft.revision), content: draft.content, executionConfig: draft.executionConfig, modelSelection: draft.modelSelection, participants: draft.participants } + } + return undefined + } + + dispose(): void { + this.#stopState() + this.#reset() + } + + #reset(): void { + this.#generation += 1 + this.#synchronization?.close() + this.#synchronization = null + this.#detach?.() + this.#detach = null + this.#current?.close() + this.#current = null + this.#client = null + } + + #connect(): Promise { + if (this.#client) + return this.#client + const generation = this.#generation + const client = new ProductRpcClient({ + send: frame => this.#runtime.request('product.exchange', { frame }).then(() => {}), + subscribe: listener => this.#runtime.onNotification((notification) => { + if (notification.method !== 'product.frame') + return + const value = notification.params + if (value && typeof value === 'object' && 'frame' in value && typeof value.frame === 'string') + listener(value.frame) + }), + close: () => {}, + }) + this.#current = client + this.#client = client.initialize({ protocol: { major: 1, minMinor: 0, maxMinor: 0 }, client: { kind: 'desktop', version: this.#version }, supportedContentKinds: ['buddy-user-content-v1'], supportedReviewKinds: [...APPROVAL_REVIEW_KINDS] }).then(() => { + if (generation !== this.#generation) { + client.close() + throw gatewayError('RUNTIME_UNAVAILABLE') + } + this.#detach = this.#commands.attach(client) + this.#synchronization = new DesktopTaskSynchronization(client, (notification) => { + if (generation !== this.#generation) + return + for (const listener of this.#notifications) { + try { + listener(notification) + } + catch {} + } + }) + return client + }) + return this.#client + } + + async #projectResult(method: string, input: Record, receipt: Receipt, options?: { timeoutMs?: number, requestId?: string }): Promise { + const read = (method: string, params: unknown) => this.#runtime.request(method, params, options) + if (questionMethods.has(method)) { + const questions = userQuestionRpc.list.response.parse(await read('questions.list', { conversationId: input.conversationId, branchId: input.branchId })) + const question = questions.find(question => question.id === receipt.target.questionId) + if (!question) + throw gatewayError('OBJECT_DELETED') + return question + } + if (turnMethods.has(method)) { + const run = runsResponseSchemas.run.parse(await read('runs.get', { runId: receipt.target.runId })) + let userMessage = null + if (run.purpose === 'chat' && run.triggeringMessageId) { + let cursor: string | null = null + do { + const page = conversationResponseSchemas.messagePage.parse(await read('conversations.listMessages', conversationRequestSchemas.conversationMessages.parse({ conversationId: run.conversationId, branchId: run.branchId, ...(cursor ? { cursor } : {}) }))) + userMessage = page.items.find(message => message.id === run.triggeringMessageId) ?? null + cursor = page.nextCursor + } while (!userMessage && cursor) + } + const draft = receipt.result.draftReceipt + return { conversationId: run.conversationId, branchId: run.branchId, runId: run.id, run, userMessage, draftReceipt: draft ? { draftId: draft.draftId, sourceRevision: input.expectedRevision, committedRevision: decodeSafeProductRevision(draft.committedRevision) } : null } + } + if (draftMethods.has(method) || method === 'composerDrafts.copy') { + const draft = buddyComposerDraftSchema.parse(await read('composerDrafts.get', { draftId: receipt.target.draftId })) + if (method === 'composerDrafts.save' && draft.revision !== decodeSafeProductRevision(receipt.result.revision!)) + throw gatewayError('DRAFT_CONFLICT') + return draft + } + if (method === 'composerDrafts.discard') + return receipt.result.discarded === true + if (method === 'chat.queue.enqueue') { + const draft = receipt.result.draftReceipt! + return { id: receipt.target.queueId, conversationId: receipt.target.conversationId, branchId: receipt.target.branchId, draftReceipt: { draftId: draft.draftId, sourceRevision: input.expectedRevision, committedRevision: decodeSafeProductRevision(draft.committedRevision) } } + } + if (method === 'chat.queue.cancel') + return receipt.result.cancelled === true + if (method === 'chat.queue.steer') + return 'accepted' + if (method === 'chat.cancel') + return read('runs.get', { runId: receipt.target.runId }) + if (method === 'approvals.approve' || method === 'approvals.deny') { + const approvals = await read('approvals.list', { runId: receipt.target.runId }) as Array<{ id: string }> + const approval = approvals.find(approval => approval.id === receipt.target.approvalId) + if (!approval) + throw gatewayError('OBJECT_DELETED') + return approval + } + if (method === 'conversations.delete') + return true + if (method === 'conversations.archive') + return receipt.result.archived === true + if (method === 'conversations.restoreArchive') + return receipt.result.restored === true + if (method === 'conversations.deleteArchived' || method === 'conversations.deleteLegacyRemoved') + return receipt.result.deleted === true + if (method === 'conversations.setArchivePolicy') + return conversationArchivePolicySchema.parse(receipt.result.archivePolicy) + if (method === 'taskMarks.delete') + return { ok: true } + if (method === 'conversations.addMember' || method === 'conversations.removeMember') + return read('conversations.listMembers', { conversationId: input.conversationId }) + if (method.startsWith('conversations.')) + return read('conversations.get', { conversationId: input.conversationId }) + if (method === 'taskMarks.create' || method === 'taskMarks.update') { + const marks = await read('taskMarks.list', {}) as Array<{ id: string }> + return marks.find(mark => mark.id === receipt.target.markId) + } + if (method.startsWith('taskMarks.')) { + const states = await read('taskMarks.states', { conversationIds: [input.conversationId] }) as unknown[] + return states[0] + } + throw gatewayError('METHOD_NOT_AVAILABLE') + } +} + +function objectInput(value: unknown): Record { + if (!value || typeof value !== 'object' || Array.isArray(value)) + throw gatewayError('VALIDATION_FAILED') + return value as Record +} +function gatewayError(code: string): Error { + return Object.assign(new Error(code), { code }) +} + +function outboxOwner(client: ProductRpcClient) { + const { serviceId, profileId, dataGeneration, deviceId } = client.identity + return { serviceId, profileId, dataGeneration, deviceId } +} diff --git a/apps/buddy/electron/main/productProtocol/ProductOutboxStore.ts b/apps/buddy/electron/main/productProtocol/ProductOutboxStore.ts new file mode 100644 index 000000000..7e5d46078 --- /dev/null +++ b/apps/buddy/electron/main/productProtocol/ProductOutboxStore.ts @@ -0,0 +1,199 @@ +import type { ProductIdentity } from '../../../shared/productProtocol/identity' +import type { ProductCommandMethod } from '../../../shared/productProtocol/methods' +import { Buffer } from 'node:buffer' +import { createHash } from 'node:crypto' +import { readdir, unlink } from 'node:fs/promises' +import { join } from 'node:path' +import { z } from 'zod' +import { isMissing, readStorageJson, syncStorageDirectory, writeStorageJson } from '../../../platform/storage/storageFiles' +import { buddyUserContentV1Schema } from '../../../shared/conversation/buddyUserContent' +import { buddyComposerDraftExecutionConfigSchema, buddyComposerDraftModelSelectionSchema, buddyDraftParticipantsSchema } from '../../../shared/conversation/composerDraft' +import { productIdentitySchema } from '../../../shared/productProtocol/identity' +import { PRODUCT_LIMITS } from '../../../shared/productProtocol/limits' +import { getProductMethod } from '../../../shared/productProtocol/methods' +import { productCommandReceiptSchema } from '../../../shared/productProtocol/receipts' +import { productIdSchema, productRevisionSchema } from '../../../shared/productProtocol/scalars' + +const ownerSchema = productIdentitySchema.pick({ serviceId: true, profileId: true, dataGeneration: true, deviceId: true }) +const envelopeSchema = z.object({ dataGeneration: productIdSchema, commandId: productIdSchema, expectedServiceInstanceId: productIdSchema, intent: z.unknown() }).strict() +const recoverySchema = z.object({ draftId: productIdSchema, editSequence: productRevisionSchema, content: buddyUserContentV1Schema, executionConfig: buddyComposerDraftExecutionConfigSchema.optional(), modelSelection: buddyComposerDraftModelSelectionSchema.nullable().optional(), participants: buddyDraftParticipantsSchema.nullable().optional() }).strict() +const recordSchema = z.object({ + version: z.literal(1), + owner: ownerSchema, + commandId: productIdSchema, + method: productIdSchema, + fingerprint: z.string().regex(/^[a-f0-9]{64}$/), + createdAt: z.iso.datetime(), + state: z.enum(['prepared', 'uncertain', 'not_accepted', 'decided']), + envelope: envelopeSchema.nullable(), + recovery: recoverySchema.nullable(), + receipt: productCommandReceiptSchema.nullable(), +}).strict().refine(record => (record.state === 'decided') === (record.receipt !== null)) +export type ProductOutboxRecord = z.infer +export type ProductOutboxOwner = z.infer +export type ProductRecoveryInput = z.infer +export type ProductOutboxEnvelope = z.infer +const maxRecordBytes = PRODUCT_LIMITS.frameBytes * 2 + +export class ProductOutboxStore { + #tail: Promise = Promise.resolve() + + readonly directory: string + + constructor(directory: string) { + this.directory = directory + } + + create(input: { identity: ProductIdentity, method: ProductCommandMethod, envelope: ProductOutboxEnvelope, recovery?: ProductRecoveryInput }): Promise<{ created: boolean, record: ProductOutboxRecord }> { + const method = getProductMethod(input.method) + if (!method?.command) + throw new Error('OUTBOX_INVALID_METHOD') + const envelope = envelopeSchema.parse(method.input.parse(input.envelope)) + const owner = ownerSchema.parse({ serviceId: input.identity.serviceId, profileId: input.identity.profileId, dataGeneration: input.identity.dataGeneration, deviceId: input.identity.deviceId }) + if (envelope.dataGeneration !== owner.dataGeneration) + throw new Error('OUTBOX_IDENTITY_CHANGED') + const fingerprint = digest({ method: input.method, dataGeneration: envelope.dataGeneration, commandId: envelope.commandId, intent: envelope.intent }) + const record = recordSchema.parse({ version: 1, owner, commandId: envelope.commandId, method: input.method, fingerprint, createdAt: new Date().toISOString(), state: 'prepared', envelope, recovery: input.recovery ?? null, receipt: null }) + return this.#serial(async () => { + const existing = await this.#read(owner, envelope.commandId) + if (existing) { + if (existing.fingerprint !== fingerprint) + throw new Error('OUTBOX_COMMAND_ID_REUSED') + return { created: false, record: existing } + } + await this.#write(record) + return { created: true, record } + }) + } + + read(owner: ProductOutboxOwner, commandId: string): Promise { + return this.#serial(() => this.#read(ownerSchema.parse(owner), productIdSchema.parse(commandId))) + } + + list(): Promise { + return this.#serial(async () => { + const names = await readdir(this.directory).catch((error) => { + if (!isMissing(error)) + throw error + return [] + }) + const records: ProductOutboxRecord[] = [] + for (const name of names.sort()) { + if (!/^[a-f0-9]{64}\.json$/.test(name)) + continue + const record = recordSchema.parse(await readStorageJson(join(this.directory, name), maxRecordBytes)) + if (name !== recordName(record.owner, record.commandId)) + throw new Error('OUTBOX_IDENTITY_CHANGED') + validateRecord(record) + records.push(record) + } + return records + }) + } + + markUncertain(record: ProductOutboxRecord): Promise { + return this.#update(record, current => current.state === 'prepared' ? { ...current, state: 'uncertain' } : current) + } + + confirmAbsent(record: ProductOutboxRecord): Promise { + return this.#update(record, current => current.state === 'decided' ? current : { ...current, state: 'not_accepted' }) + } + + decide(record: ProductOutboxRecord, receipt: z.infer): Promise { + const parsed = productCommandReceiptSchema.parse(receipt) + if (parsed.dataGeneration !== record.owner.dataGeneration || parsed.commandId !== record.commandId || parsed.method !== record.method) + throw new Error('OUTBOX_RECEIPT_MISMATCH') + return this.#update(record, (current) => { + if (current.receipt && digest(current.receipt) !== digest(parsed)) + throw new Error('OUTBOX_RECEIPT_CHANGED') + return { ...current, state: 'decided', receipt: parsed } + }) + } + + archive(record: ProductOutboxRecord): Promise { + return this.#serial(async () => { + const current = await this.#read(record.owner, record.commandId) + if (!current || current.fingerprint !== record.fingerprint) + throw new Error('OUTBOX_RECORD_CHANGED') + if (current.state !== 'decided' && current.state !== 'not_accepted') + throw new Error('OUTBOX_NOT_RECONCILED') + const archived = recordSchema.parse({ ...current, envelope: null, recovery: null }) + const name = recordName(current.owner, current.commandId) + await writeStorageJson(join(this.directory, 'archive', name), archived) + await unlink(join(this.directory, name)).catch((error) => { + if (!isMissing(error)) + throw error + }) + await syncStorageDirectory(this.directory) + return archived + }) + } + + #update(record: ProductOutboxRecord, update: (current: ProductOutboxRecord) => ProductOutboxRecord): Promise { + return this.#serial(async () => { + const current = await this.#read(record.owner, record.commandId) + if (!current || current.fingerprint !== record.fingerprint) + throw new Error('OUTBOX_RECORD_CHANGED') + const next = recordSchema.parse(update(current)) + await this.#write(next) + return next + }) + } + + async #read(owner: ProductOutboxOwner, commandId: string): Promise { + const name = recordName(owner, commandId) + const raw = await readStorageJson(join(this.directory, name), maxRecordBytes) ?? await readStorageJson(join(this.directory, 'archive', name), maxRecordBytes) + if (raw === null) + return null + const record = recordSchema.parse(raw) + if (digest(record.owner) !== digest(owner) || record.commandId !== commandId) + throw new Error('OUTBOX_IDENTITY_CHANGED') + validateRecord(record) + return record + } + + async #write(record: ProductOutboxRecord): Promise { + validateRecord(record) + if (Buffer.byteLength(JSON.stringify(record)) > maxRecordBytes) + throw new Error('OUTBOX_INPUT_TOO_LARGE') + await writeStorageJson(join(this.directory, recordName(record.owner, record.commandId)), record) + } + + #serial(operation: () => Promise): Promise { + const next = this.#tail.then(operation, operation) + this.#tail = next + return next + } +} + +export function isProductOutboxOwner(owner: ProductOutboxOwner, identity: ProductIdentity): boolean { + return owner.serviceId === identity.serviceId && owner.profileId === identity.profileId && owner.dataGeneration === identity.dataGeneration && owner.deviceId === identity.deviceId +} + +function validateRecord(record: ProductOutboxRecord): void { + if (!record.envelope && (record.state === 'prepared' || record.state === 'uncertain')) + throw new Error('OUTBOX_INPUT_MISSING') + if (record.envelope && (record.envelope.commandId !== record.commandId || record.envelope.dataGeneration !== record.owner.dataGeneration + || digest({ method: record.method, dataGeneration: record.envelope.dataGeneration, commandId: record.commandId, intent: record.envelope.intent }) !== record.fingerprint)) { + throw new Error('OUTBOX_INTENT_CHANGED') + } + if (record.receipt && (record.receipt.commandId !== record.commandId || record.receipt.dataGeneration !== record.owner.dataGeneration || record.receipt.method !== record.method)) + throw new Error('OUTBOX_RECEIPT_MISMATCH') +} +function recordName(owner: ProductOutboxOwner, commandId: string): string { + return `${digest({ ...owner, commandId })}.json` +} +function digest(value: unknown): string { + return createHash('sha256').update(JSON.stringify(canonical(value))).digest('hex') +} +function canonical(value: unknown): unknown { + if (Array.isArray(value)) + return value.map(canonical) + if (value && typeof value === 'object') + return Object.fromEntries(Object.entries(value).filter(([, entry]) => entry !== undefined).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([key, entry]) => [key, canonical(entry)])) + return value +} + +export function productOutboxRecordId(record: ProductOutboxRecord): string { + return digest({ owner: record.owner, commandId: record.commandId }) +} diff --git a/apps/buddy/electron/main/productProtocol/ProductRpcClient.ts b/apps/buddy/electron/main/productProtocol/ProductRpcClient.ts new file mode 100644 index 000000000..ab1d760a4 --- /dev/null +++ b/apps/buddy/electron/main/productProtocol/ProductRpcClient.ts @@ -0,0 +1,161 @@ +import type { z } from 'zod' +import type { ProductError } from '../../../shared/productProtocol/errors' +import type { ProductIdentity } from '../../../shared/productProtocol/identity' +import { randomUUID } from 'node:crypto' +import { z as schema } from 'zod' +import { productErrorSchema } from '../../../shared/productProtocol/errors' +import { productIdentitySchema, productInitializeResultSchema, productInitializeSchema } from '../../../shared/productProtocol/identity' +import { encodeProductFrame, PRODUCT_LIMITS, productByteLength } from '../../../shared/productProtocol/limits' +import { getProductMethod } from '../../../shared/productProtocol/methods' +import { productAckSchema, productSnapshotPageSchema, productSubscribeSchema, productUnsubscribeSchema } from '../../../shared/productProtocol/sync' + +export interface ProductClientTransport { + send: (frame: string) => void | Promise + subscribe: (listener: (frame: string) => void) => () => void + close: () => void +} +interface PendingRequest { + resolve: (value: unknown) => void + reject: (error: unknown) => void + timer: ReturnType +} +const replySchema = schema.union([ + schema.object({ jsonrpc: schema.literal('2.0'), id: schema.string(), result: schema.unknown() }).strict(), + schema.object({ jsonrpc: schema.literal('2.0'), id: schema.string().nullable(), error: productErrorSchema }).strict(), + schema.object({ jsonrpc: schema.literal('2.0'), method: schema.enum(['sync.delta', 'sync.resyncRequired']), params: schema.unknown() }).strict(), +]) + +export class ProductRpcClient { + readonly #transport: ProductClientTransport + readonly #pending = new Map() + readonly #listeners = new Set<(notification: { method: string, params: unknown }) => void>() + readonly #unsubscribe: () => void + readonly #timeoutMs: number + #identity: ProductIdentity | null = null + #initializing: Promise> | null = null + #closed = false + + constructor(transport: ProductClientTransport, timeoutMs = 30_000) { + this.#transport = transport + this.#timeoutMs = timeoutMs + this.#unsubscribe = transport.subscribe(frame => this.#receive(frame)) + } + + get identity(): ProductIdentity { + if (!this.#identity) + throw new Error('PRODUCT_NOT_INITIALIZED') + return this.#identity + } + + initialize(input: z.infer): Promise> { + if (!this.#initializing) { + const parsed = productInitializeSchema.parse(input) + this.#initializing = this.#request('initialize', parsed).then((raw) => { + const result = productInitializeResultSchema.parse(raw) + this.#identity = Object.freeze(productIdentitySchema.parse(Object.fromEntries(Object.keys(productIdentitySchema.shape).map(key => [key, result[key as keyof typeof result]])))) + return result + }).catch((error) => { + this.close() + throw error + }) + } + return this.#initializing + } + + async request(method: string, params: unknown): Promise { + if (!this.#identity) + throw new Error('PRODUCT_NOT_INITIALIZED') + const contract = getProductMethod(method) + if (contract) + return contract.output.parse(await this.#request(method, contract.input.parse(params))) + const sync = method === 'sync.subscribe' ? productSubscribeSchema : method === 'sync.snapshotPage' ? productSnapshotPageSchema : method === 'sync.ack' ? productAckSchema : method === 'sync.unsubscribe' ? productUnsubscribeSchema : null + if (!sync) + throw new Error('PRODUCT_METHOD_NOT_AVAILABLE') + return this.#request(method, sync.parse(params)) + } + + onNotification(listener: (notification: { method: string, params: unknown }) => void): () => void { + this.#listeners.add(listener) + return () => this.#listeners.delete(listener) + } + + close(): void { + if (this.#closed) + return + this.#closed = true + this.#unsubscribe() + this.#transport.close() + for (const request of this.#pending.values()) { + clearTimeout(request.timer) + request.reject(new Error('PRODUCT_CONNECTION_CLOSED')) + } + this.#pending.clear() + this.#listeners.clear() + } + + #request(method: string, params: unknown): Promise { + if (this.#closed) + return Promise.reject(new Error('PRODUCT_CONNECTION_CLOSED')) + if (this.#pending.size >= PRODUCT_LIMITS.inFlightRequests) + return Promise.reject(new Error('PRODUCT_REQUEST_LIMIT')) + const id = randomUUID() + const frame = encodeProductFrame({ jsonrpc: '2.0', id, method, params }) + return new Promise((resolve, reject) => { + const timer = setTimeout(() => { + this.#pending.delete(id) + reject(new Error('PRODUCT_RESPONSE_UNKNOWN')) + }, this.#timeoutMs) + this.#pending.set(id, { resolve, reject, timer }) + try { + Promise.resolve(this.#transport.send(frame)).catch(() => this.close()) + } + catch { this.close() } + }) + } + + #receive(frame: string): void { + if (this.#closed) + return + try { + if (frame.length > PRODUCT_LIMITS.frameBytes || productByteLength(frame) > PRODUCT_LIMITS.frameBytes) + throw new Error('PRODUCT_FRAME_LIMIT') + const reply = replySchema.parse(JSON.parse(frame)) + if ('method' in reply) { + if (!this.#identity) + throw new Error('PRODUCT_NOT_INITIALIZED') + for (const listener of this.#listeners) { + try { + listener({ method: reply.method, params: reply.params }) + } + catch {} + } + return + } + if (reply.id === null) + throw new Error('PRODUCT_UNCORRELATED_ERROR') + const request = this.#pending.get(reply.id) + if (!request) + return + this.#pending.delete(reply.id) + clearTimeout(request.timer) + if ('error' in reply) + request.reject(new ProductResponseError(reply.error)) + else + request.resolve(reply.result) + } + catch { this.close() } + } +} + +export class ProductResponseError extends Error { + readonly code: ProductError['code'] + readonly acceptance: ProductError['acceptance'] + readonly recovery: ProductError['recovery'] + + constructor(error: ProductError) { + super(error.message) + this.code = error.code + this.acceptance = error.acceptance + this.recovery = error.recovery + } +} diff --git a/apps/buddy/electron/main/productProtocol/ProductSyncClient.ts b/apps/buddy/electron/main/productProtocol/ProductSyncClient.ts new file mode 100644 index 000000000..7dc1a2593 --- /dev/null +++ b/apps/buddy/electron/main/productProtocol/ProductSyncClient.ts @@ -0,0 +1,181 @@ +import type { ZodType } from 'zod' +import type { ProductSyncCursor, ProductSyncDelta } from '../../../shared/productProtocol/sync' +import type { ProductRpcClient } from './ProductRpcClient' +import { randomUUID } from 'node:crypto' +import { PRODUCT_LIMITS, productByteLength } from '../../../shared/productProtocol/limits' +import { classifyProductDelta, productResyncRequiredSchema, productSyncDeltaSchema, productSyncSnapshotSchema } from '../../../shared/productProtocol/sync' + +export interface ProductTopicConsumer { + item: ZodType + change: ZodType + beginSnapshot: () => void | Promise + appendSnapshot: (items: Item[]) => void | Promise + commitSnapshot: (cursor: ProductSyncCursor) => void | Promise + applyChanges: (changes: Change[], cursor: ProductSyncCursor) => void | Promise + invalidate: (error: unknown) => void +} +interface Subscription { + id: string + cursor: ProductSyncCursor | null + queue: Array<{ delta: ProductSyncDelta, bytes: number }> + bytes: number + items: number + ready: boolean + active: boolean + processing: boolean + consumer: ProductTopicConsumer +} + +export class ProductSyncClient { + readonly #client: Pick + readonly #stop: () => void + readonly #subscriptions = new Map() + #bytes = 0 + #closed = false + + constructor(client: Pick) { + this.#client = client + this.#stop = client.onNotification(notification => this.#receive(notification)) + } + + subscribe(topic: string, consumer: ProductTopicConsumer) { + if (this.#closed || this.#subscriptions.size >= PRODUCT_LIMITS.subscriptionsPerConnection) + throw new Error('PRODUCT_SUBSCRIPTION_LIMIT') + const subscription: Subscription = { id: randomUUID(), cursor: null, queue: [], bytes: 0, items: 0, ready: false, active: true, processing: false, consumer: consumer as ProductTopicConsumer } + this.#subscriptions.set(subscription.id, subscription) + const ready = this.#snapshot(subscription, topic).catch((error) => { + this.#invalidate(subscription, error) + throw error + }) + return { ready, dispose: () => this.#remove(subscription) } + } + + close(): void { + if (this.#closed) + return + this.#closed = true + this.#stop() + for (const subscription of [...this.#subscriptions.values()]) + this.#remove(subscription) + } + + async #snapshot(subscription: Subscription, topic: string): Promise { + let page = productSyncSnapshotSchema.parse(await this.#client.request('sync.subscribe', { subscriptionId: subscription.id, topic })) + if (!subscription.active) + return + if (page.mode !== 'snapshot' || page.subscriptionId !== subscription.id) + throw new Error('SYNC_INVALID') + const cursor = page.cursor + await subscription.consumer.beginSnapshot() + const cursors = new Set() + while (subscription.active) { + if (page.mode !== 'snapshot' || page.subscriptionId !== subscription.id || page.cursor.syncEpoch !== cursor.syncEpoch || page.cursor.seq !== cursor.seq) + throw new Error('SYNC_INVALID') + const bytes = productByteLength(JSON.stringify(page)) + if (bytes > PRODUCT_LIMITS.pageBytes || this.#bytes + bytes > PRODUCT_LIMITS.connectionBytes) + throw new Error('RESYNC_REQUIRED') + this.#bytes += bytes + try { + const items = page.items.map(item => subscription.consumer.item.parse(item)) + await subscription.consumer.appendSnapshot(items) + } + finally { this.#bytes -= bytes } + if (!subscription.active) + return + if (page.complete) + break + if (!page.nextCursor || cursors.has(page.nextCursor)) + throw new Error('SYNC_INVALID') + cursors.add(page.nextCursor) + page = productSyncSnapshotSchema.parse(await this.#client.request('sync.snapshotPage', { subscriptionId: subscription.id, cursor: page.nextCursor })) + } + if (!subscription.active) + return + await subscription.consumer.commitSnapshot(cursor) + if (!subscription.active) + return + subscription.cursor = cursor + subscription.ready = true + await this.#pump(subscription) + } + + #receive(notification: { method: string, params: unknown }): void { + try { + if (notification.method === 'sync.resyncRequired') { + const request = productResyncRequiredSchema.parse(notification.params) + const subscription = this.#subscriptions.get(request.subscriptionId) + if (subscription) + this.#invalidate(subscription, new Error('RESYNC_REQUIRED')) + return + } + if (notification.method !== 'sync.delta') + return + const delta = productSyncDeltaSchema.parse(notification.params) + const subscription = this.#subscriptions.get(delta.subscriptionId) + if (!subscription?.active) + return + const bytes = productByteLength(JSON.stringify(delta)) + if (subscription.bytes + bytes > PRODUCT_LIMITS.subscriptionBytes || subscription.items + delta.changes.length > PRODUCT_LIMITS.subscriptionItems || this.#bytes + bytes > PRODUCT_LIMITS.connectionBytes) { + this.#invalidate(subscription, new Error('RESYNC_REQUIRED')) + return + } + subscription.queue.push({ delta, bytes }) + subscription.items += delta.changes.length + subscription.bytes += bytes + this.#bytes += bytes + void this.#pump(subscription).catch(error => this.#invalidate(subscription, error)) + } + catch (error) { + for (const subscription of [...this.#subscriptions.values()]) + this.#invalidate(subscription, error) + } + } + + async #pump(subscription: Subscription): Promise { + if (!subscription.active || !subscription.ready || subscription.processing) + return + subscription.processing = true + try { + while (subscription.active && subscription.queue.length) { + const { delta, bytes } = subscription.queue[0]! + const classification = classifyProductDelta(subscription.cursor!, delta) + if (classification === 'resync') + throw new Error('RESYNC_REQUIRED') + if (classification === 'apply') { + const cursor = { syncEpoch: delta.syncEpoch, seq: delta.toSeq } + await subscription.consumer.applyChanges(delta.changes.map(change => subscription.consumer.change.parse(change)), cursor) + if (!subscription.active) + return + subscription.cursor = cursor + } + await this.#client.request('sync.ack', { subscriptionId: subscription.id, ...subscription.cursor }) + if (!subscription.active) + return + subscription.queue.shift() + subscription.items -= delta.changes.length + subscription.bytes -= bytes + this.#bytes -= bytes + } + } + finally { subscription.processing = false } + } + + #invalidate(subscription: Subscription, error: unknown): void { + if (!subscription.active) + return + this.#remove(subscription) + subscription.consumer.invalidate(error) + } + + #remove(subscription: Subscription): void { + if (!subscription.active) + return + subscription.active = false + this.#subscriptions.delete(subscription.id) + this.#bytes -= subscription.bytes + subscription.queue = [] + subscription.bytes = 0 + subscription.items = 0 + void this.#client.request('sync.unsubscribe', { subscriptionId: subscription.id }).catch(() => {}) + } +} diff --git a/apps/buddy/electron/main/productProtocol/RendererDraftStore.ts b/apps/buddy/electron/main/productProtocol/RendererDraftStore.ts new file mode 100644 index 000000000..0ac2e2e43 --- /dev/null +++ b/apps/buddy/electron/main/productProtocol/RendererDraftStore.ts @@ -0,0 +1,86 @@ +import type { DraftRecoveryOwner, DraftRecoveryRecord } from '../../../shared/conversation/draftRecovery' +import { Buffer } from 'node:buffer' +import { createHash } from 'node:crypto' +import { readdir } from 'node:fs/promises' +import { join } from 'node:path' +import { isDeepStrictEqual } from 'node:util' +import { z } from 'zod' +import { isMissing, readStorageJson, writeStorageJson } from '../../../platform/storage/storageFiles' +import { draftRecoveryOwnerSchema, draftRecoveryRecordSchema } from '../../../shared/conversation/draftRecovery' + +const entrySchema = z.object({ owner: draftRecoveryOwnerSchema, sequence: z.number().int().nonnegative(), editorSessionId: z.uuid(), record: draftRecoveryRecordSchema.nullable() }).strict() +const maxBytes = 8 * 1024 * 1024 + +export class RendererDraftStore { + readonly #directory: string + #tail: Promise = Promise.resolve() + + constructor(directory: string) { + this.#directory = directory + } + + load(): Promise { + return this.#serial(async () => { + const files = await readdir(this.#directory).catch((error: unknown) => { + if (isMissing(error)) + return [] + throw error + }) + const records: DraftRecoveryRecord[] = [] + for (const name of files) { + if (!/^[a-f0-9]{64}\.json$/.test(name)) + continue + const entry = entrySchema.parse(await readStorageJson(join(this.#directory, name), maxBytes)) + if (name !== this.#name(entry.owner, entry.editorSessionId)) + throw new Error('DRAFT_RECOVERY_CORRUPTED') + if (entry.record) { + if (!isDeepStrictEqual(entry.record.owner, entry.owner) || entry.record.sequence !== entry.sequence || entry.record.snapshot.editorSessionId !== entry.editorSessionId) + throw new Error('DRAFT_RECOVERY_CORRUPTED') + records.push(entry.record) + } + } + if (records.length > 1000) + throw new Error('DRAFT_RECOVERY_LIMIT') + return records + }) + } + + write(raw: DraftRecoveryRecord): Promise { + if (Buffer.byteLength(JSON.stringify(raw)) > maxBytes) + return Promise.reject(new Error('DRAFT_RECOVERY_LIMIT')) + const record = draftRecoveryRecordSchema.parse(raw) + return this.#write({ owner: record.owner, sequence: record.sequence, editorSessionId: record.snapshot.editorSessionId, record }) + } + + remove(owner: DraftRecoveryOwner, editorSessionId: string, sequence: number): Promise { + return this.#write({ owner, editorSessionId, sequence, record: null }) + } + + #write(value: z.infer): Promise { + const entry = entrySchema.parse(value) + return this.#serial(async () => { + const path = join(this.#directory, this.#name(entry.owner, entry.editorSessionId)) + const previous = await readStorageJson(path, maxBytes).then(value => value === null ? null : entrySchema.parse(value)).catch((error: unknown) => { + if (isMissing(error)) + return null + throw error + }) + if (previous && (!isDeepStrictEqual(previous.owner, entry.owner) || previous.editorSessionId !== entry.editorSessionId)) + throw new Error('DRAFT_RECOVERY_CORRUPTED') + if (previous && (previous.sequence > entry.sequence || (previous.sequence === entry.sequence && !isDeepStrictEqual(previous, entry)))) + throw new Error('DRAFT_RECOVERY_STALE_WRITE') + if (!previous || previous.sequence !== entry.sequence) + await writeStorageJson(path, entry) + }) + } + + #name(owner: DraftRecoveryOwner, editorSessionId: string): string { + return `${createHash('sha256').update(JSON.stringify([owner.serviceId, owner.profileId, owner.dataGeneration, owner.deviceId, editorSessionId])).digest('hex')}.json` + } + + #serial(operation: () => Promise): Promise { + const pending = this.#tail.catch(() => {}).then(operation) + this.#tail = pending + return pending + } +} diff --git a/apps/buddy/electron/main/productProtocol/__tests__/DesktopProductCommands.spec.ts b/apps/buddy/electron/main/productProtocol/__tests__/DesktopProductCommands.spec.ts new file mode 100644 index 000000000..d33d57a4b --- /dev/null +++ b/apps/buddy/electron/main/productProtocol/__tests__/DesktopProductCommands.spec.ts @@ -0,0 +1,140 @@ +import type { ProductIdentity } from '../../../../shared/productProtocol/identity' +import type { DesktopProductConnection } from '../DesktopProductCommands' +import { mkdtemp, readdir, readFile, rm, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { DesktopProductCommands } from '../DesktopProductCommands' +import { ProductOutboxStore } from '../ProductOutboxStore' + +const roots: string[] = [] +afterEach(async () => { + await Promise.all(roots.splice(0).map(root => rm(root, { recursive: true, force: true }))) +}) +const identity: ProductIdentity = { serviceId: 'service', profileId: 'profile', dataGeneration: 'data', serviceInstanceId: 'instance', connectionId: 'connection', clientSessionId: 'session', deviceId: 'desktop-owner' } +const command = { method: 'chat.startTurn' as const, commandId: 'command', intent: { draftId: 'draft', expectedRevision: '2' } } +const receipt = { dataGeneration: 'data', commandId: 'command', method: 'chat.startTurn', decision: 'accepted' as const, decidedAt: '2026-11-07T00:00:00.000Z', target: { runId: 'run', draftId: 'draft' }, result: { draftReceipt: { draftId: 'draft', committedRevision: '3' } } } +async function fixture() { + const root = await mkdtemp(join(tmpdir(), 'lexora-product-outbox-')) + roots.push(root) + const store = new ProductOutboxStore(join(root, 'outbox')) + return { root, store, client: new DesktopProductCommands(store) } +} + +function connection(request: DesktopProductConnection['request'], changes: Partial = {}): DesktopProductConnection { + return { identity: { ...identity, ...changes }, request } +} + +describe('desktop durable command delivery', () => { + it('persists before dispatch, reconciles a lost response after restart and retains deduplication after archival', async () => { + const { root, store, client } = await fixture() + let dispatched = 0 + client.attach(connection(async () => { + dispatched += 1 + const [persisted] = await new ProductOutboxStore(join(root, 'outbox')).list() + expect(persisted).toMatchObject({ state: 'uncertain', envelope: { commandId: 'command', intent: command.intent } }) + throw new Error('transport lost after commit') + })) + const first = await client.execute(command) + expect(first.state).toBe('unknown') + expect(dispatched).toBe(1) + const restarted = new DesktopProductCommands(new ProductOutboxStore(store.directory)) + const calls: string[] = [] + restarted.attach(connection(async (method) => { + calls.push(method) + return { lookup: 'decided', receipt } + }, { serviceInstanceId: 'instance-2' })) + const [recovered] = await restarted.recover() + expect(recovered).toMatchObject({ state: 'decided', record: { receipt } }) + expect(calls).toEqual(['commands.get']) + const archived = await store.archive(recovered!.record) + expect(archived.envelope).toBeNull() + expect(archived.fingerprint).toMatch(/^[a-f0-9]{64}$/) + expect((await restarted.execute(command)).state).toBe('decided') + expect(calls).toEqual(['commands.get']) + await expect(restarted.execute({ ...command, intent: { ...command.intent, expectedRevision: '3' } })).rejects.toThrow('OUTBOX_COMMAND_ID_REUSED') + }) + + it('does not send when durable preparation fails', async () => { + const { root } = await fixture() + const directory = join(root, 'not-a-directory') + await writeFile(directory, 'keep') + const client = new DesktopProductCommands(new ProductOutboxStore(directory)) + let dispatched = 0 + client.attach(connection(async () => { + dispatched += 1 + })) + await expect(client.execute(command)).rejects.toThrow() + expect(dispatched).toBe(0) + expect(await readFile(directory, 'utf8')).toBe('keep') + }) + + it('returns absent commands as recoverable input and never changes identity or resends automatically', async () => { + const { store, client } = await fixture() + const record = await store.create({ identity, method: command.method, envelope: { dataGeneration: identity.dataGeneration, commandId: command.commandId, expectedServiceInstanceId: identity.serviceInstanceId, intent: command.intent } }) + const calls: string[] = [] + client.attach(connection(async (method) => { + calls.push(method) + return { lookup: 'not_found' } + })) + expect((await client.reconcile(record.record)).state).toBe('not_accepted') + expect((await store.list())[0]!.envelope?.intent).toEqual(command.intent) + client.attach(connection(async (method) => { + calls.push(method) + }, { dataGeneration: 'restored-data' })) + expect((await client.recover())[0]!.state).toBe('trust_changed') + expect(calls).toEqual(['commands.get']) + expect((await store.list())[0]!.owner.dataGeneration).toBe('data') + }) + + it('keeps unknown results and corrupted records without silently reconstructing an empty outbox', async () => { + const { store, client } = await fixture() + client.attach(connection(async () => ({ receipt: { ...receipt, commandId: 'another-command' } }))) + expect((await client.execute(command)).state).toBe('unknown') + client.attach(connection(async () => { + throw new Error('storage unavailable') + })) + expect((await client.recover())[0]!.state).toBe('unknown') + const [name] = await readdir(store.directory) + await writeFile(join(store.directory, name!), '{broken') + await expect(client.recover()).rejects.toThrow() + expect(await readFile(join(store.directory, name!), 'utf8')).toBe('{broken') + }) + + it('archives the original receipt but fences a late response from the replaced window connection', async () => { + const { client, store } = await fixture() + let respond!: (result: unknown) => void + let sent!: () => void + const dispatched = new Promise((resolve) => { + sent = resolve + }) + const detach = client.attach(connection(async () => { + sent() + return new Promise((resolve) => { + respond = resolve + }) + })) + const sending = client.execute(command) + await dispatched + client.attach(connection(async () => ({ lookup: 'decided', receipt }), { connectionId: 'new-window', clientSessionId: 'new-session' })) + detach() + respond({ receipt }) + expect((await sending).state).toBe('connection_replaced') + expect((await store.list())[0]!.receipt).toEqual(receipt) + expect((await client.recover())[0]!.state).toBe('decided') + }) + + it('serializes concurrent retries of one identity without returning an early not-found result', async () => { + const { client } = await fixture() + let dispatched = 0 + client.attach(connection(async (method) => { + if (method === 'commands.get') + return { lookup: 'not_found' } + dispatched += 1 + return { receipt } + })) + const results = await Promise.all([client.execute(command), client.execute(command)]) + expect(results.map(result => result.state)).toEqual(['decided', 'decided']) + expect(dispatched).toBe(1) + }) +}) diff --git a/apps/buddy/electron/main/productProtocol/__tests__/ProductRpcClient.spec.ts b/apps/buddy/electron/main/productProtocol/__tests__/ProductRpcClient.spec.ts new file mode 100644 index 000000000..54deb5f11 --- /dev/null +++ b/apps/buddy/electron/main/productProtocol/__tests__/ProductRpcClient.spec.ts @@ -0,0 +1,61 @@ +import type { ProductClientTransport } from '../ProductRpcClient' +import { describe, expect, it } from 'vitest' +import { ProductConnection } from '../../../../service/src/productProtocol/ProductConnection' +import { ProductRpcClient } from '../ProductRpcClient' + +const initialize = { protocol: { major: 1, minMinor: 0, maxMinor: 0 }, client: { kind: 'desktop' as const, version: '0.20.0' }, supportedContentKinds: ['buddy.user.v1'], supportedReviewKinds: [] } +const context = { serviceId: 'service', profileId: 'profile', dataGeneration: 'generation', serviceInstanceId: 'instance', connectionId: 'connection', clientSessionId: 'session', deviceId: 'desktop', ownerPrincipalId: 'desktop-local', credentialId: 'local', grantGeneration: '1', role: 'owner' as const } + +function fixture() { + let receive: (frame: string) => void = () => {} + let disconnected = false + const frames: string[] = [] + const server = new ProductConnection({ context, assertCurrent() {}, phase: () => 'ready', handlers: { 'commands.get': { handle: () => ({ lookup: 'not_found' }) } }, send(frame) { + receive(frame) + return true + }, close() {} }) + const transport: ProductClientTransport = { + send(frame) { + frames.push(frame) + return server.receive(frame) + }, + subscribe(listener) { + receive = listener + return () => { + receive = () => {} + } + }, + close() { + disconnected = true + }, + } + const client = new ProductRpcClient(transport) + return { client, server, frames, emit: (frame: string) => receive(frame), disconnected: () => disconnected } +} + +describe('product JSON transport client', () => { + it('negotiates with the real server connection and preserves the restricted error and query contracts', async () => { + const { client, server, frames } = fixture() + try { + await expect(client.request('commands.get', { dataGeneration: 'generation', commandId: 'command' })).rejects.toThrow('PRODUCT_NOT_INITIALIZED') + const result = await client.initialize(initialize) + expect(result).toMatchObject({ dataGeneration: 'generation', profileId: 'profile', capabilities: ['commands.get'] }) + expect(await client.request('commands.get', { dataGeneration: 'generation', commandId: 'command' })).toEqual({ lookup: 'not_found' }) + await expect(client.request('chat.cancel', { dataGeneration: 'generation', commandId: 'cancel', expectedServiceInstanceId: 'instance', intent: { runId: 'run', expectedRunGeneration: '1' } })).rejects.toMatchObject({ code: 'METHOD_NOT_AVAILABLE', acceptance: 'not_accepted' }) + expect(JSON.parse(frames[2]!).params.intent.expectedRunGeneration).toBe('1') + } + finally { + client.close() + server.close() + } + }) + + it('closes only its transport on malformed frames and does not accept secret-bearing error extensions', async () => { + const { client, server, emit, disconnected } = fixture() + await client.initialize(initialize) + emit(JSON.stringify({ jsonrpc: '2.0', id: null, error: { code: 'INTERNAL_ERROR', message: 'INTERNAL_ERROR', acceptance: 'unknown', recovery: 'query_command', path: '/private/profile' } })) + expect(disconnected()).toBe(true) + await expect(client.request('commands.get', { dataGeneration: 'generation', commandId: 'command' })).rejects.toThrow('PRODUCT_CONNECTION_CLOSED') + server.close() + }) +}) diff --git a/apps/buddy/electron/main/productProtocol/__tests__/ProductSyncClient.spec.ts b/apps/buddy/electron/main/productProtocol/__tests__/ProductSyncClient.spec.ts new file mode 100644 index 000000000..d434a1920 --- /dev/null +++ b/apps/buddy/electron/main/productProtocol/__tests__/ProductSyncClient.spec.ts @@ -0,0 +1,98 @@ +import { describe, expect, it } from 'vitest' +import { z } from 'zod' +import { ProductConnection } from '../../../../service/src/productProtocol/ProductConnection' +import { ProductSyncPublisher } from '../../../../service/src/productProtocol/ProductSyncPublisher' +import { ProductRpcClient } from '../ProductRpcClient' +import { ProductSyncClient } from '../ProductSyncClient' + +const cursor = { syncEpoch: 'epoch', seq: '0' } + +it('commits every snapshot page before applying the following delta and acknowledging it', async () => { + const publisher = new ProductSyncPublisher() + let reads = 0 + let closed = false + const schema = z.number() + publisher.register('index', { item: schema, change: schema, readSnapshot: () => ({ items: [], complete: true, nextCursor: null }), openSnapshot: () => ({ read: () => ++reads === 1 ? { items: [1], complete: false, nextCursor: 'more' } : { items: [2], complete: true, nextCursor: null }, close: () => { + closed = true + } }) }) + let receive = (_frame: string) => {} + const context = { serviceId: 'service', profileId: 'profile', dataGeneration: 'data', serviceInstanceId: 'instance', connectionId: 'connection', clientSessionId: 'session', deviceId: 'desktop', ownerPrincipalId: 'desktop-local', credentialId: 'local', grantGeneration: '1', role: 'owner' as const } + const connection = new ProductConnection({ context, assertCurrent() {}, phase: () => 'ready', handlers: {}, sync: publisher, authorizeTopic() {}, send(frame) { + receive(frame) + return true + }, close() {} }) + const client = new ProductRpcClient({ send: frame => connection.receive(frame), subscribe(listener) { + receive = listener + return () => {} + }, close() {} }) + const sync = new ProductSyncClient(client) + const events: string[] = [] + try { + await client.initialize({ protocol: { major: 1, minMinor: 0, maxMinor: 0 }, client: { kind: 'desktop', version: 'test' }, supportedContentKinds: [], supportedReviewKinds: [] }) + const handle = sync.subscribe('index', { + item: schema, + change: schema, + beginSnapshot: () => { events.push('begin') }, + appendSnapshot(items) { + events.push(`page:${items.join(',')}`) + if (items[0] === 1) + publisher.publishCommitted('index', () => [3]) + }, + commitSnapshot: () => { events.push('commit') }, + applyChanges: (changes) => { events.push(`delta:${changes.join(',')}`) }, + invalidate: () => { events.push('invalid') }, + }) + await handle.ready + expect(events).toEqual(['begin', 'page:1', 'page:2', 'commit', 'delta:3']) + expect(closed).toBe(true) + expect(publisher.inspect().connections[0]?.bytes).toBe(0) + } + finally { + sync.close() + client.close() + connection.close() + } +}) + +describe('ordered client synchronization', () => { + it('ignores duplicates, rejects gaps and does not consume a removed subscription', async () => { + let listener = (_notification: { method: string, params: unknown }) => {} + let id = '' + const acknowledgements: unknown[] = [] + const applied: unknown[] = [] + const invalidations: unknown[] = [] + const sync = new ProductSyncClient({ + onNotification(value) { + listener = value + return () => {} + }, + async request(method, params) { + if (method === 'sync.subscribe') { + id = (params as { subscriptionId: string }).subscriptionId + return { subscriptionId: id, mode: 'snapshot', cursor, items: [], complete: true, nextCursor: null } + } + if (method === 'sync.ack') + acknowledgements.push(params) + return {} + }, + }) + const handle = sync.subscribe('index', { item: z.number(), change: z.number(), beginSnapshot() {}, appendSnapshot() {}, commitSnapshot() {}, applyChanges: (changes) => { + applied.push(changes) + }, invalidate: (error) => { + invalidations.push(error) + } }) + await handle.ready + const emit = (fromSeq: string, toSeq: string) => listener({ method: 'sync.delta', params: { subscriptionId: id, syncEpoch: 'epoch', fromSeq, toSeq, changes: [1] } }) + emit('0', '1') + await expect.poll(() => acknowledgements.length).toBe(1) + emit('0', '1') + await expect.poll(() => acknowledgements.length).toBe(2) + expect(applied).toEqual([[1]]) + emit('2', '3') + await expect.poll(() => invalidations.length).toBe(1) + emit('1', '2') + expect(applied).toEqual([[1]]) + expect(acknowledgements).toHaveLength(2) + sync.close() + }) +}) diff --git a/apps/buddy/electron/main/productProtocol/__tests__/RendererDraftStore.spec.ts b/apps/buddy/electron/main/productProtocol/__tests__/RendererDraftStore.spec.ts new file mode 100644 index 000000000..54e405d6d --- /dev/null +++ b/apps/buddy/electron/main/productProtocol/__tests__/RendererDraftStore.spec.ts @@ -0,0 +1,77 @@ +import type { DraftRecoveryRecord } from '../../../../shared/conversation/draftRecovery' +import { randomUUID } from 'node:crypto' +import { mkdtemp, readdir, readFile, rm, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { createBuddyUserContent } from '../../../../shared/conversation/buddyUserContent' +import { RendererDraftStore } from '../RendererDraftStore' + +const roots: string[] = [] +afterEach(async () => { + await Promise.all(roots.splice(0).map(root => rm(root, { recursive: true, force: true }))) +}) +async function directory() { + const root = await mkdtemp(join(tmpdir(), 'renderer-drafts-')) + roots.push(root) + return root +} +function record(): DraftRecoveryRecord { + return { + owner: { serviceId: 'service', profileId: 'profile', dataGeneration: 'generation', deviceId: 'desktop' }, + sequence: 1, + snapshot: { + targetKey: 'global', + draftId: randomUUID(), + editorSessionId: randomUUID(), + editVersion: 1, + revision: 2, + confirmedSnapshot: null, + content: createBuddyUserContent('unsent input'), + approvalPolicy: 'policy', + executionProfile: 'workspace_write', + modelSelection: null, + }, + } +} + +describe('rendererDraftStore', () => { + it('durably restores independent scopes and generations in a fresh store', async () => { + const root = await directory() + const store = new RendererDraftStore(root) + const first = record() + const second = record() + second.snapshot.targetKey = 'space:space-1' + second.owner.dataGeneration = 'previous-generation' + await Promise.all([store.write(first), store.write(second)]) + expect(await new RendererDraftStore(root).load()).toEqual(expect.arrayContaining([first, second])) + }) + + it('rejects stale writes and preserves the deletion watermark across restarts', async () => { + const root = await directory() + const store = new RendererDraftStore(root) + const first = record() + const newer = { ...first, sequence: 2, snapshot: { ...first.snapshot, editVersion: 2, content: createBuddyUserContent('newer input') } } + await store.write(newer) + await expect(store.write(first)).rejects.toThrow('DRAFT_RECOVERY_STALE_WRITE') + await store.write(newer) + await store.remove(first.owner, first.snapshot.editorSessionId, 3) + const restarted = new RendererDraftStore(root) + await expect(restarted.write(newer)).rejects.toThrow('DRAFT_RECOVERY_STALE_WRITE') + expect(await restarted.load()).toEqual([]) + }) + + it('leaves malformed or future content intact instead of overwriting the recovery copy', async () => { + const root = await directory() + const store = new RendererDraftStore(root) + const first = record() + await store.write(first) + const path = join(root, (await readdir(root))[0]!) + const original = await readFile(path, 'utf8') + const future = original.replace('"version":1', '"version":2') + await writeFile(path, future) + await expect(store.load()).rejects.toThrow() + await expect(store.write({ ...first, sequence: 2 })).rejects.toThrow() + expect(await readFile(path, 'utf8')).toBe(future) + }) +}) diff --git a/apps/buddy/electron/main/productProtocol/registerDraftRecoveryIpc.ts b/apps/buddy/electron/main/productProtocol/registerDraftRecoveryIpc.ts new file mode 100644 index 000000000..c71629779 --- /dev/null +++ b/apps/buddy/electron/main/productProtocol/registerDraftRecoveryIpc.ts @@ -0,0 +1,41 @@ +import type { BrowserWindow } from 'electron' +import type { DraftRecoveryOwner } from '../../../shared/conversation/draftRecovery' +import type { ProductIdentity } from '../../../shared/productProtocol/identity' +import { ipcMain } from 'electron' +import { draftRecoveryOwnerSchema, draftRecoveryRecordSchema, draftRecoveryRemovalSchema, sameDraftRecoveryOwner } from '../../../shared/conversation/draftRecovery' +import { LOCAL_CHAT_IPC_CHANNELS } from '../../shared/localChatApi' +import { assertTrustedSender } from '../ipc' +import { RendererDraftStore } from './RendererDraftStore' + +export function registerDraftRecoveryIpc(options: { directory: string, getWindow: () => BrowserWindow | null, identity: () => Promise }): () => void { + const store = new RendererDraftStore(options.directory) + const channels = [LOCAL_CHAT_IPC_CHANNELS.draftRecoveryLoad, LOCAL_CHAT_IPC_CHANNELS.draftRecoveryWrite, LOCAL_CHAT_IPC_CHANNELS.draftRecoveryRemove] + let activeOwner: DraftRecoveryOwner | null = null + const owner = async () => { + const identity = await options.identity() + activeOwner = identity ? draftRecoveryOwnerSchema.parse({ serviceId: identity.serviceId, profileId: identity.profileId, dataGeneration: identity.dataGeneration, deviceId: identity.deviceId }) : null + return activeOwner + } + ipcMain.handle(channels[0]!, async (event) => { + assertTrustedSender(event, options.getWindow()) + const current = await owner() + return { owner: current, records: current ? await store.load() : [] } + }) + ipcMain.handle(channels[1]!, async (event, input: unknown) => { + assertTrustedSender(event, options.getWindow()) + const record = draftRecoveryRecordSchema.parse(input) + const current = activeOwner + if (!current || !sameDraftRecoveryOwner(current, record.owner)) + throw new Error('DRAFT_RECOVERY_IDENTITY_CHANGED') + await store.write(record) + }) + ipcMain.handle(channels[2]!, async (event, input: unknown) => { + assertTrustedSender(event, options.getWindow()) + const request = draftRecoveryRemovalSchema.parse(input) + const current = activeOwner + if (!current || !sameDraftRecoveryOwner(current, request.owner)) + throw new Error('DRAFT_RECOVERY_IDENTITY_CHANGED') + await store.remove(request.owner, request.editorSessionId, request.sequence) + }) + return () => channels.forEach(channel => ipcMain.removeHandler(channel)) +} diff --git a/apps/buddy/electron/main/runtime/RemoteServiceClient.ts b/apps/buddy/electron/main/runtime/RemoteServiceClient.ts new file mode 100644 index 000000000..526ade3d1 --- /dev/null +++ b/apps/buddy/electron/main/runtime/RemoteServiceClient.ts @@ -0,0 +1,193 @@ +import type { RuntimeLifecycleChange, RuntimeLifecycleSnapshot } from '../../../shared/lifecycle/runtimeLifecycle' +import type { RuntimeRpcPeerContract } from '../../../shared/runtime/rpcPeer' +import type { ServiceHostBootstrap } from '../../../shared/service/localServiceProtocol' +import type { BuddyServiceNotification, BuddyServiceRequestOptions, BuddyServiceSupervisorState } from './BuddyServiceSupervisor' +import { randomUUID } from 'node:crypto' +import { connectLocalService } from '../../../platform/service/connectLocalService' +import { ServiceRpcChannel } from '../../../platform/service/ServiceRpcChannel' +import { Emitter } from '../../../shared/events/Emitter' +import { SERVICE_LIFECYCLE_METHOD, serviceLifecycleChangeSchema } from '../../../shared/lifecycle/serviceLifecycle' +import { serviceMaintenanceTicketSchema, serviceRuntimeStateSchema } from '../../../shared/service/localServiceProtocol' + +export class RemoteServiceClient { + readonly #bootstrap: ServiceHostBootstrap + readonly #host: RuntimeRpcPeerContract + readonly #changes = new Emitter(() => {}) + readonly #lifecycle = new Emitter(() => {}) + readonly #notifications = new Emitter(() => {}) + readonly onDidChangeLifecycle = this.#lifecycle.event + #peer: ServiceRpcChannel | undefined + #connecting: Promise | undefined + #state: BuddyServiceSupervisorState = { status: 'stopped', pid: null, restartAttempt: 0, lastError: null } + #snapshot: RuntimeLifecycleSnapshot + #closed = false + #transitioning = false + + constructor(bootstrap: ServiceHostBootstrap, host: RuntimeRpcPeerContract) { + this.#bootstrap = bootstrap + this.#host = host + this.#snapshot = { revision: 0, generation: bootstrap.identity.serviceInstanceId, status: 'stopped', maintenance: null, errorCode: null, connection: null, services: null } + host.onNotification((method, params) => { + if (method === 'service.runtimeState') { + const state = serviceRuntimeStateSchema.parse(params) + this.#snapshot = { ...this.#snapshot, ...state, services: state.generation === this.#snapshot.generation ? this.#snapshot.services : null } + this.#setState(state.status) + if (state.status === 'ready' && !this.#closed) + void this.#connect().catch(() => {}) + } + if (method === SERVICE_LIFECYCLE_METHOD) { + const change = serviceLifecycleChangeSchema.safeParse(params) + if (change.success) { + this.#snapshot = { ...this.#snapshot, revision: this.#snapshot.revision + 1, services: change.data.snapshot } + this.#lifecycle.fire({ snapshot: this.lifecycleState }) + } + } + this.#notifications.fire({ method, params }) + }) + } + + get state() { + return { ...this.#state } + } + + get lifecycleState(): RuntimeLifecycleSnapshot { + return structuredClone(this.#snapshot) + } + + onStateChange(listener: (state: BuddyServiceSupervisorState) => void) { + return this.#changes.event(listener).dispose + } + + onNotification(listener: (notification: BuddyServiceNotification) => void) { + return this.#notifications.event(listener).dispose + } + + start(): void { + void this.#connect().catch(() => {}) + } + + async request(method: string, params: unknown, options: BuddyServiceRequestOptions = {}): Promise { + const peer = method === 'extensions.inference.acquire' || method === 'extensions.inference.release' ? this.#host : await this.#connect() + return peer.request(method, params, options.timeoutMs, options.signal, options.requestId) + } + + notify(method: string, params: unknown): boolean { + if (this.#closed) + return false + try { + this.#host.notify(method, params) + + return true + } + catch { + return false + } + } + + async restartDesktop(): Promise { + if (this.#closed || this.#transitioning) + throw new Error('SERVICE_NOT_READY') + await this.#host.request('service.restartDesktop', { expectedServiceInstanceId: this.#bootstrap.identity.serviceInstanceId }, 30000) + } + + restart(): Promise { + return this.#withMaintenance('restart', async () => {}) + } + + withStoppedStorage(operation: () => Promise): Promise { + return this.#withMaintenance('backup', operation) + } + + async #withMaintenance(kind: 'backup' | 'restart', operation: () => Promise): Promise { + if (this.#closed || this.#transitioning || this.#state.status !== 'ready') + throw new Error('SERVICE_NOT_READY') + this.#transitioning = true + try { + const ticket = serviceMaintenanceTicketSchema.parse(await this.#host.request('service.beginMaintenance', { expectedServiceInstanceId: this.#bootstrap.identity.serviceInstanceId, kind }, 30000)) + try { + return await operation() + } + finally { + await this.#host.request('service.endMaintenance', ticket, 120000) + await this.#connect() + } + } + finally { + this.#transitioning = false + } + } + + async stop(): Promise { + if (this.#closed) + return + let stop = () => {} + let timeout: ReturnType | undefined + const finished = new Promise((resolve, reject) => { + timeout = setTimeout(() => reject(new Error('SERVICE_STOP_UNCONFIRMED')), 45000) + stop = this.#host.onNotification((method) => { + if (method === 'service.executionStopped') + resolve() + }) + }) + void finished.catch(() => {}) + try { + await this.#host.request('service.exitDesktop', { expectedServiceInstanceId: this.#bootstrap.identity.serviceInstanceId, commandId: randomUUID(), reason: 'explicit' }, 30000) + await finished + } + finally { + clearTimeout(timeout) + stop() + } + this.disconnect() + } + + disconnect(): void { + if (this.#closed) + return + this.#closed = true + this.#peer?.close(new Error('DESKTOP_CLIENT_CLOSED')) + this.#setState('stopped') + } + + #connect(): Promise { + if (this.#closed) + return Promise.reject(new Error('SERVICE_CLIENT_STOPPED')) + if (this.#snapshot.maintenance || this.#state.status === 'stopping') + return Promise.reject(new Error('SERVICE_MAINTENANCE')) + return this.#connecting ??= (async () => { + this.#setState('starting') + const { frames } = await connectLocalService(this.#bootstrap.managementEndpoint, { ...this.#bootstrap.identity, type: 'hello', role: 'management', secret: this.#bootstrap.managementSecret }) + const peer = new ServiceRpcChannel(frames) + this.#peer = peer + peer.onDidClose(() => { + if (this.#peer !== peer) + return + this.#peer = undefined + this.#connecting = undefined + if (!this.#transitioning && !this.#snapshot.maintenance) + this.#setState(this.#closed ? 'stopped' : 'offline') + }) + peer.onNotification((method, params) => this.#notifications.fire({ method, params })) + const attached = await peer.request('service.attachDesktop', { expectedServiceInstanceId: this.#bootstrap.identity.serviceInstanceId }, 120000) as { pid: number, generation: string } + if (peer.closed || this.#closed) + throw new Error('SERVICE_CONNECTION_CLOSED') + this.#snapshot = { ...this.#snapshot, generation: attached.generation } + this.#state.pid = attached.pid + this.#setState('ready') + return peer + })().catch((error) => { + this.#connecting = undefined + if (!this.#transitioning && !this.#snapshot.maintenance) + this.#setState('offline') + throw error + }) + } + + #setState(status: BuddyServiceSupervisorState['status']): void { + this.#state = { ...this.#state, status, lastError: status === 'offline' ? 'RUNTIME_STOPPED' : null } + const connection = { component: 'runtime.connection', kind: 'service' as const, status: status === 'ready' ? 'ready' as const : status === 'starting' ? 'starting' as const : status === 'offline' ? 'start_failed' as const : 'stopped' as const, operationId: this.#bootstrap.identity.serviceInstanceId } + this.#snapshot = { ...this.#snapshot, revision: this.#snapshot.revision + 1, status, errorCode: this.#state.lastError, connection } + this.#changes.fire(this.state) + this.#lifecycle.fire({ snapshot: this.lifecycleState, connection }) + } +} diff --git a/apps/buddy/electron/main/runtime/buddyServiceEnvironment.ts b/apps/buddy/electron/main/runtime/buddyServiceEnvironment.ts index 864b44429..c8f1b9d8b 100644 --- a/apps/buddy/electron/main/runtime/buddyServiceEnvironment.ts +++ b/apps/buddy/electron/main/runtime/buddyServiceEnvironment.ts @@ -1,64 +1 @@ -import type { StorageProtection } from '../../../platform/storage/ProfileLocations' -import { userInfo } from 'node:os' -import process from 'node:process' -import { filePathAdapters } from '../../../platform/filesystem/filePaths' -import searchTools from '../../../platform/native/searchTools.json' -import { createChildProcessEnvironment } from '../../../platform/process/childProcessEnvironment' -import { createProxyEnvironment } from '../../../platform/process/proxyEnvironment' -import { createToolchainEnvironment } from '../../../platform/process/toolchainEnvironment' -import { resolveBuddyTarget } from '../../../platform/target' -import { resolveBuddyPlatform } from '../../../shared/platform' -import { isWindows } from '../../../shared/platform/identifiers' - -export function resolveBuddySearchToolsDirectory(options: { - appPath: string - isPackaged: boolean - resourcesPath: string - platform?: NodeJS.Platform - architecture?: string -}): string { - const platform = resolveBuddyPlatform(options.platform ?? process.platform) - const { id: target } = resolveBuddyTarget(platform.id, options.architecture ?? process.arch) - if (!Object.values(searchTools.tools).every(tool => Object.hasOwn(tool.targets, target))) - throw new Error(`Unsupported search tools target: ${target}`) - return filePathAdapters[platform.id].resolveInput( - options.isPackaged ? searchTools.resource.to : `${searchTools.resource.from}/${target}`, - options.isPackaged ? options.resourcesPath : options.appPath, - ) -} - -export function createBuddyServiceEnvironment( - source: NodeJS.ProcessEnv, - buddyHome: string, - platform: NodeJS.Platform = process.platform, - proxyUrl?: string, - storageProtection?: StorageProtection, -): NodeJS.ProcessEnv { - const targetPlatform = resolveBuddyPlatform(platform) - const environmentSource = !isWindows(targetPlatform.id) && !source.HOME - ? { ...source, HOME: userInfo().homedir } - : source - const recoveryMode = Object.entries(environmentSource).find(([name]) => ( - (targetPlatform.environment.caseSensitive ? name : name.toUpperCase()) === 'LEXORA_BUDDY_EVENT_RECOVERY' - ))?.[1] - const environment = createChildProcessEnvironment({ - source: environmentSource, - platform: targetPlatform, - additions: { - ...createToolchainEnvironment(environmentSource, targetPlatform.id), - LEXORA_BUDDY_HOME: buddyHome, - LEXORA_BUDDY_STORAGE_PROTECTION: storageProtection ? JSON.stringify(storageProtection.sensitiveRoots) : undefined, - LEXORA_BUDDY_EVENT_RECOVERY: recoveryMode === 'full' ? 'full' : undefined, - NODE_USE_ENV_PROXY: '1', - PI_CODING_AGENT_DIR: filePathAdapters[targetPlatform.id].resolveInput('agent', buddyHome), - }, - }) - if (proxyUrl) { - for (const key of Object.keys(environment)) { - if (/^(?:https?|all|no)_proxy$/i.test(key)) - delete environment[key] - } - Object.assign(environment, createProxyEnvironment(proxyUrl)) - } - return environment -} +export { createBuddyServiceEnvironment, resolveBuddySearchToolsDirectory } from '../../../platform/process/serviceEnvironment' diff --git a/apps/buddy/electron/main/runtime/platformHostBootstrap.ts b/apps/buddy/electron/main/runtime/platformHostBootstrap.ts new file mode 100644 index 000000000..f60df50fb --- /dev/null +++ b/apps/buddy/electron/main/runtime/platformHostBootstrap.ts @@ -0,0 +1,26 @@ +import { createReadStream } from 'node:fs' +import { connectLocalService } from '../../../platform/service/connectLocalService' +import { ServiceRpcChannel } from '../../../platform/service/ServiceRpcChannel' +import { serviceHostBootstrapSchema } from '../../../shared/service/localServiceProtocol' + +export async function connectPlatformHost() { + const input = createReadStream('', { fd: 3, autoClose: true, encoding: 'utf8' }) + const timeout = setTimeout(() => input.destroy(new Error('PLATFORM_HOST_BOOTSTRAP_TIMEOUT')), 10000) + let data = '' + try { + for await (const chunk of input) { + data += chunk + if (data.length > 65536) + throw new Error('PLATFORM_HOST_BOOTSTRAP_INVALID') + } + } + finally { + clearTimeout(timeout) + + input.destroy() + } + const bootstrap = serviceHostBootstrapSchema.parse(JSON.parse(data)) + const connected = await connectLocalService(bootstrap.endpoint, { ...bootstrap.identity, type: 'hello', role: 'host', secret: bootstrap.secret }) + return { bootstrap, peer: new ServiceRpcChannel(connected.frames) } +} +export type PlatformHostConnection = Awaited> diff --git a/apps/buddy/electron/main/runtime/serviceProcess.ts b/apps/buddy/electron/main/runtime/serviceProcess.ts new file mode 100644 index 000000000..a725099fe --- /dev/null +++ b/apps/buddy/electron/main/runtime/serviceProcess.ts @@ -0,0 +1,81 @@ +import { isAbsolute } from 'node:path' +import process from 'node:process' +import { z } from 'zod' +import { establishWindowsRuntimeGuard } from '../../../platform/windows/runtimeGuard' +import { readRuntimeProxy } from '../../../service/src/network/runtimeNetwork' +import { createRpcRuntimePlatform } from '../../../service/src/platform/createRpcRuntimePlatform' +import { prepareRuntimeEnvironment } from '../../../service/src/platform/prepareRuntimeEnvironment' +import { createBuddyService, notifyBuddyServiceFailure, notifyBuddyServiceReady } from '../../../service/src/rpc/BuddyServiceRpcServer' +import { readBuddyServiceFailureCode, startServiceRuntime } from '../../../service/src/startServiceRuntime' +import { ApplicationEvents } from '../../../shared/observability/ApplicationEvents' + +const port = process.parentPort +if (!port) { + process.stderr.write('Buddy Local Service requires an Electron utility process parent\n') + process.exitCode = 1 +} +else { + process.once('SIGINT', () => {}) + const events = new ApplicationEvents() + let service: Awaited> | undefined + const peer = createBuddyService({ recordDiagnostic: events.publish, port, announceReady: false, prepareIdleShutdown: () => service?.prepareIdleShutdown() ?? false, scheduleShutdown: () => { + void shutdown(0) + }, onFatalError: () => { + void shutdown(1) + } }) + let stopping: Promise | undefined + const started = start() + void started.catch((error: unknown) => { + notifyBuddyServiceFailure(peer, readBuddyServiceFailureCode(error)) + process.stderr.write(`Buddy Local Service failed to start: ${readBuddyServiceFailureCode(error)}\n`) + void shutdown(1) + }) + + async function start() { + const buddyHome = z.string().refine(isAbsolute).parse(process.env.LEXORA_BUDDY_HOME) + const builtinSkillsDirectories = z.array(z.string().refine(isAbsolute)).parse(JSON.parse(process.env.LEXORA_BUDDY_SKILLS_DIRS ?? '[]')) + if (process.platform === 'win32') + await establishWindowsRuntimeGuard() + await prepareRuntimeEnvironment() + const runtime = await startServiceRuntime({ + events, + buddyHome, + builtinSkillsDirectories, + proxy: readRuntimeProxy(process.env), + forceEventRecovery: process.env.LEXORA_BUDDY_EVENT_RECOVERY === 'full', + storageAuthority: z.enum(['legacy_jsonl', 'sqlite']).optional().parse(process.env.LEXORA_BUDDY_STORAGE_AUTHORITY), + platform: createRpcRuntimePlatform(peer), + notify: (method, params) => peer.notify(method, params), + onFatalFailure(error) { + notifyBuddyServiceFailure(peer, readBuddyServiceFailureCode(error)) + void shutdown(1) + }, + }) + try { + service = runtime + runtime.connectClient('product', peer) + runtime.connectClient('management', peer) + notifyBuddyServiceReady(peer) + runtime.startMaintenance() + return runtime + } + catch (error) { + await runtime.dispose() + throw error + } + } + + function shutdown(code: number): Promise { + return stopping ??= (async () => { + const runtime = await started.catch(() => null) + try { + await runtime?.dispose() + } + catch { + code = 1 + } + peer.close(new Error('Buddy Local Service is shutting down')) + process.exit(code) + })() + } +} diff --git a/apps/buddy/electron/main/sandbox/registerSandboxHostRpc.ts b/apps/buddy/electron/main/sandbox/registerSandboxHostRpc.ts index 37d0aeb58..87d01b7b0 100644 --- a/apps/buddy/electron/main/sandbox/registerSandboxHostRpc.ts +++ b/apps/buddy/electron/main/sandbox/registerSandboxHostRpc.ts @@ -11,12 +11,12 @@ import { createSandboxEnvironment } from '../../../platform/process/sandboxEnvir import { resolveWindowsSandbox } from '../../../platform/process/windowsSandbox' import { resolveSensitivePathRoots } from '../../../service/src/permissions/sensitivePaths' import { runSandboxCommand } from '../../../service/src/sandbox/runSandboxCommand' -import sandboxProcessPath from '../../../service/src/sandbox/sandboxProcess?modulePath' import { Emitter } from '../../../shared/events/Emitter' import { sandboxLifecycleNotificationSchema } from '../../../shared/permissions/sandboxLifecycle' import { SANDBOX_RPC_TIMEOUT_MS, sandboxCancelSchema, sandboxCommandSchema, sandboxNetworkRequestSchema, sandboxOutputSchema, sandboxPreparationSchema, sandboxProcessInputSchema, sandboxSettlementAckSchema, sandboxSettlementSchema } from '../../../shared/permissions/shellSandbox' import { isLinux, isWindows, OPERATING_SYSTEM, SHELL_SANDBOX_BACKEND } from '../../../shared/platform/identifiers' import { BuddyServicePeer } from '../runtime/BuddyServicePeer' +import sandboxProcessPath from './sandboxProcess?modulePath' import { WindowsSandboxPreparation } from './WindowsSandboxPreparation' export interface SandboxHostOptions { diff --git a/apps/buddy/service/src/sandbox/sandboxProcess.ts b/apps/buddy/electron/main/sandbox/sandboxProcess.ts similarity index 90% rename from apps/buddy/service/src/sandbox/sandboxProcess.ts rename to apps/buddy/electron/main/sandbox/sandboxProcess.ts index 423fb9257..fc9e0f0cd 100644 --- a/apps/buddy/service/src/sandbox/sandboxProcess.ts +++ b/apps/buddy/electron/main/sandbox/sandboxProcess.ts @@ -1,8 +1,8 @@ import process from 'node:process' import { z } from 'zod' +import { BuddyServiceRpcServer } from '../../../service/src/rpc/BuddyServiceRpcServer' +import { runSandboxCommand } from '../../../service/src/sandbox/runSandboxCommand' import { sandboxProcessInputSchema, sandboxSettlementAckSchema } from '../../../shared/permissions/shellSandbox' -import { BuddyServiceRpcServer } from '../rpc/BuddyServiceRpcServer' -import { runSandboxCommand } from './runSandboxCommand' const port = process.parentPort if (!port) diff --git a/apps/buddy/electron/main/secrets/CredentialVault.ts b/apps/buddy/electron/main/secrets/CredentialVault.ts index d62b3635c..dc164ecf3 100644 --- a/apps/buddy/electron/main/secrets/CredentialVault.ts +++ b/apps/buddy/electron/main/secrets/CredentialVault.ts @@ -13,12 +13,13 @@ import { unlink, } from 'node:fs/promises' import { homedir } from 'node:os' -import { join } from 'node:path' +import { dirname, join } from 'node:path' import process from 'node:process' import { safeStorage } from 'electron' import { safeDiagnosticReporter } from '../../../shared/diagnostics/applicationDiagnostic' import { credentialVaultSystemCodeSchema } from '../../../shared/diagnostics/credentialVaultFailure' import { OPERATING_SYSTEM } from '../../../shared/platform/identifiers' +import { runProfileWrite } from '../storage/profileWrites' type LocalCredentialNamespace = CredentialNamespace | 'sync' | 'backup' | 'extensions' | 'images' @@ -89,7 +90,7 @@ export function createCredentialVault( try { if (operation !== 'delete') assertAvailable(cipher) - return await action() + return await (operation === 'write' || operation === 'delete' ? runProfileWrite(dirname(dirname(root)), action) : action()) } catch (error) { const failure = error instanceof CredentialStoreUnavailableError || error instanceof CredentialVaultError diff --git a/apps/buddy/electron/main/storage/StorageBackupService.ts b/apps/buddy/electron/main/storage/StorageBackupService.ts index 937ae93f2..3ca2b3dd4 100644 --- a/apps/buddy/electron/main/storage/StorageBackupService.ts +++ b/apps/buddy/electron/main/storage/StorageBackupService.ts @@ -77,11 +77,13 @@ export class StorageBackupService { throw new Error('STORAGE_OPERATION_PENDING') if (runsRpc.isBusy.response.parse(await this.runtime.service.request(runsRpc.isBusy.method, {}, { timeoutMs: 5000 }))) throw new Error('SNAPSHOT_RUNTIME_BUSY') - const encrypted = options.credentials ? await encryptBackupCredentials(await exportBackupCredentials(this.environment.paths.buddyHome, options.scopes), options.password!) : undefined try { - const manifest = await this.runtime.withStoppedStorage(() => runSnapshotWorker(this.environment.paths.lexoraHome, this.environment.storage.state.identity!.dataId, (progress) => { - this.getWindow()?.webContents.send(STORAGE_CHANGED, progress) - }, this.controller.signal, this.environment.storage.privateDirectoriesExecutable, { scopes: options.scopes, credentials: options.credentials, ...(automatic ? { automatic } : {}) }, encrypted)) + const manifest = await this.runtime.withStoppedStorage(async () => { + const encrypted = options.credentials ? await encryptBackupCredentials(await exportBackupCredentials(this.environment.paths.buddyHome, options.scopes), options.password!) : undefined + return runSnapshotWorker(this.environment.paths.lexoraHome, this.environment.storage.state.identity!.dataId, (progress) => { + this.getWindow()?.webContents.send(STORAGE_CHANGED, progress) + }, this.controller.signal, this.environment.storage.privateDirectoriesExecutable, { scopes: options.scopes, credentials: options.credentials, ...(automatic ? { automatic } : {}) }, encrypted) + }) await this.prune(manifest.id).catch(() => {}) return snapshotSummary(manifest) } diff --git a/apps/buddy/electron/main/storage/StorageMigrationWindow.ts b/apps/buddy/electron/main/storage/StorageMigrationWindow.ts index 0577ac3ed..df8e6a5b7 100644 --- a/apps/buddy/electron/main/storage/StorageMigrationWindow.ts +++ b/apps/buddy/electron/main/storage/StorageMigrationWindow.ts @@ -8,6 +8,7 @@ import { join } from 'node:path' import process from 'node:process' import { Worker } from 'node:worker_threads' import { app, BrowserWindow, session, shell } from 'electron' +import { BUDDY_DEVELOPMENT_RESTART_EXIT_CODE } from '../../../shared/runtime/profile' import { ApplicationLogReader } from '../diagnostics/ApplicationLogReader' import { saveApplicationDiagnosticBundle } from '../diagnostics/saveApplicationDiagnosticBundle' import { createFeedbackIssueUrl } from '../feedbackIssue' @@ -17,6 +18,7 @@ import { storageMigrationPage, storageMigrationProgressScript } from './storageM export async function showStorageMigration(storage: DesktopStorage, diagnostics: DesktopDiagnosticLogger): Promise { await app.whenReady() const operation = storage.state.pending! + const requiredUpgrade = operation.kind === 'consolidate' || operation.kind === 'upgrade' const isolated = session.fromPartition(`storage-${randomUUID()}`, { cache: false }) isolated.setPermissionRequestHandler((_contents, _permission, respond) => respond(false)) isolated.setPermissionCheckHandler(() => false) @@ -45,8 +47,9 @@ export async function showStorageMigration(storage: DesktopStorage, diagnostics: if (finalizing) return finalizing = true + const restartDevelopment = relaunch && !process.argv.includes('--lexora-storage-maintenance') && !app.isPackaged && process.env.LEXORA_BUDDY_DEV_LAUNCHER === '1' try { - if (relaunch) + if (relaunch && !process.argv.includes('--lexora-storage-maintenance') && !restartDevelopment) app.relaunch() } catch (error) { @@ -54,7 +57,7 @@ export async function showStorageMigration(storage: DesktopStorage, diagnostics: throw error } await diagnostics.close() - app.exit(0) + app.exit(restartDevelopment ? BUDDY_DEVELOPMENT_RESTART_EXIT_CODE : 0) } const render = (progress: StorageProgress) => { const failure = progress.phase === 'failed' || progress.phase === 'cancelled' @@ -110,7 +113,7 @@ export async function showStorageMigration(storage: DesktopStorage, diagnostics: await rendering await quit(true) } - else if (cancelled && operation.kind !== 'consolidate') { + else if (cancelled && !requiredUpgrade) { await storage.store.cancel() await quit(true) } @@ -190,12 +193,12 @@ export async function showStorageMigration(storage: DesktopStorage, diagnostics: if (starting) return cancelled = true - exiting = operation.kind === 'consolidate' + exiting = requiredUpgrade if (worker) { void window.webContents.executeJavaScript('document.getElementById(\'cancel\').hidden=true;document.getElementById(\'phase\').textContent=\'正在停止,原数据会保留\'').catch(() => {}) worker.postMessage('cancel') } - else if (operation.kind !== 'consolidate') { + else if (!requiredUpgrade) { void storage.store.cancel().then(() => quit(true)).catch(error => failed(error)) } } diff --git a/apps/buddy/electron/main/storage/__tests__/profileWrites.spec.ts b/apps/buddy/electron/main/storage/__tests__/profileWrites.spec.ts new file mode 100644 index 000000000..e6422b6f7 --- /dev/null +++ b/apps/buddy/electron/main/storage/__tests__/profileWrites.spec.ts @@ -0,0 +1,40 @@ +import { readFile, writeFile } from 'node:fs/promises' +import { join } from 'node:path' +import { createTemporaryDirectory } from '@buddy-tests/temporaryDirectories' +import { expect, it } from 'vitest' +import { runProfileWrite, withProfileWritesStopped } from '../profileWrites' + +it('drains accepted writes before snapshot and rejects concurrent writes only for that profile', async () => { + const root = await createTemporaryDirectory('lexora-write-freeze-') + const admitted = Promise.withResolvers() + const frozen = Promise.withResolvers() + const resume = Promise.withResolvers() + const value = join(root, 'value') + const write = runProfileWrite(root, async () => { + await admitted.promise + await writeFile(value, 'committed') + }) + const snapshot = withProfileWritesStopped(root, async () => { + expect(await readFile(value, 'utf8')).toBe('committed') + frozen.resolve() + await resume.promise + }) + await expect(runProfileWrite(root, () => writeFile(value, 'late'))).rejects.toThrow('PROFILE_STORAGE_MAINTENANCE') + admitted.resolve() + await frozen.promise + await expect(withProfileWritesStopped(root, async () => {})).rejects.toThrow('PROFILE_STORAGE_MAINTENANCE') + await runProfileWrite(join(root, 'other-profile'), () => writeFile(join(root, 'other'), 'independent')) + resume.resolve() + await Promise.all([write, snapshot]) + await runProfileWrite(root, () => writeFile(value, 'resumed')) + expect(await readFile(value, 'utf8')).toBe('resumed') +}) + +it('releases the host write barrier when snapshot creation fails', async () => { + const root = await createTemporaryDirectory('lexora-write-failure-') + await expect(withProfileWritesStopped(root, async () => { + throw new Error('SNAPSHOT_FAILED') + })).rejects.toThrow('SNAPSHOT_FAILED') + await runProfileWrite(root, () => writeFile(join(root, 'value'), 'retained')) + expect(await readFile(join(root, 'value'), 'utf8')).toBe('retained') +}) diff --git a/apps/buddy/electron/main/storage/__tests__/requestPersistenceUpgrade.spec.ts b/apps/buddy/electron/main/storage/__tests__/requestPersistenceUpgrade.spec.ts new file mode 100644 index 000000000..add2d36ee --- /dev/null +++ b/apps/buddy/electron/main/storage/__tests__/requestPersistenceUpgrade.spec.ts @@ -0,0 +1,37 @@ +import type { DesktopStorage } from '../storageBootstrap' +import { readFile } from 'node:fs/promises' +import { join } from 'node:path' +import { createTemporaryDirectory } from '@buddy-tests/temporaryDirectories' +import { expect, it } from 'vitest' +import { StorageLocationStore } from '../../../../platform/storage/StorageLocationStore' +import { openBuddyDatabase, openBuddySqliteDatabase } from '../../../../service/src/storage/database' +import { requestPersistenceUpgrade } from '../requestPersistenceUpgrade' + +it.each(['legacy_jsonl', 'sqlite'] as const)('requests a separate upgrade only for legacy data without rewriting the %s database', async (authority) => { + const root = await createTemporaryDirectory('desktop-persistence-upgrade-') + const databasePath = join(root, 'buddy/buddy.sqlite3') + const open = authority === 'sqlite' ? openBuddySqliteDatabase : openBuddyDatabase + open({ databasePath }).close() + const original = await readFile(databasePath) + const store = new StorageLocationStore(join(root, 'bootstrap/location.json'), root) + const storage: DesktopStorage = { + store, + state: await store.read(), + bootstrapRoot: join(root, 'bootstrap'), + leaseDirectory: join(root, 'leases'), + legacySources: [], + protection: { sensitiveRoots: [], sandboxRoots: [] }, + } + await requestPersistenceUpgrade(storage) + const result = await store.read() + if (authority === 'legacy_jsonl') { + expect(result.pending).toMatchObject({ kind: 'upgrade', source: root, target: root, phase: 'pending' }) + storage.state = result + await requestPersistenceUpgrade(storage) + expect(await store.read()).toEqual(result) + } + else { + expect(result.pending).toBeNull() + } + expect(await readFile(databasePath)).toEqual(original) +}) diff --git a/apps/buddy/electron/main/storage/profileWrites.ts b/apps/buddy/electron/main/storage/profileWrites.ts new file mode 100644 index 000000000..cd924ace6 --- /dev/null +++ b/apps/buddy/electron/main/storage/profileWrites.ts @@ -0,0 +1,56 @@ +import { resolve } from 'node:path' + +interface Writes { + blocked: boolean + pending: Set> +} +const profiles = new Map() + +function state(root: string): Writes { + const key = resolve(root) + let current = profiles.get(key) + if (!current) { + current = { blocked: false, pending: new Set() } + profiles.set(key, current) + } + return current +} + +function release(root: string, current: Writes) { + if (!current.blocked && !current.pending.size && profiles.get(resolve(root)) === current) + profiles.delete(resolve(root)) +} + +export function runProfileWrite(root: string, operation: () => Promise): Promise { + const current = state(root) + if (current.blocked) + return Promise.reject(new Error('PROFILE_STORAGE_MAINTENANCE')) + const deferred = Promise.withResolvers() + const pending = deferred.promise.finally(() => { + current.pending.delete(pending) + release(root, current) + }) + current.pending.add(pending) + try { + deferred.resolve(operation()) + } + catch (error) { + deferred.reject(error) + } + return pending +} + +export async function withProfileWritesStopped(root: string, operation: () => Promise): Promise { + const current = state(root) + if (current.blocked) + throw new Error('PROFILE_STORAGE_MAINTENANCE') + current.blocked = true + try { + await Promise.allSettled([...current.pending]) + return await operation() + } + finally { + current.blocked = false + release(root, current) + } +} diff --git a/apps/buddy/electron/main/storage/requestPersistenceUpgrade.ts b/apps/buddy/electron/main/storage/requestPersistenceUpgrade.ts new file mode 100644 index 000000000..2298697a5 --- /dev/null +++ b/apps/buddy/electron/main/storage/requestPersistenceUpgrade.ts @@ -0,0 +1,23 @@ +import type { DesktopStorage } from './storageBootstrap' +import { lstat } from 'node:fs/promises' +import { join } from 'node:path' +import { isMissing, storageError } from '../../../platform/storage/storageFiles' +import { BUDDY_SCHEMA_VERSION } from '../../../service/src/storage/schema' +import { snapshotDatabaseVersion } from '../../../service/src/storage/snapshotDatabase' + +export async function requestPersistenceUpgrade(storage: DesktopStorage): Promise { + if (storage.state.pending) + return + const path = join(storage.state.activeRoot, 'buddy', 'buddy.sqlite3') + const metadata = await lstat(path).catch((error) => { + if (!isMissing(error)) + throw error + return null + }) + if (!metadata) + return + if (!metadata.isFile() || metadata.isSymbolicLink()) + throw storageError('STORAGE_DATABASE_INVALID') + if (snapshotDatabaseVersion(path) <= BUDDY_SCHEMA_VERSION) + await storage.store.request('upgrade', storage.state.activeRoot) +} diff --git a/apps/buddy/electron/main/storage/storageBootstrap.ts b/apps/buddy/electron/main/storage/storageBootstrap.ts index fb9a61086..5305a15c1 100644 --- a/apps/buddy/electron/main/storage/storageBootstrap.ts +++ b/apps/buddy/electron/main/storage/storageBootstrap.ts @@ -1,7 +1,7 @@ import type { StorageLocation } from '../../../platform/storage/StorageLocationStore' import type { LegacyStorageSource } from '../../../service/src/storage/StorageMigration' import type { BuddyRuntimePaths } from '../paths' -import { createHash, randomUUID } from 'node:crypto' +import { randomUUID } from 'node:crypto' import { existsSync, lstatSync, readdirSync } from 'node:fs' import { homedir } from 'node:os' import { dirname, join } from 'node:path' @@ -9,7 +9,7 @@ import { ensurePrivateDirectoriesSync } from '../../../platform/filesystem/priva import { profileStorageProtection } from '../../../platform/storage/ProfileLocations' import { legacyStorageExcluded } from '../../../platform/storage/storageCopyPolicy' import { readStorageJsonSync, storageError, writeStorageJsonSync } from '../../../platform/storage/storageFiles' -import { STORAGE_IDENTITY_FILE, storageLocationSchema, StorageLocationStore } from '../../../platform/storage/StorageLocationStore' +import { STORAGE_IDENTITY_FILE, storageBootstrapRoot, storageLocationSchema, StorageLocationStore } from '../../../platform/storage/StorageLocationStore' import { storageIdentitySchema } from '../../../shared/storage/storageApi' import { StorageUnavailableError } from './storageUnavailable' @@ -22,9 +22,7 @@ export interface DesktopStorage { protection: ReturnType privateDirectoriesExecutable?: string } -export function storageBootstrapRoot(root: string): string { - return join(dirname(root), '.lexora-locations', createHash('sha256').update(root).digest('hex').slice(0, 24)) -} +export { storageBootstrapRoot } from '../../../platform/storage/StorageLocationStore' export function readStorageBootstrap(defaultRoot: string) { const bootstrapRoot = storageBootstrapRoot(defaultRoot) const store = new StorageLocationStore(join(bootstrapRoot, 'location.json'), defaultRoot) @@ -35,7 +33,7 @@ export function readStorageBootstrap(defaultRoot: string) { const descriptor = readStorageJsonSync(join(state.activeRoot, STORAGE_IDENTITY_FILE)) if (state.identity) { const identity = storageIdentitySchema.safeParse(descriptor) - const finishingRestore = state.pending?.kind === 'restore' && state.pending.phase === 'ready' && state.pending.target === state.activeRoot + const finishingRestore = (state.pending?.kind === 'restore' || state.pending?.kind === 'upgrade') && state.pending.phase === 'ready' && state.pending.target === state.activeRoot if (!identity.success || identity.data.dataId !== state.identity.dataId || (identity.data.generation !== state.identity.generation && !(finishingRestore && identity.data.generation === state.pending?.id))) throw new StorageUnavailableError(state.activeRoot) } diff --git a/apps/buddy/electron/main/storage/storageMigrationDiagnostics.ts b/apps/buddy/electron/main/storage/storageMigrationDiagnostics.ts index c01ac7bb0..ee63c1d59 100644 --- a/apps/buddy/electron/main/storage/storageMigrationDiagnostics.ts +++ b/apps/buddy/electron/main/storage/storageMigrationDiagnostics.ts @@ -14,7 +14,7 @@ export function storageMigrationIssueBody(input: { appVersion: string platform: string }): string { - const kind = { consolidate: '首次升级整理', relocate: '更改数据目录', restore: '恢复备份' }[input.kind] + const kind = { consolidate: '首次升级整理', relocate: '更改数据目录', restore: '恢复备份', upgrade: '本地数据升级' }[input.kind] return [ '## 问题描述', `Lexora 在${kind}时未能完成。`, diff --git a/apps/buddy/electron/main/storage/storageMigrationPage.ts b/apps/buddy/electron/main/storage/storageMigrationPage.ts index 276736d51..a8afe7d76 100644 --- a/apps/buddy/electron/main/storage/storageMigrationPage.ts +++ b/apps/buddy/electron/main/storage/storageMigrationPage.ts @@ -35,6 +35,7 @@ function size(bytes: number): string { } function presentation(kind: StorageOperation['kind'], progress: StorageProgress, committing: boolean) { + const requiredUpgrade = kind === 'consolidate' || kind === 'upgrade' const failure = progress.phase === 'failed' || progress.phase === 'cancelled' const copying = progress.phase === 'copying' const percent = copying && progress.totalBytes !== null ? Math.min(100, Math.floor(progress.completedBytes / Math.max(1, progress.totalBytes) * 100)) : null @@ -42,7 +43,7 @@ function presentation(kind: StorageOperation['kind'], progress: StorageProgress, const phases: Record = { preparing: '正在检查需要保留的资料', copying: `正在复制${category}`, - converting: '正在更新资料中的目录位置', + converting: kind === 'upgrade' ? '正在升级任务与对话记录' : '正在更新资料中的目录位置', verifying: '正在验证数据库与附件', activating: '即将完成,正在启用数据目录', completed: '资料已准备好,正在重新启动', @@ -55,12 +56,12 @@ function presentation(kind: StorageOperation['kind'], progress: StorageProgress, percent, text: { 'phase': phases[progress.phase], - 'amount': percent === null ? '' : `${size(progress.completedBytes)} / ${size(progress.totalBytes!)}`, - 'detail': failure ? failureMessage(progress.error) : copying ? `${percent === null ? '' : `复制进度 ${percent}% · `}已复制 ${progress.files.toLocaleString('zh-CN')} 个文件` : progress.phase === 'preparing' ? '正在确认文件与可用空间,文件较多时请稍候。' : '完成后会自动重新启动 Lexora,请稍候。', - 'cancel': kind === 'consolidate' ? '停止整理并退出' : failure ? '使用原目录' : '取消并使用原目录', + 'amount': progress.totalBytes === null ? '' : `${size(progress.completedBytes)} / ${size(progress.totalBytes!)}`, + 'detail': failure ? failureMessage(progress.error) : copying ? `${percent === null ? '' : `复制进度 ${percent}% · `}已复制 ${progress.files.toLocaleString('zh-CN')} 个文件` : progress.phase === 'preparing' ? `已检查 ${progress.files.toLocaleString('zh-CN')} 项资料,正在确认文件与可用空间。` : progress.phase === 'verifying' ? `已校验 ${progress.files.toLocaleString('zh-CN')} 项资料。` : '完成后会自动重新启动 Lexora,请稍候。', + 'cancel': requiredUpgrade ? '停止整理并退出' : failure ? '使用原目录' : '取消并使用原目录', 'error-code': failure ? storageMigrationErrorCode(progress.error) : '', }, - hidden: { retry: !failure, cancel: committing || (failure && kind === 'consolidate'), exit: !failure, support: !failure }, + hidden: { retry: !failure, cancel: committing || (failure && requiredUpgrade), exit: !failure, support: !failure }, } } @@ -83,10 +84,12 @@ export function storageMigrationProgressScript(kind: StorageOperation['kind'], p export function storageMigrationPage(operation: Pick, progress: StorageProgress = preparing, committing = false, diagnosticReference = ''): string { const state = presentation(operation.kind, progress, committing) - const title = operation.kind === 'consolidate' ? '统一数据目录' : operation.kind === 'restore' ? '正在恢复数据' : '正在搬迁数据' - const explanation = operation.kind === 'consolidate' - ? '本次升级将分散的本机资料整理到同一目录,便于管理和备份。仅首次启动时需要整理。' - : operation.kind === 'restore' ? '正在把所选备份内容恢复到当前数据目录,未选择的内容保持原样。完成后会重新启动。' : '正在将资料复制到你选择的目录。验证完成后,Lexora 会从新目录启动。' + const title = operation.kind === 'upgrade' ? '正在升级本地数据' : operation.kind === 'consolidate' ? '统一数据目录' : operation.kind === 'restore' ? '正在恢复数据' : '正在搬迁数据' + const explanation = operation.kind === 'upgrade' + ? '正在升级本机的任务与对话记录。验证完成后将启用新格式,原始数据会保留为恢复副本。' + : operation.kind === 'consolidate' + ? '本次升级将分散的本机资料整理到同一目录,便于管理和备份。仅首次启动时需要整理。' + : operation.kind === 'restore' ? '正在把所选备份内容恢复到当前数据目录,未选择的内容保持原样。完成后会重新启动。' : '正在将资料复制到你选择的目录。验证完成后,Lexora 会从新目录启动。' return `Lexora · 数据整理
Lexora · ${operation.kind === 'consolidate' ? '首次升级整理' : operation.kind === 'restore' ? '恢复备份' : '更改数据目录'}
+
Lexora · ${operation.kind === 'upgrade' ? '本地数据升级' : operation.kind === 'consolidate' ? '首次升级整理' : operation.kind === 'restore' ? '恢复备份' : '更改数据目录'}

${title}

${explanation}

原数据会保留 · 本次操作仅在本机进行

整理到${escape(operation.target)}
    ${['检查资料', '复制资料', '完成准备'].map((label, index) => `
  1. ${label}
  2. `).join('')}
@@ -339,5 +342,5 @@ export function storageMigrationPage(operation: Pick导出诊断日志提交 Issue
-

${operation.kind === 'restore' ? '恢复前的数据将保留为副本,浏览器资料与缓存不受影响。' : '缓存、旧日志与崩溃报告留在原位置,无需搬迁。'}
完成后会自动重新启动 Lexora。

` +

${operation.kind === 'upgrade' ? '升级前的数据将保留在当前目录的 recovery 文件夹中。' : operation.kind === 'restore' ? '恢复前的数据将保留为副本,浏览器资料与缓存不受影响。' : '缓存、旧日志与崩溃报告留在原位置,无需搬迁。'}
完成后会自动重新启动 Lexora。

` } diff --git a/apps/buddy/electron/main/updates/linuxInstaller.ts b/apps/buddy/electron/main/updates/linuxInstaller.ts index a3a6611fb..0d97fae2e 100644 --- a/apps/buddy/electron/main/updates/linuxInstaller.ts +++ b/apps/buddy/electron/main/updates/linuxInstaller.ts @@ -3,11 +3,13 @@ import type { DesktopDistribution } from '../../shared/desktopDistribution' import type { DesktopUpdateManualInstallation } from '../../shared/desktopUpdates' import type { DesktopUpdatePackage } from './DesktopUpdatePackage' import { spawn } from 'node:child_process' +import { randomUUID } from 'node:crypto' import { accessSync, constants, readFileSync } from 'node:fs' import { mkdir } from 'node:fs/promises' import { join } from 'node:path' import process from 'node:process' import { z } from 'zod' +import { createChildProcessEnvironment } from '../../../platform/process/childProcessEnvironment' import { desktopUpdateArtifactName } from '../../shared/desktopDistribution' import { desktopVersionSchema } from '../../shared/desktopUpdates' @@ -24,7 +26,7 @@ export function linuxInstallerAvailable(helper: string | undefined, format: Linu if (!helper || process.getuid?.() === 0 || process.execPath !== '/opt/lexora-buddy/lexora-buddy') return false try { - for (const path of [helper, '/usr/bin/pkexec', managers[format]]) + for (const path of [helper, '/usr/bin/pkexec', '/usr/bin/systemd-run', '/usr/bin/env', managers[format]]) accessSync(path, constants.X_OK) return true } @@ -62,7 +64,8 @@ export async function prepareLinuxInstaller(options: { quit: () => void }): Promise<() => void> { await mkdir(options.directory, { recursive: true }) - const child = spawn(options.helper, ['--supervise-linux-update'], { detached: true, stdio: ['pipe', 'pipe', 'ignore'] }) + const environment = createChildProcessEnvironment({ source: process.env, additions: { LEXORA_HOME: process.env.LEXORA_HOME, LEXORA_BUDDY_PROFILE: process.env.LEXORA_BUDDY_PROFILE } }) + const child = spawn('/usr/bin/systemd-run', ['--user', '--pipe', '--wait', '--collect', '--quiet', `--unit=lexora-update-${randomUUID()}`, '--property=Type=exec', '--', '/usr/bin/env', '-i', ...Object.entries(environment).map(([key, value]) => `${key}=${value}`), options.helper, '--supervise-linux-update'], { detached: true, stdio: ['pipe', 'pipe', 'ignore'], env: environment }) child.stdin.on('error', () => {}) const ready = new Promise((resolve, reject) => { const timer = setTimeout(() => { @@ -90,7 +93,7 @@ export async function prepareLinuxInstaller(options: { version: options.update.version, receipt: join(options.directory, 'installation.json'), log: join(options.directory, 'installation.log'), - arguments: process.argv.slice(1).filter(argument => argument !== '--background'), + arguments: process.argv.slice(1).filter(argument => !['--background', '--lexora-platform-host', '--lexora-storage-maintenance'].includes(argument)), })}\n`) try { await ready diff --git a/apps/buddy/electron/main/updates/windowsInstaller.ts b/apps/buddy/electron/main/updates/windowsInstaller.ts index f98577c66..406c98518 100644 --- a/apps/buddy/electron/main/updates/windowsInstaller.ts +++ b/apps/buddy/electron/main/updates/windowsInstaller.ts @@ -1,12 +1,39 @@ -import { spawn } from 'node:child_process' +import { join } from 'node:path' +import process from 'node:process' +import { z } from 'zod' +import { runNativeCommand } from '../../../platform/native/nativeCommand' +import { createChildProcessEnvironment } from '../../../platform/process/childProcessEnvironment' +import { validateWindowsFilePath } from '../../../platform/windows/filePath' -export function launchWindowsUpdateInstaller(path: string): Promise { - return new Promise((resolve, reject) => { - const child = spawn(path, ['/S', '--updated'], { detached: true, stdio: 'ignore', windowsHide: false }) - child.once('error', reject) - child.once('spawn', () => { - child.unref() - resolve() - }) - }) +const launchScript = ` +$ErrorActionPreference = 'Stop' +[Console]::InputEncoding = [Text.UTF8Encoding]::new($false) +[Console]::OutputEncoding = [Text.UTF8Encoding]::new($false) +$request = [Console]::In.ReadToEnd() | ConvertFrom-Json +$startup = ([wmiclass]'Win32_ProcessStartup').CreateInstance() +$startup.EnvironmentVariables = [string[]]@($request.environment.psobject.Properties | ForEach-Object { $_.Name + '=' + [string]$_.Value }) +$startup.ShowWindow = 1 +$startup.WinstationDesktop = 'WinSta0\\Default' +$result = ([wmiclass]'Win32_Process').Create(('"' + [string]$request.path + '" /S --updated'), $request.directory, $startup) +if ($result.ReturnValue -ne 0) { throw 'UPDATE_INSTALL_UNAVAILABLE' } +$created = Get-WmiObject Win32_Process -Filter ('ProcessId = ' + $result.ProcessId) +if (!$created -or $created.SessionId -ne [Diagnostics.Process]::GetCurrentProcess().SessionId -or $created.GetOwnerSid().Sid -ne [Security.Principal.WindowsIdentity]::GetCurrent().User.Value) { + if ($created -and [string]::Equals($created.ExecutablePath, $request.path, [StringComparison]::OrdinalIgnoreCase)) { $null = $created.Terminate(1) } + throw 'UPDATE_INSTALL_SESSION_CHANGED' +} +@{ pid = [int]$result.ProcessId } | ConvertTo-Json -Compress +` + +export async function launchWindowsUpdateInstaller(path: string): Promise { + if (!process.env.SystemRoot) + throw new Error('UPDATE_INSTALL_UNAVAILABLE') + const executable = validateWindowsFilePath(path) + const environment = createChildProcessEnvironment({ source: process.env, additions: { LEXORA_HOME: process.env.LEXORA_HOME, LEXORA_BUDDY_PROFILE: process.env.LEXORA_BUDDY_PROFILE } }) + const result = await runNativeCommand(join(process.env.SystemRoot, 'System32/WindowsPowerShell/v1.0/powershell.exe'), ['-NoProfile', '-NonInteractive', '-Command', launchScript], { path: executable, directory: process.env.SystemRoot, environment }, { env: environment, timeoutMs: 15000, maxBytes: 16384 }) + try { + if (result.code === 0 && z.object({ pid: z.number().int().positive() }).strict().safeParse(JSON.parse(result.stdout.toString('utf8'))).success) + return + } + catch {} + throw new Error('UPDATE_INSTALL_UNAVAILABLE') } diff --git a/apps/buddy/electron/preload/browser.ts b/apps/buddy/electron/preload/browser.ts index 47f697813..bbd6296e5 100644 --- a/apps/buddy/electron/preload/browser.ts +++ b/apps/buddy/electron/preload/browser.ts @@ -1,5 +1,5 @@ import type { BrowserClearDataInput } from '../../shared/browser/browserData' -import type { BrowserScreenshotResult } from '../../shared/browser/browserDesktopApi' +import type { BrowserScreenshotResult, DesktopBrowserPresentationInput } from '../../shared/browser/browserDesktopApi' import type { BrowserLocateElementInput } from '../../shared/browser/browserSelection' import type { TaskFileTarget } from '../../shared/files/taskFileApi' import type { DesktopBrowserGuestDescriptor, DesktopBrowserProfileMode, DesktopBrowserSetSurfaceInput, DesktopBrowserState, LexoraDesktopApi } from '../shared/desktopApi' @@ -10,6 +10,8 @@ import { subscribe } from './subscribe' export function createBrowserApi(): Pick { return { browser: Object.freeze({ + acquirePresentation: () => ipcRenderer.invoke(DESKTOP_IPC_CHANNELS.browserAcquirePresentation), + present: (input: DesktopBrowserPresentationInput) => ipcRenderer.invoke(DESKTOP_IPC_CHANNELS.browserPresent, input), pickElement: (sessionId: string, requestId: string) => ipcRenderer.invoke(DESKTOP_IPC_CHANNELS.browserPickElement, { sessionId, requestId }), cancelElementPick: (sessionId: string, requestId: string) => ipcRenderer.invoke(DESKTOP_IPC_CHANNELS.browserCancelElementPick, { sessionId, requestId }), locateElement: (input: BrowserLocateElementInput) => ipcRenderer.invoke(DESKTOP_IPC_CHANNELS.browserLocateElement, input), diff --git a/apps/buddy/electron/preload/desktop.ts b/apps/buddy/electron/preload/desktop.ts index cbbcbcc07..b130fbbd3 100644 --- a/apps/buddy/electron/preload/desktop.ts +++ b/apps/buddy/electron/preload/desktop.ts @@ -13,6 +13,7 @@ import type { DesktopUpdateAction, DesktopUpdateState } from '../shared/desktopU import { ipcRenderer, webUtils } from 'electron' import { DESKTOP_NOTIFICATION_CHANNEL } from '../../shared/notifications/desktopNotification' import { NOTIFICATION_SOUND_CHANNEL } from '../../shared/notifications/notificationSound' +import { desktopTargetRequestSchema } from '../../shared/service/localServiceProtocol' import { STORAGE_CHANGED, STORAGE_CHANNEL } from '../../shared/storage/storageApi' import { SYNC_CHANGED, SYNC_CHANNEL } from '../../shared/sync/syncApi' import { DESKTOP_IPC_CHANNELS } from '../shared/desktopApi' @@ -120,9 +121,20 @@ export function createDesktopApi(): Pick ipcRenderer.off(DESKTOP_IPC_CHANNELS.appPrepareQuit, handler) }, - onOpenTarget: (listener: (target: DesktopOpenTarget) => void) => ( - subscribe(DESKTOP_IPC_CHANNELS.appOpenTarget, listener) - ), + onOpenTarget: (listener: (target: DesktopOpenTarget) => Promise) => { + const stopLegacy = subscribe(DESKTOP_IPC_CHANNELS.appOpenTarget, target => void listener(target).catch(() => {})) + const stopRequests = subscribe(DESKTOP_IPC_CHANNELS.appOpenTargetRequest, (raw) => { + const { requestId, intent } = desktopTargetRequestSchema.parse(raw) + const target: DesktopOpenTarget = intent.kind === 'tasks' && intent.conversationId ? { conversationId: intent.conversationId } : { kind: intent.kind } + void Promise.resolve().then(() => listener(target)).catch(() => false).then((opened) => { + ipcRenderer.send(DESKTOP_IPC_CHANNELS.appOpenTargetAck, { requestId, opened: opened === true }) + }) + }) + return () => { + stopLegacy() + stopRequests() + } + }, onHidden: (listener: () => void) => subscribe(DESKTOP_IPC_CHANNELS.appHidden, listener), openFeedbackIssue: (feedback: string) => ipcRenderer.invoke( DESKTOP_IPC_CHANNELS.appOpenFeedbackIssue, diff --git a/apps/buddy/electron/preload/local-chat/activity.ts b/apps/buddy/electron/preload/local-chat/activity.ts index 644b96663..be9e5182a 100644 --- a/apps/buddy/electron/preload/local-chat/activity.ts +++ b/apps/buddy/electron/preload/local-chat/activity.ts @@ -52,10 +52,10 @@ export function createActivityApi(): Pick ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.approvalsList, input ?? {}), - approve: (approvalId, scope) => - ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.approvalsApprove, { approvalId, scope }), - deny: approvalId => - ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.approvalsDeny, { approvalId }), + approve: (approvalId, scope, conditions) => + ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.approvalsApprove, { approvalId, scope, ...(conditions ? { conditions } : {}) }), + deny: (approvalId, conditions) => + ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.approvalsDeny, { approvalId, ...(conditions ? { conditions } : {}) }), }), usage: Object.freeze({ getSnapshot: () => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.usageSnapshot), diff --git a/apps/buddy/electron/preload/local-chat/conversation.ts b/apps/buddy/electron/preload/local-chat/conversation.ts index a1c71c39e..842feb89b 100644 --- a/apps/buddy/electron/preload/local-chat/conversation.ts +++ b/apps/buddy/electron/preload/local-chat/conversation.ts @@ -30,35 +30,34 @@ export function createConversationApi(): Pick ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsGetModelControls, { conversationId, ...(actorId ? { actorId } : {}) }), - deletePermanently: (conversationId, expectedRevision) => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsDeletePermanently, { conversationId, expectedRevision }), archive: (conversationId, expectedRevision) => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsArchive, { conversationId, expectedRevision }), restoreArchive: (conversationId, expectedRevision) => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsRestoreArchive, { conversationId, expectedRevision }), deleteArchived: (conversationId, expectedRevision) => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsDeleteArchived, { conversationId, expectedRevision }), listArchived: query => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsListArchived, query), getArchivePolicy: () => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsGetArchivePolicy, {}), setArchivePolicy: days => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsSetArchivePolicy, { days }), - delete: conversationId => - ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsDelete, { conversationId }), + delete: (conversationId, expectedRevision) => + ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsDelete, { conversationId, ...revisionCondition(expectedRevision) }), activateBranch: input => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsActivateBranch, input), listBranches: conversationId => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsListBranches, { conversationId }), listMessages: input => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsListMessages, input), - rename: (conversationId, title) => - ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsRename, { conversationId, title }), - setPermissionSettings: (conversationId, settings) => ipcRenderer.invoke( + rename: (conversationId, title, expectedTitleRevision) => + ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsRename, { conversationId, title, ...(expectedTitleRevision !== undefined ? { expectedTitleRevision } : {}) }), + setPermissionSettings: (conversationId, settings, expectedRevision) => ipcRenderer.invoke( LOCAL_CHAT_IPC_CHANNELS.conversationsSetPermissionSettings, - { conversationId, ...settings }, + { conversationId, ...settings, ...revisionCondition(expectedRevision) }, ), listMembers: conversationId => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsListMembers, { conversationId }), - addMember: (conversationId, buddyId) => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsAddMember, { conversationId, buddyId }), - removeMember: (conversationId, buddyId) => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsRemoveMember, { conversationId, buddyId }), - setBuddy: (conversationId, buddyId) => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsSetBuddy, { conversationId, buddyId }), - moveToSpace: (conversationId, spaceId) => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsMoveToSpace, { conversationId, spaceId }), + addMember: (conversationId, buddyId, expectedRevision) => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsAddMember, { conversationId, buddyId, ...revisionCondition(expectedRevision) }), + removeMember: (conversationId, buddyId, expectedRevision) => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsRemoveMember, { conversationId, buddyId, ...revisionCondition(expectedRevision) }), + setBuddy: (conversationId, buddyId, expectedRevision) => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsSetBuddy, { conversationId, buddyId, ...revisionCondition(expectedRevision) }), + moveToSpace: (conversationId, spaceId, expectedRevision) => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsMoveToSpace, { conversationId, spaceId, ...revisionCondition(expectedRevision) }), setModelSelection: (conversationId, modelSelection, target) => ipcRenderer.invoke( LOCAL_CHAT_IPC_CHANNELS.conversationsSetModelSelection, - { conversationId, modelSelection, ...(target?.actorId ? { actorId: target.actorId } : {}), ...(target?.runId ? { targetRunId: target.runId } : {}) }, + { conversationId, modelSelection, ...revisionCondition(target?.expectedRevision), ...(target?.expectedRunGeneration !== undefined ? { expectedRunGeneration: target.expectedRunGeneration } : {}), ...(target?.expectedRunModelSelection !== undefined ? { expectedRunModelSelection: target.expectedRunModelSelection } : {}), ...(target?.expectedActiveRun !== undefined ? { expectedActiveRun: target.expectedActiveRun } : {}), ...(target?.actorId ? { actorId: target.actorId } : {}), ...(target?.runId ? { targetRunId: target.runId } : {}) }, ), listTimeline: input => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.conversationsListTimeline, input), @@ -75,9 +74,13 @@ export function createConversationApi(): Pick ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.chatStartTurn, request), regenerateAssistant: input => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.chatRegenerateAssistant, input), - cancel: runId => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.chatCancel, { runId }), + cancel: (runId, expectedRunGeneration) => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.chatCancel, { runId, ...(expectedRunGeneration !== undefined ? { expectedRunGeneration } : {}) }), onRunEvent: (listener: (event: LocalRunEvent) => void) => subscribe(LOCAL_CHAT_IPC_CHANNELS.runEvent, listener), }), } } + +function revisionCondition(expectedRevision: number | undefined) { + return expectedRevision !== undefined ? { expectedRevision } : {} +} diff --git a/apps/buddy/electron/preload/local-chat/draftRecovery.ts b/apps/buddy/electron/preload/local-chat/draftRecovery.ts new file mode 100644 index 000000000..800952d06 --- /dev/null +++ b/apps/buddy/electron/preload/local-chat/draftRecovery.ts @@ -0,0 +1,13 @@ +import type { DraftRecoveryApi } from '../../../shared/conversation/draftRecovery' +import type { LocalChatApi } from '../../shared/localChatApi' +import { ipcRenderer } from 'electron' +import { draftRecoveryLoadSchema } from '../../../shared/conversation/draftRecovery' +import { LOCAL_CHAT_IPC_CHANNELS } from '../../shared/localChatApi' + +export function createDraftRecoveryApi(): Pick { + return { draftRecovery: Object.freeze({ + load: async () => draftRecoveryLoadSchema.parse(await ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.draftRecoveryLoad)), + write: record => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.draftRecoveryWrite, record), + remove: input => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.draftRecoveryRemove, input), + }) } +} diff --git a/apps/buddy/electron/preload/local-chat/executions.ts b/apps/buddy/electron/preload/local-chat/executions.ts index 3ccddb5ea..5892b2c67 100644 --- a/apps/buddy/electron/preload/local-chat/executions.ts +++ b/apps/buddy/electron/preload/local-chat/executions.ts @@ -6,6 +6,7 @@ import { subscribe } from '../subscribe' export function createExecutionsApi(): Pick { return { executions: Object.freeze({ + activity: input => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.executionsActivity, input), list: input => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.executionsList, input), detail: input => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.executionsDetail, input), inputs: input => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.executionsInputs, input), diff --git a/apps/buddy/electron/preload/local-chat/privateNetwork.ts b/apps/buddy/electron/preload/local-chat/privateNetwork.ts new file mode 100644 index 000000000..d00dfd8ac --- /dev/null +++ b/apps/buddy/electron/preload/local-chat/privateNetwork.ts @@ -0,0 +1,14 @@ +import type { PrivateNetworkManagementApi } from '../../../shared/privateNetwork/management' +import type { LocalChatApi } from '../../shared/localChatApi' +import { ipcRenderer } from 'electron' +import { privateNetworkManagement } from '../../../shared/privateNetwork/management' +import { PRIVATE_NETWORK_IPC_PREFIX } from '../../shared/privateNetworkApi' + +export function createPrivateNetworkApi(): Pick { + return { + privateNetwork: Object.freeze(Object.fromEntries(Object.keys(privateNetworkManagement).map(name => [ + name, + (input: unknown) => ipcRenderer.invoke(`${PRIVATE_NETWORK_IPC_PREFIX}${name}`, input), + ])) as PrivateNetworkManagementApi), + } +} diff --git a/apps/buddy/electron/preload/local-chat/runtime.ts b/apps/buddy/electron/preload/local-chat/runtime.ts index 4ef144f4b..2684f5dcf 100644 --- a/apps/buddy/electron/preload/local-chat/runtime.ts +++ b/apps/buddy/electron/preload/local-chat/runtime.ts @@ -7,6 +7,9 @@ import { subscribe } from '../subscribe' export function createRuntimeApi(): Pick { return { runtime: Object.freeze({ + listRecovery: () => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.runtimeRecoveryList), + acknowledgeRecovery: (id: string) => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.runtimeRecoveryAcknowledge, { id }), + restoreRecovery: (id: string) => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.runtimeRecoveryRestore, { id }), getStatus: () => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.runtimeStatus), restart: () => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.runtimeRestart), onStateChanged: (listener: (state: LocalBuddyServiceSupervisorState) => void) => diff --git a/apps/buddy/electron/preload/local-chat/taskMarks.ts b/apps/buddy/electron/preload/local-chat/taskMarks.ts index d427da3d9..d48ea7486 100644 --- a/apps/buddy/electron/preload/local-chat/taskMarks.ts +++ b/apps/buddy/electron/preload/local-chat/taskMarks.ts @@ -8,9 +8,9 @@ export function createTaskMarksApi(): Pick { list: () => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.taskMarksList), create: input => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.taskMarksCreate, input), update: input => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.taskMarksUpdate, input), - delete: id => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.taskMarksDelete, { id }), + delete: (id, expectedRevision) => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.taskMarksDelete, { id, ...(expectedRevision !== undefined ? { expectedRevision } : {}) }), states: conversationIds => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.taskMarksStates, { conversationIds }), - assign: (conversationId, markId) => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.taskMarksAssign, { conversationId, markId }), + assign: (conversationId, markId, expectedAssignmentRevision) => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.taskMarksAssign, { conversationId, markId, ...(expectedAssignmentRevision !== undefined ? { expectedAssignmentRevision } : {}) }), setRead: input => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.taskMarksSetRead, input), clear: input => ipcRenderer.invoke(LOCAL_CHAT_IPC_CHANNELS.taskMarksClear, input), }), diff --git a/apps/buddy/electron/preload/localChatApi.ts b/apps/buddy/electron/preload/localChatApi.ts index ea3a5f50f..32fd9e452 100644 --- a/apps/buddy/electron/preload/localChatApi.ts +++ b/apps/buddy/electron/preload/localChatApi.ts @@ -5,7 +5,9 @@ import { createBuddiesApi } from './local-chat/buddies' import { createComposerApi } from './local-chat/composer' import { createConnectorsApi } from './local-chat/connectors' import { createConversationApi } from './local-chat/conversation' +import { createDraftRecoveryApi } from './local-chat/draftRecovery' import { createExecutionsApi } from './local-chat/executions' +import { createPrivateNetworkApi } from './local-chat/privateNetwork' import { createPromptsApi } from './local-chat/prompts' import { createProvidersApi } from './local-chat/providers' import { createQuestionsApi } from './local-chat/questions' @@ -19,11 +21,13 @@ import { createWebApi } from './local-chat/web' export function createLocalChatApi(): LocalChatApi { return Object.freeze({ ...createBuddiesApi(), + ...createDraftRecoveryApi(), ...createExecutionsApi(), ...createQuestionsApi(), ...createActivityApi(), ...createAutomationsApi(), ...createRuntimeApi(), + ...createPrivateNetworkApi(), ...createPromptsApi(), ...createProvidersApi(), ...createSpacesApi(), diff --git a/apps/buddy/electron/shared/desktopApi.ts b/apps/buddy/electron/shared/desktopApi.ts index 057e7ed0e..138cc5a4d 100644 --- a/apps/buddy/electron/shared/desktopApi.ts +++ b/apps/buddy/electron/shared/desktopApi.ts @@ -54,6 +54,8 @@ export const DESKTOP_IPC_CHANNELS = { appOpenFeedbackIssue: 'lexora:app:open-feedback-issue', appOpenReleasePage: 'lexora:app:open-release-page', appOpenTarget: 'lexora:app:open-target', + appOpenTargetRequest: 'lexora:app:open-target-request', + appOpenTargetAck: 'lexora:app:open-target-ack', appHidden: 'lexora:app:hidden', appPrepareQuit: 'lexora:app:prepare-quit', appPrepareQuitAck: 'lexora:app:prepare-quit-ack', @@ -79,6 +81,8 @@ export const DESKTOP_IPC_CHANNELS = { browserReload: 'lexora:browser:reload', browserSetProfileMode: 'lexora:browser:set-profile-mode', browserSetSurface: 'lexora:browser:set-surface', + browserAcquirePresentation: 'lexora:browser:acquire-presentation', + browserPresent: 'lexora:browser:present', browserShowFileInFolder: 'lexora:browser:show-file-in-folder', browserStateChanged: 'lexora:browser:state-changed', browserStop: 'lexora:browser:stop', @@ -95,10 +99,7 @@ export const DESKTOP_IPC_CHANNELS = { windowToggleMaximize: 'lexora:window:toggle-maximize', } as const -export interface DesktopOpenTarget { - conversationId: string - runId: string -} +export type DesktopOpenTarget = { conversationId: string, runId?: string } | { kind: 'settings' | 'tasks' } export interface DesktopWindowState { isMaximized: boolean @@ -220,6 +221,7 @@ export interface LexoraConfig { taskSidebar: DesktopTaskSidebarPreferences developerToolsEnabled: boolean language: 'zh-CN' | 'en-US' + exitBehavior: import('../../shared/lifecycle/exitBehavior').DesktopExitBehavior launchAtLogin: boolean notificationsEnabled: boolean notificationSound: NotificationSoundPreferences @@ -289,7 +291,7 @@ export interface LexoraDesktopApi { onBeforeQuit: (listener: (requestId: string) => Promise) => () => void onQuitPreparationCancelled: (listener: (requestId: string) => void) => () => void onHidden: (listener: () => void) => () => void - onOpenTarget: (listener: (target: DesktopOpenTarget) => void) => () => void + onOpenTarget: (listener: (target: DesktopOpenTarget) => Promise) => () => void openFeedbackIssue: (feedback: string) => Promise openReleasePage: (url: string) => Promise } diff --git a/apps/buddy/electron/shared/desktopApiSchemas.ts b/apps/buddy/electron/shared/desktopApiSchemas.ts index 36d353ed9..bd7648de3 100644 --- a/apps/buddy/electron/shared/desktopApiSchemas.ts +++ b/apps/buddy/electron/shared/desktopApiSchemas.ts @@ -5,6 +5,7 @@ import { DESKTOP_NAVIGATION_LAYOUTS } from '../../shared/desktopNavigationLayout import { extensionAuthorSchema } from '../../shared/extensions/extensionIdentity' import { fileIconPreferenceSchema } from '../../shared/icons/fileIconTheme' import { imagePreferencesSchema } from '../../shared/images/imagePreferences' +import { desktopExitBehaviorSchema } from '../../shared/lifecycle/exitBehavior' import { downloadSettingsSchema } from '../../shared/network/downloadSettings' import { proxySettingsSchema } from '../../shared/network/proxySettings' import { notificationSoundPreferencesPatchSchema } from '../../shared/notifications/notificationSound' @@ -121,6 +122,7 @@ export const lexoraConfigPatchSchema: z.ZodType = z.object({ speech: speechPreferencesSchema.optional(), pluginAuthor: extensionAuthorSchema.optional(), language: z.enum(['zh-CN', 'en-US']).optional(), + exitBehavior: desktopExitBehaviorSchema.optional(), launchAtLogin: z.boolean().optional(), notificationsEnabled: z.boolean().optional(), notificationSound: notificationSoundPreferencesPatchSchema.optional(), diff --git a/apps/buddy/electron/shared/localChatApi.ts b/apps/buddy/electron/shared/localChatApi.ts index 984b7835d..4c5444bcb 100644 --- a/apps/buddy/electron/shared/localChatApi.ts +++ b/apps/buddy/electron/shared/localChatApi.ts @@ -6,7 +6,7 @@ import type { ChangeOverviewRequest, ChangeRecoveryListRequest, ChangeRecoveryRe import type { ConnectorChangeNotice, LocalConnector, LocalConnectorConfig, LocalConnectorCredential, LocalConnectorCredentialMutation } from '../../shared/connectors/connectorApi' import type { ConnectorRuntimeState, ConnectorToolSummary } from '../../shared/connectors/connectorState' import type { LocalChatCommandRequest, LocalStartTurnRequest, LocalTurnStart } from '../../shared/conversation/chatApi' -import type { LocalChatQueueItem, LocalChatQueueReceipt, LocalChatQueueScope, LocalChatQueueSteerResult, LocalChatQueueTarget } from '../../shared/conversation/chatQueueApi' +import type { LocalChatQueueItem, LocalChatQueueReceipt, LocalChatQueueScope, LocalChatQueueSteerResult, LocalChatQueueSteerTarget, LocalChatQueueTarget } from '../../shared/conversation/chatQueueApi' import type { ComposerResourcesChanged, LocalComposerDraft, LocalComposerDraftDiscard, LocalComposerDraftOpen, LocalComposerDraftSave } from '../../shared/conversation/composerApi' import type { BuddyComposerResource, @@ -19,14 +19,15 @@ import type { BuddyComposerSpaceFileSelect, } from '../../shared/conversation/composerResource' import type { LocalContextUsageSnapshot, LocalContextUsageSnapshotRequest } from '../../shared/conversation/contextApi' -import type { ConversationListFilter, ConversationModelControls, ConversationTimelineChangeNotice, LocalConversation, LocalConversationBranch, LocalConversationMember, LocalConversationSummary, LocalConversationTimelinePage, LocalMessagePage } from '../../shared/conversation/conversationApi' +import type { ConversationListFilter, ConversationModelControls, ConversationModelMutationTarget, ConversationTimelineChangeNotice, LocalConversation, LocalConversationBranch, LocalConversationMember, LocalConversationSummary, LocalConversationTimelinePage, LocalMessagePage } from '../../shared/conversation/conversationApi' import type { ConversationArchivePage, ConversationArchivePolicy, ConversationArchiveQuery } from '../../shared/conversation/conversationArchiveApi' import type { ConversationHistoryQuery, LocalConversationHistoryItem, LocalConversationHistoryPage } from '../../shared/conversation/conversationHistoryApi' import type { ConversationNodeDetailRequest, LocalConversationTree } from '../../shared/conversation/conversationTree' +import type { DraftRecoveryApi } from '../../shared/conversation/draftRecovery' import type { LocalTaskMark, LocalTaskMarkState, TaskMarkClearInput, TaskMarkInput, TaskMarkReadInput } from '../../shared/conversation/taskMarkApi' import type { LocalWorkspaceSetting, LocalWorkspaceStateValue } from '../../shared/conversation/workspaceApi' import type { LocalExecutionInputsPage } from '../../shared/executions/executionInputReceipt' -import type { ExecutionAnswer, ExecutionEventsRequest, ExecutionInputsRequest, ExecutionsChanged, ExecutionScope, ExecutionSend, ExecutionSendDraft, ExecutionTarget, LocalExecution, LocalExecutionDetail, LocalExecutionEventsPage } from '../../shared/executions/executionPresentationApi' +import type { ExecutionActivityRequest, ExecutionAnswer, ExecutionEventsRequest, ExecutionInputsRequest, ExecutionsChanged, ExecutionScope, ExecutionSend, ExecutionSendDraft, ExecutionTarget, LocalExecution, LocalExecutionActivity, LocalExecutionDetail, LocalExecutionEventsPage } from '../../shared/executions/executionPresentationApi' import type { DirectoryPage, FilePreview } from '../../shared/files/filePreview' import type { TaskFile, TaskFileLink, TaskFileTarget } from '../../shared/files/taskFileApi' import type { WebSettings, WebSettingsSnapshot } from '../../shared/network/webProtocol' @@ -34,6 +35,7 @@ import type { LocalNotificationList, NotificationsChanged } from '../../shared/n import type { LocalApproval } from '../../shared/permissions/approvalApi' import type { ApprovalGrantScope } from '../../shared/permissions/approvalReviewPayload' import type { BuddyPermissionSettings } from '../../shared/permissions/permissionMode' +import type { PrivateNetworkManagementApi } from '../../shared/privateNetwork/management' import type { BuiltinPromptCatalog } from '../../shared/prompts/promptApi' import type { LocalBuiltinProviderPreset, LocalCustomProvider, LocalCustomProviderModel, LocalDefaultModel, LocalModelSnapshot, LocalProvider, LocalProviderAuthChallenge, LocalRuntimeModelOption, ProviderChanged } from '../../shared/providers/providerApi' import type { ModelCapabilityOverrides } from '../../shared/providers/providerCapabilities' @@ -43,6 +45,7 @@ import type { ProviderModelType } from '../../shared/providers/providerModelType import type { UserQuestionRequest, UserQuestionsChanged, UserQuestionSubmit, UserQuestionTarget } from '../../shared/questions/userQuestionApi' import type { LocalRun, LocalRunEvent } from '../../shared/runs/runApi' import type { LocalTaskMetrics, TaskMetricsChanged } from '../../shared/runs/taskMetricsApi' +import type { ProductRecoveryItem } from '../../shared/runtime/productRecovery' import type { LocalBuddyServiceSupervisorState } from '../../shared/runtime/serviceState' import type { LocalSkillCatalog, SkillChangeNotice, SkillDetail, SkillDirectoryRequest, SkillFileTarget, SkillInstallPreview, SkillPreviewInput } from '../../shared/skills/skillApi' @@ -54,11 +57,15 @@ import type { LocalUsageAnalytics, LocalUsageTopTasks, LocalUsageTrend, UsagePer import type { LocalUsageSnapshot } from '../../shared/usage/usageApi' export const LOCAL_CHAT_IPC_CHANNELS = { + draftRecoveryLoad: 'lexora:local-chat:draft-recovery:load', + draftRecoveryWrite: 'lexora:local-chat:draft-recovery:write', + draftRecoveryRemove: 'lexora:local-chat:draft-recovery:remove', questionsList: 'lexora:buddy:questions:list', questionsAnswer: 'lexora:buddy:questions:answer', questionsCancel: 'lexora:buddy:questions:cancel', questionsRetry: 'lexora:buddy:questions:retry', questionsChanged: 'lexora:buddy:questions:changed', + executionsActivity: 'lexora:local-chat:executions:activity', executionsList: 'lexora:local-chat:executions:list', executionsDetail: 'lexora:local-chat:executions:detail', executionsInputs: 'lexora:local-chat:executions:inputs', @@ -169,7 +176,6 @@ export const LOCAL_CHAT_IPC_CHANNELS = { connectorsSetCredential: 'lexora:buddy:connectors:set-credential', connectorsConfirmExecution: 'lexora:buddy:connectors:confirm-execution', connectorsUpsert: 'lexora:buddy:connectors:upsert', - conversationsDeletePermanently: 'lexora:buddy:conversations:delete-permanently', conversationsArchive: 'lexora:buddy:conversations:archive', conversationsRestoreArchive: 'lexora:buddy:conversations:restore-archive', conversationsDeleteArchived: 'lexora:buddy:conversations:delete-archived', @@ -253,6 +259,9 @@ export const LOCAL_CHAT_IPC_CHANNELS = { runtimeRestart: 'lexora:buddy:runtime:restart', runtimeStateChanged: 'lexora:buddy:runtime:state-changed', runtimeStatus: 'lexora:buddy:runtime:status', + runtimeRecoveryList: 'lexora:buddy:runtime:recovery:list', + runtimeRecoveryAcknowledge: 'lexora:buddy:runtime:recovery:acknowledge', + runtimeRecoveryRestore: 'lexora:buddy:runtime:recovery:restore', skillsList: 'lexora:buddy:skills:list', skillsGet: 'lexora:buddy:skills:get', skillsListFiles: 'lexora:buddy:skills:list-files', @@ -280,6 +289,7 @@ interface LocalMutationResult { } export interface LocalChatApi { + draftRecovery?: DraftRecoveryApi questions: { list: (input: ExecutionScope) => Promise answer: (input: UserQuestionSubmit) => Promise @@ -288,6 +298,7 @@ export interface LocalChatApi { onChanged: (listener: (event: UserQuestionsChanged) => void) => () => void } executions: { + activity: (input: ExecutionActivityRequest) => Promise list: (input: ExecutionScope) => Promise detail: (input: ExecutionTarget) => Promise inputs: (input: ExecutionInputsRequest) => Promise @@ -314,10 +325,10 @@ export interface LocalChatApi { taskMarks: { list: () => Promise create: (input: TaskMarkInput) => Promise - update: (input: TaskMarkInput & { id: string }) => Promise - delete: (id: string) => Promise + update: (input: TaskMarkInput & { id: string, expectedRevision?: number }) => Promise + delete: (id: string, expectedRevision?: number) => Promise states: (conversationIds: readonly string[]) => Promise - assign: (conversationId: string, markId: string | null) => Promise + assign: (conversationId: string, markId: string | null, expectedAssignmentRevision?: number) => Promise setRead: (input: TaskMarkReadInput) => Promise clear: (input: TaskMarkClearInput) => Promise } @@ -353,7 +364,11 @@ export interface LocalChatApi { prompts: { get: () => Promise } + privateNetwork: PrivateNetworkManagementApi runtime: { + listRecovery: () => Promise + acknowledgeRecovery: (id: string) => Promise + restoreRecovery: (id: string) => Promise getStatus: () => Promise restart: () => Promise onStateChanged: (listener: (state: LocalBuddyServiceSupervisorState) => void) => () => void @@ -517,15 +532,15 @@ export interface LocalChatApi { getHistoryEntries: (ids: readonly string[]) => Promise get: (conversationId: string) => Promise getModelControls: (conversationId: string, actorId?: string) => Promise - deletePermanently: (conversationId: string, expectedRevision: number) => Promise archive: (conversationId: string, expectedRevision: number) => Promise restoreArchive: (conversationId: string, expectedRevision: number) => Promise deleteArchived: (conversationId: string, expectedRevision: number) => Promise listArchived: (query: ConversationArchiveQuery) => Promise getArchivePolicy: () => Promise setArchivePolicy: (days: ConversationArchivePolicy['days']) => Promise - delete: (conversationId: string) => Promise + delete: (conversationId: string, expectedRevision?: number) => Promise activateBranch: (input: { + expectedRevision?: number branchId: string conversationId: string }) => Promise @@ -536,20 +551,21 @@ export interface LocalChatApi { cursor?: string limit?: number }) => Promise - rename: (conversationId: string, title: string) => Promise + rename: (conversationId: string, title: string, expectedTitleRevision?: number) => Promise setPermissionSettings: ( conversationId: string, settings: BuddyPermissionSettings, + expectedRevision?: number, ) => Promise listMembers: (conversationId: string) => Promise - addMember: (conversationId: string, buddyId: string) => Promise - removeMember: (conversationId: string, buddyId: string) => Promise - setBuddy: (conversationId: string, buddyId: string) => Promise - moveToSpace: (conversationId: string, spaceId: string) => Promise + addMember: (conversationId: string, buddyId: string, expectedRevision?: number) => Promise + removeMember: (conversationId: string, buddyId: string, expectedRevision?: number) => Promise + setBuddy: (conversationId: string, buddyId: string, expectedRevision?: number) => Promise + moveToSpace: (conversationId: string, spaceId: string, expectedRevision?: number) => Promise setModelSelection: ( conversationId: string, modelSelection: LocalConversation['modelSelection'], - target?: { actorId?: string, runId?: string }, + target?: ConversationModelMutationTarget, ) => Promise listTimeline: (input: { branchId?: string @@ -594,8 +610,8 @@ export interface LocalChatApi { runId?: string | null status?: 'pending' | 'approved' | 'denied' | 'cancelled' | null }) => Promise> - approve: (approvalId: string, scope: ApprovalGrantScope) => Promise - deny: (approvalId: string) => Promise + approve: (approvalId: string, scope: ApprovalGrantScope, conditions?: { runId: string, reviewVersion: string }) => Promise + deny: (approvalId: string, conditions?: { runId: string, reviewVersion: string }) => Promise } composerResources: { readText: (input: BuddyComposerResourceTarget) => Promise @@ -620,7 +636,7 @@ export interface LocalChatApi { enqueue: (input: LocalStartTurnRequest) => Promise listQueue: (input: LocalChatQueueScope) => Promise cancelQueued: (input: LocalChatQueueTarget) => Promise - steerQueued: (input: LocalChatQueueTarget) => Promise + steerQueued: (input: LocalChatQueueSteerTarget) => Promise editUserMessage: (input: { conversationId: string draftId: string @@ -635,7 +651,7 @@ export interface LocalChatApi { requestId: string sourceRunId: string }) => Promise - cancel: (runId: string) => Promise + cancel: (runId: string, expectedRunGeneration?: number) => Promise onRunEvent: (listener: (event: LocalRunEvent) => void) => () => void } } diff --git a/apps/buddy/electron/shared/privateNetworkApi.ts b/apps/buddy/electron/shared/privateNetworkApi.ts new file mode 100644 index 000000000..412707b82 --- /dev/null +++ b/apps/buddy/electron/shared/privateNetworkApi.ts @@ -0,0 +1 @@ +export const PRIVATE_NETWORK_IPC_PREFIX = 'lexora:buddy:private-network:' diff --git a/apps/buddy/eslint/__tests__/dependency-boundaries.spec.mjs b/apps/buddy/eslint/__tests__/dependency-boundaries.spec.mjs deleted file mode 100644 index 3c6d162b6..000000000 --- a/apps/buddy/eslint/__tests__/dependency-boundaries.spec.mjs +++ /dev/null @@ -1,127 +0,0 @@ -import assert from 'node:assert/strict' -import path from 'node:path' -import { fileURLToPath } from 'node:url' -import { ESLint, Linter } from 'eslint' -import { it } from 'vitest' -import rule from '../dependency-boundaries.js' - -const root = fileURLToPath(new URL('../../', import.meta.url)) -const eslint = new ESLint() -const tsConfig = await eslint.calculateConfigForFile(path.join(root, 'src/main.ts')) -const vueConfig = await eslint.calculateConfigForFile(path.join(root, 'src/App.vue')) -const linter = new Linter() - -function lint(filename, code) { - const configuration = filename.endsWith('.vue') ? vueConfig : tsConfig - return linter.verify(code, { - files: ['**/*.{ts,js,mjs,cjs,vue}'], - languageOptions: { - ...configuration.languageOptions, - parserOptions: { ...configuration.languageOptions.parserOptions, project: false, projectService: false }, - }, - plugins: { buddy: { rules: { boundaries: rule } } }, - rules: { 'buddy/boundaries': 'error' }, - }, { filename: path.join(root, filename) }) -} - -const cases = [ - ['workbench cannot depend on app composition', 'src/workbench/browser/View.ts', 'import { root } from \'@/app/bootstrap/root\'', 'workbenchToBusiness'], - ['workbench cannot depend on contributions', 'src/workbench/common/Resource.ts', 'import type { Task } from \'@/modules/tasks\'', 'workbenchToBusiness'], - ['workbench services cannot depend on Vue', 'src/workbench/services/Controller.ts', 'import { ref } from \'vue\'', 'workbenchCoreHost'], - ['workbench services cannot depend on browser components', 'src/workbench/services/Controller.ts', 'import { Pane } from \'../browser/Pane\'', 'workbenchCoreHost'], - ['workbench services cannot depend on host APIs', 'src/workbench/services/Controller.ts', 'import type { API } from \'@buddy-electron/shared/desktopApi\'', 'workbenchCoreHost'], - ['module cannot import app through alias', 'src/modules/tasks/state/useTasks.ts', 'import { app } from \'@/app/desktopAppContext\'', 'moduleToApp'], - ['module cannot import app through relative path', 'src/modules/tasks/state/useTasks.ts', 'import { app } from \'../../../app/desktopAppContext\'', 'moduleToApp'], - ['normalized alias cannot bypass boundary', 'src/modules/tasks/state/useTasks.ts', 'import { app } from \'@/modules/tasks/../../app/desktopAppContext\'', 'moduleToApp'], - ['app cannot reach module private state', 'src/app/bootstrap/start.ts', 'import { state } from \'@/modules/tasks/state/useTasks\'', 'privateModule'], - ['cross module type imports respect public entry', 'src/modules/settings/state/settings.ts', 'import type { Task } from \'../../tasks/state/useTasks\'', 'privateModule'], - ['nested index is private', 'src/modules/settings/state/settings.ts', 'export { task } from \'@/modules/tasks/state/index.ts\'', 'privateModule'], - ['export all respects boundary', 'src/modules/settings/state/settings.ts', 'export * from \'@/modules/tasks/widgets/private\'', 'privateModule'], - ['dynamic imports respect boundary', 'src/app/router/index.ts', 'const page = import(\'@/modules/tasks/pages/TasksPage.vue\')', 'privateModule'], - ['static template imports respect boundary', 'src/app/router/index.ts', 'const page = import(`@/modules/tasks/pages/TasksPage.vue`)', 'privateModule'], - ['require respects boundary in mjs', 'src/app/shell/action.mjs', 'const task = require(\'@/modules/tasks/state/useTasks\')', 'privateModule'], - ['import type expressions respect boundary', 'src/modules/settings/state/settings.ts', 'type Task = import(\'@/modules/tasks/state/useTasks\').Task', 'privateModule'], - ['TypeScript import equals respects boundary', 'src/modules/settings/state/settings.ts', 'import task = require(\'@/modules/tasks/state/useTasks\')', 'privateModule'], - ['model cannot import state types', 'src/modules/tasks/model/rows.ts', 'import type { State } from \'../state/run\'', 'modelToPresentation'], - ['model cannot import a state directory', 'src/modules/tasks/model/rows.ts', 'import { state } from \'../state\'', 'modelToPresentation'], - ['model cannot import widgets', 'src/modules/tasks/model/rows.ts', 'import { rows } from \'../widgets/transcript/rows\'', 'modelToPresentation'], - ['model cannot import a public UI entry', 'src/modules/tasks/model/rows.ts', 'import { ModelSelector } from \'@/modules/models/ui\'', 'modelToPresentation'], - ['model cannot import a public routes entry', 'src/modules/tasks/model/rows.ts', 'import { routes } from \'@/modules/settings/routes.ts\'', 'modelToPresentation'], - ['model cannot import Vue runtime', 'src/modules/tasks/model/rows.ts', 'import { computed } from \'vue\'', 'modelRuntime'], - ['state cannot import widget types', 'src/modules/tasks/state/tasks.ts', 'import type { Resource } from \'../widgets/composer/typing\'', 'stateToPresentation'], - ['state cannot load component runtime', 'src/modules/tasks/state/tasks.ts', 'const Icon = import(\'@/shared/ui/icon/DesktopIcon.vue\')', 'stateToPresentation'], - ['state cannot load public UI entry', 'src/modules/tasks/state/tasks.ts', 'import { Selector } from \'@/modules/models/ui\'', 'stateToPresentation'], - ['state cannot re-export routes', 'src/modules/tasks/state/tasks.ts', 'export * from \'../routes.ts\'', 'stateToPresentation'], - ['model cannot re-export Vue runtime', 'src/modules/tasks/model/rows.ts', 'export { computed } from \'vue\'', 'modelRuntime'], - ['model cannot import mixed Vue values and types', 'src/modules/tasks/model/rows.ts', 'import { type Ref, computed } from \'vue\'', 'modelRuntime'], - ['model cannot load Naive runtime', 'src/modules/tasks/model/rows.ts', 'const ui = import(\'naive-ui\')', 'modelRuntime'], - ['model cannot load editor runtime', 'src/modules/prompt-input/model/content.ts', 'import { Editor } from \'@tiptap/core\'', 'modelRuntime'], - ['domain shared cannot import Node', 'shared/contract.ts', 'import type { Buffer } from \'node:buffer\'', 'sharedHost'], - ['domain shared cannot import bare builtins', 'shared/contract.mjs', 'import { readFile } from \'fs/promises\'', 'sharedHost'], - ['domain shared cannot import Electron', 'shared/contract.ts', 'import type { BrowserWindow } from \'electron\'', 'sharedHost'], - ['domain shared cannot import platform files', 'shared/contract.ts', 'import definitions from \'../platform/definitions.json\'', 'sharedHost'], - ['domain shared cannot import service files', 'shared/contract.ts', 'export type { Run } from \'../service/runtime/run\'', 'sharedHost'], - ['domain shared cannot import Electron alias', 'shared/contract.ts', 'export type { Api } from \'@buddy-electron/shared/desktopApi\'', 'sharedHost'], - ['domain shared cannot import renderer alias', 'shared/contract.ts', 'import { helper } from \'@/shared/lib/helper\'', 'sharedHost'], - ['platform cannot import renderer', 'platform/process/process.ts', 'import { state } from \'@/shared/state\'', 'platformToHost'], - ['platform cannot import Electron host', 'platform/ipc/transport.ts', 'import type { Host } from \'../../electron/main/host\'', 'platformToHost'], - ['platform cannot import runtime service', 'platform/filesystem/file.ts', 'export { files } from \'../../service/files\'', 'platformToHost'], - ['platform cannot load Electron runtime', 'platform/native/host.mjs', 'const electron = require(\'electron\')', 'platformToHost'], - ['Vue SFC imports respect module boundary', 'src/modules/tasks/widgets/Task.vue', '', 'moduleToApp'], -] - -for (const layer of ['shared', 'platform', 'i18n', 'theme']) { - cases.push([`${layer} cannot depend on modules`, `src/${layer}/test.ts`, 'import { tasks } from \'@/modules/tasks\'', 'foundationToBusiness']) - cases.push([`${layer} cannot depend on app`, `src/${layer}/test.ts`, 'import type { App } from \'@/app/desktopAppContext\'', 'foundationToBusiness']) -} - -for (const [name, filename, code, messageId] of cases) { - it(name, () => { - const messages = lint(filename, code) - assert.equal(messages.length, 1, JSON.stringify(messages)) - assert.equal(messages[0].messageId, messageId, JSON.stringify(messages)) - }) -} - -it('module public entries permit app and cross module consumers', () => { - for (const entry of ['', '/index', '/index.ts', '/contracts.ts', '/ui', '/routes.ts']) { - const code = `import { value } from '@/modules/tasks${entry}'` - assert.deepEqual(lint('src/app/bootstrap/start.ts', code), []) - assert.deepEqual(lint('src/modules/settings/widgets/Settings.ts', code), []) - } -}) - -it('module can use its own private implementation', () => { - assert.deepEqual(lint('src/modules/tasks/state/useTasks.ts', 'import { rows } from \'../model/rows\''), []) - assert.deepEqual(lint('src/modules/tasks/widgets/Task.ts', 'import { tasks } from \'@/modules/tasks/state/useTasks\''), []) -}) - -it('state can use reactive owners, domain types and public state entries', () => { - for (const dependency of ['vue', '@/modules/models', '@/modules/settings/contracts', '../composer/typing', '../model/typing']) - assert.deepEqual(lint('src/modules/tasks/state/useTasks.ts', `import { value } from '${dependency}'`), []) -}) - -it('model can use Vue and Tiptap type-only imports', () => { - for (const code of [ - 'import type { Ref } from \'vue\'', - 'import { type Ref } from \'vue\'', - 'export { type Ref } from \'vue\'', - 'type Ref = import(\'vue\').Ref', - 'import type { JSONContent } from \'@tiptap/core\'', - ]) { - assert.deepEqual(lint('src/modules/tasks/model/rows.ts', code), []) - } -}) - -it('shared domain contracts can depend on each other and neutral libraries', () => { - assert.deepEqual(lint('shared/content.ts', 'import { schema } from \'./contract\'; import { z } from \'zod\''), []) - assert.deepEqual(lint('shared/content.ts', 'import { schema } from \'@buddy-shared/contract\''), []) -}) - -it('test fixtures may exercise another layer without changing production boundaries', () => { - assert.deepEqual(lint('shared/__tests__/content.spec.ts', 'import { readFile } from \'node:fs\''), []) -}) - -it('platform can use Node, domain shared and its own implementation', () => { - assert.deepEqual(lint('platform/filesystem/paths.ts', 'import path from \'node:path\'; import { space } from \'@buddy-shared/space\'; import { kind } from \'../currentPlatform\''), []) -}) diff --git a/apps/buddy/eslint/dependency-boundaries.js b/apps/buddy/eslint/dependency-boundaries.js index 9890a5444..c6d883e6f 100644 --- a/apps/buddy/eslint/dependency-boundaries.js +++ b/apps/buddy/eslint/dependency-boundaries.js @@ -14,6 +14,7 @@ const foundationLayers = new Set(['shared', 'platform', 'i18n', 'theme']) const modelForbiddenLayers = new Set(['state', 'widgets', 'pages', 'layouts', 'components', 'editor']) const stateForbiddenLayers = new Set(['widgets', 'pages', 'layouts', 'components', 'editor']) const publicEntry = /^(?:index|contracts|ui|routes)(?:\.[cm]?[jt]s)?$/ +const readerModules = new Set(['tasks', 'buddies']) function partsWithin(root, filename) { const relative = path.relative(root, filename) @@ -55,11 +56,12 @@ export default { messages: { moduleToApp: '业务模块不能依赖 app;通过模块 Context、props 或 action 接收应用能力。', foundationToBusiness: '{{layer}} 不能反向依赖 app 或 modules;将业务逻辑归回所属模块。', - privateModule: '外部只能通过 {{module}} 的 index、contracts、ui 或 routes 公共入口使用模块。', + privateModule: '外部只能通过 {{module}} 的公共入口使用模块;reader 入口限独立阅读器。', modelToPresentation: 'model 不能依赖 {{layer}};将纯类型和计算归入 model,将交互或状态逻辑移出 model。', modelRuntime: 'model 不能运行时依赖 {{dependency}};Vue 和编辑器类型使用 import type。', stateToPresentation: 'state 不能依赖展示层 {{dependency}};业务状态与公共类型归 state/model,DOM 和编辑器交互归 widget。', sharedHost: 'apps/buddy/shared 是无宿主依赖的领域契约,不能依赖 {{dependency}}。', + serviceToHost: 'apps/buddy/service 是独立 Node 运行时,不能依赖 {{dependency}}。', platformToHost: 'apps/buddy/platform 是进程共用的底层实现,不能反向依赖 {{dependency}}。', workbenchToBusiness: 'workbench 通过注册表接收业务贡献,不能依赖 app 或 modules。', workbenchCoreHost: 'workbench common/services 不依赖 UI 或宿主,通过接口注入能力。', @@ -103,6 +105,13 @@ export default { report('platformToHost', { dependency: specifier }) return } + if (source[0] === 'service' && ( + ['src', 'electron'].includes(target?.[0]) + || /^(?:electron|electron-vite|vue|vue-router|naive-ui)(?:\/|$)/.test(specifier) + )) { + report('serviceToHost', { dependency: specifier }) + return + } if (!renderer) return if (renderer[0] === 'workbench') { @@ -149,7 +158,8 @@ export default { } if (targetModule && module !== targetModule) { const entry = targetRenderer.slice(2) - if (entry.length > 1 || (entry.length === 1 && !publicEntry.test(entry[0]))) { + const readerEntry = renderer[0] === 'mobile-reader' && readerModules.has(targetModule) && /^reader(?:\.[cm]?[jt]s)?$/.test(entry[0]) + if (entry.length > 1 || (entry.length === 1 && !publicEntry.test(entry[0]) && !readerEntry)) { report('privateModule', { module: targetModule }) return } diff --git a/apps/buddy/mobile-reader.vite.config.ts b/apps/buddy/mobile-reader.vite.config.ts new file mode 100644 index 000000000..1a5ddfdee --- /dev/null +++ b/apps/buddy/mobile-reader.vite.config.ts @@ -0,0 +1,48 @@ +import { fileURLToPath } from 'node:url' +import vue from '@vitejs/plugin-vue' +import UnoCSS from 'unocss/vite' +import { defineConfig } from 'vite' +import classicDark from './extensions/bundled/lexora.themes/themes/classic-dark.json' +import classicLight from './extensions/bundled/lexora.themes/themes/classic-light.json' +import { resolveTheme } from './shared/theme/resolveTheme' +import { themeDocumentSchema } from './shared/theme/themeDocument' +import { themeFonts } from './shared/theme/themeTokens' +import { markdownThemeVariables } from './src/theme/markdownTheme' +import unoConfig from './uno.config' + +const appearance = { + typography: { bodySize: 15, bodyLineHeight: 24 }, + ...Object.fromEntries((['light', 'dark'] as const).map((mode) => { + const theme = resolveTheme({ id: `mobile-${mode}`, label: 'Lexora', appearance: mode, source: 'fallback', extensionId: null, packageName: '', swatch: '' }, themeDocumentSchema.parse(mode === 'dark' ? classicDark : classicLight)) + return [mode, { + colors: theme.colors, + variables: { ...theme.variables, ...markdownThemeVariables(theme.colors), '--buddy-font-ui': themeFonts.ui, '--buddy-font-body': themeFonts.ui, '--buddy-font-mono': themeFonts.mono }, + }] + })), +} + +export default defineConfig({ + root: fileURLToPath(new URL('./src/mobile-reader', import.meta.url)), + base: './', + plugins: [vue(), UnoCSS(unoConfig), { + name: 'mobile-appearance', + generateBundle() { + this.emitFile({ type: 'asset', fileName: 'appearance.json', source: JSON.stringify(appearance) }) + }, + }], + resolve: { + alias: { + '@': fileURLToPath(new URL('./src', import.meta.url)), + '@buddy-shared': fileURLToPath(new URL('./shared', import.meta.url)), + '@buddy-electron': fileURLToPath(new URL('./electron', import.meta.url)), + }, + }, + build: { + rollupOptions: { input: { reader: fileURLToPath(new URL('./src/mobile-reader/index.html', import.meta.url)), document: fileURLToPath(new URL('./src/mobile-reader/document.html', import.meta.url)) } }, + target: 'chrome83', + assetsInlineLimit: 0, + cssTarget: 'chrome83', + outDir: fileURLToPath(new URL('./.output/build/mobile-reader', import.meta.url)), + emptyOutDir: true, + }, +}) diff --git a/apps/buddy/native/host/Cargo.toml b/apps/buddy/native/host/Cargo.toml index ad06e086d..099909bec 100644 --- a/apps/buddy/native/host/Cargo.toml +++ b/apps/buddy/native/host/Cargo.toml @@ -58,6 +58,14 @@ path = "__tests__/bounded_file_linux.rs" name = "bounded-file-macos" path = "__tests__/bounded_file_macos.rs" +[[test]] +name = "service-launcher" +path = "__tests__/service_launcher.rs" + +[[test]] +name = "process-identity-macos" +path = "__tests__/process_identity_macos.rs" + [[test]] name = "process-control-linux" path = "__tests__/process_control_linux.rs" diff --git a/apps/buddy/native/host/__tests__/process_identity_macos.rs b/apps/buddy/native/host/__tests__/process_identity_macos.rs new file mode 100644 index 000000000..f156791d8 --- /dev/null +++ b/apps/buddy/native/host/__tests__/process_identity_macos.rs @@ -0,0 +1,33 @@ +#![cfg(target_os = "macos")] + +#[test] +fn reads_stable_identity_and_refuses_exited_processes() { + let mut child = std::process::Command::new("/bin/sleep") + .arg("20") + .spawn() + .unwrap(); + let request = serde_json::to_vec( + &serde_json::json!({ "operation": "read", "pid": child.id(), "protectedPids": [] }), + ) + .unwrap(); + let mut first = Vec::new(); + lexora_buddy_host::process_control::run(request.as_slice(), &mut first).unwrap(); + let mut second = Vec::new(); + lexora_buddy_host::process_control::run(request.as_slice(), &mut second).unwrap(); + assert_eq!(first, second); + let identity: serde_json::Value = serde_json::from_slice(&first).unwrap(); + assert_eq!(identity[0]["pid"], child.id()); + assert!( + identity[0]["instanceId"] + .as_str() + .unwrap() + .parse::() + .unwrap() + > 0 + ); + child.kill().unwrap(); + child.wait().unwrap(); + let mut exited = Vec::new(); + lexora_buddy_host::process_control::run(request.as_slice(), &mut exited).unwrap(); + assert_eq!(exited, b"[]"); +} diff --git a/apps/buddy/native/host/__tests__/runtime_guard.windows.mjs b/apps/buddy/native/host/__tests__/runtime_guard.windows.mjs new file mode 100644 index 000000000..8362c3b20 --- /dev/null +++ b/apps/buddy/native/host/__tests__/runtime_guard.windows.mjs @@ -0,0 +1,51 @@ +import assert from 'node:assert/strict' +import { spawn } from 'node:child_process' +import { once } from 'node:events' +import path from 'node:path' +import process from 'node:process' +import test from 'vitest' + +const executable = process.env.LEXORA_TEST_RUNTIME_GUARD ?? path.resolve(import.meta.dirname, '../../target/debug/lexora-buddy-runtime-guard.exe') + +test('guard confirms that the terminated runtime and its child have left the private Job', { skip: process.platform !== 'win32', timeout: 30_000 }, async (context) => { + const target = spawn(process.execPath, ['-e', ` + process.stdin.once('data', () => { + const child = require('node:child_process').spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { stdio: 'ignore' }) + child.once('spawn', () => process.stdout.write(String(child.pid) + '\\n')) + }) + setInterval(() => {}, 1000) + `], { stdio: ['pipe', 'pipe', 'pipe'], windowsHide: true }) + await once(target, 'spawn') + context.after(() => target.kill()) + const guard = spawn(executable, [], { stdio: ['pipe', 'pipe', 'pipe'], windowsHide: true }) + context.after(() => guard.kill()) + const guardExit = once(guard, 'exit') + let output = '' + let stderr = '' + guard.stdout.on('data', (chunk) => { + output += chunk + }) + guard.stderr.on('data', (chunk) => { + stderr += chunk + }) + guard.stdin.end(JSON.stringify({ pid: target.pid, observeCleanup: true })) + await waitUntil(() => output.includes('ready\n'), () => `Guard did not become ready: ${stderr}`) + target.stdin.end('spawn') + const [chunk] = await once(target.stdout, 'data') + const descendant = Number(String(chunk).trim()) + assert(Number.isInteger(descendant) && descendant > 1) + target.kill() + const [code] = await guardExit + assert.equal(code, 0, stderr) + assert.equal(output, 'ready\nclean\n') + assert.throws(() => process.kill(descendant, 0), error => error.code === 'ESRCH') +}) + +async function waitUntil(condition, failure) { + const deadline = Date.now() + 10_000 + while (!condition()) { + if (Date.now() >= deadline) + assert.fail(failure()) + await new Promise(resolve => setTimeout(resolve, 20)) + } +} diff --git a/apps/buddy/native/host/__tests__/service_launcher.rs b/apps/buddy/native/host/__tests__/service_launcher.rs new file mode 100644 index 000000000..8c42e33d4 --- /dev/null +++ b/apps/buddy/native/host/__tests__/service_launcher.rs @@ -0,0 +1,40 @@ +#![cfg(unix)] +use std::{fs, os::unix::fs::PermissionsExt, process::Command}; + +#[test] +fn service_launch_clears_injection_and_provider_environment_before_the_runtime() { + let root = tempfile::tempdir().unwrap(); + let entry = root.path().join("runtime.sh"); + fs::write(&entry, "test -z \"$NODE_OPTIONS$NODE_PATH$ELECTRON_RUN_AS_NODE$OPENAI_API_KEY$LD_PRELOAD\" || exit 11\ntest \"$1\" = --config || exit 12\nprintf ready\n").unwrap(); + let config = root.path().join("launch.json"); + fs::write( + &config, + serde_json::to_vec(&serde_json::json!({ "nodeExecutable": "/bin/sh", "coreEntry": entry })) + .unwrap(), + ) + .unwrap(); + fs::set_permissions(&config, fs::Permissions::from_mode(0o600)).unwrap(); + let result = Command::new(env!("CARGO_BIN_EXE_lexora-buddy-process-control")) + .arg("--launch-service") + .arg(&config) + .env("NODE_OPTIONS", "--import=invalid") + .env("NODE_PATH", "invalid") + .env("ELECTRON_RUN_AS_NODE", "1") + .env("OPENAI_API_KEY", "fixture-secret") + .output() + .unwrap(); + assert!( + result.status.success(), + "{}", + String::from_utf8_lossy(&result.stderr) + ); + assert_eq!(result.stdout, b"ready"); + fs::set_permissions(&config, fs::Permissions::from_mode(0o644)).unwrap(); + let result = Command::new(env!("CARGO_BIN_EXE_lexora-buddy-process-control")) + .arg("--launch-service") + .arg(&config) + .output() + .unwrap(); + assert!(!result.status.success()); + assert!(result.stdout.is_empty()); +} diff --git a/apps/buddy/native/host/src/bin/process_control.rs b/apps/buddy/native/host/src/bin/process_control.rs index 62a1f08f5..2fd0f1e1c 100644 --- a/apps/buddy/native/host/src/bin/process_control.rs +++ b/apps/buddy/native/host/src/bin/process_control.rs @@ -7,6 +7,19 @@ fn main() -> ExitCode { return ipc_result(lexora_buddy_host::sandbox_ipc::launch(arguments)); } if let Some(command) = arguments.next() { + if command == "--launch-service" { + let path = arguments.next(); + if let Some(path) = path.filter(|_| arguments.next().is_none()) { + return match lexora_buddy_host::service_launcher::run(std::path::Path::new(&path)) { + Ok(code) => ExitCode::from(code.clamp(0, 255) as u8), + Err(_) => { + eprintln!("SERVICE_LAUNCH_FAILED"); + ExitCode::FAILURE + } + }; + } + return ExitCode::FAILURE; + } #[cfg(any(windows, target_os = "linux"))] if matches!( command.to_str(), diff --git a/apps/buddy/native/host/src/lib.rs b/apps/buddy/native/host/src/lib.rs index 5edbb3089..470417a58 100644 --- a/apps/buddy/native/host/src/lib.rs +++ b/apps/buddy/native/host/src/lib.rs @@ -28,3 +28,5 @@ mod windows_security; #[cfg(windows)] pub mod installation_gate; + +pub mod service_launcher; diff --git a/apps/buddy/native/host/src/linux_update.rs b/apps/buddy/native/host/src/linux_update.rs index c982b5af4..061333ce6 100644 --- a/apps/buddy/native/host/src/linux_update.rs +++ b/apps/buddy/native/host/src/linux_update.rs @@ -17,6 +17,7 @@ use sha2::{Digest, Sha256}; const LOCK: &str = "/run/lexora-buddy-update.lock"; const EXECUTABLE: &str = "/opt/lexora-buddy/lexora-buddy"; +const CORE_EXECUTABLE: &str = "/opt/lexora-buddy/resources/lexora-runtime/node"; #[derive(Debug, Deserialize, Serialize)] #[serde(rename_all = "lowercase")] @@ -176,7 +177,7 @@ pub fn supervise() -> io::Result<()> { .stdin(Stdio::null()) .stdout(Stdio::null()) .stderr(Stdio::null()) - .spawn()?; + .status()?; } receipt } @@ -294,7 +295,9 @@ fn application_running() -> io::Result { .is_some_and(|value| value.parse::().is_ok()) { match fs::read_link(entry.path().join("exe")) { - Ok(path) if path == Path::new(EXECUTABLE) => return Ok(true), + Ok(path) if path == Path::new(EXECUTABLE) || path == Path::new(CORE_EXECUTABLE) => { + return Ok(true); + } Err(error) if error.kind() != io::ErrorKind::NotFound => return Err(error), _ => {} } diff --git a/apps/buddy/native/host/src/process_control.rs b/apps/buddy/native/host/src/process_control.rs index d3fe10ced..8eabd47f6 100644 --- a/apps/buddy/native/host/src/process_control.rs +++ b/apps/buddy/native/host/src/process_control.rs @@ -7,6 +7,8 @@ use serde::{Deserialize, Serialize}; #[cfg(target_os = "linux")] mod linux; +#[cfg(target_os = "macos")] +mod macos; #[cfg(any(windows, target_os = "linux", test))] mod policy; #[cfg(windows)] @@ -151,7 +153,11 @@ pub fn run(input: impl Read, output: impl Write) -> Result<(), ProcessError> { let mut output = output; output.write_all(&bytes).map_err(|_| ProcessError::Failed) } - #[cfg(not(any(windows, target_os = "linux")))] + #[cfg(target_os = "macos")] + { + macos::request(&request, output) + } + #[cfg(not(any(windows, target_os = "linux", target_os = "macos")))] { let _ = (request, output); Err(ProcessError::Unavailable) diff --git a/apps/buddy/native/host/src/process_control/macos.rs b/apps/buddy/native/host/src/process_control/macos.rs new file mode 100644 index 000000000..a0736b79a --- /dev/null +++ b/apps/buddy/native/host/src/process_control/macos.rs @@ -0,0 +1,117 @@ +use std::{ + io::{self, Write}, + mem::MaybeUninit, + num::NonZeroU32, +}; + +use serde::Serialize; + +use super::{ProcessError, Request, Selector}; + +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] +struct Identity { + pid: u32, + executable: String, + instance_id: String, +} + +pub(super) fn request(request: &Request, output: impl Write) -> Result<(), ProcessError> { + let pid = match request { + Request::Read { pid, .. } + | Request::Resolve { + selector: Selector::Pid { pid }, + .. + } => *pid, + _ => return Err(ProcessError::Unavailable), + }; + let identity = read_identity(pid)?; + serde_json::to_writer(output, identity.as_slice()).map_err(|_| ProcessError::Failed) +} + +fn read_identity(pid: NonZeroU32) -> Result, ProcessError> { + let Some(first) = read_info(pid)? else { + return Ok(None); + }; + let mut path = [0_u8; libc::PROC_PIDPATHINFO_MAXSIZE as usize]; + // SAFETY: The writable buffer has the exact supplied capacity and the PID fits libc's type. + let length = unsafe { + libc::proc_pidpath( + pid.get() as i32, + path.as_mut_ptr().cast(), + path.len() as u32, + ) + }; + if length <= 0 { + return if io::Error::last_os_error().raw_os_error() == Some(libc::ESRCH) { + Ok(None) + } else { + Err(ProcessError::Failed) + }; + } + let length = usize::try_from(length).map_err(|_| ProcessError::Failed)?; + if length >= path.len() { + return Err(ProcessError::Failed); + } + let executable = std::str::from_utf8(&path[..length]) + .map_err(|_| ProcessError::Failed)? + .trim_end_matches('\0') + .to_owned(); + let Some(second) = read_info(pid)? else { + return Ok(None); + }; + if first.pbi_start_tvsec != second.pbi_start_tvsec + || first.pbi_start_tvusec != second.pbi_start_tvusec + || first.pbi_uid != second.pbi_uid + || !std::path::Path::new(&executable).is_absolute() + { + return Err(ProcessError::TargetChanged); + } + let started = first + .pbi_start_tvsec + .checked_mul(1_000_000) + .and_then(|seconds| seconds.checked_add(first.pbi_start_tvusec)) + .ok_or(ProcessError::Failed)?; + Ok(Some(Identity { + pid: pid.get(), + executable, + instance_id: started.to_string(), + })) +} + +fn read_info(pid: NonZeroU32) -> Result, ProcessError> { + let pid = i32::try_from(pid.get()).map_err(|_| ProcessError::Invalid)?; + let mut info = MaybeUninit::::uninit(); + // SAFETY: proc_pidinfo writes at most the supplied size to an aligned, owned output buffer. + let written = unsafe { + libc::proc_pidinfo( + pid, + libc::PROC_PIDTBSDINFO, + 0, + info.as_mut_ptr().cast(), + size_of::() as i32, + ) + }; + if written != size_of::() as i32 { + return if written == 0 && io::Error::last_os_error().raw_os_error() == Some(libc::ESRCH) { + Ok(None) + } else { + Err(ProcessError::Failed) + }; + } + // SAFETY: The kernel returned the entire initialized proc_bsdinfo structure above. + let info = unsafe { info.assume_init() }; + // SAFETY: geteuid has no pointer arguments and only reads the calling process identity. + let uid = unsafe { libc::geteuid() }; + if info.pbi_status == libc::SZOMB { + return Ok(None); + } + if info.pbi_pid != pid as u32 + || info.pbi_start_tvsec == 0 + || info.pbi_start_tvusec >= 1_000_000 + || info.pbi_uid != uid + { + return Err(ProcessError::TargetChanged); + } + Ok(Some(info)) +} diff --git a/apps/buddy/native/host/src/runtime_guard.rs b/apps/buddy/native/host/src/runtime_guard.rs index b34709f2f..3ed04ca83 100644 --- a/apps/buddy/native/host/src/runtime_guard.rs +++ b/apps/buddy/native/host/src/runtime_guard.rs @@ -2,6 +2,7 @@ use std::{ io::{Read, Write}, os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle}, ptr, + time::{Duration, Instant}, }; use serde::Deserialize; @@ -10,8 +11,9 @@ use windows_sys::Win32::{ System::{ JobObjects::{ AssignProcessToJobObject, CreateJobObjectW, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE, - JOBOBJECT_EXTENDED_LIMIT_INFORMATION, JobObjectExtendedLimitInformation, - SetInformationJobObject, + JOBOBJECT_BASIC_ACCOUNTING_INFORMATION, JOBOBJECT_EXTENDED_LIMIT_INFORMATION, + JobObjectBasicAccountingInformation, JobObjectExtendedLimitInformation, + QueryInformationJobObject, SetInformationJobObject, TerminateJobObject, }, Threading::{ GetCurrentProcess, GetProcessTimes, INFINITE, OpenProcess, @@ -26,9 +28,11 @@ use windows_sys::Win32::{ pub struct GuardError; #[derive(Deserialize)] -#[serde(deny_unknown_fields)] +#[serde(rename_all = "camelCase", deny_unknown_fields)] struct Request { pid: u32, + #[serde(default)] + observe_cleanup: bool, } pub fn run() -> Result<(), GuardError> { @@ -60,26 +64,7 @@ pub fn run() -> Result<(), GuardError> { if creation_time(runtime.as_raw_handle())? >= guard_created { return Err(GuardError); } - // SAFETY: Null attributes/name create a private, non-inheritable job. - let job = owned(unsafe { CreateJobObjectW(ptr::null(), ptr::null()) })?; - let mut limits = JOBOBJECT_EXTENDED_LIMIT_INFORMATION::default(); - limits.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; - // SAFETY: The live job and initialized limits remain valid for the exact supplied struct size. - if unsafe { - SetInformationJobObject( - job.as_raw_handle(), - JobObjectExtendedLimitInformation, - ptr::from_ref(&limits).cast(), - size_of::() as u32, - ) - } == 0 - { - return Err(GuardError); - } - // SAFETY: Both handles are owned and live; Runtime waits for readiness before creating tools. - if unsafe { AssignProcessToJobObject(job.as_raw_handle(), runtime.as_raw_handle()) } == 0 { - return Err(GuardError); - } + let job = create_job(runtime.as_raw_handle())?; let mut output = std::io::stdout().lock(); output.write_all(b"ready\n").map_err(|_| GuardError)?; output.flush().map_err(|_| GuardError)?; @@ -87,6 +72,38 @@ pub fn run() -> Result<(), GuardError> { if unsafe { WaitForSingleObject(runtime.as_raw_handle(), INFINITE) } != WAIT_OBJECT_0 { return Err(GuardError); } + if request.observe_cleanup { + // SAFETY: This owned private job contains only the supervised runtime and its descendants. + if unsafe { TerminateJobObject(job.as_raw_handle(), 1) } == 0 { + return Err(GuardError); + } + let deadline = Instant::now() + Duration::from_secs(10); + loop { + let mut accounting = JOBOBJECT_BASIC_ACCOUNTING_INFORMATION::default(); + // SAFETY: The owned job is live and the output buffer has the exact declared size. + if unsafe { + QueryInformationJobObject( + job.as_raw_handle(), + JobObjectBasicAccountingInformation, + ptr::from_mut(&mut accounting).cast(), + size_of::() as u32, + ptr::null_mut(), + ) + } == 0 + { + return Err(GuardError); + } + if accounting.ActiveProcesses == 0 { + output.write_all(b"clean\n").map_err(|_| GuardError)?; + output.flush().map_err(|_| GuardError)?; + break; + } + if Instant::now() >= deadline { + return Err(GuardError); + } + std::thread::sleep(Duration::from_millis(20)); + } + } drop(job); Ok(()) } @@ -110,3 +127,32 @@ fn creation_time(handle: HANDLE) -> Result { } Ok((u64::from(created.dwHighDateTime) << 32) | u64::from(created.dwLowDateTime)) } + +pub fn guard_current_process() -> Result { + // SAFETY: GetCurrentProcess supplies a live non-owning handle for the current process. + create_job(unsafe { GetCurrentProcess() }) +} + +fn create_job(process: HANDLE) -> Result { + // SAFETY: Null attributes/name create a private, non-inheritable job. + let job = owned(unsafe { CreateJobObjectW(ptr::null(), ptr::null()) })?; + let mut limits = JOBOBJECT_EXTENDED_LIMIT_INFORMATION::default(); + limits.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; + // SAFETY: The live job and initialized limits remain valid for the exact supplied struct size. + if unsafe { + SetInformationJobObject( + job.as_raw_handle(), + JobObjectExtendedLimitInformation, + ptr::from_ref(&limits).cast(), + size_of::() as u32, + ) + } == 0 + { + return Err(GuardError); + } + // SAFETY: Both handles are owned and live; Runtime waits for readiness before creating tools. + if unsafe { AssignProcessToJobObject(job.as_raw_handle(), process) } == 0 { + return Err(GuardError); + } + Ok(job) +} diff --git a/apps/buddy/native/host/src/service_launcher.rs b/apps/buddy/native/host/src/service_launcher.rs new file mode 100644 index 000000000..a1944df32 --- /dev/null +++ b/apps/buddy/native/host/src/service_launcher.rs @@ -0,0 +1,99 @@ +use serde::Deserialize; +use std::{ + env, fs, io, + path::{Path, PathBuf}, + process::{Command, Stdio}, +}; + +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +struct Launch { + node_executable: PathBuf, + core_entry: PathBuf, +} + +pub fn run(path: &Path) -> io::Result { + if !path.is_absolute() { + return Err(io::ErrorKind::InvalidInput.into()); + } + let metadata = fs::symlink_metadata(path)?; + if !metadata.is_file() || metadata.len() > 65536 { + return Err(io::ErrorKind::InvalidInput.into()); + } + #[cfg(unix)] + { + use std::os::unix::fs::MetadataExt; + // SAFETY: geteuid has no pointer arguments and reads only the calling identity. + if metadata.uid() != unsafe { libc::geteuid() } || metadata.mode() & 0o077 != 0 { + return Err(io::ErrorKind::PermissionDenied.into()); + } + } + let launch: Launch = serde_json::from_slice(&fs::read(path)?)?; + for entry in [&launch.node_executable, &launch.core_entry] { + if !entry.is_absolute() || !fs::metadata(entry)?.is_file() { + return Err(io::ErrorKind::InvalidInput.into()); + } + } + let mut command = Command::new(launch.node_executable); + command + .arg("--") + .arg(launch.core_entry) + .arg("--config") + .arg(path) + .env_clear() + .stdin(Stdio::null()) + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()); + const NAMES: &[&str] = &[ + "HOME", + "USER", + "LOGNAME", + "LANG", + "LC_ALL", + "PATH", + "TMPDIR", + "TZ", + "DISPLAY", + "WAYLAND_DISPLAY", + "XAUTHORITY", + "DBUS_SESSION_BUS_ADDRESS", + "XDG_CONFIG_HOME", + "XDG_DATA_HOME", + "XDG_STATE_HOME", + "XDG_RUNTIME_DIR", + "XDG_SESSION_ID", + "XDG_SESSION_TYPE", + "XDG_CURRENT_DESKTOP", + "KDE_SESSION_VERSION", + "DESKTOP_SESSION", + "SystemRoot", + "SystemDrive", + "ProgramFiles", + "ComSpec", + "PATHEXT", + "TEMP", + "TMP", + "USERPROFILE", + "HOMEDRIVE", + "HOMEPATH", + "USERNAME", + "APPDATA", + "LOCALAPPDATA", + ]; + for key in NAMES { + if let Some(value) = env::var_os(key) { + command.env(key, value); + } + } + #[cfg(unix)] + { + use std::os::unix::process::CommandExt; + Err(command.exec()) + } + #[cfg(windows)] + { + let _job = crate::runtime_guard::guard_current_process() + .map_err(|_| io::ErrorKind::PermissionDenied)?; + Ok(command.status()?.code().unwrap_or(1)) + } +} diff --git a/apps/buddy/native/pet/src/native_pet/window.rs b/apps/buddy/native/pet/src/native_pet/window.rs index f044662c2..584a81577 100644 --- a/apps/buddy/native/pet/src/native_pet/window.rs +++ b/apps/buddy/native/pet/src/native_pet/window.rs @@ -1,122 +1,68 @@ -use std::{cell::Cell, rc::Rc, time::Duration}; +use std::{cell::Cell, path::PathBuf, rc::Rc}; use gtk::prelude::*; use crate::error::{BuddyError, BuddyResult}; use super::{ - animation::NativePetRequestedAnimationState, assets::{load_default_app_icon, load_default_pet_animation_set, load_default_pet_spritesheet}, - bounds::{ - native_pet_runtime_initial_placement, native_pet_runtime_resolve_restored_placement, - native_pet_runtime_resolve_window_placement, - }, - control_runtime::{ - native_pet_apply_completed_play_action_behavior, native_pet_drain_control_runtime_requests, - NativePetControlRuntimeState, - }, - coordinates::{NativePetLogicalOffset, NativePetLogicalPoint, NATIVE_PET_COORDINATE_SPACE}, - drag_motion::{ - native_pet_flush_drag_motion_sample, native_pet_record_drag_motion_sample, - native_pet_take_drag_frame_update, - }, - drag_runtime::{ - native_pet_commit_drag_update, NativePetDragCommitContext, NativePetDragRuntimeState, - }, - drag_state::{NativePetDragPhase, NativePetDragStateMachine}, - edge_runout::{native_pet_advance_edge_runout, native_pet_edge_runout_after_inertia_step}, - frame_timing::{ - native_pet_frame_clock_time_ms, native_pet_frame_dt_seconds, native_pet_frame_elapsed_ms, - }, - geometry::{native_pet_window_logical_size, NativePetFacing}, + bounds::{native_pet_runtime_initial_placement, native_pet_runtime_resolve_restored_placement}, + config::NativePetConfig, + coordinates::{NativePetLogicalSize, NativePetPosition}, + geometry::native_pet_window_logical_size, layer_shell::{validate_layer_shell_availability, LayerShellApi}, - lifecycle::{ - native_pet_animation_after_drag_release, native_pet_animation_for_hover_state, - native_pet_animation_for_velocity, native_pet_facing_for_velocity, - native_pet_fallen_get_up_animation, native_pet_next_throw_outcome_seed, - native_pet_requested_animation_after_pointer_interaction, - native_pet_requested_animation_for_control_animation, - native_pet_should_block_pointer_interaction, NativePetFallenGetUpActionTargets, - NativePetLifecycleActionTargets, NativePetLocalInteractionAnimationState, - }, - physics::{NativePetInertiaState, NativePetPhysicsPhase}, - pointer_interaction::{ - native_pet_open_chat_click_matches, native_pet_open_chat_release_cancels_candidate, - native_pet_pointer_cursor_name, native_pet_pointer_press_can_open_chat, - native_pet_should_start_pointer_interaction, - native_pet_window_local_pointer_tracking_position, NativePetOpenChatClick, - }, - position_state::{ - clear_native_pet_position_state, load_native_pet_position_state, - save_native_pet_position_state, should_persist_native_pet_rest_position, - }, - preset_behavior::{ - native_pet_fallen_get_up_preset_behavior_event, - native_pet_fallen_recovery_state_after_throw_finish, - native_pet_new_preset_behavior_interaction_id, - native_pet_new_preset_behavior_interaction_uuid, native_pet_preset_behavior_interaction_id, - native_pet_start_preset_behavior_execute_step, - native_pet_throw_after_drag_finish_after_runout, - native_pet_throw_after_drag_preset_behavior_event, NativePetThrowAfterDragFinishTargets, - }, + lifecycle::NativePetFallenGetUpActionTargets, + position_state::{clear_native_pet_position_state, load_native_pet_position_state}, + preset_behavior::NativePetThrowAfterDragFinishTargets, process::{ - emit_native_pet_sidecar_event, NativePetControlPoll, NativePetLaunchConfig, NativePetLayer, - NativePetSidecarEvent, - }, - renderer::{ - clear_transparent, draw_pet_frame, install_transparent_window_css, - native_pet_pointer_hits_visible_pet, + emit_native_pet_sidecar_event, NativePetLaunchConfig, NativePetLayer, NativePetSidecarEvent, }, - scripted_walk::{native_pet_step_scripted_walk, NativePetScriptedWalkArrival}, - step_runtime::{ - native_pet_active_play_action_completion_behavior, native_pet_advance_active_step, - native_pet_complete_active_step, native_pet_interrupt_active_step_for_local_interaction, - native_pet_play_action_completion_behavior_for_response, - native_pet_step_response_is_motion_timeout, - }, - window_cursor::native_pet_apply_pointer_cursor, - window_events::{native_pet_button_press_opens_chat, native_pet_event_time_ms}, - window_layer::native_pet_layer_for_scripted_walk, - window_movement::{native_pet_reconcile_visible_placement, NativePetWindowMovementAdapter}, + renderer::install_transparent_window_css, + window_movement::NativePetWindowMovementAdapter, window_state::NativePetRuntimeState, - window_tick::{native_pet_advance_lifecycle_tick, NativePetLifecycleTickState}, }; -const NATIVE_PET_DRAG_DEBUG_ENV: &str = "LEXORA_BUDDY_NATIVE_PET_DRAG_DEBUG"; -const NATIVE_PET_RUNTIME_TICK_MS: u64 = 16; -const NATIVE_PET_PLACEMENT_REFRESH_MS: u64 = 1_000; +#[path = "window_draw.rs"] +mod draw; +#[path = "window_pointer.rs"] +mod pointer; +#[path = "window_runtime.rs"] +mod runtime; -fn native_pet_preempt_agent_step_for_local_interaction( - active_step_state: &std::cell::RefCell>, -) { - let response = - native_pet_interrupt_active_step_for_local_interaction(&mut active_step_state.borrow_mut()); - if let Some(response) = response { - let _ = emit_native_pet_sidecar_event(NativePetSidecarEvent::StepResponse(response)); - } -} +const NATIVE_PET_DRAG_DEBUG_ENV: &str = "LEXORA_BUDDY_NATIVE_PET_DRAG_DEBUG"; -fn native_pet_requested_animation_after_motion_timeout( - lifecycle_action_targets: &NativePetLifecycleActionTargets, -) -> NativePetRequestedAnimationState { - NativePetRequestedAnimationState::from((*lifecycle_action_targets).idle()) +struct NativePetWindowController { + runtime: NativePetRuntimeState, + pet_spritesheet: gdk_pixbuf::Pixbuf, + throw_after_drag_finish_targets: NativePetThrowAfterDragFinishTargets, + fallen_get_up_action_targets: NativePetFallenGetUpActionTargets, + layer_shell: Option, + window_size: NativePetLogicalSize, + preferences: Cell, + config_path: PathBuf, + position_state_path: PathBuf, + pending_rest_position_save: Cell, + should_quit: Cell, + window_layer: Cell, + window_position: Cell, + window_monitor_index: Cell>, + last_frame_time: Cell>, + placement_refresh_elapsed_ms: Cell, + drag_debug: bool, } -fn native_pet_apply_window_layer( - gtk_window: >k::Window, - layer_shell: Option<&LayerShellApi>, - current_layer: &Cell, - next_layer: NativePetLayer, -) { - if current_layer.get() == next_layer { - return; +impl NativePetWindowController { + fn movement_adapter<'a>( + &'a self, + window: &'a gtk::Window, + ) -> NativePetWindowMovementAdapter<'a> { + NativePetWindowMovementAdapter::new( + window, + self.layer_shell.as_ref(), + &self.window_monitor_index, + &self.window_position, + ) } - - gtk_window.set_keep_above(next_layer.keep_above()); - if let Some(layer_shell) = layer_shell { - layer_shell.set_layer(gtk_window, next_layer); - } - current_layer.set(next_layer); } pub(super) fn run_native_pet_sidecar(config: NativePetLaunchConfig) -> BuddyResult<()> { @@ -124,12 +70,10 @@ pub(super) fn run_native_pet_sidecar(config: NativePetLaunchConfig) -> BuddyResu gtk::init().map_err(|error| BuddyError::Runtime(error.to_string()))?; let pet_animations = Rc::new(load_default_pet_animation_set()?); - let throw_after_drag_finish_targets = Rc::new( - NativePetThrowAfterDragFinishTargets::load_bundled(pet_animations.as_ref())?, - ); - let fallen_get_up_action_targets = Rc::new(NativePetFallenGetUpActionTargets::load_bundled( - pet_animations.as_ref(), - )?); + let throw_after_drag_finish_targets = + NativePetThrowAfterDragFinishTargets::load_bundled(pet_animations.as_ref())?; + let fallen_get_up_action_targets = + NativePetFallenGetUpActionTargets::load_bundled(pet_animations.as_ref())?; let window_size = native_pet_window_logical_size(pet_animations.geometry()); let window_width = window_size.width; let window_height = window_size.height; @@ -154,7 +98,7 @@ pub(super) fn run_native_pet_sidecar(config: NativePetLaunchConfig) -> BuddyResu let layer_shell_api = LayerShellApi::load(); validate_layer_shell_availability(initial_layer, layer_shell_api.is_some())?; - let layer_shell = Rc::new(layer_shell_api); + let layer_shell = layer_shell_api; if !config.preferences.remember_position { clear_native_pet_position_state(&config.position_state_path)?; } @@ -172,14 +116,14 @@ pub(super) fn run_native_pet_sidecar(config: NativePetLaunchConfig) -> BuddyResu let initial_placement = restored_position .map(|position| native_pet_runtime_resolve_restored_placement(position, window_size)) .unwrap_or_else(|| native_pet_runtime_initial_placement(window_size)); - let preferences = Rc::new(Cell::new(config.preferences)); - let config_path = Rc::new(config.config_path); - let position_state_path = Rc::new(config.position_state_path); - let pending_rest_position_save = Rc::new(Cell::new(false)); - let should_quit = Rc::new(Cell::new(false)); - let window_layer = Rc::new(Cell::new(initial_layer)); - let window_position = Rc::new(Cell::new(initial_placement.position)); - let window_monitor_index = Rc::new(Cell::new(initial_placement.monitor_index)); + let preferences = Cell::new(config.preferences); + let config_path = config.config_path; + let position_state_path = config.position_state_path; + let pending_rest_position_save = Cell::new(false); + let should_quit = Cell::new(false); + let window_layer = Cell::new(initial_layer); + let window_position = Cell::new(initial_placement.position); + let window_monitor_index = Cell::new(initial_placement.monitor_index); if let Some(layer_shell) = layer_shell.as_ref() { layer_shell.configure_window(>k_window, initial_layer, initial_placement); } else { @@ -204,942 +148,28 @@ pub(super) fn run_native_pet_sidecar(config: NativePetLaunchConfig) -> BuddyResu gtk_window.add(&drawing_area); let pet_spritesheet = load_default_pet_spritesheet()?; - let runtime_state = NativePetRuntimeState::new(pet_animations)?; - let NativePetRuntimeState { - active_step_state, - animation_playback, - control_messages, - drag_state, - edge_runout_state, - fallen_preset_behavior_recovery_state, - idle_lifecycle_elapsed_ms, - idle_presence_schedule_seed, - inertia_state, - lifecycle_action_targets, - movement_action_targets, - open_chat_click, - pet_animations, - pet_facing, - physics_params, - pointer_hovered, - requested_animation, - scripted_walk_state, - task_presence_elapsed_ms, - throw_outcome_seed, - } = runtime_state; - let drag_debug = std::env::var_os(NATIVE_PET_DRAG_DEBUG_ENV).is_some(); - - { - let pet_spritesheet = pet_spritesheet.clone(); - let pet_animations = Rc::clone(&pet_animations); - let animation_playback = Rc::clone(&animation_playback); - let pet_facing = Rc::clone(&pet_facing); - drawing_area.connect_draw(move |_, context| { - clear_transparent(context); - draw_pet_frame( - context, - &pet_spritesheet, - &pet_animations, - animation_playback.get(), - pet_facing.get(), - ); - glib::Propagation::Proceed - }); - } - - { - let pet_spritesheet = pet_spritesheet.clone(); - let pet_animations = Rc::clone(&pet_animations); - let lifecycle_action_targets = Rc::clone(&lifecycle_action_targets); - let animation_playback = Rc::clone(&animation_playback); - let requested_animation = Rc::clone(&requested_animation); - let pointer_hovered = Rc::clone(&pointer_hovered); - let drag_state = Rc::clone(&drag_state); - let inertia_state = Rc::clone(&inertia_state); - drawing_area.connect_enter_notify_event(move |drawing_area, event| { - let is_dragging = drag_state.borrow().is_some(); - let is_inertia_active = inertia_state.borrow().is_some(); - let (local_x, local_y) = event.position(); - let pointer_hits_visible_pet = native_pet_pointer_hits_visible_pet( - &pet_spritesheet, - &pet_animations, - animation_playback.get(), - local_x, - local_y, - ); - pointer_hovered.set(pointer_hits_visible_pet); - native_pet_apply_pointer_cursor( - drawing_area, - native_pet_pointer_cursor_name(pointer_hits_visible_pet, is_dragging), - ); - if !is_dragging && !is_inertia_active { - let mut playback = animation_playback.get(); - playback.set_lifecycle_animation(native_pet_animation_for_hover_state( - lifecycle_action_targets.as_ref(), - pet_animations.as_ref(), - pointer_hits_visible_pet, - false, - false, - requested_animation.get(), - playback, - )); - animation_playback.set(playback); - } - - glib::Propagation::Proceed - }); - } - - { - let pet_animations = Rc::clone(&pet_animations); - let lifecycle_action_targets = Rc::clone(&lifecycle_action_targets); - let animation_playback = Rc::clone(&animation_playback); - let requested_animation = Rc::clone(&requested_animation); - let pointer_hovered = Rc::clone(&pointer_hovered); - let drag_state = Rc::clone(&drag_state); - let inertia_state = Rc::clone(&inertia_state); - drawing_area.connect_leave_notify_event(move |drawing_area, _| { - pointer_hovered.set(false); - let is_dragging = drag_state.borrow().is_some(); - let is_inertia_active = inertia_state.borrow().is_some(); - native_pet_apply_pointer_cursor( - drawing_area, - native_pet_pointer_cursor_name(false, is_dragging), - ); - if !is_dragging && !is_inertia_active { - let mut playback = animation_playback.get(); - playback.set_lifecycle_animation(native_pet_animation_for_hover_state( - lifecycle_action_targets.as_ref(), - pet_animations.as_ref(), - false, - false, - false, - requested_animation.get(), - playback, - )); - animation_playback.set(playback); - } - - glib::Propagation::Proceed - }); - } - - { - let pet_spritesheet = pet_spritesheet.clone(); - let pet_animations = Rc::clone(&pet_animations); - let active_step_state = Rc::clone(&active_step_state); - let animation_playback = Rc::clone(&animation_playback); - let requested_animation = Rc::clone(&requested_animation); - let movement_action_targets = Rc::clone(&movement_action_targets); - let lifecycle_action_targets = Rc::clone(&lifecycle_action_targets); - let fallen_get_up_action_targets = Rc::clone(&fallen_get_up_action_targets); - let open_chat_click = Rc::clone(&open_chat_click); - let drag_state = Rc::clone(&drag_state); - let inertia_state = Rc::clone(&inertia_state); - let edge_runout_state = Rc::clone(&edge_runout_state); - let fallen_preset_behavior_recovery_state = - Rc::clone(&fallen_preset_behavior_recovery_state); - let scripted_walk_state = Rc::clone(&scripted_walk_state); - let window_position = Rc::clone(&window_position); - drawing_area.connect_button_press_event(move |drawing_area, event| { - let mut playback = animation_playback.get(); - let (local_x, local_y) = event.position(); - let press_time_ms = native_pet_event_time_ms(event.time()); - let Some(press_position) = NativePetLogicalPoint::try_new(local_x, local_y) else { - return glib::Propagation::Proceed; - }; - let pointer_hits_visible_pet = native_pet_pointer_hits_visible_pet( - &pet_spritesheet, - &pet_animations, - playback, - local_x, - local_y, - ); - if pointer_hits_visible_pet { - requested_animation.set(native_pet_requested_animation_after_pointer_interaction( - requested_animation.get(), - lifecycle_action_targets.as_ref().sleep(), - lifecycle_action_targets.as_ref().idle(), - )); - } - if native_pet_pointer_press_can_open_chat(pointer_hits_visible_pet, event.button()) { - if native_pet_button_press_opens_chat(event.button(), event.event_type()) - || native_pet_open_chat_click_matches( - open_chat_click.get(), - press_time_ms, - press_position, - ) - { - open_chat_click.set(None); - native_pet_preempt_agent_step_for_local_interaction( - active_step_state.as_ref(), - ); - let _ = emit_native_pet_sidecar_event(NativePetSidecarEvent::OpenChat); - playback.set_animation_target(movement_action_targets.tap()); - animation_playback.set(playback); - return glib::Propagation::Proceed; - } - - open_chat_click.set(Some(NativePetOpenChatClick { - time_ms: press_time_ms, - position: press_position, - })); - } else { - open_chat_click.set(None); - } - - if !native_pet_should_start_pointer_interaction(pointer_hits_visible_pet) { - return glib::Propagation::Proceed; - } - - native_pet_preempt_agent_step_for_local_interaction(active_step_state.as_ref()); - scripted_walk_state.replace(None); - let interaction_state = - NativePetLocalInteractionAnimationState::from_playback(&pet_animations, playback); - if let Some(get_up_animation) = native_pet_fallen_get_up_animation( - fallen_get_up_action_targets.as_ref(), - interaction_state, - ) { - let interaction_id = fallen_preset_behavior_recovery_state - .borrow_mut() - .take() - .map(|state| state.into_interaction_id()) - .unwrap_or_else(native_pet_new_preset_behavior_interaction_id); - let _ = emit_native_pet_sidecar_event(NativePetSidecarEvent::PresetBehavior( - native_pet_fallen_get_up_preset_behavior_event( - get_up_animation, - interaction_id.clone(), - pet_animations.as_ref(), - ), - )); - native_pet_start_preset_behavior_execute_step( - active_step_state.as_ref(), - pet_animations.as_ref(), - &mut playback, - requested_animation.as_ref(), - get_up_animation, - movement_action_targets.as_ref().idle(), - interaction_id.as_str(), - ); - animation_playback.set(playback); - inertia_state.replace(None); - edge_runout_state.set(None); - native_pet_apply_pointer_cursor( - drawing_area, - native_pet_pointer_cursor_name(true, false), - ); - return glib::Propagation::Proceed; - } - - if native_pet_should_block_pointer_interaction(interaction_state) { - return glib::Propagation::Proceed; - } - - native_pet_apply_pointer_cursor( - drawing_area, - native_pet_pointer_cursor_name(true, true), - ); - playback.set_animation_target(movement_action_targets.grab_start()); - animation_playback.set(playback); - inertia_state.replace(None); - edge_runout_state.set(None); - - let origin_position = window_position.get(); - let Some(press_cursor_position) = - native_pet_window_local_pointer_tracking_position(origin_position, local_x, local_y) - else { - return glib::Propagation::Proceed; - }; - let machine = NativePetDragStateMachine::begin_with_grab_offset( - press_cursor_position, - NativePetLogicalOffset { - x: local_x, - y: local_y, - }, - native_pet_event_time_ms(event.time()), - ); - let drag = NativePetDragRuntimeState::begin(drawing_area, event, machine); - let grab_offset = drag.grab_offset(); - drag_state.replace(Some(drag)); - if drag_debug { - eprintln!( - "native-pet-drag-debug press cursor=({:.1},{:.1}) origin=({}, {}) offset=({:.1},{:.1}) space={}", - press_cursor_position.x, - press_cursor_position.y, - origin_position.x, - origin_position.y, - grab_offset.x, - grab_offset.y, - NATIVE_PET_COORDINATE_SPACE.label(), - ); - } - - glib::Propagation::Proceed - }); - } - - { - let pet_spritesheet = pet_spritesheet.clone(); - let pet_animations = Rc::clone(&pet_animations); - let animation_playback = Rc::clone(&animation_playback); - let requested_animation = Rc::clone(&requested_animation); - let drag_state = Rc::clone(&drag_state); - let inertia_state = Rc::clone(&inertia_state); - let lifecycle_action_targets = Rc::clone(&lifecycle_action_targets); - let pointer_hovered = Rc::clone(&pointer_hovered); - let window_position = Rc::clone(&window_position); - drawing_area.connect_motion_notify_event(move |drawing_area, event| { - event.request_motions(); - let is_dragging = { - let mut drag_state = drag_state.borrow_mut(); - if let Some(state) = drag_state.as_mut() { - let (local_x, local_y) = event.position(); - if let Some(cursor_position) = - native_pet_window_local_pointer_tracking_position( - window_position.get(), - local_x, - local_y, - ) - { - native_pet_record_drag_motion_sample( - state.motion_mut(), - cursor_position, - native_pet_event_time_ms(event.time()), - ); - } - true - } else { - false - } - }; - native_pet_apply_pointer_cursor( - drawing_area, - native_pet_pointer_cursor_name(pointer_hovered.get(), is_dragging), - ); - - if !is_dragging { - let (local_x, local_y) = event.position(); - let pointer_hits_visible_pet = native_pet_pointer_hits_visible_pet( - &pet_spritesheet, - &pet_animations, - animation_playback.get(), - local_x, - local_y, - ); - let was_hovered = pointer_hovered.replace(pointer_hits_visible_pet); - let is_inertia_active = inertia_state.borrow().is_some(); - native_pet_apply_pointer_cursor( - drawing_area, - native_pet_pointer_cursor_name(pointer_hits_visible_pet, false), - ); - if was_hovered != pointer_hits_visible_pet && !is_inertia_active { - let mut playback = animation_playback.get(); - playback.set_lifecycle_animation(native_pet_animation_for_hover_state( - lifecycle_action_targets.as_ref(), - pet_animations.as_ref(), - pointer_hits_visible_pet, - false, - false, - requested_animation.get(), - playback, - )); - animation_playback.set(playback); - } - } - - glib::Propagation::Proceed - }); - } - - { - let pet_spritesheet = pet_spritesheet.clone(); - let pet_animations = Rc::clone(&pet_animations); - let animation_playback = Rc::clone(&animation_playback); - let movement_action_targets = Rc::clone(&movement_action_targets); - let pointer_hovered = Rc::clone(&pointer_hovered); - let open_chat_click = Rc::clone(&open_chat_click); - let drag_state = Rc::clone(&drag_state); - let gtk_window = gtk_window.clone(); - let layer_shell = Rc::clone(&layer_shell); - let pet_facing = Rc::clone(&pet_facing); - let physics_params = Rc::clone(&physics_params); - let inertia_state = Rc::clone(&inertia_state); - let edge_runout_state = Rc::clone(&edge_runout_state); - let window_monitor_index = Rc::clone(&window_monitor_index); - let window_position = Rc::clone(&window_position); - let pending_rest_position_save = Rc::clone(&pending_rest_position_save); - drawing_area.connect_button_release_event(move |drawing_area, event| { - let mut playback = animation_playback.get(); - let state = { - let mut drag_state = drag_state.borrow_mut(); - if let Some(state) = drag_state.as_mut() { - let (local_x, local_y) = event.position(); - if let Some(cursor_position) = - native_pet_window_local_pointer_tracking_position( - window_position.get(), - local_x, - local_y, - ) - { - if let Some(update) = native_pet_flush_drag_motion_sample( - state.motion_mut(), - cursor_position, - native_pet_event_time_ms(event.time()), - ) { - let movement_adapter = NativePetWindowMovementAdapter::new( - >k_window, - layer_shell.as_ref().as_ref(), - &window_monitor_index, - &window_position, - ); - native_pet_commit_drag_update( - state, - update, - NativePetDragCommitContext { - playback: &mut playback, - movement_action_targets: movement_action_targets.as_ref(), - pet_facing: pet_facing.as_ref(), - movement_adapter: &movement_adapter, - window_size, - drag_debug, - }, - ); - } - } - } - drag_state.take() - }; - - if let Some(state) = state { - let release = state.release(&physics_params); - let (release_x, release_y) = event.position(); - if release.was_dragging() { - pending_rest_position_save.set(true); - if let Some(release_position) = - NativePetLogicalPoint::try_new(release_x, release_y) - { - if open_chat_click.get().is_some_and(|previous| { - native_pet_open_chat_release_cancels_candidate( - previous, - release_position, - ) - }) { - open_chat_click.set(None); - } - } - } - - let inertia = if release.was_dragging() { - NativePetInertiaState::from_release( - window_position.get(), - release.release_velocity, - &physics_params, - ) - } else { - None - }; - let inertia_velocity = inertia.map(NativePetInertiaState::velocity); - inertia_state.replace(inertia); - edge_runout_state.set(None); - playback.set_animation_target(native_pet_animation_after_drag_release( - movement_action_targets.as_ref(), - release.was_dragging(), - inertia_velocity, - pet_facing.get(), - )); - let pointer_hits_visible_pet = inertia_velocity.is_none() && { - let (local_x, local_y) = event.position(); - native_pet_pointer_hits_visible_pet( - &pet_spritesheet, - &pet_animations, - playback, - local_x, - local_y, - ) - }; - pointer_hovered.set(pointer_hits_visible_pet); - native_pet_apply_pointer_cursor( - drawing_area, - native_pet_pointer_cursor_name(pointer_hits_visible_pet, false), - ); - animation_playback.set(playback); - if drag_debug { - eprintln!( - "native-pet-drag-debug release phase={:?} cursor=({:.1},{:.1}) velocity=({:.1},{:.1}) speed={:.1} inertia={}", - release.phase_before_release, - release.last_cursor_position.x, - release.last_cursor_position.y, - release.release_velocity.x, - release.release_velocity.y, - release.release_velocity.speed(), - inertia_velocity.is_some(), - ); - } - } - animation_playback.set(playback); - - glib::Propagation::Proceed - }); - } - - { - let drawing_area = drawing_area.clone(); - let pet_animations = Rc::clone(&pet_animations); - let lifecycle_action_targets = Rc::clone(&lifecycle_action_targets); - let movement_action_targets = Rc::clone(&movement_action_targets); - let animation_playback = Rc::clone(&animation_playback); - let active_step_state = Rc::clone(&active_step_state); - let requested_animation = Rc::clone(&requested_animation); - let pointer_hovered = Rc::clone(&pointer_hovered); - let idle_lifecycle_elapsed_ms = Rc::clone(&idle_lifecycle_elapsed_ms); - let task_presence_elapsed_ms = Rc::clone(&task_presence_elapsed_ms); - let idle_presence_schedule_seed = Rc::clone(&idle_presence_schedule_seed); - let throw_outcome_seed = Rc::clone(&throw_outcome_seed); - let throw_after_drag_finish_targets = Rc::clone(&throw_after_drag_finish_targets); - let control_messages = Rc::clone(&control_messages); - let drag_state = Rc::clone(&drag_state); - let edge_runout_state = Rc::clone(&edge_runout_state); - let fallen_preset_behavior_recovery_state = - Rc::clone(&fallen_preset_behavior_recovery_state); - let inertia_state = Rc::clone(&inertia_state); - let scripted_walk_state = Rc::clone(&scripted_walk_state); - let gtk_window = gtk_window.clone(); - let layer_shell = Rc::clone(&layer_shell); - let pet_facing = Rc::clone(&pet_facing); - let physics_params = Rc::clone(&physics_params); - let window_monitor_index = Rc::clone(&window_monitor_index); - let window_layer = Rc::clone(&window_layer); - let window_position = Rc::clone(&window_position); - let config_path = Rc::clone(&config_path); - let pending_rest_position_save = Rc::clone(&pending_rest_position_save); - let position_state_path = Rc::clone(&position_state_path); - let preferences = Rc::clone(&preferences); - let should_quit = Rc::clone(&should_quit); - let last_frame_time = Rc::new(Cell::new(None::)); - let placement_refresh_elapsed_ms = Rc::new(Cell::new(0_u64)); - glib::timeout_add_local( - Duration::from_millis(NATIVE_PET_RUNTIME_TICK_MS), - move || { - let frame_time = glib::monotonic_time(); - let previous_frame_time = last_frame_time.replace(Some(frame_time)); - let elapsed_ms = native_pet_frame_elapsed_ms(previous_frame_time, frame_time); - placement_refresh_elapsed_ms.set( - placement_refresh_elapsed_ms - .get() - .saturating_add(elapsed_ms), - ); - let frame_dt_seconds = - native_pet_frame_dt_seconds(previous_frame_time, frame_time, &physics_params); - let (step_response, completed_play_action_behavior) = { - let mut active_step_state = active_step_state.borrow_mut(); - let completion_behavior = - native_pet_active_play_action_completion_behavior(&active_step_state); - let response = - native_pet_advance_active_step(&mut active_step_state, elapsed_ms); - let completed_behavior = response.as_ref().and_then(|response| { - native_pet_play_action_completion_behavior_for_response( - completion_behavior, - response, - ) - }); - (response, completed_behavior) - }; - if let Some(response) = step_response { - if native_pet_step_response_is_motion_timeout(&response) { - scripted_walk_state.replace(None); - requested_animation.set( - native_pet_requested_animation_after_motion_timeout( - lifecycle_action_targets.as_ref(), - ), - ); - } - let _ = emit_native_pet_sidecar_event(NativePetSidecarEvent::StepResponse( - response, - )); - } - let mut playback = animation_playback.get(); - if let Some(completion_behavior) = completed_play_action_behavior { - native_pet_apply_completed_play_action_behavior( - completion_behavior, - pet_animations.as_ref(), - lifecycle_action_targets.as_ref(), - &mut playback, - requested_animation.as_ref(), - ); - } - let drag_phase = { - let mut phase = NativePetDragPhase::Idle; - let mut drag_state = drag_state.borrow_mut(); - if let Some(state) = drag_state.as_mut() { - phase = state.phase(); - let frame_time_ms = native_pet_frame_clock_time_ms(frame_time); - if let Some(update) = - native_pet_take_drag_frame_update(state.motion_mut(), frame_time_ms) - { - let movement_adapter = NativePetWindowMovementAdapter::new( - >k_window, - layer_shell.as_ref().as_ref(), - &window_monitor_index, - &window_position, - ); - native_pet_commit_drag_update( - state, - update, - NativePetDragCommitContext { - playback: &mut playback, - movement_action_targets: movement_action_targets.as_ref(), - pet_facing: pet_facing.as_ref(), - movement_adapter: &movement_adapter, - window_size, - drag_debug, - }, - ); - } - } - phase - }; - let is_dragging = !matches!(drag_phase, NativePetDragPhase::Idle); - let is_inertia_active = if is_dragging { - false - } else { - let mut inertia_state = inertia_state.borrow_mut(); - if let Some(state) = inertia_state.as_mut() { - let movement_adapter = NativePetWindowMovementAdapter::new( - >k_window, - layer_shell.as_ref().as_ref(), - &window_monitor_index, - &window_position, - ); - let impact_velocity = state.velocity(); - let step = state.step(frame_dt_seconds, &physics_params, |position| { - native_pet_runtime_resolve_window_placement(position, window_size, None) - .placement - .position - }); - let placement = native_pet_runtime_resolve_window_placement( - step.position, - window_size, - None, - ) - .placement; - if step.velocity.x.abs() > 1.0 { - pet_facing.set(if step.velocity.x > 0.0 { - NativePetFacing::Right - } else { - NativePetFacing::Left - }); - } - movement_adapter.move_to(placement); - if matches!(step.phase, NativePetPhysicsPhase::Inertia) { - playback.set_animation_target(native_pet_animation_for_velocity( - movement_action_targets.as_ref(), - step.velocity, - pet_facing.get(), - )); - if drag_debug { - eprintln!( - "native-pet-drag-debug inertia dt={:.4}s position=({}, {}) velocity=({:.1},{:.1}) speed={:.1} clamped={}", - step.clamped_dt_seconds, - step.position.x, - step.position.y, - step.velocity.x, - step.velocity.y, - step.velocity.speed(), - step.hit_position_clamp, - ); - } - true - } else { - inertia_state.take(); - let run_facing = - native_pet_facing_for_velocity(impact_velocity, pet_facing.get()); - let throw_finish = native_pet_throw_after_drag_finish_after_runout( - run_facing, - throw_outcome_seed.get(), - ); - let finish_animation = - throw_after_drag_finish_targets.animation_target(throw_finish); - let interaction_uuid = - native_pet_new_preset_behavior_interaction_uuid(); - let interaction_id = - native_pet_preset_behavior_interaction_id(interaction_uuid); - fallen_preset_behavior_recovery_state.replace( - native_pet_fallen_recovery_state_after_throw_finish( - throw_finish, - interaction_id.clone(), - ), - ); - let _ = emit_native_pet_sidecar_event( - NativePetSidecarEvent::PresetBehavior( - native_pet_throw_after_drag_preset_behavior_event( - throw_finish, - finish_animation, - interaction_id.clone(), - pet_animations.as_ref(), - ), - ), - ); - throw_outcome_seed - .set(native_pet_next_throw_outcome_seed(throw_outcome_seed.get())); - if let Some(runout) = native_pet_edge_runout_after_inertia_step( - movement_action_targets.as_ref(), - step.hit_position_clamp, - pet_facing.get(), - impact_velocity, - finish_animation, - interaction_uuid, - &physics_params, - ) { - edge_runout_state.set(Some(runout)); - } else { - native_pet_start_preset_behavior_execute_step( - active_step_state.as_ref(), - pet_animations.as_ref(), - &mut playback, - requested_animation.as_ref(), - finish_animation, - lifecycle_action_targets.as_ref().idle(), - interaction_id.as_str(), - ); - } - if drag_debug { - eprintln!( - "native-pet-drag-debug inertia-end dt={:.4}s position=({}, {}) clamped={}", - step.clamped_dt_seconds, - step.position.x, - step.position.y, - step.hit_position_clamp, - ); - } - false - } - } else { - false - } - }; - let is_edge_runout_active = if is_dragging || is_inertia_active { - false - } else if let Some(state) = edge_runout_state.get() { - let step = native_pet_advance_edge_runout(state, elapsed_ms); - edge_runout_state.set(step.next_state); - if step.next_state.is_some() { - playback.set_animation_target(step.animation); - } else { - let interaction_id = native_pet_preset_behavior_interaction_id( - state.preset_behavior_interaction_uuid, - ); - native_pet_start_preset_behavior_execute_step( - active_step_state.as_ref(), - pet_animations.as_ref(), - &mut playback, - requested_animation.as_ref(), - step.animation, - lifecycle_action_targets.as_ref().idle(), - interaction_id.as_str(), - ); - } - step.next_state.is_some() - } else { - false - }; - let is_scripted_walk_active = - if is_dragging || is_inertia_active || is_edge_runout_active { - false - } else { - native_pet_apply_window_layer( - >k_window, - layer_shell.as_ref().as_ref(), - window_layer.as_ref(), - native_pet_layer_for_scripted_walk( - NativePetLayer::from_always_on_top(preferences.get().always_on_top), - scripted_walk_state.borrow().as_ref(), - ), - ); - let mut scripted_walk_state = scripted_walk_state.borrow_mut(); - if let Some(state) = scripted_walk_state.as_mut() { - let movement_adapter = NativePetWindowMovementAdapter::new( - >k_window, - layer_shell.as_ref().as_ref(), - &window_monitor_index, - &window_position, - ); - let step = native_pet_step_scripted_walk( - movement_action_targets.as_ref(), - window_position.get(), - state.target_position(), - elapsed_ms, - ); - let placement = native_pet_runtime_resolve_window_placement( - step.position, - window_size, - None, - ) - .placement; - if step.movement_dx.abs() > 1.0 { - pet_facing.set(if step.movement_dx > 0.0 { - NativePetFacing::Right - } else { - NativePetFacing::Left - }); - } - movement_adapter.move_to(placement); - playback.set_animation_target(step.animation); - if step.finished { - match state.advance_after_arrival(elapsed_ms) { - NativePetScriptedWalkArrival::Holding => { - let after_animation = - state.after_animation.unwrap_or_else(|| { - lifecycle_action_targets.as_ref().idle() - }); - playback.set_animation_target(after_animation); - true - } - NativePetScriptedWalkArrival::Advanced => true, - NativePetScriptedWalkArrival::Finished => { - let after_animation = - state.after_animation.unwrap_or_else(|| { - lifecycle_action_targets.as_ref().idle() - }); - let requested = - native_pet_requested_animation_for_control_animation( - &pet_animations, - after_animation, - lifecycle_action_targets.as_ref().idle(), - ); - requested_animation.set(requested); - playback.restart_animation_target(after_animation); - *scripted_walk_state = None; - let step_response = { - let mut active_step_state = - active_step_state.borrow_mut(); - native_pet_complete_active_step(&mut active_step_state) - }; - if let Some(response) = step_response { - let _ = emit_native_pet_sidecar_event( - NativePetSidecarEvent::StepResponse(response), - ); - } - false - } - } - } else { - true - } - } else { - false - } - }; - let is_motion_locked = is_dragging - || is_inertia_active - || is_edge_runout_active - || is_scripted_walk_active; - - if should_persist_native_pet_rest_position( - pending_rest_position_save.get(), - is_dragging, - is_inertia_active, - is_edge_runout_active, - is_scripted_walk_active, - ) { - pending_rest_position_save.set(false); - if preferences.get().remember_position { - if let Err(error) = save_native_pet_position_state( - position_state_path.as_ref(), - window_position.get(), - ) { - eprintln!("Lexora Buddy native pet position save failed: {error}"); - } - } - } - - if !is_motion_locked { - let force_native_refresh = layer_shell.is_some() - && placement_refresh_elapsed_ms.get() >= NATIVE_PET_PLACEMENT_REFRESH_MS; - let movement_adapter = NativePetWindowMovementAdapter::new( - >k_window, - layer_shell.as_ref().as_ref(), - &window_monitor_index, - &window_position, - ); - native_pet_reconcile_visible_placement( - &movement_adapter, - window_position.get(), - window_monitor_index.get(), - window_size, - force_native_refresh, - drag_debug, - ); - if force_native_refresh { - placement_refresh_elapsed_ms.set(0); - } - } - - let is_dragging_for_control_state = drag_state.borrow().is_some(); - let control_poll = - native_pet_drain_control_runtime_requests(NativePetControlRuntimeState { - active_step_state: active_step_state.as_ref(), - control_messages: control_messages.as_ref(), - pet_animations: pet_animations.as_ref(), - lifecycle_action_targets: lifecycle_action_targets.as_ref(), - playback: &mut playback, - requested_animation: requested_animation.as_ref(), - pointer_hovered: pointer_hovered.as_ref(), - idle_lifecycle_elapsed_ms: idle_lifecycle_elapsed_ms.as_ref(), - idle_presence_schedule_seed: idle_presence_schedule_seed.as_ref(), - task_presence_elapsed_ms: task_presence_elapsed_ms.as_ref(), - inertia_state: inertia_state.as_ref(), - edge_runout_state: edge_runout_state.as_ref(), - scripted_walk_state: scripted_walk_state.as_ref(), - window_position: window_position.as_ref(), - window_monitor_index: window_monitor_index.as_ref(), - window_size, - is_dragging: is_dragging_for_control_state, - is_motion_locked, - config_path: config_path.as_ref(), - position_state_path: position_state_path.as_ref(), - preferences: preferences.as_ref(), - pending_rest_position_save: pending_rest_position_save.as_ref(), - should_quit: should_quit.as_ref(), - }); - if matches!(control_poll, NativePetControlPoll::Disconnected) { - gtk::main_quit(); - return glib::ControlFlow::Break; - } - if should_quit.get() { - gtk::main_quit(); - return glib::ControlFlow::Break; - } - native_pet_apply_window_layer( - >k_window, - layer_shell.as_ref().as_ref(), - window_layer.as_ref(), - native_pet_layer_for_scripted_walk( - NativePetLayer::from_always_on_top(preferences.get().always_on_top), - scripted_walk_state.borrow().as_ref(), - ), - ); - - native_pet_advance_lifecycle_tick(NativePetLifecycleTickState { - playback: &mut playback, - pet_animations: &pet_animations, - lifecycle_action_targets: lifecycle_action_targets.as_ref(), - requested_animation: requested_animation.as_ref(), - pointer_hovered: pointer_hovered.as_ref(), - idle_lifecycle_elapsed_ms: idle_lifecycle_elapsed_ms.as_ref(), - idle_presence_schedule_seed: idle_presence_schedule_seed.as_ref(), - task_presence_elapsed_ms: task_presence_elapsed_ms.as_ref(), - elapsed_ms, - is_dragging, - is_inertia_active, - is_edge_runout_active, - is_scripted_walk_active, - }); - animation_playback.set(playback); - drawing_area.queue_draw(); - glib::ControlFlow::Continue - }, - ); - } + let controller = Rc::new(NativePetWindowController { + runtime: NativePetRuntimeState::new(pet_animations)?, + pet_spritesheet, + throw_after_drag_finish_targets, + fallen_get_up_action_targets, + layer_shell, + window_size, + preferences, + config_path, + position_state_path, + pending_rest_position_save, + should_quit, + window_layer, + window_position, + window_monitor_index, + last_frame_time: Cell::new(None), + placement_refresh_elapsed_ms: Cell::new(0), + drag_debug: std::env::var_os(NATIVE_PET_DRAG_DEBUG_ENV).is_some(), + }); + controller.connect_draw(&drawing_area); + controller.connect_pointer_handlers(>k_window, &drawing_area); + controller.start_runtime_updates(>k_window, &drawing_area); gtk_window.show_all(); emit_native_pet_sidecar_event(NativePetSidecarEvent::Ready)?; diff --git a/apps/buddy/native/pet/src/native_pet/window_draw.rs b/apps/buddy/native/pet/src/native_pet/window_draw.rs new file mode 100644 index 000000000..f31b280e1 --- /dev/null +++ b/apps/buddy/native/pet/src/native_pet/window_draw.rs @@ -0,0 +1,23 @@ +use std::rc::Rc; + +use gtk::prelude::*; + +use super::super::renderer::{clear_transparent, draw_pet_frame}; +use super::NativePetWindowController; + +impl NativePetWindowController { + pub(super) fn connect_draw(self: &Rc, drawing_area: >k::DrawingArea) { + let controller = Rc::clone(self); + drawing_area.connect_draw(move |_, context| { + clear_transparent(context); + draw_pet_frame( + context, + &controller.pet_spritesheet, + &controller.runtime.pet_animations, + controller.runtime.animation_playback.get(), + controller.runtime.pet_facing.get(), + ); + glib::Propagation::Proceed + }); + } +} diff --git a/apps/buddy/native/pet/src/native_pet/window_pointer.rs b/apps/buddy/native/pet/src/native_pet/window_pointer.rs new file mode 100644 index 000000000..4e9f7a425 --- /dev/null +++ b/apps/buddy/native/pet/src/native_pet/window_pointer.rs @@ -0,0 +1,522 @@ +use std::rc::Rc; + +use gtk::prelude::*; + +use super::super::{ + coordinates::{NativePetLogicalOffset, NativePetLogicalPoint, NATIVE_PET_COORDINATE_SPACE}, + drag_motion::{native_pet_flush_drag_motion_sample, native_pet_record_drag_motion_sample}, + drag_runtime::{ + native_pet_commit_drag_update, NativePetDragCommitContext, NativePetDragRuntimeState, + }, + drag_state::NativePetDragStateMachine, + lifecycle::{ + native_pet_animation_after_drag_release, native_pet_animation_for_hover_state, + native_pet_fallen_get_up_animation, + native_pet_requested_animation_after_pointer_interaction, + native_pet_should_block_pointer_interaction, NativePetLocalInteractionAnimationState, + }, + physics::NativePetInertiaState, + pointer_interaction::{ + native_pet_open_chat_click_matches, native_pet_open_chat_release_cancels_candidate, + native_pet_pointer_cursor_name, native_pet_pointer_press_can_open_chat, + native_pet_should_start_pointer_interaction, + native_pet_window_local_pointer_tracking_position, NativePetOpenChatClick, + }, + preset_behavior::{ + native_pet_fallen_get_up_preset_behavior_event, + native_pet_new_preset_behavior_interaction_id, + native_pet_start_preset_behavior_execute_step, + }, + process::{emit_native_pet_sidecar_event, NativePetSidecarEvent}, + renderer::native_pet_pointer_hits_visible_pet, + step_runtime::native_pet_interrupt_active_step_for_local_interaction, + window_cursor::native_pet_apply_pointer_cursor, + window_events::{native_pet_button_press_opens_chat, native_pet_event_time_ms}, + window_state::NativePetRuntimeState, +}; +use super::NativePetWindowController; + +impl NativePetWindowController { + pub(super) fn connect_pointer_handlers( + self: &Rc, + window: >k::Window, + drawing_area: >k::DrawingArea, + ) { + let controller = Rc::clone(self); + drawing_area + .connect_enter_notify_event(move |area, event| controller.pointer_enter(area, event)); + let controller = Rc::clone(self); + drawing_area.connect_leave_notify_event(move |area, _| controller.pointer_leave(area)); + let controller = Rc::clone(self); + drawing_area + .connect_button_press_event(move |area, event| controller.pointer_press(area, event)); + let controller = Rc::clone(self); + drawing_area + .connect_motion_notify_event(move |area, event| controller.pointer_motion(area, event)); + let controller = Rc::clone(self); + let window = window.downgrade(); + drawing_area.connect_button_release_event(move |area, event| { + let Some(window) = window.upgrade() else { + return glib::Propagation::Proceed; + }; + controller.pointer_release(&window, area, event) + }); + } + + fn preempt_agent_step(&self) { + let response = native_pet_interrupt_active_step_for_local_interaction( + &mut self.runtime.active_step_state.borrow_mut(), + ); + if let Some(response) = response { + let _ = emit_native_pet_sidecar_event(NativePetSidecarEvent::StepResponse(response)); + } + } + + fn pointer_enter( + &self, + drawing_area: >k::DrawingArea, + event: &gdk::EventCrossing, + ) -> glib::Propagation { + let NativePetRuntimeState { + animation_playback, + drag_state, + inertia_state, + lifecycle_action_targets, + pet_animations, + pointer_hovered, + requested_animation, + .. + } = &self.runtime; + let Self { + pet_spritesheet, .. + } = self; + let is_dragging = drag_state.borrow().is_some(); + let is_inertia_active = inertia_state.borrow().is_some(); + let (local_x, local_y) = event.position(); + let pointer_hits_visible_pet = native_pet_pointer_hits_visible_pet( + pet_spritesheet, + pet_animations, + animation_playback.get(), + local_x, + local_y, + ); + pointer_hovered.set(pointer_hits_visible_pet); + native_pet_apply_pointer_cursor( + drawing_area, + native_pet_pointer_cursor_name(pointer_hits_visible_pet, is_dragging), + ); + if !is_dragging && !is_inertia_active { + let mut playback = animation_playback.get(); + playback.set_lifecycle_animation(native_pet_animation_for_hover_state( + lifecycle_action_targets.as_ref(), + pet_animations.as_ref(), + pointer_hits_visible_pet, + false, + false, + requested_animation.get(), + playback, + )); + animation_playback.set(playback); + } + + glib::Propagation::Proceed + } + + fn pointer_leave(&self, drawing_area: >k::DrawingArea) -> glib::Propagation { + let NativePetRuntimeState { + animation_playback, + drag_state, + inertia_state, + lifecycle_action_targets, + pet_animations, + pointer_hovered, + requested_animation, + .. + } = &self.runtime; + pointer_hovered.set(false); + let is_dragging = drag_state.borrow().is_some(); + let is_inertia_active = inertia_state.borrow().is_some(); + native_pet_apply_pointer_cursor( + drawing_area, + native_pet_pointer_cursor_name(false, is_dragging), + ); + if !is_dragging && !is_inertia_active { + let mut playback = animation_playback.get(); + playback.set_lifecycle_animation(native_pet_animation_for_hover_state( + lifecycle_action_targets.as_ref(), + pet_animations.as_ref(), + false, + false, + false, + requested_animation.get(), + playback, + )); + animation_playback.set(playback); + } + + glib::Propagation::Proceed + } + + fn pointer_press( + &self, + drawing_area: >k::DrawingArea, + event: &gdk::EventButton, + ) -> glib::Propagation { + let NativePetRuntimeState { + animation_playback, + active_step_state, + drag_state, + edge_runout_state, + fallen_preset_behavior_recovery_state, + inertia_state, + lifecycle_action_targets, + movement_action_targets, + open_chat_click, + pet_animations, + requested_animation, + scripted_walk_state, + .. + } = &self.runtime; + let Self { + pet_spritesheet, + fallen_get_up_action_targets, + window_position, + .. + } = self; + let drag_debug = self.drag_debug; + let mut playback = animation_playback.get(); + let (local_x, local_y) = event.position(); + let press_time_ms = native_pet_event_time_ms(event.time()); + let Some(press_position) = NativePetLogicalPoint::try_new(local_x, local_y) else { + return glib::Propagation::Proceed; + }; + let pointer_hits_visible_pet = native_pet_pointer_hits_visible_pet( + pet_spritesheet, + pet_animations, + playback, + local_x, + local_y, + ); + if pointer_hits_visible_pet { + requested_animation.set(native_pet_requested_animation_after_pointer_interaction( + requested_animation.get(), + lifecycle_action_targets.as_ref().sleep(), + lifecycle_action_targets.as_ref().idle(), + )); + } + if native_pet_pointer_press_can_open_chat(pointer_hits_visible_pet, event.button()) { + if native_pet_button_press_opens_chat(event.button(), event.event_type()) + || native_pet_open_chat_click_matches( + open_chat_click.get(), + press_time_ms, + press_position, + ) + { + open_chat_click.set(None); + self.preempt_agent_step(); + let _ = emit_native_pet_sidecar_event(NativePetSidecarEvent::OpenChat); + playback.set_animation_target(movement_action_targets.tap()); + animation_playback.set(playback); + return glib::Propagation::Proceed; + } + + open_chat_click.set(Some(NativePetOpenChatClick { + time_ms: press_time_ms, + position: press_position, + })); + } else { + open_chat_click.set(None); + } + + if !native_pet_should_start_pointer_interaction(pointer_hits_visible_pet) { + return glib::Propagation::Proceed; + } + + self.preempt_agent_step(); + scripted_walk_state.replace(None); + let interaction_state = + NativePetLocalInteractionAnimationState::from_playback(pet_animations, playback); + if let Some(get_up_animation) = + native_pet_fallen_get_up_animation(fallen_get_up_action_targets, interaction_state) + { + let interaction_id = fallen_preset_behavior_recovery_state + .borrow_mut() + .take() + .map(|state| state.into_interaction_id()) + .unwrap_or_else(native_pet_new_preset_behavior_interaction_id); + let _ = emit_native_pet_sidecar_event(NativePetSidecarEvent::PresetBehavior( + native_pet_fallen_get_up_preset_behavior_event( + get_up_animation, + interaction_id.clone(), + pet_animations.as_ref(), + ), + )); + native_pet_start_preset_behavior_execute_step( + active_step_state.as_ref(), + pet_animations.as_ref(), + &mut playback, + requested_animation.as_ref(), + get_up_animation, + movement_action_targets.as_ref().idle(), + interaction_id.as_str(), + ); + animation_playback.set(playback); + inertia_state.replace(None); + edge_runout_state.set(None); + native_pet_apply_pointer_cursor( + drawing_area, + native_pet_pointer_cursor_name(true, false), + ); + return glib::Propagation::Proceed; + } + + if native_pet_should_block_pointer_interaction(interaction_state) { + return glib::Propagation::Proceed; + } + + native_pet_apply_pointer_cursor(drawing_area, native_pet_pointer_cursor_name(true, true)); + playback.set_animation_target(movement_action_targets.grab_start()); + animation_playback.set(playback); + inertia_state.replace(None); + edge_runout_state.set(None); + + let origin_position = window_position.get(); + let Some(press_cursor_position) = + native_pet_window_local_pointer_tracking_position(origin_position, local_x, local_y) + else { + return glib::Propagation::Proceed; + }; + let machine = NativePetDragStateMachine::begin_with_grab_offset( + press_cursor_position, + NativePetLogicalOffset { + x: local_x, + y: local_y, + }, + native_pet_event_time_ms(event.time()), + ); + let drag = NativePetDragRuntimeState::begin(drawing_area, event, machine); + let grab_offset = drag.grab_offset(); + drag_state.replace(Some(drag)); + if drag_debug { + eprintln!( + "native-pet-drag-debug press cursor=({:.1},{:.1}) origin=({}, {}) offset=({:.1},{:.1}) space={}", + press_cursor_position.x, + press_cursor_position.y, + origin_position.x, + origin_position.y, + grab_offset.x, + grab_offset.y, + NATIVE_PET_COORDINATE_SPACE.label(), + ); + } + + glib::Propagation::Proceed + } + + fn pointer_motion( + &self, + drawing_area: >k::DrawingArea, + event: &gdk::EventMotion, + ) -> glib::Propagation { + let NativePetRuntimeState { + animation_playback, + drag_state, + inertia_state, + lifecycle_action_targets, + pet_animations, + pointer_hovered, + requested_animation, + .. + } = &self.runtime; + let Self { + pet_spritesheet, + window_position, + .. + } = self; + event.request_motions(); + let is_dragging = { + let mut drag_state = drag_state.borrow_mut(); + if let Some(state) = drag_state.as_mut() { + let (local_x, local_y) = event.position(); + if let Some(cursor_position) = native_pet_window_local_pointer_tracking_position( + window_position.get(), + local_x, + local_y, + ) { + native_pet_record_drag_motion_sample( + state.motion_mut(), + cursor_position, + native_pet_event_time_ms(event.time()), + ); + } + true + } else { + false + } + }; + native_pet_apply_pointer_cursor( + drawing_area, + native_pet_pointer_cursor_name(pointer_hovered.get(), is_dragging), + ); + + if !is_dragging { + let (local_x, local_y) = event.position(); + let pointer_hits_visible_pet = native_pet_pointer_hits_visible_pet( + pet_spritesheet, + pet_animations, + animation_playback.get(), + local_x, + local_y, + ); + let was_hovered = pointer_hovered.replace(pointer_hits_visible_pet); + let is_inertia_active = inertia_state.borrow().is_some(); + native_pet_apply_pointer_cursor( + drawing_area, + native_pet_pointer_cursor_name(pointer_hits_visible_pet, false), + ); + if was_hovered != pointer_hits_visible_pet && !is_inertia_active { + let mut playback = animation_playback.get(); + playback.set_lifecycle_animation(native_pet_animation_for_hover_state( + lifecycle_action_targets.as_ref(), + pet_animations.as_ref(), + pointer_hits_visible_pet, + false, + false, + requested_animation.get(), + playback, + )); + animation_playback.set(playback); + } + } + + glib::Propagation::Proceed + } + + fn pointer_release( + &self, + gtk_window: >k::Window, + drawing_area: >k::DrawingArea, + event: &gdk::EventButton, + ) -> glib::Propagation { + let NativePetRuntimeState { + animation_playback, + drag_state, + edge_runout_state, + inertia_state, + movement_action_targets, + open_chat_click, + pet_animations, + pet_facing, + physics_params, + pointer_hovered, + .. + } = &self.runtime; + let Self { + pet_spritesheet, + pending_rest_position_save, + window_position, + .. + } = self; + let window_size = self.window_size; + let drag_debug = self.drag_debug; + let mut playback = animation_playback.get(); + let state = { + let mut drag_state = drag_state.borrow_mut(); + if let Some(state) = drag_state.as_mut() { + let (local_x, local_y) = event.position(); + if let Some(cursor_position) = native_pet_window_local_pointer_tracking_position( + window_position.get(), + local_x, + local_y, + ) { + if let Some(update) = native_pet_flush_drag_motion_sample( + state.motion_mut(), + cursor_position, + native_pet_event_time_ms(event.time()), + ) { + let movement_adapter = self.movement_adapter(gtk_window); + native_pet_commit_drag_update( + state, + update, + NativePetDragCommitContext { + playback: &mut playback, + movement_action_targets: movement_action_targets.as_ref(), + pet_facing: pet_facing.as_ref(), + movement_adapter: &movement_adapter, + window_size, + drag_debug, + }, + ); + } + } + } + drag_state.take() + }; + + if let Some(state) = state { + let release = state.release(physics_params); + let (release_x, release_y) = event.position(); + if release.was_dragging() { + pending_rest_position_save.set(true); + if let Some(release_position) = NativePetLogicalPoint::try_new(release_x, release_y) + { + if open_chat_click.get().is_some_and(|previous| { + native_pet_open_chat_release_cancels_candidate(previous, release_position) + }) { + open_chat_click.set(None); + } + } + } + + let inertia = if release.was_dragging() { + NativePetInertiaState::from_release( + window_position.get(), + release.release_velocity, + physics_params, + ) + } else { + None + }; + let inertia_velocity = inertia.map(NativePetInertiaState::velocity); + inertia_state.replace(inertia); + edge_runout_state.set(None); + playback.set_animation_target(native_pet_animation_after_drag_release( + movement_action_targets.as_ref(), + release.was_dragging(), + inertia_velocity, + pet_facing.get(), + )); + let pointer_hits_visible_pet = inertia_velocity.is_none() && { + let (local_x, local_y) = event.position(); + native_pet_pointer_hits_visible_pet( + pet_spritesheet, + pet_animations, + playback, + local_x, + local_y, + ) + }; + pointer_hovered.set(pointer_hits_visible_pet); + native_pet_apply_pointer_cursor( + drawing_area, + native_pet_pointer_cursor_name(pointer_hits_visible_pet, false), + ); + animation_playback.set(playback); + if drag_debug { + eprintln!( + "native-pet-drag-debug release phase={:?} cursor=({:.1},{:.1}) velocity=({:.1},{:.1}) speed={:.1} inertia={}", + release.phase_before_release, + release.last_cursor_position.x, + release.last_cursor_position.y, + release.release_velocity.x, + release.release_velocity.y, + release.release_velocity.speed(), + inertia_velocity.is_some(), + ); + } + } + animation_playback.set(playback); + + glib::Propagation::Proceed + } +} diff --git a/apps/buddy/native/pet/src/native_pet/window_runtime.rs b/apps/buddy/native/pet/src/native_pet/window_runtime.rs new file mode 100644 index 000000000..1d4b6417d --- /dev/null +++ b/apps/buddy/native/pet/src/native_pet/window_runtime.rs @@ -0,0 +1,655 @@ +use std::{rc::Rc, time::Duration}; + +use gtk::prelude::*; + +use super::super::{ + animation::{NativePetAnimationPlayback, NativePetRequestedAnimationState}, + bounds::native_pet_runtime_resolve_window_placement, + control_runtime::{ + native_pet_apply_completed_play_action_behavior, native_pet_drain_control_runtime_requests, + NativePetControlRuntimeState, + }, + drag_motion::native_pet_take_drag_frame_update, + drag_runtime::{native_pet_commit_drag_update, NativePetDragCommitContext}, + drag_state::NativePetDragPhase, + edge_runout::{native_pet_advance_edge_runout, native_pet_edge_runout_after_inertia_step}, + frame_timing::{ + native_pet_frame_clock_time_ms, native_pet_frame_dt_seconds, native_pet_frame_elapsed_ms, + }, + geometry::NativePetFacing, + lifecycle::{ + native_pet_animation_for_velocity, native_pet_facing_for_velocity, + native_pet_next_throw_outcome_seed, native_pet_requested_animation_for_control_animation, + }, + physics::NativePetPhysicsPhase, + position_state::{save_native_pet_position_state, should_persist_native_pet_rest_position}, + preset_behavior::{ + native_pet_fallen_recovery_state_after_throw_finish, + native_pet_new_preset_behavior_interaction_uuid, native_pet_preset_behavior_interaction_id, + native_pet_start_preset_behavior_execute_step, + native_pet_throw_after_drag_finish_after_runout, + native_pet_throw_after_drag_preset_behavior_event, + }, + process::{ + emit_native_pet_sidecar_event, NativePetControlPoll, NativePetLayer, NativePetSidecarEvent, + }, + scripted_walk::{native_pet_step_scripted_walk, NativePetScriptedWalkArrival}, + step_runtime::{ + native_pet_active_play_action_completion_behavior, native_pet_advance_active_step, + native_pet_complete_active_step, native_pet_play_action_completion_behavior_for_response, + native_pet_step_response_is_motion_timeout, + }, + window_layer::native_pet_layer_for_scripted_walk, + window_movement::native_pet_reconcile_visible_placement, + window_state::NativePetRuntimeState, + window_tick::{native_pet_advance_lifecycle_tick, NativePetLifecycleTickState}, +}; +use super::NativePetWindowController; + +const NATIVE_PET_RUNTIME_TICK_MS: u64 = 16; +const NATIVE_PET_PLACEMENT_REFRESH_MS: u64 = 1_000; + +#[derive(Clone, Copy)] +struct NativePetFrameMotion { + is_dragging: bool, + is_inertia_active: bool, + is_edge_runout_active: bool, + is_scripted_walk_active: bool, +} + +impl NativePetFrameMotion { + fn is_locked(self) -> bool { + self.is_dragging + || self.is_inertia_active + || self.is_edge_runout_active + || self.is_scripted_walk_active + } +} + +impl NativePetWindowController { + pub(super) fn start_runtime_updates( + self: &Rc, + window: >k::Window, + drawing_area: >k::DrawingArea, + ) { + let controller = Rc::clone(self); + let window = window.downgrade(); + let drawing_area = drawing_area.downgrade(); + glib::timeout_add_local( + Duration::from_millis(NATIVE_PET_RUNTIME_TICK_MS), + move || { + let (Some(window), Some(drawing_area)) = (window.upgrade(), drawing_area.upgrade()) + else { + return glib::ControlFlow::Break; + }; + controller.tick(&window, &drawing_area) + }, + ); + } + + fn apply_window_layer(&self, window: >k::Window, next_layer: NativePetLayer) { + if self.window_layer.get() == next_layer { + return; + } + window.set_keep_above(next_layer.keep_above()); + if let Some(layer_shell) = &self.layer_shell { + layer_shell.set_layer(window, next_layer); + } + self.window_layer.set(next_layer); + } + + fn tick(&self, gtk_window: >k::Window, drawing_area: >k::DrawingArea) -> glib::ControlFlow { + let NativePetRuntimeState { + animation_playback, + idle_lifecycle_elapsed_ms, + idle_presence_schedule_seed, + lifecycle_action_targets, + pet_animations, + physics_params, + pointer_hovered, + requested_animation, + task_presence_elapsed_ms, + .. + } = &self.runtime; + let Self { + last_frame_time, + placement_refresh_elapsed_ms, + .. + } = self; + let frame_time = glib::monotonic_time(); + let previous_frame_time = last_frame_time.replace(Some(frame_time)); + let elapsed_ms = native_pet_frame_elapsed_ms(previous_frame_time, frame_time); + placement_refresh_elapsed_ms.set( + placement_refresh_elapsed_ms + .get() + .saturating_add(elapsed_ms), + ); + let frame_dt_seconds = + native_pet_frame_dt_seconds(previous_frame_time, frame_time, physics_params); + let mut playback = self.advance_agent_step(elapsed_ms); + let is_dragging = self.advance_drag(gtk_window, &mut playback, frame_time); + let is_inertia_active = + !is_dragging && self.advance_inertia(gtk_window, &mut playback, frame_dt_seconds); + let is_edge_runout_active = !is_dragging + && !is_inertia_active + && self.advance_edge_runout(&mut playback, elapsed_ms); + let is_scripted_walk_active = !is_dragging + && !is_inertia_active + && !is_edge_runout_active + && self.advance_scripted_walk(gtk_window, &mut playback, elapsed_ms); + let motion = NativePetFrameMotion { + is_dragging, + is_inertia_active, + is_edge_runout_active, + is_scripted_walk_active, + }; + self.reconcile_rest_position(gtk_window, motion); + if self + .poll_control(gtk_window, &mut playback, motion) + .is_break() + { + return glib::ControlFlow::Break; + } + native_pet_advance_lifecycle_tick(NativePetLifecycleTickState { + playback: &mut playback, + pet_animations, + lifecycle_action_targets: lifecycle_action_targets.as_ref(), + requested_animation: requested_animation.as_ref(), + pointer_hovered: pointer_hovered.as_ref(), + idle_lifecycle_elapsed_ms: idle_lifecycle_elapsed_ms.as_ref(), + idle_presence_schedule_seed: idle_presence_schedule_seed.as_ref(), + task_presence_elapsed_ms: task_presence_elapsed_ms.as_ref(), + elapsed_ms, + is_dragging, + is_inertia_active, + is_edge_runout_active, + is_scripted_walk_active, + }); + animation_playback.set(playback); + drawing_area.queue_draw(); + glib::ControlFlow::Continue + } + + fn advance_agent_step(&self, elapsed_ms: u64) -> NativePetAnimationPlayback { + let NativePetRuntimeState { + animation_playback, + active_step_state, + lifecycle_action_targets, + pet_animations, + requested_animation, + scripted_walk_state, + .. + } = &self.runtime; + let (step_response, completed_play_action_behavior) = { + let mut active_step_state = active_step_state.borrow_mut(); + let completion_behavior = + native_pet_active_play_action_completion_behavior(&active_step_state); + let response = native_pet_advance_active_step(&mut active_step_state, elapsed_ms); + let completed_behavior = response.as_ref().and_then(|response| { + native_pet_play_action_completion_behavior_for_response( + completion_behavior, + response, + ) + }); + (response, completed_behavior) + }; + if let Some(response) = step_response { + if native_pet_step_response_is_motion_timeout(&response) { + scripted_walk_state.replace(None); + requested_animation.set(NativePetRequestedAnimationState::from( + lifecycle_action_targets.idle(), + )); + } + let _ = emit_native_pet_sidecar_event(NativePetSidecarEvent::StepResponse(response)); + } + let mut playback = animation_playback.get(); + if let Some(completion_behavior) = completed_play_action_behavior { + native_pet_apply_completed_play_action_behavior( + completion_behavior, + pet_animations.as_ref(), + lifecycle_action_targets.as_ref(), + &mut playback, + requested_animation.as_ref(), + ); + } + playback + } + + fn advance_drag( + &self, + gtk_window: >k::Window, + playback: &mut NativePetAnimationPlayback, + frame_time: i64, + ) -> bool { + let NativePetRuntimeState { + drag_state, + movement_action_targets, + pet_facing, + .. + } = &self.runtime; + let window_size = self.window_size; + let drag_debug = self.drag_debug; + let drag_phase = { + let mut phase = NativePetDragPhase::Idle; + let mut drag_state = drag_state.borrow_mut(); + if let Some(state) = drag_state.as_mut() { + phase = state.phase(); + let frame_time_ms = native_pet_frame_clock_time_ms(frame_time); + if let Some(update) = + native_pet_take_drag_frame_update(state.motion_mut(), frame_time_ms) + { + let movement_adapter = self.movement_adapter(gtk_window); + native_pet_commit_drag_update( + state, + update, + NativePetDragCommitContext { + playback, + movement_action_targets: movement_action_targets.as_ref(), + pet_facing: pet_facing.as_ref(), + movement_adapter: &movement_adapter, + window_size, + drag_debug, + }, + ); + } + } + phase + }; + !matches!(drag_phase, NativePetDragPhase::Idle) + } + + fn advance_inertia( + &self, + gtk_window: >k::Window, + playback: &mut NativePetAnimationPlayback, + frame_dt_seconds: f64, + ) -> bool { + let NativePetRuntimeState { + active_step_state, + edge_runout_state, + fallen_preset_behavior_recovery_state, + inertia_state, + lifecycle_action_targets, + movement_action_targets, + pet_animations, + pet_facing, + physics_params, + requested_animation, + throw_outcome_seed, + .. + } = &self.runtime; + let Self { + throw_after_drag_finish_targets, + .. + } = self; + let window_size = self.window_size; + let drag_debug = self.drag_debug; + let mut inertia_state = inertia_state.borrow_mut(); + if let Some(state) = inertia_state.as_mut() { + let movement_adapter = self.movement_adapter(gtk_window); + let impact_velocity = state.velocity(); + let step = state.step(frame_dt_seconds, physics_params, |position| { + native_pet_runtime_resolve_window_placement(position, window_size, None) + .placement + .position + }); + let placement = + native_pet_runtime_resolve_window_placement(step.position, window_size, None) + .placement; + if step.velocity.x.abs() > 1.0 { + pet_facing.set(if step.velocity.x > 0.0 { + NativePetFacing::Right + } else { + NativePetFacing::Left + }); + } + movement_adapter.move_to(placement); + if matches!(step.phase, NativePetPhysicsPhase::Inertia) { + playback.set_animation_target(native_pet_animation_for_velocity( + movement_action_targets.as_ref(), + step.velocity, + pet_facing.get(), + )); + if drag_debug { + eprintln!( + "native-pet-drag-debug inertia dt={:.4}s position=({}, {}) velocity=({:.1},{:.1}) speed={:.1} clamped={}", + step.clamped_dt_seconds, + step.position.x, + step.position.y, + step.velocity.x, + step.velocity.y, + step.velocity.speed(), + step.hit_position_clamp, + ); + } + true + } else { + inertia_state.take(); + let run_facing = native_pet_facing_for_velocity(impact_velocity, pet_facing.get()); + let throw_finish = native_pet_throw_after_drag_finish_after_runout( + run_facing, + throw_outcome_seed.get(), + ); + let finish_animation = + throw_after_drag_finish_targets.animation_target(throw_finish); + let interaction_uuid = native_pet_new_preset_behavior_interaction_uuid(); + let interaction_id = native_pet_preset_behavior_interaction_id(interaction_uuid); + fallen_preset_behavior_recovery_state.replace( + native_pet_fallen_recovery_state_after_throw_finish( + throw_finish, + interaction_id.clone(), + ), + ); + let _ = emit_native_pet_sidecar_event(NativePetSidecarEvent::PresetBehavior( + native_pet_throw_after_drag_preset_behavior_event( + throw_finish, + finish_animation, + interaction_id.clone(), + pet_animations.as_ref(), + ), + )); + throw_outcome_seed + .set(native_pet_next_throw_outcome_seed(throw_outcome_seed.get())); + if let Some(runout) = native_pet_edge_runout_after_inertia_step( + movement_action_targets.as_ref(), + step.hit_position_clamp, + pet_facing.get(), + impact_velocity, + finish_animation, + interaction_uuid, + physics_params, + ) { + edge_runout_state.set(Some(runout)); + } else { + native_pet_start_preset_behavior_execute_step( + active_step_state.as_ref(), + pet_animations.as_ref(), + playback, + requested_animation.as_ref(), + finish_animation, + lifecycle_action_targets.as_ref().idle(), + interaction_id.as_str(), + ); + } + if drag_debug { + eprintln!( + "native-pet-drag-debug inertia-end dt={:.4}s position=({}, {}) clamped={}", + step.clamped_dt_seconds, + step.position.x, + step.position.y, + step.hit_position_clamp, + ); + } + false + } + } else { + false + } + } + + fn advance_edge_runout( + &self, + playback: &mut NativePetAnimationPlayback, + elapsed_ms: u64, + ) -> bool { + let NativePetRuntimeState { + active_step_state, + edge_runout_state, + lifecycle_action_targets, + pet_animations, + requested_animation, + .. + } = &self.runtime; + if let Some(state) = edge_runout_state.get() { + let step = native_pet_advance_edge_runout(state, elapsed_ms); + edge_runout_state.set(step.next_state); + if step.next_state.is_some() { + playback.set_animation_target(step.animation); + } else { + let interaction_id = native_pet_preset_behavior_interaction_id( + state.preset_behavior_interaction_uuid, + ); + native_pet_start_preset_behavior_execute_step( + active_step_state.as_ref(), + pet_animations.as_ref(), + playback, + requested_animation.as_ref(), + step.animation, + lifecycle_action_targets.as_ref().idle(), + interaction_id.as_str(), + ); + } + step.next_state.is_some() + } else { + false + } + } + + fn advance_scripted_walk( + &self, + gtk_window: >k::Window, + playback: &mut NativePetAnimationPlayback, + elapsed_ms: u64, + ) -> bool { + let NativePetRuntimeState { + active_step_state, + lifecycle_action_targets, + movement_action_targets, + pet_animations, + pet_facing, + requested_animation, + scripted_walk_state, + .. + } = &self.runtime; + let Self { + preferences, + window_position, + .. + } = self; + let window_size = self.window_size; + self.apply_window_layer( + gtk_window, + native_pet_layer_for_scripted_walk( + NativePetLayer::from_always_on_top(preferences.get().always_on_top), + scripted_walk_state.borrow().as_ref(), + ), + ); + let mut scripted_walk_state = scripted_walk_state.borrow_mut(); + if let Some(state) = scripted_walk_state.as_mut() { + let movement_adapter = self.movement_adapter(gtk_window); + let step = native_pet_step_scripted_walk( + movement_action_targets.as_ref(), + window_position.get(), + state.target_position(), + elapsed_ms, + ); + let placement = + native_pet_runtime_resolve_window_placement(step.position, window_size, None) + .placement; + if step.movement_dx.abs() > 1.0 { + pet_facing.set(if step.movement_dx > 0.0 { + NativePetFacing::Right + } else { + NativePetFacing::Left + }); + } + movement_adapter.move_to(placement); + playback.set_animation_target(step.animation); + if step.finished { + match state.advance_after_arrival(elapsed_ms) { + NativePetScriptedWalkArrival::Holding => { + let after_animation = state + .after_animation + .unwrap_or_else(|| lifecycle_action_targets.as_ref().idle()); + playback.set_animation_target(after_animation); + true + } + NativePetScriptedWalkArrival::Advanced => true, + NativePetScriptedWalkArrival::Finished => { + let after_animation = state + .after_animation + .unwrap_or_else(|| lifecycle_action_targets.as_ref().idle()); + let requested = native_pet_requested_animation_for_control_animation( + pet_animations, + after_animation, + lifecycle_action_targets.as_ref().idle(), + ); + requested_animation.set(requested); + playback.restart_animation_target(after_animation); + *scripted_walk_state = None; + let step_response = { + let mut active_step_state = active_step_state.borrow_mut(); + native_pet_complete_active_step(&mut active_step_state) + }; + if let Some(response) = step_response { + let _ = emit_native_pet_sidecar_event( + NativePetSidecarEvent::StepResponse(response), + ); + } + false + } + } + } else { + true + } + } else { + false + } + } + + fn reconcile_rest_position(&self, gtk_window: >k::Window, motion: NativePetFrameMotion) { + let Self { + layer_shell, + preferences, + position_state_path, + pending_rest_position_save, + window_position, + window_monitor_index, + placement_refresh_elapsed_ms, + .. + } = self; + let window_size = self.window_size; + let drag_debug = self.drag_debug; + let NativePetFrameMotion { + is_dragging, + is_inertia_active, + is_edge_runout_active, + is_scripted_walk_active, + } = motion; + let is_motion_locked = motion.is_locked(); + if should_persist_native_pet_rest_position( + pending_rest_position_save.get(), + is_dragging, + is_inertia_active, + is_edge_runout_active, + is_scripted_walk_active, + ) { + pending_rest_position_save.set(false); + if preferences.get().remember_position { + if let Err(error) = save_native_pet_position_state( + position_state_path.as_ref(), + window_position.get(), + ) { + eprintln!("Lexora Buddy native pet position save failed: {error}"); + } + } + } + + if !is_motion_locked { + let force_native_refresh = layer_shell.is_some() + && placement_refresh_elapsed_ms.get() >= NATIVE_PET_PLACEMENT_REFRESH_MS; + let movement_adapter = self.movement_adapter(gtk_window); + native_pet_reconcile_visible_placement( + &movement_adapter, + window_position.get(), + window_monitor_index.get(), + window_size, + force_native_refresh, + drag_debug, + ); + if force_native_refresh { + placement_refresh_elapsed_ms.set(0); + } + } + } + + fn poll_control( + &self, + gtk_window: >k::Window, + playback: &mut NativePetAnimationPlayback, + motion: NativePetFrameMotion, + ) -> glib::ControlFlow { + let NativePetRuntimeState { + active_step_state, + control_messages, + drag_state, + edge_runout_state, + idle_lifecycle_elapsed_ms, + idle_presence_schedule_seed, + inertia_state, + lifecycle_action_targets, + pet_animations, + pointer_hovered, + requested_animation, + scripted_walk_state, + task_presence_elapsed_ms, + .. + } = &self.runtime; + let Self { + preferences, + config_path, + position_state_path, + pending_rest_position_save, + should_quit, + window_position, + window_monitor_index, + .. + } = self; + let window_size = self.window_size; + let is_motion_locked = motion.is_locked(); + let is_dragging_for_control_state = drag_state.borrow().is_some(); + let control_poll = + native_pet_drain_control_runtime_requests(NativePetControlRuntimeState { + active_step_state: active_step_state.as_ref(), + control_messages: control_messages.as_ref(), + pet_animations: pet_animations.as_ref(), + lifecycle_action_targets: lifecycle_action_targets.as_ref(), + playback, + requested_animation: requested_animation.as_ref(), + pointer_hovered: pointer_hovered.as_ref(), + idle_lifecycle_elapsed_ms: idle_lifecycle_elapsed_ms.as_ref(), + idle_presence_schedule_seed: idle_presence_schedule_seed.as_ref(), + task_presence_elapsed_ms: task_presence_elapsed_ms.as_ref(), + inertia_state: inertia_state.as_ref(), + edge_runout_state: edge_runout_state.as_ref(), + scripted_walk_state: scripted_walk_state.as_ref(), + window_position, + window_monitor_index, + window_size, + is_dragging: is_dragging_for_control_state, + is_motion_locked, + config_path: config_path.as_ref(), + position_state_path: position_state_path.as_ref(), + preferences, + pending_rest_position_save, + should_quit, + }); + if matches!(control_poll, NativePetControlPoll::Disconnected) { + gtk::main_quit(); + return glib::ControlFlow::Break; + } + if should_quit.get() { + gtk::main_quit(); + return glib::ControlFlow::Break; + } + self.apply_window_layer( + gtk_window, + native_pet_layer_for_scripted_walk( + NativePetLayer::from_always_on_top(preferences.get().always_on_top), + scripted_walk_state.borrow().as_ref(), + ), + ); + + glib::ControlFlow::Continue + } +} diff --git a/apps/buddy/package.json b/apps/buddy/package.json index eb4c24da6..9772db248 100644 --- a/apps/buddy/package.json +++ b/apps/buddy/package.json @@ -24,10 +24,12 @@ "main": "./.output/build/electron/main/index.js", "scripts": { "predev": "install-electron --no && node ../../packaging/buddy/release/prepare-desktop-features.mjs", - "dev": "electron-vite dev", + "dev": "node electron/dev.mjs", "build": "pnpm build:desktop", - "build:electron": "vue-tsc -b --pretty false && electron-vite build", - "build:desktop": "node ../../packaging/buddy/release/prepare-desktop-features.mjs --release && pnpm build:electron", + "build:electron": "pnpm type-check && electron-vite build", + "build:service": "pnpm type-check:service && node ../../packaging/buddy/release/build-service.mjs", + "type-check:service": "tsc -p tsconfig.service.json --pretty false", + "build:desktop": "node ../../packaging/buddy/release/prepare-desktop-features.mjs --release && pnpm build:service && node ../../packaging/buddy/release/node-runtime.mjs && pnpm build:electron && node ../../packaging/buddy/release/product-release-set.mjs", "build:native-pet": "cargo build --locked --target-dir .output/build/native --manifest-path native/Cargo.toml --package lexora-buddy-pet --bin lexora-buddy-pet", "build:native-pet:release": "cargo build --locked --release --target-dir .output/build/native --manifest-path native/Cargo.toml --package lexora-buddy-pet --bin lexora-buddy-pet", "clean": "node ../../packaging/buddy/release/clean-output.mjs", @@ -39,14 +41,15 @@ "package:pet": "pnpm build:native-pet:release && node ../../packaging/buddy/release/package-pet.mjs", "lint": "eslint .", "lint:fix": "eslint . --fix", - "type-check": "vue-tsc -b --pretty false", + "type-check": "vue-tsc --incremental --noEmit -p tsconfig.app.json --pretty false && tsc --incremental -p tsconfig.node.json --pretty false && pnpm type-check:service", "prepare:test-runtime": "node ../../packaging/buddy/release/search-tools.mjs && node ../../packaging/buddy/release/shell-sandbox.mjs && node ../../packaging/buddy/release/native-host.mjs", "test": "vitest run --maxWorkers=4", "test:unit": "pnpm test --project=unit", "test:runtime": "pnpm test --project=runtime", "test:platform": "pnpm test --project=platform", "test:sandbox:windows": "node --test native/host/__tests__/shell_sandbox.windows.mjs", - "test:watch": "vitest" + "test:watch": "vitest", + "build:mobile-reader": "vite build --config mobile-reader.vite.config.ts" }, "dependencies": { "@anthropic-ai/sandbox-runtime": "0.0.80", @@ -57,8 +60,10 @@ "@js-temporal/polyfill": "^0.5.1", "@modelcontextprotocol/client": "2.3.1", "@mozilla/readability": "^0.6.0", + "@peculiar/x509": "3.1.0", "@silvia-odwyer/photon-node": "0.3.4", "@smithy/types": "4.19.0", + "core-js": "3.50.0", "diff": "^9.0.0", "electron-updater": "6.8.9", "eventemitter2": "6.4.9", @@ -66,12 +71,14 @@ "linkedom": "^0.18.13", "minisearch": "7.2.0", "proxy-chain": "^3.0.1", + "reflect-metadata": "0.2.2", "semver": "^7.8.5", "smol-toml": "^1.9.1", "turndown": "^7.2.4", "typebox": "^1.3.36", "undici": "8.12.3", "unpdf": "^1.8.1", + "ws": "8.21.0", "yaml": "2.9.0", "zod": "^4.6.6" }, @@ -90,6 +97,7 @@ "@types/node": "catalog:", "@types/semver": "^7.8.0", "@types/turndown": "^5.0.6", + "@types/ws": "8.18.1", "@unocss/core": "66.10.5", "@vicons/fluent": "^0.13.0", "@vitejs/plugin-vue": "^6.0.10", diff --git a/apps/buddy/platform/extensions/ExtensionDownloads.ts b/apps/buddy/platform/extensions/ExtensionDownloads.ts new file mode 100644 index 000000000..26fd5cffd --- /dev/null +++ b/apps/buddy/platform/extensions/ExtensionDownloads.ts @@ -0,0 +1,26 @@ +export class ExtensionDownloads { + readonly #entries = new Map() + + async run(id: string, host: object, owner: string, signals: AbortSignal[], operation: (signal: AbortSignal) => Promise): Promise { + if (this.#entries.has(id)) + throw new Error('EXTENSION_RESOURCE_DOWNLOAD_EXISTS') + const abort = new AbortController() + const entry = { host, owner, abort } + this.#entries.set(id, entry) + try { + return await operation(AbortSignal.any([...signals, abort.signal])) + } + finally { + this.#entries.delete(id) + } + } + + cancel(id: string, host: object, owner: string): void { + const entry = this.#entries.get(id) + if (!entry) + return + if (entry.host !== host || entry.owner !== owner) + throw new Error('EXTENSION_RESOURCE_DENIED') + entry.abort.abort(new Error('EXTENSION_RESOURCE_DOWNLOAD_CANCELLED')) + } +} diff --git a/apps/buddy/platform/extensions/ExtensionInferenceRequests.ts b/apps/buddy/platform/extensions/ExtensionInferenceRequests.ts new file mode 100644 index 000000000..b6321d28d --- /dev/null +++ b/apps/buddy/platform/extensions/ExtensionInferenceRequests.ts @@ -0,0 +1,204 @@ +import type { InferenceResourceProgress } from '../../shared/extensions/extensionInference' +import type { JsonValue } from '../../shared/workbench/workbenchState' +import type { ExtensionDownloads } from './ExtensionDownloads' +import type { ExtensionInference, ExtensionInferencePorts } from './ExtensionInference' +import type { InferenceResourceSource } from './ExtensionInferenceResources' +import type { ExtensionPackage, ExtensionPackageStore } from './ExtensionPackageStore' +import process from 'node:process' +import { z } from 'zod' +import { inferenceBindingSchema, inferenceCredentialReferenceSchema, inferenceCredentialSchema, inferenceHardwareSchema, inferenceHashSchema, inferenceIdSchema, inferencePrepareSchema, inferenceRuntimeReferenceSchema } from '../../shared/extensions/extensionInference' +import { resolveInferenceEnvironment } from '../../shared/extensions/inferencePlatform' + +interface ExtensionInferenceRequestContext { + identity: object + manifest: ExtensionPackage['manifest'] + signal: AbortSignal + inference?: ExtensionInference + preparations: Set> + call: (method: string, params: JsonValue, signal?: AbortSignal) => Promise + source: () => InferenceResourceSource + assertCurrent: () => void + changed: () => void +} + +export class ExtensionInferenceRequests { + readonly #store: ExtensionPackageStore + readonly #ports: ExtensionInferencePorts | undefined + readonly #downloads: ExtensionDownloads + + constructor(store: ExtensionPackageStore, inference: ExtensionInferencePorts | undefined, downloads: ExtensionDownloads) { + this.#store = store + this.#ports = inference + this.#downloads = downloads + } + + source(manifest: ExtensionPackage['manifest'], inference: ExtensionInference | undefined, access: { + assertCurrent: () => void + authorizeDownload: (url: string) => void + networkResponse: (url: string, signal: AbortSignal, headers?: Record) => Promise + }): InferenceResourceSource { + const { id, permissions } = manifest + return { + prepare: manifest.contributes.inference?.preparation && inference + ? (...args) => inference!.prepareResources(...args) + : undefined, + assertDownload: (url) => { + access.authorizeDownload(url) + access.assertCurrent() + }, + response: (source, signal, headers) => { + if ('directoryId' in source) + throw new Error('EXTENSION_INFERENCE_REFERENCE_REQUIRED') + return 'url' in source ? access.networkResponse(source.url, signal, headers) : this.#store.resources.response(id, source.resourceId, new Request('https://resource.invalid/'), signal, permissions.localResources) + }, + directoryReferencePath: async (input) => { + access.assertCurrent() + if (!permissions.localResources) + throw new Error('EXTENSION_PERMISSION_DENIED') + const path = await this.#store.resources.directoryReferencePath(id, input) + access.assertCurrent() + return path + }, + referencePath: async (resourceId) => { + access.assertCurrent() + if (!permissions.localResources) + throw new Error('EXTENSION_PERMISSION_DENIED') + const path = await this.#store.resources.referencePath(id, resourceId) + access.assertCurrent() + return path + }, + openDirectoryReference: async (input) => { + access.assertCurrent() + if (!permissions.localResources) + throw new Error('EXTENSION_PERMISSION_DENIED') + const handle = await this.#store.resources.openDirectoryReference(id, input) + try { + access.assertCurrent() + return handle + } + catch (error) { + await handle.close() + throw error + } + }, + openReference: async (resourceId) => { + access.assertCurrent() + if (!permissions.localResources) + throw new Error('EXTENSION_PERMISSION_DENIED') + return this.#store.resources.openReference(id, resourceId) + }, + } + } + + async request(context: ExtensionInferenceRequestContext, method: string, params: unknown, lifetime: AbortSignal, owner: string): Promise { + const { id, permissions } = context.manifest + if (!permissions.inference) + throw new Error('EXTENSION_PERMISSION_DENIED') + if (method.startsWith('inference.credentials.')) { + const credentials = this.#ports?.credentials + if (!credentials || !context.inference) + throw new Error('EXTENSION_INFERENCE_UNAVAILABLE') + const assertCurrent = () => context.assertCurrent() + if (method === 'inference.credentials.remove') { + const input = z.object({ id: inferenceIdSchema }).strict().parse(params) + await context.inference.withStoppedCredential(input.id, () => credentials.remove(id, input.id, assertCurrent)) + context.changed() + return null + } + if (method === 'inference.credentials.has' || method === 'inference.credentials.set') { + const input = method === 'inference.credentials.set' ? inferenceCredentialSchema.parse(params) : inferenceCredentialReferenceSchema.parse(params) + if (!permissions.inferenceNetwork.includes(input.origin)) + throw new Error('EXTENSION_PERMISSION_DENIED') + if ('token' in input) { + await context.inference.withStoppedCredential(input.id, () => credentials.set(id, inferenceCredentialSchema.parse(input), assertCurrent)) + context.changed() + return null + } + return credentials.has(id, input) + } + throw new Error('EXTENSION_METHOD_DENIED') + } + if (method === 'inference.environment') { + const hardware = inferenceHardwareSchema.safeParse(await this.#ports?.hardware?.(permissions.inferenceGpu).catch(() => undefined)) + context.assertCurrent() + return { + ...resolveInferenceEnvironment(process.platform, process.arch), + ...(hardware.success ? { hardware: { ...hardware.data, gpus: permissions.inferenceGpu ? hardware.data.gpus : [] } } : {}), + } + } + if (method === 'inference.status') + return context.inference?.status() ?? [] + if (method === 'inference.stop') { + const input = params === null ? undefined : inferenceRuntimeReferenceSchema.parse(params) + if (input && !context.inference) + throw new Error('EXTENSION_INFERENCE_RUNTIME_UNAVAILABLE') + await context.inference?.stop(input) + return null + } + if (method === 'inference.unbind') { + const input = inferenceRuntimeReferenceSchema.parse(params) + if (!context.inference) + throw new Error('EXTENSION_INFERENCE_RUNTIME_UNAVAILABLE') + await context.inference.unbind(input) + context.changed() + return null + } + if (method === 'inference.resources.list') { + const [files, directories] = await Promise.all([this.#store.resources.list(id, permissions.localResources), permissions.localResources ? this.#store.resources.directories(id) : []]) + return this.#store.inference.inspect(id, new Set(files.map(file => file.id)), new Set(directories.map(directory => directory.id))) + } + if (method === 'inference.resources.remove') { + const resource = z.object({ id: inferenceIdSchema, revision: inferenceHashSchema }).strict().parse(params) + await this.#store.inference.remove(id, resource, () => context.assertCurrent()) + context.inference?.forgetResource(resource.id, resource.revision) + context.changed() + return null + } + if (method === 'inference.bind') { + if (!context.inference) + throw new Error('EXTENSION_INFERENCE_UNAVAILABLE') + await context.inference.bind(inferenceBindingSchema.parse(params)) + context.changed() + return null + } + if (method === 'inference.resources.prepare') { + const input = z.object({ id: z.uuid(), resource: inferencePrepareSchema }).strict().parse(params) + return this.#downloads.run(input.id, context.identity, owner, [lifetime, context.signal], async (signal) => { + const pendingProgress: InferenceResourceProgress[] = [] + let publishing: Promise | undefined + const publish = () => { + if (publishing || signal.aborted) + return + const next = pendingProgress.shift() + if (!next) + return + publishing = context.call('inference.progress', { id: input.id, progress: { ...next } }, signal).then(() => {}, () => {}).finally(() => { + publishing = undefined + publish() + }) + } + const report = (progress: InferenceResourceProgress) => { + if (signal.aborted) + return + const last = pendingProgress.at(-1) + if (last && last.path === progress.path && last.phase === progress.phase && last.completedFiles === progress.completedFiles && last.bytes > 0 && progress.bytes > 0) + pendingProgress[pendingProgress.length - 1] = progress + else + pendingProgress.push(progress) + publish() + } + const preparing = this.#store.inference.prepare(id, input.resource, context.source(), signal, () => context.assertCurrent(), report) + context.preparations.add(preparing) + try { + return await preparing + } + finally { + for (let pending = publishing; pending; pending = publishing) + await pending + context.preparations.delete(preparing) + } + }) + } + throw new Error('EXTENSION_METHOD_DENIED') + } +} diff --git a/apps/buddy/platform/extensions/ExtensionInstallations.ts b/apps/buddy/platform/extensions/ExtensionInstallations.ts index 345ae321d..214ba5af9 100644 --- a/apps/buddy/platform/extensions/ExtensionInstallations.ts +++ b/apps/buddy/platform/extensions/ExtensionInstallations.ts @@ -49,11 +49,13 @@ export class ExtensionInstallations { return controller.signal } - identify(id: string, extensionId: string, version: string): void { + identify(id: string, extensionId: string, version: string, executionScope?: ExtensionInstallation['executionScope']): void { const record = this.#records.find(item => item.id === id) if (record) { record.extensionId = extensionId record.version = version + if (executionScope !== undefined) + record.executionScope = executionScope this.#save() } } diff --git a/apps/buddy/platform/extensions/ExtensionInvocationScope.ts b/apps/buddy/platform/extensions/ExtensionInvocationScope.ts new file mode 100644 index 000000000..1faecb6e6 --- /dev/null +++ b/apps/buddy/platform/extensions/ExtensionInvocationScope.ts @@ -0,0 +1,21 @@ +import { AsyncOperationScope } from '../../shared/lifecycle/AsyncOperationScope' + +export class ExtensionInvocationScope { + readonly abort = new AbortController() + readonly signal: AbortSignal + readonly #operations = new AsyncOperationScope(() => new Error('EXTENSION_INVOCATION_EXPIRED')) + + constructor(signals: AbortSignal[]) { + this.signal = AbortSignal.any([...signals, this.abort.signal]) + } + + track(operation: () => Promise): Promise { + return this.#operations.track(operation) + } + + async finish(): Promise { + this.#operations.stopAccepting() + this.abort.abort() + await this.#operations.drain() + } +} diff --git a/apps/buddy/platform/extensions/ExtensionPackageStore.ts b/apps/buddy/platform/extensions/ExtensionPackageStore.ts index 8da9dff5d..c18189b33 100644 --- a/apps/buddy/platform/extensions/ExtensionPackageStore.ts +++ b/apps/buddy/platform/extensions/ExtensionPackageStore.ts @@ -34,7 +34,7 @@ const grantSchema = z.union([ z.object({ resource: extensionResourceSchema, source: extensionFileOpeningSourceSchema }).strict(), ]) export type { ExtensionPackage, InstalledExtension } from './extensionState' -export interface ExtensionCandidate { token: string, package: ExtensionPackage, files: Map, development: boolean, expires: number, fileAssociations: ReviewedFileOpening[] } +export interface ExtensionCandidate { token: string, package: ExtensionPackage, files: Map, development: boolean, expires: number, fileAssociations: ReviewedFileOpening[], currentRevision: string | null, dependencyRevisions: Record } export class ExtensionPackageStore { readonly root: string @@ -159,6 +159,7 @@ export class ExtensionPackageStore { throw unavailable.error if (current && !development && !gt(manifest.version, current.current.manifest.version)) throw new Error('EXTENSION_VERSION_NOT_NEWER') + const dependencyRevisions = serviceDependencyRevisions(this.installed, { manifest, hashes, revision, source }) const token = randomUUID() const snapshot = this.fileOpeners() const fileAssociations: FileOpeningReviewChoice[] = manifest.contributes.fileHandlers.flatMap(provider => provider.extensions.flatMap(extension => extensionFileHandlerBindings(provider).map(({ location, requestDefault }) => { @@ -166,12 +167,14 @@ export class ExtensionPackageStore { const currentProviderId = resolved.preferredId ?? resolved.provider?.id ?? null return { extension, location, providerId: provider.id, title: provider.title, currentProviderId, requestDefault } }))) - this.#candidates.set(token, { token, files: new Map(files), development, expires: Date.now() + 600000, package: { manifest, hashes, revision, source }, fileAssociations: fileAssociations.map(({ extension, location, providerId }) => ({ choice: { extension, location, providerId }, revision: this.#index.fileOpeningRevisions[extension]?.[location] ?? 0 })) }) + this.#candidates.set(token, { token, currentRevision: current?.current.revision ?? null, dependencyRevisions, files: new Map(files), development, expires: Date.now() + 600000, package: { manifest, hashes, revision, source }, fileAssociations: fileAssociations.map(({ extension, location, providerId }) => ({ choice: { extension, location, providerId }, revision: this.#index.fileOpeningRevisions[extension]?.[location] ?? 0 })) }) const addedPermissions = addedExtensionPermissions(current?.current.manifest.permissions, manifest.permissions) + if (manifest.executionScope === 'service' && current?.current.manifest.executionScope !== 'service') + addedPermissions.push('executionScope:service') const previousSupervisedPlatforms = supervisedInferencePlatforms(current?.current.manifest.contributes.inference) if (supervisedInferencePlatforms(manifest.contributes.inference).some(platform => !previousSupervisedPlatforms.includes(platform)) && !addedPermissions.includes('inference')) addedPermissions.push('inference') - return { token, manifest, iconUrl, sha256: revision, development, currentVersion: current?.current.manifest.version ?? null, addedPermissions, fileAssociations } + return { token, manifest, iconUrl, sha256: revision, development, currentVersion: current?.current.manifest.version ?? null, previousExecutionScope: current?.current.manifest.executionScope ?? null, addedPermissions, fileAssociations } } #checkReviewBytes(files: ReadonlyMap, replaced?: string): void { @@ -240,6 +243,7 @@ export class ExtensionPackageStore { const current = this.installed[pkg.manifest.id] if (current && !candidate.development && !gt(pkg.manifest.version, current.current.manifest.version)) throw new Error('EXTENSION_VERSION_NOT_NEWER') + this.#assertReviewedDependencies(candidate, this.installed) const parent = join(this.root, 'packages', pkg.manifest.id) const target = this.packageRoot(pkg) const temporary = join(parent, `.install-${randomUUID()}`) @@ -262,6 +266,7 @@ export class ExtensionPackageStore { } await this.#update((index) => { signal?.throwIfAborted() + this.#assertReviewedDependencies(candidate, index.installed) const old = index.installed[pkg.manifest.id] if (old && !candidate.development && !gt(pkg.manifest.version, old.current.manifest.version)) throw new Error('EXTENSION_VERSION_NOT_NEWER') @@ -281,6 +286,13 @@ export class ExtensionPackageStore { } } + #assertReviewedDependencies(candidate: ExtensionCandidate, installed: Readonly>): void { + if ((installed[candidate.package.manifest.id]?.current.revision ?? null) !== candidate.currentRevision + || !isDeepStrictEqual(serviceDependencyRevisions(installed, candidate.package), candidate.dependencyRevisions)) { + throw new Error('EXTENSION_REVIEW_CHANGED') + } + } + fileOpeners(): FileOpeningSnapshot { const providers = [...builtinFileOpeningProviders] for (const [id, record] of Object.entries(this.installed)) { @@ -332,6 +344,8 @@ export class ExtensionPackageStore { if (!record) throw new Error('EXTENSION_NOT_INSTALLED') record.enabled = enabled + if (enabled && record.current.manifest.executionScope === 'service') + extensionActivationOrder(index.installed, id) }) } @@ -347,6 +361,7 @@ export class ExtensionPackageStore { const issue = extensionCompatibilityIssue(record.pending.manifest, this.appVersion) if (issue) throw new ExtensionCompatibilityError(record.pending.manifest, issue) + serviceDependencyRevisions(index.installed, record.pending) record.current = record.pending record.pending = null this.#applyFileOpeners(index, record.pendingFileOpeners) @@ -548,6 +563,8 @@ export function extensionActivationOrder(installed: Readonly>, candidate: ExtensionPackage): Record { + const id = candidate.manifest.id + const next = { ...installed, [id]: { current: candidate, pending: null, pendingFileOpeners: [], enabled: true, development: false } } + const dependencies = new Map() + for (const [rootId, record] of Object.entries(next)) { + if (!record.enabled || record.current.manifest.executionScope !== 'service') + continue + if (rootId !== id && !dependsOn(next, rootId, id)) + continue + const closure = extensionActivationOrder(next, rootId) + for (const dependency of closure) { + if (dependency !== id) + dependencies.set(dependency, next[dependency]!.current.revision) + } + } + return Object.fromEntries([...dependencies].sort(([left], [right]) => left.localeCompare(right))) +} + +function dependsOn(installed: Readonly>, root: string, target: string, seen = new Set()): boolean { + if (root === target) + return true + if (seen.has(root)) + return false + seen.add(root) + return Object.keys(installed[root]?.current.manifest.dependencies ?? {}).some(dependency => dependsOn(installed, dependency, target, seen)) +} diff --git a/apps/buddy/platform/extensions/ExtensionSelectedResources.ts b/apps/buddy/platform/extensions/ExtensionSelectedResources.ts new file mode 100644 index 000000000..132f2b3bd --- /dev/null +++ b/apps/buddy/platform/extensions/ExtensionSelectedResources.ts @@ -0,0 +1,130 @@ +import type { ExtensionFileOpeningSource, ExtensionResource } from '../../shared/extensions/extensionApi' +import type { TaskFileTarget } from '../../shared/files/taskFileApi' +import type { SpaceFileTarget } from '../../shared/spaces/spaceFileApi' +import type { ExtensionPackage, ExtensionPackageStore } from './ExtensionPackageStore' +import { Buffer } from 'node:buffer' +import { randomUUID } from 'node:crypto' +import { fileRange } from '../filesystem/fileRange' +import { readResponseBytes } from '../network/publicWebTransport' + +export interface ExtensionSelectedResourceAccess { + owner: object + manifest: ExtensionPackage['manifest'] + signal: AbortSignal + assertCurrent: () => void +} + +interface ExtensionSelectedResourcePorts { + readText: (target: SpaceFileTarget, signal: AbortSignal) => Promise + readResource?: (target: SpaceFileTarget, signal: AbortSignal, request?: Request) => Promise + readArtifactResource?: (artifactId: string, signal: AbortSignal, request?: Request) => Promise<{ name: string, response: Response }> + readTaskFileResource?: (target: TaskFileTarget, signal: AbortSignal, request?: Request) => Promise<{ name: string, response: Response }> +} + +export class ExtensionSelectedResources { + readonly #store: Pick + readonly #ports: ExtensionSelectedResourcePorts + readonly #snapshots = new Map() + + constructor(store: Pick, ports: ExtensionSelectedResourcePorts) { + this.#store = store + this.#ports = ports + } + + hasSnapshot(id: string): boolean { return this.#snapshots.has(id) } + + snapshot(access: ExtensionSelectedResourceAccess, source: ExtensionFileOpeningSource, name: string, text: string, retained: (id: string) => boolean): ExtensionResource { + const bytes = Buffer.byteLength(text) + for (const [id, entry] of this.#snapshots) { + if (entry.signal.aborted || (entry.created < Date.now() - 600000 && !retained(id))) + this.#snapshots.delete(id) + } + if (bytes > 2 * 1024 * 1024 || this.#snapshots.size >= 64 || [...this.#snapshots.values()].reduce((total, entry) => total + entry.bytes, bytes) > 16 * 1024 * 1024) + throw new Error('EXTENSION_RESOURCE_TEXT_LIMIT') + const resource = { id: randomUUID(), name } + this.#snapshots.set(resource.id, { owner: access.owner, signal: access.signal, source: structuredClone(source), text, bytes, created: Date.now() }) + return resource + } + + release(id: string): void { this.#snapshots.delete(id) } + + releaseOwner(owner: object): void { + for (const [id, snapshot] of this.#snapshots) { + if (snapshot.owner === owner) + this.#snapshots.delete(id) + } + } + + async resolve(access: ExtensionSelectedResourceAccess, id: string): Promise { + if (access.manifest.permissions.selectedResource !== 'read') + throw new Error('EXTENSION_RESOURCE_DENIED') + const snapshot = this.#snapshots.get(id) + if (snapshot && snapshot.owner !== access.owner) + throw new Error('EXTENSION_RESOURCE_DENIED') + const source = snapshot?.source ?? await this.#store.resolveFileOpeningGrant(access.manifest.id, id) + access.assertCurrent() + return source + } + + async sourceResponse(source: ExtensionFileOpeningSource, signal: AbortSignal, request?: Request): Promise<{ name: string, response: Response }> { + if (source.kind === 'artifact') { + if (!this.#ports.readArtifactResource) + throw new Error('EXTENSION_RESOURCE_UNAVAILABLE') + return this.#ports.readArtifactResource(source.artifactId, signal, request) + } + if (source.kind === 'task') { + if (!this.#ports.readTaskFileResource) + throw new Error('EXTENSION_RESOURCE_UNAVAILABLE') + return this.#ports.readTaskFileResource(source.target, signal, request) + } + const response = this.#ports.readResource + ? await this.#ports.readResource(source.target, signal, request) + : new Response(await this.#ports.readText(source.target, signal)) + return { name: source.target.path.split('/').at(-1) ?? source.target.path, response } + } + + async response(access: ExtensionSelectedResourceAccess, id: string, lifetime: AbortSignal, request?: Request): Promise { + const signal = AbortSignal.any([access.signal, lifetime]) + const source = await this.resolve(access, id) + const snapshot = this.#snapshots.get(id) + const { response } = await this.sourceResponse(source, signal, snapshot ? undefined : request) + try { + access.assertCurrent() + signal.throwIfAborted() + } + catch (error) { + await response.body?.cancel().catch(() => {}) + throw error + } + if (!snapshot) + return response + await response.body?.cancel() + if (this.#snapshots.get(id) !== snapshot) + throw new Error('EXTENSION_RESOURCE_DENIED') + const bytes = Buffer.from(snapshot.text) + const range = fileRange(bytes.length, request?.headers.get('range') ?? null) + return new Response(request?.method === 'HEAD' || range.status === 416 ? null : bytes.subarray(range.start, range.end + 1), { status: range.status, headers: { ...range.headers, 'content-type': response.headers.get('content-type') ?? 'text/plain; charset=utf-8', 'content-security-policy': 'default-src \'none\'; sandbox', 'cache-control': 'no-store', 'x-content-type-options': 'nosniff', 'access-control-allow-origin': '*' } }) + } + + async text(access: ExtensionSelectedResourceAccess, id: string, signal: AbortSignal): Promise { + const source = await this.resolve(access, id) + if (source.kind === 'space' && !this.#snapshots.has(id)) + return this.#ports.readText(source.target, signal) + const response = await this.response(access, id, signal) + return new TextDecoder().decode(await readResponseBytes(response, 2 * 1024 * 1024)) + } + + async bytes(access: ExtensionSelectedResourceAccess, id: string, offset: number, length: number, signal: AbortSignal): Promise<{ base64: string, size: number, eof: boolean }> { + const url = 'https://resource.invalid/' + const metadata = await this.response(access, id, signal, new Request(url, { method: 'HEAD' })) + const size = Number(metadata.headers.get('content-length')) + await metadata.body?.cancel() + if (!Number.isSafeInteger(size) || size < 0 || !metadata.headers.has('content-length')) + throw new Error('EXTENSION_RESOURCE_UNSUPPORTED') + if (offset >= size) + return { base64: '', size, eof: true } + const response = await this.response(access, id, signal, new Request(url, { headers: { range: `bytes=${offset}-${Math.min(offset + length, size) - 1}` } })) + const bytes = Buffer.from(await readResponseBytes(response, length)) + return { base64: bytes.toString('base64'), size, eof: offset + bytes.length >= size } + } +} diff --git a/apps/buddy/platform/extensions/ExtensionService.ts b/apps/buddy/platform/extensions/ExtensionService.ts index ee36bf4d4..59d2077a4 100644 --- a/apps/buddy/platform/extensions/ExtensionService.ts +++ b/apps/buddy/platform/extensions/ExtensionService.ts @@ -7,7 +7,6 @@ import type { ExtensionClipboardData } from '../../shared/extensions/extensionCl import type { ExtensionConditionRuntime } from '../../shared/extensions/extensionConditionContext' import type { ExtensionConditionState } from '../../shared/extensions/extensionConditions' import type { ExtensionDiagnostic, ExtensionHostFailure } from '../../shared/extensions/extensionDiagnostic' -import type { InferenceResourceProgress } from '../../shared/extensions/extensionInference' import type { ExtensionNetworkResponse } from '../../shared/extensions/extensionNetwork' import type { ExtensionReferenceInput, ExtensionReferenceMaterial, ExtensionReferenceOpening } from '../../shared/extensions/extensionReferences' import type { ExtensionFileSource, ExtensionResourceSelection } from '../../shared/extensions/extensionResources' @@ -26,7 +25,6 @@ import type { ExtensionCompiler } from './compileExtensionSource' import type { ExtensionInferencePorts } from './ExtensionInference' import type { InferenceResourceSource } from './ExtensionInferenceResources' import type { ExtensionPackage, ExtensionPackageStore } from './ExtensionPackageStore' -import type { ExtensionResourceUpload } from './ExtensionResourceUpload' import type { ExtensionConfigurationApplication, ExtensionServiceChange, ExtensionServiceFact, ExtensionServiceSnapshot } from './ExtensionServiceEvents' import type { ExtensionTaskCleanupBatchResult } from './ExtensionTaskCleanupBatch' import type { ExtensionDocumentPorts } from './ExtensionWorkingCopyStore' @@ -34,7 +32,6 @@ import type { InferenceInvocation } from './NativeInferenceProcess' import { Buffer } from 'node:buffer' import { createHash, randomUUID } from 'node:crypto' import { basename } from 'node:path' -import process from 'node:process' import { z } from 'zod' import { BUDDY_ATTACHMENT_TOTAL_BYTES_LIMIT } from '../../shared/conversation/attachmentPolicy' import { buddyViewQuoteSchema } from '../../shared/conversation/buddyUserContent' @@ -51,7 +48,6 @@ import { extensionConditionInvalidationSchema } from '../../shared/extensions/ex import { extensionDiagnostic, ExtensionRequestError } from '../../shared/extensions/extensionDiagnostic' import { extensionDocumentInfoSchema } from '../../shared/extensions/extensionDocumentInfo' import { extensionFileHandlerBindings } from '../../shared/extensions/extensionFileOpening' -import { inferenceBindingSchema, inferenceCredentialReferenceSchema, inferenceCredentialSchema, inferenceHardwareSchema, inferenceHashSchema, inferenceIdSchema, inferencePrepareSchema, inferenceRuntimeReferenceSchema } from '../../shared/extensions/extensionInference' import { extensionCompatible, extensionPathSchema } from '../../shared/extensions/extensionManifest' import { projectExtensionNetworkHeaders } from '../../shared/extensions/extensionNetwork' import { extensionReferenceCaptureSchema, extensionReferenceCommitSchema, extensionReferenceInputSchema, extensionReferenceOpeningSchema, extensionReferenceTargetSchema } from '../../shared/extensions/extensionReferences' @@ -62,25 +58,28 @@ import { extensionSettingsHeaderActionsSchema } from '../../shared/extensions/ex import { extensionSkillRoots } from '../../shared/extensions/extensionSkills' import { EXTENSION_UNO_STYLESHEET } from '../../shared/extensions/extensionStyles' import { windowStyleIdSchema, windowStyleSchema } from '../../shared/extensions/extensionWindowStyles' -import { resolveInferenceEnvironment } from '../../shared/extensions/inferencePlatform' import { fileOpeningExtension } from '../../shared/files/fileOpening' import { themeRequestSchema, themeTransportSchema } from '../../shared/theme/themeApi' import { workbenchHitRegionsSchema } from '../../shared/workbench/workbenchInteraction' import { controlProposalSchema, extensionPresentationRequestSchema } from '../../shared/workbench/workbenchUi' -import { fileRange } from '../filesystem/fileRange' import { publicWebUrl, readResponseBytes } from '../network/publicWebTransport' import { detectShellPrograms } from '../process/shellPrograms' import { ExtensionCatalogService } from './ExtensionCatalogService' import { ExtensionConditionInvalidatedError } from './ExtensionConditionEvaluator' import { ExtensionConditions } from './ExtensionConditions' import { ExtensionDocuments } from './ExtensionDocuments' +import { ExtensionDownloads } from './ExtensionDownloads' import { sha256, verifiedExtensionAsset } from './extensionFiles' import { ExtensionInference } from './ExtensionInference' +import { ExtensionInferenceRequests } from './ExtensionInferenceRequests' import { ExtensionInstallations } from './ExtensionInstallations' +import { ExtensionInvocationScope } from './ExtensionInvocationScope' import { extensionActivationOrder } from './ExtensionPackageStore' -import { ExtensionResourceWriter } from './ExtensionResourceWriter' import { ExtensionScheduler } from './ExtensionScheduler' +import { ExtensionSelectedResources } from './ExtensionSelectedResources' import { ExtensionTaskCleanupController } from './ExtensionTaskCleanupController' +import { ExtensionUploads } from './ExtensionUploads' +import { ExtensionViewExports } from './ExtensionViewExports' import { hashResourceResponse } from './hashResourceResponse' export interface ExtensionHost { @@ -89,6 +88,7 @@ export interface ExtensionHost { devtools: () => void } export interface ExtensionServicePorts { + desktopAvailable?: () => boolean documents?: ExtensionDocumentPorts clipboard?: { read: (session: ExtensionViewSession, assertCurrent: () => void) => Promise @@ -132,6 +132,9 @@ interface RunningExtension { windowStyles?: Map resourceEndpoint?: { url: string, dispose: () => void } inFlight: number + users: number + runUsers: Set + idleTimer?: ReturnType inference?: ExtensionInference inferencePreparations: Set> drained: Set<() => void> @@ -146,7 +149,7 @@ interface RunningView { abort: AbortController ready: boolean error: string | null - writers: Map + exports: ExtensionViewExports } export class ExtensionService { @@ -160,7 +163,7 @@ export class ExtensionService { readonly installations: ExtensionInstallations readonly catalog: ExtensionCatalogService readonly #reviews = new Map() - readonly #fileSnapshots = new Map() + readonly #selected: ExtensionSelectedResources readonly #documents?: ExtensionDocuments readonly #ports: ExtensionServicePorts readonly #running = new Map() @@ -171,12 +174,13 @@ export class ExtensionService { readonly #viewOpenings = new Map() readonly #views = new Map() readonly #diagnostics = new Map>() - readonly #downloads = new Map() - readonly #uploadCreations = new Set<{ running: RunningExtension, owner: string, promise: Promise }>() - readonly #uploads = new Map() + readonly #downloads = new ExtensionDownloads() + readonly #inferenceRequests: ExtensionInferenceRequests + readonly #uploads = new ExtensionUploads() readonly #notificationTimes = new Map() readonly #interactions = new Map() - readonly #agentInvocations = new Map() + readonly #agentInvocations = new Map() + readonly #commandInvocations = new Map() readonly #stopping = new Set>() readonly #retired = new Set() readonly #viewRequests = new Set>() @@ -195,10 +199,15 @@ export class ExtensionService { #maintenance = false #disposal: Promise | undefined #epoch = 0 + #desktopAttached: boolean + #desktopAbort = new AbortController() constructor(store: ExtensionPackageStore, ports: ExtensionServicePorts) { this.store = store + this.#selected = new ExtensionSelectedResources(store, ports) + this.#desktopAttached = ports.desktopAvailable?.() ?? true this.#ports = ports + this.#inferenceRequests = new ExtensionInferenceRequests(store, ports.inference, this.#downloads) if (ports.documents) this.#documents = new ExtensionDocuments(store.root, { files: ports.documents, selectResources: ports.selectResources, selectSavePath: ports.selectSavePath }) this.conditions = new ExtensionConditions({ @@ -736,8 +745,16 @@ export class ExtensionService { throw new Error('EXTENSION_INFERENCE_UNAVAILABLE') z.uuid().parse(input.id) extensionJsonSchema.parse(input.input) - await running.inference.invoke(capability, input) - this.#assertCurrent(running) + running.users++ + this.#scheduleIdle() + try { + await running.inference.invoke(capability, input) + this.#assertCurrent(running) + } + finally { + running.users-- + this.#scheduleIdle() + } } async resetInference(): Promise { @@ -745,173 +762,11 @@ export class ExtensionService { } #inferenceSource(running: RunningExtension): InferenceResourceSource { - const { id, permissions } = running.package.manifest - return { - prepare: running.package.manifest.contributes.inference?.preparation && running.inference - ? (...args) => running.inference!.prepareResources(...args) - : undefined, - assertDownload: (url) => { - this.#networkUrl(running, url) - this.#assertCurrent(running) - }, - response: (source, signal, headers) => { - if ('directoryId' in source) - throw new Error('EXTENSION_INFERENCE_REFERENCE_REQUIRED') - return 'url' in source ? this.#networkResponse(running, source.url, signal, headers) : this.store.resources.response(id, source.resourceId, new Request('https://resource.invalid/'), signal, permissions.localResources) - }, - directoryReferencePath: async (input) => { - this.#assertCurrent(running) - if (!permissions.localResources) - throw new Error('EXTENSION_PERMISSION_DENIED') - const path = await this.store.resources.directoryReferencePath(id, input) - this.#assertCurrent(running) - return path - }, - referencePath: async (resourceId) => { - this.#assertCurrent(running) - if (!permissions.localResources) - throw new Error('EXTENSION_PERMISSION_DENIED') - const path = await this.store.resources.referencePath(id, resourceId) - this.#assertCurrent(running) - return path - }, - openDirectoryReference: async (input) => { - this.#assertCurrent(running) - if (!permissions.localResources) - throw new Error('EXTENSION_PERMISSION_DENIED') - const handle = await this.store.resources.openDirectoryReference(id, input) - try { - this.#assertCurrent(running) - return handle - } - catch (error) { - await handle.close() - throw error - } - }, - openReference: async (resourceId) => { - this.#assertCurrent(running) - if (!permissions.localResources) - throw new Error('EXTENSION_PERMISSION_DENIED') - return this.store.resources.openReference(id, resourceId) - }, - } - } - - async #inferenceRequest(running: RunningExtension, method: string, params: unknown, lifetime: AbortSignal, owner: string): Promise { - const { id, permissions } = running.package.manifest - if (!permissions.inference) - throw new Error('EXTENSION_PERMISSION_DENIED') - if (method.startsWith('inference.credentials.')) { - const credentials = this.#ports.inference?.credentials - if (!credentials || !running.inference) - throw new Error('EXTENSION_INFERENCE_UNAVAILABLE') - const assertCurrent = () => this.#assertCurrent(running) - if (method === 'inference.credentials.remove') { - const input = z.object({ id: inferenceIdSchema }).strict().parse(params) - await running.inference.withStoppedCredential(input.id, () => credentials.remove(id, input.id, assertCurrent)) - this.#publish({ kind: 'inference-models', extensionId: id }) - return null - } - if (method === 'inference.credentials.has' || method === 'inference.credentials.set') { - const input = method === 'inference.credentials.set' ? inferenceCredentialSchema.parse(params) : inferenceCredentialReferenceSchema.parse(params) - if (!permissions.inferenceNetwork.includes(input.origin)) - throw new Error('EXTENSION_PERMISSION_DENIED') - if ('token' in input) { - await running.inference.withStoppedCredential(input.id, () => credentials.set(id, inferenceCredentialSchema.parse(input), assertCurrent)) - this.#publish({ kind: 'inference-models', extensionId: id }) - return null - } - return credentials.has(id, input) - } - throw new Error('EXTENSION_METHOD_DENIED') - } - if (method === 'inference.environment') { - const hardware = inferenceHardwareSchema.safeParse(await this.#ports.inference?.hardware?.(permissions.inferenceGpu).catch(() => undefined)) - this.#assertCurrent(running) - return { - ...resolveInferenceEnvironment(process.platform, process.arch), - ...(hardware.success ? { hardware: { ...hardware.data, gpus: permissions.inferenceGpu ? hardware.data.gpus : [] } } : {}), - } - } - if (method === 'inference.status') - return running.inference?.status() ?? [] - if (method === 'inference.stop') { - const input = params === null ? undefined : inferenceRuntimeReferenceSchema.parse(params) - if (input && !running.inference) - throw new Error('EXTENSION_INFERENCE_RUNTIME_UNAVAILABLE') - await running.inference?.stop(input) - return null - } - if (method === 'inference.unbind') { - const input = inferenceRuntimeReferenceSchema.parse(params) - if (!running.inference) - throw new Error('EXTENSION_INFERENCE_RUNTIME_UNAVAILABLE') - await running.inference.unbind(input) - this.#publish({ kind: 'inference-models', extensionId: id }) - return null - } - if (method === 'inference.resources.list') { - const [files, directories] = await Promise.all([this.store.resources.list(id, permissions.localResources), permissions.localResources ? this.store.resources.directories(id) : []]) - return this.store.inference.inspect(id, new Set(files.map(file => file.id)), new Set(directories.map(directory => directory.id))) - } - if (method === 'inference.resources.remove') { - const resource = z.object({ id: inferenceIdSchema, revision: inferenceHashSchema }).strict().parse(params) - await this.store.inference.remove(id, resource, () => this.#assertCurrent(running)) - running.inference?.forgetResource(resource.id, resource.revision) - this.#publish({ kind: 'inference-models', extensionId: id }) - return null - } - if (method === 'inference.bind') { - if (!running.inference) - throw new Error('EXTENSION_INFERENCE_UNAVAILABLE') - await running.inference.bind(inferenceBindingSchema.parse(params)) - this.#publish({ kind: 'inference-models', extensionId: id }) - return null - } - if (method === 'inference.resources.prepare') { - const input = z.object({ id: z.uuid(), resource: inferencePrepareSchema }).strict().parse(params) - if (this.#downloads.has(input.id)) - throw new Error('EXTENSION_RESOURCE_DOWNLOAD_EXISTS') - const abort = new AbortController() - this.#downloads.set(input.id, { running, owner, abort }) - const signal = AbortSignal.any([abort.signal, lifetime, running.abort.signal]) - const pendingProgress: InferenceResourceProgress[] = [] - let publishing: Promise | undefined - const publish = () => { - if (publishing || signal.aborted) - return - const next = pendingProgress.shift() - if (!next) - return - publishing = running.host.call('inference.progress', { id: input.id, progress: { ...next } }, signal).then(() => {}, () => {}).finally(() => { - publishing = undefined - publish() - }) - } - const report = (progress: InferenceResourceProgress) => { - if (signal.aborted) - return - const last = pendingProgress.at(-1) - if (last && last.path === progress.path && last.phase === progress.phase && last.completedFiles === progress.completedFiles && last.bytes > 0 && progress.bytes > 0) - pendingProgress[pendingProgress.length - 1] = progress - else - pendingProgress.push(progress) - publish() - } - const preparing = this.store.inference.prepare(id, input.resource, this.#inferenceSource(running), signal, () => this.#assertCurrent(running), report) - running.inferencePreparations.add(preparing) - try { - return await preparing - } - finally { - for (let pending = publishing; pending; pending = publishing) - await pending - running.inferencePreparations.delete(preparing) - this.#downloads.delete(input.id) - } - } - throw new Error('EXTENSION_METHOD_DENIED') + return this.#inferenceRequests.source(running.package.manifest, running.inference, { + assertCurrent: () => this.#assertCurrent(running), + authorizeDownload: (url) => { this.#networkUrl(running, url) }, + networkResponse: (url, signal, headers) => this.#networkResponse(running, url, signal, headers), + }) } async invokeAgent(input: ExtensionAgentInvocation, signal: AbortSignal): Promise { @@ -955,12 +810,21 @@ export class ExtensionService { } if (references.length && (await this.agentContributions()).find(item => item.id === input.extensionId)?.configurationRevision !== input.configurationRevision) throw new Error('EXTENSION_AGENT_UNAVAILABLE') - const controller = new AbortController() - const abort = AbortSignal.any([signal, running.abort.signal, controller.signal]) + const invocation = Object.assign(new ExtensionInvocationScope([signal, running.abort.signal]), { running, ...'tool' in input ? { access: current.agent.tools.find(tool => tool.id === input.tool)!.access ?? 'read' as const } : {} }) + const abort = invocation.signal abort.throwIfAborted() if (this.#agentInvocations.has(input.invocationId) || [...this.#agentInvocations.values()].filter(item => item.running === running).length >= extensionAgentInvocationLimits.concurrentPerExtension) throw new Error('EXTENSION_REQUEST_LIMIT') - this.#agentInvocations.set(input.invocationId, { running, signal: abort, abort: controller, ...'tool' in input ? { access: current.agent.tools.find(tool => tool.id === input.tool)!.access ?? 'read' } : {} }) + this.#agentInvocations.set(input.invocationId, invocation) + if ('tool' in input && input.context?.runId) { + for (const id of this.#order(input.extensionId)) { + const owner = this.#running.get(id) + if (owner) { + owner.runUsers.add(input.context.runId) + this.#scheduleIdle() + } + } + } const cancel = () => { void running.host.call('agent.cancel', { invocationId: input.invocationId }).catch(() => {}) } @@ -978,7 +842,9 @@ export class ExtensionService { } finally { abort.removeEventListener('abort', cancel) + await invocation.finish() this.#agentInvocations.delete(input.invocationId) + this.#scheduleIdle() } } @@ -1008,7 +874,7 @@ export class ExtensionService { const job = this.installations.begin(basename(path), 'validate') try { const review = await this.store.review(path, development, job.signal) - this.installations.identify(job.id, review.manifest.id, review.manifest.version) + this.installations.identify(job.id, review.manifest.id, review.manifest.version, review.manifest.executionScope) job.signal.throwIfAborted() this.#reviews.set(review.token, job.id) this.installations.log(job.id, 'review', 'permissions') @@ -1036,7 +902,7 @@ export class ExtensionService { this.installations.log(job.id, 'validate', 'checksum') job.signal.throwIfAborted() const review = await this.store.reviewArchive(bytes, source, entry.manifest, job.signal) - this.installations.identify(job.id, review.manifest.id, review.manifest.version) + this.installations.identify(job.id, review.manifest.id, review.manifest.version, review.manifest.executionScope) this.#reviews.set(review.token, job.id) this.installations.log(job.id, 'review', 'permissions') return { ...review, installationId: job.id, source } @@ -1223,7 +1089,7 @@ export class ExtensionService { const binding = handler && extensionFileHandlerBindings(handler).find(target => target.location === input.location) if (!handler || !binding) throw new Error('EXTENSION_FILE_OPENER_UNAVAILABLE') - const { name, response } = await this.#sourceResponse(input.source, running.abort.signal) + const { name, response } = await this.#selected.sourceResponse(input.source, running.abort.signal) await response.body?.cancel() this.#assertCurrent(running) const extension = fileOpeningExtension(name, [provider]) @@ -1231,15 +1097,7 @@ export class ExtensionService { throw new Error('EXTENSION_FILE_ASSOCIATION_DENIED') let resource: ExtensionResource if (input.text !== undefined) { - const bytes = Buffer.byteLength(input.text) - for (const [id, entry] of this.#fileSnapshots) { - if (entry.running.abort.signal.aborted || (entry.created < Date.now() - 600000 && ![...this.#views.values()].some(view => view.input.resource?.id === id))) - this.#fileSnapshots.delete(id) - } - if (bytes > 2 * 1024 * 1024 || this.#fileSnapshots.size >= 64 || [...this.#fileSnapshots.values()].reduce((total, entry) => total + entry.bytes, bytes) > 16 * 1024 * 1024) - throw new Error('EXTENSION_RESOURCE_TEXT_LIMIT') - resource = { id: randomUUID(), name } - this.#fileSnapshots.set(resource.id, { running, source: structuredClone(input.source), text: input.text, bytes, created: Date.now() }) + resource = this.#selected.snapshot(this.#selectedAccess(running), input.source, name, input.text, id => [...this.#views.values()].some(view => view.input.resource?.id === id)) } else { resource = await this.store.grantFileOpening(provider.extensionId, input.source, name, () => this.#assertCurrent(running)) @@ -1268,11 +1126,11 @@ export class ExtensionService { throw new Error('EXTENSION_REFERENCE_UNAVAILABLE') const resource = current.input.resource if (input.source) { - const source = resource ? await this.#resource(running, resource.id) : null + const source = resource ? await this.#selected.resolve(this.#selectedAccess(running), resource.id) : null if (!sameReferenceJson(source, input.source)) throw new Error('EXTENSION_RESOURCE_DENIED') if (resource) { - const response = await this.#selectedResponse(running, resource.id, current.abort.signal) + const response = await this.#selected.response(this.#selectedAccess(running), resource.id, current.abort.signal) await response.body?.cancel() } } @@ -1285,7 +1143,7 @@ export class ExtensionService { return { extensionId: input.extensionId, viewType: view.id, resource: null } if (view.resource === 'none' || running.package.manifest.permissions.selectedResource !== 'read') throw new Error('EXTENSION_RESOURCE_DENIED') - const { name, response } = await this.#sourceResponse(input.source, running.abort.signal) + const { name, response } = await this.#selected.sourceResponse(input.source, running.abort.signal) await response.body?.cancel() this.#assertCurrent(running) const granted = await this.store.grantFileOpening(input.extensionId, input.source, name, () => this.#assertCurrent(running)) @@ -1390,6 +1248,7 @@ export class ExtensionService { } async #executeCommand(running: RunningExtension, command: string, target: SpaceFileTarget | null, invocation: JsonValue = null, argumentsValue: JsonValue = null, cleanupToken: string | null = null): Promise { + this.#requireDesktop() if (!running.package.manifest.contributes.commands.some(item => item.id === command)) throw new Error('EXTENSION_COMMAND_UNAVAILABLE') let resource: ExtensionResource | null = null @@ -1415,14 +1274,29 @@ export class ExtensionService { } async #callCommand(running: RunningExtension, params: JsonValue): Promise { - const result = await running.host.call('command', params) - const parsed = extensionJsonSchema.safeParse(result) - if (!parsed.success) - throw new ExtensionRequestError('EXTENSION_INVALID_RESULT', 'command', parsed.error, 'result') - return parsed.data + const invocationId = randomUUID() + const invocation = Object.assign(new ExtensionInvocationScope([running.abort.signal]), { running }) + const signal = invocation.signal + this.#commandInvocations.set(invocationId, invocation) + try { + const result = await running.host.call('command', running.package.manifest.apiVersion >= 5 ? { ...params as Record, scopeId: invocationId } : params, signal) + signal.throwIfAborted() + this.#requireDesktop() + const parsed = extensionJsonSchema.safeParse(result) + if (!parsed.success) + throw new ExtensionRequestError('EXTENSION_INVALID_RESULT', 'command', parsed.error, 'result') + return parsed.data + } + finally { + await invocation.finish() + this.#commandInvocations.delete(invocationId) + this.#scheduleIdle() + } } async openView(input: ExtensionViewInput): Promise { + this.#requireDesktop() + const desktopSignal = this.#desktopAbort.signal const request = Symbol('view-open') this.#viewOpenings.set(input.viewId, request) try { @@ -1446,12 +1320,14 @@ export class ExtensionService { if (input.resource) { if (view.resource === 'none') throw new Error('EXTENSION_RESOURCE_DENIED') - const response = await this.#selectedResponse(running, input.resource.id, running.abort.signal) + const response = await this.#selected.response(this.#selectedAccess(running), input.resource.id, running.abort.signal) await response.body?.cancel() } this.#assertCurrent(running) if (input.interactionId && !this.#interactions.has(input.interactionId)) throw new Error('EXTENSION_INTERACTION_ENDED') + desktopSignal.throwIfAborted() + this.#requireDesktop() if (this.#viewOpenings.get(input.viewId) !== request) throw new Error('EXTENSION_VIEW_EXPIRED') const old = this.#views.get(input.viewId) @@ -1461,12 +1337,12 @@ export class ExtensionService { const current = this.#views.get(input.viewId) if (current?.session.token !== endpoint.token || resourceId !== current.input.resource?.id) throw new Error('EXTENSION_RESOURCE_DENIED') - return this.#selectedResponse(running, resourceId, AbortSignal.any([signal, current.abort.signal]), request) + return this.#selected.response(this.#selectedAccess(running), resourceId, AbortSignal.any([signal, current.abort.signal]), request) }) const abort = new AbortController() - const writers = new Map() + const exports = new ExtensionViewExports() const session = { id: input.viewId, extensionId: input.extensionId, generation: running.generation, token: endpoint.token, url: endpoint.url } - this.#views.set(input.viewId, { input: structuredClone(input), session: structuredClone(session), running, abort, ready: false, error: null, writers, dispose: () => endpoint.dispose() }) + this.#views.set(input.viewId, { input: structuredClone(input), session: structuredClone(session), running, abort, ready: false, error: null, exports, dispose: () => endpoint.dispose() }) this.#publish({ kind: 'view', extensionId: input.extensionId, generation: running.generation, viewId: input.viewId, status: 'opened' }) return structuredClone(session) } @@ -1661,9 +1537,9 @@ export class ExtensionService { signal, current, source: async (resourceId) => { - if (resourceId !== view.input.resource?.id || this.#fileSnapshots.has(resourceId)) + if (resourceId !== view.input.resource?.id || this.#selected.hasSnapshot(resourceId)) throw new Error('EXTENSION_DOCUMENT_DENIED') - return this.#resource(view.running, resourceId) + return this.#selected.resolve(this.#selectedAccess(view.running), resourceId) }, }, method, params) } @@ -1678,50 +1554,12 @@ export class ExtensionService { if (view.abort.signal.aborted || this.#views.get(id) !== view) throw new Error('EXTENSION_VIEW_EXPIRED') } - if (method === 'resources.beginSave') { - if (!this.#ports.selectSavePath || view.writers.size >= 2) - throw new Error('EXTENSION_RESOURCE_SAVE_UNAVAILABLE') - const input = z.object({ name: z.string().min(1).max(255).regex(/^[^/\\\0]+$/), size: z.number().int().min(0).max(Number.MAX_SAFE_INTEGER) }).strict().parse(params) - const signal = AbortSignal.any([view.abort.signal, view.running.abort.signal, AbortSignal.timeout(120000)]) - const path = await this.#ports.selectSavePath(view.running.package.manifest.name, input.name, signal) - signal.throwIfAborted() - assertView() - if (path) { - const writer = await ExtensionResourceWriter.create(path, input.size, assertView) - try { - assertView() - } - catch (error) { - await writer.dispose() - throw error - } - view.writers.set(writer.id, writer) - result = writer.id - } - else { result = null } - } - else { - const input = z.object({ id: z.string().uuid(), offset: z.number().int().min(0).optional(), base64: z.string().max(128 * 1024).regex(/^(?:[A-Z0-9+/]{4})*(?:[A-Z0-9+/]{2}==|[A-Z0-9+/]{3}=)?$/i).optional() }).strict().parse(params) - const writer = view.writers.get(input.id) - if (!writer) - throw new Error('EXTENSION_RESOURCE_WRITE_EXPIRED') - if (method === 'resources.writeChunk') { - if (input.offset === undefined || input.base64 === undefined) - throw new Error('EXTENSION_RESOURCE_WRITE_RANGE') - await writer.append(input.offset, input.base64) - } - else { - try { - if (method === 'resources.commitSave') - await writer.commit() - } - finally { - view.writers.delete(input.id) - await writer.dispose() - } - } - result = null - } + result = await view.exports.request({ + name: view.running.package.manifest.name, + signal: AbortSignal.any([view.abort.signal, view.running.abort.signal]), + assertCurrent: assertView, + selectSavePath: this.#ports.selectSavePath, + }, method, params) } else if (method === 'resources.hash') { result = await this.#broker(view.running, method, params, view.abort.signal, view.session.id) @@ -1730,17 +1568,17 @@ export class ExtensionService { const resourceId = view.input.resource.id if (method === 'resources.getUrl') { z.object({ id: z.string().uuid() }).strict().parse(params) - const response = await this.#selectedResponse(view.running, resourceId, view.abort.signal) + const response = await this.#selected.response(this.#selectedAccess(view.running), resourceId, view.abort.signal) await response.body?.cancel() result = new URL(`/__selected/${resourceId}`, view.session.url).href } else if (method === 'resources.readBytes') { const input = z.object({ id: z.string().uuid(), offset: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER), length: z.number().int().min(1).max(128 * 1024) }).strict().parse(params) - result = await this.#selectedBytes(view.running, input.id, input.offset, input.length, view.abort.signal) + result = await this.#selected.bytes(this.#selectedAccess(view.running), input.id, input.offset, input.length, view.abort.signal) } else { z.object({ id: z.string().uuid() }).strict().parse(params) - result = await this.#selectedText(view.running, resourceId, view.abort.signal) + result = await this.#selected.text(this.#selectedAccess(view.running), resourceId, view.abort.signal) } } else if (method.startsWith('resources.')) { @@ -1894,6 +1732,95 @@ export class ExtensionService { return this.#disposal } + attachDesktop(): void { + if (this.#disposed || this.#desktopAttached) + return + this.#desktopAttached = true + this.#desktopAbort = new AbortController() + this.scheduler.refresh() + this.#refreshShellFingerprint() + void this.#refreshAgentContributions().catch(() => {}) + this.#ports.capabilitiesChanged?.() + this.#ports.skillsChanged?.() + } + + async detachDesktop(): Promise { + this.#desktopAttached = false + this.#desktopAbort.abort(new Error('EXTENSION_DESKTOP_REQUIRED')) + this.#viewOpenings.clear() + this.updatePanes([]) + for (const view of this.#views.values()) + this.#closeView(view) + for (const invocation of this.#commandInvocations.values()) + invocation.abort.abort(new Error('EXTENSION_DESKTOP_REQUIRED')) + for (const [id] of this.#interactions) + void this.endInteraction(id).catch(() => {}) + this.scheduler.refresh() + this.#refreshShellFingerprint() + const stopping = [...this.#running.values()].filter(running => running.package.manifest.executionScope !== 'service').map(running => this.#stop(running.package.manifest.id)) + const results = await Promise.allSettled([...stopping, ...this.#viewCleanups]) + await this.#refreshAgentContributions() + this.#ports.capabilitiesChanged?.() + this.#ports.skillsChanged?.() + this.#scheduleIdle() + const failure = results.find(result => result.status === 'rejected') + if (failure?.status === 'rejected') + throw failure.reason + if (this.#viewCleanupError) + throw this.#viewCleanupError + } + + releaseAllRuns(): void { + for (const running of this.#running.values()) { + running.runUsers.clear() + this.#scheduleIdle() + } + } + + releaseRun(runId: string): void { + for (const running of this.#running.values()) { + running.runUsers.delete(runId) + this.#scheduleIdle() + } + } + + #hasUsers(running: RunningExtension, visited = new Set()): boolean { + if (visited.has(running)) + return false + visited.add(running) + return !!(running.users || running.inFlight || running.runUsers.size || running.inferencePreparations.size + || [...this.#views.values()].some(view => view.running === running) + || [...this.#agentInvocations.values()].some(invocation => invocation.running === running) + || [...this.#running.values()].some(dependent => running.package.manifest.id in dependent.package.manifest.dependencies && this.#hasUsers(dependent, visited))) + } + + #scheduleIdle(): void { + for (const running of this.#running.values()) { + if (running.package.manifest.executionScope !== 'service' || !running.active || running.abort.signal.aborted || this.#hasUsers(running)) { + if (running.idleTimer) + clearTimeout(running.idleTimer) + running.idleTimer = undefined + continue + } + if (running.idleTimer) + continue + running.idleTimer = setTimeout(() => { + running.idleTimer = undefined + if (this.#hasUsers(running)) { + this.#scheduleIdle() + return + } + void this.#stop(running.package.manifest.id).catch(() => {}) + }, 5_000) + running.idleTimer.unref() + } + } + + #requireDesktop(): void { + if (!this.#desktopAttached) + throw new Error('EXTENSION_DESKTOP_REQUIRED') + } + async resetHosts(): Promise { this.#epoch++ await Promise.all([...new Set([...this.#running.keys(), ...this.#pendingStops.keys()])].map(id => this.#stop(id))) @@ -1958,12 +1885,25 @@ export class ExtensionService { this.#publish({ kind: 'host', extensionId: dependency, generation, status: 'failed', errorCode: extensionError(error) }) throw error } - running = { package: pkg, generation, abort, host, ready: Promise.resolve(), active: false, inFlight: 0, inferencePreparations: new Set(), drained: new Set() } + running = { package: pkg, generation, abort, host, ready: Promise.resolve(), active: false, inFlight: 0, users: 0, runUsers: new Set(), inferencePreparations: new Set(), drained: new Set() } if (pkg.manifest.contributes.inference && this.#ports.inference) { const owner = running running.inference = new ExtensionInference(this.store, pkg, this.#ports.inference, this.#inferenceSource(owner), () => this.#assertCurrent(owner), () => this.#publish({ kind: 'inference-models', extensionId: pkg.manifest.id })) } this.#running.set(dependency, running) + const call = host.call.bind(host) + const owner = running + host.call = async (...args) => { + owner.users++ + this.#scheduleIdle() + try { + return await call(...args) + } + finally { + owner.users-- + this.#scheduleIdle() + } + } const start = performance.now() const instance = running this.#clearError(dependency) @@ -1971,6 +1911,7 @@ export class ExtensionService { this.#assertCurrent(instance) instance.registeredCommands = result && typeof result === 'object' && !Array.isArray(result) && Array.isArray(result.commands) ? z.array(z.string()).parse(result.commands).filter(id => pkg.manifest.contributes.commands.some(command => command.id === id)) : [] instance.active = true + this.#scheduleIdle() this.#publish({ kind: 'host', extensionId: dependency, generation, status: 'active', durationMs: Math.round(performance.now() - start) }) this.#publishPanes(instance) this.#log(dependency, 'activated', undefined, Math.round(performance.now() - start)) @@ -1996,6 +1937,8 @@ export class ExtensionService { throw new Error('EXTENSION_DEPENDENCY_FAILED') for (const next of order) { const manifest = this.store.installed[next]!.current.manifest + if (manifest.executionScope !== 'service') + this.#requireDesktop() const issue = extensionCompatibilityIssue(manifest, this.store.appVersion) if (issue) throw new ExtensionCompatibilityError(manifest, issue) @@ -2006,6 +1949,8 @@ export class ExtensionService { #assertCurrent(running: RunningExtension): void { const id = running.package.manifest.id const installed = this.store.installed[id] + if (running.package.manifest.executionScope !== 'service') + this.#requireDesktop() if (this.#disposed || this.#maintenance || running.abort.signal.aborted || this.#running.get(id) !== running || !installed?.enabled || installed.current.revision !== running.package.revision) throw new Error('EXTENSION_HOST_STOPPED') } @@ -2031,6 +1976,14 @@ export class ExtensionService { if (!running || running.generation !== generation) throw new Error('EXTENSION_HOST_STOPPED') try { + if (method === 'invocation.request') { + const input = z.object({ invocationId: z.uuid(), method: z.string().min(1).max(100), params: extensionJsonSchema }).strict().parse(params) + const invocation = this.#agentInvocations.get(input.invocationId) ?? this.#commandInvocations.get(input.invocationId) + if (!invocation || invocation.running !== running || ['invocation.request', 'agent.request'].includes(input.method)) + throw new Error('EXTENSION_INVOCATION_EXPIRED') + invocation.signal.throwIfAborted() + return await invocation.track(() => this.#broker(running, input.method, parseExtensionRequestParams(input.method, input.params), invocation.signal, input.invocationId)) + } return await this.#broker(running, method, parseExtensionRequestParams(method, params)) } catch (error) { @@ -2040,9 +1993,28 @@ export class ExtensionService { } async #broker(running: RunningExtension, method: string, params: unknown, lifetime = running.abort.signal, owner = running.generation): Promise { - this.#assertCurrent(running) + if (/^(?:window\.|views\.|placements\.|interactions\.|links\.)/.test(method)) { + this.#requireDesktop() + lifetime = AbortSignal.any([lifetime, this.#desktopAbort.signal]) + } + const assertCurrent = () => { + this.#assertCurrent(running) + lifetime.throwIfAborted() + if (owner !== running.generation && ![...this.#views.values()].some(view => view.session.id === owner && view.running === running && !view.abort.signal.aborted)) { + const invocation = this.#agentInvocations.get(owner) ?? this.#commandInvocations.get(owner) + if (!invocation || invocation.running !== running || invocation.signal.aborted) + throw new Error('EXTENSION_INVOCATION_EXPIRED') + } + } + assertCurrent() + if (running.package.manifest.executionScope === 'service' && owner === running.generation) { + const initialization = !running.active && ['storage.read', 'storage.get', 'storage.set', 'configuration.get'].includes(method) + if (!initialization && !['configuration.get', 'conditions.invalidate', 'agent.request'].includes(method)) + throw new Error('EXTENSION_INVOCATION_REQUIRED') + } if (++running.inFlight > 64) { running.inFlight-- + this.#scheduleIdle() throw new Error('EXTENSION_REQUEST_LIMIT') } const { id, dataVersion } = running.package.manifest @@ -2107,7 +2079,17 @@ export class ExtensionService { result = { ...cleanupResult, summary: { ...cleanupResult.summary } } } else if (method.startsWith('inference.')) { - result = await this.#inferenceRequest(running, method, params, lifetime, owner) + result = await this.#inferenceRequests.request({ + identity: running, + manifest: running.package.manifest, + signal: running.abort.signal, + inference: running.inference, + preparations: running.inferencePreparations, + call: running.host.call.bind(running.host), + source: () => this.#inferenceSource(running), + assertCurrent: () => this.#assertCurrent(running), + changed: () => this.#publish({ kind: 'inference-models', extensionId: id }), + }, method, params, lifetime, owner) } else if (method === 'window.styles.set' || method === 'window.styles.remove') { if (running.package.manifest.apiVersion < 3 || !running.package.manifest.permissions.windowEffects) @@ -2125,7 +2107,7 @@ export class ExtensionService { result = null } else if (method === 'themes.request') { - result = await this.#themeRequest(running, `${id}:${running.generation}`, params, () => this.#assertCurrent(running)) + result = await this.#themeRequest(running, `${id}:${running.generation}`, params, assertCurrent, lifetime) } else if (method === 'shells.detect') { z.null().parse(params) @@ -2164,7 +2146,7 @@ export class ExtensionService { } else if (method === 'storage.set') { const data = z.object({ value: extensionJsonSchema, version: z.literal(dataVersion) }).strict().parse(params) - await this.store.saveData(id, data.value, data.version, () => this.#assertCurrent(running)) + await this.store.saveData(id, data.value, data.version, () => assertCurrent()) result = null } else if (method === 'notifications.show') { @@ -2183,7 +2165,7 @@ export class ExtensionService { const input = extensionScheduleInputSchema.parse(params) if (!running.package.manifest.contributes.commands.some(command => command.id === input.command)) throw new Error('EXTENSION_COMMAND_UNAVAILABLE') - result = await this.scheduler.set(id, input, () => this.#assertCurrent(running)) + result = await this.scheduler.set(id, input, () => assertCurrent()) } else { const input = z.object({ id: extensionScheduleIdSchema }).strict().parse(params) @@ -2191,7 +2173,7 @@ export class ExtensionService { result = this.scheduler.get(id, input.id) } else if (method === 'schedules.remove') { - await this.scheduler.remove(id, input.id, () => this.#assertCurrent(running)) + await this.scheduler.remove(id, input.id, () => assertCurrent()) result = null } else { @@ -2202,53 +2184,24 @@ export class ExtensionService { else if (method === 'resources.beginCreate') { const input = extensionResourceCreateSchema.parse(params) const signal = AbortSignal.any([running.abort.signal, lifetime]) - const creation = { running, owner, promise: this.store.resources.create(id, input, signal, () => this.#assertCurrent(running)) } - this.#uploadCreations.add(creation) - try { - const upload = await creation.promise - this.#uploads.set(upload.id, { running, owner, upload }) - void upload.finished.finally(() => this.#uploads.delete(upload.id)).catch(() => {}) - result = upload.id - } - finally { this.#uploadCreations.delete(creation) } + result = await this.#uploads.create(running, owner, () => this.store.resources.create(id, input, signal, assertCurrent)) } else if (['resources.writeChunk', 'resources.commitSave', 'resources.cancelSave'].includes(method)) { - const input = z.object({ id: z.string().uuid(), offset: z.number().int().min(0).optional(), base64: z.string().max(128 * 1024).regex(/^(?:[A-Z0-9+/]{4})*(?:[A-Z0-9+/]{2}==|[A-Z0-9+/]{3}=)?$/i).optional() }).strict().parse(params) - const entry = this.#uploads.get(input.id) - if (!entry) - throw new Error('EXTENSION_RESOURCE_WRITE_EXPIRED') - if (entry.running !== running || entry.owner !== owner) - throw new Error('EXTENSION_RESOURCE_DENIED') - if (method === 'resources.writeChunk') { - if (input.offset === undefined || input.base64 === undefined) - throw new Error('EXTENSION_RESOURCE_WRITE_RANGE') - await entry.upload.append(input.offset, input.base64) - result = null - } - else if (method === 'resources.commitSave') { - try { - result = { ...await entry.upload.commit() } - } - finally { await entry.upload.dispose() } - } - else { - await entry.upload.dispose() - result = null - } + result = await this.#uploads.request(running, owner, method, params) } else if (method === 'resources.readText') { const { id: resource } = z.object({ id: z.string().uuid() }).strict().parse(params) const allowLocal = running.package.manifest.permissions.localResources result = (await this.store.resources.list(id, allowLocal)).some(item => item.id === resource) ? await this.store.resources.readText(id, resource, lifetime, allowLocal) - : await this.#selectedText(running, resource, lifetime) + : await this.#selected.text(this.#selectedAccess(running), resource, lifetime) } else if (method === 'resources.listFiles') { result = await this.store.resources.list(id, running.package.manifest.permissions.localResources) } else if (method === 'resources.getUrl') { const path = await this.#resourcePath(running, params) - this.#assertCurrent(running) + assertCurrent() running.resourceEndpoint ??= this.#ports.createView(running.package) result = new URL(path, running.resourceEndpoint.url).href } @@ -2258,73 +2211,61 @@ export class ExtensionService { } else if (method === 'resources.release') { const input = z.object({ id: z.string().uuid() }).strict().parse(params) - await this.store.resources.release(id, input.id, () => this.#assertCurrent(running)) + await this.store.resources.release(id, input.id, () => assertCurrent()) result = null } else if (method === 'resources.hash') { const input = extensionResourceHashRequestSchema.parse(params) - if (this.#downloads.has(input.id)) - throw new Error('EXTENSION_RESOURCE_DOWNLOAD_EXISTS') - const abort = new AbortController() - this.#downloads.set(input.id, { running, owner, abort }) - const signal = AbortSignal.any([running.abort.signal, lifetime, abort.signal]) - try { + result = await this.#downloads.run(input.id, running, owner, [running.abort.signal, lifetime], async (signal) => { const allowLocal = running.package.manifest.permissions.localResources if ((await this.store.resources.list(id, allowLocal)).some(resource => resource.id === input.resourceId)) { - result = await this.store.resources.hash(id, input.resourceId, signal, allowLocal) + const result = await this.store.resources.hash(id, input.resourceId, signal, allowLocal) + signal.throwIfAborted() + return result } else { if (owner !== running.generation && this.#views.get(owner)?.input.resource?.id !== input.resourceId) throw new Error('EXTENSION_RESOURCE_DENIED') - result = await hashResourceResponse(await this.#selectedResponse(running, input.resourceId, signal), signal) + const result = await hashResourceResponse(await this.#selected.response(this.#selectedAccess(running), input.resourceId, signal), signal) + signal.throwIfAborted() + return result } - signal.throwIfAborted() - } - finally { this.#downloads.delete(input.id) } + }) } else if (method === 'resources.importFile' || method === 'network.download') { const input = z.object({ id: z.string().uuid(), url: z.string().url().max(8192).optional(), resourceId: z.string().uuid().optional(), name: z.string().min(1).max(255).regex(/^[^/\\\0]+$/).optional() }).strict().parse(params) - if (this.#downloads.has(input.id)) - throw new Error('EXTENSION_RESOURCE_DOWNLOAD_EXISTS') - const abort = new AbortController() - this.#downloads.set(input.id, { running, owner, abort }) - const signal = AbortSignal.any([running.abort.signal, lifetime, abort.signal]) - const guard = () => { - signal.throwIfAborted() - this.#assertCurrent(running) - } - let response: Response | undefined - try { - if (method === 'network.download' && (!input.url || input.resourceId)) - throw new Error('EXTENSION_INVALID_ARGUMENT') - if (method === 'resources.importFile' && (!input.resourceId || input.url)) - throw new Error('EXTENSION_RESOURCE_UNAVAILABLE') - const copied = input.resourceId ? await this.store.resources.copy(id, input.resourceId, input.name, guard) : null - if (copied) { - result = copied + result = await this.#downloads.run(input.id, running, owner, [running.abort.signal, lifetime], async (signal) => { + const guard = () => { + signal.throwIfAborted() + assertCurrent() } - else { - response = input.url - ? await this.#networkResponse(running, input.url, signal) - : await this.#fileResponse(running, { resourceId: input.resourceId! }, guard, signal) - const name = input.name ?? (input.url ? decodeURIComponent(new URL(input.url).pathname.split('/').at(-1) || 'download') : 'resource') - const mimeType = response.headers.get('content-type')?.split(';')[0]?.trim() || 'application/octet-stream' - result = await this.store.resources.import(id, name.slice(0, 255), mimeType, response, signal, guard) + let response: Response | undefined + try { + if (method === 'network.download' && (!input.url || input.resourceId)) + throw new Error('EXTENSION_INVALID_ARGUMENT') + if (method === 'resources.importFile' && (!input.resourceId || input.url)) + throw new Error('EXTENSION_RESOURCE_UNAVAILABLE') + const copied = input.resourceId ? await this.store.resources.copy(id, input.resourceId, input.name, guard) : null + if (copied) { + return copied + } + else { + response = input.url + ? await this.#networkResponse(running, input.url, signal) + : await this.#fileResponse(running, { resourceId: input.resourceId! }, guard, signal) + const name = input.name ?? (input.url ? decodeURIComponent(new URL(input.url).pathname.split('/').at(-1) || 'download') : 'resource') + const mimeType = response.headers.get('content-type')?.split(';')[0]?.trim() || 'application/octet-stream' + return await this.store.resources.import(id, name.slice(0, 255), mimeType, response, signal, guard) + } } - } - finally { - this.#downloads.delete(input.id) - await response?.body?.cancel().catch(() => {}) - } + finally { + await response?.body?.cancel().catch(() => {}) + } + }) } else if (method === 'network.cancel') { const input = z.object({ id: z.string().uuid() }).strict().parse(params) - const download = this.#downloads.get(input.id) - if (download) { - if (download.running !== running || download.owner !== owner) - throw new Error('EXTENSION_RESOURCE_DENIED') - download.abort.abort(new Error('EXTENSION_RESOURCE_DOWNLOAD_CANCELLED')) - } + this.#downloads.cancel(input.id, running, owner) result = null } else if (method === 'links.openExternal') { @@ -2364,7 +2305,7 @@ export class ExtensionService { else if (method === 'views.broadcast') { if (running.package.manifest.apiVersion < 3) throw new Error('EXTENSION_METHOD_DENIED') - await this.#ports.workbench({ kind: 'message', requestId: randomUUID(), extensionId: id, generation: running.generation, message: extensionJsonSchema.parse(params) }, running.abort.signal) + await this.#ports.workbench({ kind: 'message', requestId: randomUUID(), extensionId: id, generation: running.generation, message: extensionJsonSchema.parse(params) }, lifetime) result = null } else if (method === 'placements.show' || method === 'placements.hide') { @@ -2376,7 +2317,7 @@ export class ExtensionService { if (!!(placement.kind === 'view' && placement.interaction) !== !!input.interactionId || (input.interactionId && interaction?.running !== running)) throw new Error('EXTENSION_INTERACTION_ENDED') const requestId = randomUUID() - result = await this.#ports.workbench({ kind: 'placement', requestId, extensionId: id, generation: running.generation, placementId: placement.id, visible: method === 'placements.show', ...(input.instanceId ? { instanceId: input.instanceId } : {}), ...(input.interactionId ? { interactionId: input.interactionId } : {}) }, interaction ? AbortSignal.any([running.abort.signal, interaction.abort.signal]) : running.abort.signal) + result = await this.#ports.workbench({ kind: 'placement', requestId, extensionId: id, generation: running.generation, placementId: placement.id, visible: method === 'placements.show', ...(input.instanceId ? { instanceId: input.instanceId } : {}), ...(input.interactionId ? { interactionId: input.interactionId } : {}) }, interaction ? AbortSignal.any([lifetime, interaction.abort.signal]) : lifetime) if (placement.kind !== 'view') { if (result !== requestId) throw new Error('EXTENSION_PLACEMENT_UNAVAILABLE') @@ -2396,9 +2337,9 @@ export class ExtensionService { if (view.resource === 'selected-file' && !input.resource) throw new Error('EXTENSION_RESOURCE_REQUIRED') if (input.resource) - await this.#resource(running, input.resource.id) - this.#assertCurrent(running) - result = await this.#ports.workbench({ kind: 'open', requestId: randomUUID(), extensionId: id, generation: running.generation, viewType: view.id, resource: input.resource, state: input.state, stateVersion: view.stateVersion }, running.abort.signal) + await this.#selected.resolve(this.#selectedAccess(running), input.resource.id) + assertCurrent() + result = await this.#ports.workbench({ kind: 'open', requestId: randomUUID(), extensionId: id, generation: running.generation, viewType: view.id, resource: input.resource, state: input.state, stateVersion: view.stateVersion }, lifetime) if (!result) throw new Error('EXTENSION_VIEW_UNAVAILABLE') } @@ -2409,11 +2350,12 @@ export class ExtensionService { else { throw new Error('EXTENSION_METHOD_DENIED') } - this.#assertCurrent(running) + assertCurrent() return result } finally { running.inFlight-- + this.#scheduleIdle() if (!running.inFlight) { this.#retired.delete(running) for (const resolve of running.drained) resolve() @@ -2432,9 +2374,9 @@ export class ExtensionService { throw new Error('EXTENSION_RESOURCE_REQUIRED') let source: ExtensionFileOpeningSource | null = null if (selected && input.source !== 'view') { - const response = await this.#selectedResponse(running, selected.id, signal) + const response = await this.#selected.response(this.#selectedAccess(running), selected.id, signal) await response.body?.cancel() - source = await this.#resource(running, selected.id) + source = await this.#selected.resolve(this.#selectedAccess(running), selected.id) } const target = extensionReferenceTargetSchema.parse({ extensionId, viewId: view.input.viewId, viewType: view.input.viewType, source }) return { @@ -2451,9 +2393,9 @@ export class ExtensionService { let name: string let response: Response if (selected?.id === resourceId) { - const source = await this.#resource(running, resourceId) - response = await this.#selectedResponse(running, resourceId, signal) - if (source.kind === 'space' && !this.#fileSnapshots.has(resourceId)) { + const source = await this.#selected.resolve(this.#selectedAccess(running), resourceId) + response = await this.#selected.response(this.#selectedAccess(running), resourceId, signal) + if (source.kind === 'space' && !this.#selected.hasSnapshot(resourceId)) { await response.body?.cancel() return { kind: 'source', source: { spaceId: source.target.spaceId, bindingId: source.target.directoryId, relativePath: source.target.path } } } @@ -2472,77 +2414,8 @@ export class ExtensionService { return { kind: 'file', name, mimeType: response.headers.get('content-type') ?? 'application/octet-stream', base64: Buffer.from(bytes).toString('base64') } } - async #resource(running: RunningExtension, id: string): Promise { - if (running.package.manifest.permissions.selectedResource !== 'read') - throw new Error('EXTENSION_RESOURCE_DENIED') - const snapshot = this.#fileSnapshots.get(id) - if (snapshot && snapshot.running !== running) - throw new Error('EXTENSION_RESOURCE_DENIED') - const source = snapshot?.source ?? await this.store.resolveFileOpeningGrant(running.package.manifest.id, id) - this.#assertCurrent(running) - return source - } - - async #sourceResponse(source: ExtensionFileOpeningSource, signal: AbortSignal, request?: Request): Promise<{ name: string, response: Response }> { - if (source.kind === 'artifact') { - if (!this.#ports.readArtifactResource) - throw new Error('EXTENSION_RESOURCE_UNAVAILABLE') - return this.#ports.readArtifactResource(source.artifactId, signal, request) - } - if (source.kind === 'task') { - if (!this.#ports.readTaskFileResource) - throw new Error('EXTENSION_RESOURCE_UNAVAILABLE') - return this.#ports.readTaskFileResource(source.target, signal, request) - } - const response = this.#ports.readResource - ? await this.#ports.readResource(source.target, signal, request) - : new Response(await this.#ports.readText(source.target, signal)) - return { name: source.target.path.split('/').at(-1) ?? source.target.path, response } - } - - async #selectedResponse(running: RunningExtension, id: string, lifetime: AbortSignal, request?: Request): Promise { - const signal = AbortSignal.any([running.abort.signal, lifetime]) - const source = await this.#resource(running, id) - const snapshot = this.#fileSnapshots.get(id) - const { response } = await this.#sourceResponse(source, signal, snapshot ? undefined : request) - try { - this.#assertCurrent(running) - signal.throwIfAborted() - } - catch (error) { - await response.body?.cancel().catch(() => {}) - throw error - } - if (!snapshot) - return response - await response.body?.cancel() - if (this.#fileSnapshots.get(id) !== snapshot) - throw new Error('EXTENSION_RESOURCE_DENIED') - const bytes = Buffer.from(snapshot.text) - const range = fileRange(bytes.length, request?.headers.get('range') ?? null) - return new Response(request?.method === 'HEAD' || range.status === 416 ? null : bytes.subarray(range.start, range.end + 1), { status: range.status, headers: { ...range.headers, 'content-type': response.headers.get('content-type') ?? 'text/plain; charset=utf-8', 'content-security-policy': 'default-src \'none\'; sandbox', 'cache-control': 'no-store', 'x-content-type-options': 'nosniff', 'access-control-allow-origin': '*' } }) - } - - async #selectedText(running: RunningExtension, id: string, signal: AbortSignal): Promise { - const source = await this.#resource(running, id) - if (source.kind === 'space' && !this.#fileSnapshots.has(id)) - return this.#ports.readText(source.target, signal) - const response = await this.#selectedResponse(running, id, signal) - return new TextDecoder().decode(await readResponseBytes(response, 2 * 1024 * 1024)) - } - - async #selectedBytes(running: RunningExtension, id: string, offset: number, length: number, signal: AbortSignal): Promise<{ base64: string, size: number, eof: boolean }> { - const url = 'https://resource.invalid/' - const metadata = await this.#selectedResponse(running, id, signal, new Request(url, { method: 'HEAD' })) - const size = Number(metadata.headers.get('content-length')) - await metadata.body?.cancel() - if (!Number.isSafeInteger(size) || size < 0 || !metadata.headers.has('content-length')) - throw new Error('EXTENSION_RESOURCE_UNSUPPORTED') - if (offset >= size) - return { base64: '', size, eof: true } - const response = await this.#selectedResponse(running, id, signal, new Request(url, { headers: { range: `bytes=${offset}-${Math.min(offset + length, size) - 1}` } })) - const bytes = Buffer.from(await readResponseBytes(response, length)) - return { base64: bytes.toString('base64'), size, eof: offset + bytes.length >= size } + #selectedAccess(running: RunningExtension) { + return { owner: running, manifest: running.package.manifest, signal: running.abort.signal, assertCurrent: () => this.#assertCurrent(running) } } async #resourcePath(running: RunningExtension, params: unknown): Promise { @@ -2575,7 +2448,7 @@ export class ExtensionService { return this.store.resources.response(id, source.resourceId, new Request('https://resource.invalid/'), AbortSignal.any([lifetime, running.abort.signal]), allowLocal) if (spaceResource !== undefined && source.resourceId !== spaceResource) throw new Error('EXTENSION_RESOURCE_DENIED') - return this.#selectedResponse(running, source.resourceId, lifetime) + return this.#selected.response(this.#selectedAccess(running), source.resourceId, lifetime) } #networkUrl(running: RunningExtension, raw: string): URL { @@ -2645,17 +2518,18 @@ export class ExtensionService { if (!running) return this.#pendingStops.set(id, running) + if (running.idleTimer) + clearTimeout(running.idleTimer) + running.idleTimer = undefined + running.runUsers.clear() this.#running.delete(id) running.resourceEndpoint?.dispose() this.#ports.themes?.releaseOwner(`${id}:${running.generation}`) if (running.inFlight) this.#retired.add(running) running.abort.abort() - await this.#cancelUploads(running) - for (const [resourceId, snapshot] of this.#fileSnapshots) { - if (snapshot.running === running) - this.#fileSnapshots.delete(resourceId) - } + await this.#uploads.dispose(running) + this.#selected.releaseOwner(running) this.#publish({ kind: 'host', extensionId: id, generation: running.generation, status: 'stopping' }) for (const [interactionId, interaction] of this.#interactions) { if (interaction.running === running) @@ -2672,6 +2546,7 @@ export class ExtensionService { await Promise.allSettled([...running.inferencePreparations]) await running.host.dispose() this.#pendingStops.delete(id) + this.#scheduleIdle() this.#publish({ kind: 'host', extensionId: id, generation: running.generation, status: 'stopped' }) } catch (error) { @@ -2741,33 +2616,24 @@ export class ExtensionService { return task } - async #cancelUploads(running: RunningExtension, owner?: string): Promise { - const matches = (entry: { running: RunningExtension, owner: string }) => entry.running === running && (owner === undefined || entry.owner === owner) - await Promise.all([ - ...[...this.#uploadCreations].filter(matches).map(entry => entry.promise.then(upload => upload.dispose(), () => {})), - ...[...this.#uploads.values()].filter(matches).map(entry => entry.upload.dispose()), - ]) - } - #closeView(view: RunningView, retainResource?: string): void { if (this.#views.get(view.input.viewId) !== view) return this.#views.delete(view.input.viewId) const resourceId = view.input.resource?.id if (resourceId && resourceId !== retainResource && ![...this.#views.values()].some(current => current.input.resource?.id === resourceId)) - this.#fileSnapshots.delete(resourceId) + this.#selected.release(resourceId) this.#ports.themes?.releaseOwner(`${view.running.package.manifest.id}:${view.running.generation}:${view.input.viewId}`) this.#cancelUnconfirmedTaskCleanup(view.running) view.abort.abort() - const cleanups: Promise[] = [this.#cancelUploads(view.running, view.session.id)] + const cleanups: Promise[] = [this.#uploads.dispose(view.running, view.session.id)] if (this.#documents) cleanups.push(this.#documents.close(view.input.extensionId, view.input.viewId, view.session.token)) try { view.dispose() } catch (error) { cleanups.push(Promise.reject(error)) } - for (const writer of view.writers.values()) cleanups.push(writer.dispose()) - view.writers.clear() + cleanups.push(view.exports.dispose()) this.#publish({ kind: 'view', extensionId: view.input.extensionId, generation: view.running.generation, viewId: view.input.viewId, status: 'closed' }) const cleanup = Promise.allSettled(cleanups).then((results) => { const failure = results.find(result => result.status === 'rejected') @@ -2776,7 +2642,10 @@ export class ExtensionService { this.#viewCleanupError ??= new Error(errorCode) this.#publish({ kind: 'view', extensionId: view.input.extensionId, generation: view.running.generation, viewId: view.input.viewId, status: 'cleanup-failed', errorCode }) } - }).finally(() => this.#viewCleanups.delete(cleanup)) + }).finally(() => { + this.#viewCleanups.delete(cleanup) + this.#scheduleIdle() + }) this.#viewCleanups.add(cleanup) } diff --git a/apps/buddy/platform/extensions/ExtensionUploads.ts b/apps/buddy/platform/extensions/ExtensionUploads.ts new file mode 100644 index 000000000..8d3b47e45 --- /dev/null +++ b/apps/buddy/platform/extensions/ExtensionUploads.ts @@ -0,0 +1,55 @@ +import type { JsonValue } from '../../shared/workbench/workbenchState' +import type { ExtensionResourceUpload } from './ExtensionResourceUpload' +import { extensionResourceWriteRequestSchema } from '../../shared/extensions/extensionResources' + +export class ExtensionUploads { + readonly #creating = new Set<{ host: object, owner: string, promise: Promise }>() + readonly #uploads = new Map() + + has(id: string): boolean { return this.#uploads.has(id) } + + async create(host: object, owner: string, create: () => Promise): Promise { + const creation = { host, owner, promise: create() } + this.#creating.add(creation) + try { + const upload = await creation.promise + this.#uploads.set(upload.id, { host, owner, upload }) + void upload.finished.finally(() => this.#uploads.delete(upload.id)).catch(() => {}) + return upload.id + } + finally { this.#creating.delete(creation) } + } + + async request(host: object, owner: string, method: string, params: unknown): Promise { + const input = extensionResourceWriteRequestSchema.parse(params) + const entry = this.#uploads.get(input.id) + if (!entry) + throw new Error('EXTENSION_RESOURCE_WRITE_EXPIRED') + if (entry.host !== host || entry.owner !== owner) + throw new Error('EXTENSION_RESOURCE_DENIED') + if (method === 'resources.writeChunk') { + if (input.offset === undefined || input.base64 === undefined) + throw new Error('EXTENSION_RESOURCE_WRITE_RANGE') + await entry.upload.append(input.offset, input.base64) + return null + } + if (method === 'resources.commitSave') { + try { + return { ...await entry.upload.commit() } + } + finally { await entry.upload.dispose() } + } + if (method !== 'resources.cancelSave') + throw new Error('EXTENSION_METHOD_DENIED') + await entry.upload.dispose() + return null + } + + async dispose(host: object, owner?: string): Promise { + const matches = (entry: { host: object, owner: string }) => entry.host === host && (owner === undefined || entry.owner === owner) + await Promise.all([ + ...[...this.#creating].filter(matches).map(entry => entry.promise.then(upload => upload.dispose(), () => {})), + ...[...this.#uploads.values()].filter(matches).map(entry => entry.upload.dispose()), + ]) + } +} diff --git a/apps/buddy/platform/extensions/ExtensionViewExports.ts b/apps/buddy/platform/extensions/ExtensionViewExports.ts new file mode 100644 index 000000000..0298ce01d --- /dev/null +++ b/apps/buddy/platform/extensions/ExtensionViewExports.ts @@ -0,0 +1,70 @@ +import type { JsonValue } from '../../shared/workbench/workbenchState' +import { z } from 'zod' +import { extensionResourceWriteRequestSchema } from '../../shared/extensions/extensionResources' +import { ExtensionResourceWriter } from './ExtensionResourceWriter' + +interface ExtensionViewExportAccess { + name: string + signal: AbortSignal + assertCurrent: () => void + selectSavePath?: (name: string, suggestedName: string, signal: AbortSignal) => Promise +} + +export class ExtensionViewExports { + readonly #writers = new Map() + + async request(access: ExtensionViewExportAccess, method: string, params: unknown): Promise { + if (method === 'resources.beginSave') { + if (!access.selectSavePath || this.#writers.size >= 2) + throw new Error('EXTENSION_RESOURCE_SAVE_UNAVAILABLE') + const input = z.object({ name: z.string().min(1).max(255).regex(/^[^/\\\0]+$/), size: z.number().int().min(0).max(Number.MAX_SAFE_INTEGER) }).strict().parse(params) + const signal = AbortSignal.any([access.signal, AbortSignal.timeout(120000)]) + const path = await access.selectSavePath(access.name, input.name, signal) + signal.throwIfAborted() + access.assertCurrent() + if (!path) + return null + const writer = await ExtensionResourceWriter.create(path, input.size, access.assertCurrent) + try { + access.assertCurrent() + } + catch (error) { + await writer.dispose() + throw error + } + this.#writers.set(writer.id, writer) + return writer.id + } + const input = extensionResourceWriteRequestSchema.parse(params) + const writer = this.#writers.get(input.id) + if (!writer) + throw new Error('EXTENSION_RESOURCE_WRITE_EXPIRED') + if (method === 'resources.writeChunk') { + if (input.offset === undefined || input.base64 === undefined) + throw new Error('EXTENSION_RESOURCE_WRITE_RANGE') + await writer.append(input.offset, input.base64) + } + else { + if (method !== 'resources.commitSave' && method !== 'resources.cancelSave') + throw new Error('EXTENSION_METHOD_DENIED') + try { + if (method === 'resources.commitSave') + await writer.commit() + } + finally { + this.#writers.delete(input.id) + await writer.dispose() + } + } + return null + } + + async dispose(): Promise { + const cleanups = [...this.#writers.values()].map(writer => writer.dispose()) + this.#writers.clear() + const results = await Promise.allSettled(cleanups) + const failure = results.find(result => result.status === 'rejected') + if (failure?.status === 'rejected') + throw failure.reason + } +} diff --git a/apps/buddy/platform/extensions/__tests__/ExtensionCardActions.spec.ts b/apps/buddy/platform/extensions/__tests__/ExtensionCardActions.spec.ts index 96d67f1ab..b0a8697b3 100644 --- a/apps/buddy/platform/extensions/__tests__/ExtensionCardActions.spec.ts +++ b/apps/buddy/platform/extensions/__tests__/ExtensionCardActions.spec.ts @@ -28,7 +28,6 @@ async function fixture(confirm: (preview: ExtensionTaskCleanupPreview, signal: A } }))).token) let broker!: (method: string, params: unknown) => Promise const deleted: { conversationId: string, expectedRevision: number }[] = [] - let genericChanges = 0 let hostGeneration = 0 const progressMessages: { extensionId: string, generation: number, params: Record }[] = [] const cancelledTokens: JsonValue[] = [] @@ -62,7 +61,6 @@ async function fixture(confirm: (preview: ExtensionTaskCleanupPreview, signal: A return null }, dispose: async () => {}, devtools: () => {} } }, - changed: () => { genericChanges++ }, createView: () => ({ token: randomUUID(), url: 'lexora-extension://fixture/__view.html', dispose: () => {} }), workbench: async () => null, readText: async () => '', @@ -80,7 +78,7 @@ async function fixture(confirm: (preview: ExtensionTaskCleanupPreview, signal: A await service.execute(definition.id, 'tests.reader.cleanup', null) return { service, deleted, progressMessages, cancelledTokens, onProgress: (handler: typeof onProgress) => { onProgress = handler - }, genericChanges: () => genericChanges, broker: (method: string, params: unknown) => broker(method, params), token: () => capturedToken, onCommand: (handler: typeof onCommand) => { + }, broker: (method: string, params: unknown) => broker(method, params), token: () => capturedToken, onCommand: (handler: typeof onCommand) => { onCommand = handler }, run: (trusted = true, menu = 'tests.reader.action') => service.executeMenu(definition.id, menu, { target: 'extension.actions', resource: null }, trusted) } } @@ -91,7 +89,6 @@ it('accepts a main-only command, confirms once and returns host-owned per-item r previews.push(preview) return true }) - expect((await f.service.list())[0]?.registeredCommands).toEqual(['tests.reader.cleanup']) expect(await f.run()).toMatchObject({ taskCleanup: { status: 'completed', results: [{ conversationId: 'legacy', expectedRevision: 8, status: 'deleted' }], summary: { phase: 'completed', total: 1, processed: 1, deleted: 1, pending: 0 } } }) expect(previews).toEqual([{ total: 1, skipped: 0 }]) expect(f.deleted).toEqual([{ conversationId: 'legacy', expectedRevision: 8 }]) @@ -109,6 +106,9 @@ it('never grants cleanup to background, generic commands, slash, view self-calls await expect(f.broker('commands.execute', { command: 'tests.reader.cleanup', arguments: null })).rejects.toThrow('EXTENSION_METHOD_DENIED') await expect(f.run(true, 'tests.other.action')).rejects.toThrow('EXTENSION_COMMAND_UNAVAILABLE') expect(f.deleted).toEqual([]) + await f.broker('commands.changed', { commands: [] }) + expect((await f.service.list())[0]?.registeredCommands).toEqual([]) + await expect(f.run()).rejects.toThrow('EXTENSION_COMMAND_UNAVAILABLE') }) it('executes ordinary actions without opening a view and ignores forged cleanup feedback', async () => { @@ -135,27 +135,6 @@ it('consumes cancellation and rejects reuse while the command is still running', expect(f.deleted).toEqual([]) }) -it('revokes confirmation when the plugin is disabled and hides disposed commands', async () => { - const answer = deferred() - const started = deferred() - const f = await fixture(async () => { - started.resolve() - return answer.promise - }) - const result = f.run() - const rejected = expect(result).rejects.toThrow('EXTENSION_HOST_STOPPED') - await started.promise - await f.service.enable('tests.reader', false) - answer.resolve(true) - await rejected - expect(f.deleted).toEqual([]) - expect((await f.service.list())[0]?.registeredCommands).toEqual([]) - const second = await fixture() - await second.broker('commands.changed', { commands: [] }) - expect((await second.service.list())[0]?.registeredCommands).toEqual([]) - await expect(second.run()).rejects.toThrow('EXTENSION_COMMAND_UNAVAILABLE') -}) - it.each(['disable', 'dispose', 'reset'] as const)('releases plugin confirmation on %s with no late approval', async (action) => { const answer = deferred() const started = deferred() @@ -166,11 +145,16 @@ it.each(['disable', 'dispose', 'reset'] as const)('releases plugin confirmation const run = f.run() const rejected = expect(run).rejects.toThrow('EXTENSION_HOST_STOPPED') const signal = await started.promise - if (action === 'disable') + if (action === 'disable') { await f.service.enable('tests.reader', false) - else if (action === 'dispose') + expect((await f.service.list())[0]?.registeredCommands).toEqual([]) + } + else if (action === 'dispose') { await f.service.dispose() - else await f.service.resetHosts() + } + else { + await f.service.resetHosts() + } await rejected expect(signal.aborted).toBe(true) expect(f.cancelledTokens).toEqual([f.token()]) @@ -179,9 +163,9 @@ it.each(['disable', 'dispose', 'reset'] as const)('releases plugin confirmation expect(f.deleted).toEqual([]) }) -it.each([false, 'true', 1, null] as const)('fails closed for confirmation response %s', async (response) => { - const f = await fixture(async () => response) - expect(await f.run()).toMatchObject({ taskCleanup: { results: [], summary: { phase: response === false ? 'cancelled' : 'failed', processed: 0 } } }) +it('rejects a truthy non-boolean confirmation response', async () => { + const f = await fixture(async () => 'true') + expect(await f.run()).toMatchObject({ taskCleanup: { results: [], summary: { phase: 'failed', processed: 0 } } }) expect(f.deleted).toEqual([]) }) @@ -282,7 +266,6 @@ it('invalidates a confirmation if the plugin stops awaiting its invocation', asy it('pushes bounded progress only to the initiating token and generation', async () => { const f = await fixture() await f.service.execute('tests.peer', 'tests.peer.noop', null) - const before = f.genericChanges() const entered = deferred() const release = deferred() f.onProgress(async (params) => { @@ -298,7 +281,6 @@ it('pushes bounded progress only to the initiating token and generation', async release.resolve() const result = await run as { taskCleanup: { summary: JsonValue } } expect(f.progressMessages.at(-1)!.params.progress).toEqual(result.taskCleanup.summary) - expect(f.genericChanges()).toBe(before) expect(new Set(f.progressMessages.map(item => item.generation)).size).toBe(1) const previous = f.progressMessages[0]! for (const message of f.progressMessages) { diff --git a/apps/buddy/platform/extensions/__tests__/ExtensionCatalogService.spec.ts b/apps/buddy/platform/extensions/__tests__/ExtensionCatalogService.spec.ts index 7ee9a95b9..0ad454849 100644 --- a/apps/buddy/platform/extensions/__tests__/ExtensionCatalogService.spec.ts +++ b/apps/buddy/platform/extensions/__tests__/ExtensionCatalogService.spec.ts @@ -72,7 +72,6 @@ it('binds risk confirmation to the reviewed snapshot and retains actual integrit expect(new TextDecoder().decode(await reloaded.asset(pkg, 'view.js'))).toBe('export function render() {}') await writeFile(join(reloaded.packageRoot(pkg), 'view.js'), 'changed after installation') await expect(reloaded.asset(pkg, 'view.js')).rejects.toThrow('EXTENSION_PACKAGE_CHANGED') - await expect(service.download(value.id, '9.0.0', new AbortController().signal)).rejects.toThrow('EXTENSION_CATALOG_ENTRY_UNAVAILABLE') }) it.each([ @@ -111,7 +110,7 @@ it('shows only the latest release and refuses older marketplace versions', async it('isolates future APIs and invalid entries while preserving exact supported downloads and offline metadata', async () => { const { service, catalog, value, bytes, offline } = await fixture() const raw = catalog.plugins as unknown[] - raw.push({ ...catalog.plugins[0], manifest: { ...value, id: 'tests.future', apiVersion: 5, engines: { lexora: '*' }, contributes: { futureCapability: {} } } }) + raw.push({ ...catalog.plugins[0], manifest: { ...value, id: 'tests.future', apiVersion: 6, engines: { lexora: '*' }, contributes: { futureCapability: {} } } }) raw.push({ ...catalog.plugins[0], manifest: { ...value, id: 'tests.invalid', engines: { lexora: '*' }, futureField: true } }) raw.push({ broken: true }) const first = await service.list() @@ -130,7 +129,7 @@ it('isolates future APIs and invalid entries while preserving exact supported do }) const GITHUB_ARTIFACT = 'https://github.com/useLexora/plugins/releases/download/plugin/plugin.lexora-extension' -async function mirrorFixture(downloadMirror: 'auto' | 'off', respond: (url: string, bytes: Uint8Array) => Response) { +async function mirrorFixture(respond: (url: string, bytes: Uint8Array) => Response) { const { root, store } = await createStore() const value = manifest() const bytes = zipSync({ 'extension.json': strToU8(JSON.stringify(value)), 'extension.js': strToU8('export function activate() {}'), 'view.js': strToU8('export function render() {}') }) @@ -139,19 +138,18 @@ async function mirrorFixture(downloadMirror: 'auto' | 'off', respond: (url: stri const service = new ExtensionCatalogService(store.root, store.appVersion, async (url) => { requests.push(url) return url === EXTENSION_CATALOG_URL ? Response.json(catalog) : respond(url, bytes) - }, undefined, () => ({ ...DEFAULT_DOWNLOAD_SETTINGS, enabled: downloadMirror === 'auto' })) + }, undefined, () => DEFAULT_DOWNLOAD_SETTINGS) cleanup.push(async () => { service.dispose() await rm(root, { recursive: true, force: true }) }) await service.list() - expect(requests).toEqual([EXTENSION_CATALOG_URL]) requests.length = 0 return { service, value, bytes, requests } } it('downloads packages through a mirror first, skipping one that alters the file or fails', async () => { - const { service, value, bytes, requests } = await mirrorFixture('auto', (url, content) => { + const { service, value, bytes, requests } = await mirrorFixture((url, content) => { if (url === `${DEFAULT_DOWNLOAD_SETTINGS.proxies[0]!.url}${GITHUB_ARTIFACT}`) return new Response(null, { status: 503 }) return new Response(url.startsWith(DEFAULT_DOWNLOAD_SETTINGS.proxies[1]!.url) ? content.slice().reverse() : content) @@ -161,21 +159,15 @@ it('downloads packages through a mirror first, skipping one that alters the file expect(requests).toEqual([`${DEFAULT_DOWNLOAD_SETTINGS.proxies[0]!.url}${GITHUB_ARTIFACT}`, `${DEFAULT_DOWNLOAD_SETTINGS.proxies[1]!.url}${GITHUB_ARTIFACT}`, `${DEFAULT_DOWNLOAD_SETTINGS.proxies[2]!.url}${GITHUB_ARTIFACT}`]) }) -it('downloads packages from GitHub only when mirrors are off', async () => { - const { service, value, requests } = await mirrorFixture('off', (_url, content) => new Response(content)) - await service.download(value.id, value.version, new AbortController().signal) - expect(requests).toEqual([GITHUB_ARTIFACT]) -}) - it('uses the origin package for risk review after all mirrors return altered content', async () => { - const { service, value, requests } = await mirrorFixture('auto', (url, content) => new Response(url === GITHUB_ARTIFACT ? archive(manifest({ name: 'Republished package' })) : content.slice().reverse())) + const { service, value, requests } = await mirrorFixture((url, content) => new Response(url === GITHUB_ARTIFACT ? archive(manifest({ name: 'Republished package' })) : content.slice().reverse())) const result = await service.download(value.id, value.version, new AbortController().signal) expect(requests.at(-1)).toBe(GITHUB_ARTIFACT) expect(result.source).toMatchObject({ artifact: GITHUB_ARTIFACT, sha256: sha256(result.bytes), mismatch: { actualSize: result.bytes.length } }) }) it('does not offer a corrupt mirror package when the origin cannot be reached', async () => { - const { service, value } = await mirrorFixture('auto', (url, content) => { + const { service, value } = await mirrorFixture((url, content) => { if (url === GITHUB_ARTIFACT) throw new Error('Offline') return new Response(content.slice().reverse()) diff --git a/apps/buddy/platform/extensions/__tests__/ExtensionExecutionScope.spec.ts b/apps/buddy/platform/extensions/__tests__/ExtensionExecutionScope.spec.ts new file mode 100644 index 000000000..5b912cfe2 --- /dev/null +++ b/apps/buddy/platform/extensions/__tests__/ExtensionExecutionScope.spec.ts @@ -0,0 +1,82 @@ +import type { ExtensionManifest } from '../../../shared/extensions/extensionManifest' +import { rm } from 'node:fs/promises' +import { afterEach, describe, expect, it } from 'vitest' +import { extensionManifestSchema } from '../../../shared/extensions/extensionManifest' +import { extensionActivationOrder } from '../ExtensionPackageStore' +import { createStore, manifest, reviewPackage } from './fixtures' + +const roots: string[] = [] +afterEach(async () => { + await Promise.all(roots.splice(0).map(root => rm(root, { recursive: true, force: true }))) +}) +async function fixture() { + const value = await createStore() + roots.push(value.root) + return value +} +function service(id: string, dependencies: Record = {}, version = '1.0.0'): ExtensionManifest { + return manifest({ id, apiVersion: 5, executionScope: 'service', version, dependencies, permissions: {}, contributes: {} }) +} + +describe('explicit extension execution lifetime', () => { + it('keeps pre-service APIs confined to desktop execution', () => { + const input = manifest({ apiVersion: 4 }) + expect(extensionManifestSchema.parse(input).executionScope).toBe('desktop') + expect(extensionManifestSchema.safeParse({ ...input, executionScope: 'service' }).success).toBe(false) + }) + + it('requires an explicit API 5 backend without unknown permissions or autonomous schedules', () => { + const input = service('tests.background') + expect(extensionManifestSchema.safeParse({ ...input, entry: undefined }).success).toBe(false) + expect(extensionManifestSchema.safeParse({ ...input, permissions: { schedules: true } }).success).toBe(false) + expect(extensionManifestSchema.safeParse({ ...input, permissions: { backgroundNetwork: true } }).success).toBe(false) + expect(extensionManifestSchema.safeParse({ ...input, executionScope: 'unbounded' }).success).toBe(false) + expect(extensionManifestSchema.parse({ ...input, executionScope: 'desktop', permissions: { schedules: true } }).executionScope).toBe('desktop') + }) + + it('reviews the complete service dependency graph and preserves a dependency when a scope downgrade is rejected', async () => { + const { root, store } = await fixture() + const leaf = service('tests.leaf') + const middle = service('tests.middle', { [leaf.id]: '^1.0.0' }) + const backend = service('tests.background', { [middle.id]: '^1.0.0' }) + await expect(reviewPackage(root, store, backend)).rejects.toThrow('EXTENSION_DEPENDENCY_VERSION') + for (const item of [leaf, middle, backend]) + await store.install((await reviewPackage(root, store, item)).token) + expect(extensionActivationOrder(store.installed, backend.id)).toEqual([leaf.id, middle.id, backend.id]) + const prior = store.installed[leaf.id]!.current + await expect(reviewPackage(root, store, manifest({ ...leaf, version: '1.1.0', executionScope: 'desktop' }))).rejects.toThrow('EXTENSION_DEPENDENCY_EXECUTION_SCOPE') + expect(store.installed[leaf.id]!.current).toEqual(prior) + expect(store.installed[leaf.id]!.pending).toBeNull() + await store.enable(leaf.id, false) + expect(() => extensionActivationOrder(store.installed, backend.id)).toThrow('EXTENSION_DEPENDENCY_UNAVAILABLE') + }) + + it('pins reviewed dependency revisions until installation commits', async () => { + const { root, store } = await fixture() + const dependency = service('tests.dependency') + await store.install((await reviewPackage(root, store, dependency)).token) + const backend = service('tests.background', { [dependency.id]: '^1.0.0' }) + const review = await reviewPackage(root, store, backend) + expect(review.addedPermissions).toContain('executionScope:service') + await store.install((await reviewPackage(root, store, service(dependency.id, {}, '1.1.0'))).token) + await store.promote(dependency.id) + await expect(store.install(review.token)).rejects.toThrow('EXTENSION_REVIEW_CHANGED') + expect(store.installed[backend.id]).toBeUndefined() + expect(store.installed[dependency.id]!.current.manifest.version).toBe('1.1.0') + await store.install((await reviewPackage(root, store, backend)).token) + expect(store.installed[backend.id]!.current.manifest.executionScope).toBe('service') + }) + + it('requires review for a desktop to service upgrade and does not change the current package before promotion', async () => { + const { root, store } = await fixture() + const desktop = manifest({ id: 'tests.upgrade', permissions: {}, contributes: {} }) + await store.install((await reviewPackage(root, store, desktop)).token) + const review = await reviewPackage(root, store, service(desktop.id, {}, '1.1.0')) + expect(review).toMatchObject({ previousExecutionScope: 'desktop', addedPermissions: ['executionScope:service'] }) + await store.install(review.token) + expect(store.installed[desktop.id]!.current.manifest.executionScope).toBe('desktop') + expect(store.installed[desktop.id]!.pending!.manifest.executionScope).toBe('service') + await store.promote(desktop.id) + expect(store.installed[desktop.id]!.current.manifest.executionScope).toBe('service') + }) +}) diff --git a/apps/buddy/platform/extensions/__tests__/ExtensionLifetime.spec.ts b/apps/buddy/platform/extensions/__tests__/ExtensionLifetime.spec.ts new file mode 100644 index 000000000..08be206ce --- /dev/null +++ b/apps/buddy/platform/extensions/__tests__/ExtensionLifetime.spec.ts @@ -0,0 +1,194 @@ +import type { ExtensionAgentInvocation } from '../../../shared/extensions/extensionAgent' +import type { JsonValue } from '../../../shared/workbench/workbenchState' +import type { ExtensionServicePorts } from '../ExtensionService' +import { randomUUID } from 'node:crypto' +import { rm } from 'node:fs/promises' +import { deferred } from '@buddy-tests/deferred' +import { afterEach, expect, it, vi } from 'vitest' +import { ExtensionService } from '../ExtensionService' +import { createStore, manifest, reviewPackage } from './fixtures' + +type Broker = (method: string, params: unknown) => Promise +const cleanup: Array<() => Promise> = [] +afterEach(async () => { + vi.useRealTimers() + for (const action of cleanup.splice(0).reverse()) await action() +}) +function definition(id = 'tests.service', scope: 'desktop' | 'service' = 'service', dependencies = {}) { + return manifest({ id, apiVersion: 5, executionScope: scope, dependencies, permissions: { agent: true, network: ['https://example.com'] }, contributes: { + commands: [{ id: `${id}.open`, title: 'Open' }], + views: [{ id: `${id}.view`, title: 'View', entry: 'view.js', resource: 'none' }], + agent: { tools: [{ id: `${id}.tool`, title: 'Tool', description: 'Fixture', parameters: { type: 'object', properties: {} } }] }, + } }) +} +async function fixture(overrides: Partial = {}, definitions = [definition()]) { + const { root, store } = await createStore() + cleanup.push(() => rm(root, { recursive: true, force: true })) + for (const value of definitions) await store.install((await reviewPackage(root, store, value)).token) + const hosts: Array<{ id: string, broker: Broker, disposed: boolean }> = [] + const service = new ExtensionService(store, { + desktopAvailable: () => false, + createHost: (pkg, broker) => { + const host = { id: pkg.manifest.id, broker, disposed: false } + hosts.push(host) + return { call: async () => null, dispose: async () => { + host.disposed = true + }, devtools: () => {} } + }, + createView: () => ({ token: randomUUID(), url: 'lexora-extension://fixture/view', dispose: () => {} }), + changed: () => {}, + workbench: async event => event.requestId, + get: async () => new Response('network'), + readText: async () => '', + ...overrides, + }) + cleanup.push(() => service.dispose()) + async function input(id = definitions.at(-1)!.id, runId: string | null = 'run'): Promise { + const descriptor = (await service.agentContributions()).find(item => item.id === id)! + return { extensionId: id, revision: descriptor.revision, configurationRevision: descriptor.configurationRevision, invocationId: randomUUID(), tool: `${id}.tool`, input: {}, context: { taskId: 'task', runId } } + } + return { service, store, hosts, input } +} +const scoped = (broker: Broker, invocationId: string, method: string, params: JsonValue = null) => broker('invocation.request', { invocationId, method, params }) + +it('allows controlled activation storage, but grants effects only to a live invocation and preserves that fence across awaits', async () => { + let broker!: Broker + let invocationId = '' + let lateWrite: Promise | undefined + const writeEntered = deferred() + const writeRelease = deferred() + const callbackRelease = deferred() + const f = await fixture({ createHost: (_pkg, request) => { + broker = request + return { call: async (method, params) => { + if (method === 'activate') { + await broker('storage.set', { value: { initialized: true }, version: 1 }) + await expect(broker('network.get', { url: 'https://example.com' })).rejects.toThrow('EXTENSION_INVOCATION_REQUIRED') + } + if (method === 'agent.invoke') { + invocationId = (params as { invocationId: string }).invocationId + expect(await scoped(broker, invocationId, 'network.get', { url: 'https://example.com' })).toMatchObject({ text: 'network' }) + await expect(broker('storage.set', { value: { unowned: true }, version: 1 })).rejects.toThrow('EXTENSION_INVOCATION_REQUIRED') + lateWrite = scoped(broker, invocationId, 'storage.set', { value: { late: true }, version: 1 }) + void lateWrite.catch(() => {}) + await callbackRelease.promise + return 'done' + } + return null + }, dispose: async () => {}, devtools: () => {} } + } }) + const save = f.store.saveData.bind(f.store) + vi.spyOn(f.store, 'saveData').mockImplementation(async (...args) => { + if ((args[1] as { late?: boolean }).late) { + writeEntered.resolve() + await writeRelease.promise + } + return save(...args) + }) + const request = await f.input() + const running = f.service.invokeAgent(request, new AbortController().signal) + await writeEntered.promise + callbackRelease.resolve() + await vi.waitFor(async () => { + await expect(scoped(broker, invocationId, 'storage.get')).rejects.toThrow() + }) + writeRelease.resolve() + expect(await running).toBe('done') + await expect(lateWrite).rejects.toThrow() + expect((await f.store.data('tests.service')).value).toEqual({ initialized: true }) + await expect(scoped(broker, invocationId, 'network.get', { url: 'https://example.com' })).rejects.toThrow('EXTENSION_INVOCATION_EXPIRED') +}) + +it('withdraws desktop plugins and UI tokens while a service invocation keeps the same instance and data owner', async () => { + const hold = deferred() + const started = deferred() + const disposed = new Set() + const brokers = new Map() + const invocations = new Map() + const f = await fixture({ desktopAvailable: () => true, createHost: (pkg, broker) => { + const id = pkg.manifest.id + brokers.set(id, broker) + return { call: async (method, params, signal) => { + if (method === 'agent.invoke') { + invocations.set(id, (params as { invocationId: string }).invocationId) + if (invocations.size === 2) + started.resolve() + await Promise.race([hold.promise, new Promise(resolve => signal?.addEventListener('abort', () => resolve(), { once: true }))]) + return id + } + return null + }, dispose: async () => { disposed.add(id) }, devtools: () => {} } + } }, [definition('tests.desktop', 'desktop'), definition()]) + const serviceInput = await f.input() + const desktopInput = await f.input('tests.desktop') + const pending = f.service.invokeAgent(serviceInput, new AbortController().signal) + const old = f.service.invokeAgent(desktopInput, new AbortController().signal) + const rejected = expect(old).rejects.toThrow('EXTENSION_AGENT_CANCELLED') + await started.promise + const view = await f.service.openView({ viewId: randomUUID(), extensionId: 'tests.service', viewType: 'tests.service.view', resource: null, state: null, stateVersion: 1 }) + const generation = (await f.service.list()).find(item => item.manifest.id === 'tests.service')!.generation + await f.service.detachDesktop() + await rejected + expect(disposed).toEqual(new Set(['tests.desktop'])) + expect((await f.service.agentContributions()).map(item => item.id)).toEqual(['tests.service']) + await expect(f.service.openView({ viewId: randomUUID(), extensionId: 'tests.service', viewType: 'tests.service.view', resource: null, state: null, stateVersion: 1 })).rejects.toThrow('EXTENSION_DESKTOP_REQUIRED') + await expect(scoped(brokers.get('tests.service')!, serviceInput.invocationId, 'views.open', { type: 'tests.service.view', resource: null, state: {} })).rejects.toThrow('EXTENSION_DESKTOP_REQUIRED') + expect(await scoped(brokers.get('tests.service')!, serviceInput.invocationId, 'network.get', { url: 'https://example.com' })).toMatchObject({ text: 'network' }) + f.service.attachDesktop() + expect((await f.service.list()).find(item => item.manifest.id === 'tests.service')!.generation).toBe(generation) + await expect(f.service.viewRequest(view.id, view.generation, view.token, 'storage.get', null)).rejects.toThrow() + hold.resolve() + expect(await pending).toBe('tests.service') +}) + +it('retains a single service dependency closure until its run ends, then stops unused instances within the idle bound', async () => { + const f = await fixture({}, [definition('tests.base'), definition('tests.service', 'service', { 'tests.base': '^1.0.0' })]) + const first = await f.input() + await f.service.invokeAgent(first, new AbortController().signal) + const second = await f.input() + await f.service.invokeAgent(second, new AbortController().signal) + expect(f.hosts.map(host => host.id)).toEqual(['tests.base', 'tests.service']) + vi.useFakeTimers() + await vi.advanceTimersByTimeAsync(10_000) + expect(f.hosts.every(host => !host.disposed)).toBe(true) + f.service.releaseRun('run') + await vi.advanceTimersByTimeAsync(5_000) + expect(f.hosts.every(host => host.disposed)).toBe(true) +}) + +it('counts a desktop view as a service use and retires it after the last view and Core lifetime are released', async () => { + const f = await fixture({ desktopAvailable: () => true }) + await f.service.invokeAgent(await f.input(), new AbortController().signal) + const view = await f.service.openView({ viewId: randomUUID(), extensionId: 'tests.service', viewType: 'tests.service.view', resource: null, state: null, stateVersion: 1 }) + f.service.releaseAllRuns() + vi.useFakeTimers() + await vi.advanceTimersByTimeAsync(10_000) + expect(f.hosts[0]!.disposed).toBe(false) + f.service.closeView(view.id, view.generation, view.token) + await vi.advanceTimersByTimeAsync(5_000) + expect(f.hosts[0]!.disposed).toBe(true) +}) + +it('does not redirect an in-flight UI request to a new Desktop attachment', async () => { + let broker!: Broker + const entered = deferred() + const release = deferred() + const f = await fixture({ desktopAvailable: () => true, createHost: (_pkg, request) => { + broker = request + return { call: async (method, params) => method === 'agent.invoke' + ? scoped(broker, (params as { invocationId: string }).invocationId, 'views.open', { type: 'tests.service.view', resource: null, state: {} }) + : null, dispose: async () => {}, devtools: () => {} } + }, workbench: async (event, signal) => { + entered.resolve() + await release.promise + signal?.throwIfAborted() + return event.requestId + } }) + const request = f.service.invokeAgent(await f.input(), new AbortController().signal) + const rejected = expect(request).rejects.toThrow('EXTENSION_DESKTOP_REQUIRED') + await entered.promise + await f.service.detachDesktop() + f.service.attachDesktop() + release.resolve() + await rejected +}) diff --git a/apps/buddy/platform/extensions/__tests__/ExtensionPackageStore.spec.ts b/apps/buddy/platform/extensions/__tests__/ExtensionPackageStore.spec.ts index 0e71c0703..4fb9de55e 100644 --- a/apps/buddy/platform/extensions/__tests__/ExtensionPackageStore.spec.ts +++ b/apps/buddy/platform/extensions/__tests__/ExtensionPackageStore.spec.ts @@ -4,7 +4,7 @@ import { join } from 'node:path' import { strToU8, zipSync } from 'fflate' import { afterEach, describe, expect, it } from 'vitest' import { extensionJsonSchema } from '../../../shared/extensions/extensionApi' -import { addedExtensionPermissions, extensionManifestSchema, extensionPackageManifest } from '../../../shared/extensions/extensionManifest' +import { addedExtensionPermissions, extensionManifestSchema } from '../../../shared/extensions/extensionManifest' import { compileExtensionSource } from '../compileExtensionSource' import { readExtensionDirectory, unpackExtension } from '../extensionFiles' import { extensionActivationOrder, ExtensionPackageStore } from '../ExtensionPackageStore' @@ -81,7 +81,7 @@ describe('extension package contract', () => { }) it('rejects foreign contribution ownership, duplicate IDs, and unsupported API versions', () => { const value = manifest() - expect(extensionManifestSchema.safeParse({ ...value, apiVersion: 5 }).success).toBe(false) + expect(extensionManifestSchema.safeParse({ ...value, apiVersion: 6 }).success).toBe(false) expect(extensionManifestSchema.safeParse({ ...value, contributes: { ...value.contributes, commands: [{ id: 'another.reader.open', title: 'Open' }] } }).success).toBe(false) expect(extensionManifestSchema.safeParse({ ...value, contributes: { ...value.contributes, commands: [value.contributes.commands[0], value.contributes.commands[0]] } }).success).toBe(false) }) @@ -145,12 +145,10 @@ describe('extension package contract', () => { }, new AbortController().signal, () => {})).rejects.toThrow('EXTENSION_COMPILE_OUTPUT_INVALID') expect(store.installed).toEqual(installed) }) - it('detects installed file tampering and rejects development symlinks', async () => { + it('rejects development symlinks', async () => { const { root, store } = await fixture() await store.install((await reviewPackage(root, store)).token) const pkg = store.installed['tests.reader']!.current - await writeFile(join(store.packageRoot(pkg), 'view.js'), 'changed') - await expect(store.asset(pkg, 'view.js')).rejects.toThrow('EXTENSION_PACKAGE_CHANGED') await symlink(join(store.packageRoot(pkg), 'extension.js'), join(store.packageRoot(pkg), 'linked.js')) await expect(readExtensionDirectory(store.packageRoot(pkg))).rejects.toThrow('EXTENSION_UNSAFE_PATH') }) @@ -181,7 +179,7 @@ describe('extension package contract', () => { const path = join(store.root, 'installed.json') const index = JSON.parse(await readFile(path, 'utf8')) const future = structuredClone(index.installed[review.manifest.id]) - future.current.manifest = { ...future.current.manifest, id: 'tests.future', apiVersion: 5, engines: { lexora: '*' }, contributes: { futureCapability: {} } } + future.current.manifest = { ...future.current.manifest, id: 'tests.future', apiVersion: 6, engines: { lexora: '*' }, contributes: { futureCapability: {} } } index.installed['tests.future'] = future await writeFile(path, JSON.stringify(index)) const candidate = new ExtensionPackageStore(store.root, store.appVersion) @@ -199,7 +197,7 @@ describe('extension package contract', () => { const path = join(store.root, 'installed.json') const index = JSON.parse(await readFile(path, 'utf8')) const record = index.installed[review.manifest.id] - const pending = { ...structuredClone(record.current), manifest: { ...record.current.manifest, apiVersion: 5, version: '2.0.0', engines: { lexora: '*' }, contributes: { futureCapability: {} } } } + const pending = { ...structuredClone(record.current), manifest: { ...record.current.manifest, apiVersion: 6, version: '2.0.0', engines: { lexora: '*' }, contributes: { futureCapability: {} } } } record.pending = pending await writeFile(path, JSON.stringify(index)) const candidate = new ExtensionPackageStore(store.root, store.appVersion) @@ -246,24 +244,14 @@ describe('extension package contract', () => { }) }) -it.each([1, 2] as const)('accepts window overlays in API %s without adding local placements', (apiVersion) => { - const input = manifest({ apiVersion, permissions: { windowEffects: true }, contributes: { +it('keeps API 1 window overlays subject to their permission', () => { + const input = manifest({ apiVersion: 1, permissions: { windowEffects: true }, contributes: { views: [{ id: 'tests.reader.effect', title: 'Effect', entry: 'view.js', resource: 'none', location: 'window-overlay' }], } }) expect(extensionManifestSchema.parse(input).contributes.views[0]?.location).toBe('window-overlay') expect(extensionManifestSchema.safeParse({ ...input, permissions: {} }).success).toBe(false) }) -it.each([undefined, false])('preserves navigation header visibility %s without adding defaults to packaged manifests', (showHeader) => { - const navigation = { title: 'Reader', view: 'tests.reader.page', ...(showHeader === undefined ? {} : { showHeader }) } - const value = manifest({ apiVersion: 2, contributes: { views: [{ id: navigation.view, title: 'Reader', entry: 'view.js', resource: 'none' }], navigation } }) - expect(value.contributes.navigation?.showHeader).toBe(showHeader ?? true) - const packaged = extensionPackageManifest(value) - expect(packaged.contributes.navigation).toEqual({ title: navigation.title, view: navigation.view, ...(showHeader === false ? { showHeader: false } : {}) }) - expect(extensionManifestSchema.parse(packaged).contributes.navigation).toEqual(value.contributes.navigation) - expect(extensionManifestSchema.safeParse({ ...value, contributes: { ...value.contributes, navigation: { ...navigation, showHeader: 'false' } } }).success).toBe(false) -}) - it('keeps API 1 compatible and validates UI placements and permission upgrades in API 2', async () => { const { root, store } = await fixture() const old = manifest() @@ -326,24 +314,3 @@ it('validates each content target size and menu command ownership with API compa const next = manifest({ apiVersion: 3, permissions: { selectedResource: 'read', selectedContent: true } }) expect(addedExtensionPermissions(previous.permissions, next.permissions)).toEqual(['selectedContent']) }) - -it('validates ASCII command names while allowing localized descriptions and system names inside plugin namespaces', () => { - const base = manifest({ apiVersion: 3, contributes: { commands: [{ id: 'tests.reader.run', title: 'Run', slash: { name: 'read-file' } }] } }) - const command = base.contributes.commands[0]! - for (const name of ['/read', 'Read', '读取', 'café', 'read file', 'read:1', '-read', 'read\u202E', '', 'a'.repeat(65)]) { - expect(extensionManifestSchema.safeParse({ ...base, contributes: { commands: [{ ...command, slash: { name } }] } }).success, name).toBe(false) - } - for (const name of ['read-file', 'compact', 'review', 'start-2']) - expect(extensionManifestSchema.safeParse({ ...base, contributes: { commands: [{ ...command, slash: { name } }] } }).success, name).toBe(true) - expect(extensionManifestSchema.safeParse({ ...base, apiVersion: 2 }).success).toBe(false) - expect(extensionManifestSchema.safeParse({ ...base, contributes: { commands: [{ ...command, hidden: true }] } }).success).toBe(false) - expect(extensionManifestSchema.safeParse({ ...base, contributes: { commands: [command, { ...command, id: 'tests.reader.other' }] } }).success).toBe(false) -}) - -it('restricts transient interactions to absolute API 3 workbench and pane mounts', () => { - const base = manifest({ apiVersion: 3, contributes: { views: [{ id: 'tests.reader.game', title: 'Game', entry: 'view.js', resource: 'none' }], placements: [{ id: 'tests.reader.layer', view: 'tests.reader.game', kind: 'view', target: 'workbench', interaction: 'regions', presentation: { position: 'absolute' } }] } }) - expect(extensionManifestSchema.safeParse({ ...base, apiVersion: 2 }).success).toBe(false) - for (const target of ['app.sidebar', 'workbench.sidebar']) - expect(extensionManifestSchema.safeParse({ ...base, contributes: { ...base.contributes, placements: [{ ...base.contributes.placements[0], target }] } }).success).toBe(false) - expect(extensionManifestSchema.safeParse({ ...base, contributes: { ...base.contributes, placements: [{ ...base.contributes.placements[0], presentation: { position: 'static' } }] } }).success).toBe(false) -}) diff --git a/apps/buddy/platform/extensions/__tests__/ExtensionTaskCleanup.spec.ts b/apps/buddy/platform/extensions/__tests__/ExtensionTaskCleanup.spec.ts index 61880e3e2..0345821bc 100644 --- a/apps/buddy/platform/extensions/__tests__/ExtensionTaskCleanup.spec.ts +++ b/apps/buddy/platform/extensions/__tests__/ExtensionTaskCleanup.spec.ts @@ -2,7 +2,7 @@ import type { JsonValue } from '../../../shared/workbench/workbenchState' import { randomUUID } from 'node:crypto' import { rm } from 'node:fs/promises' import { afterEach, expect, it } from 'vitest' -import { addedExtensionPermissions, extensionManifestSchema, extensionPermissionsSchema } from '../../../shared/extensions/extensionManifest' +import { addedExtensionPermissions, extensionPermissionsSchema } from '../../../shared/extensions/extensionManifest' import { ExtensionService } from '../ExtensionService' import { createStore, manifest, reviewPackage } from './fixtures' @@ -16,7 +16,7 @@ const item = { ...target, title: 'Legacy task', removedAt: '2026-01-01T00:00:00Z async function fixture(permission = true) { const { root, store } = await createStore() cleanups.push(() => rm(root, { recursive: true, force: true })) - const definition = manifest({ apiVersion: 4, permissions: { legacyTaskCleanup: permission }, contributes: { views: [{ id: 'tests.reader.page', title: 'Cleanup', entry: 'view.js', location: 'page', resource: 'none' }] } }) + const definition = manifest({ apiVersion: 5, permissions: { legacyTaskCleanup: permission }, contributes: { views: [{ id: 'tests.reader.page', title: 'Cleanup', entry: 'view.js', location: 'page', resource: 'none' }] } }) await store.install((await reviewPackage(root, store, definition)).token) const records = [item] let broker!: (method: string, params: unknown) => Promise @@ -44,13 +44,6 @@ async function fixture(permission = true) { return { service, records, view, broker: (method: string, params: unknown) => broker(method, params), request: (method: string, params: unknown) => service.viewRequest(view.id, view.generation, view.token, method, params) } } -it('requires API 4 and an interactive view for legacy cleanup permission', () => { - const definition = manifest({ apiVersion: 4, permissions: { legacyTaskCleanup: true }, contributes: { views: [{ id: 'tests.reader.page', title: 'Cleanup', entry: 'view.js', location: 'page', resource: 'none' }] } }) - expect(extensionManifestSchema.safeParse(definition).success).toBe(true) - expect(extensionManifestSchema.safeParse({ ...definition, apiVersion: 3 }).success).toBe(false) - expect(extensionManifestSchema.safeParse({ ...definition, contributes: {} }).success).toBe(false) -}) - it('requires an explicitly reviewed permission and denies background access', async () => { expect(extensionPermissionsSchema.parse({}).legacyTaskCleanup).toBe(false) expect(addedExtensionPermissions(extensionPermissionsSchema.parse({}), extensionPermissionsSchema.parse({ legacyTaskCleanup: true }))).toContain('legacyTaskCleanup') @@ -59,19 +52,14 @@ it('requires an explicitly reviewed permission and denies background access', as await expect(denied.request('taskCleanup.delete', target)).rejects.toThrow('EXTENSION_METHOD_DENIED') expect(denied.records).toEqual([item]) const allowed = await fixture() + await expect(allowed.request('taskCleanup.list', { limit: 50 })).rejects.toThrow('EXTENSION_METHOD_DENIED') + await expect(allowed.request('taskCleanup.delete', target)).rejects.toThrow('EXTENSION_METHOD_DENIED') await expect(allowed.request('taskCleanup.run', { token: randomUUID() })).rejects.toThrow('EXTENSION_METHOD_DENIED') await expect(allowed.broker('taskCleanup.run', { token: randomUUID() })).rejects.toThrow('EXTENSION_METHOD_DENIED') await expect(allowed.broker('taskCleanup.delete', target)).rejects.toThrow('EXTENSION_METHOD_DENIED') expect(allowed.records).toEqual([item]) }) -it('denies legacy view list and delete even with permission and a valid revision', async () => { - const f = await fixture() - await expect(f.request('taskCleanup.list', { limit: 50 })).rejects.toThrow('EXTENSION_METHOD_DENIED') - await expect(f.request('taskCleanup.delete', target)).rejects.toThrow('EXTENSION_METHOD_DENIED') - expect(f.records).toEqual([item]) -}) - it('revokes the old session after uninstall', async () => { const f = await fixture() await f.service.uninstall(f.view.extensionId) diff --git a/apps/buddy/platform/extensions/__tests__/createExtensionTaskCleanup.spec.ts b/apps/buddy/platform/extensions/__tests__/createExtensionTaskCleanup.spec.ts index a6dc0b5d1..811a75f82 100644 --- a/apps/buddy/platform/extensions/__tests__/createExtensionTaskCleanup.spec.ts +++ b/apps/buddy/platform/extensions/__tests__/createExtensionTaskCleanup.spec.ts @@ -38,12 +38,3 @@ it('preserves the exact target and revision without letting the plugin expand th await expect(adapter.delete({ conversationId: 'removed', expectedRevision: 4 })).resolves.toEqual({ status: 'deleted' }) expect([...records.keys()]).toEqual(['active']) }) - -it('keeps accepted cleanup separate from completion and preserves failure diagnostics', async () => { - const adapter = createExtensionTaskCleanup({ - list: async () => ({ items: [], nextCursor: null }), - delete: async input => input.conversationId === 'pending' ? { status: 'pending' } : { status: 'failed', errorCode: 'CONVERSATION_FILES_REFERENCED' }, - }) - await expect(adapter.delete({ conversationId: 'pending', expectedRevision: 4 })).resolves.toEqual({ status: 'pending' }) - await expect(adapter.delete({ conversationId: 'failed', expectedRevision: 4 })).resolves.toEqual({ status: 'failed', errorCode: 'CONVERSATION_FILES_REFERENCED' }) -}) diff --git a/apps/buddy/platform/ipc/__tests__/localTransport.spec.ts b/apps/buddy/platform/ipc/__tests__/localTransport.spec.ts index a3a27ad87..ab27a19f7 100644 --- a/apps/buddy/platform/ipc/__tests__/localTransport.spec.ts +++ b/apps/buddy/platform/ipc/__tests__/localTransport.spec.ts @@ -1,4 +1,5 @@ import { chmod, lstat, mkdir, mkdtemp, rm, symlink } from 'node:fs/promises' +import { createServer } from 'node:net' import { tmpdir } from 'node:os' import { join } from 'node:path' import process from 'node:process' @@ -24,3 +25,21 @@ describe.skipIf(process.platform === 'win32')('unix socket directory ownership', } }) }) + +it.skipIf(process.platform === 'win32')('never unlinks an active socket or a socket it did not bind', async () => { + const root = await mkdtemp(join(tmpdir(), 'lexora-owned-socket-')) + const address = join(root, 'active.sock') + const server = createServer(socket => socket.destroy()) + try { + await new Promise(resolve => server.listen(address, resolve)) + const original = await lstat(address) + const contender = localTransports.unix(address, { ownsProfileLease: () => true }) + await expect(contender.prepare()).rejects.toThrow('already owned') + await contender.dispose() + expect((await lstat(address)).ino).toBe(original.ino) + } + finally { + await new Promise(resolve => server.close(() => resolve())) + await rm(root, { recursive: true, force: true }) + } +}) diff --git a/apps/buddy/platform/ipc/localTransport.ts b/apps/buddy/platform/ipc/localTransport.ts index 7ea20bdb9..0cc8f77e8 100644 --- a/apps/buddy/platform/ipc/localTransport.ts +++ b/apps/buddy/platform/ipc/localTransport.ts @@ -1,4 +1,5 @@ import { chmod, lstat, mkdir, unlink } from 'node:fs/promises' +import { createConnection } from 'node:net' import { dirname, isAbsolute } from 'node:path' import process from 'node:process' import { isLocalNamedPipe } from '../../shared/platform/localEndpoint' @@ -15,13 +16,18 @@ export const localTransports = { namedPipe: createNamedPipeEndpoint, } -function createNamedPipeEndpoint(address: string): LocalEndpoint { +interface LocalEndpointOptions { + ownsProfileLease?: () => boolean +} + +function createNamedPipeEndpoint(address: string, _options?: LocalEndpointOptions): LocalEndpoint { if (!isLocalNamedPipe(address)) throw new Error('Expected a local named pipe endpoint') return { address, prepare: async () => {}, secure: async () => {}, dispose: async () => {} } } -function createUnixEndpoint(address: string): LocalEndpoint { +function createUnixEndpoint(address: string, options: LocalEndpointOptions = {}): LocalEndpoint { + let owned: { dev: number, ino: number } | undefined if (!isAbsolute(address)) throw new Error('Expected an absolute Unix socket endpoint') return { @@ -37,12 +43,25 @@ function createUnixEndpoint(address: string): LocalEndpoint { return if (!metadata.isSocket()) throw new Error('Local socket path is occupied by a non-socket file') + if (!options.ownsProfileLease?.() || metadata.uid !== process.getuid?.() || await socketAcceptsConnections(address)) + throw new Error('Local socket endpoint is already owned') + const current = await socketMetadata(address) + if (current?.dev !== metadata.dev || current.ino !== metadata.ino) + throw new Error('Local socket endpoint changed') await unlink(address) }, - secure: () => chmod(address, 0o600), + async secure() { + const metadata = await socketMetadata(address) + if (!metadata?.isSocket() || metadata.uid !== process.getuid?.()) + throw new Error('Local socket endpoint is not owned') + owned = { dev: metadata.dev, ino: metadata.ino } + await chmod(address, 0o600) + }, async dispose() { - if ((await socketMetadata(address))?.isSocket()) + const metadata = await socketMetadata(address) + if (owned && metadata?.isSocket() && metadata.dev === owned.dev && metadata.ino === owned.ino) await unlink(address) + owned = undefined }, } } @@ -57,3 +76,18 @@ async function socketMetadata(address: string) { throw error } } + +async function socketAcceptsConnections(address: string): Promise { + return new Promise((resolve) => { + const socket = createConnection(address) + let timeout: ReturnType + const finish = (active: boolean) => { + clearTimeout(timeout) + socket.destroy() + resolve(active) + } + timeout = setTimeout(finish, 500, true) + socket.once('connect', () => finish(true)) + socket.once('error', error => finish((error as NodeJS.ErrnoException).code !== 'ECONNREFUSED')) + }) +} diff --git a/apps/buddy/platform/native/nodeRuntime.json b/apps/buddy/platform/native/nodeRuntime.json new file mode 100644 index 000000000..17bf1cfa7 --- /dev/null +++ b/apps/buddy/platform/native/nodeRuntime.json @@ -0,0 +1,45 @@ +{ + "version": "26.2.0", + "release": "https://nodejs.org/dist/v26.2.0", + "resource": { + "from": ".output/resources/node-runtime", + "to": "lexora-runtime" + }, + "targets": { + "linux-x64": { + "archive": "node-v26.2.0-linux-x64.tar.gz", + "sha256": "8e5b4b969bb2414aca2ff84d2b99608511aedca32bf8a8427e6187dbdd703bbe", + "entry": "node-v26.2.0-linux-x64/bin/node", + "license": "node-v26.2.0-linux-x64/LICENSE", + "binary": "node" + }, + "linux-arm64": { + "archive": "node-v26.2.0-linux-arm64.tar.gz", + "sha256": "bf52461d25017479cdc549642c4e7a2303ac5c5395d7c3d026d5d8b3fa283c05", + "entry": "node-v26.2.0-linux-arm64/bin/node", + "license": "node-v26.2.0-linux-arm64/LICENSE", + "binary": "node" + }, + "win32-x64": { + "archive": "node-v26.2.0-win-x64.zip", + "sha256": "371071a4f7e2c8a5dd280730049c685911feecc59f50ebc488d675dc1087c69c", + "entry": "node-v26.2.0-win-x64/node.exe", + "license": "node-v26.2.0-win-x64/LICENSE", + "binary": "node.exe" + }, + "win32-arm64": { + "archive": "node-v26.2.0-win-arm64.zip", + "sha256": "131efa8bd858f8ad000a1a0436d8bb1320c638291c779a921fb20e5702a1cc0a", + "entry": "node-v26.2.0-win-arm64/node.exe", + "license": "node-v26.2.0-win-arm64/LICENSE", + "binary": "node.exe" + }, + "darwin-arm64": { + "archive": "node-v26.2.0-darwin-arm64.tar.gz", + "sha256": "2d8575b6a749c0bc66cc6ecd22a952f7d9bbf3b0ea76fcb0ed3ce858018afff7", + "entry": "node-v26.2.0-darwin-arm64/bin/node", + "license": "node-v26.2.0-darwin-arm64/LICENSE", + "binary": "node" + } + } +} diff --git a/apps/buddy/platform/process/serviceEnvironment.ts b/apps/buddy/platform/process/serviceEnvironment.ts new file mode 100644 index 000000000..8cca01fb8 --- /dev/null +++ b/apps/buddy/platform/process/serviceEnvironment.ts @@ -0,0 +1,65 @@ +import type { StorageProtection } from '../storage/ProfileLocations' +import { userInfo } from 'node:os' +import process from 'node:process' +import { resolveBuddyPlatform } from '../../shared/platform' +import { isWindows } from '../../shared/platform/identifiers' +import { filePathAdapters } from '../filesystem/filePaths' +import searchTools from '../native/searchTools.json' +import { createChildProcessEnvironment } from '../process/childProcessEnvironment' +import { createProxyEnvironment } from '../process/proxyEnvironment' +import { createToolchainEnvironment } from '../process/toolchainEnvironment' +import { resolveBuddyTarget } from '../target' + +export function resolveBuddySearchToolsDirectory(options: { + appPath: string + isPackaged: boolean + resourcesPath: string + platform?: NodeJS.Platform + architecture?: string +}): string { + const platform = resolveBuddyPlatform(options.platform ?? process.platform) + const { id: target } = resolveBuddyTarget(platform.id, options.architecture ?? process.arch) + if (!Object.values(searchTools.tools).every(tool => Object.hasOwn(tool.targets, target))) + throw new Error(`Unsupported search tools target: ${target}`) + return filePathAdapters[platform.id].resolveInput( + options.isPackaged ? searchTools.resource.to : `${searchTools.resource.from}/${target}`, + options.isPackaged ? options.resourcesPath : options.appPath, + ) +} + +export function createBuddyServiceEnvironment( + source: NodeJS.ProcessEnv, + buddyHome: string, + platform: NodeJS.Platform = process.platform, + proxyUrl?: string, + storageProtection?: StorageProtection, +): NodeJS.ProcessEnv { + const targetPlatform = resolveBuddyPlatform(platform) + const environmentSource = !isWindows(targetPlatform.id) && !source.HOME + ? { ...source, HOME: userInfo().homedir } + : source + const recoveryMode = Object.entries(environmentSource).find(([name]) => ( + (targetPlatform.environment.caseSensitive ? name : name.toUpperCase()) === 'LEXORA_BUDDY_EVENT_RECOVERY' + ))?.[1] + const environment = createChildProcessEnvironment({ + source: environmentSource, + platform: targetPlatform, + additions: { + ...createToolchainEnvironment(environmentSource, targetPlatform.id), + LEXORA_BUDDY_HOME: buddyHome, + LEXORA_BUDDY_STORAGE_PROTECTION: storageProtection ? JSON.stringify(storageProtection.sensitiveRoots) : undefined, + LEXORA_BUDDY_EVENT_RECOVERY: recoveryMode === 'full' ? 'full' : undefined, + LEXORA_BUDDY_STORAGE_AUTHORITY: environmentSource.LEXORA_BUDDY_STORAGE_AUTHORITY === 'sqlite' ? 'sqlite' : undefined, + NODE_USE_ENV_PROXY: '1', + PI_CODING_AGENT_DIR: filePathAdapters[targetPlatform.id].resolveInput('agent', buddyHome), + }, + }) + if (proxyUrl) { + for (const key of Object.keys(environment)) { + if (/^(?:https?|all|no)_proxy$/i.test(key)) + delete environment[key] + } + Object.assign(environment, createProxyEnvironment(proxyUrl)) + } + return environment +} diff --git a/apps/buddy/platform/service/HostConnection.ts b/apps/buddy/platform/service/HostConnection.ts new file mode 100644 index 000000000..23a8d283c --- /dev/null +++ b/apps/buddy/platform/service/HostConnection.ts @@ -0,0 +1,89 @@ +import type { RuntimeRequestHandler, RuntimeRpcPeerContract } from '../../shared/runtime/rpcPeer' +import { Emitter } from '../../shared/events/Emitter' + +export class HostUnavailableError extends Error { + readonly code = 'PLATFORM_HOST_UNAVAILABLE' + constructor() { + super('PLATFORM_HOST_UNAVAILABLE') + this.name = 'HostUnavailableError' + } +} + +export class HostConnection implements RuntimeRpcPeerContract { + readonly #notifications = new Emitter<{ method: string, params: unknown }>(() => {}) + readonly #handlers = new Map() + #peer: RuntimeRpcPeerContract | null = null + #instanceId: string | null = null + #notificationBinding: (() => void) | undefined + readonly #requestBindings = new Map void>() + #closed = false + + get instanceId(): string | null { return this.#instanceId } + get available(): boolean { return this.#peer !== null && !this.#closed } + + attach(instanceId: string, peer: RuntimeRpcPeerContract): void { + if (this.#closed || this.#peer) + throw new Error('PLATFORM_HOST_ALREADY_ATTACHED') + this.#peer = peer + this.#instanceId = instanceId + this.#notificationBinding = peer.onNotification((method, params) => { + if (this.#peer === peer) + this.#notifications.fire({ method, params }) + }) + for (const [method, handler] of this.#handlers) + this.#requestBindings.set(method, peer.onRequest(method, handler)) + } + + detach(instanceId: string): void { + if (this.#instanceId !== instanceId) + return + const peer = this.#peer + this.#peer = null + this.#instanceId = null + this.#notificationBinding?.() + this.#notificationBinding = undefined + for (const stop of this.#requestBindings.values()) stop() + this.#requestBindings.clear() + peer?.close(new HostUnavailableError()) + } + + request(method: string, params: unknown, timeoutMs?: number | null, signal?: AbortSignal, requestId?: string): Promise { + const peer = this.#peer + if (!peer || this.#closed) + return Promise.reject(new HostUnavailableError()) + return peer.request(method, params, timeoutMs, signal, requestId) + } + + notify(method: string, params: unknown): void { + if (!this.#peer || this.#closed) + throw new HostUnavailableError() + this.#peer.notify(method, params) + } + + onNotification(listener: (method: string, params: unknown) => void): () => void { + return this.#notifications.event(event => listener(event.method, event.params)).dispose + } + + onRequest(method: string, handler: RuntimeRequestHandler): () => void { + if (this.#handlers.has(method)) + throw new Error('PLATFORM_HOST_DUPLICATE_HANDLER') + this.#handlers.set(method, handler) + const dispose = this.#peer?.onRequest(method, handler) + if (dispose) + this.#requestBindings.set(method, dispose) + return () => { + if (this.#handlers.get(method) === handler) + this.#handlers.delete(method) + this.#requestBindings.get(method)?.() + this.#requestBindings.delete(method) + } + } + + close(_reason: Error): void { + this.#closed = true + if (this.#instanceId) + this.detach(this.#instanceId) + this.#notifications.dispose() + this.#handlers.clear() + } +} diff --git a/apps/buddy/platform/service/LocalServiceServer.ts b/apps/buddy/platform/service/LocalServiceServer.ts new file mode 100644 index 000000000..55a877756 --- /dev/null +++ b/apps/buddy/platform/service/LocalServiceServer.ts @@ -0,0 +1,98 @@ +import type { Server } from 'node:net' +import type { ServiceHello, ServiceIdentity } from '../../shared/service/localServiceProtocol' +import type { LocalEndpoint } from '../ipc/localTransport' +import { Buffer } from 'node:buffer' +import { randomUUID, timingSafeEqual } from 'node:crypto' +import { createServer } from 'node:net' +import { serviceHelloSchema, serviceIdentitySchema } from '../../shared/service/localServiceProtocol' +import { SocketFrames } from './SocketFrames' + +interface LocalServiceServerOptions { + endpoint: LocalEndpoint + identity: ServiceIdentity + role: ServiceHello['role'] + secret: string + singleUse?: boolean + connected: (frames: SocketFrames, connectionId: string) => void +} + +export class LocalServiceServer { + readonly #options: LocalServiceServerOptions + readonly #connections = new Set() + #server: Server | undefined + #consumed = false + #stopping = false + + constructor(options: LocalServiceServerOptions) { + this.#options = { ...options, identity: serviceIdentitySchema.strip().parse(options.identity) } + } + + async listen(): Promise { + if (this.#server || this.#stopping) + throw new Error('SERVICE_LISTENER_STATE_INVALID') + await this.#options.endpoint.prepare() + const server = createServer((socket) => { + if (this.#stopping || this.#connections.size >= 64 || (this.#options.singleUse && this.#consumed)) { + socket.destroy() + return + } + const frames = new SocketFrames(socket) + this.#connections.add(frames) + const timeout = setTimeout(() => frames.close(new Error('SERVICE_HANDSHAKE_TIMEOUT')), 5000) + const stop = frames.onFrame((frame) => { + const parsed = serviceHelloSchema.safeParse(JSON.parse(frame)) + const expected = this.#options + if (!parsed.success || parsed.data.role !== expected.role || (expected.singleUse && this.#consumed) + || Object.entries(expected.identity).some(([key, value]) => parsed.data[key as keyof ServiceIdentity] !== value) + || !timingSafeEqual(Buffer.from(parsed.data.secret, 'hex'), Buffer.from(expected.secret, 'hex'))) { + frames.close(new Error('SERVICE_HANDSHAKE_REJECTED')) + return + } + this.#consumed = true + clearTimeout(timeout) + stop.dispose() + const connectionId = randomUUID() + if (!frames.send(JSON.stringify({ ...expected.identity, type: 'welcome', connectionId }))) + return + try { + expected.connected(frames, connectionId) + } + catch { frames.close(new Error('SERVICE_CONNECTION_FAILED')) } + }) + frames.onDidClose(() => { + clearTimeout(timeout) + stop.dispose() + this.#connections.delete(frames) + }) + }) + server.on('error', () => { + for (const frames of this.#connections) frames.close(new Error('SERVICE_LISTENER_FAILED')) + }) + this.#server = server + try { + await new Promise((resolve, reject) => { + const failed = (error: Error) => reject(error) + server.once('error', failed) + server.listen(this.#options.endpoint.address, () => { + server.off('error', failed) + resolve() + }) + }) + await this.#options.endpoint.secure() + } + catch (error) { + await this.dispose() + throw error + } + } + + async dispose(): Promise { + if (this.#stopping) + return + this.#stopping = true + for (const frames of this.#connections) frames.close(new Error('SERVICE_STOPPED')) + if (this.#server?.listening) + await new Promise((resolve, reject) => this.#server!.close(error => error ? reject(error) : resolve())) + await this.#options.endpoint.dispose() + } +} diff --git a/apps/buddy/platform/service/ServiceChildProcess.ts b/apps/buddy/platform/service/ServiceChildProcess.ts new file mode 100644 index 000000000..7b52680ef --- /dev/null +++ b/apps/buddy/platform/service/ServiceChildProcess.ts @@ -0,0 +1,182 @@ +import type { ChildProcess } from 'node:child_process' +import type { Readable, Writable } from 'node:stream' +import type { z } from 'zod' +import type { serviceProcessIdentitySchema } from '../../shared/service/localServiceProtocol' +import { spawn } from 'node:child_process' +import { readdir, readFile } from 'node:fs/promises' +import process from 'node:process' +import { setTimeout as delay } from 'node:timers/promises' +import { readServiceProcessIdentity } from './serviceProcessIdentity' + +type Identity = z.infer +export interface ServiceProcessReceipt { + identity: Identity + processGroup: number | null +} +export class ServiceChildProcess { + readonly child: ChildProcess + readonly exited: Promise<{ code: number | null, signal: NodeJS.Signals | null }> + readonly #helper?: string + #receipt: ServiceProcessReceipt | undefined + #exit: { code: number | null, signal: NodeJS.Signals | null } | undefined + #stopping: Promise | undefined + readonly #runtimeGuard?: string + #guardClean: Promise | undefined + + constructor(options: { executable: string, args: string[], env: NodeJS.ProcessEnv, processControl?: string, runtimeGuard?: string, output?: (stream: 'stdout' | 'stderr', value: string) => void }) { + this.#helper = options.processControl + this.#runtimeGuard = options.runtimeGuard + this.child = spawn(options.executable, options.args, { env: options.env, detached: process.platform !== 'win32', stdio: ['ignore', 'pipe', 'pipe', 'pipe'], windowsHide: true }) + this.exited = new Promise((resolve, reject) => { + this.child.once('error', reject) + this.child.once('exit', (code, signal) => { + this.#exit = { code, signal } + + resolve(this.#exit) + }) + }) + void this.exited.catch(() => {}) + this.child.stdout?.on('data', value => options.output?.('stdout', String(value))) + this.child.stderr?.on('data', value => options.output?.('stderr', String(value))) + } + + async ready(bootstrap: unknown): Promise { + if (!this.child.pid) { + await new Promise((resolve, reject) => { + this.child.once('spawn', resolve) + + this.child.once('error', reject) + }) + } + const identity = await readServiceProcessIdentity(this.child.pid!, this.#helper) + this.#receipt = { identity, processGroup: process.platform === 'win32' ? null : identity.pid } + if (process.platform === 'win32') + await this.#guard(identity.pid) + const channel = this.child.stdio[3] as Readable & Writable + channel.on('error', () => {}) + channel.end(JSON.stringify(bootstrap)) + return this.#receipt + } + + stopAndVerify(): Promise { + return this.#stopping ??= this.#stopAndVerify() + } + + async #stopAndVerify(): Promise { + const receipt = this.#receipt + if (!receipt) + throw new Error('SERVICE_CHILD_IDENTITY_UNAVAILABLE') + if (receipt.processGroup !== null) { + if (!this.#exit) { + const current = await readServiceProcessIdentity(receipt.identity.pid, this.#helper) + if (current.startIdentity !== receipt.identity.startIdentity || current.executable !== receipt.identity.executable) + throw new Error('SERVICE_CHILD_IDENTITY_CHANGED') + } + for (const signal of ['SIGTERM', 'SIGKILL'] as const) { + if (!(await this.#groupAlive(receipt.processGroup))) + return + try { + process.kill(-receipt.processGroup, signal) + } + catch (error) { + if ((error as NodeJS.ErrnoException).code !== 'ESRCH') + throw error + } + const deadline = Date.now() + 3000 + while (Date.now() < deadline) { + if (!(await this.#groupAlive(receipt.processGroup))) + return + await delay(25) + } + } + throw new Error('SERVICE_CHILD_CLEANUP_UNCONFIRMED') + } + if (!this.#exit) { + const current = await readServiceProcessIdentity(receipt.identity.pid, this.#helper) + if (current.startIdentity !== receipt.identity.startIdentity || current.executable !== receipt.identity.executable) + throw new Error('SERVICE_CHILD_IDENTITY_CHANGED') + this.child.kill() + } + await Promise.race([this.exited, delay(5000, undefined, { ref: false }).then(() => { + throw new Error('SERVICE_CHILD_CLEANUP_UNCONFIRMED') + })]) + if (!this.#guardClean) + throw new Error('SERVICE_CHILD_CLEANUP_UNCONFIRMED') + await this.#guardClean + } + + async #guard(pid: number): Promise { + if (!this.#runtimeGuard) + throw new Error('SERVICE_RUNTIME_GUARD_MISSING') + const guard = spawn(this.#runtimeGuard, [], { env: { SystemRoot: process.env.SystemRoot }, stdio: ['pipe', 'pipe', 'ignore'], windowsHide: true }) + let accept!: () => void + let reject!: (reason: Error) => void + const ready = new Promise((resolve, fail) => { + accept = resolve + + reject = fail + }) + let output = '' + const timeout = setTimeout(() => reject(new Error('SERVICE_RUNTIME_GUARD_TIMEOUT')), 10000) + this.#guardClean = new Promise((resolve, fail) => { + guard.once('error', (error) => { + reject(error) + + fail(error) + }) + guard.stdout.on('data', (value) => { + output += String(value) + if (output === 'ready\n' || output === 'ready\nclean\n') { + clearTimeout(timeout) + accept() + } + else if (!'ready\nclean\n'.startsWith(output)) { + const error = new Error('SERVICE_RUNTIME_GUARD_INVALID') + reject(error) + fail(error) + } + }) + guard.once('exit', (code) => { + clearTimeout(timeout) + if (code === 0 && output === 'ready\nclean\n') { + resolve() + } + else { + const error = new Error('SERVICE_CHILD_CLEANUP_UNCONFIRMED') + reject(error) + fail(error) + } + }) + }) + void this.#guardClean.catch(() => {}) + guard.stdin.on('error', error => reject(error)) + guard.stdin.end(JSON.stringify({ pid, observeCleanup: true })) + await ready + } + + async #groupAlive(group: number): Promise { + if (process.platform === 'linux') { + for (const entry of await readdir('/proc')) { + if (!/^\d+$/.test(entry)) + continue + const stat = await readFile(`/proc/${entry}/stat`, 'utf8').catch(error => ['ENOENT', 'ESRCH'].includes((error as NodeJS.ErrnoException).code ?? '') ? null : Promise.reject(error)) + if (!stat) + continue + const fields = stat.slice(stat.lastIndexOf(')') + 2).split(' ') + if (Number(fields[2]) === group && !['Z', 'X'].includes(fields[0]!)) + return true + } + return false + } + try { + process.kill(-group, 0) + + return true + } + catch (error) { + if ((error as NodeJS.ErrnoException).code === 'ESRCH') + return false + throw error + } + } +} diff --git a/apps/buddy/platform/service/ServiceProfileOwner.ts b/apps/buddy/platform/service/ServiceProfileOwner.ts new file mode 100644 index 000000000..aad226c31 --- /dev/null +++ b/apps/buddy/platform/service/ServiceProfileOwner.ts @@ -0,0 +1,97 @@ +import type { ServiceDiscovery, ServiceLaunch } from '../../shared/service/localServiceProtocol' +import { createHash, randomBytes, randomUUID } from 'node:crypto' +import { lstat, mkdir, readFile, realpath, unlink } from 'node:fs/promises' +import { homedir } from 'node:os' +import { dirname, join } from 'node:path' +import process from 'node:process' +import { serviceDiscoverySchema } from '../../shared/service/localServiceProtocol' +import { ensurePrivateDirectories } from '../filesystem/privateDirectories' +import { readOrCreateProductIdentity } from '../storage/productIdentity' +import { acquireStorageRootLease } from '../storage/StorageRootLease' +import { writePrivateServiceJson } from './serviceFiles' +import { readServiceProcessIdentity } from './serviceProcessIdentity' + +export class ServiceProfileOwner { + readonly discovery: ServiceDiscovery + readonly managementSecret = randomBytes(32).toString('hex') + readonly productSecret = randomBytes(32).toString('hex') + readonly #release: () => void + readonly #discoveryPath: string + readonly #credentialsPath: string + #disposed = false + + private constructor(discovery: ServiceDiscovery, directory: string, release: () => void) { + this.discovery = discovery + this.#discoveryPath = join(directory, 'service.json') + this.#credentialsPath = join(directory, 'local-client.json') + this.#release = release + } + + static async acquire(config: ServiceLaunch, processControl?: string): Promise { + await ensurePrivateDirectories([config.profileRoot], config.privateDirectoriesExecutable) + const root = await realpath(config.profileRoot) + const release = acquireStorageRootLease(root, config.leaseDirectory ?? join(homedir(), '.lexora-locations', 'locks'), config.privateDirectoriesExecutable) + try { + const directory = join(root, '.runtime') + await mkdir(directory, { recursive: true, mode: 0o700 }) + const metadata = await lstat(directory) + if (!metadata.isDirectory() || metadata.isSymbolicLink() || (process.platform !== 'win32' && (metadata.uid !== process.getuid?.() || (metadata.mode & 0o077) !== 0))) + throw new Error('SERVICE_PRIVATE_DIRECTORY_INVALID') + await ensurePrivateDirectories([directory], config.privateDirectoriesExecutable) + const sockets = process.platform === 'win32' ? directory : join('/tmp', `lexora-services-${process.getuid?.()}`, createHash('sha256').update(root).digest('hex').slice(0, 24)) + if (process.platform !== 'win32') { + await ensurePrivateDirectories([dirname(sockets), sockets], config.privateDirectoriesExecutable) + for (const path of [dirname(sockets), sockets]) { + const metadata = await lstat(path) + if (!metadata.isDirectory() || metadata.isSymbolicLink() || metadata.uid !== process.getuid?.() || (metadata.mode & 0o077) !== 0) + throw new Error('SERVICE_PRIVATE_DIRECTORY_INVALID') + } + } + const product = await readOrCreateProductIdentity(root) + const serviceInstanceId = randomUUID() + const address = (role: string) => process.platform === 'win32' + ? `\\\\.\\pipe\\lexora-${serviceInstanceId}-${role}` + : join(sockets, `${serviceInstanceId.slice(0, 8)}-${role}.sock`) + const owner = new ServiceProfileOwner(serviceDiscoverySchema.parse({ + protocol: 1, + profileId: product.profileId, + serviceInstanceId, + buildId: config.buildId, + sessionId: config.sessionId, + userId: config.userId, + process: await readServiceProcessIdentity(process.pid, processControl), + managementEndpoint: address('management'), + productEndpoint: address('product'), + phase: 'starting', + }), directory, release) + await owner.publish('starting') + await writePrivateServiceJson(owner.#credentialsPath, { version: 1, serviceInstanceId, managementSecret: owner.managementSecret, productSecret: owner.productSecret }) + return owner + } + catch (error) { + release() + throw error + } + } + + async publish(phase: ServiceDiscovery['phase']): Promise { + if (this.#disposed) + throw new Error('SERVICE_OWNER_STOPPED') + this.discovery.phase = phase + await writePrivateServiceJson(this.#discoveryPath, this.discovery) + } + + async dispose(): Promise { + if (this.#disposed) + return + this.#disposed = true + try { + for (const path of [this.#discoveryPath, this.#credentialsPath]) { + const current = await readFile(path, 'utf8').catch(error => (error as NodeJS.ErrnoException).code === 'ENOENT' ? null : Promise.reject(error)) + if (current !== null && JSON.parse(current).serviceInstanceId === this.discovery.serviceInstanceId) + await unlink(path) + } + } + finally { this.#release() } + } +} diff --git a/apps/buddy/platform/service/ServiceRpcChannel.ts b/apps/buddy/platform/service/ServiceRpcChannel.ts new file mode 100644 index 000000000..01011255f --- /dev/null +++ b/apps/buddy/platform/service/ServiceRpcChannel.ts @@ -0,0 +1,28 @@ +import type { SocketFrames } from './SocketFrames' +import { Emitter } from '../../shared/events/Emitter' +import { RuntimeRpcPeer } from '../ipc/runtimeRpcPeer' + +export class ServiceRpcChannel extends RuntimeRpcPeer { + readonly #frames: SocketFrames + readonly #closed = new Emitter(() => {}) + readonly onDidClose = this.#closed.event + #ended = false + + constructor(frames: SocketFrames) { + super({ transport: frames.transport(), onFatalError: error => frames.close(error) }) + this.#frames = frames + frames.onDidClose(error => this.close(error)) + } + + get closed(): boolean { return this.#ended } + + override close(reason: Error): void { + if (this.#ended) + return + this.#ended = true + super.close(reason) + this.#frames.close(reason) + this.#closed.fire() + this.#closed.dispose() + } +} diff --git a/apps/buddy/platform/service/SessionServiceRegistration.ts b/apps/buddy/platform/service/SessionServiceRegistration.ts new file mode 100644 index 000000000..d86a77921 --- /dev/null +++ b/apps/buddy/platform/service/SessionServiceRegistration.ts @@ -0,0 +1,121 @@ +import type { ServiceLaunch } from '../../shared/service/localServiceProtocol' +import { execFile } from 'node:child_process' +import { createHash } from 'node:crypto' +import { mkdir, readFile, writeFile } from 'node:fs/promises' +import { dirname, join } from 'node:path' +import process from 'node:process' +import { promisify } from 'node:util' +import { z } from 'zod' +import { createChildProcessEnvironment } from '../process/childProcessEnvironment' + +const execute = promisify(execFile) +export interface ServiceSession { userId: string, sessionId: string } +const unsafeEnvironment = ['NODE_OPTIONS', 'NODE_PATH', 'ELECTRON_RUN_AS_NODE', 'LD_PRELOAD', 'LD_LIBRARY_PATH', 'DYLD_INSERT_LIBRARIES', 'DYLD_LIBRARY_PATH', 'BASH_ENV', 'ENV'] +const displayEnvironment = ['DISPLAY', 'WAYLAND_DISPLAY', 'XAUTHORITY', 'DBUS_SESSION_BUS_ADDRESS', 'XDG_RUNTIME_DIR', 'XDG_SESSION_ID', 'XDG_SESSION_TYPE', 'XDG_CURRENT_DESKTOP', 'KDE_SESSION_VERSION'] + +async function run(command: string, args: string[]) { + return (await execute(command, args, { env: createChildProcessEnvironment({ source: process.env }), timeout: 15_000, maxBuffer: 1024 * 1024, windowsHide: true })).stdout.trim() +} + +export async function readServiceSession(): Promise { + if (process.platform === 'win32') { + const result = await run(windowsSystem('WindowsPowerShell/v1.0/powershell.exe'), ['-NoProfile', '-NonInteractive', '-Command', `[pscustomobject]@{userId=[Security.Principal.WindowsIdentity]::GetCurrent().User.Value;sessionId=[string][Diagnostics.Process]::GetProcessById(${process.pid}).SessionId}|ConvertTo-Json -Compress`]) + return z.object({ userId: z.string().regex(/^S-1-[\d-]+$/), sessionId: z.string().regex(/^\d+$/) }).strict().parse(JSON.parse(result)) + } + const userId = String(process.getuid?.()) + if (!/^\d+$/.test(userId) || userId === '0') + throw new Error('SERVICE_USER_SESSION_REQUIRED') + if (process.platform === 'linux') { + const session = process.env.XDG_SESSION_ID + if (!session || !/^[\w.-]{1,128}$/.test(session)) + throw new Error('SERVICE_USER_SESSION_REQUIRED') + const owner = await run('/usr/bin/loginctl', ['show-session', session, '--property=User', '--value']) + if (owner !== userId) + throw new Error('SERVICE_SESSION_OWNER_CHANGED') + const boot = (await readFile('/proc/sys/kernel/random/boot_id', 'utf8')).trim() + return { userId, sessionId: `${boot}:${session}` } + } + if (process.platform === 'darwin') { + await run('/bin/launchctl', ['print', `gui/${userId}`]) + const boot = await run('/usr/sbin/sysctl', ['-n', 'kern.bootsessionuuid']) + return { userId, sessionId: `${boot}:gui/${userId}` } + } + throw new Error('SERVICE_PLATFORM_UNSUPPORTED') +} + +export class SessionServiceRegistration { + readonly name: string + readonly #launch: ServiceLaunch + readonly #configPath: string + readonly #launcher: string + + constructor(launch: ServiceLaunch, configPath: string, launcher: string) { + this.#launch = launch + this.#configPath = configPath + this.#launcher = launcher + this.name = `lexora-core-${createHash('sha256').update(`${launch.defaultRoot ?? launch.profileRoot}\0${launch.sessionId}`).digest('hex').slice(0, 24)}` + } + + async start(): Promise { + const session = await readServiceSession() + if (session.userId !== this.#launch.userId || session.sessionId !== this.#launch.sessionId) + throw new Error('SERVICE_SESSION_CHANGED') + if (process.platform === 'linux') { + const unit = `${this.name}.service` + const state = await run('/usr/bin/systemctl', ['--user', 'show', unit, '--property=ActiveState', '--value']) + if (state === 'active' || state === 'activating') + return + const environment = displayEnvironment.flatMap(key => process.env[key] !== undefined ? [`--setenv=${key}=${process.env[key]}`] : []) + await run('/usr/bin/systemd-run', ['--user', '--collect', `--unit=${unit}`, '--property=Type=exec', '--property=PartOf=graphical-session.target', '--property=After=graphical-session.target', '--property=Restart=on-failure', '--property=RestartSec=2s', '--property=StartLimitBurst=3', '--property=StartLimitIntervalSec=60s', '--property=KillMode=mixed', '--property=TimeoutStopSec=30s', `--property=UnsetEnvironment=${unsafeEnvironment.join(' ')}`, ...environment, '--', this.#launcher, '--launch-service', this.#configPath]) + return + } + if (process.platform === 'win32') { + const task = `\\Lexora\\${this.name}` + const xmlPath = join(dirname(this.#configPath), `${this.name}.xml`) + const query = await run(windowsSystem('schtasks.exe'), ['/Query', '/TN', task, '/XML']).catch(() => null) + if (query && /false<\/Enabled>/i.test(query)) + throw new Error('SERVICE_SESSION_ENTRY_DISABLED') + if (!query) { + await writeFile(xmlPath, windowsServiceTask(this.#launch.userId, this.#launcher, this.#configPath), { mode: 0o600 }) + await run(windowsSystem('schtasks.exe'), ['/Create', '/TN', task, '/XML', xmlPath]) + } + await run(windowsSystem('schtasks.exe'), ['/Run', '/TN', task]) + return + } + if (process.platform === 'darwin') { + const domain = `gui/${this.#launch.userId}` + const label = `app.uselexora.${this.name}` + const path = join(dirname(this.#configPath), `${label}.plist`) + const disabled = await run('/bin/launchctl', ['print-disabled', domain]) + if (disabled.includes(`"${label}" => true`)) + throw new Error('SERVICE_SESSION_ENTRY_DISABLED') + const loaded = await run('/bin/launchctl', ['print', `${domain}/${label}`]).then(() => true, () => false) + if (!loaded) { + await mkdir(dirname(path), { recursive: true, mode: 0o700 }) + await writeFile(path, macosServiceAgent(label, this.#launcher, this.#configPath), { mode: 0o600 }) + await run('/bin/launchctl', ['bootstrap', domain, path]) + } + await run('/bin/launchctl', ['kickstart', `${domain}/${label}`]) + return + } + throw new Error('SERVICE_PLATFORM_UNSUPPORTED') + } +} + +function windowsSystem(path: string): string { + if (!process.env.SystemRoot) + throw new Error('SERVICE_SYSTEM_DIRECTORY_UNAVAILABLE') + return join(process.env.SystemRoot, 'System32', path) +} +function xml(value: string) { + return value.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"').replaceAll('\'', ''') +} +function quoteWindows(value: string) { + return `"${value.replace(/(\\*)"/g, '$1$1\\"').replace(/(\\+)$/, '$1$1')}"` +} +export function windowsServiceTask(userId: string, executable: string, config: string): string { + return `${xml(userId)}InteractiveTokenLeastPrivilegeIgnoreNewfalsefalsetruetruePT0SPT1M3${xml(executable)}--launch-service ${xml(quoteWindows(config))}` +} +export function macosServiceAgent(label: string, executable: string, config: string): string { + return `Label${xml(label)}ProgramArguments${xml(executable)}--launch-service${xml(config)}LimitLoadToSessionTypeAquaRunAtLoadKeepAliveSuccessfulExitThrottleInterval5ExitTimeOut30` +} diff --git a/apps/buddy/platform/service/SocketFrames.ts b/apps/buddy/platform/service/SocketFrames.ts new file mode 100644 index 000000000..f916aac7b --- /dev/null +++ b/apps/buddy/platform/service/SocketFrames.ts @@ -0,0 +1,111 @@ +import type { Socket } from 'node:net' +import type { RuntimeMessageTransport } from '../../shared/runtime/rpcPeer' +import { Buffer } from 'node:buffer' +import { Emitter } from '../../shared/events/Emitter' +import { SERVICE_CONNECTION_BYTES, SERVICE_FRAME_BYTES } from '../../shared/service/localServiceProtocol' + +export class SocketFrames { + readonly #socket: Socket + readonly #messages = new Emitter(() => this.close(new Error('SERVICE_FRAME_HANDLER_FAILED'))) + readonly #closed = new Emitter(() => {}) + readonly onFrame = this.#messages.event + readonly onDidClose = this.#closed.event + #buffer = Buffer.alloc(0) + #ended = false + #paused = false + #queuedBytes = 0 + readonly #queued: string[] = [] + + constructor(socket: Socket) { + this.#socket = socket + socket.on('data', chunk => this.#receive(typeof chunk === 'string' ? Buffer.from(chunk) : chunk)) + socket.once('error', () => this.close(new Error('SERVICE_CONNECTION_FAILED'))) + socket.once('close', () => this.close(new Error('SERVICE_CONNECTION_CLOSED'))) + } + + send(frame: string): boolean { + if (this.#ended) + return false + const bytes = Buffer.byteLength(frame) + if (frame.includes('\n') || bytes > SERVICE_FRAME_BYTES || this.#socket.writableLength + bytes > SERVICE_CONNECTION_BYTES) { + this.close(new Error('SERVICE_FRAME_LIMIT')) + return false + } + this.#socket.write(`${frame}\n`) + return true + } + + pause(): void { + this.#paused = true + } + + resume(): void { + this.#paused = false + while (!this.#ended && !this.#paused && this.#queued.length) { + const frame = this.#queued.shift()! + this.#queuedBytes -= Buffer.byteLength(frame) + this.#messages.fire(frame) + } + } + + transport(): RuntimeMessageTransport { + return { + postMessage: (message) => { + if (!this.send(JSON.stringify(message))) + throw new Error('SERVICE_CONNECTION_CLOSED') + }, + subscribe: (listener) => { + const subscription = this.onFrame(frame => listener(JSON.parse(frame))) + this.resume() + return subscription.dispose + }, + } + } + + close(reason = new Error('SERVICE_CONNECTION_CLOSED')): void { + if (this.#ended) + return + this.#ended = true + this.#buffer = Buffer.alloc(0) + this.#queued.length = 0 + this.#queuedBytes = 0 + this.#socket.destroy() + this.#closed.fire(reason) + this.#closed.dispose() + this.#messages.dispose() + } + + #receive(chunk: Buffer): void { + if (this.#ended) + return + this.#buffer = Buffer.concat([this.#buffer, chunk]) + while (!this.#ended) { + const index = this.#buffer.indexOf(10) + if (index < 0) { + if (this.#buffer.length > SERVICE_FRAME_BYTES) + this.close(new Error('SERVICE_FRAME_LIMIT')) + return + } + if (index === 0 || index > SERVICE_FRAME_BYTES) { + this.close(new Error('SERVICE_FRAME_LIMIT')) + return + } + const bytes = this.#buffer.subarray(0, index) + this.#buffer = this.#buffer.subarray(index + 1) + try { + const frame = new TextDecoder('utf-8', { fatal: true }).decode(bytes) + JSON.parse(frame) + if (this.#paused) { + this.#queuedBytes += bytes.length + if (this.#queuedBytes > SERVICE_CONNECTION_BYTES) + throw new Error('SERVICE_FRAME_LIMIT') + this.#queued.push(frame) + } + else { this.#messages.fire(frame) } + } + catch { + this.close(new Error('SERVICE_FRAME_INVALID')) + } + } + } +} diff --git a/apps/buddy/platform/service/__tests__/ServiceChildProcess.spec.ts b/apps/buddy/platform/service/__tests__/ServiceChildProcess.spec.ts new file mode 100644 index 000000000..abce3d2ee --- /dev/null +++ b/apps/buddy/platform/service/__tests__/ServiceChildProcess.spec.ts @@ -0,0 +1,36 @@ +import { readFile } from 'node:fs/promises' +import process from 'node:process' +import { expect, it } from 'vitest' +import { ServiceChildProcess } from '../ServiceChildProcess' + +it.skipIf(process.platform !== 'linux')('stops the owned process group after its leader exits and preserves unrelated processes', async () => { + let output = '' + const child = new ServiceChildProcess({ executable: process.execPath, args: ['-e', ` + const fs = require('node:fs'); const { spawn } = require('node:child_process'); + fs.readFileSync(3); + const child = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { stdio: 'ignore' }); + process.stdout.write(String(child.pid) + '\\n'); child.unref(); + `], env: { PATH: process.env.PATH }, output: (_stream, value) => { output += value } }) + const receipt = await child.ready({ synthetic: true }) + try { + expect(receipt.identity.pid).toBe(child.child.pid) + expect(receipt.identity.startIdentity).not.toBe('') + await child.exited + const descendant = Number(output.trim()) + expect(descendant).toBeGreaterThan(1) + expect(await active(descendant)).toBe(true) + await child.stopAndVerify() + expect(await active(descendant)).toBe(false) + expect(await active(process.pid)).toBe(true) + await child.stopAndVerify() + } + finally { await child.stopAndVerify() } +}) + +async function active(pid: number) { + try { + const stat = await readFile(`/proc/${pid}/stat`, 'utf8') + return !['Z', 'X'].includes(stat.slice(stat.lastIndexOf(')') + 2).split(' ')[0]!) + } + catch { return false } +} diff --git a/apps/buddy/platform/service/__tests__/localService.spec.ts b/apps/buddy/platform/service/__tests__/localService.spec.ts new file mode 100644 index 000000000..f447f0b97 --- /dev/null +++ b/apps/buddy/platform/service/__tests__/localService.spec.ts @@ -0,0 +1,141 @@ +import type { ServiceIdentity, ServiceLaunch } from '../../../shared/service/localServiceProtocol' +import { spawn } from 'node:child_process' +import { randomUUID } from 'node:crypto' +import { once } from 'node:events' +import { mkdir, mkdtemp, rm, symlink } from 'node:fs/promises' +import { createConnection } from 'node:net' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import process from 'node:process' +import { afterEach, expect, it } from 'vitest' +import { serviceLaunchSchema } from '../../../shared/service/localServiceProtocol' +import { localTransports } from '../../ipc/localTransport' +import { RuntimeRpcPeer } from '../../ipc/runtimeRpcPeer' +import { connectLocalService } from '../connectLocalService' +import { HostConnection } from '../HostConnection' +import { LocalServiceServer } from '../LocalServiceServer' +import { readPrivateServiceJson } from '../serviceFiles' +import { readServiceProcessIdentity, waitForServiceProcessExit } from '../serviceProcessIdentity' +import { ServiceProfileOwner } from '../ServiceProfileOwner' + +const cleanup: Array<() => void | Promise> = [] +afterEach(async () => { + for (const action of cleanup.splice(0).reverse()) await action() +}) +const identity: ServiceIdentity = { protocol: 1, profileId: randomUUID(), serviceInstanceId: randomUUID(), buildId: 'a'.repeat(64), sessionId: 'test-session', userId: 'test-user' } +const secret = 'b'.repeat(64) +async function server(role: 'host' | 'management' | 'product' = 'host') { + const root = await mkdtemp(join(tmpdir(), 'lexora-local-')) + cleanup.push(() => rm(root, { recursive: true, force: true })) + const endpoint = process.platform === 'win32' ? localTransports.namedPipe(`\\\\.\\pipe\\lexora-test-${randomUUID()}`) : localTransports.unix(join(root, 'service.sock')) + const peers: RuntimeRpcPeer[] = [] + const listener = new LocalServiceServer({ endpoint, identity, role, secret, connected: (frames) => { + const peer = new RuntimeRpcPeer({ transport: frames.transport(), onFatalError: () => frames.close() }) + frames.onDidClose(error => peer.close(error)) + peer.onRequest('echo', value => value) + peer.notify('initial', { ready: true }) + peers.push(peer) + } }) + await listener.listen() + cleanup.push(() => listener.dispose()) + return { endpoint, listener, peers } +} + +it('authenticates the original instance and preserves frames immediately following the handshake', async () => { + const f = await server() + const client = await connectLocalService(f.endpoint.address, { ...identity, type: 'hello', role: 'host', secret }) + const notifications: unknown[] = [] + client.frames.onFrame(frame => notifications.push(JSON.parse(frame))) + client.frames.resume() + const peer = new RuntimeRpcPeer({ transport: client.frames.transport() }) + cleanup.push(() => client.frames.close()) + expect(await peer.request('echo', { same: true })).toEqual({ same: true }) + expect(notifications).toContainEqual({ jsonrpc: '2.0', method: 'initial', params: { ready: true } }) +}) + +it.each(['role', 'secret', 'instance', 'session', 'build'] as const)('rejects an invalid %s without closing unrelated clients', async (field) => { + const f = await server() + const hello = { ...identity, type: 'hello' as const, role: 'host' as const, secret } + const altered = { ...hello, ...field === 'role' ? { role: 'management' as const } : field === 'secret' ? { secret: 'c'.repeat(64) } : field === 'instance' ? { serviceInstanceId: randomUUID() } : field === 'session' ? { sessionId: 'other-session' } : { buildId: 'c'.repeat(64) } } + await expect(connectLocalService(f.endpoint.address, altered)).rejects.toThrow() + const client = await connectLocalService(f.endpoint.address, hello) + cleanup.push(() => client.frames.close()) + const peer = new RuntimeRpcPeer({ transport: client.frames.transport() }) + expect(await peer.request('echo', field)).toBe(field) +}) + +it('isolates malformed and oversized frames to their connection', async () => { + const f = await server() + const bad = createConnection(f.endpoint.address) + const closed = new Promise(resolve => bad.once('close', () => resolve())) + bad.on('error', () => {}) + bad.write('x'.repeat(1024 * 1024 + 1)) + await closed + const client = await connectLocalService(f.endpoint.address, { ...identity, type: 'hello', role: 'host', secret }) + cleanup.push(() => client.frames.close()) + const peer = new RuntimeRpcPeer({ transport: client.frames.transport() }) + expect(await peer.request('echo', 'alive')).toBe('alive') +}) + +it('invalidates a dispatched old-host call and never replays it into a replacement', async () => { + const first = await server() + const second = await server() + const connection = new HostConnection() + let dispatched = 0 + const connect = async (f: Awaited>, id: string) => { + const client = await connectLocalService(f.endpoint.address, { ...identity, type: 'hello', role: 'host', secret }) + cleanup.push(() => client.frames.close()) + const peer = new RuntimeRpcPeer({ transport: client.frames.transport() }) + connection.attach(id, peer) + f.peers[0]!.onRequest('mutate', (_input, signal) => new Promise((resolve) => { + dispatched++ + signal?.addEventListener('abort', () => resolve('unknown'), { once: true }) + })) + } + await connect(first, 'first') + const pending = connection.request('mutate', {}) + const rejected = expect(pending).rejects.toThrow('PLATFORM_HOST_UNAVAILABLE') + await new Promise(resolve => setTimeout(resolve, 10)) + connection.detach('first') + await rejected + await connect(second, 'second') + expect(await connection.request('echo', 'replacement')).toBe('replacement') + expect(dispatched).toBe(1) + connection.detach('first') + expect(connection.available).toBe(true) + connection.close(new Error('done')) +}) + +it.skipIf(process.platform !== 'linux')('locks the canonical profile before publishing discovery, independent of profile label and aliases', async () => { + const root = await mkdtemp(join(tmpdir(), 'lexora-owner-')) + cleanup.push(() => rm(root, { recursive: true, force: true })) + const profile = join(root, 'profile') + await mkdir(profile) + await symlink(profile, join(root, 'alias')) + const config: ServiceLaunch = serviceLaunchSchema.parse({ version: 1, profile: 'test', profileRoot: profile, resourcesRoot: root, nodeExecutable: process.execPath, coreEntry: join(root, 'core.mjs'), hostExecutable: process.execPath, releaseSetPath: join(root, 'release-set.json'), buildId: identity.buildId, userId: identity.userId, sessionId: identity.sessionId, storageAuthority: 'sqlite', leaseDirectory: join(root, 'locks') }) + const owner = await ServiceProfileOwner.acquire(config) + cleanup.push(() => owner.dispose()) + await expect(ServiceProfileOwner.acquire({ ...config, profile: 'stable', profileRoot: join(root, 'alias') })).rejects.toThrow('STORAGE_ROOT_IN_USE') + const discovery = await readPrivateServiceJson(join(profile, '.runtime/service.json')) + expect(discovery).toMatchObject({ serviceInstanceId: owner.discovery.serviceInstanceId, process: { pid: process.pid }, phase: 'starting' }) + expect(JSON.stringify(discovery)).not.toContain(owner.managementSecret) + await owner.dispose() + const replacement = await ServiceProfileOwner.acquire(config) + cleanup.push(() => replacement.dispose()) + expect(replacement.discovery.serviceInstanceId).not.toBe(owner.discovery.serviceInstanceId) +}) + +it.skipIf(process.platform !== 'linux')('confirms process exit by generation and never accepts a live matching process', async () => { + const child = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { stdio: 'ignore' }) + await once(child, 'spawn') + cleanup.push(() => { + child.kill() + }) + const identity = await readServiceProcessIdentity(child.pid!) + await expect(waitForServiceProcessExit(identity, undefined, 20)).rejects.toThrow('SERVICE_STOP_UNCONFIRMED') + await expect(waitForServiceProcessExit({ ...identity, startIdentity: 'previous-generation' })).resolves.toBeUndefined() + const exited = once(child, 'exit') + child.kill() + await exited + await expect(waitForServiceProcessExit(identity)).resolves.toBeUndefined() +}) diff --git a/apps/buddy/platform/service/connectLocalService.ts b/apps/buddy/platform/service/connectLocalService.ts new file mode 100644 index 000000000..8d6c98a95 --- /dev/null +++ b/apps/buddy/platform/service/connectLocalService.ts @@ -0,0 +1,37 @@ +import type { ServiceHello } from '../../shared/service/localServiceProtocol' +import { createConnection } from 'node:net' +import { serviceHelloSchema, serviceWelcomeSchema } from '../../shared/service/localServiceProtocol' +import { SocketFrames } from './SocketFrames' + +export function connectLocalService(address: string, hello: ServiceHello, signal?: AbortSignal): Promise<{ frames: SocketFrames, connectionId: string }> { + const input = serviceHelloSchema.parse(hello) + return new Promise((resolve, reject) => { + signal?.throwIfAborted() + const socket = createConnection(address) + const frames = new SocketFrames(socket) + const timeout = setTimeout(() => frames.close(new Error('SERVICE_HANDSHAKE_TIMEOUT')), 5000) + const abort = () => frames.close(new Error('SERVICE_CONNECTION_CANCELLED')) + signal?.addEventListener('abort', abort, { once: true }) + const received = frames.onFrame((frame) => { + const welcome = serviceWelcomeSchema.safeParse(JSON.parse(frame)) + if (!welcome.success || ['protocol', 'profileId', 'serviceInstanceId', 'buildId', 'sessionId', 'userId'].some(key => welcome.data[key as keyof typeof welcome.data] !== input[key as keyof ServiceHello])) { + frames.close(new Error('SERVICE_HANDSHAKE_REJECTED')) + return + } + frames.pause() + cleanup() + resolve({ frames, connectionId: welcome.data.connectionId }) + }) + const closed = frames.onDidClose((error) => { + cleanup() + reject(error) + }) + function cleanup() { + clearTimeout(timeout) + signal?.removeEventListener('abort', abort) + received.dispose() + closed.dispose() + } + socket.once('connect', () => frames.send(JSON.stringify(input))) + }) +} diff --git a/apps/buddy/platform/service/launchLocalService.ts b/apps/buddy/platform/service/launchLocalService.ts new file mode 100644 index 000000000..c62dba885 --- /dev/null +++ b/apps/buddy/platform/service/launchLocalService.ts @@ -0,0 +1,98 @@ +import type { ServiceLaunch } from '../../shared/service/localServiceProtocol' +import type { ReleaseFileSystem } from './releaseSet' +import { mkdir, realpath } from 'node:fs/promises' +import { dirname, join } from 'node:path' +import { setTimeout as delay } from 'node:timers/promises' +import { z } from 'zod' +import { serviceDiscoverySchema, serviceIdentitySchema, serviceLaunchSchema } from '../../shared/service/localServiceProtocol' +import { ensurePrivateDirectories } from '../filesystem/privateDirectories' +import { resolveBuddyPrivateDirectories, resolveBuddyProcessControl } from '../native/nativeHost' +import { storageBootstrapRoot } from '../storage/StorageLocationStore' +import { connectLocalService } from './connectLocalService' +import { readReleaseSet } from './releaseSet' +import { readPrivateServiceJson, writePrivateServiceJson } from './serviceFiles' +import { readServiceProcessIdentity, waitForServiceProcessExit } from './serviceProcessIdentity' +import { ServiceRpcChannel } from './ServiceRpcChannel' +import { readServiceSession, SessionServiceRegistration } from './SessionServiceRegistration' + +export interface LocalServiceLaunchOptions { + releaseSetPath: string + releaseFileSystem?: ReleaseFileSystem + profileRoot: string + defaultRoot?: string + profile: ServiceLaunch['profile'] + hostExecutable: string + hostArguments: string[] + leaseDirectory: string + storageAuthority: ServiceLaunch['storageAuthority'] + startIfMissing?: boolean +} + +export async function launchLocalService(options: LocalServiceLaunchOptions) { + const release = await readReleaseSet(options.releaseSetPath, options.releaseFileSystem) + const resourcesRoot = join(dirname(release.coreEntry), 'resources') + const native = { appPath: resourcesRoot, resourcesPath: resourcesRoot, isPackaged: true } + const launcher = resolveBuddyProcessControl(native) + if (!launcher) + throw new Error('SERVICE_LAUNCHER_UNAVAILABLE') + const privateDirectoriesExecutable = resolveBuddyPrivateDirectories(native) + await ensurePrivateDirectories([options.profileRoot], privateDirectoriesExecutable) + const root = await realpath(options.profileRoot) + const directory = join(root, '.runtime') + await mkdir(directory, { recursive: true, mode: 0o700 }) + await ensurePrivateDirectories([directory], privateDirectoriesExecutable) + const session = await readServiceSession() + const config = serviceLaunchSchema.parse({ + version: 1, + profileRoot: root, + defaultRoot: await realpath(options.defaultRoot ?? root), + profile: options.profile, + ...session, + resourcesRoot, + nodeExecutable: release.nodeExecutable, + coreEntry: release.coreEntry, + releaseSetPath: await realpath(options.releaseSetPath), + buildId: release.manifest.buildId, + hostExecutable: await realpath(options.hostExecutable), + hostArguments: options.hostArguments, + storageAuthority: options.storageAuthority, + leaseDirectory: options.leaseDirectory, + privateDirectoriesExecutable, + }) + const existing = await findLiveService(directory, config, launcher) + if (existing || options.startIfMissing === false) + return existing + const launchDirectory = join(storageBootstrapRoot(config.defaultRoot!), 'service') + await ensurePrivateDirectories([launchDirectory], privateDirectoriesExecutable) + const path = join(launchDirectory, 'launch.json') + await writePrivateServiceJson(path, config) + await new SessionServiceRegistration(config, path, launcher).start() + const deadline = Date.now() + 120_000 + while (Date.now() < deadline) { + const connected = await findLiveService(directory, config, launcher) + if (connected) + return connected + await delay(250) + } + throw new Error('SERVICE_START_TIMEOUT') +} + +async function findLiveService(directory: string, config: ServiceLaunch, helper: string) { + const discovery = await readPrivateServiceJson(join(directory, 'service.json')).catch(error => (error as NodeJS.ErrnoException).code === 'ENOENT' ? null : Promise.reject(error)) + if (!discovery) + return null + const current = serviceDiscoverySchema.parse(discovery) + const actual = await readServiceProcessIdentity(current.process.pid, helper).catch(error => ['ENOENT', 'ESRCH'].includes((error as NodeJS.ErrnoException).code ?? '') || (error as Error).message === 'SERVICE_PROCESS_CHANGED' ? null : Promise.reject(error)) + if (!actual || actual.startIdentity !== current.process.startIdentity || actual.executable !== current.process.executable) + return null + if (current.buildId !== config.buildId || current.sessionId !== config.sessionId || current.userId !== config.userId) + throw new Error('SERVICE_RUNNING_DIFFERENT_RELEASE_OR_SESSION') + if (current.phase === 'failed' || current.phase === 'stopping') + throw new Error('SERVICE_REQUIRES_RECOVERY') + if (current.phase !== 'ready') + return null + const credentials = z.object({ version: z.literal(1), serviceInstanceId: z.literal(current.serviceInstanceId), managementSecret: z.string().regex(/^[a-f0-9]{64}$/), productSecret: z.string().regex(/^[a-f0-9]{64}$/) }).strict().parse(await readPrivateServiceJson(join(directory, 'local-client.json'))) + const identity = serviceIdentitySchema.strip().parse(current) + const connection = await connectLocalService(current.managementEndpoint, { ...identity, type: 'hello', role: 'management', secret: credentials.managementSecret }) + return { discovery: current, peer: new ServiceRpcChannel(connection.frames), waitForExit: () => waitForServiceProcessExit(current.process, helper) } +} diff --git a/apps/buddy/platform/service/releaseSet.ts b/apps/buddy/platform/service/releaseSet.ts new file mode 100644 index 000000000..28caf51e5 --- /dev/null +++ b/apps/buddy/platform/service/releaseSet.ts @@ -0,0 +1,120 @@ +import { createHash } from 'node:crypto' +import fs from 'node:fs' +import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path' +import { z } from 'zod' + +export type ReleaseFileSystem = Pick + +const digestSchema = z.string().regex(/^[a-f0-9]{64}$/) +const resourcePath = z.string().min(1).max(1024).refine(value => !isAbsolute(value) && !value.includes('\\') && !value.split('/').some(part => part === '..' || part === '.' || part === '') && !value.includes('\0')) +export const releaseSetContentSchema = z.object({ + version: z.literal(1), + productVersion: z.string().min(1).max(100), + target: z.enum(['linux-x64', 'linux-arm64', 'win32-x64', 'win32-arm64', 'darwin-arm64']), + core: z.object({ entry: resourcePath, manifest: resourcePath, manifestSha256: digestSchema }).strict(), + node: z.object({ entry: resourcePath, version: z.string().regex(/^\d+\.\d+\.\d+$/), archiveSha256: digestSchema, binarySha256: digestSchema }).strict(), + host: z.object({ kind: z.enum(['asar', 'inventory']), path: resourcePath, sha256: digestSchema }).strict(), +}).strict() +export const releaseSetSchema = releaseSetContentSchema.extend({ buildId: digestSchema }).strict() +export type ReleaseSet = z.infer + +export function createReleaseSet(input: z.infer): ReleaseSet { + const value = releaseSetContentSchema.parse(input) + return { ...value, buildId: createHash('sha256').update(JSON.stringify(canonical(value))).digest('hex') } +} + +export async function readReleaseSet(path: string, fileSystem: ReleaseFileSystem = fs) { + const { realpath, readFile } = fileSystem.promises + const hash = (file: string) => hashReleaseFile(file, fileSystem) + const file = await realpath(path) + const resourcesRoot = dirname(file) + const manifest = releaseSetSchema.parse(JSON.parse(await readFile(file, 'utf8'))) + const { buildId, ...content } = manifest + if (buildId !== createReleaseSet(content).buildId) + throw new Error('SERVICE_RELEASE_IDENTITY_CHANGED') + const resolveResource = async (value: string) => { + const location = await realpath(resolve(resourcesRoot, value)) + const boundary = relative(resourcesRoot, location) + if (isAbsolute(boundary) || boundary === '..' || boundary.startsWith(`..${sep}`)) + throw new Error('SERVICE_RESOURCE_OUTSIDE_RELEASE') + return location + } + const coreManifest = await resolveResource(manifest.core.manifest) + const hostArtifact = await resolveResource(manifest.host.path) + if (await hash(coreManifest) !== manifest.core.manifestSha256 || await hash(hostArtifact) !== manifest.host.sha256) + throw new Error('SERVICE_RELEASE_CONTENT_CHANGED') + const coreEntry = await resolveResource(manifest.core.entry) + const nodeExecutable = await resolveResource(manifest.node.entry) + const core = JSON.parse(await readFile(coreManifest, 'utf8')) as { entry?: string, bundled?: Array<{ path: string, sha256: string }>, inventory?: { path: string, sha256: string } } + const entryName = relative(dirname(coreManifest), coreEntry).split(sep).join('/') + const recordedEntry = core.bundled?.find(entry => entry.path === entryName) + if (core.entry !== entryName || !recordedEntry || await hash(coreEntry) !== recordedEntry.sha256) + throw new Error('SERVICE_RELEASE_CONTENT_CHANGED') + if (await hash(nodeExecutable) !== manifest.node.binarySha256 || !core.inventory) + throw new Error('SERVICE_RELEASE_CONTENT_CHANGED') + const coreInventory = await resolveResource(relative(resourcesRoot, join(dirname(coreManifest), resourcePath.parse(core.inventory.path)))) + if (await hash(coreInventory) !== digestSchema.parse(core.inventory.sha256)) + throw new Error('SERVICE_RELEASE_CONTENT_CHANGED') + await verifyFileInventory(coreInventory, [coreManifest], fileSystem) + if (manifest.host.kind === 'inventory') + await verifyFileInventory(hostArtifact, [], fileSystem) + return { manifest, resourcesRoot, nodeExecutable, coreEntry } +} + +export async function hashReleaseFile(path: string, fileSystem: ReleaseFileSystem = fs): Promise { + const hash = createHash('sha256') + for await (const chunk of fileSystem.createReadStream(path)) hash.update(chunk) + return hash.digest('hex') +} +function canonical(value: unknown): unknown { + if (Array.isArray(value)) + return value.map(canonical) + if (value && typeof value === 'object') + return Object.fromEntries(Object.entries(value).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([key, entry]) => [key, canonical(entry)])) + return value +} + +const inventoryEntrySchema = z.union([ + z.object({ path: resourcePath, size: z.number().int().nonnegative(), sha256: digestSchema }).strict(), + z.object({ path: resourcePath, link: z.string().min(1).max(1024) }).strict(), +]) + +export async function verifyFileInventory(inventoryPath: string, excluded: string[] = [], fileSystem: ReleaseFileSystem = fs) { + const { lstat, readdir, readFile, readlink, realpath } = fileSystem.promises + const root = await realpath(dirname(inventoryPath)) + const inventory = z.object({ version: z.literal(1), files: z.array(inventoryEntrySchema).max(100_000) }).strict().parse(JSON.parse(await readFile(inventoryPath, 'utf8'))) + const expected = new Map(inventory.files.map(entry => [entry.path, entry])) + if (expected.size !== inventory.files.length) + throw new Error('SERVICE_RELEASE_CONTENT_CHANGED') + async function visit(directory: string) { + for (const entry of await readdir(directory, { withFileTypes: true })) { + const path = join(directory, entry.name) + if (path === inventoryPath || excluded.includes(path)) + continue + if (entry.isDirectory()) { + await visit(path) + continue + } + const name = relative(root, path).split(sep).join('/') + const recorded = expected.get(name) + if (!recorded) + throw new Error('SERVICE_RELEASE_CONTENT_CHANGED') + expected.delete(name) + if ('link' in recorded) { + if (!entry.isSymbolicLink()) + throw new Error('SERVICE_RELEASE_CONTENT_CHANGED') + const target = await readlink(path) + const actual = await realpath(path) + const boundary = relative(root, actual) + if (target.split(sep).join('/') !== recorded.link || isAbsolute(target) || isAbsolute(boundary) || boundary === '..' || boundary.startsWith(`..${sep}`)) + throw new Error('SERVICE_RESOURCE_OUTSIDE_RELEASE') + } + else if (!entry.isFile() || (await lstat(path)).size !== recorded.size || await hashReleaseFile(path, fileSystem) !== recorded.sha256) { + throw new Error('SERVICE_RELEASE_CONTENT_CHANGED') + } + } + } + await visit(root) + if (expected.size) + throw new Error('SERVICE_RELEASE_CONTENT_CHANGED') +} diff --git a/apps/buddy/platform/service/serviceFiles.ts b/apps/buddy/platform/service/serviceFiles.ts new file mode 100644 index 000000000..ca5c1f8e0 --- /dev/null +++ b/apps/buddy/platform/service/serviceFiles.ts @@ -0,0 +1,52 @@ +import { randomUUID } from 'node:crypto' +import { constants } from 'node:fs' +import { lstat, open, realpath, rename, rm } from 'node:fs/promises' +import { dirname, isAbsolute, join } from 'node:path' +import process from 'node:process' +import { serviceLaunchSchema } from '../../shared/service/localServiceProtocol' +import { ensurePrivateDirectories } from '../filesystem/privateDirectories' +import { syncStorageDirectory } from '../storage/storageFiles' + +export async function readPrivateServiceJson(path: string): Promise { + if (!isAbsolute(path)) + throw new Error('SERVICE_PATH_INVALID') + const metadata = await lstat(path) + if (!metadata.isFile() || metadata.isSymbolicLink() || metadata.size > 64 * 1024 + || (process.platform !== 'win32' && (metadata.uid !== process.getuid?.() || (metadata.mode & 0o077) !== 0))) { + throw new Error('SERVICE_PRIVATE_FILE_INVALID') + } + const file = await open(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0)) + try { + const current = await file.stat() + if (current.dev !== metadata.dev || current.ino !== metadata.ino || current.size > 64 * 1024) + throw new Error('SERVICE_PRIVATE_FILE_CHANGED') + return JSON.parse(await file.readFile('utf8')) + } + finally { await file.close() } +} + +export async function writePrivateServiceJson(path: string, value: unknown): Promise { + const temporary = join(dirname(path), `.${randomUUID()}.tmp`) + const file = await open(temporary, 'wx', 0o600) + try { + await file.writeFile(JSON.stringify(value)) + await file.sync() + await file.close() + await rename(temporary, path) + await syncStorageDirectory(dirname(path)) + } + finally { + await file.close() + await rm(temporary, { force: true }) + } +} + +export async function readServiceLaunch(path: string) { + const input = serviceLaunchSchema.parse(await readPrivateServiceJson(path)) + for (const path of [input.profileRoot, input.defaultRoot, input.resourcesRoot, input.nodeExecutable, input.coreEntry, input.hostExecutable, input.releaseSetPath, input.leaseDirectory, input.privateDirectoriesExecutable]) { + if (path !== undefined && !isAbsolute(path)) + throw new Error('SERVICE_PATH_INVALID') + } + await ensurePrivateDirectories([input.profileRoot], input.privateDirectoriesExecutable) + return { ...input, profileRoot: await realpath(input.profileRoot) } +} diff --git a/apps/buddy/platform/service/serviceProcessIdentity.ts b/apps/buddy/platform/service/serviceProcessIdentity.ts new file mode 100644 index 000000000..be6a90b97 --- /dev/null +++ b/apps/buddy/platform/service/serviceProcessIdentity.ts @@ -0,0 +1,45 @@ +import { readFile, readlink, realpath } from 'node:fs/promises' +import process from 'node:process' +import { setTimeout as delay } from 'node:timers/promises' +import { z } from 'zod' +import { serviceProcessIdentitySchema } from '../../shared/service/localServiceProtocol' +import { runNativeCommand } from '../native/nativeCommand' + +export async function readServiceProcessIdentity(pid = process.pid, helper?: string) { + if (process.platform === 'linux') { + const stat = await readFile(`/proc/${pid}/stat`, 'utf8') + const fields = stat.slice(stat.lastIndexOf(')') + 2).split(' ') + const start = fields[19] + const executable = await readlink(`/proc/${pid}/exe`) + const boot = (await readFile('/proc/sys/kernel/random/boot_id', 'utf8')).trim() + const current = await readFile(`/proc/${pid}/stat`, 'utf8') + if (!start || !/^\d+$/.test(start) || current.slice(current.lastIndexOf(')') + 2).split(' ')[19] !== start || ['Z', 'X'].includes(fields[0]!)) + throw new Error('SERVICE_PROCESS_CHANGED') + return serviceProcessIdentitySchema.parse({ pid, executable, startIdentity: `${boot}:${start}` }) + } + if (helper) { + const result = await runNativeCommand(helper, [], { operation: 'read', pid, protectedPids: [] }, { env: process.platform === 'win32' ? { SystemRoot: process.env.SystemRoot } : {}, maxBytes: 16384, timeoutMs: 5000 }) + if (result.code !== 0) + throw new Error('SERVICE_PROCESS_UNVERIFIABLE') + const [target] = z.array(z.object({ pid: z.number(), executable: z.string(), instanceId: z.string() })).max(1).parse(JSON.parse(result.stdout.toString('utf8'))) + if (target?.pid !== pid) + throw new Error('SERVICE_PROCESS_CHANGED') + return serviceProcessIdentitySchema.parse({ pid, executable: await realpath(target.executable), startIdentity: target.instanceId }) + } + throw new Error('SERVICE_PROCESS_UNVERIFIABLE') +} + +export async function waitForServiceProcessExit(identity: z.infer, helper?: string, timeoutMs = 45000): Promise { + const deadline = Date.now() + timeoutMs + while (Date.now() < deadline) { + const current = await readServiceProcessIdentity(identity.pid, helper).catch((error) => { + if (['ENOENT', 'ESRCH'].includes((error as NodeJS.ErrnoException).code ?? '') || (error as Error).message === 'SERVICE_PROCESS_CHANGED') + return null + throw error + }) + if (!current || current.startIdentity !== identity.startIdentity) + return + await delay(100) + } + throw new Error('SERVICE_STOP_UNCONFIRMED') +} diff --git a/apps/buddy/platform/storage/StorageLocationStore.ts b/apps/buddy/platform/storage/StorageLocationStore.ts index 8265f13ff..34dc155bb 100644 --- a/apps/buddy/platform/storage/StorageLocationStore.ts +++ b/apps/buddy/platform/storage/StorageLocationStore.ts @@ -1,6 +1,6 @@ import type { BackupScope } from '../../shared/storage/backupScope' import type { StorageHistory, StorageIdentity, StorageOperation } from '../../shared/storage/storageApi' -import { randomUUID } from 'node:crypto' +import { createHash, randomUUID } from 'node:crypto' import { lstat, readdir } from 'node:fs/promises' import { dirname, join, parse } from 'node:path' import { z } from 'zod' @@ -8,6 +8,10 @@ import { storageHistorySchema, storageIdentitySchema, storageOperationSchema } f import { canonicalStoragePath, containsStoragePath, isMissing, readStorageJson, storageError, writeStorageJson } from './storageFiles' import { discardStorageOperation } from './storageOperationFiles' +export function storageBootstrapRoot(root: string): string { + return join(dirname(root), '.lexora-locations', createHash('sha256').update(root).digest('hex').slice(0, 24)) +} + export const STORAGE_IDENTITY_FILE = 'storage.json' export const storageLocationSchema = z.object({ version: z.literal(1), @@ -41,8 +45,8 @@ export class StorageLocationStore { if (state.pending) throw storageError('STORAGE_OPERATION_PENDING') await assertStorageIdentity(state) - const restoringHere = kind === 'restore' && await canonicalStoragePath(target) === await canonicalStoragePath(state.activeRoot) - if (kind === 'restore' && !restoringHere) + const restoringHere = (kind === 'restore' || kind === 'upgrade') && await canonicalStoragePath(target) === await canonicalStoragePath(state.activeRoot) + if ((kind === 'restore' || kind === 'upgrade') && !restoringHere) throw storageError('STORAGE_RESTORE_ROOT_MISMATCH') const canonical = restoringHere ? state.activeRoot : await validateStorageTarget(state.activeRoot, target, [dirname(this.path), ...protectedRoots], state.identity?.dataId) const pending: StorageOperation = { id: operationId, kind, ...(kind === 'restore' ? { credentials } : {}), ...(scopes ? { scopes } : {}), ...(snapshotId ? { snapshotId } : {}), source: state.activeRoot, target: canonical, phase: 'pending', error: null, createdAt: new Date().toISOString() } diff --git a/apps/buddy/platform/storage/StorageRootLease.ts b/apps/buddy/platform/storage/StorageRootLease.ts index 17bf11c53..a5ef6a276 100644 --- a/apps/buddy/platform/storage/StorageRootLease.ts +++ b/apps/buddy/platform/storage/StorageRootLease.ts @@ -6,6 +6,13 @@ import { DatabaseSync } from 'node:sqlite' import { ensurePrivateDirectoriesSync } from '../filesystem/privateDirectories' import { isMissing, storageError } from './storageFiles' +const activeLeases = new WeakMap<() => void, string>() + +export function assertStorageRootLease(lease: () => void, root: string): void { + if (activeLeases.get(lease) !== canonical(resolve(root))) + throw storageError('STORAGE_LEASE_INVALID') +} + function canonical(path: string): string { try { return realpathSync.native(path) @@ -18,7 +25,8 @@ function canonical(path: string): string { } export function acquireStorageRootLease(root: string, directory = join(homedir(), '.lexora-locations', 'locks'), privateDirectoriesExecutable?: string): () => void { ensurePrivateDirectoriesSync([directory], privateDirectoriesExecutable) - const identity = createHash('sha256').update(canonical(resolve(root))).digest('hex') + const canonicalRoot = canonical(resolve(root)) + const identity = createHash('sha256').update(canonicalRoot).digest('hex') const database = new DatabaseSync(join(directory, `${identity}.sqlite3`)) try { database.exec('PRAGMA busy_timeout = 0; BEGIN EXCLUSIVE;') @@ -27,5 +35,11 @@ export function acquireStorageRootLease(root: string, directory = join(homedir() database.close() throw storageError('STORAGE_ROOT_IN_USE') } - return () => database.close() + const release = () => { + if (!activeLeases.delete(release)) + return + database.close() + } + activeLeases.set(release, canonicalRoot) + return release } diff --git a/apps/buddy/platform/storage/productIdentity.ts b/apps/buddy/platform/storage/productIdentity.ts new file mode 100644 index 000000000..c7775d33d --- /dev/null +++ b/apps/buddy/platform/storage/productIdentity.ts @@ -0,0 +1,28 @@ +import { randomUUID } from 'node:crypto' +import { mkdir, open } from 'node:fs/promises' +import { dirname, join } from 'node:path' +import { z } from 'zod' +import { productIdSchema } from '../../shared/productProtocol/scalars' +import { storageIdentitySchema } from '../../shared/storage/storageApi' +import { readStorageJson, syncStorageDirectory } from './storageFiles' + +const identitySchema = z.object({ version: z.literal(1), serviceId: productIdSchema, profileId: productIdSchema }).strict() +export type StoredProductIdentity = z.infer + +export async function readOrCreateProductIdentity(profileRoot: string): Promise { + const path = join(profileRoot, 'device/service/identity.json') + const existing = await readStorageJson(path) + if (existing !== null) + return identitySchema.parse(existing) + const storage = await readStorageJson(join(profileRoot, 'storage.json')) + const identity = identitySchema.parse({ version: 1, serviceId: randomUUID(), profileId: storage === null ? randomUUID() : storageIdentitySchema.parse(storage).dataId }) + await mkdir(dirname(path), { recursive: true, mode: 0o700 }) + const file = await open(path, 'wx', 0o600) + try { + await file.writeFile(JSON.stringify(identity)) + await file.sync() + } + finally { await file.close() } + await syncStorageDirectory(dirname(path)) + return identity +} diff --git a/apps/buddy/platform/storage/storageOperationFiles.ts b/apps/buddy/platform/storage/storageOperationFiles.ts index 09b5b2d9c..3d3c302e4 100644 --- a/apps/buddy/platform/storage/storageOperationFiles.ts +++ b/apps/buddy/platform/storage/storageOperationFiles.ts @@ -5,7 +5,7 @@ import { ensurePrivateDirectories } from '../filesystem/privateDirectories' import { isMissing, readStorageJson, storageError, writeStorageJson } from './storageFiles' export function storageOperationStage(operation: StorageOperation): string { - return operation.kind === 'relocate' + return operation.kind === 'relocate' || operation.kind === 'upgrade' ? join(dirname(operation.target), `.${basename(operation.target)}.storage-${operation.id}`) : join(operation.target, `.storage-${operation.id}`) } diff --git a/apps/buddy/service.vite.config.ts b/apps/buddy/service.vite.config.ts new file mode 100644 index 000000000..67db61f99 --- /dev/null +++ b/apps/buddy/service.vite.config.ts @@ -0,0 +1,50 @@ +import type { Plugin } from 'vite' +import { readFile } from 'node:fs/promises' +import { isBuiltin } from 'node:module' +import { basename, isAbsolute } from 'node:path' +import { fileURLToPath } from 'node:url' +import { defineConfig } from 'vite' + +function serviceResources(): Plugin { + return { + name: 'lexora-service-resources', + enforce: 'pre', + async load(id) { + if (!id.endsWith('?asset')) + return + const source = id.slice(0, -6) + const reference = this.emitFile({ type: 'asset', name: basename(source), source: await readFile(source) }) + return `import { fileURLToPath } from 'node:url'; export default fileURLToPath(import.meta.ROLLUP_FILE_URL_${reference});` + }, + resolveFileUrl({ relativePath }) { return `new URL(${JSON.stringify(relativePath)}, import.meta.url).href` }, + generateBundle(_options, bundle) { + const dependencies = new Set() + for (const file of Object.values(bundle)) { + if (file.type !== 'chunk') + continue + for (const id of [...file.imports, ...file.dynamicImports]) { + if (isBuiltin(id) || id.startsWith('.') || Object.hasOwn(bundle, id)) + continue + dependencies.add(id.startsWith('@') ? id.split('/').slice(0, 2).join('/') : id.split('/')[0]!) + } + } + this.emitFile({ type: 'asset', fileName: 'dependencies.json', source: `${JSON.stringify([...dependencies].sort(), null, 2)}\n` }) + }, + } +} + +export default defineConfig({ + plugins: [serviceResources()], + ssr: { noExternal: ['fflate'] }, + build: { + ssr: fileURLToPath(new URL('./service/src/index.ts', import.meta.url)), + outDir: '.output/build/service', + target: 'node26', + minify: false, + ssrEmitAssets: true, + rolldownOptions: { + external: id => id !== 'fflate/browser' && !id.startsWith('.') && !isAbsolute(id) && !id.startsWith('\0'), + output: { entryFileNames: 'service.mjs', format: 'es' }, + }, + }, +}) diff --git a/apps/buddy/service/assets.d.ts b/apps/buddy/service/assets.d.ts new file mode 100644 index 000000000..aefecf8ac --- /dev/null +++ b/apps/buddy/service/assets.d.ts @@ -0,0 +1,9 @@ +declare module '*?asset' { + const path: string + export default path +} + +declare module '*?raw' { + const content: string + export default content +} diff --git a/apps/buddy/service/resources/skills/plugin-creator/references/api.d.ts b/apps/buddy/service/resources/skills/plugin-creator/references/api.d.ts index 15728be5e..8502e0e6a 100644 --- a/apps/buddy/service/resources/skills/plugin-creator/references/api.d.ts +++ b/apps/buddy/service/resources/skills/plugin-creator/references/api.d.ts @@ -176,7 +176,10 @@ export interface AgentReference { scope: { taskId: string, branchId: string } } export interface AgentPublishedArtifact { id: string, name: string, mimeType: string, size: number } +export type InvocationCapabilities = Pick + export interface AgentToolContext { + readonly capabilities: InvocationCapabilities readonly scope: Readonly<{ taskId: string, branchId: string, runId: string }> readonly artifacts: { /** Publish a private file to this task. Requires a write tool. Creates an independent workspace copy and a task output card. */ @@ -258,7 +261,7 @@ export interface ExtensionContext { readonly signal: AbortSignal readonly workers: WorkerApi readonly themes: ThemeApi - readonly extension: { readonly id: string, readonly version: string, readonly apiVersion: 1 | 2 | 3 | 4 } + readonly extension: { readonly id: string, readonly version: string, readonly apiVersion: 1 | 2 | 3 | 4 | 5, readonly executionScope: 'desktop' | 'service' } readonly events: EventSubscriber readonly configuration: { get: () => Promise> @@ -272,7 +275,7 @@ export interface ExtensionContext { registerAction: (id: string, execute: (context: AgentActionContext) => AgentActionResult | Promise) => Disposable } readonly subscriptions: { add: (disposable: T) => T } - readonly commands: { register: (id: string, execute: (context: { resource: Resource | null, arguments: Json, invocation: CommandInvocation | null, taskCleanup?: LegacyTaskCleanupCommandApi }) => Json | void | Promise) => Disposable } + readonly commands: { register: (id: string, execute: (context: { signal: AbortSignal, capabilities: InvocationCapabilities, resource: Resource | null, arguments: Json, invocation: CommandInvocation | null, taskCleanup?: LegacyTaskCleanupCommandApi }) => Json | void | Promise) => Disposable } readonly placements: { show: (id: string, options?: string | PlacementOptions) => Promise, hide: (id: string, options?: string | PlacementOptions) => Promise } readonly window: { readonly styles: { set: (id: string, css: string) => Promise, remove: (id: string) => Promise } } readonly workbench: { readonly panes: readonly PaneSnapshot[], onPanesChange: (listener: (panes: readonly PaneSnapshot[]) => void) => Disposable } @@ -493,7 +496,7 @@ export interface ViewContext { } | null readonly environment: ViewEnvironment onEnvironmentChange: (listener: (environment: ViewContext['environment']) => void) => Disposable - readonly apiVersion: 1 | 2 | 3 | 4 + readonly apiVersion: 1 | 2 | 3 | 4 | 5 readonly resource: Resource | null readonly state: Json readonly stateVersion: number diff --git a/apps/buddy/service/resources/skills/plugin-creator/references/lifecycle.md b/apps/buddy/service/resources/skills/plugin-creator/references/lifecycle.md index a8bbdd1ef..aeb9727a3 100644 --- a/apps/buddy/service/resources/skills/plugin-creator/references/lifecycle.md +++ b/apps/buddy/service/resources/skills/plugin-creator/references/lifecycle.md @@ -38,6 +38,21 @@ context.subscriptions.add(context.commands.register(`${context.extension.id}.ope 同步 CPU 密集任务应移到 Worker,或分块让出事件循环。二进制格式检查应使用有界的 `resources.readBytes` 范围读取和预读缓存,不把整个模型权重传过 RPC,也不以保持心跳代替格式、大小和授权检查。 +## 执行寿命 + +执行范围见[清单契约](protocol.md#执行范围)。`context.extension.executionScope` 表示本次包声明的 desktop 或 service 寿命。service 后端只有一份私有数据,视图按需附着;关闭视图使旧选区、选择器和视图句柄失效。更新前会先停止旧调用和实例,再独占迁移私有数据;迁移失败保留原数据,不能由插件自行启动第二份写入者。 + +API 5 的命令回调和 Agent 工具/动作上下文提供 `capabilities` 与 `signal`。service 插件把本次副作用通过 `invocation.capabilities` 发起;不要在 `activate` 的全局 `context` 上保存可继续执行的网络、存储写入或 UI 闭包。调用结束或取消后,这些 scoped 能力立即失效,已发出的操作不会自动重试。 + +```ts +context.subscriptions.add(context.commands.register(`${context.extension.id}.save`, async (invocation) => { + invocation.signal.throwIfAborted() + await invocation.capabilities.storage.set({ saved: true }) +})) +``` + +关闭 Desktop 会撤销所有视图与 UI 请求。service 后端中的有效任务调用可以继续;无工作台时 UI 方法拒绝为 `EXTENSION_DESKTOP_REQUIRED`。最后一个任务、调用和视图释放后,后端在闲置期限内停止。重开界面复用仍被任务使用的实例,不恢复旧视图 token。 + ## 状态与模块 - `activate(context)` 管理命令、私有业务数据、通知和定时任务;`render(context, container)` 管理自己的隔离 UI。只需要视图时不创建空宿主;需要命令时必须同时声明宿主入口和命令 ID。 diff --git a/apps/buddy/service/resources/skills/plugin-creator/references/protocol.md b/apps/buddy/service/resources/skills/plugin-creator/references/protocol.md index 9d5904c96..eecb5c2f3 100644 --- a/apps/buddy/service/resources/skills/plugin-creator/references/protocol.md +++ b/apps/buddy/service/resources/skills/plugin-creator/references/protocol.md @@ -51,6 +51,14 @@ 声明文件路径使用包内相对路径,`.ts` 文件名直接指向源码,编译工具负责产出运行文件。 +## 执行范围 + +`executionScope` 省略时为 `desktop`,随桌面运行。旧 API 1–4 不会因声明 Agent 工具而获得后台寿命。 + +需要在桌面关闭后继续服务任务的插件,声明 `apiVersion: 5`、`executionScope: "service"` 和 `entry`。全部运行依赖也必须为 service 包。API 5 拒绝未知权限,service 包不能申请 `schedules`。升级为 service 会在安装审核中显示;仍使用隔离的浏览器模块入口,不提供 Node 或任意系统访问。 + +service 的 `activate` 只注册能力、读取配置并初始化私有数据。模型、任务、网络等操作放在本次有效的工具或用户交互调用中,遵守其 signal;调用结束后,缓存的闭包和计时器不能继续使用权限。UI 贡献可选,窗口、选择器和选区操作在没有桌面时返回 `EXTENSION_DESKTOP_REQUIRED`。后端按使用关系启动,无任务或界面使用时停止,打开视图不会再次 activate。 + ## 包格式与版本 - `format: "source"`:自包含 TS/JS 与包内相对导入,由宿主固定编译器处理;原始 HTML、Vue 单文件组件和 npm 裸导入不属于该编译协议。 diff --git a/apps/buddy/service/src/BuddyService.ts b/apps/buddy/service/src/BuddyService.ts index a51c65678..e440b379a 100644 --- a/apps/buddy/service/src/BuddyService.ts +++ b/apps/buddy/service/src/BuddyService.ts @@ -6,47 +6,46 @@ import type { BuddySessionExtensionServices } from './agent/extensions/createBud import type { ReusableBuddySession } from './agent/sessions/ReusableBuddySession' import type { AutomationClock } from './automations/AutomationScheduleEvaluator' import type { BuddyRuntime } from './BuddyRuntime' +import type { TrustedCommandPrincipal } from './commands/CommandAdmissionService' import type { RunEventLogPort } from './events/RunEventPorts' -import type { BuddyServiceRpcServer } from './rpc/BuddyServiceRpcServer' -import type { BuddyServiceErrorCode } from './rpc/runtimeRequest' +import type { RuntimePlatform } from './platform/RuntimePlatform' +import type { ProductActorCommands as BuddyServiceCommands } from './productProtocol/ProductActorCommandScope' +import type { ProductConnectionOptions } from './productProtocol/ProductConnection' +import type { BuddyServiceErrorCode, RuntimeRequestRegistrar } from './rpc/runtimeRequest' +import type { ServiceClientPeer, ServiceClientRole } from './rpc/ServiceClientConnections' import type { ConversationRecord } from './storage/conversationRecord' import { randomUUID } from 'node:crypto' import { mkdir } from 'node:fs/promises' import { join } from 'node:path' -import process from 'node:process' import { currentPlatform } from '../../platform/currentPlatform' -import { resolveWindowsPowerShell } from '../../platform/windows/powerShell' import { artifactsChanged } from '../../shared/artifacts/artifactApi' import { automationNotifications } from '../../shared/automation/automationApi' import { buddiesChanged } from '../../shared/buddies/buddyApi' import { changesChanged } from '../../shared/changes/changeApi' import { connectorNotifications } from '../../shared/connectors/connectorApi' - -import { contextPanelRpc, contextPanelStateSchema } from '../../shared/context-panel/contextPanel' import { buddyUserMessageContentV1Schema, getBuddyUserContentActorReferences } from '../../shared/conversation/buddyUserContent' + import { composerResourcesChanged } from '../../shared/conversation/composerApi' import { CONVERSATION_CHANGED, conversationTimelineChanged } from '../../shared/conversation/conversationApi' - import { Emitter } from '../../shared/events/Emitter' + import { executionsChanged } from '../../shared/executions/executionPresentationApi' import { extensionActionRpc } from '../../shared/extensions/extensionActionApi' -import { extensionAgentRpc } from '../../shared/extensions/extensionAgent' -import { extensionCapabilitiesRpc, extensionCapabilityApplyRpc, extensionCapabilityDescriptorSchema, extensionCapabilityImpactRpc } from '../../shared/extensions/extensionCapabilities' -import { extensionShellRpc } from '../../shared/extensions/extensionShell' -import { extensionSkillDescriptorSchema, extensionSkillsRpc } from '../../shared/extensions/extensionSkills' -import { imagePreferencesSchema, imageRpc } from '../../shared/images/imagePreferences' +import { imageRpc } from '../../shared/images/imagePreferences' import { ServiceHost } from '../../shared/lifecycle/ServiceHost' import { ServiceLifecycleSource } from '../../shared/lifecycle/ServiceLifecycleSource' import { webSettingsChanged } from '../../shared/network/webApi' import { notificationsChanged } from '../../shared/notifications/notificationApi' import { ApplicationEvents as EventPublisher } from '../../shared/observability/ApplicationEvents' import { observeLifecycleDiagnostics } from '../../shared/observability/lifecycleDiagnostics' +import { resourcePrepareSchema } from '../../shared/privateNetwork/resources' +import { ProductProtocolError } from '../../shared/productProtocol/errors' +import { desktopProductContentProject } from '../../shared/productProtocol/transport' import { promptsRpc } from '../../shared/prompts/promptApi' import { userQuestionsChanged } from '../../shared/questions/userQuestionApi' +import { toPublicRunEvent } from '../../shared/runs/publicRunEvent' +import { runNotifications } from '../../shared/runs/runApi' import { taskMetricsChanged } from '../../shared/runs/taskMetricsApi' -import { openExternalResultSchema } from '../../shared/runtime/credentialProtocol' -import { runtimePreferencesRpc, runtimePreferencesSchema } from '../../shared/runtime/runtimePreferences' -import { skillReviewRequested } from '../../shared/skills/skillApi' import { spaceChanged } from '../../shared/spaces/spaceApi' import { spaceFilesChanged } from '../../shared/spaces/spaceFileApi' import { PiEventBridge } from './agent/events/PiEventBridge' @@ -61,6 +60,7 @@ import { ExecutionPresentationService } from './agent/execution/ExecutionPresent import { PiTurnExecutor } from './agent/execution/PiTurnExecutor' import { readExecutionResult } from './agent/execution/readExecutionResult' import { registerExecutionRpc } from './agent/execution/registerExecutionRpc' +import { RuntimeExecutionLifecycle } from './agent/execution/RuntimeExecutionLifecycle' import { observeSessionResourceDiagnostics } from './agent/resources/observeSessionResourceDiagnostics' import { SessionResourceReconciler } from './agent/resources/SessionResourceReconciler' import { bindRuntimePreferences } from './agent/sessions/bindRuntimePreferences' @@ -77,7 +77,6 @@ import { reconcileLegacyArtifactOutputs } from './artifacts/LegacyArtifactRecove import { registerArtifactRpc } from './artifacts/registerArtifactRpc' import { AttachmentService } from './attachments/AttachmentService' import { ComposerResourceService } from './attachments/ComposerResourceService' -import { registerAttachmentRpc } from './attachments/registerAttachmentRpc' import { registerComposerResourceRpc } from './attachments/registerComposerResourceRpc' import { AgentTaskAutomationAction } from './automations/AgentTaskAutomationAction' import { AutomationChangeCoordinator } from './automations/AutomationChangeCoordinator' @@ -89,7 +88,6 @@ import { AutomationService } from './automations/AutomationService' import { AutomationTurnService } from './automations/AutomationTurnService' import { registerAutomationRpc } from './automations/registerAutomationRpc' import { resolveAutomationModelSelection } from './automations/resolveAutomationModelSelection' -import { BrowserHostClient } from './browser/BrowserHostClient' import { BuddiesService } from './buddies/BuddiesService' import { BuddyDefinitionResolver } from './buddies/BuddyDefinitionResolver' import { readBuddyToolOptions } from './buddies/readBuddyToolOptions' @@ -111,8 +109,8 @@ import { QueueContinuation } from './chat/QueueContinuation' import { registerChatRpc } from './chat/registerChatRpc' import { registerComposerDraftRpc } from './chat/registerComposerDraftRpc' import { TurnRequestService } from './chat/TurnRequestService' +import { createLocalCommandPrincipal } from './commands/CommandAdmissionService' import { - HostConnectorSecretStore, McpConnectorService, } from './connectors/mcp/McpConnectorService' import { McpSessionResourceConsumer } from './connectors/mcp/McpSessionResourceConsumer' @@ -142,61 +140,68 @@ import { DirectoryGrantService } from './directories/DirectoryGrantService' import { observeRunDiagnostics } from './events/observeRunDiagnostics' import { RunEventCorruptionError } from './events/RunEventFailure' import { RunEventLog } from './events/RunEventLog' +import { SqliteRunEventLog } from './events/SqliteRunEventLog' import { FileResourceCoordinator } from './files/FileResourceCoordinator' import { LocalDocumentFileService, registerLocalDocumentRpc } from './files/LocalDocumentFileService' import { NativeFileEngine } from './files/NativeFileEngine' import { registerTaskFileRpc } from './files/registerTaskFileRpc' import { TaskFileAccess } from './files/TaskFileAccess' import { TaskFileService } from './files/TaskFileService' -import { createPluginImageBackends } from './images/createPluginImageBackends' import { observeImageDiagnostics } from './images/ImageOperationLifecycle' -import { ImageReadingService } from './images/ImageReadingService' import { ImageRequestStore } from './images/ImageRequestStore' import { ImageTransformService } from './images/ImageTransformService' import { OpenAiImageGenerationService } from './images/OpenAiImageGenerationService' -import { PluginImageGateway } from './images/PluginImageGateway' import { RoutedImageGateway } from './images/RoutedImageGateway' import { AttentionNotificationService } from './notifications/AttentionNotificationService' import { NotificationProjection } from './notifications/NotificationProjection' import { registerNotificationRpc } from './notifications/registerNotificationRpc' -import { NetworkPermissionsClient } from './permissions/NetworkPermissionsClient' import { observePetActionDiagnostics } from './pet/observePetActionDiagnostics' import { PetActionService } from './pet/PetActionService' +import { RuntimePlatformLifecycle } from './platform/RuntimePlatformLifecycle' import { bindExtensionActions } from './plugins/bindExtensionActions' import { ExtensionActionService } from './plugins/ExtensionActionService' import { ExtensionAgentRuntime } from './plugins/ExtensionAgentRuntime' import { ExtensionCapabilitySync } from './plugins/ExtensionCapabilitySync' -import { createExtensionResourceCapabilities } from './plugins/extensionResourceCapabilities' import { observeExtensionAgentDiagnostics } from './plugins/observeExtensionAgentDiagnostics' import { PluginAuthoringService } from './plugins/PluginAuthoringService' -import { registerExtensionConditionRpc } from './plugins/registerExtensionConditionRpc' +import { PrivateNetworkController } from './privateNetwork/PrivateNetworkController' +import { RemoteAvatarService } from './privateNetwork/RemoteAvatarService' +import { RemoteResourceService } from './privateNetwork/RemoteResourceService' +import { RemoteUploadService } from './privateNetwork/RemoteUploadService' +import { createProductCommandHandlers } from './productProtocol/createProductCommandHandlers' +import { createProductModelQueries } from './productProtocol/createProductModelQueries' +import { createProductSynchronization } from './productProtocol/createProductSynchronization' +import { ProductActorCommandScope } from './productProtocol/ProductActorCommandScope' +import { ProductConnection } from './productProtocol/ProductConnection' +import { ProductContentReferences } from './productProtocol/ProductContentReferences' +import { ProductQueries } from './productProtocol/ProductQueries' +import { registerLocalProductConnection } from './productProtocol/registerLocalProductConnection' import { getBuiltinPromptCatalog } from './prompts/builtinPromptCatalog' import { createProviderService } from './providers/createProviderService' import { createExtensionModelCapabilities } from './providers/extensionModelCapabilities' -import { PluginModelRegistry } from './providers/PluginModelRegistry' import { ProviderDependents } from './providers/ProviderDependents' -import { registerProviderRpc } from './providers/registerProviderRpc' -import { refreshProviderSpeechModels, registerProviderSpeechRpc } from './providers/registerProviderSpeechRpc' +import { registerProviderCatalogRpc, registerProviderRpc } from './providers/registerProviderRpc' import { resolveInteractiveModelSelection } from './providers/resolveInteractiveModelSelection' import { registerUserQuestionRpc } from './questions/registerUserQuestionRpc' import { UserQuestionDelivery } from './questions/UserQuestionDelivery' import { UserQuestionService } from './questions/UserQuestionService' +import { candidateLegacyRegistrar, ProductCommandRequiredError } from './rpc/candidateLegacyRegistrar' import { BuddyServiceError, registerRuntimeRequest } from './rpc/runtimeRequest' +import { ServiceClientConnections } from './rpc/ServiceClientConnections' import { ConversationStatusService } from './runs/ConversationStatusService' import { registerRunRpc } from './runs/registerRunRpc' import { RunContinuityService } from './runs/RunContinuityService' import { RunLifecycleService } from './runs/RunLifecycleService' import { RunRecoveryService } from './runs/RunRecoveryService' import { observeSandboxClient } from './sandbox/observeSandboxClient' -import { ShellSandboxClient } from './sandbox/ShellSandboxClient' -import { ExtensionShellRegistry } from './sandbox/ShellSelection' import { observeSkillDiagnostics, observeSkillNotifications } from './skills/observeSkillEvents' import { + registerSkillCatalogRpc, registerSkillServiceRpc, SkillService, } from './skills/SkillService' import { registerSpaceFileRpc } from './spaces/registerSpaceFileRpc' -import { registerSpaceRpc } from './spaces/registerSpaceRpc' +import { registerSpaceQueries, registerSpaceRpc } from './spaces/registerSpaceRpc' import { SpaceDependents } from './spaces/SpaceDependents' import { matchesSpaceExecutionContext } from './spaces/spaceExecutionContext' import { SpaceFileService } from './spaces/SpaceFileService' @@ -208,9 +213,10 @@ import { createAutomationRepositories } from './storage/automationRepository' import { createAutomationTurnRepository } from './storage/automationTurnRepository' import { BuddyDataPaths } from './storage/BuddyDataPaths' import { createBuddyRepository } from './storage/buddyRepository' +import { ChatControlCommandRepository } from './storage/ChatControlCommandRepository' import { createChatQueueRepository } from './storage/chatQueueRepository' import { createCommandRequestRepository } from './storage/commandRequestRepository' -import { createComposerDraftRepository } from './storage/composerDraftRepository' +import { createComposerDraftRepository, findPersonalComposerDraft } from './storage/composerDraftRepository' import { createComposerResourceRepository } from './storage/composerResourceRepository' import { createConnectorRepository } from './storage/connectorRepository' import { createConversationArchiveRepository } from './storage/conversationArchiveRepository' @@ -234,9 +240,11 @@ import { createRunRepository } from './storage/runRepository' import { createSessionReferenceRepository } from './storage/sessionReferenceRepository' import { createSkillRepository } from './storage/skillRepository' import { createSpaceRepository } from './storage/spaceRepository' +import { requireSqliteAuthority } from './storage/sqliteAuthority' +import { guardStorageDatabase } from './storage/StorageIsolation' import { createTaskMarkRepository } from './storage/taskMarkRepository' import { createThreadBindingRepository } from './storage/threadBindingRepository' -import { createTurnRequestRepository } from './storage/turnRequestRepository' +import { afterTransactionCommit, withTransaction } from './storage/transaction' import { createUsageAnalyticsRepository } from './storage/usageAnalyticsRepository' import { createUsageRepository } from './storage/usageRepository' import { createWorkspaceRepository } from './storage/workspaceRepository' @@ -244,13 +252,14 @@ import { registerUsageRpc } from './usage/registerUsageRpc' import { UsageService } from './usage/UsageService' import { observeWebDiagnostics, observeWebHostDiagnostics } from './web/observeWebDiagnostics' import { WebCapabilityService } from './web/WebCapabilityService' -import { WebHostClient } from './web/WebHostClient' import { registerWebSettingsRpc, WebSettingsService } from './web/WebSettingsService' import { normalizeComposerWorkspace } from './workspace/normalizeComposerWorkspace' import { registerWorkspaceStateRpc } from './workspace/registerWorkspaceStateRpc' import { WorkspaceStateService } from './workspace/WorkspaceStateService' export interface StartBuddyServiceOptions { + serviceInstanceId?: string + productIdentity?: { serviceId: string, profileId: string } record?: ApplicationDiagnosticReporter events?: ApplicationEvents lifecycle?: ServiceLifecycleSource @@ -258,11 +267,21 @@ export interface StartBuddyServiceOptions { buddyHome: string builtinSkillsDirectories?: readonly string[] database: DatabaseSync - rpc: BuddyServiceRpcServer + platform: RuntimePlatform eventLog: RunEventLogPort } +export type { ProductActorCommands as BuddyServiceCommands } from './productProtocol/ProductActorCommandScope' + export interface BuddyServiceHandle { + suspendPlatform: (reason?: string) => Promise + resumePlatform: () => Promise + assertPlatformAvailable: () => void + + createLocalProductConnection: (options: Pick & { connectionId: string }) => ProductConnection + createProductConnection: (options: Pick & { allowLocalEffects?: boolean, trustedPrincipal?: TrustedCommandPrincipal }) => ProductConnection + commands: BuddyServiceCommands | null + connectClient: (role: ServiceClientRole, peer: ServiceClientPeer) => () => void startMaintenance: () => void dispose: () => Promise prepareIdleShutdown: () => boolean @@ -272,18 +291,41 @@ export interface BuddyServiceHandle { export async function startBuddyService( options: StartBuddyServiceOptions, ): Promise { + if (options.database.prepare('SELECT 1 FROM sqlite_schema WHERE type = \'table\' AND name = \'storage_profile\'').get()) { + requireSqliteAuthority(options.database) + if (!options.productIdentity) + throw new ProductProtocolError('SERVICE_NOT_READY') + if (!(options.eventLog instanceof SqliteRunEventLog)) + throw new Error('SQLite authority requires its event owner') + } const events = options.events ?? new EventPublisher() if (options.record) events.subscribe(options.record) const record = events.publish const host = new ServiceHost(options.lifecycle ?? new ServiceLifecycleSource(() => record({ event: 'observer.failed', component: 'runtime.lifecycle', level: 'warn' }))) const stopLifecycleDiagnostics = options.lifecycle ? () => {} : observeLifecycleDiagnostics(host.lifecycle, events) + const storage = guardStorageDatabase(options.database) + const clients = new ServiceClientConnections(storage.assertAvailable) + let stopExecution: () => Promise = async () => {} + const notify = (method: string, params: unknown) => clients.notify('product', method, params) try { + await host.start('runtime.clients', ({ defer }) => { + const isolation = storage.onDidIsolate(() => { + clients.stopAccepting() + void stopExecution().catch(() => record({ event: 'execution.cleanup_failed', level: 'error' })) + }) + const delivery = options.eventLog.onDidCommit(event => notify(runNotifications.event.method, toPublicRunEvent(event))) + defer(() => { + isolation.dispose() + delivery.dispose() + clients.dispose() + }) + }) + const platformLifecycle = new RuntimePlatformLifecycle(record) + const assertPlatformAvailable = () => platformLifecycle.assertAvailable() const paths = new BuddyDataPaths(options.buddyHome) const agentDirectory = join(options.buddyHome, 'agent') await host.step('runtime.filesystem', async () => { - if (currentPlatform.shell === 'powershell') - process.env.PI_POWERSHELL_PATH ??= await resolveWindowsPowerShell() await Promise.all([ mkdir(agentDirectory, { mode: 0o700, recursive: true }), mkdir(paths.conversationsDirectory, { mode: 0o700, recursive: true }), @@ -291,6 +333,19 @@ export async function startBuddyService( ]) }) + const actorCommandScopes = new Map() + const commandContext = options.eventLog instanceof SqliteRunEventLog + ? await host.start('runtime.command_admission', ({ defer }) => { + const sourceInstanceId = options.serviceInstanceId ?? randomUUID() + const principal = createLocalCommandPrincipal(options.database) + const identity = { ...requireSqliteAuthority(options.database), serviceInstanceId: sourceInstanceId } + const scope = new ProductActorCommandScope(options.database, principal, identity) + actorCommandScopes.set(principal.actorId, scope) + const repository = new ChatControlCommandRepository(options.database, sourceInstanceId) + defer(() => scope.dispose()) + return { admission: scope.admission, repository, principal, writer: (options.eventLog as SqliteRunEventLog).transactionWriter, identity } + }) + : null const spacesRepository = createSpaceRepository(options.database) const conversations = createConversationRepository(options.database) const conversationHistory = createConversationHistoryStore(options.database) @@ -298,6 +353,7 @@ export async function startBuddyService( const conversationMutations = new ConversationMutationGuard() const conversationAvailability = { assertConversationWritable: (id: string) => { + commandContext?.repository.assertAccepting() conversationMutations.assertWritable(id) const conversation = conversations.findById(id) if (conversation?.archivedAt || conversation?.deletedAt) @@ -326,7 +382,7 @@ export async function startBuddyService( const conversationDirectoryGrants = createConversationDirectoryGrantRepository(options.database) const taskMarkRepository = createTaskMarkRepository(options.database) const taskMarks = await host.start('runtime.task_marks', ({ defer }) => { - const service = new TaskMarkService(taskMarkRepository, () => record({ event: 'observer.failed', component: 'runtime.task_marks', level: 'warn' })) + const service = new TaskMarkService(taskMarkRepository, () => record({ event: 'observer.failed', component: 'runtime.task_marks', level: 'warn' }), commandContext ? options.database : undefined) const diagnostics = service.onDidCommit(event => record({ event: `task.mark.${event.kind}`, level: 'info', @@ -361,11 +417,11 @@ export async function startBuddyService( const usageRepository = createUsageRepository(options.database) const workspace = createWorkspaceRepository(options.database) const turnRequests = await host.start('runtime.turn_requests', ({ defer }) => { - const service = new TurnRequestService(createTurnRequestRepository(options.database), () => record({ event: 'observer.failed', component: 'runtime.turn_requests', level: 'warn' }), conversationAvailability) + const service = new TurnRequestService(options.database, () => record({ event: 'observer.failed', component: 'runtime.turn_requests', level: 'warn' }), conversationAvailability, commandContext?.writer) const notifications = service.onDidCommit((event) => { const conversation = conversations.findById(event.conversationId) if (conversation) - options.rpc.notify(CONVERSATION_CHANGED, conversation) + notify(CONVERSATION_CHANGED, conversation) }) const diagnostics = service.onDidCommit((event) => { for (const fact of event.facts) @@ -382,7 +438,7 @@ export async function startBuddyService( managedCapabilities.suspendAll() const buddies = await host.start('runtime.buddies', ({ defer }) => { const service = new BuddiesService(createBuddyRepository(options.database), () => record({ event: 'buddies.observer.failed', level: 'warn' })) - const notices = service.onDidChange(event => options.rpc.notify(buddiesChanged.method, event)) + const notices = service.onDidChange(event => notify(buddiesChanged.method, event)) defer(() => { service.dispose() notices.dispose() @@ -390,11 +446,12 @@ export async function startBuddyService( return service }) const composerDrafts = createComposerDraftRepository(options.database) + const personalDrafts = commandContext ? { findById: (id: string) => findPersonalComposerDraft(options.database, id) } : composerDrafts const commandRequests = createCommandRequestRepository(options.database) const connectorsRepository = createConnectorRepository(options.database) const spaceService = await host.start('runtime.spaces', ({ defer }) => { const service = new SpaceService(spacesRepository, () => record({ event: 'space.observer.failed', level: 'warn' })) - const notices = service.onDidCommit(({ sourceId, revision, spaceId, kind }) => options.rpc.notify(spaceChanged.method, { sourceId, revision, spaceId, kind })) + const notices = service.onDidCommit(({ sourceId, revision, spaceId, kind }) => notify(spaceChanged.method, { sourceId, revision, spaceId, kind })) const diagnostics = service.onDidCommit(event => record({ event: `space.${event.kind.replaceAll('-', '_')}`, level: 'info', spaceId: event.spaceId, revision: event.revision, count: event.directories.length })) defer(async () => { await service.dispose() @@ -412,7 +469,7 @@ export async function startBuddyService( const fileResources = new FileResourceCoordinator(fileEngine) const spaceFiles = await host.start('runtime.space_files', ({ defer }) => { const service = new SpaceFileService(spacesRepository, fileEngine) - const notices = service.onDidChange(event => options.rpc.notify(spaceFilesChanged.method, event)) + const notices = service.onDidChange(event => notify(spaceFilesChanged.method, event)) const revoked = spaceService.onDidCommit(event => service.revoke(event.revokedDirectoryIds)) defer(() => { notices.dispose() @@ -432,6 +489,7 @@ export async function startBuddyService( let delegation!: ExecutionDelegationService const approvalService = await host.start('runtime.approvals', ({ defer }) => { const service = new ApprovalService({ + commandAdmission: commandContext ? { database: options.database, admission: commandContext.admission, events: commandContext.writer } : undefined, eventLog: options.eventLog, onExpired: async (runId) => { await runner.cancel(runId, 'AUTOMATION_APPROVAL_EXPIRED') @@ -498,7 +556,7 @@ export async function startBuddyService( const notifyRecovery = (runId: string) => { const run = runs.findById(runId) if (run) - options.rpc.notify(changesChanged.method, { sourceId: recoverySourceId, revision: ++recoveryRevision, conversationId: run.conversationId, runId }) + notify(changesChanged.method, { sourceId: recoverySourceId, revision: ++recoveryRevision, conversationId: run.conversationId, runId }) } const mutations = await host.start('runtime.mutations', async ({ defer }) => { const service = new MutationCoordinator({ repository: createMutationRepository(options.database), evidence: mutationEvidence, resources: fileResources, getRunTitle: (runId) => { @@ -525,7 +583,7 @@ export async function startBuddyService( service.onDidChange(event => record({ event: `changes.${event.kind.replaceAll('-', '_')}`, level: event.errorCode ? 'warn' : 'info', producerInstanceId: event.sourceId, operationId: event.operationId, conversationId: event.conversationId, runId: event.runId, toolCallId: event.toolCallId, revision: event.revision, count: event.count, errorCode: event.errorCode })) service.onDidChange((event) => { if (event.conversationId && !event.errorCode) - options.rpc.notify(changesChanged.method, { sourceId: event.sourceId, revision: event.revision, conversationId: event.conversationId, runId: event.runId }) + notify(changesChanged.method, { sourceId: event.sourceId, revision: event.revision, conversationId: event.conversationId, runId: event.runId }) }) defer(() => service.dispose()) return service @@ -567,7 +625,7 @@ export async function startBuddyService( service.onDidChange(event => record({ event: `artifact.${event.receipt.cause}.${event.kind.replaceAll('-', '_')}${event.kind === 'batch-settled' ? `.${event.receipt.outcome}.${event.receipt.stage}` : ''}`, level: event.errorCode ? 'warn' : 'info', producerInstanceId: event.sourceId, operationId: event.receipt.operationId, conversationId: event.receipt.conversationId, revision: event.revision, count: event.kind === 'file-written' ? event.receipt.written : event.receipt.artifactIds.length, errorCode: event.errorCode })) service.onDidChange((event) => { if (event.kind === 'catalogue-committed') - options.rpc.notify(artifactsChanged.method, { sourceId: event.sourceId, revision: event.revision, conversationId: event.receipt.conversationId }) + notify(artifactsChanged.method, { sourceId: event.sourceId, revision: event.revision, conversationId: event.receipt.conversationId }) }) defer(() => service.dispose()) return service @@ -580,8 +638,8 @@ export async function startBuddyService( paths, })) const composerResourceService = await host.start('runtime.composer_resources', ({ defer }) => { - const service = new ComposerResourceService({ artifacts: artifactService, attachments: attachmentService, conversationGrants: conversationDirectoryGrants, conversations, drafts: composerDrafts, publications: treeRepository, paths, repository: createComposerResourceRepository(options.database), spaces: spacesRepository }) - service.onDidChange(event => options.rpc.notify(composerResourcesChanged.method, { revision: event.revision, draftIds: [...new Set(event.resources.map(resource => resource.draftId))] })) + const service = new ComposerResourceService({ artifacts: artifactService, attachments: attachmentService, conversationGrants: conversationDirectoryGrants, conversations, drafts: personalDrafts, publications: treeRepository, paths, repository: createComposerResourceRepository(options.database), spaces: spacesRepository }) + service.onDidChange(event => notify(composerResourcesChanged.method, { revision: event.revision, draftIds: [...new Set(event.resources.map(resource => resource.draftId))] })) service.onDidChange(event => record({ event: 'composer.resources.committed', level: 'info', operationId: event.operationId, revision: event.revision, count: event.resources.length })) defer(() => service.dispose()) return service @@ -615,7 +673,9 @@ export async function startBuddyService( agentDirectory, database: options.database, getActiveRuns: () => runs.listIncomplete(), - peer: options.rpc, + platform: options.platform, + notify, + notifyManagement: (method, params) => clients.notify('management', method, params), providers: providersRepository, record, }) @@ -629,24 +689,24 @@ export async function startBuddyService( resolveSourceProviderId: providerId => executionModels.resolveSourceProviderId(providerId), }) const pluginImages = await host.start('runtime.plugin_images', ({ defer }) => { - const gateway = new PluginImageGateway(options.rpc) + const gateway = options.platform.createPluginImages() defer(() => gateway.dispose()) return gateway }) - const pluginImageBackends = createPluginImageBackends(options.rpc, providerService, pluginImages) - const getImagePreferences = async (signal?: AbortSignal) => imagePreferencesSchema.parse(await options.rpc.request(imageRpc.preferences, {}, 30000, signal)) + const pluginImageBackends = options.platform.createPluginImageBackends(providerService, pluginImages) + const getImagePreferences = options.platform.imagePreferences const imageGenerationGateway = new RoutedImageGateway({ native: nativeImageGateway, plugins: pluginImageBackends.generation, providers: providerService, preferences: getImagePreferences, }) - const imageReading = new ImageReadingService({ plugins: pluginImageBackends.reading, rpc: options.rpc, providers: providerService, usage: usageService, preferences: getImagePreferences }) + const imageReading = options.platform.createImageReading({ plugins: pluginImageBackends.reading, providers: providerService, usage: usageService, preferences: getImagePreferences }) const sessions = await host.start('runtime.sessions', () => new BuddySessionRegistry()) const sessionReferences = createSessionReferenceRepository(options.database) const conversationMetadata = await host.start('runtime.task_metadata', ({ defer }) => { const service = new ConversationMetadataService({ - assertWritable: id => conversationMutations.assertWritable(id), + database: commandContext ? options.database : undefined, buddies, runInputs, runs, @@ -666,7 +726,7 @@ export async function startBuddyService( resolveModelSelection: selection => resolveInteractiveModelSelection(providerService, selection), onObserverError: () => record({ event: 'task.observer_failed', level: 'warn' }), }) - const notification = service.onDidCommit(event => options.rpc.notify(CONVERSATION_CHANGED, event.conversation)) + const notification = service.onDidCommit(event => notify(CONVERSATION_CHANGED, event.conversation)) const diagnostics = service.onDidCommit(event => record({ event: `task.${event.kind}.committed`, level: 'info', @@ -685,21 +745,31 @@ export async function startBuddyService( const extensionTaskContext = new ExtensionTaskContext(conversations, extensionHistory, runs, runInputs, conversationAvailability.assertConversationWritable) const extensionAgent = await host.start('runtime.plugins', ({ defer }) => { const service = new ExtensionAgentRuntime({ - rpc: options.rpc, + host: options.platform.plugins, createHandlers: (scope) => { const task = extensionTaskContext.open(scope) return { ...createExtensionTaskCapabilities(conversationMetadata, extensionHistory, task, scope), ...createExtensionReferenceCapabilities(extensionReferences, task, scope), - ...createExtensionResourceCapabilities({ rpc: options.rpc, artifacts: artifactService }, task, scope), + ...options.platform.createExtensionResources(artifactService, task, scope), ...createExtensionModelCapabilities(executionModels, usageService, task), } }, onObserverError: () => record({ event: 'plugins.observer_failed', level: 'warn' }), }) const binding = service.bind() + const releases = new Set>() + const terminal = options.eventLog.onDidCommit((event) => { + if (!['run.completed', 'run.failed', 'run.cancelled'].includes(event.type)) + return + const release = service.releaseRun(event.runId).catch(() => record({ event: 'plugins.run_release_failed', level: 'warn', runId: event.runId })) + releases.add(release) + void release.finally(() => releases.delete(release)) + }) const diagnostics = observeExtensionAgentDiagnostics(service, record) defer(async () => { + terminal.dispose() + await Promise.all(releases) binding() try { await service.dispose() @@ -708,22 +778,22 @@ export async function startBuddyService( diagnostics.dispose() } }) - defer(options.rpc.onNotification((method) => { - if (method === extensionShellRpc.changed) - void sessions.invalidateAll().catch(() => record({ event: 'shells.sessions_invalidation.failed', level: 'warn', errorCode: 'EXTENSION_INVALIDATION_FAILED' })) - if (method === extensionAgentRpc.changed) - void sessions.invalidateMode('interactive').catch(() => record({ event: 'plugins.sessions_invalidation.failed', level: 'warn', errorCode: 'EXTENSION_INVALIDATION_FAILED' })) + defer(options.platform.subscribeShellChanges(() => { + void sessions.invalidateAll().catch(() => record({ event: 'shells.sessions_invalidation.failed', level: 'warn', errorCode: 'EXTENSION_INVALIDATION_FAILED' })) + })) + defer(options.platform.plugins.subscribe(() => { + void sessions.invalidateMode('interactive').catch(() => record({ event: 'plugins.sessions_invalidation.failed', level: 'warn', errorCode: 'EXTENSION_INVALIDATION_FAILED' })) })) return service }) const pluginModels = await host.start('runtime.plugin_models', ({ defer }) => { - const registry = new PluginModelRegistry(options.rpc, providerService, () => record({ event: 'plugins.models.refresh_failed', level: 'warn', errorCode: 'EXTENSION_INFERENCE_CATALOG_FAILED' })) + const registry = options.platform.createPluginModels(providerService, () => record({ event: 'plugins.models.refresh_failed', level: 'warn', errorCode: 'EXTENSION_INFERENCE_CATALOG_FAILED' })) defer(() => registry.dispose()) void registry.refresh().catch(() => record({ event: 'plugins.models.refresh_failed', level: 'warn', errorCode: 'EXTENSION_INFERENCE_CATALOG_FAILED' })) return registry }) const pluginBuilder = await host.start('runtime.plugin_authoring', ({ defer }) => { - const service = new PluginAuthoringService(options.rpc, () => record({ event: 'plugins.authoring.observer_failed', level: 'warn' })) + const service = new PluginAuthoringService(options.platform.authoring, () => record({ event: 'plugins.authoring.observer_failed', level: 'warn' })) const diagnostics = service.onDidChange(event => record({ event: `plugins.authoring.${event.kind.replaceAll('-', '_')}`, level: event.errorCode ? 'warn' : 'info', extensionId: event.extensionId, operationId: event.operationId, conversationId: event.conversationId, runId: event.runId, count: event.bytes, ...(event.errorCode ? { errorCode: event.errorCode } : {}) })) defer(async () => { try { @@ -753,16 +823,12 @@ export async function startBuddyService( const service = new McpConnectorService({ connectors: connectorsRepository, managed: managedCapabilities, - openExternal: async (url) => { - const result = openExternalResultSchema.parse(await options.rpc.request('host.openExternal', { url })) - if (!result.ok) - throw new Error('MCP authorization page is unavailable') - }, + openExternal: options.platform.openExternal, onListenerError: () => record({ event: 'connectors.observer.failed', level: 'warn', errorCode: 'MCP_OBSERVER_FAILED' }), - secrets: new HostConnectorSecretStore(options.rpc), + secrets: options.platform.connectorSecrets, }) const diagnostics = observeMcpDiagnostics(service, record) - const notifications = observeMcpNotifications(service, event => options.rpc.notify(connectorNotifications.changed.method, event)) + const notifications = observeMcpNotifications(service, event => notify(connectorNotifications.changed.method, event)) defer(async () => { try { await service.close() @@ -777,10 +843,7 @@ export async function startBuddyService( const { skillService, sessionResources } = await host.start('runtime.skills', async ({ defer }) => { const pluginSkillsChanged = new Emitter(() => record({ event: 'skills.plugins.observer_failed', level: 'warn', errorCode: 'SKILL_OBSERVER_FAILED' })) defer(() => pluginSkillsChanged.dispose()) - defer(options.rpc.onNotification((method) => { - if (method === extensionSkillsRpc.changed) - pluginSkillsChanged.fire() - })) + defer(options.platform.skills.subscribe(() => pluginSkillsChanged.fire())) const skillService = new SkillService({ agentDirectory, builtinSkillsDirectories: options.builtinSkillsDirectories, @@ -788,7 +851,7 @@ export async function startBuddyService( repository: createSkillRepository(options.database), paths, pluginSkills: { - list: async () => extensionSkillDescriptorSchema.array().parse(await options.rpc.request(extensionSkillsRpc.list, {})), + list: options.platform.skills.list, onDidChange: pluginSkillsChanged.event, }, onListenerError: () => record({ event: 'skills.observer.failed', level: 'warn', errorCode: 'SKILL_OBSERVER_FAILED' }), @@ -800,7 +863,7 @@ export async function startBuddyService( }) const resourceDiagnostics = observeSessionResourceDiagnostics(sessionResources, record) const skillDiagnostics = observeSkillDiagnostics(skillService, record) - const notifications = observeSkillNotifications(skillService, event => options.rpc.notify('skills.changed', event)) + const notifications = observeSkillNotifications(skillService, event => notify('skills.changed', event)) defer(async () => { try { try { @@ -830,26 +893,23 @@ export async function startBuddyService( buddies, connectors: connectorService, sources: managedCapabilities, - descriptors: async () => extensionCapabilityDescriptorSchema.array().parse(await options.rpc.request(extensionCapabilitiesRpc.list, {})), + descriptors: options.platform.capabilities.list, report: event => record(event), }) - const impact = registerRuntimeRequest(options.rpc, extensionCapabilityImpactRpc, input => sync.impact(input.extensionId)) - const apply = registerRuntimeRequest(options.rpc, extensionCapabilityApplyRpc, async (input, signal) => { - await sync.apply(input.descriptors, input.clearExtensionId, signal) - return null + const binding = options.platform.capabilities.bind({ + impact: extensionId => sync.impact(extensionId), + apply: (descriptors, clearExtensionId, signal) => sync.apply(descriptors, clearExtensionId, signal), }) const refreshAvailability = () => { void sync.refreshAvailability().catch(() => record({ event: 'extensions.capabilities.availability_failed', level: 'warn', errorCode: 'EXTENSION_CAPABILITY_FAILED' })) } const connectorChanges = connectorService.onDidChange(refreshAvailability) const connectionChanges = connectorService.onDidChangeConnection(refreshAvailability) - defer(options.rpc.onNotification((method) => { - if (method === extensionCapabilitiesRpc.changed) - void sync.schedule() + defer(options.platform.capabilities.subscribe(() => { + void sync.schedule() })) defer(async () => { - impact() - apply() + binding() connectorChanges.dispose() connectionChanges.dispose() await sync.dispose() @@ -857,10 +917,10 @@ export async function startBuddyService( void sync.schedule() return sync }, ['runtime.buddies', 'runtime.connectors']) - const browserHost = new BrowserHostClient(options.rpc) + const browserHost = options.platform.browser const webSettings = await host.start('runtime.web_settings', ({ defer }) => { - const service = new WebSettingsService(workspace, options.rpc) - const notifications = service.onDidChange(change => options.rpc.notify(webSettingsChanged.method, { operationId: change.operationId, revision: change.revision })) + const service = new WebSettingsService(workspace, options.platform.webSecrets) + const notifications = service.onDidChange(change => notify(webSettingsChanged.method, { operationId: change.operationId, revision: change.revision })) const diagnostics = service.onDidChange(change => record({ event: `web.settings.${change.kind.replaceAll('-', '_')}`, level: change.kind === 'credential-failed' ? 'warn' : 'info', operationId: change.operationId, revision: change.revision })) defer(async () => { await service.dispose() @@ -870,7 +930,7 @@ export async function startBuddyService( return service }) const webHost = await host.start('runtime.web_host', ({ defer }) => { - const client = new WebHostClient(options.rpc) + const client = options.platform.createWeb() const diagnostics = observeWebHostDiagnostics(client, record) defer(async () => { await client.dispose() @@ -895,14 +955,23 @@ export async function startBuddyService( const automationClock = options.automationClock ?? systemAutomationClock const automationRepositories = createAutomationRepositories(options.database) const automationTurns = await host.start('runtime.automation_turns', ({ defer }) => { - const service = new AutomationTurnService(createAutomationTurnRepository(options.database), () => record({ event: 'automation.turn_observer_failed', level: 'warn' })) + const repository = createAutomationTurnRepository(options.database) + const service = new AutomationTurnService({ bind: input => withTransaction(options.database, () => { + commandContext?.repository.assertAccepting() + const binding = repository.bind(input) + if (commandContext && binding.kind === 'bound') { + options.database.prepare('UPDATE runs SET next_event_sequence = 1 WHERE id = ? AND next_event_sequence IS NULL').run(binding.run.id) + commandContext.writer.append({ runId: binding.run.id, type: 'run.queued', payload: { automationId: binding.occurrence.automationId, occurrenceId: binding.occurrence.id }, createdAt: input.boundAt }) + } + return binding + }) }, () => record({ event: 'automation.turn_observer_failed', level: 'warn' })) const notifications = service.onDidCommit((event) => { for (const fact of event.facts) { if (fact.kind !== 'task.created') continue const conversation = conversations.findById(fact.conversationId) if (conversation) - options.rpc.notify(CONVERSATION_CHANGED, conversation) + notify(CONVERSATION_CHANGED, conversation) } }) const diagnostics = service.onDidCommit((event) => { @@ -957,7 +1026,7 @@ export async function startBuddyService( ), listModels: () => providersRepository.models.list(), }) - const changed = service.onDidCommit(event => options.rpc.notify(notificationsChanged.method, { revision: event.revision })) + const changed = service.onDidCommit(event => notify(notificationsChanged.method, { revision: event.revision })) const diagnostics = service.onDidCommit(event => record({ event: `notifications.${event.reason}`, level: 'info', operationId: event.operationId, revision: event.revision, count: event.changes.length })) const projection = new NotificationProjection({ service, providers: providerService, automations: automationService, runs: options.eventLog, runRecords: runs, lifecycle: runLifecycleService, record }) defer(async () => { @@ -970,10 +1039,9 @@ export async function startBuddyService( }) let automationScheduler: AutomationScheduler | null = null let automationDispatcher: AutomationDispatcher | null = null - let stopExecution: () => Promise = async () => {} const automationChanges = await host.start('runtime.automation_changes', ({ defer }) => { const consumer = new AutomationChangeCoordinator({ - notify: automationId => options.rpc.notify(automationNotifications.changed.method, { automationId }), + notify: automationId => notify(automationNotifications.changed.method, { automationId }), service: automationService, turns: automationTurns, runChanges: { eventLog: options.eventLog, lifecycle: runLifecycleService, runs }, @@ -1006,7 +1074,7 @@ export async function startBuddyService( }, ['runtime.spaces', 'runtime.directory_grants', 'runtime.sessions']) let chatQueueService: ChatQueueService | undefined const petActions = await host.start('runtime.pet_actions', ({ defer }) => { - const service = new PetActionService({ eventSink: event => options.eventLog.append(event), peer: options.rpc }) + const service = new PetActionService({ eventSink: event => options.eventLog.append(event), executeSequence: options.platform.executePetSequence }) const diagnostics = observePetActionDiagnostics(service, record) defer(async () => { try { @@ -1022,12 +1090,12 @@ export async function startBuddyService( codemodeWorkers: await host.start('runtime.codemode_workers', async ({ defer }) => { const pool = new CodemodeWorkerPool() defer(() => pool.dispose()) - const unsubscribe = await bindRuntimePreferences(options.rpc, preferences => pool.configure(preferences)) + const unsubscribe = await bindRuntimePreferences(options.platform.preferences, preferences => pool.configure(preferences)) defer(unsubscribe) return pool }), - shells: new ExtensionShellRegistry(options.rpc), - networkPermissions: new NetworkPermissionsClient(options.rpc), + shells: options.platform.shells, + networkPermissions: options.platform.networkPermissions, imageReading, getImagePreferences, fileResources, @@ -1036,7 +1104,7 @@ export async function startBuddyService( followUp: async (runId, signal) => { await chatQueueService?.followUp(runId, signal) }, prepareForRun: signal => connectorService.prepareForRun(signal), shellSandbox: await host.start('runtime.shell_sandbox', ({ defer }) => { - const sandbox = new ShellSandboxClient(options.rpc) + const sandbox = options.platform.createSandbox() const diagnostics = observeSandboxClient(sandbox, record) const prepareSpaces = (spaceId?: string) => { if (currentPlatform.shell !== 'powershell') @@ -1054,10 +1122,7 @@ export async function startBuddyService( if (event.facets.includes('directories') && event.kind !== 'deleted') prepareSpaces(event.spaceId) }) - const installed = options.rpc.onNotification((method) => { - if (method === 'host.sandbox.ready') - prepareSpaces() - }) + const installed = options.platform.subscribeSandboxReady(() => prepareSpaces()) prepareSpaces() defer(async () => { preparedSpaces.dispose() @@ -1079,9 +1144,9 @@ export async function startBuddyService( buddies, getRunBuddyId: runId => runs.findById(runId)?.buddyId ?? null, record, - pluginAuthoring: options.rpc, + pluginAuthoring: options.platform.authoring, pluginBuilder, - skillAuthoring: { skills: skillService, requestReview: preview => options.rpc.notify(skillReviewRequested.method, preview) }, + skillAuthoring: { skills: skillService, requestReview: options.platform.reviewSkill }, pluginCapabilities: context => extensionAgent.capabilities(context), artifactService, attachmentService, @@ -1089,7 +1154,7 @@ export async function startBuddyService( browserHost, presentBrowser: async (source) => { try { - contextPanelStateSchema.parse(await options.rpc.request(contextPanelRpc.presentBrowser, source)) + await options.platform.presentBrowser?.(source) } catch { record({ event: 'context_panel.presentation.failed', level: 'warn', errorCode: 'CONTEXT_PANEL_UNAVAILABLE' }) @@ -1207,7 +1272,7 @@ export async function startBuddyService( }) questions = await host.start('runtime.user_questions', ({ defer }) => { const service = new UserQuestionService({ database: options.database, executions: executionRecords, conversations, coordinator: executionCoordinator, deliver: request => questionDelivery.deliver(request) }) - const subscription = service.onDidChange(event => options.rpc.notify(userQuestionsChanged.method, { conversationId: event.conversationId, branchId: event.branchId, runId: event.runId })) + const subscription = service.onDidChange(event => notify(userQuestionsChanged.method, { conversationId: event.conversationId, branchId: event.branchId, runId: event.runId })) defer(async () => { subscription.dispose() await service.dispose() @@ -1234,7 +1299,7 @@ export async function startBuddyService( const service = new BuddySessionFactory({ getRunPermissionCeiling, getFileAccessCeiling, - bindPreferences: apply => bindRuntimePreferences(options.rpc, apply), + bindPreferences: apply => bindRuntimePreferences(options.platform.preferences, apply), tree: conversationTree, events, agentDirectory, @@ -1264,7 +1329,7 @@ export async function startBuddyService( stopExecution: id => runner.interruptExecution(id), onObserverError: () => record({ event: 'execution.presentation_observer_failed', level: 'warn' }), }) - const subscription = service.onDidChange(event => options.rpc.notify(executionsChanged.method, event)) + const subscription = service.onDidChange(event => notify(executionsChanged.method, event)) defer(() => { subscription.dispose() service.dispose() @@ -1280,6 +1345,7 @@ export async function startBuddyService( }) runner = await host.start('runtime.execution', ({ defer }) => { const service = new BuddyAgentRunner({ + assertStorageAvailable: storage.assertAvailable, executor: piTurnExecutor, executions: executionCoordinator, lifecycle: runLifecycleService, @@ -1320,18 +1386,12 @@ export async function startBuddyService( operationId: settled.executionId, ...(settled.cleanup === 'degraded' ? { errorCode: 'EXECUTION_CLEANUP_DEGRADED' } : {}), })) - let stopped: Promise | undefined - stopExecution = () => stopped ??= (async () => { - const scanning = automationScheduler?.dispose() - automationDispatcher?.stop() - const errors: unknown[] = [] - await scanning?.catch(error => errors.push(error)) - await service.dispose().catch(error => errors.push(error)) - await automationDispatcher?.dispose().catch(error => errors.push(error)) - await automationScheduler?.settle().catch(error => errors.push(error)) - if (errors.length) - throw new AggregateError(errors, 'Automation and execution cleanup failed') - })() + const executionLifecycle = new RuntimeExecutionLifecycle({ + scheduler: () => automationScheduler, + dispatcher: () => automationDispatcher, + disposeExecutions: () => service.dispose(), + }) + stopExecution = () => executionLifecycle.stop() defer(async () => { try { await stopExecution() @@ -1343,7 +1403,7 @@ export async function startBuddyService( return service }) let extensionActions: ExtensionActionService - const conversationPurge = new ConversationPurgeService({ database: options.database, home: paths.root }) + const conversationPurge = new ConversationPurgeService({ database: options.database, home: paths.root, storageAuthority: commandContext ? 'sqlite' : 'legacy_jsonl' }) const conversationLifecycle = await host.start('runtime.task_deletion', ({ defer }) => { const service = new ConversationLifecycleService({ conversations, @@ -1363,22 +1423,19 @@ export async function startBuddyService( } }, sessions: { invalidateConversation: id => sessions.invalidateConversationWithResult(id) }, - stopWriters: async (id) => { - chatQueue.pause(id) - await extensionActions.cancelAndWait(id) - }, - purge: (id) => { - const events = options.eventLog - if (!(events instanceof RunEventLog)) - throw new BuddyServiceError('CONVERSATION_BUSY') - return attachmentService.withStorageScope(() => events.withConversationPurge(id, () => conversationPurge.purge(id))) + stopWriters: async (conversationId) => { + chatQueue.pause(conversationId) + await extensionActions.cancelAndWait(conversationId) }, + purge: id => attachmentService.withStorageScope(() => options.eventLog instanceof RunEventLog + ? options.eventLog.withConversationPurge(id, () => conversationPurge.purge(id)) + : conversationPurge.purge(id)), onObserverError: () => record({ event: 'task.deletion_observer_failed', level: 'warn' }), }) const notification = service.onDidCommit((event) => { const conversation = conversations.findById(event.conversationId) if (conversation) - options.rpc.notify(CONVERSATION_CHANGED, conversation) + notify(CONVERSATION_CHANGED, conversation) }) const deletionDiagnostic = service.onDidCommit((event) => { if (event.tombstoned) @@ -1404,13 +1461,9 @@ export async function startBuddyService( }) const binding = bindExtensionActions(service, { turns: turnRequests, execution: runner, metadata: conversationMetadata, deletions: conversationLifecycle, runs, events: options.eventLog }) const diagnostics = service.onDidFail(event => record({ event: 'plugins.action.failed', level: 'warn', ...event })) - const notification = service.onDidChange(event => options.rpc.notify(conversationTimelineChanged.method, event)) - const list = registerRuntimeRequest(options.rpc, extensionActionRpc.list, (_input, signal) => service.list(signal)) - const invoke = registerRuntimeRequest(options.rpc, extensionActionRpc.invoke, (input, signal) => service.invoke(input, signal)) + const notification = service.onDidChange(event => notify(conversationTimelineChanged.method, event)) defer(async () => { binding.dispose() - list() - invoke() await service.dispose() diagnostics.dispose() notification.dispose() @@ -1445,6 +1498,11 @@ export async function startBuddyService( skills: skillService, }) turnLauncher = new BuddyTurnLauncher({ + assertServiceAccepting: () => { + assertPlatformAvailable() + commandContext?.repository.assertAccepting() + }, + assertStorageAvailable: storage.assertAvailable, executions: executionCoordinator, planner: executionPlanner, runner, @@ -1460,7 +1518,7 @@ export async function startBuddyService( commands: commandRequests, conversationLifecycle, conversations, - drafts: composerDrafts, + drafts: personalDrafts, spaces: spacesRepository, runs, turnLauncher, @@ -1511,7 +1569,7 @@ export async function startBuddyService( tree: conversationTree, }), composerResources: composerResourceService, - drafts: composerDrafts, + drafts: personalDrafts, attachments: attachmentService, conversationLifecycle, conversations, @@ -1529,12 +1587,12 @@ export async function startBuddyService( }) questionDelivery = new UserQuestionDelivery({ database: options.database, executions: executionRecords, mailbox: createMailboxRepository(options.database), coordinator: executionCoordinator, runner, turns: chatTurnService }) chatQueueService = await host.start('runtime.chat-queue', async ({ defer }) => { - const service = new ChatQueueService({ availability: conversationAvailability, queue: chatQueue, turns: chatTurnService, requests: turnRequests, launcher: turnLauncher, runner, runs, runInputs, getRunPermissionCeiling, eventLog: options.eventLog, onObserverError: () => record({ event: 'queue.observer_failed', level: 'error' }) }) + const service = new ChatQueueService({ control: commandContext?.repository, availability: conversationAvailability, queue: chatQueue, turns: chatTurnService, requests: turnRequests, launcher: turnLauncher, runner, runs, runInputs, getRunPermissionCeiling, eventLog: options.eventLog, onObserverError: () => record({ event: 'queue.observer_failed', level: 'error' }) }) const sourceId = randomUUID() let revision = 0 const notification = service.onDidChange((event) => { if (event.kind === 'delivered' || event.kind === 'dispatched') - options.rpc.notify(conversationTimelineChanged.method, { sourceId, revision: ++revision, ...event.scope }) + notify(conversationTimelineChanged.method, { sourceId, revision: ++revision, ...event.scope }) }) const actionInputs = service.onDidChange((event) => { if (event.kind === 'cancelled') @@ -1573,18 +1631,20 @@ export async function startBuddyService( return service }) const conversationArchive = await host.start('runtime.task_archive', ({ defer }) => { + const onError = () => record({ event: 'task.archive_failed', level: 'warn' as const }) const service = new ConversationArchiveService({ database: options.database, repository: createConversationArchiveRepository(options.database), conversations, mutations: conversationMutations, lifecycle: conversationLifecycle, - onError: () => record({ event: 'task.archive_failed', level: 'warn' }), + ...(commandContext ? { afterCommit: (publish: () => void) => afterTransactionCommit(options.database, publish, onError) } : {}), + onError, }) const subscription = service.onDidChange((id) => { const conversation = conversations.findById(id) if (conversation) - options.rpc.notify(CONVERSATION_CHANGED, conversation) + notify(CONVERSATION_CHANGED, conversation) }) defer(async () => { await service.dispose() @@ -1592,9 +1652,56 @@ export async function startBuddyService( }) return service }) - const conversationCleanup = new ConversationCleanupService({ database: options.database, archive: conversationArchive, lifecycle: conversationLifecycle }) + const remoteUploads = commandContext + ? await host.start('runtime.remote_uploads', ({ defer }) => { + const service = new RemoteUploadService({ database: options.database, root: paths.root, instanceId: commandContext.identity.serviceInstanceId, drafts: composerDraftService }) + defer(() => service.close()) + return service + }) + : null + const createActorCommands = (principal: TrustedCommandPrincipal): BuddyServiceCommands => { + if (!commandContext) + throw new ProductProtocolError('SERVICE_NOT_READY') + const scope = actorCommandScopes.get(principal.actorId) ?? new ProductActorCommandScope(options.database, principal, commandContext.identity) + actorCommandScopes.set(principal.actorId, scope) + const draftAccess = { + find: (id: string) => personalDrafts.findById(id), + run: (id: string, operation: () => T | Promise) => composerDraftService.withDraftLock(id, operation), + withDraftLock: (id: string, operation: () => T | Promise) => composerDraftService.withDraftLock(id, operation), + notifyCommitted: composerDraftService.notifyCommitted.bind(composerDraftService), + } + return scope.initialize({ + runs: { turns: turnRequests, events: commandContext.writer }, + input: { turns: chatTurnService, compact: chatCommandService, queue: chatQueueService!, assertExecutionAvailable: assertPlatformAvailable }, + drafts: { access: draftAccess, prepareAttachments: input => attachmentService.prepareDraftCopy(input), releaseResources: draftId => composerResourceService.discard(draftId) }, + conversations: { metadata: conversationMetadata, lifecycle: conversationLifecycle, archive: conversationArchive ?? undefined, marks: taskMarks }, + control: { repository: commandContext.repository, queue: chatQueueService!, stopExecution: () => stopExecution() }, + uploads: remoteUploads!, + questions, + approvals: approvalService, + openDesktop: options.platform.openDesktop, + onError: () => record({ event: 'command.dispatch_failed', level: 'error' }), + }) + } + const commands = commandContext + ? await host.start('runtime.input_commands', ({ defer }) => { + defer(async () => { + await Promise.all([...actorCommandScopes.values()].map(scope => scope.dispose())) + }) + return createActorCommands(commandContext.principal) + }) + : null const runtime: BuddyRuntime = { - startTurn: input => chatTurnService.start(input), + startTurn: async (input) => { + if (commands) + throw new ProductCommandRequiredError() + storage.assertAvailable() + assertPlatformAvailable() + commandContext?.repository.assertAccepting() + const result = await chatTurnService.start(input) + storage.assertAvailable() + return result + }, } const statistics = await host.start('runtime.conversation_statistics', ({ defer }) => { const service = new ConversationStatusService({ @@ -1607,7 +1714,7 @@ export async function startBuddyService( return branchId ? sessions.getReady(conversationId, branchId, conversation?.buddyId)?.getCacheWarmingStatus?.() ?? null : null }, }) - const notices = service.onDidChange(event => options.rpc.notify(taskMetricsChanged.method, event)) + const notices = service.onDidChange(event => notify(taskMetricsChanged.method, event)) const inputs = chatQueueService.onDidChange((event) => { if (event.kind === 'delivered' || event.kind === 'dispatched') service.notifyChanged(event.scope.conversationId) @@ -1627,9 +1734,7 @@ export async function startBuddyService( events: options.eventLog, definitions: buddyDefinitions, getDefaultActorId: () => buddies.defaultBuddyId, - getRuntimePreferences: async () => { - return runtimePreferencesSchema.parse(await options.rpc.request(runtimePreferencesRpc.get, {})) - }, + getRuntimePreferences: options.platform.preferences.get, drafts: composerDrafts, tree: conversationTree, agentDirectory, @@ -1678,14 +1783,15 @@ export async function startBuddyService( const scheduler = new AutomationScheduler({ automationService, clock: automationClock, + canDispatch: () => platformLifecycle.available, dispatch: occurrence => dispatcher.dispatch(occurrence), onObserverError: () => record({ event: 'automation.scheduler_observer_failed', level: 'warn' }), }) automationScheduler = scheduler - const isBusy = () => runner.hasActiveExecutions - || conversationLifecycle.hasPendingOperations - || conversationMutations.hasPendingOperations + const isBusy = () => conversationLifecycle.hasPendingOperations + || [...actorCommandScopes.values()].some(scope => scope.hasPendingOperations) + || runner.hasActiveExecutions || composerDraftService.hasPendingOperations || chatTurnService.hasPendingOperations || chatCommandService.hasPendingOperations @@ -1705,113 +1811,156 @@ export async function startBuddyService( record({ event: 'runtime.runs_recovered', level: 'info', count }) }) - await host.start('runtime.rpc', ({ defer }) => { - const register = (dispose: () => void) => defer(dispose) - register(registerExtensionConditionRpc({ rpc: options.rpc, tasks: conversations, history: extensionHistory, runs, inputs: runInputs, providers: providerService })) - register(registerConversationCleanupRpc({ rpc: options.rpc, cleanup: conversationCleanup })) - register(registerConversationArchiveRpc({ rpc: options.rpc, archive: conversationArchive })) - register( - registerWebSettingsRpc(options.rpc, webSettings), - ) - register( + let privateNetwork: PrivateNetworkController | null = null + const registerClient = await host.start('runtime.rpc', ({ defer }) => { + defer(() => clients.stopAccepting()) + defer(options.platform.bindInternal({ + attachments: attachmentService, + conditions: { tasks: conversations, history: extensionHistory, runs, inputs: runInputs, providers: providerService }, + speech: { providers: providerService, workspace }, + })) + defer(options.platform.subscribeWake(() => automationChanges.wakeScheduler())) + return (connection: RuntimeRequestRegistrar, role: ServiceClientRole) => { + const rpc = connection + const conditionalRpc = commandContext && role === 'product' ? candidateLegacyRegistrar(connection) : connection + if (role === 'management') { + privateNetwork?.register(rpc) + { + const cleanup = new ConversationCleanupService({ + database: options.database, + archive: conversationArchive, + lifecycle: conversationLifecycle, + execute: async (intent) => { + if (!commands || !commandContext) + return conversationArchive.deleteLegacyRemoved(intent) + const receipt = await commands.metadata.execute('conversations.deleteLegacyRemoved', { + commandId: randomUUID(), + dataGeneration: commandContext.identity.dataGeneration, + expectedServiceInstanceId: commandContext.identity.serviceInstanceId, + intent, + }) + return receipt.receipt.decision === 'accepted' + }, + }) + registerConversationCleanupRpc({ rpc, cleanup }) + } + if (commandContext) { + const references = new ProductContentReferences(options.database, commandContext.principal.actorId) + registerRuntimeRequest(rpc, desktopProductContentProject, input => ({ content: references.project(input.content) })) + registerComposerResourceRpc({ drafts: composerDraftService, rpc, service: composerResourceService }) + } + registerWebSettingsRpc(rpc, webSettings) + registerWorkspaceStateRpc({ + service: workspaceStateService, + rpc, + }) + registerAutomationRpc({ + approvals: approvalsRepository, + changes: automationChanges, + clock: automationClock, + lifecycle: automationOccurrenceLifecycle, + rpc, + runs, + service: automationService, + }) + registerMcpConnectorRpc(rpc, connectorService) + registerSpaceRpc({ + rpc, + service: spaceService, + }) + registerLocalDocumentRpc(rpc, new LocalDocumentFileService(options.buddyHome, fileEngine, async (source) => { + const location = source.kind === 'artifact' + ? await artifactService.locate(source.artifactId) + : source.kind === 'space' ? await spaceFiles.locate(source.target) : await taskFileService.locate(source.target) + if (location.kind !== 'file') + throw new Error('FILE_PERMISSION_REVOKED') + return location.path + })) + registerSpaceFileRpc(rpc, spaceFiles) + registerProviderRpc({ + rpc, + service: providerService, + refreshCapabilities: async () => { + await Promise.all([options.platform.refreshProviderSpeech({ providers: providerService, workspace }), pluginModels.refresh()]) + }, + }) + registerSkillServiceRpc(rpc, skillService) + registerRuntimeRequest(rpc, extensionActionRpc.list, (_input, signal) => extensionActions.list(signal)) + registerRuntimeRequest(rpc, extensionActionRpc.invoke, (input, signal) => extensionActions.invoke(input, signal)) + return + } + registerProviderCatalogRpc({ rpc, service: providerService, refreshCapabilities: async () => { + await Promise.all([options.platform.refreshProviderSpeech({ providers: providerService, workspace }), pluginModels.refresh()]) + } }) + registerSpaceQueries({ rpc, service: spaceService }) + registerSkillCatalogRpc(rpc, skillService) registerNotificationRpc({ - rpc: options.rpc, + rpc, service: notificationService, - }), - ) - register( - registerWorkspaceStateRpc({ - service: workspaceStateService, - rpc: options.rpc, - }), - ) - register( + }) registerArtifactRpc({ - rpc: options.rpc, + rpc, service: artifactService, - }), - ) - register( + }) registerChangeRpc({ conversations, runs, - rpc: options.rpc, + rpc, service: changeCaptureService, recovery: mutationRecovery, - }), - ) - register( + }) registerRunRpc({ statistics, isBusy, eventLog: options.eventLog, inputs: runInputs, repository: runs, - rpc: options.rpc, - }), - ) - register(registerRuntimeRequest(options.rpc, promptsRpc.get, () => getBuiltinPromptCatalog())) - register(registerContextPanelRpc({ rpc: options.rpc, runs, events: options.eventLog })) - register(registerUserQuestionRpc({ rpc: options.rpc, service: questions })) - register(registerExecutionRpc({ rpc: options.rpc, service: executionPresentation, inputs: new ExecutionComposerInputService({ drafts: composerDraftService, conversations, executions: executionRecords, getRunPermissionCeiling, mailbox: createMailboxRepository(options.database), delivery: executionPresentation }) })) - register( - registerAttachmentRpc({ - rpc: options.rpc, - service: attachmentService, - }), - ) - register( - registerComposerResourceRpc({ - drafts: composerDraftService, - rpc: options.rpc, - service: composerResourceService, - }), - ) - register( + rpc, + }) + registerRuntimeRequest(rpc, promptsRpc.get, () => getBuiltinPromptCatalog()) + registerContextPanelRpc({ rpc, runs, events: options.eventLog }) + registerUserQuestionRpc({ rpc: conditionalRpc, service: questions }) + registerExecutionRpc({ rpc, service: executionPresentation, inputs: new ExecutionComposerInputService({ drafts: composerDraftService, conversations, executions: executionRecords, getRunPermissionCeiling, mailbox: createMailboxRepository(options.database), delivery: executionPresentation }) }) + if (!commandContext) { + registerComposerResourceRpc({ drafts: composerDraftService, rpc, service: composerResourceService }) + } registerComposerDraftRpc({ - rpc: options.rpc, + rpc: conditionalRpc, service: composerDraftService, - }), - ) - register( + }) registerUsageRpc({ analytics: createUsageAnalyticsRepository(options.database), repository: usageRepository, - rpc: options.rpc, - }), - ) - register( + rpc, + }) registerChatRpc({ attachments: attachmentService, conversations, drafts: composerDraftService, commands: chatCommandService, - rpc: options.rpc, + rpc: conditionalRpc, runtime, turns: chatTurnService, queue: chatQueueService, - }), - ) - register( + }) registerContextRpc({ - rpc: options.rpc, + rpc, service: contextUsageService, - }), - ) - register(registerBuddiesRpc(options.rpc, buddies, signal => readBuddyToolOptions(sessionExtensionServices.createCapabilities, agentDirectory, signal, sessionExtensionServices.shells))) - register(registerTaskMarkRpc(options.rpc, taskMarks, taskAttention)) - register(registerConversationTreeRpc({ - database: options.database, - conversations, - rpc: options.rpc, - artifacts: artifactsRepository, - attachments: attachmentService, - changes: changeCaptureService, - eventLog: options.eventLog, - runInputs, - runs, - })) - register( + }) + registerBuddiesRpc(rpc, buddies, signal => readBuddyToolOptions(sessionExtensionServices.createCapabilities, agentDirectory, signal, sessionExtensionServices.shells)) + registerTaskMarkRpc(conditionalRpc, taskMarks, taskAttention) + registerConversationTreeRpc({ + database: options.database, + conversations, + rpc, + artifacts: artifactsRepository, + attachments: attachmentService, + changes: changeCaptureService, + eventLog: options.eventLog, + runInputs, + runs, + }) + registerConversationArchiveRpc({ rpc: conditionalRpc, archive: conversationArchive, legacyWrites: !commandContext }) registerConversationRpc({ artifacts: artifactsRepository, attachments: attachmentService, @@ -1824,65 +1973,22 @@ export async function startBuddyService( }, eventLog: options.eventLog, isDeleting: conversationId => conversationLifecycle.isDeleting(conversationId), - rpc: options.rpc, + rpc: conditionalRpc, runInputs, runs, - }), - ) - register( + }) registerApprovalRpc({ + commandService: commands ? approvalService : undefined, repository: approvalsRepository, - rpc: options.rpc, + rpc: conditionalRpc, service: approvalService, - }), - ) - register( - registerAutomationRpc({ - approvals: approvalsRepository, - changes: automationChanges, - clock: automationClock, - lifecycle: automationOccurrenceLifecycle, - rpc: options.rpc, - runs, - service: automationService, - }), - ) - register( - registerMcpConnectorRpc(options.rpc, connectorService), - ) - register( - registerSpaceRpc({ - rpc: options.rpc, - service: spaceService, - }), - ) - register(registerLocalDocumentRpc(options.rpc, new LocalDocumentFileService(options.buddyHome, fileEngine, async (source) => { - const location = source.kind === 'artifact' - ? await artifactService.locate(source.artifactId) - : source.kind === 'space' ? await spaceFiles.locate(source.target) : await taskFileService.locate(source.target) - if (location.kind !== 'file') - throw new Error('FILE_PERMISSION_REVOKED') - return location.path - }))) - register(registerSpaceFileRpc(options.rpc, spaceFiles)) - register(registerTaskFileRpc(options.rpc, taskFileService)) - register(options.rpc.onRequest(imageRpc.catalog, async () => { - await pluginModels.refresh() - return { ...await imageGenerationGateway.catalog(), readingCandidates: await imageReading.candidates() } - })) - register(registerProviderSpeechRpc({ rpc: options.rpc, providers: providerService, workspace })) - register( - registerProviderRpc({ - rpc: options.rpc, - service: providerService, - refreshCapabilities: async () => { - await Promise.all([refreshProviderSpeechModels({ rpc: options.rpc, providers: providerService, workspace }), pluginModels.refresh()]) - }, - }), - ) - register( - registerSkillServiceRpc(options.rpc, skillService), - ) + }) + registerTaskFileRpc(rpc, taskFileService) + rpc.onRequest(imageRpc.catalog, async () => { + await pluginModels.refresh() + return { ...await imageGenerationGateway.catalog(), readingCandidates: await imageReading.candidates() } + }) + } }) conversationArchive.start() await host.start('runtime.scheduler', ({ defer }) => { @@ -1897,9 +2003,132 @@ export async function startBuddyService( }) return scheduler.start() }, ['runtime.rpc', 'runtime.execution']) + const remoteResources = commandContext + ? await host.start('runtime.remote_resources', ({ defer }) => { + const service = new RemoteResourceService({ database: options.database, root: paths.root, instanceId: commandContext.identity.serviceInstanceId, artifacts: artifactService, views: actorId => new ProductQueries(options.database, actorId, commandContext.identity.serviceInstanceId).views }) + defer(() => service.close()) + return service + }) + : null + const productQueries = commandContext ? new ProductQueries(options.database, commandContext.principal.actorId, commandContext.identity.serviceInstanceId, undefined, (id, version) => remoteResources!.hasReview(commandContext.principal.actorId, id, version)) : null + const productSync = productQueries ? createProductSynchronization({ database: options.database, queries: productQueries, drafts: composerDraftService, metadata: conversationMetadata, lifecycle: conversationLifecycle, archive: conversationArchive ?? undefined, marks: taskMarks, questions, events: options.eventLog, queue: chatQueueService }) : null + const actorProductScopes = new Map }>() + if (commands && productQueries && productSync) + actorProductScopes.set(commandContext!.principal.actorId, { commands, queries: productQueries, sync: productSync }) + let productStopping = false + const productConnections = new Set() + const createProductConnection: BuddyServiceHandle['createProductConnection'] = (connectionOptions) => { + if (!commands || !commandContext) + throw new ProductProtocolError('SERVICE_NOT_READY') + const context = Object.freeze({ ...connectionOptions.context }) + const assertOwner = () => { + connectionOptions.assertCurrent() + storage.assertAvailable() + commandContext.principal.assertAuthenticated() + if (context.profileId !== options.productIdentity!.profileId || context.serviceId !== options.productIdentity!.serviceId) + throw new ProductProtocolError('NOT_AUTHORIZED') + if (context.ownerPrincipalId !== commandContext.principal.actorId) + throw new ProductProtocolError('NOT_AUTHORIZED') + if (context.dataGeneration !== commands.identity.dataGeneration) + throw new ProductProtocolError('DATA_GENERATION_CHANGED') + if (context.serviceInstanceId !== commands.identity.serviceInstanceId) + throw new ProductProtocolError('SERVICE_INSTANCE_CHANGED') + } + assertOwner() + const principal = connectionOptions.trustedPrincipal ?? commandContext.principal + if (principal.actorId !== context.deviceId) + throw new ProductProtocolError('NOT_AUTHORIZED') + principal.assertAuthenticated() + let scope = actorProductScopes.get(principal.actorId) + if (!scope) { + const queries = new ProductQueries(options.database, principal.actorId, commandContext.identity.serviceInstanceId, undefined, (id, version) => remoteResources!.hasReview(principal.actorId, id, version)) + scope = { commands: createActorCommands(principal), queries, sync: createProductSynchronization({ database: options.database, queries, drafts: composerDraftService, metadata: conversationMetadata, lifecycle: conversationLifecycle, archive: conversationArchive ?? undefined, marks: taskMarks, questions, events: options.eventLog, queue: chatQueueService }) } + actorProductScopes.set(principal.actorId, scope) + } + const actorScope = scope + const connection = new ProductConnection({ + ...connectionOptions, + context, + assertCurrent: assertOwner, + sync: actorScope.sync.publisher, + authorizeTopic: (topic) => { + assertOwner() + actorScope.sync.authorizeTopic(topic) + }, + phase: () => productStopping ? 'stopping' : 'ready', + handlers: { 'resources.prepare': { handle: input => remoteResources!.prepare(resourcePrepareSchema.parse(input), principal) }, ...actorScope.queries.handlers(), ...createProductModelQueries(providerService, id => id ? conversationMetadata.getModelControls(id).next.modelSelection : buddies.get(buddies.defaultBuddyId).settings.model), ...createProductCommandHandlers({ commands: actorScope.commands, assertOwner, hasReview: (id, version) => remoteResources!.hasReview(principal.actorId, id, version), assertExecutionAvailable: assertPlatformAvailable, allowLocalEffects: principal.actorId === commandContext.principal.actorId && connectionOptions.allowLocalEffects, allowedActions: new Set(['compact']), resolveQuote: resourceId => actorScope.queries.resolveQuote(resourceId) }) }, + close: () => { + productConnections.delete(connection) + connectionOptions.close() + }, + }) + productConnections.add(connection) + return connection + } + const createLocalProductConnection: BuddyServiceHandle['createLocalProductConnection'] = (transport) => { + if (!commands || !commandContext || !options.productIdentity) + throw new ProductProtocolError('SERVICE_NOT_READY') + return createProductConnection({ ...transport, allowLocalEffects: true, context: { ...options.productIdentity, ...commands.identity, ownerPrincipalId: commandContext.principal.actorId, deviceId: commandContext.principal.actorId, credentialId: commandContext.principal.actorId, grantGeneration: '0', role: 'owner', connectionId: transport.connectionId, clientSessionId: randomUUID() } }) + } + if (commandContext && options.productIdentity) { + privateNetwork = await host.start('runtime.private_network', async ({ defer }) => { + const avatars = new RemoteAvatarService(options.database) + const controller: PrivateNetworkController = new PrivateNetworkController({ database: options.database, ...options.productIdentity!, serviceInstanceId: commandContext.identity.serviceInstanceId, secrets: options.platform.privateNetworkSecrets, bytes: async (request, response, identity) => avatars.bytes(request, response, controller.auth.principal(identity)) || await remoteUploads!.bytes(request, response, controller.auth.principal(identity)) || await remoteResources!.bytes(request, response, controller.auth.principal(identity)), connect: (identity, transport) => createProductConnection({ ...transport, context: controller.auth.context(identity), assertCurrent: () => controller.auth.assertCurrent(identity), trustedPrincipal: controller.auth.principal(identity) }) }) + defer(() => controller.close()) + await controller.restore() + return controller + }) + } + const platformRecovery = { + pauseContinuations: () => { for (const scope of chatQueueService!.continuationScopes()) chatQueueService!.pause(scope) }, + incompleteRunIds: () => runs.listIncomplete().map(run => run.id), + cancelRun: (id: string, reason: string) => runner.cancel(id, reason), + suspendConnectors: () => connectorService.suspendPlatform(), + resumeConnectors: () => connectorService.resumePlatform(), + invalidateSessions: () => sessions.invalidateAll(), + isStopping: () => productStopping, + } return { + commands, + createProductConnection, + createLocalProductConnection, + assertPlatformAvailable, + suspendPlatform: reason => platformLifecycle.suspend(platformRecovery, reason), + resumePlatform: () => platformLifecycle.resume(platformRecovery), + connectClient: (role, peer) => { + const disconnect = clients.connect(role, peer, registerClient) + if (!commands || !commandContext || role !== 'product' || !options.productIdentity) + return disconnect + try { + const stopProduct = registerLocalProductConnection(peer, transport => createLocalProductConnection({ + ...transport, + connectionId: randomUUID(), + assertCurrent: () => { + if (peer.closed) + throw new ProductProtocolError('NOT_AUTHORIZED') + }, + })) + return () => { + stopProduct() + disconnect() + } + } + catch (error) { + disconnect() + throw error + } + }, startMaintenance: () => options.eventLog.startMaintenance({ isBusy }), async dispose() { + productStopping = true + for (const [actorId, scope] of actorProductScopes) { + scope.sync.stopping() + scope.sync.dispose() + actorCommandScopes.get(actorId)!.stopAccepting() + } + for (const connection of productConnections) connection.close() + for (const scope of actorCommandScopes.values()) scope.stopAccepting() + clients.stopAccepting() try { await options.eventLog.stopMaintenance() } @@ -1910,6 +2139,9 @@ export async function startBuddyService( prepareIdleShutdown() { if (isBusy()) return false + productStopping = true + for (const scope of actorCommandScopes.values()) scope.stopAccepting() + clients.stopAccepting() void options.eventLog.stopMaintenance().catch(() => record({ event: 'runtime.maintenance.stop_failed', level: 'error' })) void composerDraftService.dispose() void chatTurnService.dispose() diff --git a/apps/buddy/service/src/LocalServiceDaemon.ts b/apps/buddy/service/src/LocalServiceDaemon.ts new file mode 100644 index 000000000..2053dcf8c --- /dev/null +++ b/apps/buddy/service/src/LocalServiceDaemon.ts @@ -0,0 +1,436 @@ +import type { serviceRuntimeStateSchema } from '../../shared/service/localServiceProtocol' +import type { startServiceRuntime } from './startServiceRuntime' +import { randomBytes, randomUUID } from 'node:crypto' +import { dirname, join } from 'node:path' +import process from 'node:process' +import { z } from 'zod' +import { currentPlatform } from '../../platform/currentPlatform' +import { localTransports } from '../../platform/ipc/localTransport' +import { createBuddyNativeEnvironment, resolveBuddyProcessControl } from '../../platform/native/nativeHost' +import { createProxyEnvironment } from '../../platform/process/proxyEnvironment' +import { HostConnection } from '../../platform/service/HostConnection' +import { LocalServiceServer } from '../../platform/service/LocalServiceServer' +import { ServiceChildProcess } from '../../platform/service/ServiceChildProcess' +import { readServiceLaunch } from '../../platform/service/serviceFiles' +import { ServiceProfileOwner } from '../../platform/service/ServiceProfileOwner' +import { ServiceRpcChannel } from '../../platform/service/ServiceRpcChannel' +import { ApplicationEvents } from '../../shared/observability/ApplicationEvents' +import { desktopOpenIntentSchema, platformHostReadySchema, serviceIdentitySchema, serviceMaintenanceTicketSchema, serviceStopReasonSchema } from '../../shared/service/localServiceProtocol' +import { createRpcRuntimePlatform } from './platform/createRpcRuntimePlatform' +import { prepareNodeService } from './platform/nodeServiceConfig' +import { startServiceRuntime as startRuntime } from './startServiceRuntime' +import { validateServiceRelease } from './validateServiceRelease' + +type Runtime = Awaited> +interface ServiceMaintenance { + token: string + hostId: string + kind: 'backup' | 'restart' + phase: 'stopping' | 'stopped' | 'resuming' | 'failed' + pending?: Promise +} + +interface HostGeneration { + id: string + server: LocalServiceServer + child: ServiceChildProcess + peer?: ServiceRpcChannel + intentional: boolean + cancelReady: () => void +} + +export async function startLocalServiceDaemon(path: string, options: { restartDesktop?: () => void } = {}) { + const config = await readServiceLaunch(path) + await validateServiceRelease(config) + const nativePaths = { appPath: config.resourcesRoot, resourcesPath: config.resourcesRoot, isPackaged: true } + const processControl = resolveBuddyProcessControl(nativePaths) + const owner = await ServiceProfileOwner.acquire(config, processControl) + const identity = serviceIdentitySchema.parse(serviceIdentitySchema.strip().parse(owner.discovery)) + const host = new HostConnection() + const events = new ApplicationEvents() + let runtime: Runtime | undefined + let generation: HostGeneration | undefined + let stopping: Promise | undefined + let recovering: Promise | undefined + let failures = 0 + let restartRequested = false + let exitingHost: string | undefined + let runtimeGeneration = randomUUID() + let readyHost: z.infer | undefined + let builtinSkillsDirectories: readonly string[] = [] + let maintenance: ServiceMaintenance | undefined + const attachedClients = new Set() + function publishRuntime(status: z.infer['status'], errorCode: string | null = null) { + if (host.available) + host.notify('service.runtimeState', { generation: runtimeGeneration, status, maintenance: maintenance?.kind === 'backup' ? 'backup' : null, errorCode }) + } + async function createRuntime() { + const value = await startRuntime({ buddyHome: join(config.profileRoot, 'buddy'), builtinSkillsDirectories, proxy: readyHost!.proxy, platform: createRpcRuntimePlatform(host), events, serviceInstanceId: runtimeGeneration, storageAuthority: config.storageAuthority, notify: (method, params) => { + if (host.available) + host.notify(method, params) + }, onFatalFailure: (error) => { + terminate(error) + } }) + runtime = value + value.startMaintenance() + return value + } + async function requestStop(raw: unknown, fromHost = false) { + const input = z.object({ expectedServiceInstanceId: z.literal(identity.serviceInstanceId), reason: serviceStopReasonSchema, commandId: z.uuid() }).strict().parse(raw) + if (runtime?.commands) { + const commands = runtime.commands + const result = await commands.control.execute('runtime.stop', { dataGeneration: commands.identity.dataGeneration, expectedServiceInstanceId: commands.identity.serviceInstanceId, commandId: input.commandId, intent: { targetServiceInstanceId: commands.identity.serviceInstanceId, scope: 'stop_service' } }) + if (result.receipt.decision !== 'accepted') + throw new Error('SERVICE_STOP_REJECTED') + await commands.control.waitForEffects() + } + if (generation) { + generation.intentional = true + if (fromHost) + exitingHost = generation.id + } + setImmediate(() => { + void stop().catch(failed) + }) + return { accepted: true } + } + host.onRequest('service.exitDesktop', raw => requestStop(raw, true)) + host.onRequest('service.restartDesktop', async (raw) => { + z.object({ expectedServiceInstanceId: z.literal(identity.serviceInstanceId) }).strict().parse(raw) + if (!options.restartDesktop || stopping || recovering || maintenance || owner.discovery.phase !== 'ready') + throw new Error('SERVICE_NOT_READY') + if (!restartRequested) { + restartRequested = true + setImmediate(() => options.restartDesktop!()) + } + return { accepted: true } + }) + host.onRequest('service.beginMaintenance', async (raw) => { + const input = z.object({ expectedServiceInstanceId: z.literal(identity.serviceInstanceId), kind: z.enum(['backup', 'restart']) }).strict().parse(raw) + if (stopping || recovering || maintenance || !runtime || owner.discovery.phase !== 'ready' || !host.instanceId) + throw new Error('SERVICE_NOT_READY') + if (input.kind === 'backup' && !runtime.prepareIdleShutdown()) + throw new Error('SNAPSHOT_RUNTIME_BUSY') + const ticket: ServiceMaintenance = { token: randomUUID(), hostId: host.instanceId, kind: input.kind, phase: 'stopping' } + maintenance = ticket + const previous = runtime + publishRuntime('stopping') + for (const peer of attachedClients) peer.close(new Error('SERVICE_MAINTENANCE')) + ticket.pending = (async () => { + await owner.publish('maintenance') + await previous.dispose() + runtime = undefined + ticket.phase = 'stopped' + publishRuntime('stopped') + })().catch((error) => { + ticket.phase = 'failed' + publishRuntime('offline', 'SERVICE_MAINTENANCE_FAILED') + throw error + }) + await ticket.pending + return { token: ticket.token, generation: runtimeGeneration } + }) + host.onRequest('service.endMaintenance', async (raw) => { + const input = serviceMaintenanceTicketSchema.parse(raw) + const ticket = maintenance + if (!ticket || ticket.token !== input.token || runtimeGeneration !== input.generation || host.instanceId !== ticket.hostId || ticket.phase !== 'stopped' || stopping) + throw new Error('SERVICE_MAINTENANCE_CHANGED') + ticket.phase = 'resuming' + runtimeGeneration = randomUUID() + publishRuntime('starting') + ticket.pending = (async () => { + await createRuntime() + if (stopping || host.instanceId !== ticket.hostId) + throw new Error('SERVICE_MAINTENANCE_CHANGED') + maintenance = undefined + await owner.publish('ready') + publishRuntime('ready') + })().catch((error) => { + maintenance = ticket + ticket.phase = 'failed' + publishRuntime('offline', 'SERVICE_MAINTENANCE_FAILED') + throw error + }) + await ticket.pending + return { generation: runtimeGeneration } + }) + let resolveRuntime!: (runtime: Runtime) => void + let rejectRuntime!: (error: unknown) => void + const available = new Promise((resolve, reject) => { + resolveRuntime = resolve + + rejectRuntime = reject + }) + void available.catch(() => {}) + const management = new LocalServiceServer({ + endpoint: endpoint(owner.discovery.managementEndpoint), + identity, + role: 'management', + secret: owner.managementSecret, + connected(frames) { + const peer = new ServiceRpcChannel(frames) + peer.onRequest('service.status', (raw) => { + z.object({}).strict().parse(raw) + return owner.discovery + }) + let attached = false + peer.onRequest('service.attachDesktop', async (raw, signal) => { + if (attached) + throw new Error('SERVICE_CLIENT_ALREADY_ATTACHED') + attached = true + z.object({ expectedServiceInstanceId: z.literal(identity.serviceInstanceId) }).strict().parse(raw) + if (stopping || recovering || maintenance) + throw new Error('SERVICE_NOT_READY') + const service = runtime ?? await available + signal?.throwIfAborted() + if (stopping || peer.closed || service !== runtime || maintenance) + throw new Error('SERVICE_NOT_READY') + attachedClients.add(peer) + peer.onDidClose(() => attachedClients.delete(peer)) + service.connectClient('product', peer) + service.connectClient('management', peer) + return { serviceInstanceId: identity.serviceInstanceId, pid: process.pid, generation: runtimeGeneration } + }) + peer.onRequest('service.openDesktop', async (raw, signal) => { + const input = z.object({ expectedServiceInstanceId: z.literal(identity.serviceInstanceId), intent: desktopOpenIntentSchema }).strict().parse(raw) + if (owner.discovery.phase !== 'ready' || !generation?.peer) + throw new Error('SERVICE_NOT_READY') + return generation.peer.request('host.openDesktop', input.intent, 25000, signal) + }) + peer.onRequest('service.stop', async (raw) => { + return requestStop(raw) + }) + }, + }) + const product = new LocalServiceServer({ + endpoint: endpoint(owner.discovery.productEndpoint), + identity, + role: 'product', + secret: owner.productSecret, + connected(frames, connectionId) { + if (!runtime || stopping || owner.discovery.phase !== 'ready') + throw new Error('SERVICE_NOT_READY') + const connection = runtime.createLocalProductConnection({ connectionId, send: frame => frames.send(frame), close: () => frames.close(), assertCurrent: () => { + if (stopping) + throw new Error('SERVICE_STOPPING') + } }) + frames.onFrame((frame) => { + void connection.receive(frame) + }) + frames.onDidClose(() => connection.close()) + }, + }) + function endpoint(address: string) { + return localTransports[currentPlatform.transport](address, { ownsProfileLease: () => true }) + } + function reportFailure(error: unknown) { + process.stderr.write(`Lexora service unavailable: ${error instanceof Error && /^[A-Z_]+$/.test(error.message) ? error.message : 'SERVICE_FAILED'}\n`) + } + function failed(error: unknown) { + reportFailure(error) + void owner.publish('failed').catch(() => {}) + } + function terminate(error: unknown) { + reportFailure(error) + void stop().then(() => { + process.exitCode = 1 + }).catch(failed) + } + async function launchHost() { + if (stopping) + throw new Error('SERVICE_STOPPING') + const id = randomUUID() + const secret = randomBytes(32).toString('hex') + const address = process.platform === 'win32' ? `\\\\.\\pipe\\lexora-${id}-host` : join(dirname(owner.discovery.managementEndpoint), `${id.slice(0, 8)}-host.sock`) + let accept!: (value: z.infer) => void + let reject!: (reason: unknown) => void + const ready = new Promise>((resolve, fail) => { + accept = resolve + + reject = fail + }) + void ready.catch(() => {}) + const timeout = setTimeout(() => reject(new Error('PLATFORM_HOST_READINESS_TIMEOUT')), 120000) + let current: HostGeneration | undefined + const server = new LocalServiceServer({ endpoint: endpoint(address), identity, role: 'host', secret, singleUse: true, connected(frames) { + const peer = new ServiceRpcChannel(frames) + if (!current) + throw new Error('PLATFORM_HOST_CHANGED') + current.peer = peer + host.attach(id, peer) + peer.onRequest('host.ready', (raw) => { + const input = platformHostReadySchema.parse(raw) + if (input.hostInstanceId !== id) + throw new Error('PLATFORM_HOST_CHANGED') + accept(input) + return { accepted: true } + }) + const connected = current + peer.onDidClose(() => { + reject(new Error('PLATFORM_HOST_UNAVAILABLE')) + lost(connected) + }) + } }) + try { + await server.listen() + if (stopping) + throw new Error('SERVICE_STOPPING') + const child = new ServiceChildProcess({ executable: config.hostExecutable, args: [...config.hostArguments, '--lexora-platform-host'], processControl, runtimeGuard: createBuddyNativeEnvironment(nativePaths).LEXORA_BUDDY_RUNTIME_GUARD, env: { ...process.env, ...createBuddyNativeEnvironment(nativePaths), LEXORA_HOME: config.defaultRoot ?? config.profileRoot, LEXORA_BUDDY_PROFILE: config.profile, LEXORA_BUDDY_STORAGE_AUTHORITY: config.storageAuthority }, output: (_stream, value) => process.stderr.write(value) }) + current = { id, server, child, intentional: false, cancelReady: () => reject(new Error('SERVICE_STOPPING')) } + generation = current + void child.exited.then(() => { + reject(new Error('PLATFORM_HOST_EXITED')) + + lost(current!) + }, reject) + await child.ready({ version: 1, identity, profile: config.profile, profileRoot: config.profileRoot, resourcesRoot: config.resourcesRoot, endpoint: address, managementEndpoint: owner.discovery.managementEndpoint, managementSecret: owner.managementSecret, hostInstanceId: id, secret }) + return await ready + } + catch (error) { + if (current) { + current.intentional = true + host.detach(id) + await current.child.stopAndVerify() + } + await server.dispose() + if (generation === current) + generation = undefined + throw error + } + finally { + clearTimeout(timeout) + } + } + function lost(current: HostGeneration) { + if (current.intentional || current !== generation || stopping) + return + current.intentional = true + host.detach(current.id) + if (recovering) + return + if (maintenance) { + maintenance.phase = 'failed' + terminate(new Error('PLATFORM_HOST_UNAVAILABLE')) + return + } + if (!runtime) + return + recovering = Promise.resolve().then(async () => { + await owner.publish('recovering') + updateProxy('http://127.0.0.1:0') + const suspended = runtime!.suspendPlatform('PLATFORM_HOST_UNAVAILABLE') + void suspended.catch(() => {}) + await current.child.stopAndVerify() + await current.server.dispose() + generation = undefined + await suspended + while (failures < 3) { + if (stopping) + return + failures++ + let ready: z.infer + try { + ready = await launchHost() + } + catch (error) { + if (generation) + throw error + continue + } + if (stopping) + return + readyHost = ready + updateProxy(ready.proxy.httpProxy) + await runtime!.resumePlatform(ready.proxy) + if (host.instanceId !== ready.hostInstanceId) + throw new Error('PLATFORM_HOST_UNAVAILABLE') + if (stopping) + return + await owner.publish('ready') + if (host.instanceId !== ready.hostInstanceId) + throw new Error('PLATFORM_HOST_UNAVAILABLE') + publishRuntime('ready') + return + } + if (!stopping) + throw new Error('PLATFORM_HOST_RECOVERY_EXHAUSTED') + }).catch(terminate).finally(() => { + recovering = undefined + }) + } + async function stop(): Promise { + return stopping ??= (async () => { + const errors: unknown[] = [] + const phase = async (operation: () => Promise, milliseconds: number) => { + let timer: ReturnType | undefined + try { + await Promise.race([operation(), new Promise((_resolve, reject) => { + timer = setTimeout(() => reject(new Error('SERVICE_STOP_PHASE_TIMEOUT')), milliseconds) + })]) + } + catch (error) { + errors.push(error) + } + finally { + clearTimeout(timer) + } + } + generation?.cancelReady() + await owner.publish('stopping') + await phase(() => product.dispose(), 2000) + await phase(async () => recovering, 15000) + await phase(async () => maintenance?.pending, 30000) + await phase(async () => runtime?.dispose(), 20000) + if (!errors.length && host.available) + host.notify('service.executionStopped', {}) + if (generation) { + generation.intentional = true + const current = generation + await phase(async () => { + if (exitingHost === current.id) + await current.child.exited + else if (current.peer && !current.peer.closed) + await current.peer.request('host.stop', {}, 10000) + }, exitingHost === current.id ? 20000 : 11000) + await phase(() => current.child.stopAndVerify(), 12000) + host.detach(current.id) + await phase(() => current.server.dispose(), 2000) + } + host.close(new Error('SERVICE_STOPPED')) + if (errors.length) { + await owner.publish('failed') + throw new AggregateError(errors, 'SERVICE_STOP_UNCONFIRMED') + } + await management.dispose() + await owner.dispose() + })() + } + try { + await management.listen() + const ready = await launchHost() + updateProxy(ready.proxy.httpProxy) + readyHost = ready + builtinSkillsDirectories = await prepareNodeService({ profile: config.profile, buddyHome: join(config.profileRoot, 'buddy'), resourcesRoot: config.resourcesRoot, preferences: ready.preferences, proxy: ready.proxy, display: ready.display, storageAuthority: config.storageAuthority }) + await createRuntime() + if (host.instanceId !== ready.hostInstanceId) + throw new Error('PLATFORM_HOST_UNAVAILABLE') + resolveRuntime(runtime!) + await product.listen() + await owner.publish('ready') + publishRuntime('ready') + return { config, discovery: owner.discovery, stop, openDesktop: () => generation!.peer!.request('host.openDesktop', { kind: 'tasks' }, 25000) } + } + catch (error) { + rejectRuntime(error) + await stop().catch(failed) + throw error + } +} + +function updateProxy(url: string): void { + for (const key of Object.keys(process.env)) { + if (/^(?:https?|all|no)_proxy$/i.test(key)) + delete process.env[key] + } + Object.assign(process.env, createProxyEnvironment(url), { NODE_USE_ENV_PROXY: '1' }) +} diff --git a/apps/buddy/service/src/LocalServiceSupervisor.ts b/apps/buddy/service/src/LocalServiceSupervisor.ts new file mode 100644 index 000000000..d484ca338 --- /dev/null +++ b/apps/buddy/service/src/LocalServiceSupervisor.ts @@ -0,0 +1,78 @@ +import type { ServiceLaunch } from '../../shared/service/localServiceProtocol' +import { realpath } from 'node:fs/promises' +import { join } from 'node:path' +import process from 'node:process' +import { createBuddyNativeEnvironment, resolveBuddyProcessControl } from '../../platform/native/nativeHost' +import { ServiceChildProcess } from '../../platform/service/ServiceChildProcess' +import { readServiceLaunch, writePrivateServiceJson } from '../../platform/service/serviceFiles' +import { assertStorageIdentity, storageBootstrapRoot, StorageLocationStore } from '../../platform/storage/StorageLocationStore' +import { startLocalServiceDaemon } from './LocalServiceDaemon' +import { validateServiceRelease } from './validateServiceRelease' + +export function startLocalServiceSupervisor(path: string) { + let active: Awaited> | undefined + let transition: Promise | undefined + let maintenance: ServiceChildProcess | undefined + let stopping = false + async function boot(openDesktop: boolean) { + const config = await readServiceLaunch(path) + await validateServiceRelease(config) + if (stopping) + return + const defaultRoot = config.defaultRoot ?? config.profileRoot + const store = new StorageLocationStore(join(storageBootstrapRoot(defaultRoot), 'location.json'), defaultRoot) + if ((await store.read()).pending) + await migrate(config, defaultRoot) + if (stopping) + return + const state = await store.read() + if (state.pending) + throw new Error('SERVICE_STORAGE_MAINTENANCE_PENDING') + await assertStorageIdentity(state) + const profileRoot = await realpath(state.activeRoot) + await writePrivateServiceJson(path, { ...config, profileRoot, defaultRoot }) + active = await startLocalServiceDaemon(path, { restartDesktop }) + if (stopping) + await active.stop() + else if (openDesktop) + await active.openDesktop() + } + async function migrate(config: ServiceLaunch, defaultRoot: string) { + const native = { appPath: config.resourcesRoot, resourcesPath: config.resourcesRoot, isPackaged: true } + const environment = createBuddyNativeEnvironment(native) + const child = new ServiceChildProcess({ executable: config.hostExecutable, args: [...config.hostArguments, '--lexora-storage-maintenance'], env: { ...process.env, ...environment, LEXORA_HOME: defaultRoot, LEXORA_BUDDY_PROFILE: config.profile, LEXORA_BUDDY_SERVICE_MODE: 'utility' }, processControl: resolveBuddyProcessControl(native), runtimeGuard: environment.LEXORA_BUDDY_RUNTIME_GUARD, output: (_stream, value) => process.stderr.write(value) }) + maintenance = child + try { + await child.ready({}) + const result = await child.exited + await child.stopAndVerify() + if (!stopping && result.code !== 0) + throw new Error('SERVICE_STORAGE_MAINTENANCE_FAILED') + } + finally { maintenance = undefined } + } + function restartDesktop() { + if (stopping || transition) + return + transition = (async () => { + await active?.stop() + active = undefined + await boot(true) + })().catch((error: unknown) => { + const code = error instanceof Error && /^[A-Z_]+$/.test(error.message) ? error.message : 'SERVICE_RESTART_FAILED' + process.stderr.write(`${code}\n`) + if (!active && code !== 'SERVICE_STORAGE_MAINTENANCE_PENDING') + process.exitCode = 1 + }).finally(() => { transition = undefined }) + } + const ready = boot(false) + return { + ready, + async stop() { + stopping = true + await maintenance?.stopAndVerify() + await Promise.allSettled([ready, transition]) + await active?.stop() + }, + } +} diff --git a/apps/buddy/service/src/__tests__/LocalServiceDaemon.spec.ts b/apps/buddy/service/src/__tests__/LocalServiceDaemon.spec.ts new file mode 100644 index 000000000..020a3e792 --- /dev/null +++ b/apps/buddy/service/src/__tests__/LocalServiceDaemon.spec.ts @@ -0,0 +1,233 @@ +import type { RuntimeRequestHandler } from '../../../shared/runtime/rpcPeer' +import process from 'node:process' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { ServiceRpcChannel } from '../../../platform/service/ServiceRpcChannel' +import { Emitter } from '../../../shared/events/Emitter' +import { startLocalServiceDaemon } from '../LocalServiceDaemon' + +const fixture = vi.hoisted(() => ({ + servers: [] as any[], + children: [] as any[], + phases: [] as string[], + failures: new Set(), + cleanupFailures: new Set(), + unready: new Set(), + ownerReleased: false, + disposed: false, + suspended: 0, + resumed: 0, + resume: undefined as (() => Promise) | undefined, +})) + +class Frames { + readonly messages = new Emitter(() => {}) + readonly closes = new Emitter(() => {}) + readonly onDidClose = this.closes.event + opposite!: Frames + closed = false + transport() { + return { + postMessage: (message: unknown) => this.opposite.messages.fire(message), + subscribe: (listener: (message: unknown) => void) => this.messages.event(listener).dispose, + } + } + + close(error: Error) { + if (this.closed) + return + this.closed = true + this.closes.fire(error) + this.opposite.close(error) + } +} + +vi.mock('../../../platform/ipc/localTransport', () => ({ localTransports: new Proxy({}, { get: () => (address: string) => ({ address }) }) })) +vi.mock('../../../platform/native/nativeHost', () => ({ createBuddyNativeEnvironment: () => ({}), resolveBuddyProcessControl: () => '/fixture/helper' })) +vi.mock('../../../platform/service/serviceFiles', () => ({ readServiceLaunch: async () => ({ profileRoot: '/fixture/profile', resourcesRoot: '/fixture/resources', hostExecutable: '/fixture/host', hostArguments: [], profile: 'test', storageAuthority: 'sqlite' }) })) +vi.mock('../validateServiceRelease', () => ({ validateServiceRelease: async () => {} })) +vi.mock('../platform/createRpcRuntimePlatform', () => ({ createRpcRuntimePlatform: () => ({}) })) +vi.mock('../platform/nodeServiceConfig', () => ({ prepareNodeService: async () => [] })) +vi.mock('../startServiceRuntime', () => ({ startServiceRuntime: async () => ({ + startMaintenance() {}, + async dispose() { fixture.disposed = true }, + async suspendPlatform() { fixture.suspended++ }, + async resumePlatform() { + fixture.resumed++ + await fixture.resume?.() + }, +}) })) +vi.mock('../../../platform/service/ServiceProfileOwner', () => ({ ServiceProfileOwner: { acquire: async () => ({ + discovery: { protocol: 1, profileId: 'profile', serviceInstanceId: '09f1e2ae-4b53-4730-b4e9-160bd5b14da3', buildId: 'a'.repeat(64), sessionId: 'session', userId: 'user', managementEndpoint: '/fixture/management.sock', productEndpoint: '/fixture/product.sock', phase: 'starting' }, + managementSecret: 'b'.repeat(64), + productSecret: 'c'.repeat(64), + async publish(phase: string) { + this.discovery.phase = phase + fixture.phases.push(phase) + }, + async dispose() { fixture.ownerReleased = true }, +}) } })) +vi.mock('../../../platform/service/LocalServiceServer', () => ({ LocalServiceServer: class { + disposed = false + readonly options: any + constructor(options: any) { + this.options = options + fixture.servers.push(this) + } + + async listen() {} + async dispose() { this.disposed = true } +} })) +vi.mock('../../../platform/service/ServiceChildProcess', () => ({ ServiceChildProcess: class { + readonly ordinal = fixture.children.length + readonly exited: Promise<{ code: number, signal: null }> + exit!: () => void + peer?: ServiceRpcChannel + clean = false + constructor() { + fixture.children.push(this) + this.exited = new Promise(resolve => this.exit = () => resolve({ code: 1, signal: null })) + } + + async ready(bootstrap: any) { + if (fixture.unready.has(this.ordinal)) + return + if (fixture.failures.has(this.ordinal)) + throw new Error('PLATFORM_HOST_START_FAILED') + const service = new Frames() + const client = new Frames() + service.opposite = client + client.opposite = service + this.peer = new ServiceRpcChannel(client as never) + fixture.servers.find(server => server.options.endpoint.address === bootstrap.endpoint).options.connected(service) + this.peer.onRequest('host.stop', (() => ({ accepted: true })) satisfies RuntimeRequestHandler) + await this.peer.request('host.ready', { hostInstanceId: bootstrap.hostInstanceId, preferences: { cacheWarming: 'off', codemode: 'off', codemodeWorkerLimit: 2, codemodeOverflow: 'wait', modelRetryLimit: 2 }, display: true, proxy: { httpProxy: 'http://127.0.0.1:10000', httpsProxy: 'http://127.0.0.1:10000', noProxy: '' } }) + } + + async stopAndVerify() { + if (fixture.cleanupFailures.has(this.ordinal)) + throw new Error('SERVICE_CHILD_CLEANUP_UNCONFIRMED') + this.clean = true + this.exit() + this.peer?.close(new Error('HOST_STOPPED')) + } +} })) + +let daemon: Awaited> | undefined +let exitCode: typeof process.exitCode +let environment: NodeJS.ProcessEnv +beforeEach(() => { + exitCode = process.exitCode + environment = { ...process.env } + Object.assign(fixture, { servers: [], children: [], phases: [], failures: new Set(), cleanupFailures: new Set(), unready: new Set(), ownerReleased: false, disposed: false, suspended: 0, resumed: 0, resume: undefined }) +}) +afterEach(async () => { + await daemon?.stop().catch(() => {}) + daemon = undefined + process.exitCode = exitCode + process.env = environment +}) + +const readyAgain = () => expect.poll(() => fixture.phases).toEqual(['ready', 'recovering', 'ready']) + +describe('local service host recovery', () => { + it.each(['process', 'socket'])('recovers exactly once when %s loss arrives first', async (first) => { + daemon = await startLocalServiceDaemon('/fixture/launch.json') + const original = fixture.children[0] + if (first === 'process') + original.exit() + else + original.peer.close(new Error('CONNECTION_LOST')) + await readyAgain() + expect(fixture.children).toHaveLength(2) + expect(fixture.suspended).toBe(1) + expect(fixture.resumed).toBe(1) + expect(original.clean).toBe(true) + expect(fixture.ownerReleased).toBe(false) + expect(process.exitCode).toBe(exitCode) + original.exit() + await Promise.resolve() + expect(fixture.children).toHaveLength(2) + }) + + it('retries a replacement only after its cleanup is confirmed', async () => { + fixture.failures.add(1) + daemon = await startLocalServiceDaemon('/fixture/launch.json') + fixture.children[0].exit() + await readyAgain() + expect(fixture.children).toHaveLength(3) + expect(fixture.children.slice(0, 2).every(child => child.clean)).toBe(true) + expect(fixture.resumed).toBe(1) + expect(fixture.disposed).toBe(false) + }) + + it('releases the profile and fails the process after three unsuccessful replacements', async () => { + fixture.failures = new Set([1, 2, 3]) + daemon = await startLocalServiceDaemon('/fixture/launch.json') + fixture.children[0].exit() + await expect.poll(() => process.exitCode).toBe(1) + expect(fixture.children).toHaveLength(4) + expect(fixture.children.every(child => child.clean)).toBe(true) + expect(fixture.servers.every(server => server.disposed)).toBe(true) + expect(fixture.disposed).toBe(true) + expect(fixture.ownerReleased).toBe(true) + expect(fixture.phases).toEqual(['ready', 'recovering', 'stopping']) + }) + + it('retains the lease and stops replacement attempts when cleanup is unconfirmed', async () => { + fixture.failures.add(1) + fixture.cleanupFailures.add(1) + daemon = await startLocalServiceDaemon('/fixture/launch.json') + fixture.children[0].exit() + await expect.poll(() => fixture.phases.at(-1)).toBe('failed') + await expect(daemon.stop()).rejects.toThrow('SERVICE_STOP_UNCONFIRMED') + expect(fixture.children).toHaveLength(2) + expect(fixture.ownerReleased).toBe(false) + expect(process.exitCode).toBe(exitCode) + }) + + it('does not publish ready when a replacement disconnects while the runtime resumes', async () => { + fixture.resume = async () => { + fixture.children.at(-1).peer.close(new Error('CONNECTION_LOST')) + } + daemon = await startLocalServiceDaemon('/fixture/launch.json') + fixture.children[0].exit() + await expect.poll(() => process.exitCode).toBe(1) + expect(fixture.phases).toEqual(['ready', 'recovering', 'stopping']) + expect(fixture.ownerReleased).toBe(true) + }) + + it('cancels a replacement readiness wait before awaiting recovery during shutdown', async () => { + fixture.unready.add(1) + daemon = await startLocalServiceDaemon('/fixture/launch.json') + fixture.children[0].exit() + await expect.poll(() => fixture.children.length).toBe(2) + await daemon.stop() + expect(fixture.children.every(child => child.clean)).toBe(true) + expect(fixture.servers.every(server => server.disposed)).toBe(true) + expect(fixture.ownerReleased).toBe(true) + expect(fixture.phases).toEqual(['ready', 'recovering', 'stopping']) + expect(fixture.resumed).toBe(0) + expect(process.exitCode).toBe(exitCode) + }, 2000) + + it('keeps the lease when a cancelled replacement cannot confirm child cleanup', async () => { + fixture.unready.add(1) + fixture.cleanupFailures.add(1) + daemon = await startLocalServiceDaemon('/fixture/launch.json') + fixture.children[0].exit() + await expect.poll(() => fixture.children.length).toBe(2) + await expect(daemon.stop()).rejects.toThrow('SERVICE_STOP_UNCONFIRMED') + expect(fixture.ownerReleased).toBe(false) + expect(fixture.children).toHaveLength(2) + }, 2000) + + it('does not launch a replacement after shutdown starts during recovery', async () => { + daemon = await startLocalServiceDaemon('/fixture/launch.json') + fixture.children[0].exit() + await Promise.resolve() + await daemon.stop() + expect(fixture.children).toHaveLength(1) + expect(fixture.ownerReleased).toBe(true) + expect(process.exitCode).toBe(exitCode) + }) +}) diff --git a/apps/buddy/service/src/__tests__/buddyService.integration.spec.ts b/apps/buddy/service/src/__tests__/buddyService.integration.spec.ts index 5e40a818c..4bdb6d96d 100644 --- a/apps/buddy/service/src/__tests__/buddyService.integration.spec.ts +++ b/apps/buddy/service/src/__tests__/buddyService.integration.spec.ts @@ -12,6 +12,7 @@ import type { LocalBuddy } from '../../../shared/buddies/buddyApi' import type { BuddyComposerResource } from '../../../shared/conversation/composerResource' import type { ApplicationDiagnostic } from '../../../shared/diagnostics/applicationDiagnostic' +import type { StartBuddyServiceOptions } from '../BuddyService' import type { BuddyServiceRpcServer } from '../rpc/BuddyServiceRpcServer' import { Buffer } from 'node:buffer' import { appendFile, mkdir, mkdtemp, readFile, realpath, rm, unlink, writeFile } from 'node:fs/promises' @@ -20,15 +21,20 @@ import { tmpdir } from 'node:os' import { join } from 'node:path' import { SessionManager } from '@earendil-works/pi-coding-agent' import { afterEach, describe, expect, it } from 'vitest' +import { ProductDesktopGateway } from '../../../electron/main/productProtocol/ProductDesktopGateway' import { DEFAULT_BUDDY_SETTINGS } from '../../../shared/buddies/buddySettings' -import { createBuddyUserContent } from '../../../shared/conversation/buddyUserContent' +import { createBuddyUserContent } from '../../../shared/conversation/buddyUserContent' +import { Emitter } from '../../../shared/events/Emitter' import { DEFAULT_NETWORK_PERMISSIONS } from '../../../shared/permissions/networkPermissions' import { DEFAULT_RUNTIME_PREFERENCES } from '../../../shared/runtime/runtimePreferences' import { inspectCommittedPiCompaction } from '../agent/sessions/recovery/inspectCommittedPiCompaction' import { readCheckpoint } from '../agent/sessions/tree/BuddyTreeStore' -import { startBuddyService } from '../BuddyService' +import { startBuddyService as startService } from '../BuddyService' import { createRunEventLog } from '../events/createRunEventLog' +import { SqliteRunEventLog } from '../events/SqliteRunEventLog' +import { createRpcRuntimePlatform } from '../platform/createRpcRuntimePlatform' +import { createServiceConnection } from '../rpc/__tests__/serviceConnectionHarness' import { RunLifecycleService } from '../runs/RunLifecycleService' import { RunRecoveryService } from '../runs/RunRecoveryService' import { @@ -39,9 +45,10 @@ import { createExecutionStorageFixture, executionNow } from '../storage/__tests_ import { createApprovalRepository } from '../storage/approvalRepository' import { createArtifactRepository } from '../storage/artifactRepository' import { createAttachmentRepository } from '../storage/attachmentRepository' +import { createComposerDraftRepository } from '../storage/composerDraftRepository' import { createConversationDeletionRepository } from '../storage/conversationDeletionRepository' import { createConversationRepository } from '../storage/conversationRepository' -import { openBuddyDatabase } from '../storage/database' +import { openBuddyDatabase, openBuddyMigrationCandidate } from '../storage/database' import { createExecutionRepository } from '../storage/executionRepository' import { createRunRepository } from '../storage/runRepository' import { createSpaceRepository } from '../storage/spaceRepository' @@ -60,6 +67,280 @@ afterEach(async () => { }) describe('buddy runtime cross-subsystem contract', () => { + it('cancels current execution when its platform disappears and only admits new intent after recovery', async () => { + const root = await realpath(await mkdtemp(join(tmpdir(), 'lexora-platform-loss-'))) + directories.push(root) + const buddyHome = join(root, 'buddy') + const database = openTrackedDatabase(join(buddyHome, 'buddy.sqlite3')) + const eventLog = createRunEventLog({ conversationsDirectory: join(buddyHome, 'conversations'), database }) + const entered = Promise.withResolvers() + const release = Promise.withResolvers() + let requests = 0 + const provider = await startLocalModelProvider(async () => { + if (++requests === 1) { + entered.resolve() + await release.promise + } + return { text: 'Explicit new intent completed' } + }) + const harness = createRuntimeRpcHarness({ 'offline-provider': { type: 'api_key', key: 'test-api-key' } }) + const service = await startBuddyService({ buddyHome, database, eventLog, rpc: harness.rpc, builtinSkillsDirectories: [] }) + try { + await harness.invoke('providers.upsertCustom', offlineProviderConfig(provider.baseUrl)) + const first = await service.runtime.startTurn(await turnRequest(harness, { content: 'First execution', requestId: 'before-platform-loss' })) + await entered.promise + await service.suspendPlatform() + expect((await waitForTerminalRun(harness, first.runId)).status).toBe('cancelled') + const fresh = await turnRequest(harness, { conversationId: first.conversationId, branchId: first.branchId, content: 'Explicit new intent', requestId: 'after-platform-loss' }) + await expect(service.runtime.startTurn(fresh)).rejects.toMatchObject({ code: 'PLATFORM_HOST_UNAVAILABLE' }) + expect(requests).toBe(1) + release.resolve() + await service.resumePlatform() + expect(requests).toBe(1) + const second = await service.runtime.startTurn(fresh) + expect((await waitForTerminalRun(harness, second.runId)).status).toBe('completed') + expect(requests).toBe(2) + expect((await eventLog.read(first.runId)).filter(event => event.type === 'run.completed')).toEqual([]) + } + finally { + release.resolve() + await service.dispose() + await eventLog.close() + } + }, 30000) + + it('runs a candidate Core command through one SQLite writer and replays its receipt after disconnect', async () => { + const root = await realpath(await mkdtemp(join(tmpdir(), 'lexora-buddy-candidate-core-'))) + directories.push(root) + const buddyHome = join(root, 'buddy') + const database = openBuddyMigrationCandidate({ databasePath: join(buddyHome, 'buddy.sqlite3') }) + databases.push(database) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + const eventLog = new SqliteRunEventLog({ database }) + const harness = createRuntimeRpcHarness({ 'offline-provider': { type: 'api_key', key: 'test-api-key' } }) + const provider = await startLocalModelProvider(async () => ({ text: 'CANDIDATE_COMMAND_COMPLETED' })) + const service = await startBuddyService({ serviceInstanceId: 'candidate-instance', productIdentity: { serviceId: 'candidate-service', profileId: 'candidate-profile' }, buddyHome, database, eventLog, rpc: harness.rpc, builtinSkillsDirectories: [] }) + let product = createServiceConnection() + const disconnect = service.connectClient('product', product.server) + const productFrames: Array> = [] + product.client.onNotification((method, params) => { + if (method === 'product.frame') + productFrames.push(JSON.parse((params as { frame: string }).frame)) + }) + const exchange = async (method: string, params: unknown) => { + const id = String(productFrames.length + 1) + await product.client.request('product.exchange', { frame: JSON.stringify({ jsonrpc: '2.0', id, method, params }) }) + const frame = productFrames.find(frame => frame.id === id)! + if (frame.error) + throw new Error(JSON.stringify(frame.error)) + return frame.result + } + try { + await expect(product.client.request('runtime.stop', {})).rejects.toMatchObject({ code: -32601 }) + const initialized = await exchange('initialize', { protocol: { major: 1, minMinor: 0, maxMinor: 0 }, client: { kind: 'desktop', version: 'test' }, supportedContentKinds: ['buddy-user-content-v1'], supportedReviewKinds: [] }) + expect(initialized.profileId).toBe('candidate-profile') + expect(initialized.capabilities).toContain('composerDrafts.open') + await harness.invoke('providers.upsertCustom', offlineProviderConfig(provider.baseUrl)) + const initialSync = await exchange('sync.subscribe', { subscriptionId: 'task-index', topic: 'taskIndex' }) + expect(initialSync).toMatchObject({ mode: 'snapshot', items: [] }) + const projected = await harness.invoke('product.content.project', { content: createBuddyUserContent('Use the committed candidate command') }) as { content: unknown } + await exchange('composerDrafts.open', { + commandId: 'candidate-open', + dataGeneration: service.commands!.identity.dataGeneration, + expectedServiceInstanceId: 'candidate-instance', + intent: { draftId: 'candidate-draft', scope: { kind: 'global' }, expectedSlotRevision: '0', initialContent: projected.content, initialExecutionConfig: { approvalPolicy: 'policy', executionProfile: 'workspace_write' }, initialModelSelection: { modelId: 'offline-model', providerId: 'offline-provider', reasoning: null, serviceTier: null } }, + }) + const draft = createComposerDraftRepository(database).findById('candidate-draft')! + expect(await exchange('composerDrafts.get', { draftId: draft.draftId })).toMatchObject({ draftId: draft.draftId, revision: '0', content: { kind: 'editable' } }) + for (const method of ['chat.startTurn', 'composerDrafts.save', 'approvals.approve', 'conversations.rename']) + await expect(harness.invoke(method, {})).rejects.toMatchObject({ code: 'METHOD_NOT_AVAILABLE' }) + const commands = service.commands! + expect(commands.identity.serviceInstanceId).toBe('candidate-instance') + const input = { commandId: 'candidate-command', dataGeneration: commands.identity.dataGeneration, expectedServiceInstanceId: commands.identity.serviceInstanceId, intent: { draftId: draft.draftId, expectedRevision: draft.revision } } + const accepted = await exchange('chat.startTurn', { ...input, intent: { ...input.intent, expectedRevision: String(input.intent.expectedRevision) } }) + expect(await exchange('commands.get', { commandId: input.commandId, dataGeneration: input.dataGeneration })).toEqual({ lookup: 'decided', receipt: accepted.receipt }) + const runId = String((accepted.receipt as unknown as { target: { runId: string } }).target.runId) + const task = await exchange('conversations.get', { conversationId: accepted.receipt.target.conversationId }) + expect(typeof task.revision).toBe('string') + expect(task).not.toHaveProperty('metadataRevision') + expect(productFrames.some(frame => frame.method === 'sync.delta' && frame.params.changes[0]?.kind === 'upsert')).toBe(true) + disconnect() + product.close() + expect(await waitForTerminalRun(harness, runId)).toMatchObject({ status: 'completed' }) + product = createServiceConnection() + service.connectClient('product', product.server) + product.client.onNotification((method, params) => { + if (method === 'product.frame') + productFrames.push(JSON.parse((params as { frame: string }).frame)) + }) + await exchange('initialize', { protocol: { major: 1, minMinor: 0, maxMinor: 0 }, client: { kind: 'desktop', version: 'test' }, supportedContentKinds: ['buddy-user-content-v1'], supportedReviewKinds: [] }) + expect(await exchange('chat.startTurn', { ...input, intent: { ...input.intent, expectedRevision: String(input.intent.expectedRevision) } })).toEqual(accepted) + const events = await eventLog.read(runId) + expect(events[0]?.type).toBe('run.queued') + expect(events.map(event => event.sequence)).toEqual(events.map((_, index) => index + 1)) + expect(JSON.stringify(events)).toContain('CANDIDATE_COMMAND_COMPLETED') + expect(provider.requests).toHaveLength(1) + expect(database.prepare('SELECT count(*) AS count FROM command_receipts').get()).toEqual({ count: 2 }) + } + finally { + product.close() + await service.dispose() + await eventLog.close() + } + }) + + it('accepts immutable local input on its explicit branch and preserves deletion rejections', async () => { + const root = await realpath(await mkdtemp(join(tmpdir(), 'lexora-submitted-input-'))) + directories.push(root) + const buddyHome = join(root, 'buddy') + const database = openBuddyMigrationCandidate({ databasePath: join(buddyHome, 'buddy.sqlite3') }) + databases.push(database) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + const eventLog = new SqliteRunEventLog({ database }) + const harness = createRuntimeRpcHarness({ 'offline-provider': { type: 'api_key', key: 'test-api-key' } }) + const provider = await startLocalModelProvider(async () => ({ text: 'LOCAL_INPUT_COMPLETED' })) + const service = await startBuddyService({ serviceInstanceId: 'input-instance', productIdentity: { serviceId: 'input-service', profileId: 'input-profile' }, buddyHome, database, eventLog, rpc: harness.rpc, builtinSkillsDirectories: [] }) + try { + await harness.invoke('providers.upsertCustom', offlineProviderConfig(provider.baseUrl)) + const commands = service.commands! + const snapshot = { inputId: 'local-input', scope: { kind: 'task', draftId: 'local-input', spaceId: null }, content: createBuddyUserContent('Local immutable input 😀'), executionConfig: { approvalPolicy: 'policy', executionProfile: 'read_only' }, modelSelection: { modelId: 'offline-model', providerId: 'offline-provider', reasoning: null, serviceTier: null }, participants: { leaderId: 'default', actorIds: ['default'] } } + const input = { commandId: 'submit-local', dataGeneration: commands.identity.dataGeneration, expectedServiceInstanceId: commands.identity.serviceInstanceId, intent: { operation: 'start', input: snapshot } } + const accepted = await commands.input.execute('chat.submitInput', input) + expect(accepted.receipt.decision).toBe('accepted') + const target = accepted.receipt.target as { runId: string, conversationId: string, branchId: string } + expect(await waitForTerminalRun(harness, target.runId)).toMatchObject({ status: 'completed' }) + expect(database.prepare('SELECT count(*) AS count FROM composer_drafts').get()).toEqual({ count: 0 }) + expect(database.prepare('SELECT draft_id, draft_revision FROM turn_requests WHERE run_id = ?').get(target.runId)).toEqual({ draft_id: null, draft_revision: null }) + expect(provider.requests).toHaveLength(1) + expect(JSON.stringify(provider.requests)).toContain('Local immutable input') + expect(await commands.input.execute('chat.submitInput', { ...input, expectedServiceInstanceId: 'previous-instance' })).toEqual({ created: false, receipt: accepted.receipt }) + await expect(commands.input.execute('chat.submitInput', { ...input, intent: { ...input.intent, input: { ...snapshot, content: createBuddyUserContent('Changed intent') } } })).rejects.toMatchObject({ code: 'COMMAND_ID_REUSED' }) + const conversations = createConversationRepository(database) + conversations.createBranch({ id: 'other-branch', conversationId: target.conversationId, parentBranchId: target.branchId, forkedFromMessageId: conversations.listBranchMessages(target.conversationId, target.branchId)[0]!.id, createdAt: new Date().toISOString(), activate: true }) + const continued = { ...input, commandId: 'continue-local', intent: { operation: 'start', input: { ...snapshot, inputId: 'continue-input', scope: { kind: 'conversation_branch', conversationId: target.conversationId, branchId: target.branchId } } } } + const continuedResult = await commands.input.execute('chat.submitInput', continued) + expect(continuedResult.receipt).toMatchObject({ decision: 'accepted', target: { conversationId: target.conversationId, branchId: target.branchId } }) + expect(await waitForTerminalRun(harness, String(continuedResult.receipt.target.runId))).toMatchObject({ status: 'completed', branchId: target.branchId }) + expect(conversations.findById(target.conversationId)?.activeBranchId).toBe('other-branch') + expect(provider.requests).toHaveLength(2) + conversations.markDeleted(target.conversationId, new Date().toISOString()) + const stale = { ...continued, commandId: 'deleted-local', intent: { ...continued.intent, input: { ...continued.intent.input, inputId: 'deleted-input' } } } + const rejected = await commands.input.execute('chat.submitInput', stale) + expect(rejected.receipt).toMatchObject({ decision: 'rejected', errorCode: 'SOURCE_CHANGED' }) + database.prepare('UPDATE conversations SET deleted_at = NULL WHERE id = ?').run(target.conversationId) + expect(await commands.input.execute('chat.submitInput', stale)).toEqual({ created: false, receipt: rejected.receipt }) + expect(provider.requests).toHaveLength(2) + } + finally { + await service.dispose() + await eventLog.close() + } + }) + + it('adapts Desktop drafts and task receipts through the product connection without repeating execution', async () => { + const root = await realpath(await mkdtemp(join(tmpdir(), 'lexora-desktop-product-'))) + directories.push(root) + const buddyHome = join(root, 'buddy') + const database = openBuddyMigrationCandidate({ databasePath: join(buddyHome, 'buddy.sqlite3') }) + databases.push(database) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + const eventLog = new SqliteRunEventLog({ database }) + const harness = createRuntimeRpcHarness({ 'offline-provider': { type: 'api_key', key: 'test-api-key' } }) + const provider = await startLocalModelProvider(async () => ({ text: 'DESKTOP_PRODUCT_COMPLETED' })) + const service = await startBuddyService({ serviceInstanceId: 'desktop-instance', productIdentity: { serviceId: 'desktop-service', profileId: 'desktop-profile' }, buddyHome, database, eventLog, rpc: harness.rpc, builtinSkillsDirectories: [] }) + const product = createServiceConnection() + service.connectClient('product', product.server) + const gateway = new ProductDesktopGateway({ + state: { status: 'ready' }, + onNotification: listener => product.client.onNotification((method, params) => listener({ method, params })), + onStateChange: () => () => {}, + restart: async () => {}, + request: (method, params) => method === 'product.exchange' ? product.client.request(method, params) : harness.invoke(method, params), + }, join(root, 'outbox'), 'test') + try { + await harness.invoke('providers.upsertCustom', offlineProviderConfig(provider.baseUrl)) + const opened = await gateway.request('composerDrafts.open', { + draftId: 'desktop-draft', + scope: { kind: 'global' }, + initialContent: createBuddyUserContent('Original input'), + initialExecutionConfig: { approvalPolicy: 'policy', executionProfile: 'workspace_write' }, + initialModelSelection: { modelId: 'offline-model', providerId: 'offline-provider', reasoning: null, serviceTier: null }, + }) as { draftId: string, revision: number } + expect(opened).toMatchObject({ draftId: 'desktop-draft', revision: 0 }) + const draft = await gateway.request('composerDrafts.save', { draftId: opened.draftId, expectedRevision: 0, content: createBuddyUserContent('Desktop committed input'), modelSelection: { modelId: 'offline-model', providerId: 'offline-provider', reasoning: null, serviceTier: null }, executionConfig: { approvalPolicy: 'policy', executionProfile: 'workspace_write' } }) as { draftId: string, revision: number } + expect(draft.revision).toBe(1) + const input = { draftId: draft.draftId, expectedRevision: draft.revision, requestId: 'desktop-start' } + const turn = await gateway.request('chat.startTurn', input) as LocalTurnStart + expect(turn).toMatchObject({ runId: expect.any(String), draftReceipt: { sourceRevision: 1, committedRevision: 2 }, userMessage: { id: expect.any(String), role: 'user' } }) + expect(turn.userMessage?.id).toBe(turn.run.triggeringMessageId) + expect(await waitForTerminalRun(harness, turn.runId)).toMatchObject({ status: 'completed' }) + expect(await gateway.request('chat.startTurn', input)).toMatchObject({ runId: turn.runId, userMessage: { id: turn.userMessage!.id } }) + expect(provider.requests).toHaveLength(1) + const renamed = await gateway.request('conversations.rename', { conversationId: turn.conversationId, expectedTitleRevision: 0, title: 'Renamed via product' }) as { title: string } + expect(renamed.title).toBe('Renamed via product') + await expect(gateway.request('conversations.rename', { conversationId: turn.conversationId, expectedTitleRevision: 0, title: 'Stale title' })).rejects.toMatchObject({ code: 'SOURCE_CHANGED' }) + const recovery = await gateway.recover() + expect(recovery).toMatchObject([{ state: 'rejected', method: 'conversations.rename', canRestore: false }]) + await gateway.acknowledgeRecovery(recovery[0]!.id) + expect(await gateway.recover()).toEqual([]) + await expect(gateway.request('composerDrafts.save', { draftId: draft.draftId, expectedRevision: 0, content: createBuddyUserContent('Recovered local input'), executionConfig: { approvalPolicy: 'policy', executionProfile: 'workspace_write' }, modelSelection: null })).rejects.toMatchObject({ code: 'DRAFT_CONFLICT' }) + const [recoverable] = await gateway.recover() + expect(recoverable).toMatchObject({ state: 'rejected', canRestore: true }) + const restoredId = await gateway.restoreRecovery(recoverable!.id) + expect(createComposerDraftRepository(database).findById(restoredId)?.content.body).toEqual(createBuddyUserContent('Recovered local input').body) + expect(await gateway.recover()).toEqual([]) + } + finally { + gateway.dispose() + product.close() + await service.dispose() + await eventLog.close() + } + }) + + it('keeps an accepted run alive across product disconnection and exposes it to a new client', async () => { + const root = await realpath(await mkdtemp(join(tmpdir(), 'lexora-buddy-client-lifetime-'))) + directories.push(root) + const buddyHome = join(root, 'buddy') + const database = openTrackedDatabase(join(buddyHome, 'buddy.sqlite3')) + const eventLog = createRunEventLog({ database, conversationsDirectory: join(buddyHome, 'conversations') }) + const harness = createRuntimeRpcHarness({ 'offline-provider': { type: 'api_key', key: 'test-api-key' } }) + const response = Promise.withResolvers() + const provider = await startLocalModelProvider(async () => { + await response.promise + return { text: 'ACCEPTED_RUN_SURVIVED_CLIENT_DISCONNECT' } + }) + const service = await startBuddyService({ buddyHome, database, eventLog, rpc: harness.rpc, builtinSkillsDirectories: [] }) + const first = createServiceConnection() + const reconnected = createServiceConnection() + service.connectClient('product', first.server) + try { + await expect(first.client.request('providers.upsertCustom', {})).rejects.toMatchObject({ code: -32601 }) + await expect(first.client.request('runtime.shutdown', {})).rejects.toMatchObject({ code: -32601 }) + await expect(first.client.request('attachments.resolvePreview', {})).rejects.toMatchObject({ code: -32601 }) + await harness.invoke('providers.upsertCustom', offlineProviderConfig(provider.baseUrl)) + const input = await turnRequest(harness, { content: 'Keep the accepted task running', requestId: 'client-lifetime' }) + const turn = await first.client.request('chat.startTurn', input) as LocalTurnStart + await expect.poll(() => provider.requests.length).toBe(1) + first.close() + expect(createRunRepository(database).findById(turn.runId)?.status).toBe('running') + service.connectClient('product', reconnected.server) + await expect(reconnected.client.request('runs.get', { runId: turn.runId })).resolves.toMatchObject({ status: 'running' }) + response.resolve() + expect(await waitForTerminalRun(harness, turn.runId)).toMatchObject({ status: 'completed' }) + await expect(reconnected.client.request('runs.get', { runId: turn.runId })).resolves.toMatchObject({ status: 'completed' }) + expect(JSON.stringify(await eventLog.read(turn.runId))).toContain('ACCEPTED_RUN_SURVIVED_CLIENT_DISCONNECT') + expect(provider.requests).toHaveLength(1) + } + finally { + response.resolve() + first.close() + reconnected.close() + await service.dispose() + await eventLog.close() + } + }) + it('preserves task admission and running work until an idle backup can close admission', async () => { const root = await realpath(await mkdtemp(join(tmpdir(), 'lexora-buddy-idle-backup-'))) directories.push(root) @@ -90,7 +371,7 @@ describe('buddy runtime cross-subsystem contract', () => { expect(JSON.stringify(await eventLog.read(turn.runId))).toContain('TASK_PRESERVED_DURING_BACKUP_CHECK') const late = await turnRequest(harness, { content: 'After admission closed', requestId: 'after-backup' }) expect(service.prepareIdleShutdown()).toBe(true) - await expect(harness.invoke('chat.startTurn', late)).rejects.toMatchObject({ code: 'VALIDATION_FAILED' }) + await expect(harness.invoke('chat.startTurn', late)).rejects.toThrow('Runtime handler is unavailable') expect(createRunRepository(database).listIncomplete()).toEqual([]) } finally { @@ -246,7 +527,7 @@ describe('buddy runtime cross-subsystem contract', () => { }) await seedLog.append({ runId: `run-${id}`, type: 'audit.before', payload: { preserved: true } }) if (maintenance === 'pending deletion') { - createConversationDeletionRepository(database).commitSoftDelete(id, '2026-10-12T00:00:01.000Z') + createConversationDeletionRepository(database).commit(id, '2026-10-12T00:00:01.000Z') } else { const artifactsDirectory = join(conversationsDirectory, id, 'artifacts') @@ -275,14 +556,14 @@ describe('buddy runtime cross-subsystem contract', () => { expect(eventLog.state).toBe('open') expect(runs.findById('run-damaged')).toMatchObject({ status: 'failed', errorCode: 'EVENT_LOG_CORRUPTED' }) expect(database.prepare('SELECT * FROM run_events WHERE run_id = ?').all('run-damaged')).toEqual(projection) - expect(runs.findById('run-healthy')).toMatchObject(maintenance === 'pending deletion' - ? { status: 'cancelled', errorCode: 'RUN_CANCELLED' } - : { status: 'failed', errorCode: 'RUNTIME_RESTARTED' }) if (maintenance === 'pending deletion') { + expect(runs.findById('run-healthy')).toBeNull() + expect(createConversationRepository(database).findById('healthy')).toBeNull() expect(createConversationRepository(database).isDeleted('damaged')).toBe(true) expect(createConversationDeletionRepository(database).pending()).toEqual([]) } if (maintenance === 'legacy outputs') { + expect(runs.findById('run-healthy')).toMatchObject({ status: 'failed', errorCode: 'RUNTIME_RESTARTED' }) const artifacts = createArtifactRepository(database) expect(artifacts.findById('artifact-damaged')).toBeNull() expect(artifacts.findById('artifact-healthy')).toMatchObject({ currentPath: join(conversationsDirectory, 'healthy', 'artifacts', 'output.txt') }) @@ -1394,7 +1675,8 @@ async function waitForAutomationOccurrence( } await new Promise(resolve => setTimeout(resolve, 10)) } - throw new Error(`Automation occurrence did not reach a terminal state: ${automationId}`) + const remaining = await harness.invoke('automations.listOccurrences', { automationId, limit: 1 }) + throw new Error(`Automation occurrence did not reach a terminal state: ${JSON.stringify(remaining)}`) } function readDataImageUrls(request: Record | undefined): string[] { @@ -1433,8 +1715,15 @@ function createRuntimeRpcHarness( const credentials = new Map(Object.entries(initialCredentials).map(([providerId, credential]) => [providerId, structuredClone(credential)], )) + const closed = new Emitter(() => {}) + let isClosed = false const rpc = { - close() {}, + get closed() { return isClosed }, + onDidClose: closed.event, + close() { + isClosed = true + closed.fire() + }, notify() {}, onNotification(listener: (method: string, params: unknown) => void) { notificationListeners.add(listener) @@ -1598,3 +1887,11 @@ function closeServer(server: Server): Promise { server.close(error => error ? reject(error) : resolve()) }) } + +async function startBuddyService(options: Omit & { rpc: BuddyServiceRpcServer }) { + const { rpc, ...configuration } = options + const service = await startService({ ...configuration, platform: createRpcRuntimePlatform(rpc) }) + service.connectClient('product', rpc) + service.connectClient('management', rpc) + return service +} diff --git a/apps/buddy/service/src/__tests__/mutationConcurrencyFixture.ts b/apps/buddy/service/src/__tests__/mutationConcurrencyFixture.ts index be0f1be61..bfdc83d02 100644 --- a/apps/buddy/service/src/__tests__/mutationConcurrencyFixture.ts +++ b/apps/buddy/service/src/__tests__/mutationConcurrencyFixture.ts @@ -2,6 +2,7 @@ import type { DatabaseSync } from 'node:sqlite' import type { LocalBuddy } from '../../../shared/buddies/buddyApi' import type { LocalTurnStart } from '../../../shared/conversation/chatApi' import type { ExtensionShellDescriptor } from '../../../shared/extensions/extensionShell' +import type { StartBuddyServiceOptions } from '../BuddyService' import type { MutationLease } from '../changes/MutationCoordinator' import type { BuddyServiceRpcServer } from '../rpc/BuddyServiceRpcServer' import { Buffer } from 'node:buffer' @@ -15,12 +16,14 @@ import { SessionManager } from '@earendil-works/pi-coding-agent' import { expect, vi } from 'vitest' import { DEFAULT_BUDDY_SETTINGS } from '../../../shared/buddies/buddySettings' import { createBuddyUserContent } from '../../../shared/conversation/buddyUserContent' +import { Emitter } from '../../../shared/events/Emitter' import { DEFAULT_RUNTIME_PREFERENCES } from '../../../shared/runtime/runtimePreferences' import { readCheckpoint } from '../agent/sessions/tree/BuddyTreeStore' -import { startBuddyService } from '../BuddyService' +import { startBuddyService as startService } from '../BuddyService' import { createChangeSetRepository } from '../changes/changeSetRepository' import { MutationCoordinator } from '../changes/MutationCoordinator' import { createRunEventLog } from '../events/createRunEventLog' +import { createRpcRuntimePlatform } from '../platform/createRpcRuntimePlatform' import { openBuddyDatabase } from '../storage/database' import { createRunRepository } from '../storage/runRepository' @@ -68,8 +71,15 @@ function requestRole(payload: Payload): Role { function rpcHarness(shells: readonly ExtensionShellDescriptor[]) { const handlers = new Map[1]>() + const closed = new Emitter(() => {}) + let isClosed = false const rpc = { - close() {}, + get closed() { return isClosed }, + onDidClose: closed.event, + close() { + isClosed = true + closed.fire() + }, notify() {}, onNotification() { return () => {} }, onRequest(method: string, handler: Parameters[1]) { @@ -406,3 +416,11 @@ export async function assertCaptureChain(f: ConcurrencyRuntime, path: string, be } return chain } + +async function startBuddyService(options: Omit & { rpc: BuddyServiceRpcServer }) { + const { rpc, ...configuration } = options + const service = await startService({ ...configuration, platform: createRpcRuntimePlatform(rpc) }) + service.connectClient('product', rpc) + service.connectClient('management', rpc) + return service +} diff --git a/apps/buddy/service/src/agent/events/BufferedRunEventWriter.ts b/apps/buddy/service/src/agent/events/BufferedRunEventWriter.ts index 85195f735..4001497ea 100644 --- a/apps/buddy/service/src/agent/events/BufferedRunEventWriter.ts +++ b/apps/buddy/service/src/agent/events/BufferedRunEventWriter.ts @@ -1,14 +1,23 @@ import type { BuddyAssistantTextPhase } from '../../../../shared/runs/assistantTextPhase' import type { RunEventWriter } from '../../events/RunEventPorts' import type { BuddyProjectedEvent } from './projectPiEvent' +import { Buffer } from 'node:buffer' +import { performance } from 'node:perf_hooks' import { buddyAssistantTextPhaseSchema } from '../../../../shared/runs/assistantTextPhase' import { buddyToolPresentationDeltaSchema } from '../../../../shared/runs/runEventPresentation' +const STREAM_WINDOW_MS = 25 +const STREAM_EVENT_LIMIT = 20 +const STREAM_BYTE_LIMIT = 64 * 1024 + export class BufferedRunEventWriter { readonly #eventLog: Pick readonly #runId: string readonly #timestamp: () => string #pending: TimestampedBuddyProjectedEvent[] = [] + #pendingCount = 0 + #pendingBytes = 0 + #pendingSince = 0 #tail = Promise.resolve() #timer: NodeJS.Timeout | undefined @@ -30,14 +39,29 @@ export class BufferedRunEventWriter { return } + const bytes = Buffer.byteLength(JSON.stringify({ ...timestamped, runId: this.#runId })) + if (this.#pendingCount > 0 && ( + this.#pendingBytes + bytes > STREAM_BYTE_LIMIT + || performance.now() - this.#pendingSince >= STREAM_WINDOW_MS + )) { + this.#flushPending() + } + if (this.#pendingCount === 0) + this.#pendingSince = performance.now() + this.#pendingCount += 1 + this.#pendingBytes += bytes const previous = this.#pending.at(-1) const merged = previous ? mergeStreamDelta(previous, timestamped) : null if (merged) this.#pending[this.#pending.length - 1] = merged else this.#pending.push(timestamped) + if (this.#pendingCount >= STREAM_EVENT_LIMIT || this.#pendingBytes >= STREAM_BYTE_LIMIT) { + this.#flushPending() + return + } if (!this.#timer) { - this.#timer = setTimeout(() => this.#flushPending(), 25) + this.#timer = setTimeout(() => this.#flushPending(), STREAM_WINDOW_MS) this.#timer.unref() } } @@ -85,6 +109,8 @@ export class BufferedRunEventWriter { return const events = this.#pending this.#pending = [] + this.#pendingCount = 0 + this.#pendingBytes = 0 this.#enqueue(events) } } @@ -111,7 +137,6 @@ function mergeStreamDelta( || leftPayload.messageId !== rightPayload.messageId || leftPayload.contentIndex !== rightPayload.contentIndex || leftPayload.phase !== rightPayload.phase - || leftPayload.delta.length + rightPayload.delta.length > 64 * 1024 ) { return null } @@ -140,7 +165,6 @@ function mergeToolPresentationDelta( || leftPayload.toolName !== rightPayload.toolName || leftPayload.delta.outputStart + leftPayload.delta.outputDelta.length !== rightPayload.delta.outputStart - || leftPayload.delta.outputDelta.length + rightPayload.delta.outputDelta.length > 64 * 1024 ) { return null } @@ -192,7 +216,6 @@ function mergeMessageBlockDelta( || !rightPayload || leftPayload.messageId !== rightPayload.messageId || leftPayload.contentIndex !== rightPayload.contentIndex - || leftPayload.delta.length + rightPayload.delta.length > 64 * 1024 ) { return null } diff --git a/apps/buddy/service/src/agent/events/PiEventBridge.ts b/apps/buddy/service/src/agent/events/PiEventBridge.ts index 2e5097d89..1e4281280 100644 --- a/apps/buddy/service/src/agent/events/PiEventBridge.ts +++ b/apps/buddy/service/src/agent/events/PiEventBridge.ts @@ -270,6 +270,7 @@ class ActivePiEventChannel implements PiCompactionEventChannel, PiTurnEventChann this.#pendingTerminalToolUpdates.clear() for (const event of updates) this.#enqueueEvent(event) + this.#eventTail = this.#eventTail.then(() => this.#eventWriter.drain()) } #queueTerminalToolUpdate(event: PiToolExecutionUpdateEvent): void { diff --git a/apps/buddy/service/src/agent/events/__tests__/BufferedRunEventWriter.spec.ts b/apps/buddy/service/src/agent/events/__tests__/BufferedRunEventWriter.spec.ts index 0e588caf2..b9f1e242a 100644 --- a/apps/buddy/service/src/agent/events/__tests__/BufferedRunEventWriter.spec.ts +++ b/apps/buddy/service/src/agent/events/__tests__/BufferedRunEventWriter.spec.ts @@ -1,9 +1,89 @@ import type { AppendBuddyRunEventInput } from '../../../events/BuddyRunEvent' -import { describe, expect, it, vi } from 'vitest' +import type { BuddyProjectedEvent } from '../projectPiEvent' +import { Buffer } from 'node:buffer' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { RunEventQueries } from '../../../events/RunEventQueries' +import { SqliteRunEventLog } from '../../../events/SqliteRunEventLog' +import { seedSqliteRun } from '../../../storage/__tests__/sqliteRunFixture' +import { openBuddyMigrationCandidate } from '../../../storage/database' +import { guardStorageDatabase } from '../../../storage/StorageIsolation' import { BufferedRunEventWriter } from '../BufferedRunEventWriter' +const cleanups: Array<() => Promise> = [] +const now = '2026-09-30T00:00:00.000Z' + +afterEach(async () => { + for (const cleanup of cleanups.splice(0)) + await cleanup() + vi.useRealTimers() + vi.restoreAllMocks() +}) + +function fixture() { + const database = openBuddyMigrationCandidate({ databasePath: ':memory:' }) + const isolation = guardStorageDatabase(database) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + seedSqliteRun(database, 'run-1') + const log = new SqliteRunEventLog({ database }) + const writer = new BufferedRunEventWriter(log, 'run-1', () => now) + const queries = new RunEventQueries(database) + cleanups.push(async () => { + await writer.drain().catch(() => {}) + await log.close() + database.close() + }) + return { database, isolation, log, writer, read: () => queries.read('run-1') } +} + +function textDelta(delta = 'x', messageId = 'answer'): BuddyProjectedEvent { + return { type: 'message.delta', payload: { messageId, delta } } +} + describe('bufferedRunEventWriter', () => { + it('counts UTF-8 bytes across the batch and writes an indivisible oversized event immediately', async () => { + vi.useFakeTimers() + const f = fixture() + for (let index = 0; index < 4; index++) + f.writer.append(textDelta('你'.repeat(6000))) + expect(f.read()).toMatchObject([{ sequence: 1, payload: { delta: '你'.repeat(18000) } }]) + expect(Buffer.byteLength(JSON.stringify(f.read()[0]))).toBeLessThan(64 * 1024) + f.writer.append(textDelta('🙂'.repeat(20000))) + expect(f.read()).toMatchObject([ + { sequence: 1, payload: { delta: '你'.repeat(18000) } }, + { sequence: 2, payload: { delta: '你'.repeat(6000) } }, + { sequence: 3, payload: { delta: '🙂'.repeat(20000) } }, + ]) + await f.writer.drain() + }) + + it('commits message completion after pending deltas without waiting for a timer', () => { + vi.useFakeTimers() + const f = fixture() + f.writer.append(textDelta('answer')) + f.writer.append({ type: 'message.completed', payload: { + messageId: 'answer', + role: 'assistant', + content: { text: 'answer' }, + stopReason: 'completed', + } }) + expect(f.read().map(event => event.type)).toEqual(['message.delta', 'message.completed']) + expect(f.database.prepare('SELECT content_json FROM messages WHERE id = \'answer\'').get()).toEqual({ content_json: '{"text":"answer"}' }) + }) + + it('rejects drain after storage isolation without publishing buffered content', async () => { + vi.useFakeTimers() + const f = fixture() + const committed: unknown[] = [] + f.log.onDidCommit(event => committed.push(event)) + f.writer.append(textDelta('uncommitted')) + f.isolation.isolate(new Error('Fixture storage failure')) + await expect(f.writer.drain()).rejects.toThrow('database failed') + await vi.advanceTimersByTimeAsync(50) + expect(committed).toEqual([]) + expect(f.log.state).toBe('failed') + }) + it('coalesces adjacent terminal output deltas before durable append', async () => { const appended: AppendBuddyRunEventInput[][] = [] const appendBatch = vi.fn(async (events: readonly AppendBuddyRunEventInput[]) => { diff --git a/apps/buddy/service/src/agent/execution/ActiveRunRegistry.ts b/apps/buddy/service/src/agent/execution/ActiveRunRegistry.ts index b0d62c034..31987322f 100644 --- a/apps/buddy/service/src/agent/execution/ActiveRunRegistry.ts +++ b/apps/buddy/service/src/agent/execution/ActiveRunRegistry.ts @@ -56,7 +56,7 @@ interface StartActiveRunInput { export class ActiveRunRegistry { readonly #executions = new Map() - readonly #degradedCleanup = new Map() + readonly #degradedCleanup = new Map() readonly #settled: Emitter readonly onDidSettle: Emitter['event'] @@ -67,8 +67,8 @@ export class ActiveRunRegistry { this.onDidSettle = this.#settled.event } - hasActiveExecution(conversationId: string, actorId?: string): boolean { - return [...this.#executions.values()].some(execution => execution.state.identity.conversationId === conversationId && (!actorId || execution.state.identity.actorId === actorId)) + hasActiveExecution(conversationId: string, actorId?: string, branchId?: string): boolean { + return [...this.#executions.values()].some(execution => execution.state.identity.conversationId === conversationId && (!actorId || execution.state.identity.actorId === actorId) && (!branchId || execution.state.identity.branchId === branchId)) } activeRunIds(canonicalRoot: string): string[] { @@ -87,8 +87,8 @@ export class ActiveRunRegistry { this.#disposed = true } - hasDegradedCleanup(conversationId: string): boolean { - return [...this.#degradedCleanup.values()].includes(conversationId) + hasDegradedCleanup(conversationId: string, branchId?: string): boolean { + return [...this.#degradedCleanup.values()].some(identity => identity.conversationId === conversationId && (!branchId || identity.branchId === branchId)) } start(input: StartActiveRunInput): BuddyTurnHandle { @@ -127,7 +127,7 @@ export class ActiveRunRegistry { if (execution?.state === state) this.#executions.delete(state.runId) if (cleanup === 'degraded') - this.#degradedCleanup.set(state.runId, state.identity.conversationId) + this.#degradedCleanup.set(state.runId, { conversationId: state.identity.conversationId, branchId: state.identity.branchId }) else this.#degradedCleanup.delete(state.runId) this.#settled.fire(copyEventSnapshot({ diff --git a/apps/buddy/service/src/agent/execution/BuddyAgentRunner.ts b/apps/buddy/service/src/agent/execution/BuddyAgentRunner.ts index 2d2796fdd..3e2fa8a9c 100644 --- a/apps/buddy/service/src/agent/execution/BuddyAgentRunner.ts +++ b/apps/buddy/service/src/agent/execution/BuddyAgentRunner.ts @@ -22,6 +22,7 @@ export interface BuddyAgentRunnerOptions { lifecycle: Pick releaseRunResources?: (runId: string) => void | Promise onObserverError?: (error: unknown) => void + assertStorageAvailable?: () => void sessions: Pick, 'withConversationRun' | 'dispose'> } @@ -31,6 +32,7 @@ export class BuddyAgentRunner { readonly #executor: BuddyAgentRunnerOptions['executor'] readonly #executions: ExecutionLifecyclePort readonly #lifecycle: BuddyAgentRunnerOptions['lifecycle'] + readonly #assertStorageAvailable: () => void readonly #releaseRunResources: NonNullable readonly #sessions: Pick, 'withConversationRun' | 'dispose'> #lastTimestamp = 0 @@ -41,11 +43,13 @@ export class BuddyAgentRunner { this.#activeRuns = new ActiveRunRegistry(options.onObserverError) this.onDidSettle = this.#activeRuns.onDidSettle this.#lifecycle = options.lifecycle + this.#assertStorageAvailable = options.assertStorageAvailable ?? (() => {}) this.#releaseRunResources = options.releaseRunResources ?? (() => {}) this.#sessions = options.sessions } startTurn(input: StartBuddyTurnInput): BuddyTurnHandle { + this.#assertStorageAvailable() if (!input.userInput.prompt.trim()) throw new BuddyAgentRunError('VALIDATION_FAILED') @@ -76,6 +80,7 @@ export class BuddyAgentRunner { } startCompaction(input: StartBuddyCompactionInput): BuddyTurnHandle { + this.#assertStorageAvailable() const run = this.#lifecycle.find(input.runId) const session = input.session if ( @@ -101,11 +106,12 @@ export class BuddyAgentRunner { } steer(runId: string, prepare: () => BuddyInputReferenceV1, skills?: readonly SkillReference[]): boolean { + this.#assertStorageAvailable() return this.#activeRuns.steer(runId, prepare, skills) } - hasActiveExecution(conversationId: string, actorId?: string): boolean { - return this.#activeRuns.hasActiveExecution(conversationId, actorId) + hasActiveExecution(conversationId: string, actorId?: string, branchId?: string): boolean { + return this.#activeRuns.hasActiveExecution(conversationId, actorId, branchId) } get hasActiveExecutions(): boolean { @@ -116,11 +122,12 @@ export class BuddyAgentRunner { return this.#activeRuns.isStopping } - hasDegradedCleanup(conversationId: string): boolean { - return this.#activeRuns.hasDegradedCleanup(conversationId) + hasDegradedCleanup(conversationId: string, branchId?: string): boolean { + return this.#activeRuns.hasDegradedCleanup(conversationId, branchId) } followUp(runId: string, prepare: () => BuddyInputReferenceV1, skills?: readonly SkillReference[]): boolean { + this.#assertStorageAvailable() return this.#activeRuns.followUp(runId, prepare, skills) } @@ -201,6 +208,7 @@ export class BuddyAgentRunner { throw new BuddyAgentRunError('RUN_NOT_FOUND') return run } + this.#assertStorageAvailable() const result: RunExecutionOutcome = outcome ?? { status: 'failed', errorCode: 'AGENT_RUN_FAILED' } try { return await this.#executions.finalize({ runId: execution.runId, outcome: withCancellationCode(result, execution.getCancellationCode()), cleanup: execution.getCleanupState() }) @@ -220,6 +228,7 @@ export class BuddyAgentRunner { execution: ActiveRunContext, ): Promise { const { identity, runId, signal } = execution + this.#assertStorageAvailable() signal.throwIfAborted() const continuation = await this.#executions.begin(runId, signal) signal.throwIfAborted() @@ -229,6 +238,8 @@ export class BuddyAgentRunner { runId, startedAt: this.#timestamp(), }) + this.#assertStorageAvailable() + signal.throwIfAborted() const outcome = await this.#executor.executeTurn({ continuation, identity, @@ -247,6 +258,7 @@ export class BuddyAgentRunner { run: RunRecord, execution: ActiveRunContext, ): Promise { + this.#assertStorageAvailable() execution.signal.throwIfAborted() const startedRun = await this.#lifecycle.start({ expectedPurposes: ['conversation.compaction'], @@ -254,6 +266,8 @@ export class BuddyAgentRunner { runId: run.id, startedAt: this.#timestamp(), }) + this.#assertStorageAvailable() + execution.signal.throwIfAborted() const outcome = await this.#executor.executeCompaction({ identity: execution.identity, input, @@ -269,6 +283,7 @@ export class BuddyAgentRunner { execution: ActiveRunContext, error: unknown, ): Promise { + this.#assertStorageAvailable() await this.#executor.invalidateSessionContinuityAfterFailure({ error, identity: execution.identity, diff --git a/apps/buddy/service/src/agent/execution/BuddyRunExecutionPlanner.ts b/apps/buddy/service/src/agent/execution/BuddyRunExecutionPlanner.ts index 348f64271..d0cfe64a8 100644 --- a/apps/buddy/service/src/agent/execution/BuddyRunExecutionPlanner.ts +++ b/apps/buddy/service/src/agent/execution/BuddyRunExecutionPlanner.ts @@ -51,7 +51,6 @@ export class BuddyRunExecutionPlanner { if ( !conversation || conversation.deletedAt !== null - || conversation.activeBranchId !== run.branchId ) { throw new BuddyAgentRunError('CONVERSATION_BINDING_MISMATCH') } diff --git a/apps/buddy/service/src/agent/execution/BuddyTurnLauncher.ts b/apps/buddy/service/src/agent/execution/BuddyTurnLauncher.ts index 47fc496e1..d72411a83 100644 --- a/apps/buddy/service/src/agent/execution/BuddyTurnLauncher.ts +++ b/apps/buddy/service/src/agent/execution/BuddyTurnLauncher.ts @@ -4,9 +4,11 @@ import type { ExecutionCoordinator } from './ExecutionCoordinator' import type { BuddyTurnHandle } from './turnTypes' export interface BuddyTurnLauncherOptions { - executions: Pick + executions: Pick & Partial> planner: Pick runner: Pick + assertStorageAvailable?: () => void + assertServiceAccepting?: () => void beforeRun?: (runId: string, signal: AbortSignal) => Promise } @@ -18,20 +20,29 @@ export class BuddyTurnLauncher { } async launch(runId: string, signal?: AbortSignal): Promise { + this.#options.assertStorageAvailable?.() let preparation: ReturnType | undefined try { + this.#options.assertServiceAccepting?.() preparation = this.#options.executions.prepare(runId, signal) preparation.signal.throwIfAborted() const plan = await this.#options.planner.resolve(runId) + this.#options.assertStorageAvailable?.() preparation.signal.throwIfAborted() + this.#options.assertServiceAccepting?.() + this.#options.executions.assertPrepared?.(runId) if (plan.kind === 'turn') await this.#options.beforeRun?.(runId, preparation.signal) preparation.signal.throwIfAborted() + this.#options.assertStorageAvailable?.() + this.#options.assertServiceAccepting?.() + this.#options.executions.assertPrepared?.(runId) return plan.kind === 'turn' ? this.#options.runner.startTurn(plan.input) : this.#options.runner.startCompaction(plan.input) } catch (error) { + this.#options.assertStorageAvailable?.() const failed = await this.#options.executions.failBeforeStart(runId, error) if (!failed) throw error diff --git a/apps/buddy/service/src/agent/execution/ExecutionComposerInputService.ts b/apps/buddy/service/src/agent/execution/ExecutionComposerInputService.ts index 6a11b79bc..17508b720 100644 --- a/apps/buddy/service/src/agent/execution/ExecutionComposerInputService.ts +++ b/apps/buddy/service/src/agent/execution/ExecutionComposerInputService.ts @@ -53,7 +53,7 @@ export class ExecutionComposerInputService { || (actors.length === 0 && conversation?.buddyId === execution.run.buddyId && (questionId || (input.delivery?.mode === 'current' && execution.run.executionKind === 'actor_child')))) || execution.run.rootRunId !== input.rootRunId || execution.run.conversationId !== input.conversationId || execution.run.branchId !== input.branchId - || this.#options.conversations.findById(input.conversationId)?.activeBranchId !== input.branchId) { + || !conversation || conversation.deletedAt !== null) { throw new BuddyServiceError('VALIDATION_FAILED') } if (!isExecutionTextInput(draft.content)) diff --git a/apps/buddy/service/src/agent/execution/ExecutionCoordinator.ts b/apps/buddy/service/src/agent/execution/ExecutionCoordinator.ts index 4d7929499..b614bd8c3 100644 --- a/apps/buddy/service/src/agent/execution/ExecutionCoordinator.ts +++ b/apps/buddy/service/src/agent/execution/ExecutionCoordinator.ts @@ -101,6 +101,14 @@ export class ExecutionCoordinator implements ExecutionLifecyclePort { return { signal: controller.signal, release } } + assertPrepared(runId: string): void { + this.#options.assertRunAdmission?.(runId) + const current = this.#require(runId) + if (this.#stopped || current.cancelRequestedAt || current.phase !== 'preparing') + throw new ExecutionAdmissionError('stale') + this.#options.executions.assertCurrent(this.#fence(current)) + } + async begin(runId: string, signal: AbortSignal): Promise { const accepted = this.#require(runId) await this.#claimWorking(runId, signal) diff --git a/apps/buddy/service/src/agent/execution/ExecutionPresentationService.ts b/apps/buddy/service/src/agent/execution/ExecutionPresentationService.ts index 5133264f7..06136c8c8 100644 --- a/apps/buddy/service/src/agent/execution/ExecutionPresentationService.ts +++ b/apps/buddy/service/src/agent/execution/ExecutionPresentationService.ts @@ -1,5 +1,5 @@ import type { LocalExecutionInput, LocalExecutionInputsPage } from '../../../../shared/executions/executionInputReceipt' -import type { ExecutionAnswer, ExecutionEventsRequest, ExecutionInputsRequest, ExecutionsChanged, ExecutionScope, ExecutionSend, ExecutionTarget, LocalExecution, LocalExecutionDetail, LocalExecutionEventsPage } from '../../../../shared/executions/executionPresentationApi' +import type { ExecutionActivityRequest, ExecutionAnswer, ExecutionEventsRequest, ExecutionInputsRequest, ExecutionsChanged, ExecutionScope, ExecutionSend, ExecutionTarget, LocalExecution, LocalExecutionDetail, LocalExecutionEventsPage } from '../../../../shared/executions/executionPresentationApi' import type { LocalRunTokenUsage } from '../../../../shared/usage/runTokenUsage' import type { BuddiesService } from '../../buddies/BuddiesService' import type { ChangeCaptureService } from '../../changes/ChangeCaptureService' @@ -18,7 +18,7 @@ import type { ExecutionCoordinator } from './ExecutionCoordinator' import { randomUUID } from 'node:crypto' import { Emitter } from '../../../../shared/events/Emitter' import { EXECUTION_INPUT_PAGE_SIZE } from '../../../../shared/executions/executionInputReceipt' -import { executionAnswerSchema, executionEventsRequestSchema, executionInputsRequestSchema, executionScopeSchema, executionSendSchema, executionTargetSchema } from '../../../../shared/executions/executionPresentationApi' +import { executionActivityRequestSchema, executionAnswerSchema, executionEventsRequestSchema, executionInputsRequestSchema, executionScopeSchema, executionSendSchema, executionTargetSchema } from '../../../../shared/executions/executionPresentationApi' import { toPublicRunEvent } from '../../../../shared/runs/publicRunEvent' import { buddyRunProgressSchema } from '../../../../shared/runs/runProgress' import { projectRunOutputs } from '../../conversations/projectRunOutputs' @@ -31,7 +31,7 @@ export interface ExecutionPresentationOptions { approvals: Pick conversations: Pick runs: Pick - executions: Pick + executions: Pick mailbox: Pick buddies: Pick coordinator: Pick @@ -74,6 +74,14 @@ export class ExecutionPresentationService { ] } + activity(input: ExecutionActivityRequest) { + const { conversationId } = parse(executionActivityRequestSchema, input) + const conversation = this.#options.conversations.findById(conversationId) + if (!conversation || conversation.deletedAt) + throw new BuddyServiceError('VALIDATION_FAILED') + return { hasBlockingAnswer: this.#options.executions.waitingRunIds(conversationId).some(runId => this.#options.questions?.isWaiting(runId)) } + } + list(input: ExecutionScope): LocalExecution[] { const scope = parse(executionScopeSchema, input) const trees = this.#visibleRoots(scope).map(rootId => this.#options.executions.listTree(rootId)) @@ -234,7 +242,7 @@ export class ExecutionPresentationService { const root = this.#options.executions.find(target.rootRunId) if (!root || root.phase === 'closed' || !isCancellable(record) || record.run.branchId !== target.branchId - || this.#options.conversations.findById(target.conversationId)?.activeBranchId !== target.branchId) { + || this.#options.conversations.findById(target.conversationId)?.deletedAt !== null) { throw new BuddyServiceError('VALIDATION_FAILED') } return record @@ -245,7 +253,7 @@ export class ExecutionPresentationService { const root = this.#options.executions.find(target.rootRunId) if (!root || !isLive(record) || !isLive(root) || record.run.branchId !== target.branchId - || this.#options.conversations.findById(target.conversationId)?.activeBranchId !== target.branchId) { + || this.#options.conversations.findById(target.conversationId)?.deletedAt !== null) { throw new BuddyServiceError('VALIDATION_FAILED') } return record @@ -267,7 +275,7 @@ export class ExecutionPresentationService { }) const activeChildren = tree.filter(child => child.run.parentRunId === run.id && child.phase !== 'closed').length const mutable = isLive(record) && isLive(tree.find(member => member.run.id === run.rootRunId) ?? record) - && run.branchId === scope.branchId && this.#options.conversations.findById(scope.conversationId)?.activeBranchId === scope.branchId + && run.branchId === scope.branchId && this.#options.conversations.findById(scope.conversationId)?.deletedAt === null const affectedResources = mutations.hazards.flatMap(hazard => hazard.claims.map(claim => claim.kind === 'host' ? { kind: 'host' as const } : { kind: 'path' as const, path: claim.canonicalPath })) @@ -293,7 +301,7 @@ export class ExecutionPresentationService { waitingFor: record.phase === 'waiting' && this.#options.questions?.isWaiting(run.id) ? 'user_answer' : questions.some(question => question.open) ? 'answer' : isLive(record) && this.#options.approvals.hasPending(run.id) ? 'approval' : mutations.pending ? 'resource' : record.phase === 'waiting' && activeChildren ? 'children' : null, activeChildren, canCancel: isCancellable(record) && tree.find(member => member.run.id === run.rootRunId)?.phase !== 'closed' - && run.branchId === scope.branchId && this.#options.conversations.findById(scope.conversationId)?.activeBranchId === scope.branchId, + && run.branchId === scope.branchId && this.#options.conversations.findById(scope.conversationId)?.deletedAt === null, canSend: mutable && (run.executionKind === 'actor_child' || run.executionKind === 'npc_child'), canAnswer: mutable && questions.some(question => question.open && question.audience === 'user'), cleanup: quarantined || mutations.hazards.length ? 'degraded' : record.cleanupState, diff --git a/apps/buddy/service/src/agent/execution/RuntimeExecutionLifecycle.ts b/apps/buddy/service/src/agent/execution/RuntimeExecutionLifecycle.ts new file mode 100644 index 000000000..dbeb2f53b --- /dev/null +++ b/apps/buddy/service/src/agent/execution/RuntimeExecutionLifecycle.ts @@ -0,0 +1,35 @@ +import type { AutomationDispatcher } from '../../automations/AutomationDispatcher' +import type { AutomationScheduler } from '../../automations/AutomationScheduler' + +interface RuntimeExecutionLifecycleOptions { + scheduler: () => Pick | null + dispatcher: () => Pick | null + disposeExecutions: () => Promise +} + +export class RuntimeExecutionLifecycle { + readonly #options: RuntimeExecutionLifecycleOptions + #stopped: Promise | undefined + + constructor(options: RuntimeExecutionLifecycleOptions) { + this.#options = options + } + + stop(): Promise { + return this.#stopped ??= this.#stop() + } + + async #stop(): Promise { + const scheduler = this.#options.scheduler() + const dispatcher = this.#options.dispatcher() + const scanning = scheduler?.dispose() + dispatcher?.stop() + const errors: unknown[] = [] + await scanning?.catch(error => errors.push(error)) + await this.#options.disposeExecutions().catch(error => errors.push(error)) + await this.#options.dispatcher()?.dispose().catch(error => errors.push(error)) + await this.#options.scheduler()?.settle().catch(error => errors.push(error)) + if (errors.length) + throw new AggregateError(errors, 'Automation and execution cleanup failed') + } +} diff --git a/apps/buddy/service/src/agent/execution/__tests__/BuddyAgentRunner.spec.ts b/apps/buddy/service/src/agent/execution/__tests__/BuddyAgentRunner.spec.ts index a956fa7d5..6a2f289ce 100644 --- a/apps/buddy/service/src/agent/execution/__tests__/BuddyAgentRunner.spec.ts +++ b/apps/buddy/service/src/agent/execution/__tests__/BuddyAgentRunner.spec.ts @@ -3,7 +3,9 @@ import type { AgentSessionEvent, CompactionResult } from '@earendil-works/pi-cod import type { DatabaseSync } from 'node:sqlite' import type { BuddySessionBlueprint } from '../../sessions/BuddySessionBlueprint' import type { BuddySessionTurnContext, ReusableBuddySession } from '../../sessions/ReusableBuddySession' +import type { BuddyAgentRunnerOptions } from '../BuddyAgentRunner' import type { PiTurnExecutorOptions } from '../PiTurnExecutor' +import { existsSync } from 'node:fs' import { mkdir, mkdtemp, realpath, rm, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' @@ -21,6 +23,7 @@ import { createConversationRepository } from '../../../storage/conversationRepos import { openBuddyDatabase } from '../../../storage/database' import { createExecutionRepository } from '../../../storage/executionRepository' import { createRunRepository } from '../../../storage/runRepository' +import { getStorageIsolation } from '../../../storage/StorageIsolation' import { createUsageRepository } from '../../../storage/usageRepository' import { UsageService } from '../../../usage/UsageService' import { createBuddyInputReference } from '../../context/BuddyInputReference' @@ -42,6 +45,64 @@ afterEach(async () => { }) describe('buddyAgentRunner', () => { + it('releases execution without starting Pi or inventing a terminal state when storage fails during start', async () => { + const fixture = await createFixture() + const storage = getStorageIsolation(fixture.database) + const started = Promise.withResolvers() + const waiting = Promise.withResolvers() + const lifecycle = new RunLifecycleService({ eventLog: fixture.eventLog, repository: fixture.runs }) + const output = join(fixture.root, 'must-not-exist.md') + let released = false + const runner = fixture.createRunnerFromFactory(async () => ({ + piSessionFile: join(fixture.root, 'session.jsonl'), + session: new OfflineSession(output), + }), undefined, {}, { + assertStorageAvailable: storage.assertAvailable, + releaseRunResources: () => { released = true }, + lifecycle: { + find: lifecycle.find.bind(lifecycle), + async start(input) { + const run = await lifecycle.start(input) + started.resolve() + await waiting.promise + return run + }, + }, + }) + const input = { + branchId: 'branch-1', + canonicalRoot: fixture.root, + conversationId: 'conversation-1', + cwd: fixture.root, + model: 'claude-sonnet-4-5', + prompt: 'Write output', + provider: 'anthropic', + runId: 'run-isolation', + resources: emptyResources(), + spaceId: null, + userInput: createBuddyInputReference({ images: [], messageId: 'message-isolation', prompt: 'Write output' }), + } + fixture.prepareTurn(input, 'message-isolation') + const handle = runner.startTurn(withSession(input)) + const rejected = expect(handle.completion).rejects.toThrow('Storage is isolated') + try { + await started.promise + storage.isolate(new Error('Storage read failed')) + waiting.resolve() + await rejected + expect(fixture.runs.findById(input.runId)).toMatchObject({ status: 'running', errorCode: null, piSessionFile: null }) + expect((await fixture.eventLog.read(input.runId)).map(event => event.type)).toEqual(['run.started']) + expect(existsSync(output)).toBe(false) + expect(released).toBe(true) + expect(runner.hasActiveExecutions).toBe(false) + } + finally { + waiting.resolve() + await expect(runner.dispose()).rejects.toThrow('Execution backend cleanup failed') + await lifecycle.dispose() + } + }) + it('bounds automation background session startup and records a stable timeout', async () => { const fixture = await createFixture() const startupSignal: { current: AbortSignal | null } = { current: null } @@ -832,32 +893,6 @@ describe('buddyAgentRunner', () => { expect(shutdownFailedSession).toHaveBeenCalledOnce() }) - it('fails an ineligible manual compaction without activating the provider or calling Pi compact', async () => { - const fixture = await createFixture() - fixture.prepareCompletedTurn() - fixture.prepareCompaction('run-compact-not-needed') - const session = new IneligibleCompactionSession() - const runner = fixture.createRunner(session) - - const run = await runner.startCompaction(withSession({ - branchId: 'branch-1', - canonicalRoot: fixture.root, - conversationId: 'conversation-1', - customInstructions: '', - cwd: fixture.root, - spaceId: null, - resources: emptyResources(), - runId: 'run-compact-not-needed', - })).completion - - expect(run).toMatchObject({ - errorCode: 'CONTEXT_COMPACTION_NOT_NEEDED', - status: 'failed', - }) - expect(session.activationCount).toBe(0) - expect(session.compactionInstructions).toEqual([]) - }) - it('cancels Pi compaction before closing the Buddy run', async () => { const fixture = await createFixture() fixture.prepareCompletedTurn() @@ -1244,19 +1279,6 @@ class CompactionStorageFailureSession extends IdleSession { } } -class IneligibleCompactionSession extends CompactionSession { - activationCount = 0 - - canCompact(): boolean { - return false - } - - override activateTurn = async (): Promise<() => void> => { - this.activationCount += 1 - return () => {} - } -} - class WaitingCompactionSession extends IdleSession { readonly started: Promise compactionAborted = false @@ -1424,6 +1446,7 @@ async function createFixture(options: { onCommit?: () => void } = {}) { sessionFactory: PiTurnExecutorOptions['sessionFactory'], sessions = new BuddySessionRegistry(), executorOptions: Pick = {}, + runnerOptions: Partial> = {}, ) { const piEvents = new PiEventBridge({ eventLog, usage: usageService }) const lifecycle = new RunLifecycleService({ eventLog, repository: runs }) @@ -1441,6 +1464,7 @@ async function createFixture(options: { onCommit?: () => void } = {}) { }), lifecycle, sessions, + ...runnerOptions, }) return runner }, diff --git a/apps/buddy/service/src/agent/execution/__tests__/BuddyTurnLauncher.spec.ts b/apps/buddy/service/src/agent/execution/__tests__/BuddyTurnLauncher.spec.ts index ecf5b5385..24ac4417d 100644 --- a/apps/buddy/service/src/agent/execution/__tests__/BuddyTurnLauncher.spec.ts +++ b/apps/buddy/service/src/agent/execution/__tests__/BuddyTurnLauncher.spec.ts @@ -20,10 +20,12 @@ import { createCommandRequestRepository } from '../../../storage/commandRequestR import { createConversationMemberRepository } from '../../../storage/conversationMemberRepository' import { createConversationRepository } from '../../../storage/conversationRepository' import { openBuddyDatabase } from '../../../storage/database' +import { createExecutionRepository } from '../../../storage/executionRepository' import { createRunInputRepository } from '../../../storage/runInputRepository' import { createRunRepository } from '../../../storage/runRepository' import { createSkillRepository } from '../../../storage/skillRepository' import { createSpaceRepository } from '../../../storage/spaceRepository' +import { getStorageIsolation } from '../../../storage/StorageIsolation' import { BuddySessionBlueprintService } from '../../sessions/BuddySessionBlueprintService' import { BuddyRunExecutionPlanner } from '../BuddyRunExecutionPlanner' import { BuddyTurnLauncher } from '../BuddyTurnLauncher' @@ -39,6 +41,29 @@ afterEach(async () => { }) describe('buddyTurnLauncher', () => { + it.each(['resolved', 'rejected'] as const)('leaves the original run queued when a %s planner resumes after isolation', async (outcome) => { + const fixture = await createFixture() + fixture.prepareTurn({ spaceId: null }) + const waiting = Promise.withResolvers() + const entered = Promise.withResolvers() + fixture.resolveInputReferences.mockImplementationOnce(async () => { + entered.resolve() + await waiting.promise + if (outcome === 'rejected') + throw new Error('Preparation failed after isolation') + return { images: [], documents: [] } + }) + const launcher = fixture.createLauncher() + const pending = expect(launcher.launch('run-1')).rejects.toThrow('Storage is isolated') + await entered.promise + fixture.storage.isolate(new Error('Unreadable storage')) + waiting.resolve() + await pending + expect(fixture.runs.findById('run-1')?.status).toBe('queued') + expect(fixture.database.prepare('SELECT COUNT(*) AS total FROM run_events WHERE run_id = ?').get('run-1')?.total).toBe(0) + await expect(launcher.launch('run-1')).rejects.toThrow('Storage is isolated') + }) + it('rejects a saved Skill reference missing from the launch resource snapshot', async () => { const fixture = await createFixture({ selectedSkill: { id: 'writer-id', name: 'writer', revision: 'old' } }) fixture.prepareTurn({ spaceId: null }) @@ -153,25 +178,6 @@ describe('buddyTurnLauncher', () => { expect(startTurn).not.toHaveBeenCalled() }) - it('durably fails a queued run when the runner rejects it synchronously', async () => { - const fixture = await createFixture() - fixture.prepareTurn({ spaceId: null }) - const launcher = fixture.createLauncher({ - startTurn() { - throw new Error('runner rejected launch') - }, - }) - - const handle = await launcher.launch('run-1') - - await expect(handle.completion).resolves.toMatchObject({ - errorCode: 'AGENT_RUN_FAILED', - status: 'failed', - }) - expect((await fixture.eventLog.read('run-1')).map(event => event.type)) - .toEqual(['run.failed']) - }) - it('awaits preparation before the model starts and durably fails a rejected hook while retaining input', async () => { const fixture = await createFixture() fixture.prepareTurn({ spaceId: null }) @@ -195,6 +201,26 @@ describe('buddyTurnLauncher', () => { expect(fixture.runs.findById('run-1')?.triggeringMessageId).toBe('message-1') }) + it('honors a committed cancellation while its execution notification is still pending', async () => { + const fixture = await createFixture() + fixture.prepareTurn({ spaceId: null }) + const gate = Promise.withResolvers() + fixture.resolveInputReferences.mockImplementationOnce(async () => { + await gate.promise + return { images: [], documents: [] } + }) + const launcher = fixture.createLauncher({ startTurn: () => { + throw new Error('Cancelled run cannot start') + } }) + const launch = launcher.launch('run-1') + await vi.waitFor(() => expect(fixture.resolveInputReferences).toHaveBeenCalled()) + createExecutionRepository(fixture.database).requestCancel('run-1', new Date().toISOString()) + gate.resolve() + const handle = await launch + await expect(handle.completion).resolves.toMatchObject({ status: 'cancelled', errorCode: 'RUN_CANCELLED' }) + expect((await fixture.eventLog.read('run-1')).map(event => event.type)).toEqual(['run.cancelled']) + }) + it('cancels an accepted queued run when its launch scope stops during planning', async () => { const fixture = await createFixture() fixture.prepareTurn({ spaceId: null }) @@ -223,6 +249,7 @@ async function createFixture(options: { modelInput?: readonly ('text' | 'image') await mkdir(root, { recursive: true }) const database = openBuddyDatabase({ databasePath: ':memory:' }) databases.push(database) + const storage = getStorageIsolation(database) const paths = new BuddyDataPaths(root) const spaces = createSpaceRepository(database) const runs = createRunRepository(database) @@ -266,6 +293,7 @@ async function createFixture(options: { modelInput?: readonly ('text' | 'image') beforeRun?: (runId: string, signal: AbortSignal) => Promise } = {}) { return new BuddyTurnLauncher({ + assertStorageAvailable: storage.assertAvailable, executions, planner, beforeRun: overrides.beforeRun, @@ -279,6 +307,7 @@ async function createFixture(options: { modelInput?: readonly ('text' | 'image') }, }) }, + database, eventLog, planner, resolveInputReferences, @@ -315,6 +344,7 @@ async function createFixture(options: { modelInput?: readonly ('text' | 'image') `).run(JSON.stringify(['attachment-1']), 'run-1') }, root, + storage, runs, spaces, } diff --git a/apps/buddy/service/src/agent/execution/__tests__/ExecutionCoordinator.spec.ts b/apps/buddy/service/src/agent/execution/__tests__/ExecutionCoordinator.spec.ts index 7690f11e9..4b7995aec 100644 --- a/apps/buddy/service/src/agent/execution/__tests__/ExecutionCoordinator.spec.ts +++ b/apps/buddy/service/src/agent/execution/__tests__/ExecutionCoordinator.spec.ts @@ -169,37 +169,6 @@ describe('execution coordination', () => { release() }) - it('keeps accepted input ahead of sealing and rejects input after closing or cancellation', async () => { - const f = await fixture() - const controller = new AbortController() - const parent = await f.coordinator.begin('root', controller.signal) - let pending = false - const release = parent.bindInputs({ hasPendingInput: () => pending, hasPendingSteering: () => pending }) - const closing = parent.prepareClose({ outcome: { status: 'completed' }, consumedReceipts: [] }) - expect(parent.acceptInput(() => { - pending = true - return true - })).toBe(true) - expect(await closing).toMatchObject({ kind: 'pending' }) - await parent.readPending() - expect(f.executions.find('root')?.phase).toBe('working') - pending = false - expect(await parent.prepareClose({ outcome: { status: 'completed' }, consumedReceipts: [] })).toMatchObject({ kind: 'sealed' }) - let committed = false - expect(parent.acceptInput(() => { - committed = true - return true - })).toBe(false) - expect(committed).toBe(false) - controller.abort() - expect(parent.acceptInput(() => { - committed = true - return true - })).toBe(false) - expect(committed).toBe(false) - release() - }) - it('closes a quarantined child only after its owner stops while healthy siblings and the root settle normally', async () => { const stopped = Promise.withResolvers() const entered = Promise.withResolvers() @@ -613,17 +582,6 @@ describe('execution coordination', () => { expect(f.mutations.getOwnedProcesses('old')).toEqual([{ processId: 'unknown-process', settlement: 'unknown' }]) }) - it('reclaims the working permit when a wait reaches its deadline', async () => { - const f = await fixture() - const signal = new AbortController().signal - await f.coordinator.begin('root', signal) - const waiting = f.coordinator.wait('root', [], 30, signal) - await vi.waitFor(() => expect(f.executions.find('root')?.phase).toBe('waiting'), { interval: 1 }) - await waiting - expect(f.executions.find('root')?.phase).toBe('working') - expect(f.mailbox.listUnconsumed('root')).toEqual([]) - }) - it('serializes supplementary input against successful close with an explicit acceptance result', async () => { for (const supplementFirst of [true, false]) { const f = await fixture() @@ -695,8 +653,6 @@ describe('execution coordination', () => { const next = await f.mutations.acquire({ lifetime: 'runtime' as const, runId: 'later', invocationId: 'next', claims: [{ kind: 'file', canonicalPath: '/workspace/a/result' }], signal: new AbortController().signal }) next.release() expect(f.mutations.getHazards()).toHaveLength(1) - const safe = await f.mutations.acquire({ lifetime: 'runtime' as const, runId: 'later', invocationId: 'safe', claims: [{ kind: 'file', canonicalPath: '/workspace/b/result' }], signal: new AbortController().signal }) - safe.release() }) it('seals failed parent admission before waiting for child cleanup', async () => { @@ -720,16 +676,6 @@ describe('execution coordination', () => { expect(f.runs.findById('child')?.status).toBe('cancelled') }) - it('aggregates child cleanup failures when the root settles', async () => { - const f = await fixture() - f.executions.admitChild(f.child('child')) - await f.coordinator.failBeforeStart('child', new Error('Unavailable')) - f.executions.recordCleanup('child', 'degraded', 'EXECUTION_CLEANUP_DEGRADED') - await f.coordinator.finalize({ runId: 'root', cleanup: 'completed', outcome: { status: 'failed', errorCode: 'AGENT_RUN_FAILED' } }) - expect(f.executions.find('root')?.cleanupState).toBe('degraded') - expect(f.executions.find('child')?.cleanupState).toBe('degraded') - }) - it('waits for a private user answer without consuming the ordinary chat queue', async () => { const f = await fixture() const port = await f.coordinator.begin('root', new AbortController().signal) diff --git a/apps/buddy/service/src/agent/execution/__tests__/ExecutionPresentationService.spec.ts b/apps/buddy/service/src/agent/execution/__tests__/ExecutionPresentationService.spec.ts index 82be0aa09..1757d3d0a 100644 --- a/apps/buddy/service/src/agent/execution/__tests__/ExecutionPresentationService.spec.ts +++ b/apps/buddy/service/src/agent/execution/__tests__/ExecutionPresentationService.spec.ts @@ -3,7 +3,7 @@ import type { BuddyExecutionProfile } from '../../../../../shared/permissions/ex import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' -import { afterEach, describe, expect, it } from 'vitest' +import { afterEach, describe, expect, it, vi } from 'vitest' import { buddySettingsSchema } from '../../../../../shared/buddies/buddySettings' import { createBuddyUserContent } from '../../../../../shared/conversation/buddyUserContent' import { resolveBuddyPermissionSettings } from '../../../../../shared/permissions/permissionMode' @@ -12,6 +12,7 @@ import { MutationCoordinator } from '../../../changes/MutationCoordinator' import { isReviewUserMessage } from '../../../chat/buddyReviewPrompt' import { ComposerDraftService } from '../../../chat/ComposerDraftService' import { createRunEventLog } from '../../../events/createRunEventLog' +import { UserQuestionService } from '../../../questions/UserQuestionService' import { RunLifecycleService } from '../../../runs/RunLifecycleService' import { createExecutionStorageFixture, executionNow } from '../../../storage/__tests__/executionFixture' import { createApprovalRepository } from '../../../storage/approvalRepository' @@ -45,7 +46,11 @@ async function fixture() { const coordinator = new ExecutionCoordinator({ listIncomplete: () => storage.runs.listIncomplete(), executions: storage.executions, mailbox, events, lifecycle, mutations, readResult: () => '', launchChild: async () => { throw new Error('Not used') }, stopExecution: async () => false }) + const questions = new UserQuestionService({ ...storage, coordinator, deliver: async () => { + throw new Error('No answer expected') + } }) const service = new ExecutionPresentationService({ + questions, approvals: createApprovalRepository(storage.database), conversations: storage.conversations, runs: storage.runs, @@ -77,6 +82,7 @@ async function fixture() { const composerInput = new ExecutionComposerInputService(inputOptions) cleanups.push(async () => { await drafts.dispose() + await questions.dispose() service.dispose() usageService.dispose() await coordinator.dispose() @@ -86,13 +92,32 @@ async function fixture() { storage.database.close() await rm(directory, { recursive: true, force: true }) }) - return { ...storage, events, mailbox, mutations, coordinator, service, usage, usageService, drafts, composerInput, inputOptions } + return { ...storage, events, mailbox, mutations, coordinator, service, questions, usage, usageService, drafts, composerInput, inputOptions } } const scope = { conversationId: 'conversation', branchId: 'branch' } const target = { ...scope, rootRunId: 'root', runId: 'child' } describe('execution presentation ownership', () => { + it('aggregates only real blocking question waits across branches without exposing execution data', async () => { + const f = await fixture() + f.conversations.createBranch({ id: 'other', conversationId: 'conversation', parentBranchId: null, forkedFromMessageId: null, createdAt: executionNow, activate: false }) + const source = f.runs.findById('root')! + f.runs.create({ ...source, id: 'root-B', rootRunId: 'root-B', branchId: 'other' }) + const controller = new AbortController() + const question = f.questions.ask('root-B', 'question-B', { questions: [{ id: 'choice', question: 'Continue?', kind: 'single', options: [{ id: 'yes', label: 'Continue' }, { id: 'no', label: 'Stop' }] }] }, controller.signal) + expect(f.service.activity({ conversationId: 'conversation' })).toEqual({ hasBlockingAnswer: false }) + const waiting = f.questions.wait('root-B', question.id, controller.signal) + void waiting.catch(() => {}) + await vi.waitFor(() => expect(f.service.activity({ conversationId: 'conversation' })).toEqual({ hasBlockingAnswer: true })) + expect(f.conversations.findById('conversation')?.activeBranchId).toBe('branch') + f.executions.requestCancel('root-B', executionNow) + expect(f.service.activity({ conversationId: 'conversation' })).toEqual({ hasBlockingAnswer: false }) + controller.abort() + await waiting.catch(() => {}) + expect(() => f.service.activity({ conversationId: 'missing' })).toThrow() + }) + it('derives approval waiting from pending approvals even when model progress still says preparing', async () => { const f = await fixture() f.executions.admitChild(f.child('child', f.actor.id)) @@ -161,20 +186,6 @@ describe('execution presentation ownership', () => { expect(() => f.service.list({ ...scope, branchId: 'missing' })).toThrow() }) - it('keeps same-role ordinals stable when later children sort earlier by ID and when executions finish', async () => { - const f = await fixture() - f.executions.admitChild(f.child('z-child')) - f.executions.admitChild(f.child('a-child')) - const ordinals = () => f.service.list(scope).map(record => [record.runId, record.ordinal]) - expect(ordinals()).toEqual([['root', null], ['z-child', 1], ['a-child', 2]]) - await f.service.cancel({ ...target, runId: 'z-child' }) - f.executions.admitChild(f.child('earlier-child')) - expect(ordinals()).toEqual([['root', null], ['z-child', 1], ['a-child', 2], ['earlier-child', 3]]) - expect((await f.service.detail({ ...target, runId: 'a-child' })).execution.ordinal).toBe(2) - await f.events.replayAll() - expect(ordinals()).toEqual([['root', null], ['z-child', 1], ['a-child', 2], ['earlier-child', 3]]) - }) - it('closes cancelled questions without reviving the child and reports unsettled resources separately', async () => { const f = await fixture() f.executions.admitChild(f.child('child')) @@ -283,7 +294,9 @@ describe('execution presentation ownership', () => { expect(f.executions.find('child')).toMatchObject({ phase: 'closed', run: { status: 'cancelled' } }) expect((await f.service.detail(target)).inputs).toEqual([expect.objectContaining({ messageId, state: 'undelivered' })]) f.conversations.createBranch({ id: 'other', conversationId: 'conversation', parentBranchId: null, forkedFromMessageId: null, createdAt: executionNow, activate: true }) - await expect(f.service.send({ ...input, runId: 'sibling', messageId: '22222222-2222-4222-8222-222222222222' })).rejects.toMatchObject({ code: 'VALIDATION_FAILED' }) + await expect(f.service.send({ ...input, runId: 'sibling', messageId: '22222222-2222-4222-8222-222222222222' })).resolves.toEqual({ accepted: true }) + expect(f.mailbox.listUnconsumed('sibling')).toEqual([expect.objectContaining({ text: input.text, targetRunId: 'sibling' })]) + await expect(f.service.send({ ...input, branchId: 'other', runId: 'sibling', messageId: '33333333-3333-4333-8333-333333333333' })).rejects.toMatchObject({ code: 'VALIDATION_FAILED' }) }) it('pages sanitized process events and counts separate invocations of a reused call ID without leaking the transcript', async () => { @@ -345,7 +358,6 @@ describe('actor input from the composer', () => { it.each([ { mode: 'read_only', kind: 'supplement' }, { mode: 'manual_approval', kind: 'answer' }, - { mode: 'full_access', kind: 'supplement' }, ] as const)('accepts a current $kind after switching to $mode without changing the original run', async ({ mode, kind }) => { const f = await inputFixture() if (kind === 'answer') diff --git a/apps/buddy/service/src/agent/execution/registerExecutionRpc.ts b/apps/buddy/service/src/agent/execution/registerExecutionRpc.ts index 0febfcefc..0c9d8ac39 100644 --- a/apps/buddy/service/src/agent/execution/registerExecutionRpc.ts +++ b/apps/buddy/service/src/agent/execution/registerExecutionRpc.ts @@ -6,6 +6,7 @@ import { registerRuntimeRequest } from '../../rpc/runtimeRequest' export function registerExecutionRpc(options: { rpc: RuntimeRequestRegistrar, service: ExecutionPresentationService, inputs: ExecutionComposerInputService }): () => void { const stops = [ + registerRuntimeRequest(options.rpc, executionPresentationRpc.activity, input => options.service.activity(input)), registerRuntimeRequest(options.rpc, executionPresentationRpc.list, input => options.service.list(input)), registerRuntimeRequest(options.rpc, executionPresentationRpc.detail, input => options.service.detail(input)), registerRuntimeRequest(options.rpc, executionPresentationRpc.inputs, input => options.service.inputs(input)), diff --git a/apps/buddy/service/src/agent/extensions/__tests__/browserSessionExtensions.spec.ts b/apps/buddy/service/src/agent/extensions/__tests__/browserSessionExtensions.spec.ts index 480487011..4d7f56c38 100644 --- a/apps/buddy/service/src/agent/extensions/__tests__/browserSessionExtensions.spec.ts +++ b/apps/buddy/service/src/agent/extensions/__tests__/browserSessionExtensions.spec.ts @@ -239,7 +239,7 @@ function createCompositionServices( }, }, } as unknown as BuddyCapabilityServices, new PetActionService({ - peer: { request: async () => { throw new Error('No host action is expected during extensions') } }, + executeSequence: async () => { throw new Error('No host action is expected during extensions') }, })), directoryGrants: { assertCurrent: () => {}, diff --git a/apps/buddy/service/src/agent/sessions/BuddySessionFactory.ts b/apps/buddy/service/src/agent/sessions/BuddySessionFactory.ts index f07ab34da..84616423b 100644 --- a/apps/buddy/service/src/agent/sessions/BuddySessionFactory.ts +++ b/apps/buddy/service/src/agent/sessions/BuddySessionFactory.ts @@ -78,7 +78,6 @@ export class BuddySessionFactory { || run.status !== 'running' || !conversation || conversation.deletedAt !== null - || conversation.activeBranchId !== blueprint.branchId || conversation.id !== run.conversationId || conversation.spaceId !== (blueprint.space?.id ?? null) || conversation.approvalPolicy !== blueprint.approvalPolicy diff --git a/apps/buddy/service/src/agent/sessions/PiRequestMaterializer.ts b/apps/buddy/service/src/agent/sessions/PiRequestMaterializer.ts new file mode 100644 index 000000000..6522bdf9f --- /dev/null +++ b/apps/buddy/service/src/agent/sessions/PiRequestMaterializer.ts @@ -0,0 +1,198 @@ +import type { Api, Context, Model, UserMessage } from '@earendil-works/pi-ai' +import type { AgentSession } from '@earendil-works/pi-coding-agent' +import type { AttachmentFileInput } from '../../attachments/AttachmentDocumentReference' +import type { AttachmentImageReference } from '../../attachments/AttachmentImageReference' +import type { PreparedAttachmentImage } from '../../attachments/AttachmentImageStore' +import type { InputModel } from '../../providers/modelCapabilities' +import type { BuddyInputReferenceV1 } from '../context/BuddyInputReference' +import { Buffer } from 'node:buffer' +import { randomUUID } from 'node:crypto' +import { createAssistantMessageEventStream } from '@earendil-works/pi-ai' +import { applyDocumentInputPayload } from '../../providers/documentInputPayload' +import { supportsModelFileInput, supportsModelToolCalls } from '../../providers/modelCapabilities' +import { getModelImageLimits, getModelRequestBytesLimit, getModelRequestOverheadReserve } from '../../providers/modelInputBudget' +import { readBuddyInputReference } from '../context/BuddyInputReference' +import { prepareBuddyInputHistory } from '../context/prepareBuddyInputHistory' +import { projectBuddyInput, projectMessageImages } from '../context/projectBuddyInput' + +export interface PiInputMaterialization { + getInputMetadata?: (ids: readonly string[]) => readonly { id: string, sizeBytes: number }[] + allowsNativeImages?: () => Promise + prepareInputImages?: (images: readonly AttachmentImageReference[], model: InputModel) => Promise> + materializeInput: (input: BuddyInputReferenceV1, images?: ReadonlyMap) => Promise + materializeDocuments?: (input: BuddyInputReferenceV1) => Promise +} + +interface PiRequestMaterializerOptions extends PiInputMaterialization { + hasPendingInput: () => boolean + convertToLlm: AgentSession['agent']['convertToLlm'] + streamFunction: AgentSession['agent']['streamFunction'] + onRequest: (model: Model) => void +} + +export class PiRequestMaterializer { + readonly #options: PiRequestMaterializerOptions + readonly #requests = new WeakMap }>() + #context: Context | null = null + + constructor(options: PiRequestMaterializerOptions) { + this.#options = options + } + + get context(): Context | null { + return this.#context + } + + reset(): void { + this.#context = null + } + + async convert(messages: Parameters[0], model: Model) { + const request = { failed: this.#options.hasPendingInput(), failureCode: undefined as string | undefined, documents: new Map() } + const materialized = [] + const projected = new Map() + const preparedImages = new Map() + const projectedMessages = new Map() + const imageModel = model.input.includes('image') && await this.#options.allowsNativeImages?.() === false ? { ...model, input: model.input.filter(kind => kind !== 'image') } : model + const history = prepareBuddyInputHistory(messages) + const imageLimits = getModelImageLimits(model) + const mergeToolImages = ['openai-completions', 'anthropic-messages', 'bedrock-converse-stream'].includes(model.api) + const budget = { + remainingBytes: getModelRequestBytesLimit(model) - getModelRequestOverheadReserve(model) + - Buffer.byteLength(JSON.stringify(history.map(message => projectMessageImages(message, { ...model, input: ['text'] }))), 'utf8'), + remainingImages: imageLimits.maxPerRequest, + remainingMessageImages: imageLimits.maxPerMessage, + } + try { + let previousRole: AgentSession['messages'][number]['role'] | undefined + for (const message of [...history].reverse()) { + if (!mergeToolImages || message.role !== 'toolResult' || previousRole !== 'toolResult') + budget.remainingMessageImages = imageLimits.maxPerMessage + previousRole = message.role + const reference = readBuddyInputReference(message) + if (!reference) { + projectedMessages.set(message, projectMessageImages(message, imageModel, budget)) + continue + } + const ids = reference.attachmentIds ?? [...reference.images, ...reference.documents ?? []].map(file => file.attachmentId) + const sizes = new Map(this.#options.getInputMetadata?.(ids).map(file => [file.id, file.sizeBytes]) ?? []) + let images: ReadonlyMap | undefined + if (imageModel.input.includes('image') && this.#options.prepareInputImages) { + images = budget.remainingBytes > 512 && budget.remainingImages > 0 + ? await this.#options.prepareInputImages(reference.images, model) + : new Map() + for (const image of reference.images) { + const prepared = images.get(image.attachmentId) ?? null + sizes.set(image.attachmentId, prepared ? Buffer.byteLength(prepared.image.data, 'base64') : Number.POSITIVE_INFINITY) + } + } + const projection = projectBuddyInput(reference, imageModel, sizes, budget.remainingBytes, budget.remainingImages) + for (const image of projection.input.images) { + const prepared = images?.get(image.attachmentId) + if (prepared) + preparedImages.set(image.attachmentId, prepared) + } + projected.set(reference.messageId, projection.input) + budget.remainingBytes -= projection.bytes + budget.remainingImages -= projection.input.images.length + } + } + catch { + request.failed = true + } + for (const message of history) { + try { + const reference = readBuddyInputReference(message) + if (!reference) { + materialized.push(projectedMessages.get(message) ?? message) + continue + } + const input = projected.get(reference.messageId) + if (!input) + throw new Error('Missing input projection') + const content = await this.#options.materializeInput(input, preparedImages) + if ( + !Array.isArray(content) + || content.some(block => block.type === 'image' && !block.data) + ) { + throw new Error('Empty image input') + } + const documents = input.documents?.length ? await this.#options.materializeDocuments?.(input) : [] + if (!documents || documents.length !== (input.documents?.length ?? 0)) + throw new Error('Missing document input') + const documentBlocks = documents.flatMap((file, index) => { + const token = `buddy-file:${randomUUID()}` + request.documents.set(token, file) + return [ + { type: 'text' as const, text: `Native attachment: ${input.documents![index]!.attachmentId} (${JSON.stringify(file.name)})` }, + { type: 'text' as const, text: token }, + ] + }) + materialized.push({ content: [...content, ...documentBlocks], role: 'user' as const, timestamp: message.timestamp }) + } + catch (error) { + request.failed = true + if (error instanceof Error && error.message === 'MODEL_INPUT_UNSUPPORTED') + request.failureCode = error.message + } + } + const converted = await this.#options.convertToLlm(materialized) + this.#requests.set(converted, request) + return converted + } + + stream(...[model, context, streamOptions]: Parameters) { + this.#context = null + const request = this.#requests.get(context.messages) + if (request?.failed) + return createInputMaterializationFailure(model, request.failureCode) + if (request && [...request.documents.values()].some(file => !supportsModelFileInput(model, file.mimeType))) + return createInputMaterializationFailure(model, 'MODEL_INPUT_UNSUPPORTED') + const requestContext = supportsModelToolCalls(model) + ? context + : { + ...context, + messages: context.messages.map(message => message.role === 'system' + ? { ...message, toolsAdded: undefined, toolsRemoved: undefined } + : message), + } + this.#context = requestContext + this.#options.onRequest(model) + return this.#options.streamFunction(model, requestContext, { + ...streamOptions, + onPayload: async (payload, target) => { + const previous = await streamOptions?.onPayload?.(payload, target) + return applyDocumentInputPayload(previous ?? payload, target, request?.documents ?? new Map()) + }, + }) + } +} + +function createInputMaterializationFailure(model: Model, code = 'RESOURCE_MATERIALIZATION_FAILED') { + const stream = createAssistantMessageEventStream() + queueMicrotask(() => { + stream.push({ + error: { + api: model.api, + content: [], + errorMessage: code, + model: model.id, + provider: model.provider, + role: 'assistant', + stopReason: 'error', + timestamp: Date.now(), + usage: { + cacheRead: 0, + cacheWrite: 0, + cost: { cacheRead: 0, cacheWrite: 0, input: 0, output: 0, total: 0 }, + input: 0, + output: 0, + totalTokens: 0, + }, + }, + reason: 'error', + type: 'error', + }) + }) + return stream +} diff --git a/apps/buddy/service/src/agent/sessions/PiSessionMailbox.ts b/apps/buddy/service/src/agent/sessions/PiSessionMailbox.ts new file mode 100644 index 000000000..5bcfd8c66 --- /dev/null +++ b/apps/buddy/service/src/agent/sessions/PiSessionMailbox.ts @@ -0,0 +1,112 @@ +import type { AgentSession } from '@earendil-works/pi-coding-agent' +import type { ExecutionMailboxMessage, ExecutionMailboxReceipt } from '../../../../shared/executions/executionApi' +import type { BuddyExtensionRunContext, BuddyExtensionRunContextStore } from '../extensions/BuddyExtensionRunContext' +import { EXECUTION_MAILBOX_ENTRY_TYPE, executionMailboxMessageSchema, executionMailboxReceiptSchema } from '../../../../shared/executions/executionApi' +import { BuddyAgentRunError } from '../../runs/runError' +import { readExecutionMailboxEntries, renderMailboxMessage } from '../execution/executionMailboxEntries' +import { BuddySessionStorageError } from './BuddySessionErrors' +import { syncBuddySessionJournal } from './tree/syncBuddySessionJournal' +import { withPiSessionStorageBoundary } from './withPiSessionStorageBoundary' + +export class PiSessionMailbox { + readonly #session: AgentSession + readonly #runs: BuddyExtensionRunContextStore + readonly #generation: () => number | undefined + + constructor(session: AgentSession, runs: BuddyExtensionRunContextStore, generation: () => number | undefined) { + this.#session = session + this.#runs = runs + this.#generation = generation + } + + appendPending(messages: readonly ExecutionMailboxMessage[], run: BuddyExtensionRunContext) { + return withPiSessionStorageBoundary(this.#session, async () => { + for (const value of messages) { + const message = this.#message(value, run) + this.#session.sessionManager.appendCustomMessageEntry(EXECUTION_MAILBOX_ENTRY_TYPE, renderMailboxMessage(message), false, message) + } + syncBuddySessionJournal(this.#session.sessionManager) + this.#assertCurrent(run) + return this.#session.sessionManager.buildSessionProjection().messages + }) + } + + readReceipts(messages: readonly ExecutionMailboxMessage[], run: BuddyExtensionRunContext) { + return withPiSessionStorageBoundary(this.#session, async () => { + const receipts: ExecutionMailboxReceipt[] = [] + for (const value of messages) { + const message = this.#message(value, run) + const entry = this.#find(message) + if (entry) + receipts.push({ sourceRunId: message.sourceRunId, messageId: message.messageId, payloadHash: message.payloadHash, contextEntryId: entry.contextEntryId }) + } + if (receipts.length) + syncBuddySessionJournal(this.#session.sessionManager) + this.#assertCurrent(run) + return receipts + }) + } + + append(messages: readonly ExecutionMailboxMessage[], run: BuddyExtensionRunContext) { + return withPiSessionStorageBoundary(this.#session, async () => { + const receipts: ExecutionMailboxReceipt[] = [] + for (const value of messages) { + const message = this.#message(value, run) + this.#assertCurrent(run) + let entry = this.#find(message) + if (!entry) { + await this.#session.sendCustomMessage({ customType: EXECUTION_MAILBOX_ENTRY_TYPE, content: renderMailboxMessage(message), details: message, display: false }, { triggerTurn: false }) + this.#assertCurrent(run) + entry = this.#find(message) + } + if (!entry) + throw new BuddySessionStorageError() + receipts.push({ sourceRunId: message.sourceRunId, messageId: message.messageId, payloadHash: message.payloadHash, contextEntryId: entry.contextEntryId }) + } + if (receipts.length) + syncBuddySessionJournal(this.#session.sessionManager) + this.#assertCurrent(run) + return receipts + }) + } + + resolveReceipts(receipts: readonly ExecutionMailboxReceipt[]) { + if (!receipts.length) + throw new BuddyAgentRunError('VALIDATION_FAILED') + return receipts.map((value) => { + const receipt = executionMailboxReceiptSchema.parse(value) + const entry = this.#entries().find(entry => entry.contextEntryId === receipt.contextEntryId && entry.message.sourceRunId === receipt.sourceRunId && entry.message.messageId === receipt.messageId) + if (!entry || entry.message.payloadHash !== receipt.payloadHash) + throw new BuddySessionStorageError() + return { sourceRunId: receipt.sourceRunId, messageId: receipt.messageId, payloadHash: receipt.payloadHash } + }) + } + + #message(value: ExecutionMailboxMessage, run: BuddyExtensionRunContext): ExecutionMailboxMessage { + const message = executionMailboxMessageSchema.parse(value) + if (message.targetRunId !== run.runId || message.targetGeneration !== this.#generation()) + throw new BuddyAgentRunError('CONVERSATION_BINDING_MISMATCH') + return message + } + + #assertCurrent(run: BuddyExtensionRunContext) { + run.signal.throwIfAborted() + if (this.#runs.current !== run) + throw new BuddyAgentRunError('RUN_STATE_MISMATCH') + } + + #entries() { + const run = this.#runs.current + const generation = this.#generation() + if (!run || generation === undefined) + throw new BuddyAgentRunError('RUN_STATE_MISMATCH') + return readExecutionMailboxEntries(this.#session.sessionManager.getBranch(), { runId: run.runId, generation }) + } + + #find(message: ExecutionMailboxMessage) { + const entry = this.#entries().find(entry => entry.message.sourceRunId === message.sourceRunId && entry.message.messageId === message.messageId) + if (entry && JSON.stringify(entry.message) !== JSON.stringify(message)) + throw new BuddySessionStorageError() + return entry ?? null + } +} diff --git a/apps/buddy/service/src/agent/sessions/bindRuntimePreferences.ts b/apps/buddy/service/src/agent/sessions/bindRuntimePreferences.ts index d91717a01..3242f3633 100644 --- a/apps/buddy/service/src/agent/sessions/bindRuntimePreferences.ts +++ b/apps/buddy/service/src/agent/sessions/bindRuntimePreferences.ts @@ -1,20 +1,17 @@ -import type { RuntimeRpcPeerContract } from '../../../../shared/runtime/rpcPeer' import type { RuntimePreferences } from '../../../../shared/runtime/runtimePreferences' -import { runtimePreferencesRpc, runtimePreferencesSchema } from '../../../../shared/runtime/runtimePreferences' +import type { RuntimePreferenceSource } from '../../platform/RuntimePlatform' export async function bindRuntimePreferences( - rpc: Pick, + source: RuntimePreferenceSource, apply: (preferences: RuntimePreferences) => void, ): Promise<() => void> { let updated = false - const unsubscribe = rpc.onNotification((method, params) => { - if (method !== runtimePreferencesRpc.changed) - return + const unsubscribe = source.subscribe((preferences) => { updated = true - apply(runtimePreferencesSchema.parse(params)) + apply(preferences) }) try { - const preferences = runtimePreferencesSchema.parse(await rpc.request(runtimePreferencesRpc.get, {})) + const preferences = await source.get() if (!updated) apply(preferences) return unsubscribe diff --git a/apps/buddy/service/src/agent/sessions/createReusableBuddySession.ts b/apps/buddy/service/src/agent/sessions/createReusableBuddySession.ts index 7c880b96f..5ab500ec5 100644 --- a/apps/buddy/service/src/agent/sessions/createReusableBuddySession.ts +++ b/apps/buddy/service/src/agent/sessions/createReusableBuddySession.ts @@ -1,47 +1,35 @@ -import type { Api, Context, Model, UserMessage } from '@earendil-works/pi-ai' +import type { Api, Model } from '@earendil-works/pi-ai' import type { AgentSession, SessionEntry, } from '@earendil-works/pi-coding-agent' -import type { ExecutionMailboxMessage, ExecutionMailboxReceipt } from '../../../../shared/executions/executionApi' +import type { ExecutionMailboxMessage } from '../../../../shared/executions/executionApi' import type { ImagePreferences } from '../../../../shared/images/imagePreferences' import type { BuddyPermissionSettings } from '../../../../shared/permissions/permissionMode' import type { RuntimePreferences } from '../../../../shared/runtime/runtimePreferences' import type { SkillReference } from '../../../../shared/skills/skillApi' -import type { AttachmentFileInput } from '../../attachments/AttachmentDocumentReference' -import type { AttachmentImageReference } from '../../attachments/AttachmentImageReference' -import type { PreparedAttachmentImage } from '../../attachments/AttachmentImageStore' -import type { InputModel } from '../../providers/modelCapabilities' import type { BuddyInputReferenceStore, BuddyInputReferenceV1, } from '../context/BuddyInputReference' import type { BuddyExtensionRunContextStore } from '../extensions/BuddyExtensionRunContext' +import type { PiInputMaterialization } from './PiRequestMaterializer' import type { BuddySessionModelSelection, BuddySessionShutdownReason, ReusableBuddySession } from './ReusableBuddySession' import type { BuddyConversationTreeCursor } from './tree/BuddyConversationTree' -import { Buffer } from 'node:buffer' -import { randomUUID } from 'node:crypto' -import { createAssistantMessageEventStream } from '@earendil-works/pi-ai' import { prepareCompaction } from '@earendil-works/pi-coding-agent' import { BUDDY_DEFAULT_THINKING_LEVEL } from '../../../../shared/conversation/modelSelection' import { copyEventSnapshot } from '../../../../shared/events/eventSnapshot' -import { EXECUTION_MAILBOX_ENTRY_TYPE, executionMailboxMessageSchema, executionMailboxReceiptSchema } from '../../../../shared/executions/executionApi' import { DEFAULT_RUNTIME_PREFERENCES } from '../../../../shared/runtime/runtimePreferences' -import { applyDocumentInputPayload } from '../../providers/documentInputPayload' -import { supportsModelFileInput, supportsModelToolCalls } from '../../providers/modelCapabilities' -import { getModelImageLimits, getModelRequestBytesLimit, getModelRequestOverheadReserve } from '../../providers/modelInputBudget' import { BuddyAgentRunError } from '../../runs/runError' import { SkillError } from '../../skills/skillFiles' import { createBuddyInputReferenceMessage, readBuddyInputReference } from '../context/BuddyInputReference' import { createContextUsageBreakdown, createEstimatedContextUsage } from '../context/contextUsageBreakdown' -import { prepareBuddyInputHistory } from '../context/prepareBuddyInputHistory' -import { projectBuddyInput, projectMessageImages } from '../context/projectBuddyInput' -import { readExecutionMailboxEntries, renderMailboxMessage } from '../execution/executionMailboxEntries' -import { BuddySessionStorageError, toBuddySessionStorageError } from './BuddySessionErrors' import { getBuddyCacheWarmingStatus } from './getBuddyCacheWarmingStatus' -import { syncBuddySessionJournal } from './tree/syncBuddySessionJournal' +import { PiRequestMaterializer } from './PiRequestMaterializer' +import { PiSessionMailbox } from './PiSessionMailbox' +import { withPiSessionStorageBoundary } from './withPiSessionStorageBoundary' -export interface CreateReusableBuddySessionOptions { +export interface CreateReusableBuddySessionOptions extends PiInputMaterialization { getPendingExecutionMessages?: (runId: string, entries: readonly SessionEntry[]) => readonly ExecutionMailboxMessage[] getModelSelection?: () => Promise getImagePreferences?: (signal: AbortSignal) => Promise @@ -58,11 +46,6 @@ export interface CreateReusableBuddySessionOptions { maxTokens: number | null, ) => Promise> inputReferences: BuddyInputReferenceStore - getInputMetadata?: (ids: readonly string[]) => readonly { id: string, sizeBytes: number }[] - allowsNativeImages?: () => Promise - prepareInputImages?: (images: readonly AttachmentImageReference[], model: InputModel) => Promise> - materializeInput: (input: BuddyInputReferenceV1, images?: ReadonlyMap) => Promise - materializeDocuments?: (input: BuddyInputReferenceV1) => Promise runContext: BuddyExtensionRunContextStore session: AgentSession shutdown: (reason: BuddySessionShutdownReason) => Promise @@ -78,6 +61,7 @@ export function createReusableBuddySession( let pendingRetryOffset: number | null = null let finishingTurn: Promise | undefined let generation: number | undefined + const mailbox = new PiSessionMailbox(session, options.runContext, () => generation) let preparedModelRun: BuddyExtensionRunContextStore['current'] = null let completedTurn: Parameters>[0] | undefined const finishAgentTurn = session.agent.finishTurn @@ -134,20 +118,10 @@ export function createReusableBuddySession( const messages = options.getPendingExecutionMessages?.(run.runId, session.sessionManager.getBranch()) ?? [] if (!messages.length) return prepared ?? undefined - return withPiSessionStorageBoundary(session, async () => { - for (const value of messages) { - const message = executionMailboxMessageSchema.parse(value) - if (message.targetRunId !== run.runId || message.targetGeneration !== generation) - throw new BuddyAgentRunError('CONVERSATION_BINDING_MISMATCH') - session.sessionManager.appendCustomMessageEntry(EXECUTION_MAILBOX_ENTRY_TYPE, renderMailboxMessage(message), false, message) - } - syncBuddySessionJournal(session.sessionManager) - assertCurrentRun(run) - return { - ...prepared, - context: { ...(prepared?.context ?? request.context), messages: session.sessionManager.buildSessionProjection().messages }, - } - }) + return { + ...prepared, + context: { ...(prepared?.context ?? request.context), messages: await mailbox.appendPending(messages, run) }, + } } async function refreshRequestModel(signal?: AbortSignal) { const run = options.runContext.current @@ -187,9 +161,6 @@ export function createReusableBuddySession( pendingRetryOffset = null applySessionSettings(preferences.cacheWarming) }) - const convertToLlm = session.agent.convertToLlm - const streamFunction = session.agent.streamFunction - let latestContext: Context | null = null const pendingInputs = new Map() const pendingSteering = new Set() const idleInputs: { input: BuddyInputReferenceV1, mode: 'steer' | 'followUp' }[] = [] @@ -202,7 +173,6 @@ export function createReusableBuddySession( pendingSteering.delete(input.messageId) } }) - const requestInputs = new WeakMap }>() function enqueueInput(mode: 'steer' | 'followUp', prepare: () => BuddyInputReferenceV1, skills: readonly SkillReference[] = []) { if (!options.runContext.current || options.runContext.current.signal.aborted || finishingTurn) return false @@ -241,135 +211,27 @@ export function createReusableBuddySession( options.inputReferences.pending = null }) } - session.agent.convertToLlm = async (messages) => { - const request = { failed: options.inputReferences.pending !== null, failureCode: undefined as string | undefined, documents: new Map() } - const materialized = [] - const projected = new Map() - const preparedImages = new Map() - const projectedMessages = new Map() - const model = session.model - if (!model) + const materializer = new PiRequestMaterializer({ + hasPendingInput: () => options.inputReferences.pending !== null, + convertToLlm: session.agent.convertToLlm, + streamFunction: session.agent.streamFunction, + getInputMetadata: options.getInputMetadata, + allowsNativeImages: options.allowsNativeImages, + prepareInputImages: options.prepareInputImages, + materializeInput: options.materializeInput, + materializeDocuments: options.materializeDocuments, + onRequest: (model) => { + const run = options.runContext.current + if (run && !run.signal.aborted) + run.requestModel = Object.freeze({ providerId: model.provider, modelId: model.id, reasoning: session.thinkingLevel, serviceTier: run.serviceTier ?? null }) + }, + }) + session.agent.convertToLlm = (messages) => { + if (!session.model) throw new Error('Missing input model') - const imageModel = model.input.includes('image') && await options.allowsNativeImages?.() === false ? { ...model, input: model.input.filter(kind => kind !== 'image') } : model - const history = prepareBuddyInputHistory(messages) - const imageLimits = getModelImageLimits(model) - const mergeToolImages = ['openai-completions', 'anthropic-messages', 'bedrock-converse-stream'].includes(model.api) - const budget = { - remainingBytes: getModelRequestBytesLimit(model) - getModelRequestOverheadReserve(model) - - Buffer.byteLength(JSON.stringify(history.map(message => projectMessageImages(message, { ...model, input: ['text'] }))), 'utf8'), - remainingImages: imageLimits.maxPerRequest, - remainingMessageImages: imageLimits.maxPerMessage, - } - try { - let previousRole: AgentSession['messages'][number]['role'] | undefined - for (const message of [...history].reverse()) { - if (!mergeToolImages || message.role !== 'toolResult' || previousRole !== 'toolResult') - budget.remainingMessageImages = imageLimits.maxPerMessage - previousRole = message.role - const reference = readBuddyInputReference(message) - if (!reference) { - projectedMessages.set(message, projectMessageImages(message, imageModel, budget)) - continue - } - const ids = reference.attachmentIds ?? [...reference.images, ...reference.documents ?? []].map(file => file.attachmentId) - const sizes = new Map(options.getInputMetadata?.(ids).map(file => [file.id, file.sizeBytes]) ?? []) - let images: ReadonlyMap | undefined - if (imageModel.input.includes('image') && options.prepareInputImages) { - images = budget.remainingBytes > 512 && budget.remainingImages > 0 - ? await options.prepareInputImages(reference.images, model) - : new Map() - for (const image of reference.images) { - const prepared = images.get(image.attachmentId) ?? null - sizes.set(image.attachmentId, prepared ? Buffer.byteLength(prepared.image.data, 'base64') : Number.POSITIVE_INFINITY) - } - } - const projection = projectBuddyInput(reference, imageModel, sizes, budget.remainingBytes, budget.remainingImages) - for (const image of projection.input.images) { - const prepared = images?.get(image.attachmentId) - if (prepared) - preparedImages.set(image.attachmentId, prepared) - } - projected.set(reference.messageId, projection.input) - budget.remainingBytes -= projection.bytes - budget.remainingImages -= projection.input.images.length - } - } - catch { - request.failed = true - } - for (const message of history) { - try { - const reference = readBuddyInputReference(message) - if (!reference) { - materialized.push(projectedMessages.get(message) ?? message) - continue - } - const input = projected.get(reference.messageId) - if (!input) - throw new Error('Missing input projection') - const content = await options.materializeInput(input, preparedImages) - if ( - !Array.isArray(content) - || content.some(block => block.type === 'image' && !block.data) - ) { - throw new Error('Empty image input') - } - const documents = input.documents?.length ? await options.materializeDocuments?.(input) : [] - if (!documents || documents.length !== (input.documents?.length ?? 0)) - throw new Error('Missing document input') - const documentBlocks = documents.flatMap((file, index) => { - const token = `buddy-file:${randomUUID()}` - request.documents.set(token, file) - return [ - { type: 'text' as const, text: `Native attachment: ${input.documents![index]!.attachmentId} (${JSON.stringify(file.name)})` }, - { type: 'text' as const, text: token }, - ] - }) - materialized.push({ content: [...content, ...documentBlocks], role: 'user' as const, timestamp: message.timestamp }) - } - catch (error) { - request.failed = true - if (error instanceof Error && error.message === 'MODEL_INPUT_UNSUPPORTED') - request.failureCode = error.message - } - } - const converted = await convertToLlm(materialized) - requestInputs.set(converted, request) - return converted - } - session.agent.streamFunction = (model, context, streamOptions) => { - latestContext = null - const request = requestInputs.get(context.messages) - if (request?.failed) - return createInputMaterializationFailure(model, request.failureCode) - if (request && [...request.documents.values()].some(file => !supportsModelFileInput(model, file.mimeType))) - return createInputMaterializationFailure(model, 'MODEL_INPUT_UNSUPPORTED') - const requestContext = supportsModelToolCalls(model) - ? context - : { - ...context, - messages: context.messages.map(message => message.role === 'system' - ? { ...message, toolsAdded: undefined, toolsRemoved: undefined } - : message), - } - latestContext = requestContext - const run = options.runContext.current - if (run && !run.signal.aborted) { - run.requestModel = Object.freeze({ - providerId: model.provider, - modelId: model.id, - reasoning: session.thinkingLevel, - serviceTier: run.serviceTier ?? null, - }) - } - return streamFunction(model, requestContext, { - ...streamOptions, - onPayload: async (payload, target) => { - const previous = await streamOptions?.onPayload?.(payload, target) - return applyDocumentInputPayload(previous ?? payload, target, request?.documents ?? new Map()) - }, - }) + return materializer.convert(messages, session.model) } + session.agent.streamFunction = (...input) => materializer.stream(...input) return { updatePermissionSettings: options.updatePermissionSettings, refreshModelSelection: () => { @@ -431,7 +293,7 @@ export function createReusableBuddySession( options.assertCodemodeReady?.() input.signal.throwIfAborted() finishingTurn = undefined - latestContext = null + materializer.reset() preparedModelRun = null completedTurn = undefined generation = input.generation @@ -498,66 +360,16 @@ export function createReusableBuddySession( }, ), getToolLabel: name => session.getToolDefinition(name)?.label, - getContextUsageEstimate: () => latestContext ? createEstimatedContextUsage(latestContext) : null, - getContextUsageBreakdown: totalTokens => latestContext - ? createContextUsageBreakdown(latestContext, totalTokens) + getContextUsageEstimate: () => materializer.context ? createEstimatedContextUsage(materializer.context) : null, + getContextUsageBreakdown: totalTokens => materializer.context + ? createContextUsageBreakdown(materializer.context, totalTokens) : null, prompt, - readInternalMessageReceipts: messages => withPiSessionStorageBoundary(session, async () => { - const run = requireIdleRun() - const receipts: ExecutionMailboxReceipt[] = [] - for (const value of messages) { - const message = executionMailboxMessageSchema.parse(value) - if (message.targetRunId !== run.runId || message.targetGeneration !== generation) - throw new BuddyAgentRunError('CONVERSATION_BINDING_MISMATCH') - const entry = findMailboxEntry(message) - if (entry) - receipts.push({ sourceRunId: message.sourceRunId, messageId: message.messageId, payloadHash: message.payloadHash, contextEntryId: entry.contextEntryId }) - } - if (receipts.length) - syncBuddySessionJournal(session.sessionManager) - assertCurrentRun(run) - return receipts - }), - appendInternalMessages: messages => withPiSessionStorageBoundary(session, async () => { - const run = requireIdleRun() - const receipts: ExecutionMailboxReceipt[] = [] - for (const value of messages) { - const message = executionMailboxMessageSchema.parse(value) - assertCurrentRun(run) - if (message.targetRunId !== run.runId || message.targetGeneration !== generation) - throw new BuddyAgentRunError('CONVERSATION_BINDING_MISMATCH') - let entry = findMailboxEntry(message) - if (!entry) { - await session.sendCustomMessage({ - customType: EXECUTION_MAILBOX_ENTRY_TYPE, - content: renderMailboxMessage(message), - details: message, - display: false, - }, { triggerTurn: false }) - assertCurrentRun(run) - entry = findMailboxEntry(message) - } - if (!entry) - throw new BuddySessionStorageError() - receipts.push({ sourceRunId: message.sourceRunId, messageId: message.messageId, payloadHash: message.payloadHash, contextEntryId: entry.contextEntryId }) - } - if (receipts.length) - syncBuddySessionJournal(session.sessionManager) - assertCurrentRun(run) - return receipts - }), + readInternalMessageReceipts: messages => withPiSessionStorageBoundary(session, () => mailbox.readReceipts(messages, requireIdleRun())), + appendInternalMessages: messages => withPiSessionStorageBoundary(session, () => mailbox.append(messages, requireIdleRun())), resumeInternal: receipts => withPiSessionStorageBoundary(session, async () => { const run = requireIdleRun() - if (!receipts.length) - throw new BuddyAgentRunError('VALIDATION_FAILED') - const sources = receipts.map((value) => { - const receipt = executionMailboxReceiptSchema.parse(value) - const entry = mailboxEntries().find(entry => entry.contextEntryId === receipt.contextEntryId && entry.message.sourceRunId === receipt.sourceRunId && entry.message.messageId === receipt.messageId) - if (!entry || entry.message.payloadHash !== receipt.payloadHash) - throw new BuddySessionStorageError() - return { sourceRunId: receipt.sourceRunId, messageId: receipt.messageId, payloadHash: receipt.payloadHash } - }) + const sources = mailbox.resolveReceipts(receipts) if (options.getPermissionSettings) options.updatePermissionSettings?.(options.getPermissionSettings(run.runId)) assertCurrentRun(run) @@ -611,65 +423,6 @@ export function createReusableBuddySession( if (options.runContext.current !== run) throw new BuddyAgentRunError('RUN_STATE_MISMATCH') } - - function mailboxEntries() { - const run = options.runContext.current - if (!run || generation === undefined) - throw new BuddyAgentRunError('RUN_STATE_MISMATCH') - return readExecutionMailboxEntries(session.sessionManager.getBranch(), { runId: run.runId, generation }) - } - - function findMailboxEntry(message: ExecutionMailboxMessage) { - const entry = mailboxEntries().find(entry => entry.message.sourceRunId === message.sourceRunId && entry.message.messageId === message.messageId) - if (entry && JSON.stringify(entry.message) !== JSON.stringify(message)) - throw new BuddySessionStorageError() - return entry ?? null - } -} - -function createInputMaterializationFailure(model: Model, code = 'RESOURCE_MATERIALIZATION_FAILED') { - const stream = createAssistantMessageEventStream() - queueMicrotask(() => { - stream.push({ - error: { - api: model.api, - content: [], - errorMessage: code, - model: model.id, - provider: model.provider, - role: 'assistant', - stopReason: 'error', - timestamp: Date.now(), - usage: { - cacheRead: 0, - cacheWrite: 0, - cost: { cacheRead: 0, cacheWrite: 0, input: 0, output: 0, total: 0 }, - input: 0, - output: 0, - totalTokens: 0, - }, - }, - reason: 'error', - type: 'error', - }) - }) - return stream -} - -async function withPiSessionStorageBoundary( - session: AgentSession, - operation: () => Promise, -): Promise { - try { - return await operation() - } - catch (error) { - const sessionFile = session.sessionManager.getSessionFile() - const storageError = sessionFile - ? toBuddySessionStorageError(error, sessionFile) - : null - throw storageError ?? error - } } async function applyModelSelection( diff --git a/apps/buddy/service/src/agent/sessions/tree/__tests__/BuddyConversationTree.spec.ts b/apps/buddy/service/src/agent/sessions/tree/__tests__/BuddyConversationTree.spec.ts index c9407fd91..e2e5d1887 100644 --- a/apps/buddy/service/src/agent/sessions/tree/__tests__/BuddyConversationTree.spec.ts +++ b/apps/buddy/service/src/agent/sessions/tree/__tests__/BuddyConversationTree.spec.ts @@ -42,6 +42,44 @@ afterEach(async () => { }) describe('native conversation tree', () => { + it('runs branch sessions on separate journals and recovers public ancestry without touching the legacy journal', async () => { + const f = await createFixture() + const legacy = f.run('legacy-root', 'b0', 'legacy-question') + const old = await f.open(legacy) + await old.cursor.begin(old.session.session, legacy.id) + old.cursor.manager.appendMessage(assistant([{ type: 'text', text: 'LEGACY_PRIVATE_DETAIL' }])) + f.publish(legacy, 'legacy-answer', 'SHARED_PUBLIC_ANCESTRY') + old.cursor.finish() + f.complete(legacy) + await old.session.shutdown('quit') + const legacyBytes = await readFile(old.session.piSessionFile) + f.branch('b1', 'legacy-answer') + const makeRun = (id: string, branch: string) => { + const run = f.run(id, branch, `input-${id}`) + const thread = f.threads.ensureActor(run.conversationId, run.buddyId, run.startedAt, branch) + f.database.prepare('UPDATE runs SET thread_id = ? WHERE id = ?').run(thread.id, run.id) + return f.runs.findById(run.id)! + } + const a = makeRun('parallel-A', 'b0') + const b = makeRun('parallel-B', 'b1') + const [openedA, openedB] = await Promise.all([f.open(a), f.open(b)]) + expect(openedA.session.piSessionFile).not.toBe(openedB.session.piSessionFile) + expect(openedA.session.piSessionFile).not.toBe(old.session.piSessionFile) + await Promise.all([openedA.cursor.begin(openedA.session.session, a.id), openedB.cursor.begin(openedB.session.session, b.id)]) + await Promise.all([openedA.session.session.prompt('ONLY_BRANCH_A_INPUT'), openedB.session.session.prompt('ONLY_BRANCH_B_INPUT')]) + const payloadA = f.requests.find(request => request.includes('ONLY_BRANCH_A_INPUT'))! + const payloadB = f.requests.find(request => request.includes('ONLY_BRANCH_B_INPUT'))! + expect(payloadA).toContain('SHARED_PUBLIC_ANCESTRY') + expect(payloadB).toContain('SHARED_PUBLIC_ANCESTRY') + expect(payloadA).not.toContain('ONLY_BRANCH_B_INPUT') + expect(payloadB).not.toContain('ONLY_BRANCH_A_INPUT') + expect(payloadA + payloadB).not.toContain('LEGACY_PRIVATE_DETAIL') + openedA.cursor.finish() + openedB.cursor.finish() + await Promise.all([openedA.session.shutdown('quit'), openedB.session.shutdown('quit')]) + expect(await readFile(old.session.piSessionFile)).toEqual(legacyBytes) + }) + it.each([ ['actor', 'none'], ['npc', 'none'], @@ -417,7 +455,6 @@ describe('native conversation tree', () => { expect(commits.map(event => event.kind)).toEqual(['file.created', 'binding.committed']) expect(commits[1]).toMatchObject({ reason: 'reconciled', treeId: commits[0]!.treeId }) expect((await readFile(file, 'utf8')).startsWith(initial)).toBe(true) - expect(Reflect.set(commits[0]!, 'treeId', 'changed')).toBe(false) await opened.session.shutdown('quit') }) @@ -437,7 +474,6 @@ describe('native conversation tree', () => { const count = commits.length expect(commits.filter(event => event.kind === 'checkpoint.committed').map(event => event.position)).toEqual(['before', 'after']) const snapshot = await fixture.tree.preview('conversation', 'b0', fixture.root, actorThreadId('conversation', 'default')) - expect(snapshot?.getSessionFile()).toBeUndefined() snapshot!.appendMessage(user('private-preview', 'only in preview')) expect(await readFile(file, 'utf8')).toBe(saved) expect(JSON.stringify((await fixture.tree.preview('conversation', 'b0', fixture.root, actorThreadId('conversation', 'default')))!.getEntries())).not.toContain('only in preview') @@ -445,42 +481,11 @@ describe('native conversation tree', () => { await opened.session.shutdown('quit') }) - it('durably preserves the journal through checkpoints and reopening', async () => { - const fixture = await createFixture() - const first = fixture.run('first', 'b0', 'q1') - const opened = await fixture.open(first) - const file = opened.cursor.manager.getSessionFile()! - const initial = await readFile(file, 'utf8') - await opened.cursor.begin(opened.session.session, first.id) - opened.cursor.manager.appendMessage(user('q1', 'durable question')) - opened.cursor.manager.appendMessage(assistant([{ type: 'text', text: 'durable answer' }])) - opened.cursor.finish() - fixture.complete(first, 'a1') - fixture.runs.bindSession(first.id, file) - await opened.session.shutdown('quit') - - const saved = await readFile(file, 'utf8') - expect(saved.startsWith(initial)).toBe(true) - const entries = saved.trim().split('\n').map(line => JSON.parse(line)) - expect(entries.filter(entry => entry.customType === 'lexora.conversation.checkpoint.v1').map(entry => entry.data.position)) - .toEqual(['before', 'after']) - - const next = fixture.run('next', 'b0', 'q2') - const resumed = await fixture.open(next) - await resumed.cursor.begin(resumed.session.session, next.id) - expect(resumed.cursor.manager.getSessionFile()).toBe(file) - expect(JSON.stringify(resumed.session.session.messages)).toContain('durable answer') - resumed.cursor.finish() - await resumed.session.shutdown('quit') - expect((await readFile(file, 'utf8')).startsWith(saved)).toBe(true) - }) - it('keeps tool context and compaction across continuation, regeneration, another branch and restart', async () => { const fixture = await createFixture() const first = fixture.run('r1', 'b0', 'q1') const opened = await fixture.open(first) await opened.cursor.begin(opened.session.session, first.id) - expect(SessionManager.open(opened.cursor.manager.getSessionFile()!).getLeafEntry()?.type).toBe('custom') const userEntry = opened.cursor.manager.appendMessage(user('q1', 'original question')) opened.cursor.manager.appendMessage(assistant([{ type: 'toolCall', id: 'read-call', name: 'read', arguments: { path: 'fixture.txt' } }], 'toolUse')) opened.cursor.manager.appendMessage({ role: 'toolResult', toolCallId: 'read-call', toolName: 'read', content: [{ type: 'text', text: 'NATIVE_TOOL_CONTEXT' }], isError: false, timestamp: Date.now() }) @@ -665,7 +670,6 @@ describe('native conversation tree', () => { expect(commits.filter(event => event.kind === 'binding.committed').map(event => event.reason)).toEqual(['created', 'replaced']) expect(commits.find(event => event.kind === 'checkpoint.committed' && event.runId === first.id)) .toMatchObject({ recovery: { source: 'product_history', missingAttachmentCount: 1, recoveredImageCount: 0 } }) - expect(Reflect.set(recovered.cursor.recoveryDegradation!, 'recoveredImageCount', 9)).toBe(false) } finally { await recovered.session.shutdown('quit') diff --git a/apps/buddy/service/src/agent/sessions/withPiSessionStorageBoundary.ts b/apps/buddy/service/src/agent/sessions/withPiSessionStorageBoundary.ts new file mode 100644 index 000000000..9c383d020 --- /dev/null +++ b/apps/buddy/service/src/agent/sessions/withPiSessionStorageBoundary.ts @@ -0,0 +1,18 @@ +import type { AgentSession } from '@earendil-works/pi-coding-agent' +import { toBuddySessionStorageError } from './BuddySessionErrors' + +export async function withPiSessionStorageBoundary( + session: AgentSession, + operation: () => Promise, +): Promise { + try { + return await operation() + } + catch (error) { + const sessionFile = session.sessionManager.getSessionFile() + const storageError = sessionFile + ? toBuddySessionStorageError(error, sessionFile) + : null + throw storageError ?? error + } +} diff --git a/apps/buddy/service/src/approvals/ApprovalCommandService.ts b/apps/buddy/service/src/approvals/ApprovalCommandService.ts new file mode 100644 index 000000000..7ab05c1c7 --- /dev/null +++ b/apps/buddy/service/src/approvals/ApprovalCommandService.ts @@ -0,0 +1,139 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { CommandAdmissionService } from '../commands/CommandAdmissionService' +import type { InputCommandIntent } from '../commands/commandIntent' +import type { ApprovalRecord, ApprovalRepository } from '../storage/approvalRepository' +import type { CommandRejection } from '../storage/CommandCommitter' +import type { ApprovalResolutionDecision } from './ApprovalService' +import { SqliteRunEventWriter } from '../events/SqliteRunEventWriter' +import { getStorageIsolation } from '../storage/StorageIsolation' +import { afterTransactionCommit, withTransaction } from '../storage/transaction' + +export interface ApprovalCommandAdmission { + database: DatabaseSync + admission: CommandAdmissionService + events?: SqliteRunEventWriter +} + +export interface ApprovalCommandDecision { + allows: (resolution: ApprovalResolutionDecision) => boolean + release: (resolution: ApprovalResolutionDecision) => Promise +} + +interface ApprovalCommandOptions extends ApprovalCommandAdmission { + repository: Pick + lifecycle: { + activate: (approvalId: string) => void + captureDecision: (approval: ApprovalRecord) => ApprovalCommandDecision | null + expire: (approval: ApprovalRecord) => void | Promise + } + onObserverError?: (error: unknown) => void +} + +export class ApprovalCommandService { + readonly #options: ApprovalCommandOptions + readonly #events: SqliteRunEventWriter + readonly #onObserverError: (error: unknown) => void + readonly #effects = new Set>() + + constructor(options: ApprovalCommandOptions) { + this.#options = options + this.#events = options.events ?? new SqliteRunEventWriter(options.database, options.onObserverError) + this.#onObserverError = options.onObserverError ?? (() => {}) + } + + get storageFailure(): Error | null { + return getStorageIsolation(this.#options.database).failure + } + + appendRequested(approval: ApprovalRecord): Promise { + const event = withTransaction(this.#options.database, () => { + afterTransactionCommit(this.#options.database, () => this.#options.lifecycle.activate(approval.id), this.#onObserverError) + return this.#events.append({ runId: approval.runId, type: 'approval.requested', payload: approval }) + }) + return Promise.resolve(event) + } + + resolve(input: unknown, admission = this.#options.admission) { + let accepted: { pending: ApprovalRecord, effect: ApprovalCommandDecision } | undefined + return admission.execute('approvals.resolve', input, { + prepare: async intent => intent, + validate: (intent, command) => { + const decision = this.#validate(intent, command.actorId) + if ('rejection' in decision) + return decision.rejection + accepted = decision + return null + }, + commit: (intent, command) => { + const { pending, effect } = accepted! + const status = intent.decision === 'denied' ? 'denied' : 'approved' + const resolvedAt = new Date().toISOString() + this.#events.append({ runId: pending.runId, type: 'approval.resolved', payload: { + id: pending.id, + resolution: intent.decision, + status, + resolvedAt, + actorId: command.actorId, + } }) + afterTransactionCommit(this.#options.database, () => this.#runEffect(() => effect.release(intent.decision)), this.#onObserverError) + return { target: { approvalId: pending.id, runId: pending.runId }, result: { status, resolution: intent.decision, reviewVersion: intent.reviewVersion, resolvedAt } } + }, + }) + } + + async drain(): Promise { + await Promise.allSettled(this.#effects) + } + + dispose(): void { + if (!this.#options.events) + this.#events.dispose() + } + + #validate(intent: Readonly>, actorId: string): { rejection: CommandRejection } | { pending: ApprovalRecord, effect: ApprovalCommandDecision } { + const pending = this.#options.repository.findById(intent.approvalId) + const target = { approvalId: intent.approvalId, runId: intent.runId } + const rejected = (errorCode: CommandRejection['errorCode'], reason: string): { rejection: CommandRejection } => ({ rejection: { errorCode, target, result: { reason, status: pending?.status ?? null, resolution: pending?.resolution ?? null } } }) + if (!pending) + return rejected('OBJECT_DELETED', 'approval_missing') + if (pending.status !== 'pending') + return rejected('APPROVAL_ALREADY_RESOLVED', 'already_resolved') + if (pending.runId !== intent.runId || !pending.review || pending.review.reviewVersion !== intent.reviewVersion) + return rejected('REVIEW_CHANGED', 'review_mismatch') + if (Date.now() >= Date.parse(pending.review.expiresAt)) { + this.#expire(pending) + return { rejection: { errorCode: 'APPROVAL_ALREADY_RESOLVED', target, result: { reason: 'expired', status: 'cancelled', resolution: 'cancelled' } } } + } + const effect = this.#options.lifecycle.captureDecision(pending) + if (!effect) + return rejected('RESOURCE_UNAVAILABLE', 'waiter_unavailable') + const run = this.#options.database.prepare('SELECT status FROM runs WHERE id = ?').get(pending.runId) + if (!run || !['queued', 'running'].includes(String(run.status))) + return rejected('RESOURCE_UNAVAILABLE', 'run_unavailable') + if (!pending.review.allowedDecisions.includes(intent.decision) || (intent.decision !== 'denied' && !pending.review.complete)) + return rejected('REVIEW_CHANGED', 'decision_not_allowed') + const localActor = this.#options.database.prepare('SELECT local_actor_id FROM storage_profile WHERE id = 1').get()!.local_actor_id + if (intent.decision !== 'denied' && pending.review.persistentEffect && actorId !== localActor) + return rejected('REVIEW_CHANGED', 'local_authorization_required') + if (!effect.allows(intent.decision)) + return rejected('REVIEW_CHANGED', 'decision_not_allowed') + return { pending, effect } + } + + #runEffect(operation: () => void | Promise): void { + const effect = Promise.resolve(operation()) + this.#effects.add(effect) + void effect.finally(() => this.#effects.delete(effect)).catch(this.#onObserverError) + } + + #expire(approval: ApprovalRecord): void { + this.#events.append({ runId: approval.runId, type: 'approval.resolved', payload: { + id: approval.id, + status: 'cancelled', + resolution: 'cancelled', + cancellationReason: 'expired', + resolvedAt: new Date().toISOString(), + } }) + afterTransactionCommit(this.#options.database, () => this.#runEffect(() => this.#options.lifecycle.expire(approval)), this.#onObserverError) + } +} diff --git a/apps/buddy/service/src/approvals/ApprovalService.ts b/apps/buddy/service/src/approvals/ApprovalService.ts index f3827f0fc..f2f4ae8f5 100644 --- a/apps/buddy/service/src/approvals/ApprovalService.ts +++ b/apps/buddy/service/src/approvals/ApprovalService.ts @@ -9,6 +9,7 @@ import type { SystemActionApprovalReviewInput, } from '../../../shared/permissions/approvalReviewPayload' import type { SandboxDirectoryRequest, SandboxNetworkTarget } from '../../../shared/permissions/shellSandbox' +import type { CommandAdmissionService } from '../commands/CommandAdmissionService' import type { AppendBuddyRunEventInput } from '../events/BuddyRunEvent' import type { ToolCallBlockingError } from '../permissions/permissionContract' import type { @@ -16,6 +17,7 @@ import type { ApprovalRepository, } from '../storage/approvalRepository' import type { ApprovalAuthorizationKeys, ApprovalAuthorizationOverride } from './approvalAuthorization' +import type { ApprovalCommandAdmission, ApprovalCommandDecision } from './ApprovalCommandService' import { randomUUID } from 'node:crypto' import { readDiagnosticErrorCode } from '../../../shared/diagnostics/applicationDiagnostic' import { Emitter } from '../../../shared/events/Emitter' @@ -23,6 +25,8 @@ import { copyEventSnapshot } from '../../../shared/events/eventSnapshot' import { createApprovalReviewPayload } from '../../../shared/permissions/approvalReviewPayload' import { RunEventCorruptionError } from '../events/RunEventFailure' import { approvalReuseScopes, createApprovalAuthorizationKeys } from './approvalAuthorization' +import { ApprovalCommandService } from './ApprovalCommandService' +import { createApprovalReview } from './approvalReview' export const APPROVAL_WAIT_TIMEOUT_MS = 30 * 60 * 1_000 @@ -62,6 +66,7 @@ export interface ApprovalResolution { } export interface ApprovalServiceOptions { + commandAdmission?: ApprovalCommandAdmission approvalTimeoutMs?: number eventLog: { append: (input: AppendBuddyRunEventInput) => Promise } onExpired?: (runId: string) => Promise | void @@ -82,6 +87,8 @@ export type ApprovalLifecycleFact = Readonly< > interface ApprovalWaiter { + active: boolean + approval: Pick persisted?: boolean persist?: () => Promise startedAt: number @@ -95,6 +102,7 @@ interface ApprovalWaiter { } export class ApprovalService { + readonly #commands: ApprovalCommandService | null readonly #approvalTimeoutMs: number readonly #eventLog: ApprovalServiceOptions['eventLog'] readonly #onExpired: NonNullable @@ -116,6 +124,26 @@ export class ApprovalService { #disposePromise: Promise | null = null constructor(options: ApprovalServiceOptions) { + this.#commands = options.commandAdmission + ? new ApprovalCommandService({ + ...options.commandAdmission, + repository: options.repository, + onObserverError: options.onObserverError, + lifecycle: { + activate: (id) => { + const waiter = this.#waiters.get(id) + if (waiter) + waiter.active = true + }, + captureDecision: approval => this.#captureCommandDecision(approval), + expire: (approval) => { + this.#waiters.get(approval.id)?.reject(new ApprovalExpiredError()) + this.#releaseWaiter(approval, 'expired', 'committed') + return this.#onExpired(approval.runId) + }, + }, + }) + : null this.#approvalTimeoutMs = options.approvalTimeoutMs ?? APPROVAL_WAIT_TIMEOUT_MS this.#eventLog = options.eventLog this.#onExpired = options.onExpired ?? (() => {}) @@ -208,11 +236,15 @@ export class ApprovalService { summary: input.summary, toolCallId: input.toolCallId, } + if (this.#commands) + approval.review = createApprovalReview(input, approval.id, approval.createdAt, this.#approvalTimeoutMs, reuseScopes) const abort = () => this.#scheduleCancellation(approval, 'cancelled') const expire = () => this.#scheduleCancellation(approval, 'expired') let timer: ReturnType | null = null const decision = new Promise((resolve, reject) => { this.#waiters.set(approval.id, { + active: false, + approval, persist: input.persist, startedAt: Date.now(), authorizationSignal: input.runSignal ?? input.signal, @@ -234,8 +266,11 @@ export class ApprovalService { try { await this.#appendRequested(approval) this.#requireApproval(approval.id) + const waiter = this.#waiters.get(approval.id) + if (waiter) + waiter.active = true if (!this.#stopping && !input.signal.aborted && this.#waiters.has(approval.id)) { - timer = setTimeout(expire, this.#approvalTimeoutMs) + timer = setTimeout(expire, approval.review ? Math.max(0, Date.parse(approval.review.expiresAt) - Date.now()) : this.#approvalTimeoutMs) timer.unref?.() } } @@ -253,7 +288,7 @@ export class ApprovalService { } async resolve(input: ApprovalResolution): Promise { - if (this.#stopping || this.#resolving.has(input.id)) + if (this.#commands || this.#stopping || this.#resolving.has(input.id)) throw new ApprovalResolutionError() return this.#trackResolution(input.id, this.#resolvePending(input)) } @@ -297,6 +332,13 @@ export class ApprovalService { const approval = this.#requireApproval(input.id) if (approval.status !== decision) throw new ApprovalResolutionError() + this.#finishDecision(pending, input.decision, waiter) + return approval + } + + #finishDecision(pending: ApprovalRecord, resolution: ApprovalResolutionDecision, waiter: ApprovalWaiter | undefined): void { + const approvedScope = readApprovedScope(resolution) + const decision: ApprovalDecision = resolution === 'denied' ? 'denied' : 'approved' if (approvedScope && waiter && !this.#stopping && !waiter.signal.aborted && !waiter.authorizationSignal.aborted) { this.#storeAuthorization( approvedScope, @@ -315,14 +357,57 @@ export class ApprovalService { approvalId: pending.id, decision: approvedScope ? `approved_for_${approvedScope}` - : input.decision === 'approved_persistently' + : resolution === 'approved_persistently' ? 'approved_persistently' : decision === 'approved' ? 'approved_once' : 'denied', }) this.#releaseWaiter(pending, decision, 'committed') - return approval + } + + getReview(approvalId: string): ApprovalRecord | null { + return this.#repository.findById(approvalId) + } + + async resolveCommand(input: unknown, trustedAdmission?: CommandAdmissionService) { + if (!this.#commands) + throw new ApprovalResolutionError() + return this.#commands.resolve(input, trustedAdmission) + } + + #captureCommandDecision(pending: ApprovalRecord): ApprovalCommandDecision | null { + const waiter = this.#waiters.get(pending.id) + if (this.#stopping || !waiter?.active || waiter.signal.aborted || waiter.authorizationSignal.aborted) + return null + return { + allows: (resolution) => { + const scope = readApprovedScope(resolution) + return (!scope || waiter.reuseScopes.has(scope)) && (resolution !== 'approved_persistently' || Boolean(waiter.persist)) + }, + release: resolution => this.#releaseApprovedCommand(pending, resolution, waiter), + } + } + + async #releaseApprovedCommand(pending: ApprovalRecord, resolution: ApprovalResolutionDecision, waiter: ApprovalWaiter): Promise { + try { + if (this.#waiters.get(pending.id) !== waiter) + return + if (this.#stopping || waiter.signal.aborted || waiter.authorizationSignal.aborted) + throw new ApprovalCancelledError() + if (resolution === 'approved_persistently') { + await waiter.persist!() + waiter.persisted = true + if (this.#stopping || waiter.signal.aborted || waiter.authorizationSignal.aborted) + throw new ApprovalCancelledError() + } + this.#finishDecision(pending, resolution, waiter) + } + catch (error) { + waiter.reject(asError(error)) + this.#releaseWaiter(pending, 'request_failed', 'committed') + throw error + } } async cancelPendingApprovals(reason: 'recovery' | 'shutdown' = 'recovery', canCancel: (runId: string) => boolean = () => true): Promise { @@ -342,11 +427,19 @@ export class ApprovalService { await resolving.catch(() => {}) return this.#cancel(approval, reason) } - const pending = this.#repository.findById(approval.id) + let pending: ApprovalRecord | null + try { + pending = this.#repository.findById(approval.id) + } + catch (error) { + this.#waiters.get(approval.id)?.reject(asError(error)) + this.#releaseWaiter(approval, reason, 'failed') + throw error + } if (!pending || pending.status !== 'pending') return const waiter = this.#waiters.get(approval.id) - if (waiter?.persisted) { + if (!this.#commands && waiter?.persisted) { try { await this.#trackResolution(approval.id, this.#resolvePending({ id: approval.id, decision: 'approved_persistently' })) } @@ -381,6 +474,7 @@ export class ApprovalService { resolvedAt, status: 'cancelled', resolution: 'cancelled', + cancellationReason: reason, }) persistence = 'committed' waiter?.reject(reason === 'expired' ? new ApprovalExpiredError() : new ApprovalCancelledError()) @@ -405,20 +499,34 @@ export class ApprovalService { async #dispose(): Promise { const failures: unknown[] = [] try { - const pending = this.#repository.listPending() - const cancelled = await Promise.allSettled(pending.map(approval => this.#cancel(approval, 'shutdown'))) - for (const [index, result] of cancelled.entries()) { - if (result.status !== 'rejected') - continue - if (!isIsolatedRecoveryFailure(result.reason)) { - failures.push(result.reason) - continue + try { + const pending = this.#repository.listPending() + const cancelled = await Promise.allSettled(pending.map(approval => this.#cancel(approval, 'shutdown'))) + for (const [index, result] of cancelled.entries()) { + if (result.status !== 'rejected') + continue + if (!isIsolatedRecoveryFailure(result.reason)) { + failures.push(result.reason) + continue + } + const approval = pending[index]! + this.#lifecycle.fire(copyEventSnapshot({ kind: 'cancellation.failed', approvalId: approval.id, runId: approval.runId, toolCallId: approval.toolCallId, reason: 'shutdown', errorCode: readDiagnosticErrorCode(result.reason) })) } - const approval = pending[index]! - this.#lifecycle.fire(copyEventSnapshot({ kind: 'cancellation.failed', approvalId: approval.id, runId: approval.runId, toolCallId: approval.toolCallId, reason: 'shutdown', errorCode: readDiagnosticErrorCode(result.reason) })) + } + catch (error) { + failures.push(error) + for (const waiter of this.#waiters.values()) { + waiter.reject(asError(error)) + this.#releaseWaiter(waiter.approval, 'shutdown', 'failed') + } + } + for (const waiter of this.#waiters.values()) { + waiter.reject(asError(this.#commands?.storageFailure ?? new ApprovalCancelledError())) + this.#releaseWaiter(waiter.approval, 'shutdown', 'failed') } await Promise.allSettled(this.#queues.values()) await Promise.allSettled(this.#resolving.values()) + await this.#commands?.drain() const cancellations = await Promise.allSettled(this.#cancellations) failures.push(...cancellations.flatMap(result => result.status === 'rejected' && !isIsolatedRecoveryFailure(result.reason) ? [result.reason] : [])) } @@ -426,13 +534,14 @@ export class ApprovalService { const runIds = new Set([...this.#authorizations.values()].flatMap(authorizations => [...authorizations.keys()])) for (const runId of runIds) this.clearRunAuthorizations(runId, 'shutdown') + this.#commands?.dispose() this.#lifecycle.dispose() } if (failures.length) throw new AggregateError(failures, 'Lexora Buddy approval shutdown could not persist every cancellation') } - #releaseWaiter(approval: ApprovalRecord, reason: Extract['reason'], persistence: 'committed' | 'failed'): void { + #releaseWaiter(approval: Pick, reason: Extract['reason'], persistence: 'committed' | 'failed'): void { const waiter = this.#waiters.get(approval.id) if (!waiter) return @@ -451,6 +560,8 @@ export class ApprovalService { } #appendRequested(approval: ApprovalRecord): Promise { + if (this.#commands) + return this.#commands.appendRequested(approval) return this.#eventLog.append({ runId: approval.runId, type: 'approval.requested', @@ -469,6 +580,7 @@ export class ApprovalService { resolution: approval.resolution, status: approval.status, resolvedAt: approval.resolvedAt, + ...(approval.cancellationReason ? { cancellationReason: approval.cancellationReason } : {}), }, }) } diff --git a/apps/buddy/service/src/approvals/__tests__/ApprovalCommands.spec.ts b/apps/buddy/service/src/approvals/__tests__/ApprovalCommands.spec.ts new file mode 100644 index 000000000..e481b5cc9 --- /dev/null +++ b/apps/buddy/service/src/approvals/__tests__/ApprovalCommands.spec.ts @@ -0,0 +1,227 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { ApprovalRequest } from '../ApprovalService' +import { setImmediate } from 'node:timers/promises' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { PRODUCT_QUERY_METHODS } from '../../../../shared/productProtocol/queries' +import { CommandAdmissionService, createLocalCommandPrincipal } from '../../commands/CommandAdmissionService' +import { SqliteRunEventLog } from '../../events/SqliteRunEventLog' +import { ProductQueries } from '../../productProtocol/ProductQueries' +import { seedSqliteRun } from '../../storage/__tests__/sqliteRunFixture' +import { createApprovalRepository } from '../../storage/approvalRepository' +import { openBuddyMigrationCandidate } from '../../storage/database' +import { ApprovalService } from '../ApprovalService' + +const databases: DatabaseSync[] = [] +const services: ApprovalService[] = [] +const logs: SqliteRunEventLog[] = [] +afterEach(async () => { + vi.restoreAllMocks() + for (const service of services.splice(0)) await service.dispose().catch(() => {}) + for (const log of logs.splice(0)) await log.close().catch(() => {}) + for (const database of databases.splice(0)) { + if (database.isOpen) + database.close() + } +}) +function fixture(options: { onExpired?: (runId: string) => Promise | void } = {}) { + const database = openBuddyMigrationCandidate({ databasePath: ':memory:' }) + databases.push(database) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + seedSqliteRun(database, 'run-1') + database.exec('UPDATE runs SET status = \'running\'') + const log = new SqliteRunEventLog({ database }) + logs.push(log) + const admission = new CommandAdmissionService({ database, principal: createLocalCommandPrincipal(database), sourceInstanceId: 'instance-1' }) + const repository = createApprovalRepository(database) + const service = new ApprovalService({ repository, eventLog: log, commandAdmission: { database, admission, events: log.transactionWriter }, ...options }) + services.push(service) + const dataGeneration = String(database.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation) + const request = (overrides: Partial = {}) => { + const signal = new AbortController() + const promise = service.request({ runId: 'run-1', toolCallId: 'tool-1', toolName: 'bash', kind: 'shell', summary: 'Run a fixture command', arguments: { command: 'printf fixture', api_key: 'secret-fixture-token' }, signal: signal.signal, ...overrides }) + void promise.catch(() => {}) + return { promise, signal } + } + const pending = async () => { + await vi.waitFor(() => expect(repository.listPending()).toHaveLength(1)) + await Promise.resolve() + return repository.listPending()[0]! + } + const command = (record: Awaited>, commandId = 'command-1', decision = 'approved') => ({ + dataGeneration, + commandId, + expectedServiceInstanceId: 'instance-1', + intent: { approvalId: record.id, runId: record.runId, reviewVersion: record.review!.reviewVersion, decision }, + }) + return { database, log, admission, repository, service, dataGeneration, request, pending, command } +} + +describe('conditional approval decisions', () => { + it('drains an accepted persistent effect during shutdown without rewriting its receipt', async () => { + const f = fixture() + const gate = Promise.withResolvers() + let started = false + let persisted = false + const waiting = f.request({ persist: async () => { + started = true + expect(f.database.isTransaction).toBe(false) + await gate.promise + persisted = true + } }) + const pending = await f.pending() + const command = f.command(pending, 'persistent', 'approved_persistently') + const accepted = await f.service.resolveCommand(command) + expect(started).toBe(true) + let disposed = false + const disposal = f.service.dispose().then(() => { + disposed = true + }) + await expect(waiting.promise).rejects.toMatchObject({ code: 'APPROVAL_CANCELLED' }) + expect(disposed).toBe(false) + gate.resolve() + await disposal + expect(persisted).toBe(true) + expect(f.repository.findById(pending.id)).toMatchObject({ status: 'approved', resolution: 'approved_persistently' }) + expect(await f.service.resolveCommand(command)).toEqual({ created: false, receipt: accepted.receipt }) + expect((await f.log.read('run-1')).filter(event => event.type === 'approval.resolved')).toHaveLength(1) + }) + + it('keeps an NPC approval review actionable with explicit scope without exposing its private execution', async () => { + const f = fixture() + seedSqliteRun(f.database, 'npc') + f.database.prepare('UPDATE runs SET execution_kind = \'npc_child\', root_run_id = \'run-1\', parent_run_id = \'run-1\', status = \'running\' WHERE id = \'npc\'').run() + const waiting = f.request({ runId: 'npc' }) + const pending = await f.pending() + const queries = new ProductQueries(f.database, 'desktop-local', 'instance-1') + const projected = PRODUCT_QUERY_METHODS['approvals.get'].output.parse(queries.read('approvals.get', { approvalId: pending.id })) + expect(projected).toMatchObject({ runId: 'npc', conversationId: 'task-1', branchId: 'branch-task-1', review: { complete: true, allowedDecisions: expect.arrayContaining(['approved']) } }) + expect(queries.read('runs.get', { runId: 'npc' })).toBeNull() + expect(queries.read('runs.getExecutionState', { runId: 'npc' })).toBeNull() + expect(queries.read('runs.listEvents', { runId: 'npc', limit: 100, cursor: null })).toEqual({ items: [], nextCursor: null }) + expect((await f.service.resolveCommand(f.command(pending))).receipt.decision).toBe('accepted') + expect(await waiting.promise).toEqual({ approvalId: pending.id, decision: 'approved_once' }) + }) + + it('binds full safe review to an immutable lifetime and persists the original decision before releasing its waiter', async () => { + const f = fixture() + const waiting = f.request() + const pending = await f.pending() + expect(pending.review).toMatchObject({ complete: true, contentVersion: 1, allowedDecisions: expect.arrayContaining(['denied', 'approved']) }) + expect(JSON.stringify(pending.review)).not.toContain('secret-fixture-token') + expect(JSON.stringify(pending.review)).toContain('[redacted]') + expect(Date.parse(pending.review!.expiresAt) - Date.parse(pending.createdAt)).toBe(30 * 60 * 1000) + const result = await f.service.resolveCommand(f.command(pending)) + expect(result.receipt).toMatchObject({ decision: 'accepted', result: { status: 'approved', resolution: 'approved' } }) + expect(await waiting.promise).toEqual({ approvalId: pending.id, decision: 'approved_once' }) + expect(f.repository.findById(pending.id)).toMatchObject({ status: 'approved', decisionActorId: 'desktop-local', resolution: 'approved' }) + expect(await f.service.resolveCommand(f.command(pending))).toEqual({ created: false, receipt: result.receipt }) + const other = await f.service.resolveCommand(f.command(pending, 'another-command', 'denied')) + expect(other.receipt).toMatchObject({ decision: 'rejected', errorCode: 'APPROVAL_ALREADY_RESOLVED', result: { resolution: 'approved' } }) + }) + + it('rolls back approval facts and projections if the receipt fails and never releases early', async () => { + const f = fixture() + const waiting = f.request() + const pending = await f.pending() + let settled = false + void waiting.promise.then(() => { + settled = true + }) + f.database.exec('CREATE TRIGGER reject_receipt BEFORE INSERT ON command_receipts BEGIN SELECT RAISE(ABORT, \'receipt unavailable\'); END') + await expect(f.service.resolveCommand(f.command(pending))).rejects.toThrow('receipt unavailable') + expect(settled).toBe(false) + expect(f.repository.findById(pending.id)?.status).toBe('pending') + expect(f.database.prepare('SELECT * FROM approval_decisions').all()).toEqual([]) + expect((await f.log.read('run-1')).filter(event => event.type === 'approval.resolved')).toEqual([]) + f.database.exec('DROP TRIGGER reject_receipt') + await f.service.resolveCommand(f.command(pending)) + expect(await waiting.promise).toMatchObject({ decision: 'approved_once' }) + }) + + it('does not release its waiter or report a missing receipt after an uncertain COMMIT', async () => { + const f = fixture() + const waiting = f.request() + const pending = await f.pending() + let released = false + void waiting.promise.then(() => { + released = true + }, () => {}) + const exec = f.database.exec.bind(f.database) + const spy = vi.spyOn(f.database, 'exec').mockImplementation((sql) => { + exec(sql) + if (sql === 'COMMIT') + throw new Error('commit acknowledgement lost') + }) + await expect(f.service.resolveCommand(f.command(pending))).rejects.toMatchObject({ code: 'COMMAND_RESULT_UNKNOWN' }) + expect(released).toBe(false) + expect(() => f.admission.lookup({ dataGeneration: f.dataGeneration, commandId: 'command-1' })).toThrowError(expect.objectContaining({ code: 'COMMAND_RESULT_UNKNOWN' })) + spy.mockRestore() + }) + + it('expires at the fixed deadline without revival on clock rollback and drains expiry before shutdown', async () => { + const gate = Promise.withResolvers() + const f = fixture({ onExpired: () => gate.promise }) + const waiting = f.request() + const pending = await f.pending() + vi.spyOn(Date, 'now').mockReturnValue(Date.parse(pending.review!.expiresAt)) + const result = await f.service.resolveCommand(f.command(pending)) + expect(result.receipt).toMatchObject({ decision: 'rejected', errorCode: 'APPROVAL_ALREADY_RESOLVED', result: { reason: 'expired' } }) + await expect(waiting.promise).rejects.toMatchObject({ code: 'AUTOMATION_APPROVAL_EXPIRED' }) + expect(f.repository.findById(pending.id)).toMatchObject({ status: 'cancelled', cancellationReason: 'expired' }) + expect(new ProductQueries(f.database, 'phone', 'instance-1').views.approvalDecision(pending.id)).toMatchObject({ status: 'cancelled', actor: null, cancellationReason: 'expired' }) + vi.mocked(Date.now).mockReturnValue(Date.parse(pending.createdAt) - 1000) + expect(await f.service.resolveCommand(f.command(pending))).toEqual({ created: false, receipt: result.receipt }) + expect((await f.service.resolveCommand(f.command(pending, 'new-command'))).receipt).toMatchObject({ decision: 'rejected', errorCode: 'APPROVAL_ALREADY_RESOLVED' }) + let disposed = false + const disposal = f.service.dispose().then(() => { + disposed = true + }) + await setImmediate() + expect(disposed).toBe(false) + gate.resolve() + await disposal + expect(disposed).toBe(true) + }) + + it('distinguishes user denial from rejected commands and refuses obsolete review identity', async () => { + const f = fixture() + const waiting = f.request() + const pending = await f.pending() + const wrong = f.command(pending, 'wrong-review') + wrong.intent.reviewVersion = 'a'.repeat(64) + expect((await f.service.resolveCommand(wrong)).receipt).toMatchObject({ decision: 'rejected', errorCode: 'REVIEW_CHANGED' }) + expect((await f.service.resolveCommand(f.command(pending, 'deny', 'denied'))).receipt).toMatchObject({ decision: 'accepted', result: { status: 'denied' } }) + expect(await waiting.promise).toMatchObject({ decision: 'denied' }) + }) + + it('refuses pending records without a current waiter and incomplete review approval', async () => { + const f = fixture() + const waiting = f.request({ arguments: { command: 'x'.repeat(1024 * 1024 + 1) } }) + const pending = await f.pending() + expect(pending.review?.complete).toBe(false) + expect((await f.service.resolveCommand(f.command(pending))).receipt).toMatchObject({ decision: 'rejected', errorCode: 'REVIEW_CHANGED' }) + const reconstructed = new ApprovalService({ repository: f.repository, eventLog: f.log, commandAdmission: { database: f.database, admission: f.admission, events: f.log.transactionWriter } }) + services.push(reconstructed) + expect((await reconstructed.resolveCommand(f.command(pending, 'no-waiter', 'denied'))).receipt).toMatchObject({ decision: 'rejected', errorCode: 'RESOURCE_UNAVAILABLE' }) + await f.service.resolveCommand(f.command(pending, 'deny-large', 'denied')) + expect(await waiting.promise).toMatchObject({ decision: 'denied' }) + }) + + it('restricts persistent authorization effects to the trusted local actor and applies them after commit', async () => { + const f = fixture() + let persisted = false + const waiting = f.request({ persist: async () => { + expect(f.database.isTransaction).toBe(false) + expect(f.database.prepare('SELECT decision FROM command_receipts WHERE command_id = ?').get('local-command')?.decision).toBe('accepted') + persisted = true + } }) + const pending = await f.pending() + f.database.exec('INSERT INTO storage_actors VALUES (\'remote\', \'device\', \'active\', \'2026-11-07T00:00:00.000Z\')') + const remote = new CommandAdmissionService({ database: f.database, sourceInstanceId: 'instance-1', principal: { actorId: 'remote', assertAuthenticated() {} } }) + expect((await f.service.resolveCommand(f.command(pending, 'remote-command', 'approved_persistently'), remote)).receipt).toMatchObject({ decision: 'rejected', errorCode: 'REVIEW_CHANGED', result: { reason: 'local_authorization_required' } }) + expect(persisted).toBe(false) + await f.service.resolveCommand(f.command(pending, 'local-command', 'approved_persistently')) + expect(await waiting.promise).toMatchObject({ decision: 'approved_persistently' }) + expect(persisted).toBe(true) + }) +}) diff --git a/apps/buddy/service/src/approvals/__tests__/ApprovalService.spec.ts b/apps/buddy/service/src/approvals/__tests__/ApprovalService.spec.ts index 1cb38e90d..a91ab8c4e 100644 --- a/apps/buddy/service/src/approvals/__tests__/ApprovalService.spec.ts +++ b/apps/buddy/service/src/approvals/__tests__/ApprovalService.spec.ts @@ -7,9 +7,12 @@ import { deferred } from '@buddy-tests/deferred' import { afterEach, describe, expect, it, vi } from 'vitest' import { createRunEventLog } from '../../events/createRunEventLog' +import { SqliteRunEventLog } from '../../events/SqliteRunEventLog' import { prepareTestTurnRequest } from '../../storage/__tests__/composerDraftTestFixture' +import { seedSqliteRun } from '../../storage/__tests__/sqliteRunFixture' import { createApprovalRepository } from '../../storage/approvalRepository' -import { openBuddyDatabase } from '../../storage/database' +import { openBuddyDatabase, openBuddyMigrationCandidate } from '../../storage/database' +import { guardStorageDatabase } from '../../storage/StorageIsolation' import { ApprovalCancelledError, ApprovalExpiredError, @@ -34,77 +37,49 @@ const BROWSER_REVIEW = { } afterEach(async () => { - for (const database of databases.splice(0)) - database.close() + for (const database of databases.splice(0)) { + if (database.isOpen) + database.close() + } await Promise.all(directories.splice(0).map(path => rm(path, { force: true, recursive: true }))) }) describe('approvalService', () => { - it('cancels a durable approval and closes its run when the wait expires', async () => { - const database = openBuddyDatabase({ databasePath: ':memory:' }) + it('releases active and queued approval waits on disposal when storage cannot be read', async () => { + const root = await mkdtemp(join(tmpdir(), 'lexora-approval-isolation-')) + directories.push(root) + const databasePath = join(root, 'candidate.sqlite3') + const database = openBuddyMigrationCandidate({ databasePath }) databases.push(database) - prepareTestTurnRequest(database, { - attachmentBindings: [], - branchId: 'branch-timeout', - conversationId: 'conversation-timeout', - createdAt: '2026-08-14T00:00:00.000Z', - model: 'model-1', - spaceId: null, - provider: 'provider-1', - requestFingerprint: 'fingerprint-timeout', - approvalPolicy: 'policy' as const, - executionProfile: 'workspace_write', - requestId: 'request-timeout', - runInput: { - attachmentIds: [], - contextItems: [], - prompt: 'hello', - reasoning: null, - serviceTier: null, - }, - runId: 'run-timeout', - title: null, - userMessageContent: { text: 'hello' }, - userMessageId: 'message-timeout', - }) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + seedSqliteRun(database, 'run-1') + guardStorageDatabase(database) const repository = createApprovalRepository(database) - const eventLog = await createEventLog(database) - const onExpired = vi.fn() - const service = new ApprovalService({ - approvalTimeoutMs: 5, - eventLog, - onExpired, - repository, - }) - - const decision = service.request({ - reuseScopes: [], - arguments: { operation: 'upsert' }, - automation: { - executionProfile: 'workspace_write', - modelMode: 'default', - name: 'Daily review', - operation: 'upsert', - spaceId: null, - promptSummary: 'Review the day', - scheduleSummary: 'daily at 18:00', - timezone: 'Asia/Shanghai', - }, - kind: 'automation', - runId: 'run-timeout', - signal: new AbortController().signal, - summary: 'Save automation Daily review', - toolCallId: 'tool-timeout', - toolName: 'lexora_buddy_automation', - }) - - await expect(decision).rejects.toBeInstanceOf(ApprovalExpiredError) - await vi.waitFor(() => expect(onExpired).toHaveBeenCalledWith('run-timeout')) - expect(repository.listPending('run-timeout')).toEqual([]) - expect(repository.list({ runId: 'run-timeout' })[0]).toMatchObject({ - kind: 'automation', - status: 'cancelled', - }) + const eventLog = new SqliteRunEventLog({ database }) + const service = new ApprovalService({ repository, eventLog }) + const controller = new AbortController() + const request = { + arguments: { command: 'fixture' }, + kind: 'shell' as const, + runId: 'run-1', + signal: controller.signal, + summary: 'Fixture approval', + toolCallId: 'first', + toolName: 'bash', + } + const first = expect(service.request(request)).rejects.toMatchObject({ code: 'STORAGE_ISOLATED' }) + const queued = expect(service.request({ ...request, toolCallId: 'queued' })).rejects.toMatchObject({ code: 'APPROVAL_CANCELLED' }) + await vi.waitFor(() => expect(repository.listPending()).toHaveLength(1)) + database.exec('PRAGMA query_only = ON') + expect(() => database.prepare('UPDATE runs SET status = status').run()).toThrow(expect.objectContaining({ errcode: 8 })) + await expect(service.dispose()).rejects.toThrow('could not persist every cancellation') + await Promise.all([first, queued]) + await eventLog.close() + database.close() + const reopened = openBuddyMigrationCandidate({ databasePath }) + databases.push(reopened) + expect(createApprovalRepository(reopened).listPending()).toMatchObject([{ toolCallId: 'first', status: 'pending' }]) + expect(reopened.prepare('SELECT event_type FROM run_events').all()).toEqual([{ event_type: 'approval.requested' }]) }) it('expires a durable browser approval once while preserving its review payload', async () => { @@ -173,52 +148,6 @@ describe('approvalService', () => { }) }) - it('does not persist an approval when its durable request event cannot be appended', async () => { - const database = openBuddyDatabase({ databasePath: ':memory:' }) - databases.push(database) - prepareTestTurnRequest(database, { - attachmentBindings: [], - branchId: 'branch-1', - conversationId: 'conversation-1', - createdAt: '2026-08-14T00:00:00.000Z', - model: 'model-1', - spaceId: null, - provider: 'provider-1', - requestFingerprint: 'fingerprint-request-failure', - approvalPolicy: 'policy' as const, - executionProfile: 'workspace_write', - requestId: 'request-request-failure', - runInput: { - attachmentIds: [], - contextItems: [], - prompt: 'hello', - reasoning: null, - serviceTier: null, - }, - runId: 'run-request-failure', - title: null, - userMessageContent: { text: 'hello' }, - userMessageId: 'message-request-failure', - }) - const repository = createApprovalRepository(database) - const service = new ApprovalService({ - eventLog: { append: vi.fn().mockRejectedValue(new Error('event storage unavailable')) }, - repository, - }) - - await expect(service.request({ - reuseScopes: ['operation', 'source', 'turn'], - arguments: { command: 'pnpm test' }, - kind: 'shell', - runId: 'run-request-failure', - signal: new AbortController().signal, - summary: 'Run tests', - toolCallId: 'tool-request-failure', - toolName: 'bash', - })).rejects.toThrow('event storage unavailable') - expect(repository.list({ runId: 'run-request-failure' })).toEqual([]) - }) - it('allows eligible approvals only for the active turn and preserves explicit confirmations', async () => { const database = openBuddyDatabase({ databasePath: ':memory:' }) databases.push(database) @@ -344,7 +273,7 @@ describe('approvalService', () => { }) }) - it('does not arm an expiry timer after an already durable request is aborted', async () => { + it('cancels a durable request when abort races its append acknowledgement', async () => { const database = openBuddyDatabase({ databasePath: ':memory:' }) databases.push(database) prepareTestTurnRequest(database, { @@ -392,7 +321,6 @@ describe('approvalService', () => { repository, }) const controller = new AbortController() - vi.useFakeTimers() const decision = service.request({ reuseScopes: ['operation', 'source', 'turn'], @@ -413,68 +341,6 @@ describe('approvalService', () => { await expect(decision).rejects.toBeInstanceOf(ApprovalCancelledError) expect(repository.listPending('run-abort')).toEqual([]) - expect(vi.getTimerCount()).toBe(0) - }) - - it('keeps an approval pending when its durable resolution event cannot be appended', async () => { - const database = openBuddyDatabase({ databasePath: ':memory:' }) - databases.push(database) - prepareTestTurnRequest(database, { - attachmentBindings: [], - branchId: 'branch-1', - conversationId: 'conversation-1', - createdAt: '2026-08-14T00:00:00.000Z', - model: 'model-1', - spaceId: null, - provider: 'provider-1', - requestFingerprint: 'fingerprint-1', - approvalPolicy: 'policy' as const, - executionProfile: 'workspace_write', - requestId: 'request-1', - runInput: { - attachmentIds: [], - contextItems: [], - prompt: 'hello', - reasoning: null, - serviceTier: null, - }, - runId: 'run-1', - title: null, - userMessageContent: { text: 'hello' }, - userMessageId: 'message-1', - }) - const repository = createApprovalRepository(database) - const eventLog = await createEventLog(database) - const requested = deferred() - const append = vi.fn(async (input: AppendBuddyRunEventInput) => { - if (input.type === 'approval.resolved') - throw new Error('event storage unavailable') - const event = await eventLog.append(input) - requested.resolve() - return event - }) - const service = new ApprovalService({ eventLog: { append }, repository }) - const decision = service.request({ - reuseScopes: ['operation', 'source', 'turn'], - arguments: { command: 'pnpm test' }, - kind: 'shell', - runId: 'run-1', - signal: new AbortController().signal, - summary: 'Run tests', - toolCallId: 'tool-1', - toolName: 'bash', - }) - void decision.catch(() => {}) - await requested.promise - const approval = repository.listPending()[0]! - - await expect(service.resolve({ decision: 'approved', id: approval.id })) - .rejects - .toThrow('event storage unavailable') - expect(repository.findById(approval.id)).toMatchObject({ - resolvedAt: null, - status: 'pending', - }) }) it('cancels an aborted request after a concurrent resolution append fails', async () => { @@ -543,7 +409,6 @@ describe('approvalService', () => { await expect(resolving).rejects.toThrow('event projection failed') await expect(decision).rejects.toBeInstanceOf(ApprovalCancelledError) - expect(append).toHaveBeenCalledTimes(3) expect(repository.findById(approval.id)?.status).toBe('cancelled') }) }) diff --git a/apps/buddy/service/src/approvals/approvalReview.ts b/apps/buddy/service/src/approvals/approvalReview.ts new file mode 100644 index 000000000..0b0a4b28c --- /dev/null +++ b/apps/buddy/service/src/approvals/approvalReview.ts @@ -0,0 +1,60 @@ +import type { z } from 'zod' +import type { ApprovalReview } from '../../../shared/permissions/conditionalApproval' +import type { ApprovalRequest, ApprovalResolutionDecision } from './ApprovalService' +import { Buffer } from 'node:buffer' +import { createHash, randomUUID } from 'node:crypto' +import { redactSensitiveText } from '../../../shared/permissions/approvalReviewPayload' +import { approvalReviewSchema } from '../../../shared/permissions/conditionalApproval' +import { canonicalCommandJson } from '../commands/commandIntent' + +export { approvalReviewSchema } from '../../../shared/permissions/conditionalApproval' +export type { ApprovalReview } from '../../../shared/permissions/conditionalApproval' + +const sensitive = /password|passwd|secret|token|credential|authorization|cookie|api[_-]?key|private[_-]?key/i +const maximumReviewBytes = 1024 * 1024 + +export function createApprovalReview(input: ApprovalRequest, approvalId: string, createdAt: string, timeoutMs: number, scopes: readonly string[]): ApprovalReview { + const operation = JSON.parse(JSON.stringify({ + kind: input.kind, + toolName: input.toolName, + arguments: input.arguments, + cwd: input.cwd, + paths: input.paths, + browser: input.browser, + automation: input.automation, + shell: input.shell, + systemAction: input.systemAction, + network: input.network, + networkPermission: input.networkPermission, + sandboxDirectory: input.sandboxDirectory, + persistentDirectory: input.persistentDirectory, + })) + const operationDigest = digest(canonicalCommandJson(operation)) + const operationBytes = Buffer.byteLength(JSON.stringify(operation), 'utf8') + const complete = operationBytes <= maximumReviewBytes + const fullBody = complete + ? JSON.parse(JSON.stringify(operation, (key, value) => sensitive.test(key) ? '[redacted]' : typeof value === 'string' ? redactSensitiveText(value) : value)) as z.infer> + : { reason: 'review_exceeds_inline_limit', bytes: operationBytes } + const persistentEffect = Boolean(input.persist || input.persistentDirectory || input.paths?.grant) + const allowedDecisions: ApprovalResolutionDecision[] = ['denied'] + if (complete) { + allowedDecisions.push('approved', ...scopes.map(scope => `approved_for_${scope}` as ApprovalResolutionDecision)) + if (input.persist) + allowedDecisions.push('approved_persistently') + } + const content = { + contentVersion: 1 as const, + operationDigest, + body: fullBody, + complete, + expiresAt: new Date(Date.parse(createdAt) + timeoutMs).toISOString(), + lifetimeId: randomUUID(), + allowedDecisions, + persistentEffect, + } + return approvalReviewSchema.parse({ ...content, reviewVersion: digest(canonicalCommandJson({ approvalId, runId: input.runId, toolCallId: input.toolCallId, ...content })) }) +} + +function digest(value: string): string { + return createHash('sha256').update(value, 'utf8').digest('hex') +} diff --git a/apps/buddy/service/src/approvals/registerApprovalRpc.ts b/apps/buddy/service/src/approvals/registerApprovalRpc.ts index 821fd8f2e..9be3acb9d 100644 --- a/apps/buddy/service/src/approvals/registerApprovalRpc.ts +++ b/apps/buddy/service/src/approvals/registerApprovalRpc.ts @@ -4,11 +4,12 @@ import type { ApprovalRepository, } from '../storage/approvalRepository' import type { ApprovalService } from './ApprovalService' -import { approvalsRpc } from '../../../shared/permissions/approvalApi' +import { approvalsRequestSchemas, approvalsRpc } from '../../../shared/permissions/approvalApi' import { APPROVAL_REVIEW_KINDS } from '../../../shared/permissions/approvalReviewPayload' import { registerRuntimeRequest } from '../rpc/runtimeRequest' export interface RegisterApprovalRpcOptions { + commandService?: Pick repository: Pick rpc: RuntimeRequestRegistrar service: Pick @@ -18,7 +19,7 @@ export function registerApprovalRpc(options: RegisterApprovalRpcOptions): () => const disposers: Array<() => void> = [] disposers.push(registerRuntimeRequest(options.rpc, approvalsRpc.list, (input) => { - return options.repository.list(input).map(toPublicApproval) + return options.repository.list(input).map(approval => toPublicApproval(approval, Boolean(options.commandService))) })) disposers.push(registerRuntimeRequest(options.rpc, approvalsRpc.approve, async (input) => { return toPublicApproval(await options.service.resolve({ @@ -33,12 +34,25 @@ export function registerApprovalRpc(options: RegisterApprovalRpcOptions): () => })) })) + if (options.commandService) { + const service = options.commandService + disposers.push(options.rpc.onRequest('approvals.resolve', input => service.resolveCommand(input))) + disposers.push(options.rpc.onRequest('approvals.getReview', input => service.getReview(approvalsRequestSchemas.approvalId.parse(input).approvalId))) + } return () => disposers.splice(0).forEach(dispose => dispose()) } -function toPublicApproval(approval: ApprovalRecord) { +function toPublicApproval(approval: ApprovalRecord, conditional = false) { return { - ...approval, + id: approval.id, + runId: approval.runId, + toolCallId: approval.toolCallId, + status: approval.status, + summary: approval.summary, + payload: approval.payload, + ...(conditional || approval.review !== undefined ? { review: approval.review ?? null } : {}), + createdAt: approval.createdAt, + resolvedAt: approval.resolvedAt, kind: isApprovalKind(approval.kind) ? approval.kind : 'system', } } diff --git a/apps/buddy/service/src/attachments/AttachmentError.ts b/apps/buddy/service/src/attachments/AttachmentError.ts new file mode 100644 index 000000000..1ada64135 --- /dev/null +++ b/apps/buddy/service/src/attachments/AttachmentError.ts @@ -0,0 +1,9 @@ +export class AttachmentError extends Error { + readonly code: string + + constructor(code: string, options?: ErrorOptions) { + super('Lexora Buddy attachment operation failed', options) + this.name = 'AttachmentError' + this.code = code + } +} diff --git a/apps/buddy/service/src/attachments/AttachmentInputMaterializer.ts b/apps/buddy/service/src/attachments/AttachmentInputMaterializer.ts new file mode 100644 index 000000000..e1a8fe53b --- /dev/null +++ b/apps/buddy/service/src/attachments/AttachmentInputMaterializer.ts @@ -0,0 +1,258 @@ +import type { ImageContent } from '@earendil-works/pi-ai' +import type { BuddyPromptDirective, BuddyUserContentV1, BuddyUserMessageContentV1, BuddyUserMessageResourceSnapshot } from '../../../shared/conversation/buddyUserContent' +import type { InputMaterialProjectionState } from '../../../shared/conversation/inputMaterialProjection' +import type { BuddyInputReferenceV1 } from '../agent/context/BuddyInputReference' +import type { InputModel } from '../providers/modelCapabilities' +import type { AttachmentRecord } from '../storage/attachmentRepository' +import type { AttachmentDocumentReference, AttachmentFileInput } from './AttachmentDocumentReference' +import type { AttachmentImageReference } from './AttachmentImageReference' +import type { PreparedAttachmentImage } from './AttachmentImageStore' +import type { AttachmentInputReader } from './AttachmentInputReader' +import type { AttachmentToolWorkspace } from './AttachmentToolWorkspace' +import type { InputMaterialProjector } from './InputMaterialProjector' +import { isDocumentMimeType } from '../../../shared/conversation/attachmentFormats' +import { BUDDY_ATTACHMENT_TOTAL_BYTES_LIMIT, isTextAttachmentMimeType } from '../../../shared/conversation/attachmentPolicy' +import { AttachmentError } from './AttachmentError' +import { AttachmentImageStore } from './AttachmentImageStore' +import { getAttachmentLabels } from './attachmentLabels' +import { toDocumentReference, toImageReference, validateTotalBytes } from './attachmentMetadata' +import { hasDocumentSignature } from './validateDocumentBytes' + +export interface AttachmentRecoveryResult { + resourceLabels?: Record + documents: AttachmentDocumentReference[] + images: AttachmentImageReference[] + missingAttachmentIds: string[] +} + +export class AttachmentInputMaterializer { + readonly #reader: AttachmentInputReader + readonly #projector: InputMaterialProjector + readonly #images = new AttachmentImageStore() + readonly #imageDirectory: (conversationId: string) => string + readonly #listForConversation: (conversationId: string) => AttachmentRecord[] + + constructor(options: { reader: AttachmentInputReader, projector: InputMaterialProjector, imageDirectory: (conversationId: string) => string, listForConversation: (conversationId: string) => AttachmentRecord[] }) { + this.#reader = options.reader + this.#projector = options.projector + this.#imageDirectory = options.imageDirectory + this.#listForConversation = options.listForConversation + } + + async materializeConversationImages( + conversationId: string, + ids?: readonly string[], + ): Promise<{ images: ImageContent[], records: AttachmentRecord[] }> { + const available = this.#listForConversation(conversationId) + .filter(record => record.mimeType.startsWith('image/')) + const selectedIds = ids === undefined + ? available.slice(-1).map(record => record.id) + : ids + if (selectedIds.length === 0) + throw new AttachmentError('ATTACHMENT_NOT_FOUND') + const materialized = await this.materializePrompt(selectedIds, '', conversationId) + if (materialized.images.length !== selectedIds.length) + throw new AttachmentError('VALIDATION_FAILED') + return { + images: materialized.images, + records: materialized.records, + } + } + + async materializePrompt( + ids: readonly string[], + content: string, + conversationId: string | null = null, + draftId: string | null = null, + composer?: { content: BuddyUserContentV1, resourceIds: readonly string[], resources?: readonly BuddyUserMessageResourceSnapshot[], resolveDirective?: (directive: BuddyPromptDirective) => string, sourceMessageId?: string, quoteReadAvailable?: boolean, inlineBytes?: number }, + ): Promise<{ documents: AttachmentFileInput[], images: ImageContent[], prompt: string, records: AttachmentRecord[], materialProjection?: InputMaterialProjectionState }> { + const prepared = await this.#projector.preparePrompt(ids, content, conversationId, draftId, composer) + return { + documents: await this.materializeDocumentInputs(prepared.documentReferences, conversationId, draftId), + images: await this.materializePiInputImages( + prepared.imageReferences, + conversationId, + draftId, + ), + prompt: prepared.prompt, + records: prepared.records, + } + } + + async materializeInputResources(input: BuddyInputReferenceV1, conversationId: string, workspace: AttachmentToolWorkspace, message?: Pick | null): Promise { + const ids = input.attachmentIds ?? [...input.images, ...input.documents ?? []].map(file => file.attachmentId) + const records = ids.map(id => this.#reader.get(id, { conversationId })) + const nativeIds = new Set([...input.images, ...input.documents ?? []].map(file => file.attachmentId)) + const labels = input.resourceLabels ?? getAttachmentLabels(records, input.prompt) + const resources = await Promise.all(records.map(async (record) => { + const bytes = await this.#reader.read(record) + if (bytes.length !== record.sizeBytes || (isDocumentMimeType(record.mimeType) && !hasDocumentSignature(record.mimeType, bytes))) + throw new AttachmentError('VALIDATION_FAILED') + const path = await workspace.materialize(record, bytes) + const savedResource = message?.resourceSnapshots.find(resource => resource.attachmentId === record.id) + const deferred = savedResource && message?.materialProjection?.deferredResourceIds.includes(savedResource.resourceId) + return { + ...(deferred ? { snapshotRead: { sourceMessageId: input.messageId, referenceId: savedResource.resourceId, kind: 'attachment' } } : {}), + attachmentId: record.id, + label: labels[record.id], + name: record.name, + mimeType: record.mimeType, + path, + sourcePath: record.sourcePath, + content: nativeIds.has(record.id) ? 'native' : isTextAttachmentMimeType(record.mimeType) && !deferred ? 'text' : 'file_only', + } + })) + return resources.length ? `\n${JSON.stringify(resources)}\n` : '' + } + + async resolveInputReferences( + ids: readonly string[], + conversationId: string, + prompt = '', + ): Promise<{ images: AttachmentImageReference[], documents: AttachmentDocumentReference[], resourceLabels?: Record }> { + const records = ids.map(id => this.#reader.get(id, { conversationId })) + validateTotalBytes(records.map(record => record.sizeBytes)) + return { + resourceLabels: getAttachmentLabels(records, prompt), + images: records.flatMap(record => ( + record.mimeType.startsWith('image/') && record.mimeType !== 'image/svg+xml' + ? [toImageReference(record)] + : [] + )), + documents: records.filter(record => isDocumentMimeType(record.mimeType)).map(toDocumentReference), + } + } + + async materializePiInputImages( + references: readonly AttachmentImageReference[], + conversationId: string | null, + draftId: string | null = null, + ): Promise { + const records = references.map((reference) => { + const record = this.#reader.get(reference.attachmentId, { conversationId, draftId }) + if ( + record.mimeType !== reference.mimeType + || !record.mimeType.startsWith('image/') + || record.mimeType === 'image/svg+xml' + ) { + throw new AttachmentError('VALIDATION_FAILED') + } + return record + }) + validateTotalBytes(records.map(record => record.sizeBytes)) + return Promise.all(records.map(async (record) => { + try { + return { + data: (await this.#reader.read(record)).toString('base64'), + mimeType: record.mimeType, + type: 'image' as const, + } + } + catch (error) { + if (isFileNotFound(error)) + throw new AttachmentError('ATTACHMENT_NOT_FOUND', { cause: error }) + throw error + } + })) + } + + async prepareInputImages( + references: readonly AttachmentImageReference[], + conversationId: string, + model: InputModel, + ): Promise> { + const images = new Map() + for (const reference of references) { + const record = this.#reader.get(reference.attachmentId, { conversationId }) + if (record.mimeType !== reference.mimeType) + throw new AttachmentError('VALIDATION_FAILED') + const metadata = await this.#reader.inspect(record) + if (!metadata.isFile || metadata.size !== record.sizeBytes || metadata.size > BUDDY_ATTACHMENT_TOTAL_BYTES_LIMIT) + throw new AttachmentError('ATTACHMENT_INVALID') + const bytes = await this.#reader.read(record) + const directory = this.#imageDirectory(conversationId) + images.set(record.id, await this.#images.prepare(record, bytes, directory, model)) + } + return images + } + + async materializeDocumentInputs( + references: readonly AttachmentDocumentReference[], + conversationId: string | null, + draftId: string | null = null, + ): Promise { + const records = references.map((reference) => { + const record = this.#reader.get(reference.attachmentId, { conversationId, draftId }) + if (!isDocumentMimeType(record.mimeType) || reference.mimeType !== record.mimeType) + throw new AttachmentError('VALIDATION_FAILED') + return record + }) + validateTotalBytes(records.map(record => record.sizeBytes)) + return Promise.all(records.map(async (record) => { + try { + const bytes = await this.#reader.read(record) + if (bytes.length !== record.sizeBytes || !hasDocumentSignature(record.mimeType, bytes)) + throw new AttachmentError('VALIDATION_FAILED') + return { data: bytes.toString('base64'), name: record.name, mimeType: toDocumentReference(record).mimeType } + } + catch (error) { + if (isFileNotFound(error)) + throw new AttachmentError('ATTACHMENT_NOT_FOUND', { cause: error }) + throw error + } + })) + } + + async resolveRecoveryInputReferences( + ids: readonly string[], + conversationId: string, + prompt = '', + ): Promise { + const resolved = ids.map((id) => { + try { + return { id, record: this.#reader.get(id, { conversationId }) } + } + catch (error) { + if (error instanceof AttachmentError && error.code === 'ATTACHMENT_NOT_FOUND') + return { id, record: null } + throw error + } + }) + validateTotalBytes(resolved.flatMap(item => item.record ? [item.record.sizeBytes] : [])) + const recovered = await Promise.all(resolved.map(async ({ id, record }): Promise<{ + images: AttachmentImageReference[] + documents?: AttachmentDocumentReference[] + missingAttachmentId: string | null + }> => { + if (!record) + return { images: [], missingAttachmentId: id } + try { + const metadata = await this.#reader.inspect(record) + if (!metadata.isFile || metadata.size !== record.sizeBytes) + return { images: [], missingAttachmentId: id } + if (!isDocumentMimeType(record.mimeType) && (!record.mimeType.startsWith('image/') || record.mimeType === 'image/svg+xml')) + return { images: [], missingAttachmentId: null } + return isDocumentMimeType(record.mimeType) + ? { documents: [toDocumentReference(record)], images: [], missingAttachmentId: null } + : { images: [toImageReference(record)], missingAttachmentId: null } + } + catch (error) { + if (isFileNotFound(error)) + return { images: [], missingAttachmentId: id } + throw error + } + })) + return { + resourceLabels: resolved.every(item => item.record) ? getAttachmentLabels(resolved.map(item => item.record!), prompt) : {}, + documents: recovered.flatMap(item => item.documents ?? []), + images: recovered.flatMap(item => item.images), + missingAttachmentIds: recovered.flatMap( + item => item.missingAttachmentId ? [item.missingAttachmentId] : [], + ), + } + } +} + +function isFileNotFound(error: unknown): boolean { + return (error as NodeJS.ErrnoException | undefined)?.code === 'ENOENT' +} diff --git a/apps/buddy/service/src/attachments/AttachmentInputReader.ts b/apps/buddy/service/src/attachments/AttachmentInputReader.ts new file mode 100644 index 000000000..26daf9a62 --- /dev/null +++ b/apps/buddy/service/src/attachments/AttachmentInputReader.ts @@ -0,0 +1,14 @@ +import type { Buffer } from 'node:buffer' +import type { AttachmentRecord } from '../storage/attachmentRepository' + +export interface AttachmentInputScope { + conversationId: string | null + draftId?: string | null + legacyResourceDraftId?: string +} + +export interface AttachmentInputReader { + get: (id: string, scope: AttachmentInputScope) => AttachmentRecord + read: (record: AttachmentRecord) => Promise + inspect: (record: AttachmentRecord) => Promise<{ isFile: boolean, size: number }> +} diff --git a/apps/buddy/service/src/attachments/AttachmentService.ts b/apps/buddy/service/src/attachments/AttachmentService.ts index a81af0db7..828566ff1 100644 --- a/apps/buddy/service/src/attachments/AttachmentService.ts +++ b/apps/buddy/service/src/attachments/AttachmentService.ts @@ -1,65 +1,38 @@ -import type { ImageContent } from '@earendil-works/pi-ai' import type { Buffer } from 'node:buffer' import type { BuddyAttachmentUpload } from '../../../shared/conversation/attachmentPolicy' -import type { BuddyPromptDirective, BuddyUserContentV1, BuddyUserMessageContentV1, BuddyUserMessageResourceSnapshot } from '../../../shared/conversation/buddyUserContent' -import type { InputMaterialProjectionState } from '../../../shared/conversation/inputMaterialProjection' -import type { BuddyInputReferenceV1 } from '../agent/context/BuddyInputReference' -import type { InputModel } from '../providers/modelCapabilities' import type { AttachmentRecord, AttachmentRepository } from '../storage/attachmentRepository' import type { BuddyDataPaths } from '../storage/BuddyDataPaths' -import type { AttachmentDocumentReference, AttachmentFileInput } from './AttachmentDocumentReference' import type { AttachmentChange } from './attachmentEvents' -import type { AttachmentImageReference } from './AttachmentImageReference' -import type { PreparedAttachmentImage } from './AttachmentImageStore' -import type { AttachmentToolWorkspace } from './AttachmentToolWorkspace' -import { randomUUID } from 'node:crypto' -import { constants } from 'node:fs' -import { chmod, copyFile, mkdir, open, readdir, readFile, realpath, rmdir, stat, unlink, writeFile } from 'node:fs/promises' -import { basename, dirname, extname, join, normalize } from 'node:path' +import type { AttachmentInputScope } from './AttachmentInputReader' +import { createHash, randomUUID } from 'node:crypto' +import { constants, createReadStream } from 'node:fs' +import { chmod, copyFile, mkdir, open, readdir, readFile, realpath, rmdir, stat, statfs, unlink, writeFile } from 'node:fs/promises' +import { basename, dirname, extname, isAbsolute, join, normalize, relative, sep } from 'node:path' import { readBoundedFile } from '../../../platform/filesystem/boundedFile' import { fileStorage } from '../../../platform/filesystem/fileStorage' -import { BUDDY_MEDIA_EXTENSIONS, BUDDY_MEDIA_FILE_BYTES_LIMIT, isDocumentMimeType } from '../../../shared/conversation/attachmentFormats' +import { isDocumentMimeType } from '../../../shared/conversation/attachmentFormats' import { BUDDY_ATTACHMENT_COUNT_LIMIT, BUDDY_ATTACHMENT_TOTAL_BYTES_LIMIT, BUDDY_TEXT_ATTACHMENT_BYTES_LIMIT, - BUDDY_TEXT_ATTACHMENT_EXTENSIONS, - getAttachmentKind, isTextAttachmentMimeType, } from '../../../shared/conversation/attachmentPolicy' -import { projectBuddyUserContent } from '../../../shared/conversation/buddyUserContentProjection' -import { InputMaterialProjectionBudget, InputMaterialProjectionError } from '../../../shared/conversation/inputMaterialProjection' import { Emitter } from '../../../shared/events/Emitter' import { copyEventSnapshot } from '../../../shared/events/eventSnapshot' -import { AttachmentImageStore } from './AttachmentImageStore' -import { getAttachmentLabels } from './attachmentLabels' +import { ATTACHMENT_PREVIEW_BYTES_LIMIT } from '../../../shared/runtime/attachmentHostProtocol' +import { readTransactionFailure } from '../storage/transaction' +import { AttachmentError } from './AttachmentError' + +import { AttachmentInputMaterializer } from './AttachmentInputMaterializer' +import { normalizeAttachmentMetadata, validateTotalBytes } from './attachmentMetadata' +import { InputMaterialProjector } from './InputMaterialProjector' import { hasDocumentSignature } from './validateDocumentBytes' -export const DRAFT_ATTACHMENT_RETENTION_MS = 7 * 24 * 60 * 60 * 1000 -const { replace: rename, syncDirectory } = fileStorage -const MIME_TYPES: Readonly> = { - ...BUDDY_MEDIA_EXTENSIONS, - '.csv': 'text/csv', - '.gif': 'image/gif', - '.jpeg': 'image/jpeg', - '.jpg': 'image/jpeg', - '.json': 'application/json', - '.md': 'text/markdown', - '.pdf': 'application/pdf', - '.png': 'image/png', - '.toml': 'application/toml', - '.tsv': 'text/tab-separated-values', - '.txt': 'text/plain', - '.webp': 'image/webp', - '.xml': 'application/xml', - '.yaml': 'application/yaml', - '.yml': 'application/yaml', -} -const SUPPORTED_IMAGE_MIME_TYPES = new Set( - Object.values(MIME_TYPES).filter(mimeType => mimeType.startsWith('image/')), -) -const textExtensions = new Set(BUDDY_TEXT_ATTACHMENT_EXTENSIONS) +export { AttachmentError } from './AttachmentError' +export type { AttachmentRecoveryResult } from './AttachmentInputMaterializer' +export { inferMimeType, normalizeAttachmentMetadata } from './attachmentMetadata' +export const DRAFT_ATTACHMENT_RETENTION_MS = 7 * 24 * 60 * 60 * 1000 export interface AttachmentServiceOptions { assertConversationWritable?: (conversationId: string) => void paths: BuddyDataPaths @@ -72,13 +45,6 @@ export interface AttachmentFileReader { (path: string, encoding: 'utf8'): Promise } -export interface AttachmentRecoveryResult { - resourceLabels?: Record - documents: AttachmentDocumentReference[] - images: AttachmentImageReference[] - missingAttachmentIds: string[] -} - export interface AttachmentStorageReconciliation { invalidAttachmentIds: string[] missingAttachmentIds: string[] @@ -103,22 +69,31 @@ export interface PreparedMessageAttachments { bindings: readonly Readonly[] commit: () => Promise rollback: () => Promise + retain: () => Promise +} + +export interface PreparedDraftAttachmentCopy extends PreparedAttachmentUploads { + validate: () => void } export interface PreparedAttachmentUploads { readonly records: readonly Readonly[] commit: () => Promise rollback: () => Promise + retain: () => Promise } export class AttachmentService { readonly #changes = new Emitter(() => console.error('ATTACHMENT_OBSERVER_FAILED')) readonly onDidChange = this.#changes.event readonly #leasedPaths = new Map() + readonly #ownerInstance = randomUUID() + readonly #pendingReads = new Set>() #revision = 0 #storageTail = Promise.resolve() #stopping = false - readonly #images = new AttachmentImageStore() + readonly #projector: InputMaterialProjector + readonly #materializer: AttachmentInputMaterializer readonly #paths: BuddyDataPaths readonly #readFile: AttachmentFileReader readonly #repository: AttachmentRepository @@ -129,6 +104,17 @@ export class AttachmentService { this.#readFile = options.readFile ?? readFile this.#repository = options.repository this.#assertConversationWritable = options.assertConversationWritable + const reader = { + get: (id: string, scope: AttachmentInputScope) => this.#requireForPrompt(id, scope.conversationId, scope.draftId ?? null, scope.legacyResourceDraftId), + read: (record: AttachmentRecord) => this.#readBytes(record), + inspect: async (record: AttachmentRecord) => { + this.#assertFileReady(record) + const metadata = await stat(record.storedPath) + return { isFile: metadata.isFile(), size: metadata.size } + }, + } + this.#projector = new InputMaterialProjector(reader) + this.#materializer = new AttachmentInputMaterializer({ reader, projector: this.#projector, imageDirectory: id => join(this.#paths.conversationDirectory(id), 'image-inputs'), listForConversation: id => this.listForConversation(id) }) } registerFiles(draftId: string, paths: readonly string[], limits = { count: BUDDY_ATTACHMENT_COUNT_LIMIT, errorCode: 'VALIDATION_FAILED', totalBytes: BUDDY_ATTACHMENT_TOTAL_BYTES_LIMIT }): Promise { @@ -150,8 +136,8 @@ export class AttachmentService { const path = normalize(record.storedPath) this.#leasedPaths.set(path, (this.#leasedPaths.get(path) ?? 0) + 1) } - let settlement: { outcome: 'commit' | 'rollback', promise: Promise } | undefined - const settle = (outcome: 'commit' | 'rollback'): Promise => { + let settlement: { outcome: 'commit' | 'rollback' | 'retain', promise: Promise } | undefined + const settle = (outcome: 'commit' | 'rollback' | 'retain'): Promise => { if (settlement) { return settlement.outcome === outcome ? settlement.promise @@ -176,11 +162,11 @@ export class AttachmentService { settlement = { outcome, promise } return promise } - return Object.freeze({ records: copyEventSnapshot(records), commit: () => settle('commit'), rollback: () => settle('rollback') }) + return Object.freeze({ records: copyEventSnapshot(records), commit: () => settle('commit'), rollback: () => settle('rollback'), retain: () => settle('retain') }) }) } - prepareMessageAttachments(input: { attachmentIds: readonly string[], conversationId: string, draftId: string, messageId: string }): Promise { + prepareMessageAttachments(input: { attachmentIds: readonly string[], conversationId: string, draftId: string, messageId: string, legacyResourceDraftId?: string }): Promise { const owned = { ...input, attachmentIds: [...input.attachmentIds] } return this.#storage(() => { this.#assertConversationWritable?.(owned.conversationId) @@ -188,6 +174,50 @@ export class AttachmentService { }) } + prepareDraftCopy(input: { sourceDraftId: string, targetDraftId: string, attachmentIds: readonly string[] }): Promise { + const owned = { ...input, attachmentIds: [...input.attachmentIds] } + return this.#storage(async () => { + if (owned.sourceDraftId === owned.targetDraftId) + throw new AttachmentError('VALIDATION_FAILED') + const sources = owned.attachmentIds.map(id => this.#requireForPrompt(id, null, owned.sourceDraftId)) + const prepared = await this.#prepareMessageAttachments({ attachmentIds: owned.attachmentIds, conversationId: '', draftId: owned.sourceDraftId, messageId: owned.targetDraftId, targetDraftId: owned.targetDraftId }) + const records = prepared.bindings.map((binding): AttachmentRecord => ({ + id: binding.id, + draftId: owned.targetDraftId, + messageId: null, + conversationId: null, + storedPath: binding.storedPath, + createdAt: binding.createdAt, + name: binding.name, + nameSource: binding.nameSource, + mimeType: binding.mimeType, + sizeBytes: binding.sizeBytes, + sourcePath: sources.find(source => source.id === binding.sourceAttachmentId)?.sourcePath, + })) + let settled = false + const settle = (outcome: 'commit' | 'rollback' | 'retain') => { + settled = true + return prepared[outcome]() + } + return Object.freeze({ + records: copyEventSnapshot(records), + validate: () => { + if (settled) + throw new Error('ATTACHMENT_PREPARATION_SETTLED') + for (const source of sources) { + if (JSON.stringify(this.#repository.findById(source.id)) !== JSON.stringify(source)) + throw new AttachmentError('ATTACHMENT_NOT_FOUND') + this.#assertFileReady(source) + } + for (const record of records) this.#assertFileReady(record) + }, + commit: () => settle('commit'), + rollback: () => settle('rollback'), + retain: () => settle('retain'), + }) + }) + } + release(ids: readonly string[]): Promise { const owned = [...ids] return this.#storage(() => this.#release(owned)) @@ -212,6 +242,7 @@ export class AttachmentService { tail = this.#storageTail await tail } while (tail !== this.#storageTail) + await Promise.allSettled([...this.#pendingReads]) if (this.#leasedPaths.size) throw new Error('ATTACHMENT_PREPARATIONS_PENDING') this.#changes.dispose() @@ -273,16 +304,21 @@ export class AttachmentService { storedPath, } attempted.push(record) - await publishFile(sourcePath, storedPath) + const preparation = this.#repository.persistence?.prepare({ operationId, stagingDirectory: join(this.#paths.root, 'managed-files', 'staging'), availableBytes: await availableStorageBytes(directory), ownerInstance: this.#ownerInstance, kind: 'import', targetOwnerId: draftId, path: storedPath, sizeBytes: record.sizeBytes }) + const proof = await publishFile(sourcePath, storedPath, record.sizeBytes, undefined, preparation?.temporaryPath ?? undefined) + this.#repository.persistence?.publish(storedPath, proof) this.#publish('file-published', 'import', [record.id], operationId) if (isDocumentMimeType(mimeType) && !hasDocumentSignature(mimeType, await this.#readFile(storedPath))) throw new AttachmentError('ATTACHMENT_INVALID') this.#repository.create(record) + this.#repository.persistence?.settle([storedPath], 'commit') this.#publish('registered', 'import', [record.id], operationId) records.push(record) } } catch (error) { + if (readTransactionFailure(error)?.outcome === 'unknown') + throw error try { await this.#rollbackRegistration(attempted, operationId) } @@ -328,14 +364,19 @@ export class AttachmentService { storedPath, } attempted.push(record) - await publishBytes(source.bytes, storedPath) + const preparation = this.#repository.persistence?.prepare({ operationId, stagingDirectory: join(this.#paths.root, 'managed-files', 'staging'), availableBytes: await availableStorageBytes(directory), ownerInstance: this.#ownerInstance, kind: 'import', targetOwnerId: draftId, path: storedPath, sizeBytes: record.sizeBytes }) + const proof = await publishBytes(source.bytes, storedPath, preparation?.temporaryPath ?? undefined) + this.#repository.persistence?.publish(storedPath, proof) this.#publish('file-published', 'import', [record.id], operationId) this.#repository.create(record) + this.#repository.persistence?.settle([storedPath], 'commit') this.#publish('registered', 'import', [record.id], operationId) records.push(record) } } catch (error) { + if (readTransactionFailure(error)?.outcome === 'unknown') + throw error try { await this.#rollbackRegistration(attempted, operationId) } @@ -350,11 +391,14 @@ export class AttachmentService { conversationId: string draftId: string messageId: string + targetDraftId?: string + legacyResourceDraftId?: string }): Promise { const records = input.attachmentIds.map(id => this.#requireForPrompt( id, input.conversationId, input.draftId, + input.legacyResourceDraftId, )) validateTotalBytes(records.map(record => record.sizeBytes)) if (records.length === 0) { @@ -362,9 +406,10 @@ export class AttachmentService { bindings: [], commit: () => Promise.resolve(), rollback: () => Promise.resolve(), + retain: () => Promise.resolve(), } } - const directory = this.#paths.messageInputs(input.conversationId, input.messageId) + const directory = input.targetDraftId ? this.#paths.draftAttachments(input.targetDraftId) : this.#paths.messageInputs(input.conversationId, input.messageId) await mkdir(directory, { mode: 0o700, recursive: true }) const operationId = randomUUID() const bindings: MessageAttachmentBinding[] = [] @@ -385,14 +430,16 @@ export class AttachmentService { } try { for (const record of records) { - const id = record.draftId === input.draftId ? record.id : randomUUID() + const id = !input.targetDraftId && record.draftId === input.draftId ? record.id : randomUUID() const storedPath = join(directory, `${id}${safeExtension(record.name)}`) if (this.#leasedPaths.has(normalize(storedPath)) || this.#repository.listAll().some(owned => normalize(owned.storedPath) === normalize(storedPath))) throw new AttachmentError('VALIDATION_FAILED') lease(record.storedPath) lease(storedPath) attemptedPaths.push(storedPath) - await publishFile(record.storedPath, storedPath) + const preparation = this.#repository.persistence?.prepare({ operationId, stagingDirectory: join(this.#paths.root, 'managed-files', 'staging'), availableBytes: await availableStorageBytes(directory), ownerInstance: this.#ownerInstance, kind: input.targetDraftId ? 'copy' : 'message', targetOwnerId: input.targetDraftId ?? input.messageId, path: storedPath, sizeBytes: record.sizeBytes, source: record }) + const proof = await publishFile(record.storedPath, storedPath, record.sizeBytes, this.#repository.persistence?.get(record.storedPath)?.sha256 ?? undefined, preparation?.temporaryPath ?? undefined) + this.#repository.persistence?.publish(storedPath, proof) this.#publish('file-published', 'message', [id], operationId) bindings.push({ createdAt: new Date().toISOString(), @@ -410,8 +457,19 @@ export class AttachmentService { } } catch (error) { + if (readTransactionFailure(error)?.outcome === 'unknown') { + releaseLeases() + throw error + } try { - await removeFiles(attemptedPaths) + if (this.#repository.persistence) { + this.#repository.persistence.settle(attemptedPaths.filter(path => this.#repository.persistence!.get(path)), 'rollback') + releaseLeases() + await this.#collectFiles(attemptedPaths) + } + else { + await removeFiles(attemptedPaths) + } if (attemptedPaths.length) this.#publish('cleanup-completed', 'rollback', bindings.map(binding => binding.id), operationId, attemptedPaths.length) } @@ -423,8 +481,8 @@ export class AttachmentService { throw error } this.#publish('prepared', 'message', bindings.map(binding => binding.id), operationId) - let settlement: { outcome: 'commit' | 'rollback', promise: Promise } | undefined - const settle = (outcome: 'commit' | 'rollback'): Promise => { + let settlement: { outcome: 'commit' | 'rollback' | 'retain', promise: Promise } | undefined + const settle = (outcome: 'commit' | 'rollback' | 'retain'): Promise => { if (settlement) { return settlement.outcome === outcome ? settlement.promise @@ -432,6 +490,8 @@ export class AttachmentService { } const promise = this.#storage(async () => { try { + if (outcome === 'retain') + return const owned = new Set(this.#repository.listAll().map(record => normalize(record.storedPath))) if (outcome === 'commit' && bindings.some(binding => !owned.has(normalize(binding.storedPath)))) throw new Error('ATTACHMENT_OWNERSHIP_NOT_COMMITTED') @@ -439,11 +499,19 @@ export class AttachmentService { ? bindings.flatMap(binding => binding.sourceDraftId ? [binding.sourceStoredPath] : []) : bindings.map(binding => binding.storedPath) const removable = candidates.filter(path => !owned.has(normalize(path))) - await removeFiles(removable) + if (this.#repository.persistence) { + this.#repository.persistence.settle(bindings.map(binding => binding.storedPath), outcome) + releaseLeases() + await this.#collectFiles(removable) + } + else { + await removeFiles(removable) + } this.#publish('cleanup-completed', outcome, bindings.map(binding => binding.id), operationId, removable.length) } catch (error) { - this.#publish('cleanup-failed', outcome, bindings.map(binding => binding.id), operationId) + if (outcome !== 'retain') + this.#publish('cleanup-failed', outcome, bindings.map(binding => binding.id), operationId) throw error } finally { releaseLeases() } @@ -455,6 +523,7 @@ export class AttachmentService { bindings: copyEventSnapshot(bindings), commit: () => settle('commit'), rollback: () => settle('rollback'), + retain: () => settle('retain'), } } @@ -462,18 +531,65 @@ export class AttachmentService { const record = this.#repository.findVisibleById(id) if (!record) throw new AttachmentError('ATTACHMENT_NOT_FOUND') + this.#assertFileReady(record) if (!record.mimeType.startsWith('image/')) throw new AttachmentError('VALIDATION_FAILED') return { mimeType: record.mimeType, path: record.storedPath } } + async readPreview(id: string): Promise<{ mimeType: string, data: string }> { + const preview = this.resolvePreview(id) + const record = this.#requireAvailable(id) + if (record.sizeBytes > ATTACHMENT_PREVIEW_BYTES_LIMIT) + throw new AttachmentError('ATTACHMENT_TOO_LARGE') + const bytes = await this.#readBytes(record, () => readBoundedFile(dirname(preview.path), preview.path, ATTACHMENT_PREVIEW_BYTES_LIMIT)) + return { mimeType: preview.mimeType, data: bytes.toString('base64') } + } + + #readBytes(record: AttachmentRecord, read: () => Promise = () => this.#readFile(record.storedPath)): Promise { + if (this.#stopping) + return Promise.reject(new AttachmentError('RUNTIME_OFFLINE')) + const result = this.#readLeasedBytes(record, read) + this.#pendingReads.add(result) + void result.then(() => this.#pendingReads.delete(result), () => this.#pendingReads.delete(result)) + return result + } + + async #readLeasedBytes(record: AttachmentRecord, read: () => Promise): Promise { + this.#assertFileReady(record) + const path = normalize(record.storedPath) + this.#leasedPaths.set(path, (this.#leasedPaths.get(path) ?? 0) + 1) + try { + const bytes = await read() + const expected = this.#repository.persistence?.get(record.storedPath) + if (bytes.byteLength !== record.sizeBytes || (expected?.sha256 && createHash('sha256').update(bytes).digest('hex') !== expected.sha256)) { + this.#repository.persistence?.unavailable(record.storedPath) + throw new AttachmentError('ATTACHMENT_INVALID') + } + return bytes + } + catch (error) { + if (isFileNotFound(error)) { + this.#repository.persistence?.unavailable(record.storedPath) + throw new AttachmentError('ATTACHMENT_NOT_FOUND', { cause: error }) + } + throw error + } + finally { + const remaining = (this.#leasedPaths.get(path) ?? 1) - 1 + if (remaining) + this.#leasedPaths.set(path, remaining) + else this.#leasedPaths.delete(path) + } + } + async readText(id: string, draftId: string | null, conversationId: string | null): Promise { const record = this.#requireForPrompt(id, conversationId, draftId) if (!isTextAttachmentMimeType(record.mimeType)) throw new AttachmentError('ATTACHMENT_UNSUPPORTED') if (record.sizeBytes > BUDDY_TEXT_ATTACHMENT_BYTES_LIMIT) throw new AttachmentError('ATTACHMENT_TOO_LARGE') - const bytes = await readBoundedFile(dirname(record.storedPath), record.storedPath, BUDDY_TEXT_ATTACHMENT_BYTES_LIMIT) + const bytes = await this.#readBytes(record, () => readBoundedFile(dirname(record.storedPath), record.storedPath, BUDDY_TEXT_ATTACHMENT_BYTES_LIMIT)) return bytes.toString('utf8') } @@ -483,12 +599,14 @@ export class AttachmentService { const record = this.#repository.findById(id) if (!record?.draftId) continue - if (this.#leasedPaths.has(normalize(record.storedPath)) || !this.#repository.removeDraft(id)) + if (this.#leasedPaths.has(normalize(record.storedPath)) || this.#repository.persistence?.hasProtection(record.storedPath) || !this.#repository.removeDraft(id)) continue released.push(id) this.#publish('released', 'release', [id]) try { - await unlinkAvailableFile(record.storedPath) + if (this.#repository.persistence) + await this.#collectFiles([record.storedPath]) + else await unlinkAvailableFile(record.storedPath) this.#publish('cleanup-completed', 'release', [id]) } catch (error) { @@ -521,36 +639,47 @@ export class AttachmentService { } async #reconcileStorage(): Promise { + this.#repository.persistence?.recover(this.#ownerInstance) const records = this.#repository.listAll() const ownedPaths = new Set(records.map(record => normalize(record.storedPath))) const invalidAttachmentIds: string[] = [] const missingAttachmentIds: string[] = [] for (const record of records) { + if (this.#repository.persistence && !this.#repository.persistence.get(record.storedPath)) + throw new Error('ATTACHMENT_CATALOG_INCOMPLETE') try { const metadata = await stat(record.storedPath) - if (!metadata.isFile() || metadata.size !== record.sizeBytes) + if (!metadata.isFile() || metadata.size !== record.sizeBytes) { invalidAttachmentIds.push(record.id) + this.#repository.persistence?.unavailable(record.storedPath) + } } catch (error) { if (isFileNotFound(error)) { missingAttachmentIds.push(record.id) + this.#repository.persistence?.unavailable(record.storedPath) continue } throw error } } - const roots = [this.#paths.draftsDirectory] - const conversationDirectories = await listDirectories(this.#paths.conversationsDirectory) - roots.push(...conversationDirectories.map(directory => join(directory, 'inputs'))) let removedOrphanFiles = 0 - for (const root of roots) { - for (const path of await listFilesRecursively(root)) { - if (ownedPaths.has(normalize(path)) || this.#leasedPaths.has(normalize(path))) - continue - await unlinkAvailableFile(path) - removedOrphanFiles += 1 + if (this.#repository.persistence) { + removedOrphanFiles = await this.#collectFiles(this.#repository.persistence.candidates().map(file => file.path)) + } + else { + const roots = [this.#paths.draftsDirectory] + const conversationDirectories = await listDirectories(this.#paths.conversationsDirectory) + roots.push(...conversationDirectories.map(directory => join(directory, 'inputs'))) + for (const root of roots) { + for (const path of await listFilesRecursively(root)) { + if (ownedPaths.has(normalize(path)) || this.#leasedPaths.has(normalize(path))) + continue + await unlinkAvailableFile(path) + removedOrphanFiles += 1 + } } } @@ -566,334 +695,92 @@ export class AttachmentService { return this.#repository.listForConversation(conversationId) } - async materializeConversationImages( - conversationId: string, - ids?: readonly string[], - ): Promise<{ images: ImageContent[], records: AttachmentRecord[] }> { - const available = this.listForConversation(conversationId) - .filter(record => record.mimeType.startsWith('image/')) - const selectedIds = ids === undefined - ? available.slice(-1).map(record => record.id) - : ids - if (selectedIds.length === 0) - throw new AttachmentError('ATTACHMENT_NOT_FOUND') - const materialized = await this.materializePrompt(selectedIds, '', conversationId) - if (materialized.images.length !== selectedIds.length) - throw new AttachmentError('VALIDATION_FAILED') - return { - images: materialized.images, - records: materialized.records, - } + materializeConversationImages(...input: Parameters) { + return this.#materializer.materializeConversationImages(...input) } - async materializePrompt( - ids: readonly string[], - content: string, - conversationId: string | null = null, - draftId: string | null = null, - composer?: { content: BuddyUserContentV1, resourceIds: readonly string[], resources?: readonly BuddyUserMessageResourceSnapshot[], resolveDirective?: (directive: BuddyPromptDirective) => string, sourceMessageId?: string, quoteReadAvailable?: boolean, inlineBytes?: number }, - ): Promise<{ documents: AttachmentFileInput[], images: ImageContent[], prompt: string, records: AttachmentRecord[], materialProjection?: InputMaterialProjectionState }> { - const prepared = await this.preparePrompt(ids, content, conversationId, draftId, composer) - return { - documents: await this.materializeDocumentInputs(prepared.documentReferences, conversationId, draftId), - images: await this.materializePiInputImages( - prepared.imageReferences, - conversationId, - draftId, - ), - prompt: prepared.prompt, - records: prepared.records, - } + materializePrompt(...input: Parameters) { + return this.#materializer.materializePrompt(...input) } - async preparePrompt( - ids: readonly string[], - content: string, - conversationId: string | null = null, - draftId: string | null = null, - composer?: { content: BuddyUserContentV1, resourceIds: readonly string[], resources?: readonly BuddyUserMessageResourceSnapshot[], resolveDirective?: (directive: BuddyPromptDirective) => string, sourceMessageId?: string, quoteReadAvailable?: boolean, inlineBytes?: number }, - ): Promise<{ documentReferences: AttachmentDocumentReference[], imageReferences: AttachmentImageReference[], prompt: string, records: AttachmentRecord[], materialProjection?: InputMaterialProjectionState }> { - const records = ids.map(id => this.#requireForPrompt(id, conversationId, draftId)) - validateTotalBytes(records.map(record => record.sizeBytes)) - const materialized = await Promise.all(records.map(async (record): Promise<{ - imageReference: AttachmentImageReference | null - documentReference?: AttachmentDocumentReference - section: string - text: string | null - }> => { - try { - if (isDocumentMimeType(record.mimeType)) { - return { - documentReference: toDocumentReference(record), - imageReference: null, - section: `附件:${record.name}(${record.mimeType},attachmentId=${record.id})`, - text: null, - } - } - if (record.mimeType.startsWith('image/') && record.mimeType !== 'image/svg+xml') { - return { - imageReference: toImageReference(record), - section: `附件:${record.name}(图像,attachmentId=${record.id})`, - text: null, - } - } - if (!isTextAttachmentMimeType(record.mimeType) || record.sizeBytes > BUDDY_TEXT_ATTACHMENT_BYTES_LIMIT) - throw new AttachmentError('VALIDATION_FAILED') - const text = await this.#readFile(record.storedPath, 'utf8') - return { - imageReference: null, - section: `附件:${record.name}(attachmentId=${record.id})\n\n${text}`, - text, - } - } - catch (error) { - if (error instanceof AttachmentError) - throw error - if (isFileNotFound(error)) - throw new AttachmentError('ATTACHMENT_NOT_FOUND', { cause: error }) - throw error - } - })) - const budget = new InputMaterialProjectionBudget(composer?.inlineBytes) - let projected - try { - projected = composer - ? projectBuddyUserContent(composer.content, (resourceId) => { - const resource = composer.resources?.find(item => item.resourceId === resourceId) - const localReference = resource?.localReference - if (localReference && !resource.attachmentId) - return { kind: 'local', name: localReference.name, localReference } - const index = resource?.attachmentId ? ids.indexOf(resource.attachmentId) : composer.resourceIds.indexOf(resourceId) - const record = records[index] - const value = materialized[index] - if (!record || !value) - throw new AttachmentError('ATTACHMENT_NOT_FOUND') - return value.imageReference - ? { kind: 'image', name: record.name, nameSource: record.nameSource, localReference } - : value.documentReference - ? { kind: getDocumentKind(value.documentReference.mimeType), name: record.name, localReference } - : { kind: 'text', name: record.name, text: value.text! } - }, (directive) => { - if (!composer.resolveDirective) - throw new AttachmentError('VALIDATION_FAILED') - return composer.resolveDirective(directive) - }, { sourceMessageId: composer.sourceMessageId, readAvailable: composer.quoteReadAvailable, budget }) - : null - if (!composer) { - for (const value of materialized) { - if (value.text !== null) - budget.project(value.text, false) - } - } - } - catch (error) { - if (error instanceof InputMaterialProjectionError) - throw new AttachmentError(error.code, { cause: error }) - throw error - } - return { - materialProjection: projected?.materialProjection, - documentReferences: materialized.flatMap(item => item.documentReference ? [item.documentReference] : []), - imageReferences: materialized.flatMap(item => item.imageReference ? [item.imageReference] : []), - prompt: [projected?.prompt ?? content.trim(), ...materialized.slice(composer?.resourceIds.length ?? 0).map(item => item.section)] - .filter(Boolean) - .join('\n\n---\n\n'), - records, - } + materializeInputResources(...input: Parameters) { + return this.#materializer.materializeInputResources(...input) } - getInputMetadata(ids: readonly string[], conversationId: string | null, draftId: string | null = null): AttachmentRecord[] { - return ids.map(id => this.#requireForPrompt(id, conversationId, draftId)) + resolveInputReferences(...input: Parameters) { + return this.#materializer.resolveInputReferences(...input) } - async materializeInputResources(input: BuddyInputReferenceV1, conversationId: string, workspace: AttachmentToolWorkspace, message?: Pick | null): Promise { - const ids = input.attachmentIds ?? [...input.images, ...input.documents ?? []].map(file => file.attachmentId) - const records = this.getInputMetadata(ids, conversationId) - const nativeIds = new Set([...input.images, ...input.documents ?? []].map(file => file.attachmentId)) - const labels = input.resourceLabels ?? getAttachmentLabels(records, input.prompt) - const resources = await Promise.all(records.map(async (record) => { - const bytes = await this.#readFile(record.storedPath) - if (bytes.length !== record.sizeBytes || (isDocumentMimeType(record.mimeType) && !hasDocumentSignature(record.mimeType, bytes))) - throw new AttachmentError('VALIDATION_FAILED') - const path = await workspace.materialize(record, bytes) - const savedResource = message?.resourceSnapshots.find(resource => resource.attachmentId === record.id) - const deferred = savedResource && message?.materialProjection?.deferredResourceIds.includes(savedResource.resourceId) - return { - ...(deferred ? { snapshotRead: { sourceMessageId: input.messageId, referenceId: savedResource.resourceId, kind: 'attachment' } } : {}), - attachmentId: record.id, - label: labels[record.id], - name: record.name, - mimeType: record.mimeType, - path, - sourcePath: record.sourcePath, - content: nativeIds.has(record.id) ? 'native' : isTextAttachmentMimeType(record.mimeType) && !deferred ? 'text' : 'file_only', - } - })) - return resources.length ? `\n${JSON.stringify(resources)}\n` : '' + materializePiInputImages(...input: Parameters) { + return this.#materializer.materializePiInputImages(...input) } - async resolveInputReferences( - ids: readonly string[], - conversationId: string, - prompt = '', - ): Promise<{ images: AttachmentImageReference[], documents: AttachmentDocumentReference[], resourceLabels?: Record }> { - const records = ids.map(id => this.#requireForPrompt(id, conversationId, null)) - validateTotalBytes(records.map(record => record.sizeBytes)) - return { - resourceLabels: getAttachmentLabels(records, prompt), - images: records.flatMap(record => ( - record.mimeType.startsWith('image/') && record.mimeType !== 'image/svg+xml' - ? [toImageReference(record)] - : [] - )), - documents: records.filter(record => isDocumentMimeType(record.mimeType)).map(toDocumentReference), - } + prepareInputImages(...input: Parameters) { + return this.#materializer.prepareInputImages(...input) } - async materializePiInputImages( - references: readonly AttachmentImageReference[], - conversationId: string | null, - draftId: string | null = null, - ): Promise { - const records = references.map((reference) => { - const record = this.#requireForPrompt(reference.attachmentId, conversationId, draftId) - if ( - record.mimeType !== reference.mimeType - || !record.mimeType.startsWith('image/') - || record.mimeType === 'image/svg+xml' - ) { - throw new AttachmentError('VALIDATION_FAILED') - } - return record - }) - validateTotalBytes(records.map(record => record.sizeBytes)) - return Promise.all(records.map(async (record) => { - try { - return { - data: (await this.#readFile(record.storedPath)).toString('base64'), - mimeType: record.mimeType, - type: 'image' as const, - } - } - catch (error) { - if (isFileNotFound(error)) - throw new AttachmentError('ATTACHMENT_NOT_FOUND', { cause: error }) - throw error - } - })) + materializeDocumentInputs(...input: Parameters) { + return this.#materializer.materializeDocumentInputs(...input) } - async prepareInputImages( - references: readonly AttachmentImageReference[], - conversationId: string, - model: InputModel, - ): Promise> { - const images = new Map() - for (const reference of references) { - const record = this.#requireForPrompt(reference.attachmentId, conversationId, null) - if (record.mimeType !== reference.mimeType) - throw new AttachmentError('VALIDATION_FAILED') - const metadata = await stat(record.storedPath) - if (!metadata.isFile() || metadata.size !== record.sizeBytes || metadata.size > BUDDY_ATTACHMENT_TOTAL_BYTES_LIMIT) - throw new AttachmentError('ATTACHMENT_INVALID') - const bytes = await this.#readFile(record.storedPath) - const directory = join(this.#paths.conversationDirectory(conversationId), 'image-inputs') - images.set(record.id, await this.#images.prepare(record, bytes, directory, model)) - } - return images + resolveRecoveryInputReferences(...input: Parameters) { + return this.#materializer.resolveRecoveryInputReferences(...input) } - async materializeDocumentInputs( - references: readonly AttachmentDocumentReference[], - conversationId: string | null, - draftId: string | null = null, - ): Promise { - const records = references.map((reference) => { - const record = this.#requireForPrompt(reference.attachmentId, conversationId, draftId) - if (!isDocumentMimeType(record.mimeType) || reference.mimeType !== record.mimeType) - throw new AttachmentError('VALIDATION_FAILED') - return record - }) - validateTotalBytes(records.map(record => record.sizeBytes)) - return Promise.all(records.map(async (record) => { - try { - const bytes = await this.#readFile(record.storedPath) - if (bytes.length !== record.sizeBytes || !hasDocumentSignature(record.mimeType, bytes)) - throw new AttachmentError('VALIDATION_FAILED') - return { data: bytes.toString('base64'), name: record.name, mimeType: toDocumentReference(record).mimeType } - } - catch (error) { - if (isFileNotFound(error)) - throw new AttachmentError('ATTACHMENT_NOT_FOUND', { cause: error }) - throw error - } - })) + preparePrompt(...input: Parameters) { + return this.#projector.preparePrompt(...input) } - async resolveRecoveryInputReferences( - ids: readonly string[], - conversationId: string, - prompt = '', - ): Promise { - const resolved = ids.map((id) => { - try { - return { id, record: this.#requireForPrompt(id, conversationId, null) } - } - catch (error) { - if (error instanceof AttachmentError && error.code === 'ATTACHMENT_NOT_FOUND') - return { id, record: null } - throw error - } - }) - validateTotalBytes(resolved.flatMap(item => item.record ? [item.record.sizeBytes] : [])) - const recovered = await Promise.all(resolved.map(async ({ id, record }): Promise<{ - images: AttachmentImageReference[] - documents?: AttachmentDocumentReference[] - missingAttachmentId: string | null - }> => { - if (!record) - return { images: [], missingAttachmentId: id } - try { - const metadata = await stat(record.storedPath) - if (!metadata.isFile() || metadata.size !== record.sizeBytes) - return { images: [], missingAttachmentId: id } - if (!isDocumentMimeType(record.mimeType) && (!record.mimeType.startsWith('image/') || record.mimeType === 'image/svg+xml')) - return { images: [], missingAttachmentId: null } - return isDocumentMimeType(record.mimeType) - ? { documents: [toDocumentReference(record)], images: [], missingAttachmentId: null } - : { images: [toImageReference(record)], missingAttachmentId: null } - } - catch (error) { - if (isFileNotFound(error)) - return { images: [], missingAttachmentId: id } - throw error - } - })) - return { - resourceLabels: resolved.every(item => item.record) ? getAttachmentLabels(resolved.map(item => item.record!), prompt) : {}, - documents: recovered.flatMap(item => item.documents ?? []), - images: recovered.flatMap(item => item.images), - missingAttachmentIds: recovered.flatMap( - item => item.missingAttachmentId ? [item.missingAttachmentId] : [], - ), - } + getInputMetadata(ids: readonly string[], conversationId: string | null, draftId: string | null = null): AttachmentRecord[] { + return ids.map(id => this.#requireForPrompt(id, conversationId, draftId)) } #requireAvailable(id: string): AttachmentRecord { const record = this.#repository.findById(id) if (!record) throw new AttachmentError('ATTACHMENT_NOT_FOUND') + this.#assertFileReady(record) return record } + #assertFileReady(record: AttachmentRecord): void { + if (this.#repository.persistence && this.#repository.persistence.get(record.storedPath)?.state !== 'ready') + throw new AttachmentError('ATTACHMENT_NOT_FOUND') + } + + async #collectFiles(paths: readonly string[]): Promise { + const repository = this.#repository.persistence! + let removed = 0 + for (const path of new Set(paths)) { + const location = relative(this.#paths.root, path) + if (!location || location === '..' || location.startsWith(`..${sep}`) || isAbsolute(location)) + throw new Error('FILE_UNMANAGED_PATH') + if (this.#leasedPaths.has(normalize(path))) + continue + const claimed = repository.claim(path) + if (!claimed) + continue + if (claimed.temporaryPath) { + await unlinkAvailableFile(claimed.temporaryPath) + await syncAvailableDirectory(dirname(claimed.temporaryPath)) + } + await unlinkAvailableFile(path) + await syncAvailableDirectory(dirname(path)) + repository.deleted(claimed.id) + removed++ + } + return removed + } + #requireForPrompt( id: string, conversationId: string | null, draftId: string | null, + legacyResourceDraftId?: string, ): AttachmentRecord { const record = this.#requireAvailable(id) if (record.draftId !== null) { - if (record.draftId === draftId) + if (record.draftId === draftId || record.draftId === legacyResourceDraftId) return record throw new AttachmentError('VALIDATION_FAILED') } @@ -910,7 +797,15 @@ export class AttachmentService { this.#publish('released', 'rollback', [record.id], operationId) if (this.#repository.findById(record.id)) continue - await unlinkAvailableFile(record.storedPath) + if (this.#repository.persistence) { + if (this.#repository.persistence.get(record.storedPath)) { + this.#repository.persistence.settle([record.storedPath], 'rollback') + await this.#collectFiles([record.storedPath]) + } + } + else { + await unlinkAvailableFile(record.storedPath) + } this.#publish('cleanup-completed', 'rollback', [record.id], operationId) } catch (error) { @@ -923,73 +818,11 @@ export class AttachmentService { } } -function toImageReference(record: AttachmentRecord): AttachmentImageReference { - return { attachmentId: record.id, mimeType: record.mimeType } -} - -function toDocumentReference(record: AttachmentRecord): AttachmentDocumentReference { - if (!isDocumentMimeType(record.mimeType)) - throw new AttachmentError('VALIDATION_FAILED') - return { attachmentId: record.id, mimeType: record.mimeType } -} - -function getDocumentKind(mimeType: string): 'pdf' | 'audio' | 'video' { - const kind = getAttachmentKind(mimeType) - if (kind !== 'pdf' && kind !== 'audio' && kind !== 'video') - throw new AttachmentError('VALIDATION_FAILED') - return kind -} - -function validateTotalBytes( - sizes: readonly number[], - limit = BUDDY_ATTACHMENT_TOTAL_BYTES_LIMIT, - errorCode = 'VALIDATION_FAILED', -): void { - if (sizes.reduce((total, size) => total + size, 0) > limit) - throw new AttachmentError(errorCode) -} - -export class AttachmentError extends Error { - readonly code: string - - constructor(code: string, options?: ErrorOptions) { - super('Lexora Buddy attachment operation failed', options) - this.name = 'AttachmentError' - this.code = code - } -} - function safeExtension(path: string): string { const extension = extname(path).toLowerCase() return /^\.[a-z0-9]{1,16}$/.test(extension) ? extension : '' } -export function normalizeAttachmentMetadata(input: { mimeType: string, name: string, sizeBytes: number }) { - const name = input.name.trim() - const mimeType = extname(name).toLowerCase() === '.pdf' || Object.hasOwn(BUDDY_MEDIA_EXTENSIONS, extname(name).toLowerCase()) || textExtensions.has(extname(name).slice(1).toLowerCase()) - ? inferMimeType(name) - : input.mimeType.trim() || inferMimeType(name) - if (!name || /[/\\\0]/.test(name) || !Number.isSafeInteger(input.sizeBytes) || input.sizeBytes < 0) - throw new AttachmentError('VALIDATION_FAILED') - if (!isDocumentMimeType(mimeType) && !SUPPORTED_IMAGE_MIME_TYPES.has(mimeType) && !isTextAttachmentMimeType(mimeType)) - throw new AttachmentError('ATTACHMENT_UNSUPPORTED') - const byteLimit = isTextAttachmentMimeType(mimeType) - ? BUDDY_TEXT_ATTACHMENT_BYTES_LIMIT - : isDocumentMimeType(mimeType) && mimeType !== 'application/pdf' - ? BUDDY_MEDIA_FILE_BYTES_LIMIT - : BUDDY_ATTACHMENT_TOTAL_BYTES_LIMIT - if (input.sizeBytes > byteLimit) - throw new AttachmentError('ATTACHMENT_TOO_LARGE') - if (isDocumentMimeType(mimeType) && input.sizeBytes === 0) - throw new AttachmentError('ATTACHMENT_INVALID') - return { mimeType, name, sizeBytes: input.sizeBytes } -} - -export function inferMimeType(path: string): string { - const extension = extname(path).toLowerCase() - return MIME_TYPES[extension] ?? (textExtensions.has(extension.slice(1)) ? 'text/plain' : 'application/octet-stream') -} - function isFileNotFound(error: unknown): boolean { return (error as NodeJS.ErrnoException | undefined)?.code === 'ENOENT' } @@ -1008,23 +841,31 @@ async function removeFiles(paths: readonly string[]): Promise { await Promise.all(paths.map(unlinkAvailableFile)) } -async function publishBytes(bytes: Uint8Array, storedPath: string): Promise { - const temporaryPath = `${storedPath}.part` +async function publishBytes(bytes: Uint8Array, storedPath: string, temporaryPath = `${storedPath}.part`) { + await mkdir(dirname(temporaryPath), { mode: 0o700, recursive: true }) try { await writeFile(temporaryPath, bytes, { flag: 'wx', flush: true, mode: 0o600 }) - await rename(temporaryPath, storedPath) - await syncDirectory(dirname(storedPath)) + await fileStorage.replace(temporaryPath, storedPath) + await fileStorage.syncDirectory(dirname(storedPath)) + if (dirname(temporaryPath) !== dirname(storedPath)) + await fileStorage.syncDirectory(dirname(temporaryPath)) + return { sizeBytes: bytes.byteLength, sha256: createHash('sha256').update(bytes).digest('hex'), durability: fileStorage.durability } } finally { await unlinkAvailableFile(temporaryPath) } } -async function publishFile(sourcePath: string, storedPath: string): Promise { - const temporaryPath = `${storedPath}.part` +async function publishFile(sourcePath: string, storedPath: string, expectedSize: number, expectedHash?: string, temporaryPath = `${storedPath}.part`) { + await mkdir(dirname(temporaryPath), { mode: 0o700, recursive: true }) try { await copyFile(sourcePath, temporaryPath, constants.COPYFILE_EXCL) await chmod(temporaryPath, 0o600) + const sha256 = await hashFile(temporaryPath, expectedSize) + if (expectedHash && sha256 !== expectedHash) + throw new AttachmentError('ATTACHMENT_INVALID') + if (await hashFile(sourcePath, expectedSize) !== sha256) + throw new AttachmentError('ATTACHMENT_INVALID') const file = await open(temporaryPath, 'r+') try { await file.sync() @@ -1032,8 +873,11 @@ async function publishFile(sourcePath: string, storedPath: string): Promise { } return files } + +async function hashFile(path: string, expectedSize: number): Promise { + let size = 0 + const hash = createHash('sha256') + for await (const chunk of createReadStream(path)) { + size += chunk.length + if (size > expectedSize) + throw new AttachmentError('ATTACHMENT_INVALID') + hash.update(chunk) + } + if (size !== expectedSize) + throw new AttachmentError('ATTACHMENT_INVALID') + return hash.digest('hex') +} + +async function availableStorageBytes(path: string): Promise { + const storage = await statfs(path, { bigint: true }) + const available = storage.bavail * storage.bsize + return Number(available > BigInt(Number.MAX_SAFE_INTEGER) ? BigInt(Number.MAX_SAFE_INTEGER) : available) +} + +async function syncAvailableDirectory(path: string): Promise { + try { + await fileStorage.syncDirectory(path) + } + catch (error) { + if (!isFileNotFound(error)) + throw error + } +} diff --git a/apps/buddy/service/src/attachments/ComposerResourceService.ts b/apps/buddy/service/src/attachments/ComposerResourceService.ts index f3142471e..640b110ea 100644 --- a/apps/buddy/service/src/attachments/ComposerResourceService.ts +++ b/apps/buddy/service/src/attachments/ComposerResourceService.ts @@ -38,12 +38,14 @@ import { getBuddyUserContentResourceIds, readBuddyUserMessageContent } from '../ import { buddyComposerResourceAcceptSchema, buddyComposerSourceListSchema, buddyComposerSourceSelectSchema, buddyComposerSpaceFileSelectSchema } from '../../../shared/conversation/composerResource' import { Emitter } from '../../../shared/events/Emitter' import { copyEventSnapshot } from '../../../shared/events/eventSnapshot' +import { AsyncOperationScope } from '../../../shared/lifecycle/AsyncOperationScope' import { resolveArtifactFile } from '../artifacts/ArtifactFileAccess' import { resolveGrantedPath } from '../directories/resolveGrantedPath' import { createSensitivePathMatcher } from '../permissions/sensitivePaths' import { BuddyServiceError } from '../rpc/runtimeRequest' import { requireActiveSpace } from '../spaces/requireActiveSpace' import { ComposerResourceConflictError } from '../storage/composerResourceRepository' +import { readTransactionFailure } from '../storage/transaction' import { AttachmentError, DRAFT_ATTACHMENT_RETENTION_MS, normalizeAttachmentMetadata } from './AttachmentService' import { ComposerDirectorySources } from './ComposerDirectorySources' import { historicalComposerSources } from './historicalComposerSources' @@ -73,14 +75,14 @@ export interface PreparedComposerInput { validate: () => void commit: () => Promise rollback: () => Promise + retain: () => Promise } export class ComposerResourceService { readonly #changes = new Emitter(() => console.error('COMPOSER_RESOURCE_OBSERVER_FAILED')) readonly onDidChange = this.#changes.event - readonly #pending = new Set>() + readonly #operations = new AsyncOperationScope(() => new AttachmentError('RUNTIME_UNAVAILABLE')) #revision = 0 - #stopping = false #disposed = false readonly #attachments: ComposerResourceServiceOptions['attachments'] readonly #artifacts: ComposerResourceServiceOptions['artifacts'] @@ -105,7 +107,7 @@ export class ComposerResourceService { } accept(input: BuddyComposerResourceAccept): Promise { - return this.#operate(() => this.#accept(input)) + return this.#operations.track(() => this.#accept(input)) } async #accept(input: BuddyComposerResourceAccept): Promise { @@ -190,7 +192,7 @@ export class ComposerResourceService { selectSource(input: BuddyComposerSourceSelect, referencedResourceIds: readonly string[] = [], reason: ComposerResourceChangeReason = 'selection'): Promise { const ownedIds = [...referencedResourceIds] - return this.#operate(() => this.#selectSource(input, ownedIds, reason)) + return this.#operations.track(() => this.#selectSource(input, ownedIds, reason)) } async #selectSource(input: BuddyComposerSourceSelect, referencedResourceIds: readonly string[], reason: ComposerResourceChangeReason): Promise { @@ -204,7 +206,7 @@ export class ComposerResourceService { } async selectSpaceFilePath(draftId: string, spaceId: string, path: string, reason: ComposerResourceChangeReason = 'selection'): Promise { - return this.#operate(async () => { + return this.#operations.track(async () => { const space = requireActiveSpace(this.#spaces?.findById(spaceId) ?? null) const bindings = [space.primaryDirectory, ...space.additionalDirectories].filter(binding => binding !== null) const requestedPath = isAbsolute(path) ? path : space.primaryDirectory ? join(space.primaryDirectory.canonicalRoot, path) : null @@ -216,16 +218,17 @@ export class ComposerResourceService { }) } - async resolveInput(draftId: string, content: BuddyUserContentV1, scope: ComposerSourceScope = { branchId: null, conversationId: null, spaceId: null }, model?: Pick): Promise { + async resolveInput(draftId: string, content: BuddyUserContentV1, scope: ComposerSourceScope = { branchId: null, conversationId: null, spaceId: null }, model?: Pick, legacyResourceDraftId?: string): Promise { content = copyEventSnapshot(content) scope = copyEventSnapshot(scope) model = model ? { ...model, input: [...model.input], fileInputMimeTypes: [...model.fileInputMimeTypes] } : undefined - return this.#operate(async () => { + return this.#operations.track(async () => { const resourceIds = getBuddyUserContentResourceIds(content) if (resourceIds.length > BUDDY_ATTACHMENT_COUNT_LIMIT) throw new AttachmentError('VALIDATION_FAILED') const resources = resourceIds.map((resourceId) => { - const resource = this.#requireOwned({ draftId, resourceId }) + const candidate = this.#repository.findById(resourceId) + const resource = this.#requireOwned({ draftId: candidate?.draftId === legacyResourceDraftId ? legacyResourceDraftId! : draftId, resourceId }) if (resource.state !== 'ready') throw new AttachmentError('VALIDATION_FAILED') if (resource.source && 'spaceId' in resource.source && resource.source.spaceId !== scope.spaceId) @@ -235,7 +238,7 @@ export class ComposerResourceService { return resource }) const uploads: PreparedAttachmentUploads[] = [] - let settlement: { outcome: 'commit' | 'rollback', promise: Promise } | undefined + let settlement: { outcome: 'commit' | 'rollback' | 'retain', promise: Promise } | undefined const validate = () => { if (settlement) throw new Error('COMPOSER_INPUT_SETTLED') @@ -245,7 +248,7 @@ export class ComposerResourceService { throw new AttachmentError('ATTACHMENT_NOT_FOUND') } } - const settle = (outcome: 'commit' | 'rollback') => { + const settle = (outcome: 'commit' | 'rollback' | 'retain') => { if (settlement) { return settlement.outcome === outcome ? settlement.promise @@ -302,7 +305,7 @@ export class ComposerResourceService { throw new AttachmentError('VALIDATION_FAILED') const snapshots = new Map(result.map(snapshot => [snapshot.resourceId, snapshot])) validate() - return Object.freeze({ inputs: copyEventSnapshot(resourceIds.map(resourceId => snapshots.get(resourceId)!)), validate, commit: () => settle('commit'), rollback: () => settle('rollback') }) + return Object.freeze({ inputs: copyEventSnapshot(resourceIds.map(resourceId => snapshots.get(resourceId)!)), validate, commit: () => settle('commit'), rollback: () => settle('rollback'), retain: () => settle('retain') }) } catch (error) { try { @@ -320,7 +323,7 @@ export class ComposerResourceService { if (!input.conversationId) return { branchId: null, conversationId: null, spaceId: input.spaceId } const conversation = this.#conversations?.findById(input.conversationId) - if (!conversation || conversation.deletedAt !== null || conversation.activeBranchId !== input.branchId) + if (!conversation || conversation.deletedAt !== null) throw new BuddyServiceError('VALIDATION_FAILED') if (conversation.spaceId !== input.spaceId) throw new BuddyServiceError('VALIDATION_FAILED') @@ -509,7 +512,7 @@ export class ComposerResourceService { #requireVisibleHistory(conversationId: string, branchId: string) { const conversation = this.#conversations?.findById(conversationId) - if (!conversation || conversation.deletedAt !== null || conversation.activeBranchId !== branchId) + if (!conversation || conversation.deletedAt !== null) throw new BuddyServiceError('VALIDATION_FAILED') return this.#conversations!.listBranchMessages(conversationId, branchId) } @@ -570,7 +573,7 @@ export class ComposerResourceService { async complete(input: BuddyComposerResourceComplete): Promise { input = { ...input, bytes: Uint8Array.from(input.bytes) } - return this.#operate(async () => { + return this.#operations.track(async () => { const resource = this.#requireOwned(input) if (resource.source) throw new AttachmentError('VALIDATION_FAILED') @@ -611,13 +614,17 @@ export class ComposerResourceService { return ready } catch (error) { + if (readTransactionFailure(error)?.outcome === 'unknown') { + await upload?.retain() + throw error + } if (upload) { let current: ComposerResourceRecord | null try { current = this.#repository.findById(input.resourceId) } catch (readError) { - await upload.commit() + await upload.retain() throw new AggregateError([error, readError], 'COMPOSER_IMPORT_OWNERSHIP_UNKNOWN') } if (current?.attachmentId === upload.records[0]?.id) { @@ -638,7 +645,7 @@ export class ComposerResourceService { } fail(input: BuddyComposerResourceTarget): BuddyComposerResource { - this.#assertAccepting() + this.#operations.assertAccepting() return this.#fail(input) } @@ -649,7 +656,7 @@ export class ComposerResourceService { } retry(input: BuddyComposerResourceTarget): BuddyComposerResource { - this.#assertAccepting() + this.#operations.assertAccepting() this.#requireOwned(input) if (this.#importing.has(input.resourceId)) throw new AttachmentError('VALIDATION_FAILED') @@ -662,7 +669,7 @@ export class ComposerResourceService { paths: readonly string[] referencedResourceIds?: readonly string[] }): Promise { - return this.#operate(async () => { + return this.#operations.track(async () => { const { draftId, paths, referencedResourceIds = [] } = input if (!paths.length) return [] @@ -685,7 +692,7 @@ export class ComposerResourceService { } recoverInterruptedImports(unavailableAttachmentIds: readonly string[] = []): void { - this.#assertAccepting() + this.#operations.assertAccepting() this.#commit(null, 'recovery', () => { this.#repository.interruptImports(new Date().toISOString()) this.#repository.failUnavailableAttachments(unavailableAttachmentIds, new Date().toISOString()) @@ -693,7 +700,7 @@ export class ComposerResourceService { } async cleanupDrafts(now = Date.now()): Promise { - return this.#operate(async () => { + return this.#operations.track(async () => { const cutoff = new Date(now - DRAFT_ATTACHMENT_RETENTION_MS).toISOString() const staleResources = this.#repository.listAll().filter(resource => resource.createdAt < cutoff) const retainedAttachmentIds = new Set() @@ -721,16 +728,15 @@ export class ComposerResourceService { } async discard(draftId: string): Promise { - return this.#operate(async () => { + return this.#operations.track(async () => { this.#commit(draftId, 'discard', () => this.#repository.remove(draftId, this.#repository.listForDraft(draftId).map(resource => resource.resourceId))) await this.#attachments.releaseDraft(draftId) }) } async dispose(): Promise { - this.#stopping = true - while (this.#pending.size) - await Promise.allSettled([...this.#pending]) + this.#operations.stopAccepting() + await this.#operations.drain() this.#disposed = true this.#changes.dispose() } @@ -764,24 +770,6 @@ export class ComposerResourceService { } } - #assertAccepting(): void { - if (this.#stopping) - throw new AttachmentError('RUNTIME_UNAVAILABLE') - } - - #operate(work: () => Promise): Promise { - if (this.#stopping) - return Promise.reject(new AttachmentError('RUNTIME_UNAVAILABLE')) - const pending = Promise.withResolvers() - this.#pending.add(pending.promise) - try { - void Promise.resolve(work()).then(pending.resolve, pending.reject) - } - catch (error) { pending.reject(error) } - void pending.promise.then(() => this.#pending.delete(pending.promise), () => this.#pending.delete(pending.promise)) - return pending.promise - } - #requireOwned(input: BuddyComposerResourceTarget): ComposerResourceRecord { const resource = this.#repository.findById(input.resourceId) if (!resource || resource.draftId !== input.draftId) diff --git a/apps/buddy/service/src/attachments/InputMaterialProjector.ts b/apps/buddy/service/src/attachments/InputMaterialProjector.ts new file mode 100644 index 000000000..1282fb259 --- /dev/null +++ b/apps/buddy/service/src/attachments/InputMaterialProjector.ts @@ -0,0 +1,121 @@ +import type { BuddyPromptDirective, BuddyUserContentV1, BuddyUserMessageResourceSnapshot } from '../../../shared/conversation/buddyUserContent' +import type { InputMaterialProjectionState } from '../../../shared/conversation/inputMaterialProjection' +import type { AttachmentRecord } from '../storage/attachmentRepository' +import type { AttachmentDocumentReference } from './AttachmentDocumentReference' +import type { AttachmentImageReference } from './AttachmentImageReference' +import type { AttachmentInputReader } from './AttachmentInputReader' +import { isDocumentMimeType } from '../../../shared/conversation/attachmentFormats' +import { BUDDY_TEXT_ATTACHMENT_BYTES_LIMIT, isTextAttachmentMimeType } from '../../../shared/conversation/attachmentPolicy' +import { projectBuddyUserContent } from '../../../shared/conversation/buddyUserContentProjection' +import { InputMaterialProjectionBudget, InputMaterialProjectionError } from '../../../shared/conversation/inputMaterialProjection' +import { AttachmentError } from './AttachmentError' +import { getDocumentKind, toDocumentReference, toImageReference, validateTotalBytes } from './attachmentMetadata' + +export class InputMaterialProjector { + readonly #reader: AttachmentInputReader + + constructor(reader: AttachmentInputReader) { + this.#reader = reader + } + + async preparePrompt( + ids: readonly string[], + content: string, + conversationId: string | null = null, + draftId: string | null = null, + composer?: { content: BuddyUserContentV1, resourceIds: readonly string[], resources?: readonly BuddyUserMessageResourceSnapshot[], resolveDirective?: (directive: BuddyPromptDirective) => string, sourceMessageId?: string, quoteReadAvailable?: boolean, inlineBytes?: number }, + legacyResourceDraftId?: string, + ): Promise<{ documentReferences: AttachmentDocumentReference[], imageReferences: AttachmentImageReference[], prompt: string, records: AttachmentRecord[], materialProjection?: InputMaterialProjectionState }> { + const records = ids.map(id => this.#reader.get(id, { conversationId, draftId, legacyResourceDraftId })) + validateTotalBytes(records.map(record => record.sizeBytes)) + const materialized = await Promise.all(records.map(async (record): Promise<{ + imageReference: AttachmentImageReference | null + documentReference?: AttachmentDocumentReference + section: string + text: string | null + }> => { + try { + if (isDocumentMimeType(record.mimeType)) { + return { + documentReference: toDocumentReference(record), + imageReference: null, + section: `附件:${record.name}(${record.mimeType},attachmentId=${record.id})`, + text: null, + } + } + if (record.mimeType.startsWith('image/') && record.mimeType !== 'image/svg+xml') { + return { + imageReference: toImageReference(record), + section: `附件:${record.name}(图像,attachmentId=${record.id})`, + text: null, + } + } + if (!isTextAttachmentMimeType(record.mimeType) || record.sizeBytes > BUDDY_TEXT_ATTACHMENT_BYTES_LIMIT) + throw new AttachmentError('VALIDATION_FAILED') + const text = (await this.#reader.read(record)).toString('utf8') + return { + imageReference: null, + section: `附件:${record.name}(attachmentId=${record.id})\n\n${text}`, + text, + } + } + catch (error) { + if (error instanceof AttachmentError) + throw error + if (isFileNotFound(error)) + throw new AttachmentError('ATTACHMENT_NOT_FOUND', { cause: error }) + throw error + } + })) + const budget = new InputMaterialProjectionBudget(composer?.inlineBytes) + let projected + try { + projected = composer + ? projectBuddyUserContent(composer.content, (resourceId) => { + const resource = composer.resources?.find(item => item.resourceId === resourceId) + const localReference = resource?.localReference + if (localReference && !resource.attachmentId) + return { kind: 'local', name: localReference.name, localReference } + const index = resource?.attachmentId ? ids.indexOf(resource.attachmentId) : composer.resourceIds.indexOf(resourceId) + const record = records[index] + const value = materialized[index] + if (!record || !value) + throw new AttachmentError('ATTACHMENT_NOT_FOUND') + return value.imageReference + ? { kind: 'image', name: record.name, nameSource: record.nameSource, localReference } + : value.documentReference + ? { kind: getDocumentKind(value.documentReference.mimeType), name: record.name, localReference } + : { kind: 'text', name: record.name, text: value.text! } + }, (directive) => { + if (!composer.resolveDirective) + throw new AttachmentError('VALIDATION_FAILED') + return composer.resolveDirective(directive) + }, { sourceMessageId: composer.sourceMessageId, readAvailable: composer.quoteReadAvailable, budget }) + : null + if (!composer) { + for (const value of materialized) { + if (value.text !== null) + budget.project(value.text, false) + } + } + } + catch (error) { + if (error instanceof InputMaterialProjectionError) + throw new AttachmentError(error.code, { cause: error }) + throw error + } + return { + materialProjection: projected?.materialProjection, + documentReferences: materialized.flatMap(item => item.documentReference ? [item.documentReference] : []), + imageReferences: materialized.flatMap(item => item.imageReference ? [item.imageReference] : []), + prompt: [projected?.prompt ?? content.trim(), ...materialized.slice(composer?.resourceIds.length ?? 0).map(item => item.section)] + .filter(Boolean) + .join('\n\n---\n\n'), + records, + } + } +} + +function isFileNotFound(error: unknown): boolean { + return (error as NodeJS.ErrnoException | undefined)?.code === 'ENOENT' +} diff --git a/apps/buddy/service/src/attachments/__tests__/AttachmentPersistence.spec.ts b/apps/buddy/service/src/attachments/__tests__/AttachmentPersistence.spec.ts new file mode 100644 index 000000000..a355ca253 --- /dev/null +++ b/apps/buddy/service/src/attachments/__tests__/AttachmentPersistence.spec.ts @@ -0,0 +1,215 @@ +import type { DatabaseSync } from 'node:sqlite' +import { Buffer } from 'node:buffer' +import { mkdir, mkdtemp, readFile, rm, unlink, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { dirname, join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { createAttachmentRepository } from '../../storage/attachmentRepository' +import { BuddyDataPaths } from '../../storage/BuddyDataPaths' +import { createConversationRepository } from '../../storage/conversationRepository' +import { openBuddyMigrationCandidate } from '../../storage/database' +import { withTransaction } from '../../storage/transaction' +import { AttachmentService } from '../AttachmentService' + +const cleanups: (() => Promise)[] = [] +afterEach(async () => { + for (const cleanup of cleanups.splice(0).reverse()) await cleanup() +}) + +async function setup() { + const root = await mkdtemp(join(tmpdir(), 'lexora-persistent-attachments-')) + const databasePath = join(root, 'data.sqlite3') + let database = openBuddyMigrationCandidate({ databasePath }) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + const paths = new BuddyDataPaths(join(root, 'profile')) + let repository = createAttachmentRepository(database) + let service = new AttachmentService({ paths, repository }) + cleanups.push(async () => { + await service.dispose() + database.close() + await rm(root, { recursive: true, force: true }) + }) + return { + root, + paths, + get database() { return database }, + get repository() { return repository }, + get service() { return service }, + async reopen() { + await service.dispose() + database.close() + database = openBuddyMigrationCandidate({ databasePath }) + repository = createAttachmentRepository(database) + service = new AttachmentService({ paths, repository }) + }, + async upload() { + const [record] = await service.registerUploads('draft-1', [{ name: 'source.txt', mimeType: 'text/plain', bytes: Buffer.from('source bytes') }]) + return record! + }, + } +} + +function message(database: DatabaseSync) { + createConversationRepository(database).create({ id: 'conversation-1', branchId: 'branch-1', createdAt: '2026-10-01T00:00:00.000Z', spaceId: null, title: null, approvalPolicy: 'manual', executionProfile: 'read_only' }) + database.prepare(`INSERT INTO messages (id, conversation_id, branch_id, role, content_json, created_at) + VALUES ('message-1', 'conversation-1', 'branch-1', 'user', '{}', '2026-10-01T00:00:00.000Z')`).run() +} + +async function prepare(fixture: Awaited>) { + const record = await fixture.upload() + const prepared = await fixture.service.prepareMessageAttachments({ attachmentIds: [record.id], conversationId: 'conversation-1', draftId: 'draft-1', messageId: 'message-1' }) + return { record, prepared, target: prepared.bindings[0]!.storedPath } +} + +function bind(database: DatabaseSync, path: string, id: string) { + return database.prepare('UPDATE attachments SET draft_id = NULL, message_id = \'message-1\', stored_path = ? WHERE id = ?').run(path, id) +} + +describe('persistent managed attachment lifecycle', () => { + it('protects source and destination through preparation and atomically changes ownership', async () => { + const fixture = await setup() + const { record, prepared, target } = await prepare(fixture) + const persistence = fixture.repository.persistence! + message(fixture.database) + expect(await fixture.service.release([record.id])).toEqual([]) + expect(persistence.claim(record.storedPath)).toBeNull() + expect(persistence.claim(target)).toBeNull() + expect(() => withTransaction(fixture.database, () => { + bind(fixture.database, target, record.id) + throw new Error('abort command') + })).toThrow('abort command') + expect(fixture.repository.findById(record.id)?.storedPath).toBe(record.storedPath) + expect(fixture.database.prepare('SELECT kind FROM file_references').all()).toEqual([{ kind: 'draft' }]) + withTransaction(fixture.database, () => bind(fixture.database, target, record.id)) + await prepared.commit() + expect(persistence.get(record.storedPath)?.state).toBe('deleted') + expect(fixture.database.prepare('SELECT kind, owner_id FROM file_references').all()).toEqual([{ kind: 'message', owner_id: 'message-1' }]) + expect((await readFile(target)).toString()).toBe('source bytes') + await expect(readFile(record.storedPath)).rejects.toMatchObject({ code: 'ENOENT' }) + }) + + it('rejects a late binding after the source owner or actor changes', async () => { + const fixture = await setup() + const { record, prepared, target } = await prepare(fixture) + message(fixture.database) + fixture.database.prepare('UPDATE attachments SET draft_id = \'other\' WHERE id = ?').run(record.id) + expect(() => withTransaction(fixture.database, () => bind(fixture.database, target, record.id))).toThrow('FILE_SOURCE_CHANGED') + fixture.database.prepare('UPDATE attachments SET draft_id = \'draft-1\' WHERE id = ?').run(record.id) + fixture.database.exec('UPDATE storage_actors SET state = \'revoked\'') + expect(() => withTransaction(fixture.database, () => bind(fixture.database, target, record.id))).toThrow('FILE_PREPARATION_IDENTITY_CHANGED') + await prepared.rollback() + }) + + it.each([false, true])('retains unknown outcomes until reopen determines persisted ownership: committed=%s', async (committed) => { + const fixture = await setup() + const { record, prepared, target } = await prepare(fixture) + const unknown = join(dirname(record.storedPath), 'unknown.bin') + await writeFile(unknown, 'not in catalog') + if (committed) { + message(fixture.database) + withTransaction(fixture.database, () => bind(fixture.database, target, record.id)) + } + await prepared.retain() + expect((await fixture.service.reconcileStorage()).removedOrphanFiles).toBe(0) + expect((await readFile(target)).toString()).toBe('source bytes') + await fixture.reopen() + await fixture.service.reconcileStorage() + expect(fixture.repository.persistence!.get(target)?.state).toBe(committed ? 'ready' : 'deleted') + expect(fixture.repository.persistence!.get(record.storedPath)?.state).toBe(committed ? 'deleted' : 'ready') + expect((await readFile(unknown)).toString()).toBe('not in catalog') + }) + + it('recovers a deletion claim after unlink without allowing references or path reuse', async () => { + const fixture = await setup() + const record = await fixture.upload() + const persistence = fixture.repository.persistence! + fixture.repository.removeDraft(record.id) + const claimed = persistence.claim(record.storedPath)! + expect(claimed.state).toBe('deleting') + expect(() => persistence.reference(claimed.id, { kind: 'export', ownerId: 'export-1', bindingId: 'download' })).toThrow('FILE_NOT_READY') + await unlink(record.storedPath) + await fixture.reopen() + await fixture.service.reconcileStorage() + expect(fixture.repository.persistence!.get(record.storedPath)?.state).toBe('deleted') + expect(() => fixture.repository.persistence!.prepare({ operationId: 'new', availableBytes: 100, stagingDirectory: join(fixture.root, 'staging'), ownerInstance: 'new', kind: 'import', targetOwnerId: 'draft-2', path: record.storedPath, sizeBytes: 12 })).toThrow() + }) + + it('protects active reads and marks same-size corruption unavailable while retaining references', async () => { + const fixture = await setup() + const record = await fixture.upload() + let release!: () => void + let reading!: () => void + const started = new Promise((resolve) => { + reading = resolve + }) + const waiting = new Promise((resolve) => { + release = resolve + }) + const service = new AttachmentService({ paths: fixture.paths, repository: fixture.repository, readFile: (async (path: string) => { + reading() + await waiting + return readFile(path) + }) as typeof readFile }) + const pending = service.preparePrompt([record.id], '', null, 'draft-1') + await started + expect(await service.release([record.id])).toEqual([]) + release() + await pending + await service.dispose() + await writeFile(record.storedPath, 'broken bytes') + await expect(fixture.service.readText(record.id, 'draft-1', null)).rejects.toMatchObject({ code: 'ATTACHMENT_INVALID' }) + expect(fixture.repository.persistence!.get(record.storedPath)?.state).toBe('unavailable') + expect(fixture.database.prepare('SELECT count(*) AS count FROM file_references').get()?.count).toBe(1) + expect(fixture.repository.findById(record.id)).not.toBeNull() + }) + + it('cleans only cataloged interrupted publications and never adopts partial bytes as ready', async () => { + const fixture = await setup() + const path = join(fixture.paths.draftAttachments('interrupted'), 'partial.txt') + await mkdir(dirname(path), { recursive: true }) + const prepared = fixture.repository.persistence!.prepare({ operationId: 'interrupted', availableBytes: 100, stagingDirectory: join(fixture.root, 'staging'), ownerInstance: 'old', kind: 'import', targetOwnerId: 'interrupted', path, sizeBytes: 12 }) + await mkdir(dirname(prepared.temporaryPath!), { recursive: true }) + await writeFile(prepared.temporaryPath!, 'partial') + await expect(fixture.service.reconcileStorage()).resolves.toMatchObject({ removedOrphanFiles: 1 }) + expect(fixture.repository.persistence!.get(path)?.state).toBe('deleted') + await expect(readFile(prepared.temporaryPath!)).rejects.toMatchObject({ code: 'ENOENT' }) + }) + + it('copies a draft into independently owned bytes in the caller transaction', async () => { + const fixture = await setup() + const source = await fixture.upload() + const prepared = await fixture.service.prepareDraftCopy({ sourceDraftId: 'draft-1', targetDraftId: 'draft-2', attachmentIds: [source.id] }) + const target = prepared.records[0]! + expect(target.id).not.toBe(source.id) + expect(target.storedPath).not.toBe(source.storedPath) + expect(fixture.repository.findById(target.id)).toBeNull() + expect(await fixture.service.release([source.id])).toEqual([]) + expect(() => withTransaction(fixture.database, () => { + prepared.validate() + fixture.repository.create(target) + throw new Error('draft command rolled back') + })).toThrow('draft command rolled back') + expect(fixture.repository.findById(target.id)).toBeNull() + withTransaction(fixture.database, () => { + prepared.validate() + fixture.repository.create(target) + }) + await prepared.commit() + expect(await fixture.service.release([source.id])).toEqual([source.id]) + expect((await readFile(target.storedPath)).toString()).toBe('source bytes') + expect(fixture.database.prepare('SELECT kind, owner_id FROM file_references').all()).toEqual([{ kind: 'draft', owner_id: 'draft-2' }]) + }) + + it('leaves an uncertain draft copy protected until the reopened owner can verify its reference', async () => { + const fixture = await setup() + const source = await fixture.upload() + const prepared = await fixture.service.prepareDraftCopy({ sourceDraftId: 'draft-1', targetDraftId: 'draft-2', attachmentIds: [source.id] }) + const target = prepared.records[0]! + await prepared.retain() + expect(await fixture.service.release([source.id])).toEqual([]) + await fixture.reopen() + await fixture.service.reconcileStorage() + expect(fixture.repository.persistence!.get(source.storedPath)?.state).toBe('ready') + expect(fixture.repository.persistence!.get(target.storedPath)?.state).toBe('deleted') + }) +}) diff --git a/apps/buddy/service/src/attachments/attachmentMetadata.ts b/apps/buddy/service/src/attachments/attachmentMetadata.ts new file mode 100644 index 000000000..8e232597d --- /dev/null +++ b/apps/buddy/service/src/attachments/attachmentMetadata.ts @@ -0,0 +1,82 @@ +import type { AttachmentRecord } from '../storage/attachmentRepository' +import type { AttachmentDocumentReference } from './AttachmentDocumentReference' +import type { AttachmentImageReference } from './AttachmentImageReference' +import { extname } from 'node:path' +import { BUDDY_MEDIA_EXTENSIONS, BUDDY_MEDIA_FILE_BYTES_LIMIT, isDocumentMimeType } from '../../../shared/conversation/attachmentFormats' +import { BUDDY_ATTACHMENT_TOTAL_BYTES_LIMIT, BUDDY_TEXT_ATTACHMENT_BYTES_LIMIT, BUDDY_TEXT_ATTACHMENT_EXTENSIONS, getAttachmentKind, isTextAttachmentMimeType } from '../../../shared/conversation/attachmentPolicy' +import { AttachmentError } from './AttachmentError' + +const MIME_TYPES: Readonly> = { + ...BUDDY_MEDIA_EXTENSIONS, + '.csv': 'text/csv', + '.gif': 'image/gif', + '.jpeg': 'image/jpeg', + '.jpg': 'image/jpeg', + '.json': 'application/json', + '.md': 'text/markdown', + '.pdf': 'application/pdf', + '.png': 'image/png', + '.toml': 'application/toml', + '.tsv': 'text/tab-separated-values', + '.txt': 'text/plain', + '.webp': 'image/webp', + '.xml': 'application/xml', + '.yaml': 'application/yaml', + '.yml': 'application/yaml', +} +const SUPPORTED_IMAGE_MIME_TYPES = new Set( + Object.values(MIME_TYPES).filter(mimeType => mimeType.startsWith('image/')), +) +const textExtensions = new Set(BUDDY_TEXT_ATTACHMENT_EXTENSIONS) + +export function toImageReference(record: AttachmentRecord): AttachmentImageReference { + return { attachmentId: record.id, mimeType: record.mimeType } +} + +export function toDocumentReference(record: AttachmentRecord): AttachmentDocumentReference { + if (!isDocumentMimeType(record.mimeType)) + throw new AttachmentError('VALIDATION_FAILED') + return { attachmentId: record.id, mimeType: record.mimeType } +} + +export function getDocumentKind(mimeType: string): 'pdf' | 'audio' | 'video' { + const kind = getAttachmentKind(mimeType) + if (kind !== 'pdf' && kind !== 'audio' && kind !== 'video') + throw new AttachmentError('VALIDATION_FAILED') + return kind +} + +export function validateTotalBytes( + sizes: readonly number[], + limit = BUDDY_ATTACHMENT_TOTAL_BYTES_LIMIT, + errorCode = 'VALIDATION_FAILED', +): void { + if (sizes.reduce((total, size) => total + size, 0) > limit) + throw new AttachmentError(errorCode) +} + +export function normalizeAttachmentMetadata(input: { mimeType: string, name: string, sizeBytes: number }) { + const name = input.name.trim() + const mimeType = extname(name).toLowerCase() === '.pdf' || Object.hasOwn(BUDDY_MEDIA_EXTENSIONS, extname(name).toLowerCase()) || textExtensions.has(extname(name).slice(1).toLowerCase()) + ? inferMimeType(name) + : input.mimeType.trim() || inferMimeType(name) + if (!name || /[/\\\0]/.test(name) || !Number.isSafeInteger(input.sizeBytes) || input.sizeBytes < 0) + throw new AttachmentError('VALIDATION_FAILED') + if (!isDocumentMimeType(mimeType) && !SUPPORTED_IMAGE_MIME_TYPES.has(mimeType) && !isTextAttachmentMimeType(mimeType)) + throw new AttachmentError('ATTACHMENT_UNSUPPORTED') + const byteLimit = isTextAttachmentMimeType(mimeType) + ? BUDDY_TEXT_ATTACHMENT_BYTES_LIMIT + : isDocumentMimeType(mimeType) && mimeType !== 'application/pdf' + ? BUDDY_MEDIA_FILE_BYTES_LIMIT + : BUDDY_ATTACHMENT_TOTAL_BYTES_LIMIT + if (input.sizeBytes > byteLimit) + throw new AttachmentError('ATTACHMENT_TOO_LARGE') + if (isDocumentMimeType(mimeType) && input.sizeBytes === 0) + throw new AttachmentError('ATTACHMENT_INVALID') + return { mimeType, name, sizeBytes: input.sizeBytes } +} + +export function inferMimeType(path: string): string { + const extension = extname(path).toLowerCase() + return MIME_TYPES[extension] ?? (textExtensions.has(extension.slice(1)) ? 'text/plain' : 'application/octet-stream') +} diff --git a/apps/buddy/service/src/attachments/registerAttachmentRpc.ts b/apps/buddy/service/src/attachments/registerAttachmentRpc.ts index d583975e9..94b1c5cc1 100644 --- a/apps/buddy/service/src/attachments/registerAttachmentRpc.ts +++ b/apps/buddy/service/src/attachments/registerAttachmentRpc.ts @@ -9,12 +9,16 @@ export interface RegisterAttachmentRpcOptions { rpc: RuntimeRequestRegistrar service: Pick< AttachmentService, - 'resolvePreview' + 'resolvePreview' | 'readPreview' > } export function registerAttachmentRpc(options: RegisterAttachmentRpcOptions): () => void { const disposers = [ + options.rpc.onRequest('attachments.readPreview', (params) => { + const input = parse(z.object({ attachmentId: idSchema }).strict(), params) + return options.service.readPreview(input.attachmentId) + }), options.rpc.onRequest('attachments.resolvePreview', (params) => { const input = parse(z.object({ attachmentId: idSchema }).strict(), params) return options.service.resolvePreview(input.attachmentId) diff --git a/apps/buddy/service/src/automations/AutomationScheduler.ts b/apps/buddy/service/src/automations/AutomationScheduler.ts index 0082bfa11..a46cac3ac 100644 --- a/apps/buddy/service/src/automations/AutomationScheduler.ts +++ b/apps/buddy/service/src/automations/AutomationScheduler.ts @@ -20,6 +20,7 @@ export interface AutomationSchedulerOptions { automationService: AutomationService clock?: AutomationClock createOwnerId?: () => string + canDispatch?: () => boolean dispatch: (occurrence: AutomationOccurrenceRecord) => Promise onObserverError?: (error: unknown) => void } @@ -28,6 +29,7 @@ export class AutomationScheduler { readonly #automationService: AutomationService readonly #clock: AutomationClock readonly #dispatch: AutomationSchedulerOptions['dispatch'] + readonly #canDispatch: () => boolean readonly #changes: Emitter> readonly onDidChange: Emitter>['event'] readonly #owner: string @@ -45,6 +47,7 @@ export class AutomationScheduler { this.#automationService = options.automationService this.#clock = options.clock ?? systemAutomationClock this.#dispatch = options.dispatch + this.#canDispatch = options.canDispatch ?? (() => true) this.#changes = new Emitter(options.onObserverError ?? (() => {})) this.onDidChange = this.#changes.event this.#owner = (options.createOwnerId ?? randomUUID)() @@ -121,8 +124,11 @@ export class AutomationScheduler { this.#publish('degraded', 'AUTOMATION_SCAN_FAILED') throw error }).finally(() => { - if (this.#scan === scan) - this.#scan = null + if (this.#scan !== scan) + return + this.#scan = null + if (this.#scanRequested && !this.#disposed) + return this.#requestScan() }) this.#scan = scan return scan @@ -218,7 +224,7 @@ export class AutomationScheduler { } #dispatchAvailable(): void { - if (this.#disposed) + if (this.#disposed || !this.#canDispatch()) return const available = AUTOMATION_GLOBAL_CONCURRENCY - this.#activeDispatches.size if (available <= 0) @@ -232,7 +238,13 @@ export class AutomationScheduler { }) for (const occurrence of leased) { const dispatch = Promise.resolve() - .then(() => this.#disposed ? undefined : this.#dispatch(occurrence)) + .then(() => { + if (this.#disposed || !this.#canDispatch()) { + this.#automationService.finishQueued({ errorCode: 'RUNTIME_RESTARTED', id: occurrence.id, leaseOwner: this.#owner, status: 'skipped' }) + return + } + return this.#dispatch(occurrence) + }) .catch((error) => { if (this.#disposed && error instanceof Error && error.name === 'AbortError') return diff --git a/apps/buddy/service/src/automations/__tests__/AutomationOccurrenceLifecycleService.spec.ts b/apps/buddy/service/src/automations/__tests__/AutomationOccurrenceLifecycleService.spec.ts index ec2af52ee..3ddbfa593 100644 --- a/apps/buddy/service/src/automations/__tests__/AutomationOccurrenceLifecycleService.spec.ts +++ b/apps/buddy/service/src/automations/__tests__/AutomationOccurrenceLifecycleService.spec.ts @@ -1,5 +1,5 @@ import type { DatabaseSync } from 'node:sqlite' -import { afterEach, describe, expect, it, vi } from 'vitest' +import { afterEach, describe, expect, it } from 'vitest' import { createAutomationRepositories } from '../../storage/automationRepository' import { createAutomationTurnRepository } from '../../storage/automationTurnRepository' @@ -63,155 +63,16 @@ describe('automationOccurrenceLifecycleService', () => { lifecycle.onDidCleanup(event => states.push(event.status)) const first = lifecycle.deleteOccurrence(f.occurrence.id) const second = lifecycle.deleteConversation('conversation-recovery') - expect(first).toBe(second) const stopping = lifecycle.dispose() expect(states).toEqual(['started']) gate.resolve(true) await stopping - await expect(first).resolves.toMatchObject({ deleted: true }) + await expect(Promise.all([first, second])).resolves.toMatchObject([{ deleted: true }, { deleted: true }]) expect(states).toEqual(['started', 'completed']) await expect(lifecycle.deleteOccurrence(f.occurrence.id)).rejects.toThrow('stopped') }) - - it('deletes a bound occurrence, its conversation, and its notification as one lifecycle', async () => { - const database = openBuddyDatabase({ databasePath: ':memory:' }) - databases.push(database) - const automations = new AutomationService({ - clock: { now: () => Temporal.Instant.from('2026-08-24T00:00:00.000Z') }, - createId: incrementalIds('automation'), - repositories: createAutomationRepositories(database), - }) - const automation = automations.create({ - draft: { - executionProfile: 'workspace_write', - model: { mode: 'default' }, - name: 'Automation', - spaceId: null, - prompt: 'Run automation', - timing: { - activeFrom: null, - activeUntil: null, - schedule: { cadence: 'daily', kind: 'calendar', localTime: '12:00' }, - timezone: 'Asia/Shanghai', - }, - }, - requestId: 'create-1', - }) - automations.runNow({ - automationId: automation.id, - expectedRevision: automation.revision, - requestId: 'run-1', - }) - const occurrence = automations.leaseQueued({ - leaseExpiresAt: '2026-08-24T00:01:00.000Z', - limit: 1, - now: '2026-08-24T00:00:00.000Z', - owner: 'scheduler-1', - })[0]! - const bound = createAutomationTurnRepository(database).bind({ - boundAt: '2026-08-24T00:00:10.000Z', - branchId: 'branch-1', - contextWindow: 200_000, - executionContext: null, - conversationId: 'conversation-1', - leaseOwner: 'scheduler-1', - maxTokens: 32_000, - messageId: 'message-1', - model: 'model-1', - occurrenceId: occurrence.id, - spaceId: null, - provider: 'provider-1', - reasoning: null, - runId: 'run-1', - }) - expect(bound.kind).toBe('bound') - const deleteConversation = vi.fn(async () => true) - const removeAutomationRun = vi.fn() - const lifecycle = new AutomationOccurrenceLifecycleService({ - automations, - conversationLifecycle: { delete: deleteConversation, recover: async () => false }, - notifications: { removeAutomationRun }, - }) - - await expect(lifecycle.deleteOccurrence(occurrence.id)).resolves.toEqual({ - automationId: automation.id, - deleted: true, - }) - expect(deleteConversation).toHaveBeenCalledWith('conversation-1') - expect(removeAutomationRun).toHaveBeenCalledWith('run-1') - expect(automations.listHistory({ limit: 20 }).items).toEqual([]) - }) - - it('hides the same occurrence when deletion starts from chat history', async () => { - const database = openBuddyDatabase({ databasePath: ':memory:' }) - databases.push(database) - const automations = new AutomationService({ - clock: { now: () => Temporal.Instant.from('2026-08-24T00:00:00.000Z') }, - createId: incrementalIds('automation'), - repositories: createAutomationRepositories(database), - }) - const automation = automations.create({ - draft: { - executionProfile: 'workspace_write', - model: { mode: 'default' }, - name: 'Automation', - spaceId: null, - prompt: 'Run automation', - timing: { - activeFrom: null, - activeUntil: null, - schedule: { cadence: 'daily', kind: 'calendar', localTime: '12:00' }, - timezone: 'Asia/Shanghai', - }, - }, - requestId: 'create-2', - }) - automations.runNow({ - automationId: automation.id, - expectedRevision: automation.revision, - requestId: 'run-2', - }) - const occurrence = automations.leaseQueued({ - leaseExpiresAt: '2026-08-24T00:01:00.000Z', - limit: 1, - now: '2026-08-24T00:00:00.000Z', - owner: 'scheduler-1', - })[0]! - createAutomationTurnRepository(database).bind({ - boundAt: '2026-08-24T00:00:10.000Z', - branchId: 'branch-2', - contextWindow: 200_000, - executionContext: null, - conversationId: 'conversation-2', - leaseOwner: 'scheduler-1', - maxTokens: 32_000, - messageId: 'message-2', - model: 'model-1', - occurrenceId: occurrence.id, - spaceId: null, - provider: 'provider-1', - reasoning: null, - runId: 'run-2', - }) - const lifecycle = new AutomationOccurrenceLifecycleService({ - automations, - conversationLifecycle: { delete: vi.fn(async () => true), recover: async () => false }, - notifications: { removeAutomationRun: vi.fn() }, - }) - - await expect(lifecycle.deleteConversation('conversation-2')).resolves.toEqual({ - automationId: automation.id, - deleted: true, - }) - expect(automations.listHistory({ limit: 20 }).items).toEqual([]) - }) }) -function incrementalIds(prefix: string): () => string { - let next = 0 - return () => `${prefix}-${++next}` -} - function createBoundFixture() { const database = openBuddyDatabase({ databasePath: ':memory:' }) databases.push(database) diff --git a/apps/buddy/service/src/automations/__tests__/AutomationScheduler.spec.ts b/apps/buddy/service/src/automations/__tests__/AutomationScheduler.spec.ts index 78c826015..d02b00e0e 100644 --- a/apps/buddy/service/src/automations/__tests__/AutomationScheduler.spec.ts +++ b/apps/buddy/service/src/automations/__tests__/AutomationScheduler.spec.ts @@ -20,9 +20,37 @@ afterEach(() => { }) describe('automationScheduler', () => { + it('claims due work when a wake arrives while the prior scan is settling', async () => { + const fixture = createFixture('2026-08-24T00:00:00.000Z') + const automation = fixture.service.create({ draft: onceDraft('2026-08-24T00:00:10.000Z'), requestId: 'settling-wake' }) + const scheduler = fixture.scheduler(async (occurrence) => { + fixture.service.finishQueued({ id: occurrence.id, leaseOwner: occurrence.leaseOwner, status: 'skipped', errorCode: 'AUTOMATION_DEFAULT_MODEL_UNAVAILABLE' }) + }) + let wake: Promise | undefined + const subscription = scheduler.onDidChange((event) => { + if (event.state !== 'ready' || wake) + return + fixture.clock.set('2026-08-24T00:00:20.000Z') + wake = scheduler.wake() + }) + try { + await scheduler.start() + await wake + await scheduler.settle() + expect(fixture.service.listHistory({ automationId: automation.id }).items).toEqual([ + expect.objectContaining({ status: 'skipped', scheduledFor: '2026-08-24T00:00:10.000Z' }), + ]) + } + finally { + subscription.dispose() + await scheduler.dispose() + await scheduler.settle() + } + }) + it('does not become ready after the first scan fails and permits a later successful start', async () => { const fixture = createFixture('2026-08-24T00:00:00.000Z') - const listDue = vi.spyOn(fixture.service, 'listDue').mockImplementationOnce(() => { + vi.spyOn(fixture.service, 'listDue').mockImplementationOnce(() => { throw new Error('SQLite unavailable') }) const scheduler = fixture.scheduler(async () => {}) @@ -32,7 +60,6 @@ describe('automationScheduler', () => { expect(states).toEqual(['starting', 'degraded']) await scheduler.start() expect(scheduler.state).toBe('ready') - expect(listDue).toHaveBeenCalledTimes(2) await scheduler.dispose() await scheduler.settle() expect(scheduler.state).toBe('drained') @@ -46,12 +73,10 @@ describe('automationScheduler', () => { fixture.service.finishQueued({ id: occurrence.id, leaseOwner: occurrence.leaseOwner, status: 'skipped', errorCode: 'AUTOMATION_DEFAULT_MODEL_UNAVAILABLE' }) }) const wake = vi.spyOn(scheduler, 'wake') - const listDue = vi.spyOn(fixture.service, 'listDue') const changes = new AutomationChangeCoordinator({ service: fixture.service, wakeScheduler: () => scheduler.wake(), notify: () => {} }) await scheduler.start() await scheduler.settle() expect(wake).not.toHaveBeenCalled() - expect(listDue).toHaveBeenCalledTimes(1) expect(fixture.service.listHistory({}).items[0]?.status).toBe('skipped') await scheduler.dispose() await scheduler.settle() diff --git a/apps/buddy/service/src/automations/__tests__/registerAutomationRpc.spec.ts b/apps/buddy/service/src/automations/__tests__/registerAutomationRpc.spec.ts deleted file mode 100644 index 0273358d5..000000000 --- a/apps/buddy/service/src/automations/__tests__/registerAutomationRpc.spec.ts +++ /dev/null @@ -1,200 +0,0 @@ -import type { DatabaseSync } from 'node:sqlite' -import type { RuntimeRequestHandler } from '../../../../shared/runtime/rpcPeer' -import type { RuntimeRpcRegistrar } from '../../rpc/runtimeRequest' -import { afterEach, describe, expect, it } from 'vitest' -import { - automationOccurrencePageSchema, - automationRunNowResultSchema, -} from '../../../../shared/automation' - -import { createAutomationRepositories } from '../../storage/automationRepository' -import { openBuddyDatabase } from '../../storage/database' -import { AutomationChangeCoordinator } from '../AutomationChangeCoordinator' -import { AutomationOccurrenceLifecycleService } from '../AutomationOccurrenceLifecycleService' -import { AutomationService } from '../AutomationService' -import { registerAutomationRpc } from '../registerAutomationRpc' - -const databases: DatabaseSync[] = [] - -afterEach(() => { - for (const database of databases.splice(0)) - database.close() -}) - -describe('registerAutomationRpc', () => { - it('owns mutation, public occurrence projection, validation and wake notifications', async () => { - const database = openBuddyDatabase({ databasePath: ':memory:' }) - databases.push(database) - const clock = { now: () => Temporal.Instant.from('2026-08-24T00:00:00.000Z') } - const service = new AutomationService({ - clock, - createId: incrementalIds('automation'), - repositories: createAutomationRepositories(database), - }) - const published: string[] = [] - let wakeCount = 0 - const changes = new AutomationChangeCoordinator({ - notify: automationId => published.push(automationId), - service, - wakeScheduler: () => { - wakeCount += 1 - }, - }) - const lifecycle = new AutomationOccurrenceLifecycleService({ - automations: service, - conversationLifecycle: { delete: async () => false, recover: async () => false }, - notifications: { removeAutomationRun: () => false }, - }) - const harness = createRpcHarness() - const unregister = registerAutomationRpc({ - approvals: { listPending: () => [] }, - changes, - clock, - lifecycle, - rpc: harness.rpc, - runs: { findById: () => null }, - service, - }) - - const created = await harness.invoke('automations.create', { - draft: dailyDraft('Original'), - requestId: 'create-1', - }) as { id: string, revision: number } - const updated = await harness.invoke('automations.update', { - automationId: created.id, - draft: dailyDraft('Updated'), - expectedRevision: created.revision, - requestId: 'update-1', - }) as { id: string, revision: number } - const runNow = automationRunNowResultSchema.parse( - await harness.invoke('automations.runNow', { - automationId: updated.id, - expectedRevision: updated.revision, - requestId: 'run-now-1', - }), - ) - expect(runNow.outcome).toBe('started') - expect(JSON.stringify(runNow)).not.toContain('executionSnapshot') - expect(JSON.stringify(runNow)).not.toContain('dedupeKey') - - const repeated = automationRunNowResultSchema.parse( - await harness.invoke('automations.runNow', { - automationId: updated.id, - expectedRevision: updated.revision, - requestId: 'run-now-2', - }), - ) - expect(repeated).toMatchObject({ - occurrence: { id: runNow.occurrence.id }, - outcome: 'already_running', - }) - const history = automationOccurrencePageSchema.parse( - await harness.invoke('automations.listOccurrences', { - automationId: updated.id, - limit: 20, - }), - ) - expect(history.items).toEqual([ - expect.objectContaining({ - automationId: updated.id, - automationName: 'Updated', - effectiveStatus: 'queued', - pendingApprovalCount: 0, - }), - ]) - - const paused = await harness.invoke('automations.pause', { - automationId: updated.id, - expectedRevision: updated.revision, - requestId: 'pause-1', - }) as { revision: number, status: string } - expect(paused).toMatchObject({ revision: 3, status: 'paused' }) - const resumed = await harness.invoke('automations.resume', { - automationId: updated.id, - expectedRevision: paused.revision, - requestId: 'resume-1', - }) as { revision: number, status: string } - expect(resumed).toMatchObject({ revision: 4, status: 'active' }) - await expect(harness.invoke('automations.deleteOccurrence', { - occurrenceId: runNow.occurrence.id, - })).resolves.toBe(true) - await expect(harness.invoke('automations.delete', { - automationId: updated.id, - expectedRevision: resumed.revision, - requestId: 'delete-1', - })).resolves.toMatchObject({ revision: 5, status: 'completed' }) - await expect(harness.invoke('automations.get', { automationId: updated.id })) - .rejects - .toMatchObject({ code: 'AUTOMATION_NOT_FOUND' }) - await expect(harness.invoke('automations.list', { unexpected: true })) - .rejects - .toMatchObject({ code: 'VALIDATION_FAILED' }) - - expect(published).toEqual([ - created.id, - created.id, - created.id, - created.id, - created.id, - created.id, - created.id, - ]) - expect(wakeCount).toBe(6) - harness.notify('scheduler.wake', { reason: 'resume' }) - harness.notify('scheduler.wake', { reason: 'invalid' }) - expect(wakeCount).toBe(7) - - unregister() - expect(harness.notificationListenerCount()).toBe(0) - }) -}) - -function createRpcHarness() { - const handlers = new Map() - const notificationListeners = new Set<(method: string, params: unknown) => void>() - const rpc: RuntimeRpcRegistrar = { - onNotification(listener) { - notificationListeners.add(listener) - return () => notificationListeners.delete(listener) - }, - onRequest(method, handler) { - handlers.set(method, handler) - return () => handlers.delete(method) - }, - } - return { - async invoke(method: string, params: unknown) { - const handler = handlers.get(method) - if (!handler) - throw new Error(`Missing handler: ${method}`) - return await handler(params) - }, - notificationListenerCount: () => notificationListeners.size, - notify(method: string, params: unknown) { - for (const listener of notificationListeners) - listener(method, params) - }, - rpc, - } -} - -function dailyDraft(name: string) { - return { - executionProfile: 'workspace_write' as const, - model: { mode: 'default' as const }, - name, - spaceId: null, - prompt: 'Run automation', - timing: { - activeFrom: null, - activeUntil: null, - schedule: { cadence: 'daily' as const, kind: 'calendar' as const, localTime: '12:00' }, - timezone: 'Asia/Shanghai', - }, - } -} - -function incrementalIds(prefix: string): () => string { - let next = 0 - return () => `${prefix}-${++next}` -} diff --git a/apps/buddy/service/src/automations/registerAutomationRpc.ts b/apps/buddy/service/src/automations/registerAutomationRpc.ts index f5aa5da07..153322ee6 100644 --- a/apps/buddy/service/src/automations/registerAutomationRpc.ts +++ b/apps/buddy/service/src/automations/registerAutomationRpc.ts @@ -2,7 +2,7 @@ import type { AutomationOccurrence, AutomationRunNowResult, } from '../../../shared/automation' -import type { RuntimeRpcRegistrar } from '../rpc/runtimeRequest' +import type { RuntimeRequestRegistrar } from '../rpc/runtimeRequest' import type { ApprovalRepository } from '../storage/approvalRepository' import type { AutomationOccurrenceRecord } from '../storage/automationOccurrenceRecord' import type { RunRepository } from '../storage/runRepository' @@ -11,7 +11,7 @@ import type { AutomationOccurrenceLifecycleService } from './AutomationOccurrenc import type { AutomationClock } from './AutomationScheduleEvaluator' import type { AutomationService } from './AutomationService' -import { automationNotifications, automationsRpc } from '../../../shared/automation/automationApi' +import { automationsRpc } from '../../../shared/automation/automationApi' import { registerRuntimeRequest } from '../rpc/runtimeRequest' import { previewAutomationSchedule } from './AutomationScheduleEvaluator' import { AutomationServiceError } from './AutomationService' @@ -21,7 +21,7 @@ export interface RegisterAutomationRpcOptions { changes: AutomationChangeCoordinator clock: AutomationClock lifecycle: Pick - rpc: RuntimeRpcRegistrar + rpc: RuntimeRequestRegistrar runs: Pick service: AutomationService } @@ -29,13 +29,6 @@ export interface RegisterAutomationRpcOptions { export function registerAutomationRpc(options: RegisterAutomationRpcOptions): () => void { const disposers: Array<() => void> = [] - disposers.push(options.rpc.onNotification((method, params) => { - if (method !== automationNotifications.wake.method) - return - if (automationNotifications.wake.params.safeParse(params).success) - options.changes.wakeScheduler() - })) - disposers.push(registerRuntimeRequest(options.rpc, automationsRpc.preview, (input) => { return previewAutomationSchedule(input, options.clock) })) diff --git a/apps/buddy/service/src/chat/ChatCommandService.ts b/apps/buddy/service/src/chat/ChatCommandService.ts index 0a3654dcb..8e7c0a1c7 100644 --- a/apps/buddy/service/src/chat/ChatCommandService.ts +++ b/apps/buddy/service/src/chat/ChatCommandService.ts @@ -6,6 +6,7 @@ import type { RunEventAvailability } from '../events/RunEventPorts' import type { CommandRequestRecord, CommandRequestRepository, + PrepareCommandRequestInput, } from '../storage/commandRequestRepository' import type { ComposerDraftCommitReceipt } from '../storage/commitComposerDraft' import type { ComposerDraftRepository } from '../storage/composerDraftRepository' @@ -13,14 +14,18 @@ import type { ConversationRepository } from '../storage/conversationRepository' import type { RunRecord } from '../storage/runRecord' import type { RunRepository } from '../storage/runRepository' import type { SpaceRepository } from '../storage/spaceRepository' +import type { ChatInputSource } from './resolveChatInput' import { randomUUID } from 'node:crypto' import { parseBuddyChatCommand } from '../../../shared/conversation/buddyChatCommands' import { buddyUserContentToText, getBuddyUserContentResourceIds } from '../../../shared/conversation/buddyUserContent' import { Emitter } from '../../../shared/events/Emitter' import { copyEventSnapshot } from '../../../shared/events/eventSnapshot' +import { AsyncOperationScope } from '../../../shared/lifecycle/AsyncOperationScope' +import { canonicalCommandJson } from '../commands/commandIntent' import { BuddyServiceError } from '../rpc/runtimeRequest' import { toPublicRun } from '../runs/publicRun' import { requireActiveSpace } from '../spaces/requireActiveSpace' +import { resolveChatInput } from './resolveChatInput' export type ExecuteChatCommandInput = BuddyComposerDraftSend @@ -32,7 +37,7 @@ export type ChatCommandCommit = EventSnapshot<{ facts: [ { kind: 'command.accepted', command: 'compact' }, { kind: 'run.queued', runId: string, purpose: 'conversation.compaction' }, - { kind: 'draft.consumed' } & ComposerDraftCommitReceipt, + ...({ kind: 'draft.consumed' } & ComposerDraftCommitReceipt)[], ] }> @@ -52,7 +57,7 @@ export class ChatCommandService { readonly #options: ChatCommandServiceOptions readonly #committed: Emitter readonly #stopping = new AbortController() - readonly #pending = new Set>() + readonly #operations = new AsyncOperationScope(() => this.#stopping.signal.reason) readonly onDidCommit: Emitter['event'] constructor(options: ChatCommandServiceOptions) { @@ -62,20 +67,11 @@ export class ChatCommandService { } get hasPendingOperations(): boolean { - return this.#pending.size > 0 + return this.#operations.hasPending } execute(input: ExecuteChatCommandInput): Promise> { - const pending = Promise.withResolvers>() - this.#pending.add(pending.promise) - void this.#execute({ ...input }).then((value) => { - this.#pending.delete(pending.promise) - pending.resolve(value) - }, (error) => { - this.#pending.delete(pending.promise) - pending.reject(error) - }) - return pending.promise + return this.#operations.track(() => this.#execute({ ...input })) } async #execute(input: ExecuteChatCommandInput) { @@ -89,9 +85,30 @@ export class ChatCommandService { return toTurnStart(replay, requireValue(replayRun)) } - const draft = requireValue(this.#options.drafts.findById(input.draftId), 'DRAFT_CONFLICT') - if (draft.revision !== input.expectedRevision) - throw new BuddyServiceError('DRAFT_CONFLICT') + const prepared = this.#options.commands.prepare(this.prepare(input)) + + if (!prepared.created) + return toTurnStart(prepared, this.#requireRun(prepared.runId)) + + this.#committed.fire(copyEventSnapshot({ + commitId: prepared.runId, + requestId: prepared.requestId, + conversationId: prepared.conversationId, + branchId: prepared.branchId, + facts: [ + { kind: 'command.accepted', command: prepared.command }, + { kind: 'run.queued', runId: prepared.runId, purpose: 'conversation.compaction' }, + ...(prepared.draftReceipt ? [{ kind: 'draft.consumed' as const, ...prepared.draftReceipt }] : []), + ], + })) + const operation = await this.#options.turnLauncher.launch(prepared.runId, this.#stopping.signal) + void operation.completion.catch(() => {}) + return toTurnStart(prepared, this.#requireRun(operation.runId)) + } + + prepare(input: ChatInputSource & { requestId: string }): PrepareCommandRequestInput { + this.#stopping.signal.throwIfAborted() + const draft = resolveChatInput(input, this.#options.drafts) if (draft.scope.kind !== 'conversation_branch') throw new BuddyServiceError('VALIDATION_FAILED') const content = buddyUserContentToText(draft.content).trim() @@ -117,7 +134,7 @@ export class ChatCommandService { this.#options.conversations.findById(draft.scope.conversationId), ) if ( - conversation.activeBranchId !== draft.scope.branchId + conversation.deletedAt !== null || conversation.approvalPolicy !== draft.executionConfig.approvalPolicy || conversation.executionProfile !== draft.executionConfig.executionProfile || this.#options.conversationLifecycle.isDeleting(conversation.id) @@ -128,41 +145,25 @@ export class ChatCommandService { requireActiveSpace(this.#options.spaces.findById(conversation.spaceId)) this.#options.availability?.assertConversationWritable(conversation.id) const runId = randomUUID() - const prepared = this.#options.commands.prepare({ + return { approvalPolicy: conversation.approvalPolicy, arguments: command.arguments, branchId: draft.scope.branchId, command: command.name, conversationId: conversation.id, createdAt: new Date().toISOString(), - draft: { draftId: draft.draftId, expectedRevision: input.expectedRevision }, + draft: draft.commit, executionProfile: conversation.executionProfile, - requestFingerprint, + requestFingerprint: createCommandFingerprint(input), requestId: input.requestId, runId, - }) - if (!prepared.created) - return toTurnStart(prepared, this.#requireRun(prepared.runId)) - - this.#committed.fire(copyEventSnapshot({ - commitId: prepared.runId, - requestId: prepared.requestId, - conversationId: prepared.conversationId, - branchId: prepared.branchId, - facts: [ - { kind: 'command.accepted', command: prepared.command }, - { kind: 'run.queued', runId: prepared.runId, purpose: 'conversation.compaction' }, - { kind: 'draft.consumed', ...prepared.draftReceipt }, - ], - })) - const operation = await this.#options.turnLauncher.launch(prepared.runId, this.#stopping.signal) - void operation.completion.catch(() => {}) - return toTurnStart(prepared, this.#requireRun(operation.runId)) + } } async dispose(): Promise { + this.#operations.stopAccepting() this.#stopping.abort() - await Promise.allSettled(this.#pending) + await this.#operations.drain() this.#committed.dispose() } @@ -171,8 +172,8 @@ export class ChatCommandService { } } -function createCommandFingerprint(input: ExecuteChatCommandInput): string { - return `${input.draftId}:${input.expectedRevision}` +function createCommandFingerprint(input: ChatInputSource): string { + return 'snapshot' in input ? canonicalCommandJson(input.snapshot) : `${input.draftId}:${input.expectedRevision}` } function toTurnStart(request: CommandRequestRecord, run: RunRecord) { diff --git a/apps/buddy/service/src/chat/ChatControlCommandService.ts b/apps/buddy/service/src/chat/ChatControlCommandService.ts new file mode 100644 index 000000000..2bc3dedc2 --- /dev/null +++ b/apps/buddy/service/src/chat/ChatControlCommandService.ts @@ -0,0 +1,127 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { CommandAdmissionService, CommandResolution } from '../commands/CommandAdmissionService' +import type { InputCommandIntent } from '../commands/commandIntent' +import type { ChatControlCommandRepository } from '../storage/ChatControlCommandRepository' +import type { CommandDecision, CommandRejection, StorageCommand } from '../storage/CommandCommitter' +import type { ChatQueueService } from './ChatQueueService' +import { BuddyServiceError } from '../rpc/runtimeRequest' +import { ChatControlConflictError } from '../storage/ChatControlCommandRepository' +import { afterTransactionCommit } from '../storage/transaction' + +export type ChatControlCommandMethod = 'chat.queue.cancel' | 'chat.queue.steer' | 'chat.queue.continue' | 'chat.cancel' | 'runtime.stop' + +interface PreparedControl { + commit: (command: Readonly) => { decision: CommandDecision, notify: () => void | Promise } +} + +interface ChatControlCommandServiceOptions { + database: DatabaseSync + admission: Pick + repository: ChatControlCommandRepository + queue: Pick + onServiceStop: (runIds: readonly string[]) => void | Promise + onDispatchError: (error: unknown) => void +} + +export class ChatControlCommandService { + readonly #options: ChatControlCommandServiceOptions + readonly #effects = new Set>() + + constructor(options: ChatControlCommandServiceOptions) { + this.#options = options + } + + execute(method: ChatControlCommandMethod, input: unknown): Promise { + return this.#options.admission.execute(method, input, { + prepare: (intent, signal) => this.#prepare(method, intent, signal), + reject: rejectControl, + commit: (prepared, command) => { + const committed = prepared.commit(command) + afterTransactionCommit(this.#options.database, () => this.#notify(committed.notify), this.#options.onDispatchError) + return committed.decision + }, + }) + } + + async waitForEffects(): Promise { + await Promise.allSettled(this.#effects) + } + + async #prepare(method: ChatControlCommandMethod, intent: Readonly>, signal: AbortSignal): Promise { + signal.throwIfAborted() + const repository = this.#options.repository + switch (method) { + case 'chat.queue.cancel': { + const target = intent as InputCommandIntent<'chat.queue.cancel'> + return { commit: () => { + repository.cancelQueue(target) + return { decision: { target: { queueId: target.id, conversationId: target.conversationId, branchId: target.branchId }, result: { cancelled: true } }, notify: () => this.#options.queue.publishCancelled(target) } + } } + } + case 'chat.queue.continue': { + const target = intent as InputCommandIntent<'chat.queue.continue'> + repository.assertQueue(target) + const prepared = await this.#options.queue.prepareContinue(target) + signal.throwIfAborted() + return { commit: () => repository.withQueue(target, () => { + const committed = this.#options.queue.commitContinue(prepared) + return { + decision: { target: { queueId: target.id, conversationId: target.conversationId, branchId: target.branchId, runId: committed.request.runId }, result: { requestId: committed.request.requestId } }, + notify: committed.notify, + } + }) } + } + case 'chat.queue.steer': { + const target = intent as InputCommandIntent<'chat.queue.steer'> + repository.assertQueue(target) + repository.assertRun({ runId: target.targetRunId, expectedRunGeneration: target.expectedRunGeneration }, true) + const prepared = await this.#options.queue.prepareSteer(target, target.targetRunId) + signal.throwIfAborted() + return { commit: () => repository.withQueue(target, () => { + repository.assertRun({ runId: target.targetRunId, expectedRunGeneration: target.expectedRunGeneration }, true) + const deliver = this.#options.queue.commitSteer(prepared) + repository.recordDelivery(target.id, target.targetRunId) + return { + decision: { target: { queueId: target.id, conversationId: target.conversationId, branchId: target.branchId, runId: target.targetRunId }, result: { deliveryStage: 'committed' } }, + notify: () => { + let stage: 'submitted' | 'unavailable' | 'unknown' = 'unknown' + try { + stage = deliver() ? 'submitted' : 'unavailable' + } + finally { + repository.observeDelivery(target.id, stage) + } + }, + } + }) } + } + case 'chat.cancel': { + const target = intent as InputCommandIntent<'chat.cancel'> + return { commit: () => { + const decision = repository.cancelRun(target) + return { decision, notify: () => decision.result.cancelRequested ? this.#options.queue.cancelRun(target.runId) : undefined } + } } + } + case 'runtime.stop': { + const target = intent as InputCommandIntent<'runtime.stop'> + return { commit: (command) => { + const decision = repository.stopService(target, command.commandId) + return { decision, notify: () => target.scope === 'stop_service' ? this.#options.onServiceStop(decision.result.runIds) : undefined } + } } + } + } + } + + #notify(operation: () => void | Promise): void { + const effect = Promise.resolve().then(operation).then(() => {}, this.#options.onDispatchError).finally(() => this.#effects.delete(effect)) + this.#effects.add(effect) + } +} + +function rejectControl(error: unknown): CommandRejection | null { + if (error instanceof ChatControlConflictError) + return { errorCode: error.code, target: {}, result: {} } + if (error instanceof BuddyServiceError && (error.code === 'INPUT_DELIVERY_INCOMPATIBLE' || error.code === 'INPUT_DELIVERY_TARGET_UNAVAILABLE')) + return { errorCode: error.code, target: {}, result: {} } + return null +} diff --git a/apps/buddy/service/src/chat/ChatInputCommandService.ts b/apps/buddy/service/src/chat/ChatInputCommandService.ts new file mode 100644 index 000000000..1ffcfc7cb --- /dev/null +++ b/apps/buddy/service/src/chat/ChatInputCommandService.ts @@ -0,0 +1,221 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { SubmittedInput } from '../../../shared/conversation/submittedInput' +import type { CommandAdmissionService, CommandResolution } from '../commands/CommandAdmissionService' +import type { InputCommandIntent } from '../commands/commandIntent' +import type { CommandDecision, CommandRejection, StorageCommand } from '../storage/CommandCommitter' +import type { ChatCommandService } from './ChatCommandService' +import type { ChatQueueService } from './ChatQueueService' +import type { ChatTurnService } from './ChatTurnService' +import type { ComposerDraftService } from './ComposerDraftService' +import type { PreparedTurnAttachments } from './persistPreparedTurn' +import type { PreparedRunCommandService } from './PreparedRunCommandService' +import { randomUUID } from 'node:crypto' +import { BuddyServiceError } from '../rpc/runtimeRequest' +import { CommandRequestConflictError } from '../storage/commandRequestRepository' +import { ComposerDraftCommitConflictError } from '../storage/commitComposerDraft' +import { ComposerDraftConflictError } from '../storage/composerDraftRepository' +import { afterTransactionCommit } from '../storage/transaction' +import { TurnRequestConflictError } from '../storage/turnRequestRepository' +import { isPreparedResourceResultUnknown, PreparedResourceSettlement } from './PreparedResourceSettlement' +import { inputResourceOwner } from './resolveChatInput' + +export type ChatInputCommandMethod = 'chat.submitInput' | 'chat.startTurn' | 'chat.editUserMessage' | 'chat.regenerateAssistant' | 'chat.executeCommand' | 'chat.queue.enqueue' + +interface ChatInputCommandServiceOptions { + database: DatabaseSync + actorId?: string + assertExecutionAvailable?: () => void + admission: Pick + drafts: Pick + turns: Pick + compact: Pick + queue: Pick + preparedRuns: Pick + commitSubmittedInput?: (input: SubmittedInput) => void + onDispatchError: (error: unknown) => void +} + +interface PreparedInputCommand { + submitted?: SubmittedInput + attachments: PreparedTurnAttachments | null + validate: () => CommandRejection | null + commit: (command: Readonly) => { dispatch: () => void | Promise, decision: CommandDecision } +} + +export class ChatInputCommandService { + readonly #options: ChatInputCommandServiceOptions + readonly #dispatches = new Set>() + + constructor(options: ChatInputCommandServiceOptions) { + this.#options = options + } + + lookup(input: unknown) { + return this.#options.admission.lookup(input) + } + + async execute(method: ChatInputCommandMethod, input: unknown): Promise { + let resources: PreparedResourceSettlement | undefined + try { + const resolution = await this.#options.admission.execute(method, input, { + scope: (intent, operation) => 'draftId' in intent ? this.#options.drafts.run(intent.draftId, operation) : operation(), + prepare: async (intent, signal) => { + signal.throwIfAborted() + this.#options.assertExecutionAvailable?.() + if ('draftId' in intent && this.#options.actorId && !this.#ownsDraft(intent.draftId)) + throw new ComposerDraftConflictError() + if ('input' in intent && intent.input.legacyResourceDraftId && !this.#ownsDraft(intent.input.legacyResourceDraftId)) + throw new ComposerDraftConflictError() + const prepared = await this.#prepare(method, intent) + resources = new PreparedResourceSettlement(prepared.attachments, this.#options.onDispatchError) + if ('input' in intent) + prepared.submitted = intent.input + signal.throwIfAborted() + return prepared + }, + reject: rejectInputCommand, + validate: value => value.validate(), + commit: (value, command) => { + this.#options.assertExecutionAvailable?.() + const accepted = value.commit(command) + if (value.submitted) + this.#options.commitSubmittedInput?.(value.submitted) + afterTransactionCommit(this.#options.database, () => { + this.#dispatch(resources!.commit(), accepted.dispatch) + }, this.#options.onDispatchError) + return accepted.decision + }, + }) + await resources?.rollback() + return resolution + } + catch (error) { + await resources?.fail(error) + throw error + } + } + + async waitForDispatches(): Promise { + await Promise.allSettled(this.#dispatches) + } + + async #prepare(method: ChatInputCommandMethod, intent: Readonly>): Promise { + const validateSource = this.#captureSource(intent) + const requestId = randomUUID() + const submitted = 'input' in intent ? { ...intent.input, resourceOwnerId: inputResourceOwner(this.#options.actorId!, intent.input.inputId) } : null + if (submitted && !this.#options.actorId) + throw new BuddyServiceError('VALIDATION_FAILED') + const operation = 'operation' in intent ? ({ start: 'chat.startTurn', edit: 'chat.editUserMessage', enqueue: 'chat.queue.enqueue', command: 'chat.executeCommand' } as const)[intent.operation] : method + switch (operation) { + case 'chat.startTurn': { + const input = submitted ? { snapshot: submitted, ...('buddyId' in intent ? { buddyId: intent.buddyId } : {}), ...('delivery' in intent ? { delivery: intent.delivery } : {}) } : intent as InputCommandIntent<'chat.startTurn'> + if (input.delivery?.target) + throw new BuddyServiceError('INPUT_DELIVERY_INCOMPATIBLE') + const prepared = await this.#options.turns.prepareStart({ ...input, requestId }) + return { + attachments: prepared.stagedAttachments, + validate: () => this.#validate(prepared.stagedAttachments, validateSource), + commit: command => this.#acceptedRun(this.#options.preparedRuns.commitStart(command.fingerprint, prepared.prepared)), + } + } + case 'chat.editUserMessage': { + if (submitted && submitted.scope.kind !== 'message_edit') + throw new BuddyServiceError('VALIDATION_FAILED') + const input = submitted && submitted.scope.kind === 'message_edit' ? { snapshot: submitted, conversationId: submitted.scope.conversationId, userMessageId: submitted.scope.userMessageId } : intent as InputCommandIntent<'chat.editUserMessage'> + const prepared = await this.#options.turns.prepareEdit({ ...input, requestId }) + return { + attachments: prepared.stagedAttachments, + validate: () => this.#validate(prepared.stagedAttachments, validateSource), + commit: command => this.#acceptedRun(this.#options.preparedRuns.commitEdit(command.fingerprint, prepared.prepared)), + } + } + case 'chat.regenerateAssistant': { + const prepared = await this.#options.turns.prepareRegenerate({ ...intent as InputCommandIntent<'chat.regenerateAssistant'>, requestId }) + return { attachments: null, validate: validateSource, commit: command => this.#acceptedRun(this.#options.preparedRuns.commitRegenerate(command.fingerprint, prepared)) } + } + case 'chat.queue.enqueue': { + const input = submitted ? { snapshot: submitted, ...('delivery' in intent ? { delivery: intent.delivery } : {}) } : intent as InputCommandIntent<'chat.queue.enqueue'> + const prepared = await this.#options.queue.prepareEnqueue({ ...input, requestId }) + return { + attachments: prepared.stagedAttachments, + validate: () => this.#validate(prepared.stagedAttachments, validateSource), + commit: (command) => { + const accepted = this.#options.queue.commitEnqueue(prepared.prepared, command.fingerprint) + return { + decision: { target: { queueId: accepted.id, conversationId: accepted.conversationId, branchId: accepted.branchId }, result: { draftReceipt: accepted.draftReceipt } }, + dispatch: () => this.#options.queue.publishEnqueued(prepared.prepared, accepted), + } + }, + } + } + case 'chat.executeCommand': { + const input = submitted ? { snapshot: submitted } : intent as InputCommandIntent<'chat.executeCommand'> + const prepared = this.#options.compact.prepare({ ...input, requestId }) + return { attachments: null, validate: validateSource, commit: command => this.#acceptedRun(this.#options.preparedRuns.commitCompact(command.fingerprint, prepared)) } + } + default: throw new BuddyServiceError('VALIDATION_FAILED') + } + } + + #captureSource(intent: Readonly>): () => CommandRejection | null { + const scope = 'input' in intent ? intent.input.scope : 'draftId' in intent ? this.#options.drafts.find(intent.draftId)?.scope : null + const conversationId = 'conversationId' in intent ? intent.conversationId : scope && 'conversationId' in scope ? scope.conversationId : null + const readConversation = () => conversationId ? this.#options.database.prepare('SELECT deleted_at FROM conversations WHERE id = ?').get(conversationId) : null + const readSource = () => 'sourceRunId' in intent ? this.#options.database.prepare('SELECT conversation_id, branch_id, triggering_message_id, status, generation FROM runs WHERE id = ?').get(intent.sourceRunId) : null + const currentConversation = readConversation() + if (scope && 'branchId' in scope && (!currentConversation || currentConversation.deleted_at !== null + || !this.#options.database.prepare('SELECT 1 FROM conversation_branches WHERE id = ? AND conversation_id = ?').get(scope.branchId, conversationId!))) { + throw new TurnRequestConflictError() + } + const conversation = JSON.stringify(currentConversation) + const source = JSON.stringify(readSource()) + return () => (('draftId' in intent && this.#options.actorId && !this.#ownsDraft(intent.draftId)) || ('input' in intent && intent.input.legacyResourceDraftId && !this.#ownsDraft(intent.input.legacyResourceDraftId))) + ? { errorCode: 'DRAFT_CONFLICT', target: {}, result: {} } + : JSON.stringify(readConversation()) === conversation && JSON.stringify(readSource()) === source + ? null + : { errorCode: 'SOURCE_CHANGED', target: {}, result: {} } + } + + #ownsDraft(draftId: string): boolean { + return Boolean(this.#options.database.prepare('SELECT 1 FROM composer_drafts WHERE id = ? AND owner_actor_id = ?').get(draftId, this.#options.actorId!)) + } + + #validate(attachments: PreparedTurnAttachments, validateSource: () => CommandRejection | null): CommandRejection | null { + const source = validateSource() + if (source) + return source + try { + attachments.validate() + return null + } + catch (error) { + if (isPreparedResourceResultUnknown(error)) + throw error + return { errorCode: 'RESOURCE_UNAVAILABLE', target: {}, result: {} } + } + } + + #acceptedRun(accepted: { request: { runId: string }, decision: CommandDecision }) { + return { decision: accepted.decision, dispatch: () => this.#options.turns.launchAccepted(accepted.request.runId) } + } + + #dispatch(resourceCommit: Promise, dispatch: () => void | Promise): void { + const dispatched = Promise.resolve().then(async () => { + await resourceCommit + await dispatch() + }).catch(this.#options.onDispatchError).finally(() => this.#dispatches.delete(dispatched)) + this.#dispatches.add(dispatched) + } +} + +function rejectInputCommand(error: unknown): CommandRejection | null { + if (error instanceof ComposerDraftCommitConflictError || error instanceof ComposerDraftConflictError + || (error instanceof BuddyServiceError && error.code === 'DRAFT_CONFLICT')) { + return { errorCode: 'DRAFT_CONFLICT', target: {}, result: {} } + } + if (error instanceof TurnRequestConflictError || error instanceof CommandRequestConflictError) + return { errorCode: 'SOURCE_CHANGED', target: {}, result: {} } + if (error instanceof BuddyServiceError && (error.code === 'INPUT_DELIVERY_INCOMPATIBLE' || error.code === 'INPUT_DELIVERY_TARGET_UNAVAILABLE')) + return { errorCode: error.code, target: {}, result: {} } + return null +} diff --git a/apps/buddy/service/src/chat/ChatInputMaterializer.ts b/apps/buddy/service/src/chat/ChatInputMaterializer.ts new file mode 100644 index 000000000..a739698e6 --- /dev/null +++ b/apps/buddy/service/src/chat/ChatInputMaterializer.ts @@ -0,0 +1,307 @@ +import type { BuddyPromptDirective, BuddyUserContentV1, BuddyUserMessageResourceSnapshot } from '../../../shared/conversation/buddyUserContent' +import type { BuddyThinkingLevel } from '../../../shared/conversation/modelSelection' +import type { SkillReference } from '../../../shared/skills/skillApi' +import type { AttachmentService } from '../attachments/AttachmentService' +import type { BuddyDefinitionResolver } from '../buddies/BuddyDefinitionResolver' +import type { BuddyTurnContextItem } from '../BuddyRuntime' +import type { InteractiveModelSelection, RuntimeModelProvider } from '../providers/resolveInteractiveModelSelection' +import type { BuddyMaterializedSkill, SkillService } from '../skills/SkillService' +import type { RunInputRecord, RunInputRepository } from '../storage/runInputRepository' +import type { RunRecord } from '../storage/runRecord' +import type { SpaceRecord } from '../storage/spaceRepository' +import type { TurnRequestRecord } from '../storage/turnRequestRepository' +import type { ChatInputHistoryPoint, ChatInputValidationService } from './ChatInputValidationService' +import { Buffer } from 'node:buffer' +import { isAbsolute, join } from 'node:path' +import { readBoundedFile } from '../../../platform/filesystem/boundedFile' +import { isBuddyReviewCommand, isBuddyRunChatCommand, isRetiredBuddyPromptCommand, parseBuddyChatCommand } from '../../../shared/conversation/buddyChatCommands' +import { buddyUserContentToText, getBuddyUserContentActorReferences } from '../../../shared/conversation/buddyUserContent' +import { inputMaterialInlineBytes } from '../../../shared/conversation/inputMaterialProjection' +import { isBuddyThinkingLevel } from '../../../shared/conversation/modelSelection' +import { resolveGrantedPath } from '../directories/resolveGrantedPath' +import { getModelFileInputMimeTypes } from '../providers/modelCapabilities' +import { resolveInteractiveModelSelection } from '../providers/resolveInteractiveModelSelection' +import { BuddyServiceError } from '../rpc/runtimeRequest' +import { SkillError } from '../skills/skillFiles' +import { formatBuddySkillPrompt } from '../skills/SkillService' +import { BUDDY_REVIEW_PROMPT, buildBuddyReviewPrompt } from './buddyReviewPrompt' + +const MAX_CONTEXT_FILE_BYTES = 1024 * 1024 +const MAX_MODEL_INPUT_BYTES = 4 * 1024 * 1024 +const PROMPT_SECTION_SEPARATOR = '\n\n---\n\n' +type ChatContextItem = BuddyTurnContextItem + +export interface TurnReplay { + request: TurnRequestRecord + run: RunRecord +} + +export interface TurnModelSelection extends Omit { + api: string + contextWindow: number | null + input: Array<'text' | 'image'> + fileInputMimeTypes: ReturnType + maxTokens: number | null + reasoning: string | null +} + +export interface PrepareTurnMaterializationInput { + actorId: string + composer?: { content: BuddyUserContentV1, resourceIds: readonly string[], resources?: readonly BuddyUserMessageResourceSnapshot[], sourceMessageId?: string, quoteReadAvailable?: boolean } + attachmentIds: readonly string[] + content: string + contextItems: readonly ChatContextItem[] + conversationId: string + branchId: string + point?: ChatInputHistoryPoint + draftId: string + legacyResourceDraftId?: string + space: SpaceRecord | null + replay: TurnReplay | null + requestedModel: InteractiveModelSelection | null + preparedSelection?: TurnModelSelection +} + +interface ChatInputMaterializerOptions { + attachments: Pick + definitions: Pick + inputValidation: Pick + providers: RuntimeModelProvider + runInputs: Pick + skills: Pick +} + +export class ChatInputMaterializer { + readonly #options: ChatInputMaterializerOptions + + constructor(options: ChatInputMaterializerOptions) { + this.#options = options + } + + async prepare(input: PrepareTurnMaterializationInput) { + const replayInput = input.replay ? requireValue(this.#options.runInputs.findByRunId(input.replay.run.id)) : null + const selection = input.preparedSelection ?? await this.#resolveSelection( + input.replay?.run ?? null, + replayInput, + input.requestedModel, + ) + const directives = !replayInput && input.composer + ? await materializeComposerDirectives(input.composer.content, input.space, this.#options.skills) + : null + const composer = directives && input.composer + ? { ...input.composer, inlineBytes: inputMaterialInlineBytes(selection), resolveDirective: directives.resolveDirective } + : undefined + const legacyContext = !replayInput && !composer + ? await materializeContextItems(input.contextItems, input.space, this.#options.skills) + : null + const contextItems = replayInput?.contextItems ?? directives?.contextItems ?? legacyContext?.contextItems ?? input.contextItems + if (replayInput) + await this.validateSkills(input.space?.id ?? null, contextItems) + const attachmentPrompt = replayInput + ? { + prompt: '', + records: this.#options.attachments.getInputMetadata(replayInput.attachmentIds, input.conversationId), + materialProjection: undefined, + } + : await this.#options.attachments.preparePrompt( + input.attachmentIds, + input.content, + input.conversationId, + input.draftId, + composer, + input.legacyResourceDraftId, + ) + const context = replayInput + ? '' + : [ + legacyContext?.prompt ?? '', + directives?.contextSuffix ?? '', + input.composer && this.#options.definitions ? formatActorReferences(this.#options.definitions.validateActors(getBuddyUserContentActorReferences(input.composer.content).map(reference => reference.actorId))) : '', + ].filter(Boolean).join(PROMPT_SECTION_SEPARATOR) + const prompt = replayInput?.prompt + ?? [attachmentPrompt.prompt, context].filter(Boolean).join(PROMPT_SECTION_SEPARATOR) + assertPromptSize(prompt) + await this.#options.inputValidation.validate({ + actorId: input.replay?.run.buddyId ?? input.actorId, + conversationId: input.conversationId, + branchId: input.branchId, + point: input.point, + modelId: selection.modelId, + providerId: selection.providerId, + contextWindow: selection.contextWindow, + maxTokens: selection.maxTokens, + prompt, + attachments: attachmentPrompt.records, + }) + const thinkingLevel = normalizeThinkingLevel( + replayInput ? replayInput.reasoning : selection.reasoning, + ) + return { + attachmentPrompt, + prompt, + contextItems, + replayInput, + reviewRequested: directives?.reviewRequested ?? false, + selection, + thinkingLevel, + } + } + + async validateSkills(spaceId: string | null, items: readonly RunInputRecord['contextItems'][number][]) { + const selections = items.filter(item => item.kind === 'skill').map(item => item.skill ?? item.value) + const loaded = await this.#options.skills.materializeForSpace(spaceId, selections) + return items.map(item => item.kind === 'skill' ? { ...item, skill: resolveMaterializedSkill(loaded, item.skill ?? item.value).reference } : item) + } + + async #resolveSelection( + replayRun: RunRecord | null, + replayInput: RunInputRecord | null, + requested: InteractiveModelSelection | null, + ): Promise { + if (replayRun) { + const model = await this.#options.providers.executionModels.resolveAvailable({ + contextWindow: replayRun.contextWindow, + maxTokens: replayRun.maxTokens, + modelId: replayRun.model, + providerId: replayRun.provider, + }) + return { + api: model.api, + contextWindow: replayRun.contextWindow, + input: model.input, + fileInputMimeTypes: getModelFileInputMimeTypes(model), + maxTokens: replayRun.maxTokens, + modelId: replayRun.model, + providerId: replayRun.provider, + reasoning: replayInput?.reasoning ?? null, + serviceTier: replayInput?.serviceTier ?? null, + } + } + return resolveInteractiveModelSelection(this.#options.providers, requested) + } +} + +async function materializeComposerDirectives( + content: BuddyUserContentV1, + space: SpaceRecord | null, + skills: Pick, +) { + const directives = content.body.flatMap(paragraph => paragraph.content.filter(node => node.type === 'prompt_directive')) + if (directives.some(node => node.directive === 'slash_command' && node.commandId)) + throw new BuddyServiceError('VALIDATION_FAILED') + const textCommand = parseBuddyChatCommand(buddyUserContentToText(content)) + if (textCommand && isBuddyRunChatCommand(textCommand.name)) + throw new BuddyServiceError('VALIDATION_FAILED') + const hasReviewDirective = directives.some(node => node.directive === 'slash_command' && isBuddyReviewCommand(node.value)) + const reviewsAsText = textCommand?.name === 'review' && !hasReviewDirective + const selections = directives.flatMap(node => node.directive === 'skill' ? [node.skill ?? node.value] : []) + const loaded = await skills.materializeForSpace(space?.id ?? null, selections) + return { + reviewRequested: hasReviewDirective || reviewsAsText, + contextSuffix: reviewsAsText ? BUDDY_REVIEW_PROMPT : '', + contextItems: loaded.map(skill => ({ kind: 'skill' as const, value: skill.name, skill: skill.reference })), + resolveDirective(directive: BuddyPromptDirective): string { + if (directive.directive === 'skill') { + return formatBuddySkillPrompt(resolveMaterializedSkill(loaded, directive.skill ?? directive.value)) + } + if (isRetiredBuddyPromptCommand(directive.value)) + return directive.value + if (isBuddyReviewCommand(directive.value) && directive.commandMode === 'prompt') + return buildBuddyReviewPrompt(parseBuddyChatCommand(directive.value)!.arguments) + throw new BuddyServiceError('VALIDATION_FAILED') + }, + } +} + +async function materializeContextItems( + items: readonly ChatContextItem[], + space: SpaceRecord | null, + skills: Pick, +) { + const selectedSkills = await skills.materializeForSpace( + space?.id ?? null, + items.filter(item => item.kind === 'skill').map(item => item.skill ?? item.value), + ) + const sections: string[] = [] + for (const item of items) { + if (item.kind === 'skill') { + const skill = resolveMaterializedSkill(selectedSkills, item.skill ?? item.value) + sections.push(formatBuddySkillPrompt(skill)) + continue + } + if (item.kind === 'slashCommand') + continue + if (!space) + throw new BuddyServiceError('DIRECTORY_NOT_AUTHORIZED') + const primaryDirectory = space.primaryDirectory + const directories = [ + ...(primaryDirectory ? [primaryDirectory] : []), + ...space.additionalDirectories, + ] + const requestedPath = isAbsolute(item.value) + ? item.value + : primaryDirectory ? join(primaryDirectory.canonicalRoot, item.value) : null + if (!requestedPath) + throw new BuddyServiceError('DIRECTORY_NOT_AUTHORIZED') + const resolution = await resolveGrantedPath(directories.map(directory => ({ + canonicalRoot: directory.canonicalRoot, + grantId: directory.id, + kind: 'workspace' as const, + root: directory.root, + })), requestedPath, 'existing') + const directory = directories.find(item => item.id === resolution.grantId) + if (!directory) + throw new BuddyServiceError('DIRECTORY_NOT_AUTHORIZED') + const content = await readBoundedFile(directory.canonicalRoot, resolution.canonicalPath) + if (content.byteLength > MAX_CONTEXT_FILE_BYTES) + throw new BuddyServiceError('VALIDATION_FAILED') + sections.push(`上下文文件:${item.value}\n\n${content.toString('utf8')}`) + } + return { + prompt: sections.join(PROMPT_SECTION_SEPARATOR), + contextItems: items.map(item => item.kind === 'skill' ? { ...item, skill: resolveMaterializedSkill(selectedSkills, item.skill ?? item.value).reference } : item), + } +} + +function resolveMaterializedSkill(skills: readonly BuddyMaterializedSkill[], selection: string | SkillReference): BuddyMaterializedSkill { + const matches = skills.filter(skill => typeof selection === 'string' ? skill.name === selection : skill.reference.id === selection.id) + if (matches.length > 1) + throw new SkillError('SKILL_NAME_COLLISION') + if (!matches[0]) + throw new SkillError('SKILL_NOT_FOUND') + return matches[0] +} + +export function normalizeThinkingLevel(value: string | null | undefined): BuddyThinkingLevel | undefined { + if (!value) + return undefined + if (!isBuddyThinkingLevel(value)) + throw new BuddyServiceError('VALIDATION_FAILED') + return value +} + +export function assertPromptSize(prompt: string): void { + if (Buffer.byteLength(prompt) > MAX_MODEL_INPUT_BYTES) + throw new BuddyServiceError('VALIDATION_FAILED') +} + +export function toModelParameters(selection: TurnModelSelection) { + return selection.contextWindow !== null && selection.maxTokens !== null + ? { contextWindow: selection.contextWindow, maxTokens: selection.maxTokens } + : undefined +} + +function requireValue(value: T | null): T { + if (value === null) + throw new BuddyServiceError('VALIDATION_FAILED') + return value +} + +function formatActorReferences(actors: readonly { id: string, name: string }[]): string { + if (!actors.length) + return '' + const routing = actors.length === 1 + ? 'This request is addressed directly to the named actor. This does not change the conversation leader.' + : 'The conversation leader coordinates these explicitly requested actors. Delegate relevant tasks only to these named actors; bound NPCs may help as private workers.' + return `${routing}\nRequested conversation actors (stable IDs):\n${JSON.stringify(actors)}` +} diff --git a/apps/buddy/service/src/chat/ChatInputPreparation.ts b/apps/buddy/service/src/chat/ChatInputPreparation.ts new file mode 100644 index 000000000..d1a91f216 --- /dev/null +++ b/apps/buddy/service/src/chat/ChatInputPreparation.ts @@ -0,0 +1,121 @@ +import type { BuddyUserContentV1, BuddyUserMessageResourceSnapshot } from '../../../shared/conversation/buddyUserContent' +import type { InputMaterialProjectionState } from '../../../shared/conversation/inputMaterialProjection' +import type { AttachmentService } from '../attachments/AttachmentService' +import type { ComposerResourceService } from '../attachments/ComposerResourceService' +import type { BuddyDefinitionResolver } from '../buddies/BuddyDefinitionResolver' +import type { SpaceRecord } from '../storage/spaceRepository' +import type { PrepareTurnRequestInput } from '../storage/turnRequestRepository' +import type { ChatInputMaterializer } from './ChatInputMaterializer' +import type { ChatInputHistoryPoint } from './ChatInputValidationService' +import type { PreparedTurnAttachments } from './persistPreparedTurn' +import type { resolveChatInput } from './resolveChatInput' +import { randomUUID } from 'node:crypto' +import { bindResourceAttachments, buddyUserContentToText, buddyUserMessageContentV1Schema, getResourceAttachmentIds } from '../../../shared/conversation/buddyUserContent' +import { canReadReferenceSnapshots } from '../conversations/referenceSnapshotToolContract' +import { BuddyServiceError } from '../rpc/runtimeRequest' +import { toModelParameters } from './ChatInputMaterializer' +import { combinePreparedAttachments } from './persistPreparedTurn' + +type PreparedInputFields = Pick + +interface PreparationOptions { + definitions: Pick + resources?: Pick + attachments: Pick + materials: ChatInputMaterializer +} + +interface PreparationInput { + draft: ReturnType + buddyId: string + actorIds: string[] + newParticipants?: readonly string[] + conversationId: string + existingConversationId: string | null + branchId: string + space: SpaceRecord | null + point?: ChatInputHistoryPoint + sessionReferences: NonNullable +} + +export class ChatInputPreparation { + readonly #options: PreparationOptions + + constructor(options: PreparationOptions) { + this.#options = options + } + + async prepare(input: PreparationInput, build: (fields: PreparedInputFields, records: Awaited>['attachmentPrompt']['records']) => T): Promise<{ prepared: T, stagedAttachments: PreparedTurnAttachments }> { + const { draft, buddyId, actorIds, conversationId, existingConversationId, branchId, space, sessionReferences } = input + const resolvedActor = await this.#options.definitions.resolve({ buddyId, kind: 'actor', conversationId: existingConversationId, requestedModel: draft.modelSelection }) + const selectedModel = resolvedActor.selection + this.#options.definitions.validateActors(actorIds, existingConversationId ?? undefined) + if (input.newParticipants) + this.#options.definitions.validateActors(input.newParticipants) + if (!this.#options.resources) + throw new BuddyServiceError('VALIDATION_FAILED') + const materialized = await this.#options.resources.resolveInput(draft.draftId, draft.content, { branchId: existingConversationId ? branchId : null, conversationId: existingConversationId, spaceId: space?.id ?? null }, selectedModel, draft.legacyResourceDraftId) + let stagedAttachments: PreparedTurnAttachments | null = null + try { + const content = buddyUserContentToText(draft.content).trim() + const resourceInputs = materialized.inputs + if ((!content && draft.content.sessionReferences?.length) || (!content && resourceInputs.length === 0 && !draft.content.quotes?.length && !draft.content.resourceQuotes?.length)) + throw new BuddyServiceError('VALIDATION_FAILED') + const attachmentIds = getResourceAttachmentIds(resourceInputs) + const userMessageId = randomUUID() + const { attachmentPrompt, prompt, reviewRequested, contextItems, selection, thinkingLevel } = await this.#options.materials.prepare({ + actorId: buddyId, + attachmentIds, + composer: { content: { ...draft.content, sessionReferences }, resourceIds: resourceInputs.map(resource => resource.resourceId), resources: resourceInputs, sourceMessageId: userMessageId, quoteReadAvailable: selectedModel.toolCall !== false && canReadReferenceSnapshots(resolvedActor.definition.settings.tools) }, + content: '', + contextItems: [], + conversationId, + draftId: draft.draftId, + legacyResourceDraftId: draft.legacyResourceDraftId, + branchId, + point: input.point, + space, + replay: null, + requestedModel: draft.modelSelection, + preparedSelection: selectedModel, + }) + stagedAttachments = combinePreparedAttachments(await this.#options.attachments.prepareMessageAttachments({ attachmentIds, conversationId, draftId: draft.draftId, legacyResourceDraftId: draft.legacyResourceDraftId, messageId: userMessageId }), materialized) + const persistedAttachmentIds = stagedAttachments.bindings.map(binding => binding.id) + const fields: PreparedInputFields = { + expectedActorModelSelection: resolvedActor.memberModelSelection, + actorSnapshot: resolvedActor.definition, + actorIds, + ...(draft.modelSelection ? { modelOverride: draft.modelSelection } : {}), + buddyId, + attachmentBindings: stagedAttachments.bindings, + createdAt: new Date().toISOString(), + draft: draft.commit, + runExecutionProfile: reviewRequested ? 'read_only' : undefined, + model: selection.modelId, + modelParameters: toModelParameters(selection), + provider: selection.providerId, + runInput: { attachmentIds: persistedAttachmentIds, contextItems, prompt, reasoning: thinkingLevel ?? null, serviceTier: selection.serviceTier }, + runId: randomUUID(), + userMessageContent: createPersistedUserMessageContent({ ...draft.content, ...(sessionReferences.length ? { sessionReferences } : { sessionReferences: undefined }) }, bindResourceAttachments(resourceInputs, persistedAttachmentIds), attachmentPrompt.materialProjection), + userMessageId, + } + return { prepared: build(fields, attachmentPrompt.records), stagedAttachments } + } + catch (error) { + await (stagedAttachments ?? materialized).rollback().catch(() => undefined) + throw error + } + } +} + +function createPersistedUserMessageContent( + userContent: BuddyUserContentV1, + resourceSnapshots: readonly BuddyUserMessageResourceSnapshot[], + materialProjection?: InputMaterialProjectionState, +) { + return buddyUserMessageContentV1Schema.parse({ + resourceSnapshots: [...resourceSnapshots], + ...(materialProjection?.deferredReferences || materialProjection?.deferredResourceIds.length ? { materialProjection } : {}), + userContent, + }) +} diff --git a/apps/buddy/service/src/chat/ChatInputValidationService.ts b/apps/buddy/service/src/chat/ChatInputValidationService.ts index 3a5e185e1..a230bddff 100644 --- a/apps/buddy/service/src/chat/ChatInputValidationService.ts +++ b/apps/buddy/service/src/chat/ChatInputValidationService.ts @@ -83,7 +83,7 @@ export class ChatInputValidationService { if (point.kind === 'empty') return { messages: [] } const cwd = this.#options.paths.conversationWorkspace(input.conversationId) - const threadId = input.threadId ?? actorThreadId(input.conversationId, input.actorId) + const threadId = input.threadId ?? actorThreadId(input.conversationId, input.actorId, input.branchId) if (point.kind === 'branch_head') { const active = this.#options.sessions.getReady(input.conversationId, input.branchId, input.actorId, input.threadId)?.getInputContext?.() const manager = await this.#options.tree.preview(input.conversationId, input.branchId, cwd, threadId) diff --git a/apps/buddy/service/src/chat/ChatQueueService.ts b/apps/buddy/service/src/chat/ChatQueueService.ts index 6ceeb64af..673888619 100644 --- a/apps/buddy/service/src/chat/ChatQueueService.ts +++ b/apps/buddy/service/src/chat/ChatQueueService.ts @@ -6,12 +6,13 @@ import type { BuddyAgentRunner } from '../agent/execution/BuddyAgentRunner' import type { BuddyTurnLauncher } from '../agent/execution/BuddyTurnLauncher' import type { BuddyStartTurnInput } from '../BuddyRuntime' import type { RunEventAvailability, RunEventObservation } from '../events/RunEventPorts' +import type { ChatControlCommandRepository } from '../storage/ChatControlCommandRepository' import type { ChatQueueRepository, QueuedTurnRequestInput } from '../storage/chatQueueRepository' import type { RunInputRepository } from '../storage/runInputRepository' import type { RunRecord } from '../storage/runRecord' import type { RunRepository } from '../storage/runRepository' import type { PrepareTurnRequestInput, TurnRequestRepository } from '../storage/turnRequestRepository' -import type { ChatTurnService } from './ChatTurnService' +import type { ChatTurnService, PreparedStartTurnInput } from './ChatTurnService' import { createHash, randomUUID } from 'node:crypto' import { isDocumentMimeType } from '../../../shared/conversation/attachmentFormats' import { Emitter } from '../../../shared/events/Emitter' @@ -21,8 +22,10 @@ import { getAttachmentLabels } from '../attachments/attachmentLabels' import { buddyExecutionFingerprint } from '../buddies/BuddyExecutionDefinition' import { RunEventCorruptionError } from '../events/RunEventFailure' import { BuddyServiceError } from '../rpc/runtimeRequest' +import { PreparedResourceSettlement } from './PreparedResourceSettlement' export interface ChatQueueServiceOptions { + control?: ChatControlCommandRepository availability?: Pick getRunPermissionCeiling: (runId: string) => BuddyExecutionProfile | undefined queue: ChatQueueRepository @@ -46,7 +49,7 @@ export class ChatQueueService { readonly #options: ChatQueueServiceOptions readonly #operations = new Map>() readonly #enqueues = new Set>() - readonly #cancellations = new Map }>() + readonly #cancellations = new Map }>() readonly #changes: Emitter readonly #stopping = new AbortController() readonly onDidChange: Emitter['event'] @@ -62,8 +65,12 @@ export class ChatQueueService { dispose() { this.#disposed = true this.#stopping.abort() - this.#options.queue.pause() - this.#changes.dispose() + try { + this.#options.queue.pause() + } + finally { + this.#changes.dispose() + } } async drain(): Promise { @@ -81,12 +88,19 @@ export class ChatQueueService { pause(scope: LocalChatQueueScope, actorId?: string): void { if (this.#disposed) return - if (actorId ? this.#options.queue.pauseActor(scope, actorId) : this.#options.queue.pause(scope.conversationId)) + if (actorId ? this.#options.queue.pauseActor(scope, actorId) : this.#options.queue.pause(scope.conversationId, scope.branchId)) this.#changed('paused', scope) } list(scope: LocalChatQueueScope) { - return this.#options.queue.list(scope) + return this.#options.queue.list(scope).map((item) => { + if (!this.#options.control) + return item + const condition = this.#options.control.queueTarget(item) + const active = this.#options.queue.activeRun(scope, item.buddyId) + const run = active ? this.#options.runs.findById(active.id) : null + return { ...item, ...(condition ? { revision: condition.expectedRevision } : {}), steerTarget: run ? { runId: run.id, generation: run.generation } : null } + }) } cancelRun(runId: string): Promise { @@ -97,10 +111,10 @@ export class ChatQueueService { if (!run) return this.#options.turns.cancel(runId) const active = this.#options.queue.activeRun(run, run.buddyId) - if (active ? active.id !== runId : this.#options.queue.latestRun(run, run.buddyId)?.id !== runId || !this.#options.runner.hasActiveExecution(run.conversationId)) + if (active ? active.id !== runId : this.#options.queue.latestRun(run, run.buddyId)?.id !== runId || !this.#options.runner.hasActiveExecution(run.conversationId, run.buddyId, run.branchId)) return this.#options.turns.cancel(runId) const cancellation = Promise.withResolvers() - this.#cancellations.set(runId, { conversationId: run.conversationId, buddyId: run.buddyId, completion: cancellation.promise }) + this.#cancellations.set(runId, { conversationId: run.conversationId, branchId: run.branchId, buddyId: run.buddyId, completion: cancellation.promise }) void this.#cancelRun(run).then(cancellation.resolve, cancellation.reject) return cancellation.promise } @@ -140,38 +154,63 @@ export class ChatQueueService { const replay = this.#options.queue.replay(input.requestId, fingerprint) if (replay) return replay - const { prepared, stagedAttachments } = await this.#options.turns.prepareStart(input) + const { prepared, stagedAttachments } = await this.prepareEnqueue(input) + const resources = new PreparedResourceSettlement(stagedAttachments, this.#options.onObserverError) let result try { - if (this.#disposed) - throw new BuddyServiceError('VALIDATION_FAILED') const concurrent = this.#options.queue.replay(input.requestId, fingerprint) if (concurrent) { - await stagedAttachments.rollback() + await resources.rollback() return concurrent } - this.#options.availability?.assertConversationWritable(prepared.conversationId) stagedAttachments.validate() - const target = this.#options.queue.validateDelivery(prepared) - if (prepared.delivery?.mode === 'current' && target) { - if (!this.#canFollowUp(target.run, prepared)) - throw new BuddyServiceError('INPUT_DELIVERY_INCOMPATIBLE') - await this.#options.turns.validatePreparedInput(prepared, false) - const current = this.#options.queue.validateDelivery(prepared)! - if (!this.#canFollowUp(current.run, prepared)) - throw new BuddyServiceError('INPUT_DELIVERY_INCOMPATIBLE') - } - result = this.#options.queue.enqueue({ ...prepared, requestFingerprint: fingerprint }) + result = this.commitEnqueue(prepared, fingerprint) + } + catch (error) { + await resources.fail(error) + throw error + } + await resources.commit() + this.publishEnqueued(prepared, result) + return result + } + + async prepareEnqueue(input: PreparedStartTurnInput) { + if (this.#disposed) + throw new BuddyServiceError('VALIDATION_FAILED') + const materialized = await this.#options.turns.prepareStart(input) + try { + this.#validateEnqueue(materialized.prepared) + if (materialized.prepared.delivery?.mode === 'current') + await this.#options.turns.validatePreparedInput(materialized.prepared, false) + this.#validateEnqueue(materialized.prepared) + return materialized } catch (error) { - await stagedAttachments.rollback() + await materialized.stagedAttachments.rollback().catch(() => undefined) throw error } + } + + commitEnqueue(prepared: PrepareTurnRequestInput, fingerprint: string) { + this.#validateEnqueue(prepared) + return this.#options.queue.enqueue({ ...prepared, requestFingerprint: fingerprint }) + } + + #validateEnqueue(prepared: PrepareTurnRequestInput) { + if (this.#disposed) + throw new BuddyServiceError('VALIDATION_FAILED') + this.#options.availability?.assertConversationWritable(prepared.conversationId) + const target = this.#options.queue.validateDelivery(prepared) + if (prepared.delivery?.mode === 'current' && target && !this.#canFollowUp(target.run, prepared)) + throw new BuddyServiceError('INPUT_DELIVERY_INCOMPATIBLE') + } + + publishEnqueued(prepared: PrepareTurnRequestInput, result: LocalChatQueueReceipt): void { const latest = this.#options.queue.latestRun(result, prepared.buddyId) if (this.#isCancelling(result, prepared.buddyId) || latest?.status === 'cancelled' || latest?.status === 'failed') this.pause(result, prepared.buddyId) - this.#changed('enqueued', result, { commitId: randomUUID(), requestId: input.requestId, queueId: result.id, draftReceipt: result.draftReceipt, ...(prepared.attachmentBindings.length ? { attachmentOwnership: { kind: 'queue', attachmentIds: prepared.attachmentBindings.map(binding => binding.id) } } : {}) }) - await stagedAttachments.commit().catch(() => undefined) + this.#changed('enqueued', result, { commitId: randomUUID(), requestId: prepared.requestId, queueId: result.id, draftReceipt: result.draftReceipt, ...(prepared.attachmentBindings.length ? { attachmentOwnership: { kind: 'queue', attachmentIds: prepared.attachmentBindings.map(binding => binding.id) } } : {}) }) if (prepared.delivery?.mode === 'current' && prepared.delivery.target) { void this.steer(result).then((status) => { if (status !== 'accepted') @@ -182,7 +221,6 @@ export class ChatQueueService { && this.#options.queue.list(result).find(item => item.buddyId === prepared.buddyId)?.id === result.id) { void this.steer(result).catch(() => this.pause(result, prepared.buddyId)) } - return result } cancel(target: LocalChatQueueTarget) { @@ -194,7 +232,70 @@ export class ChatQueueService { return cancelled } + async prepareSteer(target: LocalChatQueueTarget, runId: string) { + const pending = this.#options.queue.pending(target) + const run = this.#options.runs.findById(runId) + if (this.#disposed || !pending || !run || this.#isCancelling(target, pending.buddyId) + || this.#options.queue.activeRun(target, pending.buddyId)?.id !== runId) { + throw new BuddyServiceError('INPUT_DELIVERY_TARGET_UNAVAILABLE') + } + const promoteNext = pending.delivery?.mode === 'next' + const source = promoteNext ? { ...pending, delivery: { ...pending.delivery!, mode: 'current' as const } } : pending + const input = await this.#options.turns.refreshQueuedInput(source) + if (!this.#canFollowUp(run, input)) + throw new BuddyServiceError('INPUT_DELIVERY_INCOMPATIBLE') + await this.#options.turns.validatePreparedInput(input, false) + return { input, runId, promoteNext } + } + + commitSteer(prepared: Awaited>) { + const { input, runId, promoteNext } = prepared + 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) + || !this.#options.runner.hasActiveExecution(input.conversationId, input.buddyId, input.branchId)) { + throw new BuddyServiceError('INPUT_DELIVERY_TARGET_UNAVAILABLE') + } + this.#options.availability?.assertRunExecutable(runId) + const reference = this.#inputReference(input) + this.#options.queue.commitInRun(input, runId, this.#options.getRunPermissionCeiling(runId), promoteNext) + return () => { + this.#changed('delivered', input, { commitId: randomUUID(), requestId: input.requestId, queueId: input.queuedMessageId!, runId, messageId: input.userMessageId }) + return this.#options.runner.steer(runId, () => reference, input.runInput.contextItems.flatMap(item => item.kind === 'skill' && item.skill ? [item.skill] : [])) + } + } + + async prepareContinue(target: LocalChatQueueTarget) { + const pending = this.#options.queue.pending(target) + if (!pending || !this.#canDispatch(pending)) + throw new BuddyServiceError('INPUT_DELIVERY_TARGET_UNAVAILABLE') + const latest = this.#options.queue.latestRun(pending, pending.buddyId) + const input = await this.#options.turns.refreshQueuedInput(pending) + await this.#options.turns.validatePreparedInput(input) + return { input, sourceRunId: latest?.id ?? null, sourceGeneration: latest ? this.#options.runs.findById(latest.id)?.generation ?? null : null } + } + + commitContinue(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') + } + const request = this.#options.requests.prepare({ ...input, createdAt: new Date().toISOString() }) + return { request, notify: async () => { + this.#changed('dispatched', input) + const turn = await this.#options.launcher.launch(request.runId, this.#stopping.signal) + void turn.completion.catch(() => {}) + } } + } + + publishCancelled(target: LocalChatQueueTarget): void { + this.#changed('cancelled', target) + } + steer(target: LocalChatQueueTarget): Promise { + const condition = this.#options.control?.queueTarget(target) const active = this.#options.queue.activeRun(target, this.#options.queue.pending(target)?.buddyId) return this.#serialize(target, async () => { if (this.#isCancelling(target, this.#options.queue.pending(target)?.buddyId)) @@ -230,7 +331,7 @@ export class ChatQueueService { const current = this.#options.runs.findById(active.id) if (!current || !this.#canFollowUp(current, input)) return 'incompatible' - return this.#deliver(input, active.id, 'steer') ? 'accepted' : 'unavailable' + return this.#deliver(input, active.id, 'steer', false, condition) ? 'accepted' : 'unavailable' }, 'unavailable') } @@ -245,6 +346,7 @@ export class ChatQueueService { const next = this.#options.queue.list(run).find(item => item.buddyId === run.buddyId) if (next?.state !== 'waiting' || (next.delivery && (next.delivery.mode !== 'current' || next.delivery.target?.runId !== runId))) return false + const condition = this.#options.control?.queueTarget(next) const pending = this.#options.queue.pending(next) if (!pending) return false @@ -264,7 +366,7 @@ export class ChatQueueService { const current = this.#options.runs.findById(runId) if (!current || !this.#canFollowUp(current, input)) return false - return this.#deliver(input, runId, 'followUp') + return this.#deliver(input, runId, 'followUp', false, condition) }, false) } catch { @@ -275,6 +377,7 @@ export class ChatQueueService { } async #steerCurrent(target: LocalChatQueueTarget, pending: QueuedTurnRequestInput, promoteNext = false): Promise { + const condition = this.#options.control?.queueTarget(target) try { const source: QueuedTurnRequestInput = promoteNext ? { ...pending, delivery: { ...pending.delivery!, mode: 'current' } } : pending this.#options.queue.validateDelivery(source) @@ -290,7 +393,7 @@ export class ChatQueueService { || !this.#options.queue.pending(target) || !this.#options.queue.isCurrentInput(input)) { throw new BuddyServiceError('INPUT_DELIVERY_TARGET_UNAVAILABLE') } - if (!this.#deliver(input, execution.run.id, 'steer', promoteNext)) + if (!this.#deliver(input, execution.run.id, 'steer', promoteNext, condition)) throw new BuddyServiceError('INPUT_DELIVERY_TARGET_UNAVAILABLE') return 'accepted' } @@ -332,36 +435,40 @@ export class ChatQueueService { && run.maxTokens === (input.modelParameters?.maxTokens ?? null))) } - #deliver(input: QueuedTurnRequestInput, runId: string, mode: 'steer' | 'followUp', promoteNext = false) { + #deliver(input: QueuedTurnRequestInput, runId: string, mode: 'steer' | 'followUp', promoteNext = false, condition?: ReturnType) { this.#options.availability?.assertRunExecutable(runId) return this.#options.runner[mode](runId, () => { this.#options.availability?.assertRunExecutable(runId) - const documents = input.attachmentBindings.flatMap(binding => isDocumentMimeType(binding.mimeType) - ? [{ attachmentId: binding.id, mimeType: binding.mimeType }] - : []) - const reference = createBuddyInputReference({ - attachmentIds: [...input.runInput.attachmentIds], - resourceLabels: getAttachmentLabels(input.attachmentBindings, input.runInput.prompt), - ...(documents.length ? { documents } : {}), - messageId: input.userMessageId, - prompt: input.runInput.prompt, - images: input.attachmentBindings.flatMap((binding) => { - const metadata = binding.mimeType - return metadata?.startsWith('image/') && metadata !== 'image/svg+xml' ? [{ attachmentId: binding.id, mimeType: metadata }] : [] - }), - }) - this.#options.queue.commitInRun(input, runId, this.#options.getRunPermissionCeiling(runId), promoteNext) + const reference = this.#inputReference(input) + this.#commitQueue(condition, () => this.#options.queue.commitInRun(input, runId, this.#options.getRunPermissionCeiling(runId), promoteNext)) this.#changed('delivered', input, { commitId: randomUUID(), requestId: input.requestId, queueId: input.queuedMessageId!, runId, messageId: input.userMessageId, ...(input.attachmentBindings.length ? { attachmentOwnership: { kind: 'message', attachmentIds: input.attachmentBindings.map(binding => binding.id) } } : {}) }) return reference }, input.runInput.contextItems.flatMap(item => item.kind === 'skill' && item.skill ? [item.skill] : [])) } + #inputReference(input: QueuedTurnRequestInput) { + const documents = input.attachmentBindings.flatMap(binding => isDocumentMimeType(binding.mimeType) + ? [{ attachmentId: binding.id, mimeType: binding.mimeType }] + : []) + return createBuddyInputReference({ + attachmentIds: [...input.runInput.attachmentIds], + resourceLabels: getAttachmentLabels(input.attachmentBindings, input.runInput.prompt), + ...(documents.length ? { documents } : {}), + messageId: input.userMessageId, + prompt: input.runInput.prompt, + images: input.attachmentBindings.flatMap((binding) => { + const metadata = binding.mimeType + return metadata?.startsWith('image/') && metadata !== 'image/svg+xml' ? [{ attachmentId: binding.id, mimeType: metadata }] : [] + }), + }) + } + reconcile(scope: LocalChatQueueScope) { return this.#serialize(scope, async () => { - const current = this.#options.queue.continuationScope(scope.conversationId) + const current = this.#options.queue.continuationScope(scope) if (!current) return false - if (this.#options.runner.hasDegradedCleanup(current.conversationId)) { + if (this.#options.runner.hasDegradedCleanup(current.conversationId, current.branchId)) { this.pause(current) return false } @@ -398,6 +505,7 @@ export class ChatQueueService { } async #dispatch(target: LocalChatQueueTarget, allowPaused = false) { + const condition = this.#options.control?.queueTarget(target) const pending = this.#options.queue.pending(target) if (!pending || !this.#canDispatch(pending)) return false @@ -408,7 +516,7 @@ export class ChatQueueService { const head = this.#options.queue.list(target).find(item => item.buddyId === input.buddyId) if (!allowPaused && (head?.id !== target.id || head.state !== 'waiting')) return false - const prepared = this.#options.requests.prepare({ ...input, createdAt: new Date().toISOString() }) + const prepared = this.#commitQueue(condition, () => this.#options.requests.prepare({ ...input, createdAt: new Date().toISOString() })) this.#changed('dispatched', target) const turn = await this.#options.launcher.launch(prepared.runId, this.#stopping.signal) void turn.completion.catch(() => {}) @@ -417,6 +525,7 @@ export class ChatQueueService { #canDispatch(scope: QueuedTurnRequestInput): boolean { try { + this.#options.control?.assertAccepting() this.#options.availability?.assertConversationWritable(scope.conversationId) } catch (error) { @@ -428,11 +537,19 @@ export class ChatQueueService { && this.#options.queue.canDispatchDelivery(scope) && !this.#options.queue.activeRun(scope, scope.buddyId) && (scope.queueRouting?.mode === 'actor' || !this.#options.queue.activeRun(scope)) - && !this.#options.runner.hasActiveExecution(scope.conversationId, scope.buddyId) + && !this.#options.runner.hasActiveExecution(scope.conversationId, scope.buddyId, scope.branchId) + } + + #commitQueue(condition: ReturnType | undefined, operation: () => T): T { + if (!this.#options.control) + return operation() + if (!condition) + throw new BuddyServiceError('INPUT_DELIVERY_TARGET_UNAVAILABLE') + return this.#options.control.withQueue(condition, operation) } #isCancelling(scope: LocalChatQueueScope, actorId?: string): boolean { - return [...this.#cancellations.values()].some(cancellation => cancellation.conversationId === scope.conversationId && (!actorId || cancellation.buddyId === actorId)) + return [...this.#cancellations.values()].some(cancellation => cancellation.conversationId === scope.conversationId && cancellation.branchId === scope.branchId && (!actorId || cancellation.buddyId === actorId)) } #changed(kind: ChatQueueChange['kind'], scope: LocalChatQueueScope, committed?: ChatQueueChange['committed']): void { @@ -440,16 +557,17 @@ export class ChatQueueService { } async #serialize(scope: LocalChatQueueScope, operation: () => Promise, disposed: T): Promise { - const previous = this.#operations.get(scope.conversationId) + const key = JSON.stringify([scope.conversationId, scope.branchId]) + const previous = this.#operations.get(key) const { promise, resolve } = Promise.withResolvers() - this.#operations.set(scope.conversationId, promise) + this.#operations.set(key, promise) await previous try { return this.#disposed ? disposed : await operation() } finally { - if (this.#operations.get(scope.conversationId) === promise) - this.#operations.delete(scope.conversationId) + if (this.#operations.get(key) === promise) + this.#operations.delete(key) resolve() } } diff --git a/apps/buddy/service/src/chat/ChatTurnService.ts b/apps/buddy/service/src/chat/ChatTurnService.ts index db183f559..b4f473021 100644 --- a/apps/buddy/service/src/chat/ChatTurnService.ts +++ b/apps/buddy/service/src/chat/ChatTurnService.ts @@ -1,13 +1,5 @@ -import type { - BuddyPromptDirective, - BuddyUserContentV1, - BuddyUserMessageResourceSnapshot, -} from '../../../shared/conversation/buddyUserContent' import type { BuddyComposerDraftScope } from '../../../shared/conversation/composerDraft' -import type { InputMaterialProjectionState } from '../../../shared/conversation/inputMaterialProjection' -import type { BuddyThinkingLevel } from '../../../shared/conversation/modelSelection' import type { UserQuestionRequest } from '../../../shared/questions/userQuestionApi' -import type { SkillReference } from '../../../shared/skills/skillApi' import type { BuddyAgentRunner } from '../agent/execution/BuddyAgentRunner' import type { BuddyTurnLauncher } from '../agent/execution/BuddyTurnLauncher' import type { @@ -17,15 +9,13 @@ import type { ComposerResourceService } from '../attachments/ComposerResourceSer import type { BuddyDefinitionResolver } from '../buddies/BuddyDefinitionResolver' import type { BuddyStartTurnInput, - BuddyTurnContextItem, BuddyTurnStart, } from '../BuddyRuntime' import type { ConversationLifecycleService } from '../conversations/ConversationLifecycleService' import type { - InteractiveModelSelection, RuntimeModelProvider, } from '../providers/resolveInteractiveModelSelection' -import type { BuddyMaterializedSkill, SkillService } from '../skills/SkillService' +import type { SkillService } from '../skills/SkillService' import type { QueuedTurnRequestInput } from '../storage/chatQueueRepository' import type { ComposerDraftRepository } from '../storage/composerDraftRepository' import type { ConversationHistoryRepository } from '../storage/conversationHistoryRepository' @@ -39,61 +29,43 @@ import type { RunRecord } from '../storage/runRecord' import type { RunRepository } from '../storage/runRepository' import type { SpaceRecord, SpaceRepository } from '../storage/spaceRepository' import type { + EditTurnRequestInput, PrepareTurnRequestInput, + RegenerateTurnRequestInput, TurnRequestRecord, TurnRequestRepository, } from '../storage/turnRequestRepository' -import type { ChatInputHistoryPoint, ChatInputValidationService } from './ChatInputValidationService' -import type { PreparedTurnAttachments } from './persistPreparedTurn' -import { Buffer } from 'node:buffer' +import type { TurnReplay } from './ChatInputMaterializer' +import type { ChatInputValidationService } from './ChatInputValidationService' +import type { ChatInputSource } from './resolveChatInput' import { createHash, randomUUID } from 'node:crypto' -import { isAbsolute, join } from 'node:path' -import { readBoundedFile } from '../../../platform/filesystem/boundedFile' -import { - isBuddyReviewCommand, - isBuddyRunChatCommand, - isRetiredBuddyPromptCommand, - parseBuddyChatCommand, -} from '../../../shared/conversation/buddyChatCommands' + import { - bindResourceAttachments, - buddyUserContentToText, buddyUserMessageContentV1Schema, createBuddyUserContent, getBuddyUserContentActorReferences, - getResourceAttachmentIds, readBuddyUserMessageContent, } from '../../../shared/conversation/buddyUserContent' -import { inputMaterialInlineBytes } from '../../../shared/conversation/inputMaterialProjection' -import { isBuddyThinkingLevel } from '../../../shared/conversation/modelSelection' +import { AsyncOperationScope } from '../../../shared/lifecycle/AsyncOperationScope' import { isExecutionProfileWithin } from '../../../shared/permissions/executionProfile' import { renderUserQuestionReply, userQuestionReplyText } from '../../../shared/questions/userQuestionReply' import { canReadReferenceSnapshots } from '../conversations/referenceSnapshotToolContract' -import { resolveGrantedPath } from '../directories/resolveGrantedPath' -import { getModelFileInputMimeTypes } from '../providers/modelCapabilities' -import { resolveInteractiveModelSelection } from '../providers/resolveInteractiveModelSelection' import { BuddyServiceError } from '../rpc/runtimeRequest' import { toPublicRun } from '../runs/publicRun' -import { SkillError } from '../skills/skillFiles' -import { - formatBuddySkillPrompt, -} from '../skills/SkillService' + import { requireActiveSpace } from '../spaces/requireActiveSpace' -import { BUDDY_REVIEW_PROMPT, buildBuddyReviewPrompt, isReviewUserMessage } from './buddyReviewPrompt' +import { isReviewUserMessage } from './buddyReviewPrompt' +import { assertPromptSize, ChatInputMaterializer, normalizeThinkingLevel, toModelParameters } from './ChatInputMaterializer' +import { ChatInputPreparation } from './ChatInputPreparation' import { createConversationTitle } from './conversationTitle' -import { combinePreparedAttachments, persistPreparedTurn } from './persistPreparedTurn' +import { persistPreparedTurn } from './persistPreparedTurn' +import { resolveChatInput } from './resolveChatInput' import { resolveAuthorizedSessionReferences } from './sessionReferenceAuthorization' -const MAX_CONTEXT_FILE_BYTES = 1024 * 1024 -const MAX_MODEL_INPUT_BYTES = 4 * 1024 * 1024 -const PROMPT_SECTION_SEPARATOR = '\n\n---\n\n' +export type PreparedStartTurnInput = ChatInputSource & Omit -type ChatContextItem = BuddyTurnContextItem - -export interface EditChatUserMessageInput { +export type EditChatUserMessageInput = ChatInputSource & { conversationId: string - draftId: string - expectedRevision: number requestId: string userMessageId: string } @@ -127,47 +99,21 @@ export interface ChatTurnServiceOptions { turnRequests: TurnRequestRepository } -interface TurnReplay { - request: TurnRequestRecord - run: RunRecord -} - -interface TurnModelSelection extends Omit { - api: string - contextWindow: number | null - input: Array<'text' | 'image'> - fileInputMimeTypes: ReturnType - maxTokens: number | null - reasoning: string | null -} - -interface PrepareTurnMaterializationInput { - actorId: string - composer?: { content: BuddyUserContentV1, resourceIds: readonly string[], resources?: readonly BuddyUserMessageResourceSnapshot[], sourceMessageId?: string, quoteReadAvailable?: boolean } - attachmentIds: readonly string[] - content: string - contextItems: readonly ChatContextItem[] - conversationId: string - branchId: string - point?: ChatInputHistoryPoint - draftId: string - space: SpaceRecord | null - replay: TurnReplay | null - requestedModel: InteractiveModelSelection | null - preparedSelection?: TurnModelSelection -} - export class ChatTurnService { readonly #options: ChatTurnServiceOptions readonly #stopping = new AbortController() - readonly #pending = new Set>() + readonly #operations = new AsyncOperationScope(() => this.#stopping.signal.reason) + readonly #materials: ChatInputMaterializer + readonly #preparation: ChatInputPreparation constructor(options: ChatTurnServiceOptions) { this.#options = options + this.#materials = new ChatInputMaterializer({ attachments: options.attachments, definitions: options.definitions, inputValidation: options.inputValidation, providers: options.providers, runInputs: options.runInputs, skills: options.skills }) + this.#preparation = new ChatInputPreparation({ definitions: options.definitions, resources: options.composerResources, attachments: options.attachments, materials: this.#materials }) } get hasPendingOperations(): boolean { - return this.#pending.size > 0 + return this.#operations.hasPending } start(input: BuddyStartTurnInput): Promise { @@ -201,8 +147,6 @@ export class ChatTurnService { if (previous) return previous.runId const conversation = this.#requireActiveConversation(request.conversationId) - if (conversation.activeBranchId !== request.branchId) - throw new BuddyServiceError('VALIDATION_FAILED') const resolved = await this.#options.definitions.resolve({ buddyId: owner.buddyId, kind: 'actor', conversationId: conversation.id, requestedModel: null }) const prompt = `The user answered an earlier question. Continue the original task using this answer. If the source was an NPC, resume that work only when needed. Existing permissions still apply.\n${renderUserQuestionReply(request)}` const selection = resolved.selection @@ -242,15 +186,12 @@ export class ChatTurnService { return prepared.runId } - async prepareStart(input: BuddyStartTurnInput) { + async prepareStart(input: PreparedStartTurnInput) { this.#stopping.signal.throwIfAborted() - const draft = this.#options.drafts.findById(input.draftId) - if (!draft || draft.revision !== input.expectedRevision) - throw new BuddyServiceError('DRAFT_CONFLICT') + const draft = resolveChatInput(input, this.#options.drafts) if (draft.scope.kind === 'message_edit') throw new BuddyServiceError('VALIDATION_FAILED') const followupScope = draft.scope.kind === 'message_followup' ? draft.scope : null - const content = buddyUserContentToText(draft.content).trim() const scope = resolveDraftScope(draft.scope) if (scope.conversationId && this.#options.conversationLifecycle.isDeleting(scope.conversationId)) throw new BuddyServiceError('VALIDATION_FAILED') @@ -288,8 +229,6 @@ export class ChatTurnService { ?? existingConversation?.activeBranchId ?? randomUUID() const branchId = followupScope ? randomUUID() : parentBranchId - if (existingConversation && !followupScope && existingConversation.activeBranchId !== branchId) - throw new BuddyServiceError('VALIDATION_FAILED') let followup: { parentBranchId: string, sourceMessageId: string, sourceRunId: string } | undefined if (followupScope) { const history = this.#options.conversations.listBranchMessages(conversationId, followupScope.branchId) @@ -304,114 +243,26 @@ export class ChatTurnService { if (this.#options.conversationLifecycle.isDeleting(conversationId)) throw new BuddyServiceError('VALIDATION_FAILED') - const resolvedActor = await this.#options.definitions.resolve({ buddyId, kind: 'actor', conversationId: existingConversation?.id ?? null, requestedModel: draft.modelSelection }) - const selectedModel = resolvedActor.selection - this.#options.definitions.validateActors(actorIds, existingConversation?.id) - if (!existingConversation) - this.#options.definitions.validateActors(draft.participants?.actorIds ?? [leaderId]) - const materialized = await requireValue(this.#options.composerResources ?? null) - .resolveInput(input.draftId, draft.content, { - branchId: existingConversation ? parentBranchId : null, - conversationId: existingConversation?.id ?? null, - spaceId: space?.id ?? null, - }, selectedModel) - let stagedAttachments: PreparedTurnAttachments | null = null - try { - const resourceInputs = materialized.inputs - if ((!content && draft.content.sessionReferences?.length) || (!content && resourceInputs.length === 0 && !draft.content.quotes?.length && !draft.content.resourceQuotes?.length)) - throw new BuddyServiceError('VALIDATION_FAILED') - const attachmentIds = getResourceAttachmentIds(resourceInputs) - const userMessageId = randomUUID() - - const { - attachmentPrompt, - prompt, - reviewRequested, - contextItems: resolvedContextItems, - selection, - thinkingLevel, - } = await this.#prepareTurnMaterialization({ - actorId: buddyId, - attachmentIds, - composer: { - sourceMessageId: userMessageId, - quoteReadAvailable: selectedModel.toolCall !== false && canReadReferenceSnapshots(resolvedActor.definition.settings.tools), - content: { ...draft.content, sessionReferences }, - resourceIds: resourceInputs.map(resource => resource.resourceId), - resources: resourceInputs, - }, - content: '', - contextItems: [], - conversationId, - draftId: input.draftId, - branchId: parentBranchId, - point: !existingConversation ? { kind: 'empty' } : followup ? { kind: 'after_run', runId: followup.sourceRunId, messageId: followup.sourceMessageId } : undefined, - space, - replay: null, - requestedModel: draft.modelSelection, - preparedSelection: selectedModel, - }) - const runId = randomUUID() - stagedAttachments = combinePreparedAttachments(await this.#options.attachments.prepareMessageAttachments({ - attachmentIds, - conversationId, - draftId: input.draftId, - messageId: userMessageId, - }), materialized) - const persistedAttachmentIds = stagedAttachments.bindings.map(binding => binding.id) - const prepared: PrepareTurnRequestInput = { - ...(input.delivery ? { delivery: input.delivery } : {}), - leaderId, - expectedActorModelSelection: resolvedActor.memberModelSelection, - actorSnapshot: resolvedActor.definition, - actorIds, - ...(draft.modelSelection ? { modelOverride: draft.modelSelection } : {}), - buddyId, - followup, - approvalPolicy: draft.executionConfig.approvalPolicy, - attachmentBindings: stagedAttachments.bindings, - branchId, - conversationId, - createdAt: new Date().toISOString(), - draft: { - draftId: input.draftId, - expectedRevision: input.expectedRevision, - }, - executionProfile: draft.executionConfig.executionProfile, - runExecutionProfile: reviewRequested ? 'read_only' : undefined, - model: selection.modelId, - modelParameters: toModelParameters(selection), - spaceId: space?.id ?? null, - provider: selection.providerId, - requestFingerprint: createStartTurnFingerprint(input), - requestId: input.requestId, - runInput: { - attachmentIds: persistedAttachmentIds, - contextItems: resolvedContextItems, - prompt, - reasoning: thinkingLevel ?? null, - serviceTier: selection.serviceTier, - }, - runId, - title: createConversationTitle(draft.content, attachmentPrompt.records), - userMessageContent: createPersistedUserMessageContent( - { ...draft.content, ...(sessionReferences.length ? { sessionReferences } : { sessionReferences: undefined }) }, - bindResourceAttachments(resourceInputs, persistedAttachmentIds), - attachmentPrompt.materialProjection, - ), - userMessageId, - } - return { prepared, stagedAttachments } - } - catch (error) { - await (stagedAttachments ?? materialized).rollback() - throw error - } + return this.#preparation.prepare({ draft, buddyId, actorIds, newParticipants: !existingConversation ? draft.participants?.actorIds ?? [leaderId] : undefined, conversationId, existingConversationId: existingConversation?.id ?? null, branchId: parentBranchId, space, sessionReferences, point: !existingConversation ? { kind: 'empty' } : followup ? { kind: 'after_run', runId: followup.sourceRunId, messageId: followup.sourceMessageId } : undefined }, (fields, records): PrepareTurnRequestInput => ({ + ...fields, + ...(input.delivery ? { delivery: input.delivery } : {}), + leaderId, + followup, + approvalPolicy: draft.executionConfig.approvalPolicy, + branchId, + conversationId, + participants: draft.participants, + executionProfile: draft.executionConfig.executionProfile, + spaceId: space?.id ?? null, + requestFingerprint: createStartTurnFingerprint(input), + requestId: input.requestId, + title: createConversationTitle(draft.content, records), + })) } async refreshQueuedInput(input: QueuedTurnRequestInput): Promise { const conversation = this.#options.conversations.findById(input.conversationId) - if (!conversation || conversation.activeBranchId !== input.branchId || this.#options.conversationLifecycle.isDeleting(input.conversationId)) + if (!conversation || conversation.deletedAt || this.#options.conversationLifecycle.isDeleting(input.conversationId)) throw new BuddyServiceError('VALIDATION_FAILED') const buddyId = input.delivery?.actorId ?? (input.queueRouting?.mode === 'actor' ? input.queueRouting.actorId : conversation.buddyId) if (buddyId !== input.buddyId) @@ -438,7 +289,7 @@ export class ChatTurnService { async validatePreparedInput(input: PrepareTurnRequestInput, validateSkills = true): Promise { this.#options.definitions.validateActors([input.buddyId ?? this.#options.getDefaultBuddyId(), ...input.actorIds ?? []]) if (validateSkills) - input.runInput.contextItems = await this.#validateSkillItems(input.spaceId, input.runInput.contextItems) + input.runInput.contextItems = await this.#materials.validateSkills(input.spaceId, input.runInput.contextItems) await this.#options.inputValidation.validate({ actorId: input.buddyId ?? this.#options.getDefaultBuddyId(), conversationId: input.conversationId, @@ -448,7 +299,7 @@ export class ChatTurnService { contextWindow: input.modelParameters?.contextWindow ?? null, maxTokens: input.modelParameters?.maxTokens ?? null, prompt: input.runInput.prompt, - attachments: this.#options.attachments.getInputMetadata(input.runInput.attachmentIds, input.conversationId, input.queuedMessageId ?? input.draft.draftId), + attachments: this.#options.attachments.getInputMetadata(input.runInput.attachmentIds, input.conversationId, input.queuedMessageId ?? input.draft?.draftId ?? null), }) } @@ -457,16 +308,24 @@ export class ChatTurnService { } async #editUserMessage(input: EditChatUserMessageInput) { - const replay = this.#findReplay( - input.requestId, - createEditUserMessageFingerprint(input), - input.conversationId, - ) - if (replay && !isInterruptedRun(replay.run)) + const replay = this.#findReplay(input.requestId, createEditUserMessageFingerprint(input), input.conversationId) + if (replay) return this.#toTurnStart(replay.request, replay.run) + const { prepared, stagedAttachments } = await this.prepareEdit(input) + const request = await persistPreparedTurn(stagedAttachments, () => { + this.#stopping.signal.throwIfAborted() + return this.#options.turnRequests.edit(prepared) + }) + return this.#launchPreparedTurn(request) + } + async prepareEdit(input: EditChatUserMessageInput) { + this.#stopping.signal.throwIfAborted() const conversation = this.#requireActiveConversation(input.conversationId) - const parentBranchId = requireValue(conversation.activeBranchId) + const draft = resolveChatInput(input, this.#options.drafts) + if (draft.scope.kind !== 'message_edit') + throw new BuddyServiceError('VALIDATION_FAILED') + const parentBranchId = draft.scope.branchId const history = this.#options.conversations.listBranchMessages( conversation.id, parentBranchId, @@ -475,141 +334,35 @@ export class ChatTurnService { const sourceMessage = sourceIndex >= 0 ? history[sourceIndex] : null if (sourceMessage?.role !== 'user') throw new BuddyServiceError('VALIDATION_FAILED') - const draft = replay ? null : this.#options.drafts.findById(input.draftId) - if (!replay && (!draft || draft.revision !== input.expectedRevision)) - throw new BuddyServiceError('DRAFT_CONFLICT') - if (draft && ( + if (( draft.scope.kind !== 'message_edit' || draft.scope.conversationId !== conversation.id || draft.scope.branchId !== parentBranchId || draft.scope.userMessageId !== input.userMessageId - || draft!.executionConfig.approvalPolicy !== conversation.approvalPolicy - || draft!.executionConfig.executionProfile !== conversation.executionProfile + || draft.executionConfig.approvalPolicy !== conversation.approvalPolicy + || draft.executionConfig.executionProfile !== conversation.executionProfile )) { throw new BuddyServiceError('VALIDATION_FAILED') } const forkedFromMessageId = sourceIndex > 0 ? history[sourceIndex - 1]?.id ?? null : null const space = this.#resolveConversationSpace(conversation) - const sessionReferences = draft - ? resolveAuthorizedSessionReferences(draft.content.sessionReferences ?? [], this.#options.conversations) - : [] - const content = draft ? buddyUserContentToText(draft.content).trim() : '' - const actorIds = draft ? [...new Set(getBuddyUserContentActorReferences(draft.content).map(reference => reference.actorId))] : [] + const sessionReferences = resolveAuthorizedSessionReferences(draft.content.sessionReferences ?? [], this.#options.conversations) + const actorIds = [...new Set(getBuddyUserContentActorReferences(draft.content).map(reference => reference.actorId))] const buddyId = actorIds.length === 1 ? actorIds[0]! : conversation.buddyId - const resolvedActor = draft && !replay ? await this.#options.definitions.resolve({ buddyId, kind: 'actor', conversationId: conversation.id, requestedModel: draft.modelSelection }) : undefined - const selectedModel = resolvedActor?.selection - this.#options.definitions.validateActors(actorIds, conversation.id) - const materialized = draft && !replay - ? await requireValue(this.#options.composerResources ?? null).resolveInput( - draft.draftId, - draft.content, - { branchId: parentBranchId, conversationId: conversation.id, spaceId: space?.id ?? null }, - selectedModel, - ) - : null - let stagedAttachments: PreparedTurnAttachments | null = null - let persistenceOwnsAttachments = false - let prepared: TurnRequestRecord - try { - const resourceInputs = materialized?.inputs ?? [] - if (!replay && ((!content && draft?.content.sessionReferences?.length) || (!content && resourceInputs.length === 0 && !draft?.content.quotes?.length && !draft?.content.resourceQuotes?.length))) - throw new BuddyServiceError('VALIDATION_FAILED') - const attachmentIds = getResourceAttachmentIds(resourceInputs) - const userMessageId = randomUUID() - const { - attachmentPrompt, - prompt, - replayInput, - reviewRequested, - contextItems: resolvedContextItems, - selection, - thinkingLevel, - } = await this.#prepareTurnMaterialization({ - actorId: buddyId, - attachmentIds, - composer: draft - ? { content: { ...draft.content, sessionReferences }, resourceIds: resourceInputs.map(resource => resource.resourceId), resources: resourceInputs, sourceMessageId: userMessageId, quoteReadAvailable: selectedModel?.toolCall !== false && !!resolvedActor && canReadReferenceSnapshots(resolvedActor.definition.settings.tools) } - : undefined, - content: '', - contextItems: [], - conversationId: conversation.id, - draftId: input.draftId, - branchId: parentBranchId, - point: { kind: 'before_message', messageId: input.userMessageId }, - space, - replay, - requestedModel: draft?.modelSelection ?? null, - preparedSelection: selectedModel, - }) - const runId = randomUUID() - stagedAttachments = replay - ? null - : combinePreparedAttachments(await this.#options.attachments.prepareMessageAttachments({ - attachmentIds, - conversationId: conversation.id, - draftId: input.draftId, - messageId: userMessageId, - }), requireValue(materialized)) - const persistedAttachmentIds = replayInput?.attachmentIds - ?? stagedAttachments?.bindings.map(binding => binding.id) - ?? [] - const persistedResourceSnapshots = replay ? [] : bindResourceAttachments(resourceInputs, persistedAttachmentIds) - persistenceOwnsAttachments = true - prepared = await persistPreparedTurn(stagedAttachments, () => { - this.#stopping.signal.throwIfAborted() - return replay - ? this.#options.turnRequests.retryInterrupted({ - createdAt: new Date().toISOString(), - requestId: input.requestId, - runId, - }) - : this.#options.turnRequests.edit({ - expectedActorModelSelection: resolvedActor?.memberModelSelection, - actorSnapshot: resolvedActor?.definition, - actorIds, - ...(draft?.modelSelection ? { modelOverride: draft.modelSelection } : {}), - buddyId, - approvalPolicy: conversation.approvalPolicy, - attachmentBindings: stagedAttachments?.bindings ?? [], - branchId: randomUUID(), - conversationId: conversation.id, - createdAt: new Date().toISOString(), - draft: { draftId: input.draftId, expectedRevision: input.expectedRevision }, - executionProfile: conversation.executionProfile, - runExecutionProfile: reviewRequested ? 'read_only' : undefined, - forkedFromMessageId, - model: selection.modelId, - modelParameters: toModelParameters(selection), - parentBranchId, - spaceId: space?.id ?? null, - provider: selection.providerId, - requestFingerprint: createEditUserMessageFingerprint(input), - requestId: input.requestId, - runId, - runInput: { - attachmentIds: persistedAttachmentIds, - contextItems: resolvedContextItems, - prompt, - reasoning: thinkingLevel ?? null, - serviceTier: replayInput ? replayInput.serviceTier : selection.serviceTier, - }, - sourceUserMessageId: input.userMessageId, - title: null, - userMessageContent: createPersistedUserMessageContent( - { ...draft!.content, ...(sessionReferences.length ? { sessionReferences } : { sessionReferences: undefined }) }, - persistedResourceSnapshots, - attachmentPrompt.materialProjection, - ), - userMessageId, - }) - }) - } - catch (error) { - if (!persistenceOwnsAttachments) - await (stagedAttachments ?? materialized)?.rollback() - throw error - } - return this.#launchPreparedTurn(prepared) + return this.#preparation.prepare({ draft, buddyId, actorIds, conversationId: conversation.id, existingConversationId: conversation.id, branchId: parentBranchId, space, sessionReferences, point: { kind: 'before_message', messageId: input.userMessageId } }, (fields): EditTurnRequestInput => ({ + ...fields, + approvalPolicy: conversation.approvalPolicy, + branchId: randomUUID(), + conversationId: conversation.id, + executionProfile: conversation.executionProfile, + forkedFromMessageId, + parentBranchId, + spaceId: space?.id ?? null, + requestFingerprint: createEditUserMessageFingerprint(input), + requestId: input.requestId, + sourceUserMessageId: input.userMessageId, + title: null, + })) } regenerateAssistant(input: RegenerateChatAssistantInput) { @@ -617,14 +370,16 @@ export class ChatTurnService { } async #regenerateAssistant(input: RegenerateChatAssistantInput) { - const replay = this.#findReplay( - input.requestId, - createRegenerationFingerprint(input), - input.conversationId, - ) - if (replay && !isInterruptedRun(replay.run)) + const replay = this.#findReplay(input.requestId, createRegenerationFingerprint(input), input.conversationId) + if (replay) return this.#toTurnStart(replay.request, replay.run) + const prepared = await this.prepareRegenerate(input) + const request = this.#options.turnRequests.regenerate(prepared) + return this.#launchPreparedTurn(request) + } + async prepareRegenerate(input: RegenerateChatAssistantInput): Promise { + this.#stopping.signal.throwIfAborted() const conversation = this.#requireActiveConversation(input.conversationId) const sourceRun = this.#options.runs.findById(input.sourceRunId) if (!sourceRun || sourceRun.conversationId !== conversation.id || sourceRun.purpose === 'conversation.compaction') @@ -635,16 +390,16 @@ export class ChatTurnService { if (triggerIndex < 0) throw new BuddyServiceError('VALIDATION_FAILED') - const storedInput = this.#requireRunInput(requireValue(replay?.run ?? sourceRun).id) + const storedInput = this.#requireRunInput(sourceRun.id) const sourceContent = buddyUserMessageContentV1Schema.safeParse(history[triggerIndex]!.content) const actorIds = sourceContent.success ? [...new Set(getBuddyUserContentActorReferences(sourceContent.data.userContent).map(reference => reference.actorId))] : [] const buddyId = actorIds.length === 1 ? actorIds[0]! : conversation.buddyId - const resolvedActor = !replay ? await this.#options.definitions.resolve({ buddyId, kind: 'actor', conversationId: conversation.id }) : undefined - const selectedModel = resolvedActor?.selection ?? await this.#resolveSelection(replay?.run ?? null, replay ? storedInput : null, conversation.modelSelection) - await this.#validateSkillItems(conversation.spaceId, storedInput.contextItems) + const resolvedActor = await this.#options.definitions.resolve({ buddyId, kind: 'actor', conversationId: conversation.id }) + const selectedModel = resolvedActor.selection + await this.#materials.validateSkills(conversation.spaceId, storedInput.contextItems) assertPromptSize(storedInput.prompt) await this.#options.inputValidation.validate({ - actorId: replay?.run.buddyId ?? buddyId, + actorId: buddyId, conversationId: conversation.id, branchId: parentBranchId, point: { kind: 'before_message', messageId: sourceRun.triggeringMessageId }, @@ -657,37 +412,37 @@ export class ChatTurnService { }) const runId = randomUUID() this.#stopping.signal.throwIfAborted() - const prepared = replay - ? this.#options.turnRequests.retryInterrupted({ - createdAt: new Date().toISOString(), - requestId: input.requestId, - runId, - }) - : this.#options.turnRequests.regenerate({ - actorIds, - expectedActorModelSelection: resolvedActor?.memberModelSelection, - actorSnapshot: resolvedActor?.definition, - buddyId, - approvalPolicy: conversation.approvalPolicy, - branchId: randomUUID(), - conversationId: conversation.id, - createdAt: new Date().toISOString(), - executionProfile: conversation.executionProfile, - model: selectedModel.modelId, - provider: selectedModel.providerId, - modelParameters: toModelParameters(selectedModel), - reasoning: normalizeThinkingLevel(selectedModel.reasoning) ?? null, - serviceTier: selectedModel.serviceTier, - expectedModelSelection: conversation.modelSelection, - runExecutionProfile: isReviewUserMessage(history[triggerIndex]!.content) ? 'read_only' : undefined, - forkedFromMessageId: requireValue(sourceRun).triggeringMessageId, - parentBranchId, - requestFingerprint: createRegenerationFingerprint(input), - requestId: input.requestId, - runId, - sourceRunId: requireValue(sourceRun).id, - }) - return this.#launchPreparedTurn(prepared) + return { + actorIds, + expectedActorModelSelection: resolvedActor?.memberModelSelection, + actorSnapshot: resolvedActor?.definition, + buddyId, + approvalPolicy: conversation.approvalPolicy, + branchId: randomUUID(), + conversationId: conversation.id, + createdAt: new Date().toISOString(), + executionProfile: conversation.executionProfile, + model: selectedModel.modelId, + provider: selectedModel.providerId, + modelParameters: toModelParameters(selectedModel), + reasoning: normalizeThinkingLevel(selectedModel.reasoning) ?? null, + serviceTier: selectedModel.serviceTier, + expectedModelSelection: conversation.modelSelection, + runExecutionProfile: isReviewUserMessage(history[triggerIndex]!.content) ? 'read_only' : undefined, + forkedFromMessageId: requireValue(sourceRun).triggeringMessageId, + parentBranchId, + requestFingerprint: createRegenerationFingerprint(input), + requestId: input.requestId, + runId, + sourceRunId: requireValue(sourceRun).id, + } + } + + launchAccepted(runId: string): Promise { + return this.#run(async () => { + const turn = await this.#options.turnLauncher.launch(runId, this.#stopping.signal) + void turn.completion.catch(() => {}) + }) } async cancel(runId: string) { @@ -721,23 +476,13 @@ export class ChatTurnService { } async dispose(): Promise { + this.#operations.stopAccepting() this.#stopping.abort() - await Promise.allSettled(this.#pending) + await this.#operations.drain() } #run(operation: () => Promise): Promise { - if (this.#stopping.signal.aborted) - return Promise.reject(this.#stopping.signal.reason) - const result = Promise.withResolvers() - this.#pending.add(result.promise) - void operation().then((value) => { - this.#pending.delete(result.promise) - result.resolve(value) - }, (error) => { - this.#pending.delete(result.promise) - result.reject(error) - }) - return result.promise + return this.#operations.track(operation) } #publicRun(run: RunRecord) { @@ -759,113 +504,12 @@ export class ChatTurnService { return requireValue(this.#options.runInputs.findByRunId(runId)) } - async #prepareTurnMaterialization(input: PrepareTurnMaterializationInput) { - const replayInput = input.replay ? this.#requireRunInput(input.replay.run.id) : null - const selection = input.preparedSelection ?? await this.#resolveSelection( - input.replay?.run ?? null, - replayInput, - input.requestedModel, - ) - const directives = !replayInput && input.composer - ? await materializeComposerDirectives(input.composer.content, input.space, this.#options.skills) - : null - const composer = directives && input.composer - ? { ...input.composer, inlineBytes: inputMaterialInlineBytes(selection), resolveDirective: directives.resolveDirective } - : undefined - const legacyContext = !replayInput && !composer - ? await materializeContextItems(input.contextItems, input.space, this.#options.skills) - : null - const contextItems = replayInput?.contextItems ?? directives?.contextItems ?? legacyContext?.contextItems ?? input.contextItems - if (replayInput) - await this.#validateSkillItems(input.space?.id ?? null, contextItems) - const attachmentPrompt = replayInput - ? { - prompt: '', - records: this.#options.attachments.getInputMetadata(replayInput.attachmentIds, input.conversationId), - materialProjection: undefined, - } - : await this.#options.attachments.preparePrompt( - input.attachmentIds, - input.content, - input.conversationId, - input.draftId, - composer, - ) - const context = replayInput - ? '' - : [ - legacyContext?.prompt ?? '', - directives?.contextSuffix ?? '', - input.composer && this.#options.definitions ? formatActorReferences(this.#options.definitions.validateActors(getBuddyUserContentActorReferences(input.composer.content).map(reference => reference.actorId))) : '', - ].filter(Boolean).join(PROMPT_SECTION_SEPARATOR) - const prompt = replayInput?.prompt - ?? [attachmentPrompt.prompt, context].filter(Boolean).join(PROMPT_SECTION_SEPARATOR) - assertPromptSize(prompt) - await this.#options.inputValidation.validate({ - actorId: input.replay?.run.buddyId ?? input.actorId, - conversationId: input.conversationId, - branchId: input.branchId, - point: input.point, - modelId: selection.modelId, - providerId: selection.providerId, - contextWindow: selection.contextWindow, - maxTokens: selection.maxTokens, - prompt, - attachments: attachmentPrompt.records, - }) - const thinkingLevel = normalizeThinkingLevel( - replayInput ? replayInput.reasoning : selection.reasoning, - ) - return { - attachmentPrompt, - prompt, - contextItems, - replayInput, - reviewRequested: directives?.reviewRequested ?? false, - selection, - thinkingLevel, - } - } - - async #validateSkillItems(spaceId: string | null, items: readonly RunInputRecord['contextItems'][number][]) { - const selections = items.filter(item => item.kind === 'skill').map(item => item.skill ?? item.value) - const loaded = await this.#options.skills.materializeForSpace(spaceId, selections) - return items.map(item => item.kind === 'skill' ? { ...item, skill: resolveMaterializedSkill(loaded, item.skill ?? item.value).reference } : item) - } - #resolveConversationSpace(conversation: ConversationRecord): SpaceRecord | null { return conversation.spaceId ? requireActiveSpace(this.#options.spaces.findById(conversation.spaceId)) : null } - async #resolveSelection( - replayRun: RunRecord | null, - replayInput: RunInputRecord | null, - requested: InteractiveModelSelection | null, - ): Promise { - if (replayRun) { - const model = await this.#options.providers.executionModels.resolveAvailable({ - contextWindow: replayRun.contextWindow, - maxTokens: replayRun.maxTokens, - modelId: replayRun.model, - providerId: replayRun.provider, - }) - return { - api: model.api, - contextWindow: replayRun.contextWindow, - input: model.input, - fileInputMimeTypes: getModelFileInputMimeTypes(model), - maxTokens: replayRun.maxTokens, - modelId: replayRun.model, - providerId: replayRun.provider, - reasoning: replayInput?.reasoning ?? null, - serviceTier: replayInput?.serviceTier ?? null, - } - } - return resolveInteractiveModelSelection(this.#options.providers, requested) - } - #toTurnStart(request: TurnRequestRecord, run: RunRecord): BuddyTurnStart { return { branchId: request.branchId, @@ -877,39 +521,7 @@ export class ChatTurnService { } } -async function materializeComposerDirectives( - content: BuddyUserContentV1, - space: SpaceRecord | null, - skills: Pick, -) { - const directives = content.body.flatMap(paragraph => paragraph.content.filter(node => node.type === 'prompt_directive')) - if (directives.some(node => node.directive === 'slash_command' && node.commandId)) - throw new BuddyServiceError('VALIDATION_FAILED') - const textCommand = parseBuddyChatCommand(buddyUserContentToText(content)) - if (textCommand && isBuddyRunChatCommand(textCommand.name)) - throw new BuddyServiceError('VALIDATION_FAILED') - const hasReviewDirective = directives.some(node => node.directive === 'slash_command' && isBuddyReviewCommand(node.value)) - const reviewsAsText = textCommand?.name === 'review' && !hasReviewDirective - const selections = directives.flatMap(node => node.directive === 'skill' ? [node.skill ?? node.value] : []) - const loaded = await skills.materializeForSpace(space?.id ?? null, selections) - return { - reviewRequested: hasReviewDirective || reviewsAsText, - contextSuffix: reviewsAsText ? BUDDY_REVIEW_PROMPT : '', - contextItems: loaded.map(skill => ({ kind: 'skill' as const, value: skill.name, skill: skill.reference })), - resolveDirective(directive: BuddyPromptDirective): string { - if (directive.directive === 'skill') { - return formatBuddySkillPrompt(resolveMaterializedSkill(loaded, directive.skill ?? directive.value)) - } - if (isRetiredBuddyPromptCommand(directive.value)) - return directive.value - if (isBuddyReviewCommand(directive.value) && directive.commandMode === 'prompt') - return buildBuddyReviewPrompt(parseBuddyChatCommand(directive.value)!.arguments) - throw new BuddyServiceError('VALIDATION_FAILED') - }, - } -} - -function createStartTurnFingerprint(input: BuddyStartTurnInput): string { +function createStartTurnFingerprint(input: PreparedStartTurnInput): string { return fingerprint({ ...input, requestId: undefined }) } @@ -944,18 +556,6 @@ function createEditUserMessageFingerprint(input: EditChatUserMessageInput): stri }) } -function createPersistedUserMessageContent( - userContent: BuddyUserContentV1, - resourceSnapshots: readonly BuddyUserMessageResourceSnapshot[], - materialProjection?: InputMaterialProjectionState, -) { - return buddyUserMessageContentV1Schema.parse({ - resourceSnapshots: [...resourceSnapshots], - ...(materialProjection?.deferredReferences || materialProjection?.deferredResourceIds.length ? { materialProjection } : {}), - userContent, - }) -} - function createRegenerationFingerprint(input: RegenerateChatAssistantInput): string { return fingerprint({ conversationId: input.conversationId, @@ -981,99 +581,8 @@ function stableSerialize(value: unknown): string { return JSON.stringify(value) ?? 'null' } -async function materializeContextItems( - items: readonly ChatContextItem[], - space: SpaceRecord | null, - skills: Pick, -) { - const selectedSkills = await skills.materializeForSpace( - space?.id ?? null, - items.filter(item => item.kind === 'skill').map(item => item.skill ?? item.value), - ) - const sections: string[] = [] - for (const item of items) { - if (item.kind === 'skill') { - const skill = resolveMaterializedSkill(selectedSkills, item.skill ?? item.value) - sections.push(formatBuddySkillPrompt(skill)) - continue - } - if (item.kind === 'slashCommand') - continue - if (!space) - throw new BuddyServiceError('DIRECTORY_NOT_AUTHORIZED') - const primaryDirectory = space.primaryDirectory - const directories = [ - ...(primaryDirectory ? [primaryDirectory] : []), - ...space.additionalDirectories, - ] - const requestedPath = isAbsolute(item.value) - ? item.value - : primaryDirectory ? join(primaryDirectory.canonicalRoot, item.value) : null - if (!requestedPath) - throw new BuddyServiceError('DIRECTORY_NOT_AUTHORIZED') - const resolution = await resolveGrantedPath(directories.map(directory => ({ - canonicalRoot: directory.canonicalRoot, - grantId: directory.id, - kind: 'workspace' as const, - root: directory.root, - })), requestedPath, 'existing') - const directory = directories.find(item => item.id === resolution.grantId) - if (!directory) - throw new BuddyServiceError('DIRECTORY_NOT_AUTHORIZED') - const content = await readBoundedFile(directory.canonicalRoot, resolution.canonicalPath) - if (content.byteLength > MAX_CONTEXT_FILE_BYTES) - throw new BuddyServiceError('VALIDATION_FAILED') - sections.push(`上下文文件:${item.value}\n\n${content.toString('utf8')}`) - } - return { - prompt: sections.join(PROMPT_SECTION_SEPARATOR), - contextItems: items.map(item => item.kind === 'skill' ? { ...item, skill: resolveMaterializedSkill(selectedSkills, item.skill ?? item.value).reference } : item), - } -} - -function resolveMaterializedSkill(skills: readonly BuddyMaterializedSkill[], selection: string | SkillReference): BuddyMaterializedSkill { - const matches = skills.filter(skill => typeof selection === 'string' ? skill.name === selection : skill.reference.id === selection.id) - if (matches.length > 1) - throw new SkillError('SKILL_NAME_COLLISION') - if (!matches[0]) - throw new SkillError('SKILL_NOT_FOUND') - return matches[0] -} - -function normalizeThinkingLevel(value: string | null | undefined): BuddyThinkingLevel | undefined { - if (!value) - return undefined - if (!isBuddyThinkingLevel(value)) - throw new BuddyServiceError('VALIDATION_FAILED') - return value -} - -function assertPromptSize(prompt: string): void { - if (Buffer.byteLength(prompt) > MAX_MODEL_INPUT_BYTES) - throw new BuddyServiceError('VALIDATION_FAILED') -} - -function toModelParameters(selection: TurnModelSelection) { - return selection.contextWindow !== null && selection.maxTokens !== null - ? { contextWindow: selection.contextWindow, maxTokens: selection.maxTokens } - : undefined -} - -function isInterruptedRun(run: RunRecord): boolean { - return run.status === 'failed' && run.errorCode === 'RUNTIME_RESTARTED' -} - function requireValue(value: T | null): T { if (value === null) throw new BuddyServiceError('VALIDATION_FAILED') return value } - -function formatActorReferences(actors: readonly { id: string, name: string }[]): string { - if (!actors.length) - return '' - const routing = actors.length === 1 - ? 'This request is addressed directly to the named actor. This does not change the conversation leader.' - : 'The conversation leader coordinates these explicitly requested actors. Delegate relevant tasks only to these named actors; bound NPCs may help as private workers.' - return `${routing}\nRequested conversation actors (stable IDs):\n${JSON.stringify(actors)}` -} diff --git a/apps/buddy/service/src/chat/ComposerDraftCommandService.ts b/apps/buddy/service/src/chat/ComposerDraftCommandService.ts new file mode 100644 index 000000000..b9d1a1437 --- /dev/null +++ b/apps/buddy/service/src/chat/ComposerDraftCommandService.ts @@ -0,0 +1,223 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { BuddyComposerDraft, BuddyComposerDraftScope } from '../../../shared/conversation/composerDraft' +import type { PreparedDraftAttachmentCopy } from '../attachments/AttachmentService' +import type { CommandAdmissionService, CommandResolution } from '../commands/CommandAdmissionService' +import type { InputCommandIntent } from '../commands/commandIntent' +import type { CommandDecision, CommandRejection } from '../storage/CommandCommitter' +import type { ComposerDraftService } from './ComposerDraftService' +import { randomUUID } from 'node:crypto' +import { buddyUserContentV1Schema, getBuddyUserContentResourceIds } from '../../../shared/conversation/buddyUserContent' +import { createAttachmentRepository } from '../storage/attachmentRepository' +import { ComposerDraftConflictError, createComposerDraftRepository, findPersonalComposerDraft } from '../storage/composerDraftRepository' +import { createDraftOwnershipRepository } from '../storage/draftOwnershipRepository' +import { afterTransactionCommit } from '../storage/transaction' +import { PreparedResourceSettlement } from './PreparedResourceSettlement' + +interface DraftCommandOptions { + database: DatabaseSync + actorId: string + admission: Pick + drafts: Pick + prepareAttachments: (input: { sourceDraftId: string, targetDraftId: string, attachmentIds: readonly string[] }) => Promise + releaseResources: (draftId: string) => Promise + onCleanupError: (error: unknown) => void +} + +type DraftCommandMethod = 'composerDrafts.open' | 'composerDrafts.save' | 'composerDrafts.discard' | 'composerDrafts.copy' +interface PreparedDraftCommand { + files?: PreparedDraftAttachmentCopy + commit: () => { decision: CommandDecision, draft: BuddyComposerDraft, kind: 'opened' | 'saved' | 'discarded' } +} +interface CopyResourceRow { + id: string + attachment_id: string | null + draft_id: string + state: string + [key: string]: unknown +} + +export class ComposerDraftCommandService { + readonly #options: DraftCommandOptions + readonly #cleanups = new Set>() + + constructor(options: DraftCommandOptions) { + this.#options = options + } + + async execute(method: DraftCommandMethod, input: unknown): Promise { + const { database, admission, drafts, onCleanupError } = this.#options + let resources: PreparedResourceSettlement | undefined + try { + const resolution = await admission.execute(method, input, { + scope: (intent, operation) => method === 'composerDrafts.copy' + ? drafts.withDraftLock((intent as InputCommandIntent<'composerDrafts.copy'>).sourceDraftId, operation) + : method === 'composerDrafts.open' + ? drafts.withDraftLock((intent as InputCommandIntent<'composerDrafts.open'>).draftId, operation) + : drafts.run('sourceDraftId' in intent ? intent.sourceDraftId : intent.draftId, operation), + prepare: async (intent, signal) => { + signal.throwIfAborted() + const prepared = await this.#prepare(method, intent) + resources = new PreparedResourceSettlement(prepared.files, onCleanupError) + signal.throwIfAborted() + return prepared + }, + reject: rejectDraftCommand, + commit: (value) => { + value.files?.validate() + const outcome = value.commit() + afterTransactionCommit(database, () => { + this.#cleanup(resources!.commit()) + drafts.notifyCommitted(outcome.kind, outcome.draft) + if (outcome.kind === 'discarded') + this.#cleanup(this.#options.releaseResources(outcome.draft.draftId)) + }, onCleanupError) + return outcome.decision + }, + }) + await resources?.rollback() + return resolution + } + catch (error) { + await resources?.fail(error) + throw error + } + } + + async dispose(): Promise { + await Promise.allSettled(this.#cleanups) + } + + async #prepare(method: DraftCommandMethod, intent: Readonly>): Promise { + const { database, actorId } = this.#options + const repository = createComposerDraftRepository(database, actorId) + if (method === 'composerDrafts.open') { + const input = intent as InputCommandIntent<'composerDrafts.open'> + return { commit: () => { + const ownership = createDraftOwnershipRepository(database, actorId) + if ((ownership.findSlot(input.scope)?.slotRevision ?? 0) !== input.expectedSlotRevision) + throw new DraftCommandConflict('DRAFT_CONFLICT') + if (database.prepare('SELECT 1 FROM composer_drafts WHERE id = ? AND owner_actor_id != ?').get(input.draftId, actorId)) + throw new DraftCommandConflict('DRAFT_CONFLICT') + requireResourceOwnership(database, input.draftId, getBuddyUserContentResourceIds(input.initialContent)) + const draft = repository.open({ ...input, now: new Date().toISOString() }) + const slot = ownership.findSlot(input.scope)! + return { draft, kind: 'opened', decision: { target: { draftId: draft.draftId }, result: { draftId: draft.draftId, revision: draft.revision, slotRevision: slot.slotRevision } } } + } } + } + if (method === 'composerDrafts.save') { + const input = intent as InputCommandIntent<'composerDrafts.save'> + return { commit: () => { + requireResourceOwnership(database, input.draftId, getBuddyUserContentResourceIds(input.content)) + const draft = repository.save({ ...input, now: new Date().toISOString() }) + return { draft, kind: 'saved', decision: { target: { draftId: draft.draftId }, result: { draftId: draft.draftId, revision: draft.revision } } } + } } + } + if (method === 'composerDrafts.discard') { + const input = intent as InputCommandIntent<'composerDrafts.discard'> + const draft = repository.findById(input.draftId) + if (!draft) + throw new DraftCommandConflict('OBJECT_DELETED') + return { commit: () => { + if (!repository.discard(input)) + throw new DraftCommandConflict('DRAFT_CONFLICT') + return { draft, kind: 'discarded', decision: { target: { draftId: draft.draftId }, result: { discarded: true } } } + } } + } + const input = intent as InputCommandIntent<'composerDrafts.copy'> + const source = findPersonalComposerDraft(database, input.sourceDraftId) + if (!source || source.revision !== input.expectedSourceRevision) + throw new DraftCommandConflict('DRAFT_CONFLICT') + const recovered = input.recoveredInput ? { ...source, ...input.recoveredInput } : source + const content = buddyUserContentV1Schema.parse(recovered.content) + const resourceIds = getBuddyUserContentResourceIds(content) + const resources = requireResourceOwnership(database, source.draftId, resourceIds) + if (resources.some(resource => resource.state !== 'ready')) + throw new DraftCommandConflict('RESOURCE_UNAVAILABLE') + const draftId = randomUUID() + const scope: BuddyComposerDraftScope = input.targetScope.kind === 'task' ? { ...input.targetScope, draftId } : input.targetScope + const files = await this.#options.prepareAttachments({ sourceDraftId: source.draftId, targetDraftId: draftId, attachmentIds: resources.flatMap(resource => resource.attachment_id ? [resource.attachment_id] : []) }) + const resourceMap = new Map(resourceIds.map(id => [id, randomUUID()])) + const copiedContent = { + ...content, + body: content.body.map(paragraph => ({ ...paragraph, content: paragraph.content.map(node => node.type === 'resource_ref' ? { ...node, resourceId: resourceMap.get(node.resourceId)! } : node) })), + panelResourceIds: content.panelResourceIds.map(id => resourceMap.get(id)!), + } + return { files, commit: () => { + if (findPersonalComposerDraft(database, source.draftId)?.revision !== input.expectedSourceRevision + || JSON.stringify(requireResourceOwnership(database, source.draftId, resourceIds)) !== JSON.stringify(resources)) { + throw new DraftCommandConflict('DRAFT_CONFLICT') + } + const ownership = createDraftOwnershipRepository(database, actorId) + if ((ownership.findSlot(scope)?.slotRevision ?? 0) !== input.expectedSlotRevision) + throw new DraftCommandConflict('DRAFT_CONFLICT') + const now = new Date().toISOString() + database.prepare(`INSERT INTO composer_drafts + (id, scope_kind, space_id, conversation_id, branch_id, source_message_id, revision, + content_json, model_selection_json, participants_json, approval_policy, execution_profile, created_at, updated_at, owner_actor_id) + VALUES (?, ?, ?, ?, ?, ?, 0, ?, ?, ?, ?, ?, ?, ?, ?)`).run( + draftId, + scope.kind, + 'spaceId' in scope ? scope.spaceId : null, + 'conversationId' in scope ? scope.conversationId : null, + 'branchId' in scope ? scope.branchId : null, + scope.kind === 'message_edit' ? scope.userMessageId : scope.kind === 'message_followup' ? scope.assistantMessageId : null, + JSON.stringify(copiedContent), + recovered.modelSelection ? JSON.stringify(recovered.modelSelection) : null, + recovered.participants ? JSON.stringify(recovered.participants) : null, + recovered.executionConfig.approvalPolicy, + recovered.executionConfig.executionProfile, + now, + now, + actorId, + ) + const attachments = createAttachmentRepository(database) + const attachmentIds = resources.flatMap(resource => resource.attachment_id ? [resource.attachment_id] : []) + if (files.records.length !== attachmentIds.length) + throw new Error('Draft attachment preparation does not match its source') + for (const record of files.records) + attachments.create({ ...record }) + const attachmentMap = new Map(attachmentIds.map((id, index) => [id, files.records[index]!.id])) + const copy = database.prepare(`INSERT INTO composer_resources + (id, draft_id, name, mime_type, size_bytes, state, attachment_id, content_hash, source_json, error_code, created_at, updated_at, name_source, source_path) + SELECT ?, ?, name, mime_type, size_bytes, state, ?, content_hash, source_json, error_code, ?, ?, name_source, source_path + FROM composer_resources WHERE id = ? AND draft_id = ?`) + for (const resource of resources) + copy.run(resourceMap.get(resource.id)!, draftId, resource.attachment_id ? attachmentMap.get(resource.attachment_id)! : null, now, now, resource.id, source.draftId) + const slot = ownership.select(scope, draftId, input.expectedSlotRevision) + const draft = repository.findById(draftId)! + return { draft, kind: 'opened', decision: { + target: { draftId, sourceDraftId: source.draftId }, + result: { draftId, revision: draft.revision, slotRevision: slot.slotRevision, sourceRevision: source.revision }, + } } + } } + } + + #cleanup(promise: Promise): void { + const cleanup = promise.catch(this.#options.onCleanupError).finally(() => this.#cleanups.delete(cleanup)) + this.#cleanups.add(cleanup) + } +} + +function requireResourceOwnership(database: DatabaseSync, draftId: string, ids: readonly string[]): CopyResourceRow[] { + return ids.map((id) => { + const row = database.prepare('SELECT * FROM composer_resources WHERE id = ? AND draft_id = ?').get(id, draftId) as CopyResourceRow | undefined + if (!row) + throw new DraftCommandConflict('RESOURCE_UNAVAILABLE') + return row + }) +} + +class DraftCommandConflict extends Error { + readonly code: CommandRejection['errorCode'] + + constructor(code: CommandRejection['errorCode']) { + super(code) + this.code = code + } +} + +function rejectDraftCommand(error: unknown): CommandRejection | null { + return error instanceof DraftCommandConflict || error instanceof ComposerDraftConflictError + ? { errorCode: error.code, target: {}, result: {} } + : null +} diff --git a/apps/buddy/service/src/chat/ComposerDraftService.ts b/apps/buddy/service/src/chat/ComposerDraftService.ts index 30d5b0d03..2989d797f 100644 --- a/apps/buddy/service/src/chat/ComposerDraftService.ts +++ b/apps/buddy/service/src/chat/ComposerDraftService.ts @@ -116,6 +116,10 @@ export class ComposerDraftService { }) } + withDraftLock(draftId: string, operation: () => T | Promise): Promise { + return this.#enqueue(draftId, operation) + } + run(draftId: string, operation: () => T | Promise): Promise { return this.#enqueue(draftId, () => { this.get(draftId) @@ -143,6 +147,10 @@ export class ComposerDraftService { this.#cleanup.dispose() } + notifyCommitted(kind: ComposerDraftCommit['kind'], draft: BuddyComposerDraft): void { + this.#publish(kind, draft) + } + #publish(kind: ComposerDraftCommit['kind'], draft: BuddyComposerDraft): void { this.#committed.fire(copyEventSnapshot({ operationId: randomUUID(), kind, draftId: draft.draftId, revision: draft.revision, scope: draft.scope })) } diff --git a/apps/buddy/service/src/chat/PreparedResourceSettlement.ts b/apps/buddy/service/src/chat/PreparedResourceSettlement.ts new file mode 100644 index 000000000..dec12609d --- /dev/null +++ b/apps/buddy/service/src/chat/PreparedResourceSettlement.ts @@ -0,0 +1,46 @@ +import { CommandResultUnknownError } from '../commands/CommandAdmissionError' +import { StorageIsolatedError } from '../storage/StorageIsolation' +import { readTransactionFailure } from '../storage/transaction' + +interface PreparedResources { + commit: () => Promise + rollback: () => Promise + retain: () => Promise +} + +export function isPreparedResourceResultUnknown(error: unknown): boolean { + return error instanceof CommandResultUnknownError || error instanceof StorageIsolatedError || readTransactionFailure(error)?.outcome === 'unknown' +} + +export class PreparedResourceSettlement { + readonly #resources: PreparedResources | null | undefined + readonly #onError: (error: unknown) => void + #settlement?: Promise + + constructor(resources: PreparedResources | null | undefined, onError: (error: unknown) => void = () => {}) { + this.#resources = resources + this.#onError = onError + } + + commit(): Promise { + return this.#settle('commit') + } + + rollback(): Promise { + return this.#settlement ? Promise.resolve() : this.#settle('rollback') + } + + fail(error: unknown): Promise { + return this.#settlement ? Promise.resolve() : this.#settle(isPreparedResourceResultUnknown(error) ? 'retain' : 'rollback') + } + + #settle(outcome: 'commit' | 'rollback' | 'retain'): Promise { + this.#settlement ??= Promise.resolve().then(() => this.#resources?.[outcome]()).catch((error) => { + try { + this.#onError(error) + } + catch {} + }) + return this.#settlement + } +} diff --git a/apps/buddy/service/src/chat/PreparedRunCommandService.ts b/apps/buddy/service/src/chat/PreparedRunCommandService.ts new file mode 100644 index 000000000..a6f5ab3c8 --- /dev/null +++ b/apps/buddy/service/src/chat/PreparedRunCommandService.ts @@ -0,0 +1,112 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { StorageCommand } from '../storage/CommandCommitter' +import type { PrepareCommandRequestInput } from '../storage/commandRequestRepository' +import type { EditTurnRequestInput, PrepareTurnRequestInput, RegenerateTurnRequestInput, TurnCommitFact, TurnRequestRecord } from '../storage/turnRequestRepository' +import { randomUUID } from 'node:crypto' +import { SqliteRunEventWriter } from '../events/SqliteRunEventWriter' +import { CommandCommitter } from '../storage/CommandCommitter' +import { createCommandRequestRepository } from '../storage/commandRequestRepository' +import { withTransaction } from '../storage/transaction' +import { TurnRequestService } from './TurnRequestService' + +type PreparedCommand = Omit +type PreparedInput = Omit + +export class PreparedRunCommandService { + readonly #ownsTurns: boolean + readonly #ownsEvents: boolean + readonly #database: DatabaseSync + readonly #sourceInstanceId: string + readonly #commands: CommandCommitter + readonly #turns: TurnRequestService + readonly #compaction: ReturnType + readonly events: SqliteRunEventWriter + readonly onDidCommit: TurnRequestService['onDidCommit'] + + constructor(options: { database: DatabaseSync, sourceInstanceId: string, turns?: TurnRequestService, events?: SqliteRunEventWriter, onObserverError?: (error: unknown) => void }) { + this.#database = options.database + this.#sourceInstanceId = options.sourceInstanceId + this.#commands = new CommandCommitter(options.database) + this.#ownsTurns = !options.turns + this.#ownsEvents = !options.events + this.#turns = options.turns ?? new TurnRequestService(options.database, options.onObserverError) + this.#compaction = createCommandRequestRepository(options.database) + this.events = options.events ?? new SqliteRunEventWriter(options.database, options.onObserverError) + this.onDidCommit = this.#turns.onDidCommit + } + + start(command: PreparedCommand, input: PreparedInput) { + return this.#accept(command, 'chat.startTurn', () => this.commitStart(command.fingerprint, input)) + } + + edit(command: PreparedCommand, input: PreparedInput) { + return this.#accept(command, 'chat.editUserMessage', () => this.commitEdit(command.fingerprint, input)) + } + + regenerate(command: PreparedCommand, input: PreparedInput) { + return this.#accept(command, 'chat.regenerateAssistant', () => this.commitRegenerate(command.fingerprint, input)) + } + + compact(command: PreparedCommand, input: PreparedInput) { + return this.#accept(command, 'chat.executeCommand', () => this.commitCompact(command.fingerprint, input)) + } + + commitStart(fingerprint: string, input: PreparedInput) { + return this.#commit(input.createdAt, () => this.#turns.prepare({ ...input, requestId: randomUUID(), requestFingerprint: fingerprint })) + } + + commitEdit(fingerprint: string, input: PreparedInput) { + return this.#commit(input.createdAt, () => this.#turns.edit({ ...input, requestId: randomUUID(), requestFingerprint: fingerprint })) + } + + commitRegenerate(fingerprint: string, input: PreparedInput) { + return this.#commit(input.createdAt, () => this.#turns.regenerate({ ...input, requestId: randomUUID(), requestFingerprint: fingerprint })) + } + + commitCompact(fingerprint: string, input: PreparedInput) { + return this.#commit(input.createdAt, () => { + const request = this.#compaction.prepare({ ...input, requestId: randomUUID(), requestFingerprint: fingerprint }) + const committedFacts: TurnCommitFact[] = [{ kind: 'run.queued' }, ...(request.draftReceipt ? [{ kind: 'draft.consumed' as const, receipt: request.draftReceipt }] : [])] + return { ...request, committedFacts } + }) + } + + find(command: Pick) { + return this.#commands.find(command) + } + + dispose(): void { + if (this.#ownsTurns) + this.#turns.dispose() + if (this.#ownsEvents) + this.events.dispose() + } + + #accept(command: PreparedCommand, method: string, operation: () => ReturnType) { + return this.#commands.accept({ ...command, method, sourceInstanceId: this.#sourceInstanceId }, () => operation().decision) + } + + #commit(createdAt: string, operation: () => TurnRequestRecord) { + return withTransaction(this.#database, () => { + const request = operation() + if (!request.created || !request.committedFacts?.length) + throw new Error('A new command must create its own run and acceptance facts') + if (!this.#database.prepare('SELECT 1 FROM run_events WHERE run_id = ? AND event_type = \'run.queued\'').get(request.runId)) { + this.#database.prepare('UPDATE runs SET next_event_sequence = 1 WHERE id = ? AND next_event_sequence IS NULL').run(request.runId) + this.events.append({ + runId: request.runId, + type: 'run.queued', + payload: { requestId: request.requestId, facts: request.committedFacts }, + createdAt, + }) + } + return { + request, + decision: { + target: { runId: request.runId, conversationId: request.conversationId, branchId: request.branchId }, + result: { draftReceipt: request.draftReceipt }, + }, + } + }) + } +} diff --git a/apps/buddy/service/src/chat/QueueContinuation.ts b/apps/buddy/service/src/chat/QueueContinuation.ts index c73da4f8e..6f5a5fc75 100644 --- a/apps/buddy/service/src/chat/QueueContinuation.ts +++ b/apps/buddy/service/src/chat/QueueContinuation.ts @@ -95,7 +95,7 @@ export class QueueContinuation { #request(scope: LocalChatQueueScope): void { if (this.#state === 'stopped') return - this.#dirty.set(`conversation:${scope.conversationId}`, { scope: { conversationId: scope.conversationId, branchId: scope.branchId }, attempts: 0 }) + this.#dirty.set(JSON.stringify([scope.conversationId, scope.branchId]), { scope: { conversationId: scope.conversationId, branchId: scope.branchId }, attempts: 0 }) this.#schedule() } diff --git a/apps/buddy/service/src/chat/TurnRequestService.ts b/apps/buddy/service/src/chat/TurnRequestService.ts index d5a108f17..d2dfc523b 100644 --- a/apps/buddy/service/src/chat/TurnRequestService.ts +++ b/apps/buddy/service/src/chat/TurnRequestService.ts @@ -1,8 +1,12 @@ +import type { DatabaseSync } from 'node:sqlite' import type { EventSnapshot } from '../../../shared/events/eventTypes' import type { RunEventAvailability } from '../events/RunEventPorts' +import type { SqliteRunEventWriter } from '../events/SqliteRunEventWriter' import type { EditTurnRequestInput, PrepareQuestionReplyInput, PrepareTurnRequestInput, RegenerateTurnRequestInput, RetryInterruptedTurnRequestInput, TurnCommitFact, TurnRequestRecord, TurnRequestRepository } from '../storage/turnRequestRepository' import { Emitter } from '../../../shared/events/Emitter' import { copyEventSnapshot } from '../../../shared/events/eventSnapshot' +import { afterTransactionCommit, withTransaction } from '../storage/transaction' +import { createTurnRequestRepository } from '../storage/turnRequestRepository' export type TurnRequestCommit = EventSnapshot<{ commitId: string @@ -14,19 +18,26 @@ export type TurnRequestCommit = EventSnapshot<{ }> export class TurnRequestService implements TurnRequestRepository { + readonly #events?: SqliteRunEventWriter readonly #availability?: Pick + readonly #database: DatabaseSync + readonly #onObserverError: (error: unknown) => void readonly #repository: TurnRequestRepository readonly #committed: Emitter readonly onDidCommit: Emitter['event'] #disposed = false constructor( - repository: TurnRequestRepository, + database: DatabaseSync, onObserverError: (error: unknown) => void = () => {}, availability?: Pick, + events?: SqliteRunEventWriter, ) { this.#availability = availability - this.#repository = repository + this.#events = events + this.#database = database + this.#repository = createTurnRequestRepository(database) + this.#onObserverError = onObserverError this.#committed = new Emitter(onObserverError) this.onDidCommit = this.#committed.event } @@ -36,25 +47,33 @@ export class TurnRequestService implements TurnRequestRepository { } prepare(input: PrepareTurnRequestInput | PrepareQuestionReplyInput) { - this.#availability?.assertConversationWritable(input.conversationId) - return this.#commit(() => this.#repository.prepare(input)) + return this.#commit(() => { + this.#availability?.assertConversationWritable(input.conversationId) + return this.#repository.prepare(input) + }) } edit(input: EditTurnRequestInput) { - this.#availability?.assertConversationWritable(input.conversationId) - return this.#commit(() => this.#repository.edit(input)) + return this.#commit(() => { + this.#availability?.assertConversationWritable(input.conversationId) + return this.#repository.edit(input) + }) } regenerate(input: RegenerateTurnRequestInput) { - this.#availability?.assertConversationWritable(input.conversationId) - return this.#commit(() => this.#repository.regenerate(input)) + return this.#commit(() => { + this.#availability?.assertConversationWritable(input.conversationId) + return this.#repository.regenerate(input) + }) } retryInterrupted(input: RetryInterruptedTurnRequestInput) { - const request = this.#repository.findByRequestId(input.requestId) - if (request) - this.#availability?.assertConversationWritable(request.conversationId) - return this.#commit(() => this.#repository.retryInterrupted(input)) + return this.#commit(() => { + const request = this.#repository.findByRequestId(input.requestId) + if (request) + this.#availability?.assertConversationWritable(request.conversationId) + return this.#repository.retryInterrupted(input) + }) } dispose(): void { @@ -65,9 +84,17 @@ export class TurnRequestService implements TurnRequestRepository { #commit(operation: () => TurnRequestRecord): TurnRequestRecord { if (this.#disposed) throw new Error('Turn request service is stopped') - const request = operation() - if (request.created && request.committedFacts?.length) - this.#committed.fire(copyEventSnapshot({ commitId: request.runId, requestId: request.requestId, runId: request.runId, conversationId: request.conversationId, branchId: request.branchId, facts: request.committedFacts })) - return request + return withTransaction(this.#database, () => { + const request = operation() + if (request.created && request.committedFacts?.length) { + if (this.#events) { + this.#database.prepare('UPDATE runs SET next_event_sequence = 1 WHERE id = ? AND next_event_sequence IS NULL').run(request.runId) + this.#events.append({ runId: request.runId, type: 'run.queued', payload: { requestId: request.requestId, facts: request.committedFacts } }) + } + const snapshot = copyEventSnapshot({ commitId: request.runId, requestId: request.requestId, runId: request.runId, conversationId: request.conversationId, branchId: request.branchId, facts: request.committedFacts }) + afterTransactionCommit(this.#database, () => this.#committed.fire(snapshot), this.#onObserverError) + } + return request + }) } } diff --git a/apps/buddy/service/src/chat/__tests__/ChatCommandService.spec.ts b/apps/buddy/service/src/chat/__tests__/ChatCommandService.spec.ts index 164225656..793070834 100644 --- a/apps/buddy/service/src/chat/__tests__/ChatCommandService.spec.ts +++ b/apps/buddy/service/src/chat/__tests__/ChatCommandService.spec.ts @@ -43,7 +43,6 @@ describe('command acceptance facts', () => { { kind: 'run.queued', runId: expect.any(String), purpose: 'conversation.compaction' }, { kind: 'draft.consumed', draftId: 'draft-1', sourceRevision: 0, committedRevision: 1 }, ]) - expect(Reflect.set(commits[0]!.facts[2], 'committedRevision', 20)).toBe(false) await expect(f.service.execute(f.input)).resolves.toMatchObject({ runId: commits[0]!.facts[1].runId, draftReceipt: { committedRevision: 1 } }) expect(commits).toHaveLength(1) expect(f.launch).toHaveBeenCalledTimes(1) @@ -51,19 +50,6 @@ describe('command acceptance facts', () => { await f.service.dispose() }) - it('does not publish partial facts when consuming the draft rolls back the transaction', async () => { - const f = fixture() - const commits: ChatCommandCommit[] = [] - f.service.onDidCommit(event => commits.push(event)) - f.database.exec('CREATE TRIGGER reject_draft BEFORE UPDATE ON composer_drafts BEGIN SELECT RAISE(ABORT, \'draft unavailable\'); END') - await expect(f.service.execute(f.input)).rejects.toThrow('draft unavailable') - expect(commits).toEqual([]) - expect(f.commands.findByRequestId(f.input.requestId)).toBeNull() - expect(f.drafts.findById('draft-1')?.revision).toBe(0) - expect(f.runs.listRecent()).toHaveLength(1) - await f.service.dispose() - }) - it('drains an accepted operation when a commit observer requests shutdown', async () => { const f = fixture() const gate = Promise.withResolvers() diff --git a/apps/buddy/service/src/chat/__tests__/ChatInputCommandService.spec.ts b/apps/buddy/service/src/chat/__tests__/ChatInputCommandService.spec.ts new file mode 100644 index 000000000..62e87bf34 --- /dev/null +++ b/apps/buddy/service/src/chat/__tests__/ChatInputCommandService.spec.ts @@ -0,0 +1,217 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { PreparedTurnAttachments } from '../persistPreparedTurn' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { createBuddyUserContent } from '../../../../shared/conversation/buddyUserContent' +import { CommandAdmissionService, createLocalCommandPrincipal } from '../../commands/CommandAdmissionService' +import { createComposerDraftRepository } from '../../storage/composerDraftRepository' +import { createConversationRepository } from '../../storage/conversationRepository' +import { openBuddyMigrationCandidate } from '../../storage/database' +import { ChatInputCommandService } from '../ChatInputCommandService' +import { ComposerDraftService } from '../ComposerDraftService' +import { PreparedRunCommandService } from '../PreparedRunCommandService' + +const databases: DatabaseSync[] = [] +const now = '2026-11-07T00:00:00.000Z' +afterEach(() => { + vi.restoreAllMocks() + for (const database of databases.splice(0)) + database.close() +}) + +function fixture() { + const database = openBuddyMigrationCandidate({ databasePath: ':memory:' }) + databases.push(database) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + const repository = createComposerDraftRepository(database) + repository.open({ draftId: 'draft', initialContent: createBuddyUserContent('Keep this input'), initialExecutionConfig: { approvalPolicy: 'policy', executionProfile: 'workspace_write' }, initialModelSelection: null, now, scope: { kind: 'global' } }) + const drafts = new ComposerDraftService(repository, async () => {}) + const admission = new CommandAdmissionService({ database, principal: createLocalCommandPrincipal(database), sourceInstanceId: 'instance' }) + const preparedRuns = new PreparedRunCommandService({ database, sourceInstanceId: 'instance' }) + const input = { + dataGeneration: String(database.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation), + expectedServiceInstanceId: 'instance', + commandId: 'command', + intent: { draftId: 'draft', expectedRevision: 0 }, + } + const materials: PreparedTurnAttachments = { bindings: [], validate() {}, commit: vi.fn(async () => {}), rollback: vi.fn(async () => {}), retain: vi.fn(async () => {}) } + let hold: Promise | null = null + let prepares = 0 + const prepareStart = async () => { + prepares++ + await hold + return { stagedAttachments: materials, prepared: { + attachmentBindings: [], + branchId: 'branch', + conversationId: 'conversation', + createdAt: now, + approvalPolicy: 'policy' as const, + executionProfile: 'workspace_write' as const, + draft: { draftId: 'draft', expectedRevision: 0 }, + model: 'fixture', + spaceId: null, + provider: 'fixture', + runInput: { attachmentIds: [], contextItems: [], prompt: 'Keep this input', reasoning: null, serviceTier: null }, + runId: 'run', + title: 'Input', + userMessageContent: { text: 'Keep this input' }, + userMessageId: 'message', + requestId: 'internal-request', + requestFingerprint: 'internal-fingerprint', + } } + } + const launched: string[] = [] + let launchError: Error | undefined + const errors: unknown[] = [] + const service = new ChatInputCommandService({ + database, + admission, + drafts, + preparedRuns, + onDispatchError: error => errors.push(error), + queue: { + async prepareEnqueue() { throw new Error('Not used') }, + commitEnqueue() { throw new Error('Not used') }, + publishEnqueued() {}, + }, + compact: { prepare() { throw new Error('Not used') } }, + turns: { + prepareStart, + async prepareEdit() { throw new Error('Not used') }, + async prepareRegenerate() { throw new Error('Not used') }, + async launchAccepted(runId) { + expect(database.isTransaction).toBe(false) + expect(admission.lookup({ dataGeneration: input.dataGeneration, commandId: input.commandId }).lookup).toBe('decided') + launched.push(runId) + if (launchError) + throw launchError + }, + }, + }) + return { database, repository, drafts, admission, service, input, materials, launched, errors, failLaunch: (error: Error) => { + launchError = error + }, prepareCount: () => prepares, hold: (promise: Promise) => { + hold = promise + } } +} + +describe('chat input command admission', () => { + it('retains acceptance and resource ownership when dispatch fails after commit', async () => { + const f = fixture() + const failure = new Error('execution owner unavailable') + f.failLaunch(failure) + const accepted = await f.service.execute('chat.startTurn', f.input) + await f.service.waitForDispatches() + expect(accepted.receipt.decision).toBe('accepted') + expect(f.database.prepare('SELECT id FROM runs').all()).toEqual([{ id: 'run' }]) + expect(f.errors).toEqual([failure]) + expect(f.materials.commit).toHaveBeenCalledOnce() + expect(f.materials.rollback).not.toHaveBeenCalled() + expect(await f.service.execute('chat.startTurn', f.input)).toEqual({ created: false, receipt: accepted.receipt }) + expect(f.launched).toEqual(['run']) + }) + + it('commits one target and launches once for concurrent requests and reply loss', async () => { + const f = fixture() + const gate = Promise.withResolvers() + f.hold(gate.promise) + const first = f.service.execute('chat.startTurn', f.input) + const second = f.service.execute('chat.startTurn', structuredClone(f.input)) + gate.resolve() + const [accepted, replay] = await Promise.all([first, second]) + await f.service.waitForDispatches() + expect(accepted.created).toBe(true) + expect(replay).toEqual({ created: false, receipt: accepted.receipt }) + expect(f.prepareCount()).toBe(1) + expect(f.launched).toEqual(['run']) + expect(f.database.prepare('SELECT count(*) AS count FROM run_events').get()).toEqual({ count: 1 }) + f.database.prepare('UPDATE runs SET status = \'failed\', error_code = \'SERVICE_INTERRUPTED\' WHERE id = \'run\'').run() + f.database.prepare('DELETE FROM turn_requests WHERE draft_id = \'draft\'').run() + f.database.prepare('DELETE FROM composer_drafts WHERE id = \'draft\'').run() + expect(await f.service.execute('chat.startTurn', { ...f.input, expectedServiceInstanceId: 'old-instance' })).toEqual({ created: false, receipt: accepted.receipt }) + expect(f.launched).toEqual(['run']) + }) + + it('keeps a draft save behind preparation and the acceptance transaction', async () => { + const f = fixture() + const gate = Promise.withResolvers() + f.hold(gate.promise) + const accepted = f.service.execute('chat.startTurn', f.input) + await vi.waitFor(() => expect(f.prepareCount()).toBe(1)) + const draft = f.repository.findById('draft')! + const save = f.drafts.save({ draftId: 'draft', expectedRevision: 0, content: createBuddyUserContent('Later edit'), executionConfig: draft.executionConfig, modelSelection: draft.modelSelection }) + const saved = save.catch(error => error) + gate.resolve() + expect((await accepted).receipt.decision).toBe('accepted') + expect(await saved).toMatchObject({ code: 'DRAFT_CONFLICT' }) + expect(f.repository.findById('draft')?.revision).toBe(1) + await f.service.waitForDispatches() + }) + + it('records a stable rejection when the draft changes through another writer during preparation', async () => { + const f = fixture() + const gate = Promise.withResolvers() + f.hold(gate.promise) + const result = f.service.execute('chat.startTurn', f.input) + await vi.waitFor(() => expect(f.prepareCount()).toBe(1)) + const draft = f.repository.findById('draft')! + f.repository.save({ draftId: 'draft', expectedRevision: 0, content: createBuddyUserContent('Changed'), executionConfig: draft.executionConfig, modelSelection: draft.modelSelection, now }) + gate.resolve() + const rejected = await result + expect(rejected.receipt).toMatchObject({ decision: 'rejected', errorCode: 'DRAFT_CONFLICT' }) + expect(f.database.prepare('SELECT count(*) AS count FROM runs').get()).toEqual({ count: 0 }) + expect(f.materials.rollback).toHaveBeenCalledOnce() + expect(await f.service.execute('chat.startTurn', f.input)).toEqual({ created: false, receipt: rejected.receipt }) + expect(f.launched).toEqual([]) + }) + + it.each(['chat.startTurn', 'chat.editUserMessage'] as const)('records a fixed rejection when %s refers to a deleted task', async (method) => { + const f = fixture() + await f.service.execute('chat.startTurn', f.input) + await f.service.waitForDispatches() + f.database.prepare('UPDATE runs SET status = \'completed\' WHERE id = \'run\'').run() + const conversations = createConversationRepository(f.database) + conversations.createBranch({ id: 'other-branch', conversationId: 'conversation', parentBranchId: 'branch', forkedFromMessageId: 'message', createdAt: now, activate: true }) + f.repository.open({ draftId: 'stale-draft', initialContent: createBuddyUserContent('Keep the original source'), initialExecutionConfig: { approvalPolicy: 'policy', executionProfile: 'workspace_write' }, initialModelSelection: null, now, scope: method === 'chat.editUserMessage' + ? { kind: 'message_edit', conversationId: 'conversation', branchId: 'branch', userMessageId: 'message' } + : { kind: 'conversation_branch', conversationId: 'conversation', branchId: 'branch' } }) + f.database.prepare('UPDATE conversations SET deleted_at = ? WHERE id = ?').run(now, 'conversation') + const input = { ...f.input, commandId: 'stale-command', intent: { draftId: 'stale-draft', expectedRevision: 0, ...(method === 'chat.editUserMessage' ? { conversationId: 'conversation', userMessageId: 'message' } : {}) } } + const result = await f.service.execute(method, input) + expect(result.receipt).toMatchObject({ decision: 'rejected', errorCode: 'SOURCE_CHANGED' }) + expect(conversations.findById('conversation')?.activeBranchId).toBe('other-branch') + expect(f.repository.findById('stale-draft')).toMatchObject({ revision: 0, content: createBuddyUserContent('Keep the original source') }) + expect(f.database.prepare('SELECT COUNT(*) AS count FROM runs').get()).toEqual({ count: 1 }) + f.database.prepare('UPDATE conversations SET deleted_at = NULL WHERE id = ?').run('conversation') + expect(await f.service.execute(method, input)).toEqual({ created: false, receipt: result.receipt }) + expect(f.launched).toEqual(['run']) + }) + + it('retains prepared files after an uncertain commit and never dispatches from that connection', async () => { + const f = fixture() + const exec = f.database.exec.bind(f.database) + vi.spyOn(f.database, 'exec').mockImplementation((sql) => { + exec(sql) + if (sql === 'COMMIT') + throw new Error('Lost commit acknowledgement') + }) + await expect(f.service.execute('chat.startTurn', f.input)).rejects.toMatchObject({ code: 'COMMAND_RESULT_UNKNOWN' }) + expect(f.materials.retain).toHaveBeenCalledOnce() + expect(f.materials.rollback).not.toHaveBeenCalled() + expect(f.launched).toEqual([]) + expect(f.database.prepare('SELECT count(*) AS count FROM command_receipts').get()).toEqual({ count: 1 }) + }) + + it('does not accept or launch after stop wins during materialization', async () => { + const f = fixture() + const gate = Promise.withResolvers() + f.hold(gate.promise) + const result = f.service.execute('chat.startTurn', f.input) + await vi.waitFor(() => expect(f.prepareCount()).toBe(1)) + f.admission.stopAccepting() + gate.resolve() + await expect(result).rejects.toMatchObject({ code: 'SERVICE_STOPPING' }) + expect(f.database.prepare('SELECT count(*) AS count FROM runs').get()).toEqual({ count: 0 }) + expect(f.materials.rollback).toHaveBeenCalledOnce() + expect(f.launched).toEqual([]) + }) +}) diff --git a/apps/buddy/service/src/chat/__tests__/ChatQueueService.spec.ts b/apps/buddy/service/src/chat/__tests__/ChatQueueService.spec.ts index e04785373..72c2f1a9e 100644 --- a/apps/buddy/service/src/chat/__tests__/ChatQueueService.spec.ts +++ b/apps/buddy/service/src/chat/__tests__/ChatQueueService.spec.ts @@ -16,23 +16,26 @@ import { createBuddyUserContent } from '../../../../shared/conversation/buddyUse import { Emitter } from '../../../../shared/events/Emitter' import { resolveBuddyPermissionSettings } from '../../../../shared/permissions/permissionMode' import { BuddyDefinitionResolver } from '../../buddies/BuddyDefinitionResolver' +import { CommandAdmissionService, createLocalCommandPrincipal } from '../../commands/CommandAdmissionService' import { ConversationMetadataService } from '../../conversations/ConversationMetadataService' import { RunEventCorruptionError } from '../../events/RunEventFailure' import { resolveInteractiveModelSelection } from '../../providers/resolveInteractiveModelSelection' import { toPublicRun } from '../../runs/publicRun' import { createAttachmentRepository } from '../../storage/attachmentRepository' import { createBuddyRepository } from '../../storage/buddyRepository' +import { ChatControlCommandRepository } from '../../storage/ChatControlCommandRepository' import { createChatQueueRepository } from '../../storage/chatQueueRepository' import { createComposerDraftRepository } from '../../storage/composerDraftRepository' import { createConversationMemberRepository } from '../../storage/conversationMemberRepository' import { createConversationRepository } from '../../storage/conversationRepository' -import { openBuddyDatabase } from '../../storage/database' +import { openBuddyDatabase, openBuddyMigrationCandidate } from '../../storage/database' import { createExecutionRepository } from '../../storage/executionRepository' import { createRunInputRepository } from '../../storage/runInputRepository' import { createRunRepository } from '../../storage/runRepository' import { createThreadBindingRepository } from '../../storage/threadBindingRepository' import { createTurnRequestRepository } from '../../storage/turnRequestRepository' import { isReviewUserMessage } from '../buddyReviewPrompt' +import { ChatControlCommandService } from '../ChatControlCommandService' import { ChatQueueService } from '../ChatQueueService' import { ChatTurnService } from '../ChatTurnService' import { QueueContinuation } from '../QueueContinuation' @@ -86,7 +89,7 @@ describe('persistent chat queue scheduling', () => { expect(attachments.findById('live-image')).toMatchObject({ messageId: 'live-input', draftId: null }) }) - it.each(['model', 'provider', 'reasoning', 'serviceTier', 'capability', 'permission'] as const)('still rejects a current %s mismatch against the effective live selection', async (mismatch) => { + it.each(['model', 'capability', 'permission'] as const)('still rejects a current %s mismatch against the effective live selection', async (mismatch) => { const f = fixture() await f.enableDefinitions() const selection: ConversationModelSelection = { providerId: 'provider', modelId: 'changed-model', reasoning: 'high', serviceTier: 'priority' } @@ -95,15 +98,6 @@ describe('persistent chat queue scheduling', () => { if (mismatch === 'model') { requested.modelId = 'different-model' } - else if (mismatch === 'provider') { - requested.providerId = 'different-provider' - } - else if (mismatch === 'reasoning') { - requested.reasoning = 'low' - } - else if (mismatch === 'serviceTier') { - requested.serviceTier = null - } else if (mismatch === 'capability') { const buddy = f.buddies.get('default') f.buddies.update({ buddyId: buddy.id, expectedRevision: buddy.revision, settings: buddySettingsSchema.parse({ ...buddy.settings, tools: { mode: 'none' } }) }) @@ -119,7 +113,7 @@ describe('persistent chat queue scheduling', () => { expect(f.runs.findById('run-initial')?.modelOverride).toEqual(selection) }) - it.each(['enqueue', 'steer', 'followUp'] as const)('rechecks a run-only model override changed during %s validation', async (boundary) => { + it.each(['enqueue', 'steer'] as const)('rechecks a run-only model override changed during %s validation', async (boundary) => { const f = fixture() await f.enableDefinitions() const selection: ConversationModelSelection = { providerId: 'provider', modelId: 'changed-model', reasoning: 'high', serviceTier: null } @@ -129,11 +123,7 @@ describe('persistent chat queue scheduling', () => { if (boundary === 'steer') f.validate.mockImplementationOnce(async () => {}) f.validate.mockImplementationOnce(() => setLiveModel(f, { ...selection, reasoning: 'low' }, 'run-initial')) - if (boundary === 'followUp') { - f.queue.enqueue(prepared) - expect(await f.service.followUp('run-initial', f.controller.signal)).toBe(false) - } - else if (boundary === 'enqueue') { + if (boundary === 'enqueue') { await expect(f.service.enqueue(enqueueRequest(f, prepared))).rejects.toMatchObject({ code: 'INPUT_DELIVERY_INCOMPATIBLE' }) expect(f.drafts.findById('draft')).toEqual(draft) expect(f.queue.list(f.scope)).toEqual([]) @@ -147,13 +137,13 @@ describe('persistent chat queue scheduling', () => { expect(f.database.prepare('SELECT id FROM messages').all()).toEqual([{ id: 'initial' }]) }) - it.each(['legacy', 'current'] as const)('uses the refreshed run selection when a %s follow-up becomes compatible during input refresh', async (delivery) => { + it('uses the refreshed run selection when a current follow-up becomes compatible during input refresh', async () => { const f = fixture() await f.enableDefinitions() const selection: ConversationModelSelection = { providerId: 'provider', modelId: 'changed-model', reasoning: 'high', serviceTier: null } await setLiveModel(f, selection) const prepared = await prepareCurrentInput(f, 'refreshed-model', selection) - f.queue.enqueue({ ...prepared, ...(delivery === 'legacy' ? { delivery: undefined } : {}) }) + f.queue.enqueue(prepared) await setLiveModel(f, { ...selection, modelId: 'temporary-model' }, 'run-initial') f.turns.refreshQueuedInput = async (input) => { const refreshed = await f.actualTurns.refreshQueuedInput(input) @@ -168,27 +158,12 @@ describe('persistent chat queue scheduling', () => { expect(f.runs.findById('run-refreshed-model')).toBeNull() }) - it.each(['current', 'next'] as const)('requires nonempty %s targets to use enqueue even after durable closure but before owner release', async (mode) => { - const f = fixture() - f.database.exec('UPDATE runs SET status = \'completed\'; UPDATE execution_state SET cleanup_state = \'clean\'') - const draft = f.drafts.findById('draft')! - await expect(f.actualTurns.start({ - draftId: draft.draftId, - expectedRevision: draft.revision, - requestId: 'direct-start', - delivery: { mode, actorId: 'default', target: { rootRunId: 'run-initial', runId: 'run-initial' } }, - })).rejects.toMatchObject({ code: 'INPUT_DELIVERY_INCOMPATIBLE' }) - expect(f.runner.hasActiveExecution('conversation', 'default')).toBe(true) - expect(f.drafts.findById('draft')).toEqual(draft) - expect(f.database.prepare('SELECT id FROM runs').all()).toEqual([{ id: 'run-initial' }]) - }) - - it.each(['none', 'multiple', 'single'] as const)('rejects a first delivery that disagrees with the %s mention routing without consuming its draft', async (mentions) => { + it.each(['none', 'single'] as const)('rejects a first delivery that disagrees with the %s mention routing without consuming its draft', async (mentions) => { const f = fixture() const other = f.buddies.create({ name: 'Other actor' }) f.conversations.addMember({ id: 'conversation', buddyId: other.id, updatedAt: 'now' }) const previous = f.drafts.findById('draft')! - const actors = mentions === 'none' ? [] : mentions === 'single' ? [other.id] : ['default', other.id] + const actors = mentions === 'none' ? [] : [other.id] const content = { ...createBuddyUserContent(), body: [{ type: 'paragraph' as const, content: [...actors.map(actorId => ({ type: 'actor_ref' as const, actorId, name: actorId === 'default' ? 'Lexora' : other.name })), { type: 'text' as const, text: 'New request' }] }], @@ -234,18 +209,13 @@ describe('persistent chat queue scheduling', () => { expect(f.database.prepare('SELECT id FROM messages ORDER BY rowid').all()).toEqual([{ id: 'initial' }, { id: 'selected' }]) }) - it.each(['incompatible', 'closed', 'refused'] as const)('keeps next intent and queue state when explicit steering is %s', async (failure) => { + it('keeps next intent and queue state when explicit steering is refused', async () => { const f = fixture() const delivery = { mode: 'next' as const, actorId: 'default', target: { rootRunId: 'run-initial', runId: 'run-initial' } } await f.service.enqueue(enqueueRequest(f, { ...f.input('next'), buddyId: 'default', delivery })) const before = f.queue.list(f.scope) - if (failure === 'incompatible') - f.database.exec('UPDATE runs SET model = \'another-model\'') - else if (failure === 'closed') - f.validate.mockImplementationOnce(async () => { f.database.exec('UPDATE runs SET status = \'completed\'') }) - else - f.runner.steer.mockReturnValueOnce(false) - expect(await f.service.steer(f.target('next'))).toBe(failure === 'incompatible' ? 'incompatible' : 'unavailable') + f.runner.steer.mockReturnValueOnce(false) + expect(await f.service.steer(f.target('next'))).toBe('unavailable') expect(f.queue.list(f.scope)).toEqual(before) expect(f.delivered).toEqual([]) expect(f.launched).toEqual([]) @@ -386,23 +356,6 @@ describe('persistent chat queue scheduling', () => { expect(f.runs.findActiveForConversation('conversation')?.id).toBe('run-initial') }) - it('pauses a failed actor queue while another actor can still receive new work', async () => { - const f = fixture() - const actor = f.buddies.create({ name: 'Available actor' }) - f.conversations.addMember({ id: 'conversation', buddyId: actor.id, updatedAt: '2026-10-25T00:00:00.000Z' }) - await f.enableDefinitions() - f.queue.enqueue(f.input('failed-owner')) - f.database.prepare('UPDATE runs SET status = \'failed\' WHERE id = ?').run('run-initial') - f.releaseSlot() - f.queue.enqueue({ ...f.input('peer'), buddyId: actor.id, actorIds: [actor.id], userMessageContent: { - userContent: { ...createBuddyUserContent(), body: [{ type: 'paragraph', content: [{ type: 'actor_ref', actorId: actor.id, name: actor.name }, { type: 'text', text: 'Independent request' }] }] }, - resourceSnapshots: [], - } }) - expect(await f.service.reconcile(f.scope)).toBe(true) - expect(f.queue.list(f.scope)).toMatchObject([{ id: 'failed-owner', state: 'paused' }]) - expect(f.launched).toEqual(['run-peer']) - }) - it('keeps queued input undelivered in a quarantined conversation across steer, followup and dispatch', async () => { const failure = new RunEventCorruptionError('historical-run') const f = fixture(false, { @@ -502,19 +455,7 @@ describe('persistent chat queue scheduling', () => { expect(f.runs.findById('run-A')?.actorSnapshot?.settings).toMatchObject({ instructions: 'Updated instructions', tools: { mode: 'none' } }) }) - it('persists an explicit model choice that matches the active run when delivering a native continuation', async () => { - const f = fixture() - await f.enableDefinitions() - const originalSnapshot = f.runs.findById('run-initial')?.actorSnapshot - const modelOverride = { providerId: 'provider', modelId: 'model', reasoning: null, serviceTier: null } - f.queue.enqueue({ ...f.input('A'), modelOverride }) - expect(await f.service.followUp('run-initial', f.controller.signal)).toBe(true) - expect(f.members.find('conversation', 'default')).toMatchObject({ modelSelection: modelOverride, modelSelectionSource: 'explicit' }) - expect(f.runs.findById('run-A')).toBeNull() - expect(f.runs.findById('run-initial')?.actorSnapshot).toEqual(originalSnapshot) - }) - - it.each(['actor', 'member_model', 'definition'] as const)('retains queued input when %s changes during async dispatch validation', async (change) => { + it.each(['actor', 'definition'] as const)('retains queued input when %s changes during async dispatch validation', async (change) => { const f = fixture() await f.enableDefinitions() f.queue.enqueue(f.input('A')) @@ -530,9 +471,6 @@ describe('persistent chat queue scheduling', () => { f.conversations.addMember({ id: 'conversation', buddyId: other.id, updatedAt: '2026-10-11T10:00:01.000Z' }) f.conversations.setBuddy({ id: 'conversation', buddyId: other.id, updatedAt: '2026-10-11T10:00:01.000Z' }) } - else if (change === 'member_model') { - f.members.setModelSelection({ conversationId: 'conversation', actorId: 'default', modelSelection: { providerId: 'provider', modelId: 'new-member-model', reasoning: null, serviceTier: null } }) - } else { const buddy = f.buddies.get('default') f.buddies.update({ buddyId: buddy.id, expectedRevision: buddy.revision, settings: buddySettingsSchema.parse({ ...buddy.settings, instructions: 'Updated during validation' }) }) @@ -583,7 +521,7 @@ describe('persistent chat queue scheduling', () => { try { for (const id of ['B', 'C']) { const prepared = f.input(id) - f.turns.prepareStart = async () => ({ prepared, stagedAttachments: { validate: () => {}, bindings: [], commit: async () => {}, rollback: async () => {} } }) + f.turns.prepareStart = async () => ({ prepared, stagedAttachments: { validate: () => {}, bindings: [], commit: async () => {}, rollback: async () => {}, retain: async () => {} } }) await f.service.enqueue({ draftId: 'draft', expectedRevision: prepared.draft.expectedRevision, requestId: prepared.requestId }) } f.releaseSlot() @@ -664,7 +602,7 @@ describe('persistent chat queue scheduling', () => { prepared.runInput.attachmentIds = ['owned-attachment'] const binding = { id: 'owned-attachment', sourceAttachmentId: 'owned-attachment', sourceDraftId: 'draft', messageId: prepared.userMessageId, storedPath: '/private/destination.txt', sourceStoredPath: '/private/source.txt', name: 'fixture.txt', sizeBytes: 1, mimeType: 'text/plain', createdAt: prepared.createdAt } prepared.attachmentBindings = [binding] - f.turns.prepareStart = async () => ({ prepared, stagedAttachments: { validate: () => {}, bindings: [binding], commit: async () => {}, rollback: async () => {} } }) + f.turns.prepareStart = async () => ({ prepared, stagedAttachments: { validate: () => {}, bindings: [binding], commit: async () => {}, rollback: async () => {}, retain: async () => {} } }) const events: ChatQueueChange[] = [] f.service.onDidChange(event => events.push(event)) const input = { requestId: prepared.requestId, draftId: 'draft', expectedRevision: prepared.draft.expectedRevision } @@ -694,7 +632,7 @@ describe('persistent chat queue scheduling', () => { prepared.userMessageContent = { userContent: { ...createBuddyUserContent(), body: [{ type: 'paragraph', content: [{ type: 'actor_ref', actorId: 'default', name: 'Lexora' }, { type: 'text', text: 'Use the attached requirements' }] }] }, resourceSnapshots: [] } const binding = { id: 'directed-attachment', sourceAttachmentId: 'directed-attachment', sourceDraftId: 'draft', messageId: prepared.userMessageId, storedPath: '/private/destination.txt', sourceStoredPath: '/private/source.txt', name: 'requirements.txt', sizeBytes: 12, mimeType: 'text/plain', createdAt: prepared.createdAt } prepared.attachmentBindings = [binding] - f.turns.prepareStart = async () => ({ prepared, stagedAttachments: { validate: () => {}, bindings: [binding], commit: async () => {}, rollback: async () => {} } }) + f.turns.prepareStart = async () => ({ prepared, stagedAttachments: { validate: () => {}, bindings: [binding], commit: async () => {}, rollback: async () => {}, retain: async () => {} } }) const executions = createExecutionRepository(f.database) expect(executions.listAttachments('run-initial')).toEqual([]) const references: BuddyInputReferenceV1[] = [] @@ -727,12 +665,15 @@ describe('persistent chat queue scheduling', () => { expect(f.launched).toEqual([]) }) - it('resolves the current queue branch instead of treating a late settlement as a launch target', async () => { + it('does not dispatch another branch after an unrelated branch settles', async () => { const f = fixture() f.queue.enqueue(f.input('A')) f.database.exec('UPDATE runs SET status = \'completed\'') f.releaseSlot() f.settled.fire({ runId: 'run-initial', conversationId: 'conversation', branchId: 'retired-branch', executionId: 'old-execution', cleanup: 'completed', stopping: false }) + await f.continuation.start() + expect(f.launched).toEqual([]) + f.settle('run-initial') await vi.waitFor(() => expect(f.launched).toEqual(['run-A'])) expect(f.runs.findById('run-A')?.branchId).toBe('branch') }) @@ -753,43 +694,17 @@ describe('persistent chat queue scheduling', () => { expect(events.map(event => event.facts.map(fact => fact.kind))).toEqual([['message.created', 'run.queued', 'queue.dispatched']]) }) - it.each(['settlement', 'reconciliation'] as const)('keeps completed work paused after degraded cleanup through %s', async (wake) => { + it('keeps completed work paused after degraded cleanup', async () => { const f = fixture() f.queue.enqueue(f.input('A')) f.database.exec('UPDATE runs SET status = \'completed\'') - if (wake === 'settlement') { - f.settle('run-initial', 'degraded') - } - else { - f.releaseSlot('degraded') - f.continuation.reconcile() - } + f.settle('run-initial', 'degraded') await vi.waitFor(() => expect(f.queue.list(f.scope)[0]?.state).toBe('paused')) f.continuation.reconcile() await new Promise(resolve => setTimeout(resolve, 15)) expect(f.launched).toEqual([]) }) - it('bounds failed reconciliation and recovers without silently resuming the paused queue', async () => { - const f = fixture() - f.queue.enqueue(f.input('A')) - f.database.exec('UPDATE runs SET status = \'completed\'') - f.releaseSlot() - const unavailable = vi.spyOn(f.queue, 'list').mockImplementation(() => { - throw new Error('Storage unavailable') - }) - f.continuation.reconcile() - await vi.waitFor(() => expect(f.continuation.state).toBe('degraded')) - expect(f.launched).toEqual([]) - unavailable.mockRestore() - expect(f.queue.list(f.scope)[0]?.state).toBe('paused') - f.continuation.reconcile() - await vi.waitFor(() => expect(f.continuation.state).toBe('ready')) - expect(f.launched).toEqual([]) - expect(await f.service.steer(f.target('A'))).toBe('accepted') - expect(f.launched).toEqual(['run-A']) - }) - it('does not start a new run when disposed during continuation validation', async () => { const f = fixture() f.queue.enqueue(f.input('A')) @@ -864,21 +779,14 @@ describe('persistent chat queue scheduling', () => { expect(f.queue.list(f.scope).map(item => item.id)).toEqual(['B', 'C']) }) - it.each(['stop', 'cancel', 'dispose', 'pause'] as const)('does not consume an input after %s during async validation', async (action) => { + it('does not consume an input after cancellation during async validation', async () => { const f = fixture() f.queue.enqueue(f.input('A')) const gate = Promise.withResolvers() f.validate.mockImplementationOnce(() => gate.promise) const following = f.service.followUp('run-initial', f.controller.signal) await vi.waitFor(() => expect(f.validate).toHaveBeenCalledOnce()) - if (action === 'stop') - f.controller.abort() - else if (action === 'cancel') - f.service.cancel(f.target('A')) - else if (action === 'pause') - f.queue.pause() - else - f.service.dispose() + f.controller.abort() gate.resolve() expect(await following).toBe(false) expect(f.delivered).toEqual([]) @@ -888,8 +796,6 @@ describe('persistent chat queue scheduling', () => { it.each([ { from: 'read_only', to: 'full_access', mode: 'steer' }, { from: 'full_access', to: 'read_only', mode: 'followUp' }, - { from: 'manual_approval', to: 'policy_approval', mode: 'followUp' }, - { from: 'policy_approval', to: 'manual_approval', mode: 'steer' }, ] as const)( 'delivers $mode in the same run after switching permissions from $from to $to', async ({ from, to, mode }) => { @@ -913,37 +819,33 @@ describe('persistent chat queue scheduling', () => { }, ) - it.each(['steer', 'followUp'] as const)('retains input when permissions change during async $mode validation and accepts a retry under current permissions', async (mode) => { + it('retains input when permissions change during async steering and accepts a retry under current permissions', async () => { const f = fixture() await f.enableDefinitions() f.queue.enqueue(f.input('A')) const gate = Promise.withResolvers() f.validate.mockImplementationOnce(() => gate.promise) - const delivery = mode === 'steer' ? f.service.steer(f.target('A')) : f.service.followUp('run-initial', f.controller.signal) + const delivery = f.service.steer(f.target('A')) try { await vi.waitFor(() => expect(f.validate).toHaveBeenCalledOnce()) f.setPermissions(resolveBuddyPermissionSettings('read_only')) } finally { gate.resolve() } - expect(await delivery).toBe(mode === 'steer' ? 'unavailable' : false) + expect(await delivery).toBe('unavailable') expect(f.queue.list(f.scope).map(item => item.id)).toEqual(['A']) expect(f.database.prepare('SELECT id FROM messages').all()).toEqual([{ id: 'initial' }]) - expect(mode === 'steer' ? await f.service.steer(f.target('A')) : await f.service.followUp('run-initial', f.controller.signal)).toBe(mode === 'steer' ? 'accepted' : true) - expect(f.delivered).toEqual([`${mode}:A`]) + expect(await f.service.steer(f.target('A'))).toBe('accepted') + expect(f.delivered).toEqual(['steer:A']) expect(f.runInputs.findByMessageId('A')?.runId).toBe('run-initial') }) - it.each(['model', 'provider', 'reasoning', 'serviceTier', 'modelParameters'] as const)( + it.each(['model', 'reasoning'] as const)( 'keeps a queued %s change for a new run with its original settings', async (field) => { const f = fixture() const input = f.input('A') if (field === 'reasoning') input.runInput.reasoning = 'high' - else if (field === 'serviceTier') - input.runInput.serviceTier = 'priority' - else if (field === 'modelParameters') - input.modelParameters = { contextWindow: 32000, maxTokens: 2048 } else input[field] = 'changed' f.database.prepare('UPDATE conversations SET approval_policy = ?, execution_profile = ?').run(input.approvalPolicy, input.executionProfile) const draft = f.drafts.findById('draft')! @@ -1025,6 +927,150 @@ describe('persistent chat queue scheduling', () => { }) }) +describe('conditional queue and stop commands', () => { + it('replays the committed unknown stop receipt after cleanup later succeeds', async () => { + const f = commandFixture() + f.database.exec('UPDATE runs SET status = \'completed\'; UPDATE execution_state SET phase = \'closed\', cleanup_state = \'degraded\'') + const request = f.command('stop-degraded', { targetServiceInstanceId: 'instance', scope: 'stop_service' }) + const accepted = await f.commands.execute('runtime.stop', request) + expect(accepted.receipt).toMatchObject({ decision: 'accepted', result: { runIds: [], executionResources: 'unknown' } }) + await f.commands.waitForEffects() + f.database.exec('UPDATE execution_state SET cleanup_state = \'clean\'') + expect(await f.commands.execute('runtime.stop', request)).toEqual({ created: false, receipt: accepted.receipt }) + expect(f.stopped).toEqual([[]]) + }) + + it('commits a queue withdrawal ahead of a steering preparation and fixes the rejected receipt', async () => { + const f = commandFixture() + f.queue.enqueue(f.input('A')) + const gate = Promise.withResolvers() + f.validate.mockImplementationOnce(() => gate.promise) + const intent = { ...f.target('A'), expectedRevision: 0, targetRunId: 'run-initial', expectedRunGeneration: 0 } + const steering = f.commands.execute('chat.queue.steer', f.command('steer', intent)) + await vi.waitFor(() => expect(f.validate).toHaveBeenCalled()) + const cancelled = await f.commands.execute('chat.queue.cancel', f.command('cancel', { ...f.target('A'), expectedRevision: 0 })) + expect(cancelled.receipt.decision).toBe('accepted') + gate.resolve() + const rejected = await steering + expect(rejected.receipt).toMatchObject({ decision: 'rejected', errorCode: 'QUEUE_CHANGED' }) + await f.commands.waitForEffects() + expect(f.database.prepare('SELECT state, revision FROM chat_queue WHERE id = \'A\'').get()).toEqual({ state: 'cancelled', revision: 1 }) + expect(f.database.prepare('SELECT id FROM messages').all()).toEqual([{ id: 'initial' }]) + expect(await f.commands.execute('chat.queue.steer', f.command('steer', intent))).toEqual({ created: false, receipt: rejected.receipt }) + expect(f.launched).toEqual([]) + }) + + it.each(['unavailable', 'unknown'] as const)('keeps committed input sent after Pi delivery is %s', async (stage) => { + const f = commandFixture() + f.queue.enqueue(f.input('A')) + f.runner.steer.mockImplementationOnce(() => { + if (stage === 'unknown') + throw new Error('Pi delivery interrupted') + return false + }) + const request = f.command('steer', { ...f.target('A'), expectedRevision: 0, targetRunId: 'run-initial', expectedRunGeneration: 0 }) + const result = await f.commands.execute('chat.queue.steer', request) + await f.commands.waitForEffects() + expect(result.receipt).toMatchObject({ decision: 'accepted', target: { queueId: 'A', runId: 'run-initial' }, result: { deliveryStage: 'committed' } }) + expect(f.database.prepare('SELECT state, revision FROM chat_queue WHERE id = \'A\'').get()).toEqual({ state: 'sent', revision: 1 }) + expect(f.database.prepare('SELECT stage FROM chat_queue_deliveries WHERE queue_id = \'A\'').get()).toEqual({ stage }) + expect(f.database.prepare('SELECT id FROM messages ORDER BY rowid').all()).toEqual([{ id: 'initial' }, { id: 'A' }]) + expect(await f.commands.execute('chat.queue.steer', request)).toEqual({ created: false, receipt: result.receipt }) + expect(f.queue.list(f.scope)).toEqual([]) + expect(f.launched).toEqual([]) + }) + + it('continues a paused idle queue once and publishes acceptance before launch', async () => { + const f = commandFixture() + f.queue.enqueue(f.input('A')) + f.queue.pauseItem(f.target('A')) + f.database.exec('UPDATE runs SET status = \'completed\' WHERE id = \'run-initial\'') + f.settle('run-initial') + const request = f.command('continue', { ...f.target('A'), expectedRevision: 1 }) + const result = await f.commands.execute('chat.queue.continue', request) + await f.commands.waitForEffects() + expect(result.receipt).toMatchObject({ decision: 'accepted', target: { queueId: 'A', runId: 'run-A' } }) + expect(f.database.prepare('SELECT state, run_id FROM chat_queue WHERE id = ?').get('A')).toEqual({ state: 'sent', run_id: 'run-A' }) + expect(f.launched).toEqual(['run-A']) + expect(await f.commands.execute('chat.queue.continue', request)).toEqual({ created: false, receipt: result.receipt }) + expect(f.launched).toEqual(['run-A']) + }) + + it('rejects continue when an active run appears during preparation without redirecting to steer', async () => { + const f = commandFixture() + f.queue.enqueue(f.input('A')) + f.queue.pauseItem(f.target('A')) + f.database.exec('UPDATE runs SET status = \'completed\' WHERE id = \'run-initial\'') + f.settle('run-initial') + f.validate.mockImplementationOnce(async () => { + f.database.exec('UPDATE runs SET status = \'running\' WHERE id = \'run-initial\'') + }) + const result = await f.commands.execute('chat.queue.continue', f.command('continue', { ...f.target('A'), expectedRevision: 1 })) + expect(result.receipt).toMatchObject({ decision: 'rejected', errorCode: 'INPUT_DELIVERY_TARGET_UNAVAILABLE' }) + expect(f.queue.pending(f.target('A'))).not.toBeNull() + expect(f.launched).toEqual([]) + expect(f.delivered).toEqual([]) + }) + + it('keeps internal follow-up from delivering a queue version changed during validation', async () => { + const f = commandFixture() + f.queue.enqueue(f.input('A')) + f.validate.mockImplementationOnce(async () => { + f.queue.pauseItem(f.target('A')) + }) + expect(await f.service.followUp('run-initial', f.controller.signal)).toBe(false) + expect(f.database.prepare('SELECT state, revision FROM chat_queue WHERE id = \'A\'').get()).toEqual({ state: 'paused', revision: 1 }) + expect(f.database.prepare('SELECT id FROM messages').all()).toEqual([{ id: 'initial' }]) + }) + + it('persists the exact run cancellation before notifying its execution owner', async () => { + const f = commandFixture() + const request = f.command('cancel', { runId: 'run-initial', expectedRunGeneration: 0 }) + f.turns.cancel = async (runId) => { + expect(f.database.isTransaction).toBe(false) + expect(f.database.prepare('SELECT cancel_requested_at FROM execution_state WHERE run_id = ?').get(runId)?.cancel_requested_at).toBeTypeOf('string') + expect(f.admission.lookup({ dataGeneration: request.dataGeneration, commandId: request.commandId }).lookup).toBe('decided') + return toPublicRun(f.runs.findById(runId)!, null) + } + const result = await f.commands.execute('chat.cancel', request) + await f.commands.waitForEffects() + expect(result.receipt).toMatchObject({ decision: 'accepted', result: { cancelRequested: true, executionResources: 'occupied' } }) + expect(f.runs.findById('run-initial')).toMatchObject({ generation: 1, status: 'running' }) + expect(await f.commands.execute('chat.cancel', request)).toEqual({ created: false, receipt: result.receipt }) + const stale = await f.commands.execute('chat.cancel', { ...request, commandId: 'late-cancel' }) + expect(stale.receipt).toMatchObject({ decision: 'rejected', errorCode: 'SOURCE_CHANGED' }) + }) + + it('keeps service-running requests open and persists an instance barrier for stop_service', async () => { + const f = commandFixture() + await f.commands.execute('runtime.stop', f.command('keep', { targetServiceInstanceId: 'instance', scope: 'keep_service' })) + expect(f.database.prepare('SELECT count(*) AS count FROM service_admission_gates').get()).toEqual({ count: 0 }) + f.queue.enqueue(f.input('A')) + const request = f.command('stop', { targetServiceInstanceId: 'instance', scope: 'stop_service' }) + const stopped = await f.commands.execute('runtime.stop', request) + await f.commands.waitForEffects() + expect(stopped.receipt).toMatchObject({ decision: 'accepted', result: { runIds: ['run-initial'], executionResources: 'occupied' } }) + expect(f.stopped).toEqual([['run-initial']]) + expect(f.queue.list(f.scope)).toMatchObject([{ id: 'A', state: 'paused' }]) + await expect(f.commands.execute('chat.queue.cancel', f.command('late-input', { ...f.target('A'), expectedRevision: 1 }))).rejects.toMatchObject({ code: 'SERVICE_STOPPING' }) + expect(await f.commands.execute('runtime.stop', request)).toEqual({ created: false, receipt: stopped.receipt }) + expect(() => f.control!.assertAccepting()).toThrow('SERVICE_STOPPING') + }) +}) + +function commandFixture() { + const f = fixture(false, undefined, true) + f.database.exec('UPDATE execution_state SET phase = \'working\'') + const admission = new CommandAdmissionService({ database: f.database, principal: createLocalCommandPrincipal(f.database), sourceInstanceId: 'instance' }) + const errors: unknown[] = [] + const stopped: Array = [] + const commands = new ChatControlCommandService({ database: f.database, admission, repository: f.control!, queue: f.service, onDispatchError: error => errors.push(error), onServiceStop: (runIds) => { + stopped.push(runIds) + } }) + const command = (commandId: string, intent: unknown) => ({ commandId, intent, expectedServiceInstanceId: 'instance', dataGeneration: String(f.database.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation) }) + return { ...f, command, commands, admission, errors, stopped } +} + async function setLiveModel(f: ReturnType, selection: ConversationModelSelection, runId?: string) { const metadata = new ConversationMetadataService({ runs: f.runs, @@ -1052,15 +1098,20 @@ function prepareCurrentInput(f: ReturnType, id: string, selectio } function enqueueRequest(f: ReturnType, prepared: PrepareTurnRequestInput) { - f.turns.prepareStart = async () => ({ prepared, stagedAttachments: { validate: () => {}, bindings: [], commit: async () => {}, rollback: async () => {} } }) + if (!prepared.draft) + throw new Error('Expected a draft-backed fixture') + f.turns.prepareStart = async () => ({ prepared, stagedAttachments: { validate: () => {}, bindings: [], commit: async () => {}, rollback: async () => {}, retain: async () => {} } }) return { draftId: prepared.draft.draftId, expectedRevision: prepared.draft.expectedRevision, requestId: prepared.requestId, ...(prepared.delivery ? { delivery: prepared.delivery } : {}) } } -function fixture(queuedOnStartup = false, availability?: ChatQueueServiceOptions['availability']) { - const database = openBuddyDatabase({ databasePath: ':memory:' }) +function fixture(queuedOnStartup = false, availability?: ChatQueueServiceOptions['availability'], candidate = false) { + const database = (candidate ? openBuddyMigrationCandidate : openBuddyDatabase)({ databasePath: ':memory:' }) + if (candidate) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + const control = candidate ? new ChatControlCommandRepository(database, 'instance') : undefined const drafts = createComposerDraftRepository(database) drafts.open({ draftId: 'draft', scope: { kind: 'global' }, initialContent: createBuddyUserContent('initial'), initialModelSelection: null, initialExecutionConfig: { approvalPolicy: 'policy', executionProfile: 'workspace_write' }, now: '2026-09-15T00:00:00.000Z' }) - const input = (id: string): PrepareTurnRequestInput => ({ + const input = (id: string): PrepareTurnRequestInput & { draft: NonNullable } => ({ ...drafts.findById('draft')!.executionConfig, attachmentBindings: [], branchId: 'branch', @@ -1078,7 +1129,7 @@ function fixture(queuedOnStartup = false, availability?: ChatQueueServiceOptions userMessageContent: { userContent: createBuddyUserContent(id), resourceSnapshots: [] }, userMessageId: id, }) - const requests = new TurnRequestService(createTurnRequestRepository(database)) + const requests = new TurnRequestService(database) requests.prepare(input('initial')) database.exec('UPDATE runs SET status = \'running\'') const queue = createChatQueueRepository(database) @@ -1116,6 +1167,7 @@ function fixture(queuedOnStartup = false, availability?: ChatQueueServiceOptions refreshQueuedInput: async input => input, } const service = new ChatQueueService({ + control, availability, getRunPermissionCeiling: (runId) => { const run = runs.findById(runId) @@ -1171,7 +1223,7 @@ function fixture(queuedOnStartup = false, availability?: ChatQueueServiceOptions } fixtures.push({ database, service, continuation }) const scope = { conversationId: 'conversation', branchId: 'branch' } - return { database, drafts, queue, input, service, actualTurns, providers, continuation, settled, settle, committed, eventLog, buddies, members, conversations, setPermissions, enableDefinitions, resolveModel, model, releaseSlot: (cleanup: ExecutionSettled['cleanup'] = 'completed') => { + return { database, control, drafts, queue, input, service, actualTurns, providers, continuation, settled, settle, committed, eventLog, buddies, members, conversations, setPermissions, enableDefinitions, resolveModel, model, releaseSlot: (cleanup: ExecutionSettled['cleanup'] = 'completed') => { occupied = false cleanupDegraded = cleanup === 'degraded' }, scope, target: (id: string) => ({ ...scope, id }), controller: new AbortController(), validate, delivered, launched, runInputs, runs, runner, requests, turns } diff --git a/apps/buddy/service/src/chat/__tests__/ComposerDraftCommandService.spec.ts b/apps/buddy/service/src/chat/__tests__/ComposerDraftCommandService.spec.ts new file mode 100644 index 000000000..57c2b5618 --- /dev/null +++ b/apps/buddy/service/src/chat/__tests__/ComposerDraftCommandService.spec.ts @@ -0,0 +1,139 @@ +import { mkdir, mkdtemp, readFile, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { createBuddyUserContent } from '../../../../shared/conversation/buddyUserContent' +import { AttachmentService } from '../../attachments/AttachmentService' +import { ComposerResourceService } from '../../attachments/ComposerResourceService' +import { CommandAdmissionService, createLocalCommandPrincipal } from '../../commands/CommandAdmissionService' +import { createAttachmentRepository } from '../../storage/attachmentRepository' +import { BuddyDataPaths } from '../../storage/BuddyDataPaths' +import { createComposerDraftRepository } from '../../storage/composerDraftRepository' +import { createComposerResourceRepository } from '../../storage/composerResourceRepository' +import { createConversationTreeRepository } from '../../storage/conversationTreeRepository' +import { openBuddyMigrationCandidate } from '../../storage/database' +import { createDraftOwnershipRepository } from '../../storage/draftOwnershipRepository' +import { ComposerDraftCommandService } from '../ComposerDraftCommandService' +import { ComposerDraftService } from '../ComposerDraftService' + +const cleanups: (() => Promise)[] = [] +const now = '2026-11-07T00:00:00.000Z' +afterEach(async () => { + for (const cleanup of cleanups.splice(0).reverse()) + await cleanup() +}) + +async function fixture() { + const root = await mkdtemp(join(tmpdir(), 'lexora-draft-commands-')) + const database = openBuddyMigrationCandidate({ databasePath: join(root, 'buddy.sqlite3') }) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + const repository = createComposerDraftRepository(database) + const open = (id: string, global = false) => repository.open({ draftId: id, initialContent: createBuddyUserContent(id), initialExecutionConfig: { approvalPolicy: 'policy', executionProfile: 'read_only' }, initialModelSelection: null, now, scope: global ? { kind: 'global' } : { kind: 'task', draftId: id, spaceId: null } }) + const source = open('source') + const previous = open('previous', true) + const attachments = new AttachmentService({ paths: new BuddyDataPaths(root), repository: createAttachmentRepository(database) }) + const resources = new ComposerResourceService({ attachments, paths: new BuddyDataPaths(root), repository: createComposerResourceRepository(database), publications: createConversationTreeRepository(database), drafts: repository }) + const drafts = new ComposerDraftService(repository, id => resources.discard(id)) + const principal = createLocalCommandPrincipal(database) + const admission = new CommandAdmissionService({ database, principal, sourceInstanceId: 'instance' }) + const errors: unknown[] = [] + const commands = new ComposerDraftCommandService({ database, actorId: principal.actorId, admission, drafts, prepareAttachments: input => attachments.prepareDraftCopy(input), releaseResources: id => resources.discard(id), onCleanupError: error => errors.push(error) }) + cleanups.push(async () => { + await commands.dispose() + await admission.dispose() + await drafts.dispose() + await resources.dispose() + await attachments.dispose() + database.close() + await rm(root, { recursive: true, force: true }) + }) + const envelope = (commandId: string, intent: unknown) => ({ commandId, intent, expectedServiceInstanceId: 'instance', dataGeneration: database.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation }) + return { root, database, repository, source, previous, attachments, drafts, commands, principal, errors, envelope } +} + +describe('conditional composer draft commands', () => { + it('opens one owner slot atomically and replays the original result after it is saved', async () => { + const f = await fixture() + const intent = { draftId: 'opened', scope: { kind: 'task', draftId: 'opened', spaceId: null }, initialContent: createBuddyUserContent('initial'), initialExecutionConfig: f.source.executionConfig, initialModelSelection: null, expectedSlotRevision: 0 } + const input = f.envelope('open', intent) + const accepted = await f.commands.execute('composerDrafts.open', input) + expect(accepted.receipt).toMatchObject({ decision: 'accepted', target: { draftId: 'opened' }, result: { revision: 0, slotRevision: 1 } }) + f.repository.save({ draftId: 'opened', expectedRevision: 0, content: createBuddyUserContent('newer local input'), executionConfig: f.source.executionConfig, modelSelection: null, now }) + expect(await f.commands.execute('composerDrafts.open', input)).toEqual({ created: false, receipt: accepted.receipt }) + const rejected = await f.commands.execute('composerDrafts.open', f.envelope('stale-open', intent)) + expect(rejected.receipt).toMatchObject({ decision: 'rejected', errorCode: 'DRAFT_CONFLICT' }) + expect(f.repository.findById('opened')?.revision).toBe(1) + }) + + it('copies independent files and IDs, preserves the displaced draft, and replays after source changes', async () => { + const f = await fixture() + await mkdir(join(f.root, 'drafts/source/attachments'), { recursive: true }) + const [attachment] = await f.attachments.registerUploads('source', [{ name: 'sample.txt', mimeType: 'text/plain', bytes: new TextEncoder().encode('kept bytes') }]) + const resources = createComposerResourceRepository(f.database) + resources.accept('source', [{ resourceId: 'resource', name: 'sample.txt', mimeType: 'text/plain', sizeBytes: 10 }], now) + resources.finish({ draftId: 'source', resourceId: 'resource', attachmentId: attachment!.id, contentHash: 'hash', now }) + const source = f.repository.save({ draftId: 'source', expectedRevision: 0, content: { ...createBuddyUserContent('source'), panelResourceIds: ['resource'] }, executionConfig: f.source.executionConfig, modelSelection: null, now }) + const slot = createDraftOwnershipRepository(f.database, f.principal.actorId).findSlot({ kind: 'global' })! + const input = f.envelope('copy', { sourceDraftId: 'source', expectedSourceRevision: source.revision, targetScope: { kind: 'global' }, expectedSlotRevision: slot.slotRevision }) + const accepted = await f.commands.execute('composerDrafts.copy', input) + const draftId = String(accepted.receipt.target.draftId) + expect(accepted.receipt.decision).toBe('accepted') + expect(draftId).not.toBe('source') + const copied = f.repository.findById(draftId)! + expect(copied.content.panelResourceIds).toHaveLength(1) + expect(copied.content.panelResourceIds[0]).not.toBe('resource') + const copiedResources = resources.listForDraft(draftId) + const copiedAttachment = createAttachmentRepository(f.database).findById(copiedResources[0]!.attachmentId!)! + expect(copiedAttachment.storedPath).not.toBe(attachment!.storedPath) + expect(await readFile(copiedAttachment.storedPath, 'utf8')).toBe('kept bytes') + expect(await readFile(attachment!.storedPath, 'utf8')).toBe('kept bytes') + expect({ ...f.repository.findById('previous'), slotRevision: undefined }).toEqual({ ...f.previous, slotRevision: undefined }) + expect(f.repository.findByScope({ kind: 'global' })?.draftId).toBe(draftId) + f.repository.save({ draftId: 'source', expectedRevision: source.revision, content: createBuddyUserContent('new source'), executionConfig: source.executionConfig, modelSelection: null, now }) + expect(await f.commands.execute('composerDrafts.copy', input)).toEqual({ created: false, receipt: accepted.receipt }) + const recovered = await f.commands.execute('composerDrafts.copy', f.envelope('recover-input', { + sourceDraftId: 'source', + expectedSourceRevision: source.revision + 1, + targetScope: { kind: 'task', spaceId: null }, + expectedSlotRevision: 0, + recoveredInput: { content: { ...createBuddyUserContent('unconfirmed local edits'), panelResourceIds: ['resource'] }, executionConfig: source.executionConfig, modelSelection: null }, + })) + const recoveredDraft = f.repository.findById(String(recovered.receipt.target.draftId))! + expect(recovered.receipt.decision).toBe('accepted') + expect(recoveredDraft.content.body).toEqual(createBuddyUserContent('unconfirmed local edits').body) + expect(recoveredDraft.content.panelResourceIds[0]).not.toBe('resource') + expect(await readFile(createAttachmentRepository(f.database).findById(resources.listForDraft(recoveredDraft.draftId)[0]!.attachmentId!)!.storedPath, 'utf8')).toBe('kept bytes') + const discard = f.envelope('discard-source', { draftId: 'source', expectedRevision: source.revision + 1 }) + const discarded = await f.commands.execute('composerDrafts.discard', discard) + expect(discarded.receipt.decision).toBe('accepted') + expect(f.repository.findById('source')).toBeNull() + expect(await f.commands.execute('composerDrafts.discard', discard)).toEqual({ created: false, receipt: discarded.receipt }) + await f.commands.dispose() + await expect(readFile(attachment!.storedPath)).rejects.toMatchObject({ code: 'ENOENT' }) + expect(await readFile(copiedAttachment.storedPath, 'utf8')).toBe('kept bytes') + expect(f.repository.findById(draftId)?.content).toEqual(copied.content) + expect(await f.commands.execute('composerDrafts.copy', input)).toEqual({ created: false, receipt: accepted.receipt }) + expect(f.errors).toEqual([]) + }) + + it('rejects an unavailable resource without creating a destination', async () => { + const f = await fixture() + createComposerResourceRepository(f.database).accept('source', [{ resourceId: 'pending', name: 'pending.txt', mimeType: 'text/plain', sizeBytes: 1 }], now) + f.repository.save({ draftId: 'source', expectedRevision: 0, content: { ...createBuddyUserContent('changed'), panelResourceIds: ['pending'] }, executionConfig: f.source.executionConfig, modelSelection: null, now }) + const input = f.envelope('conditional-copy', { sourceDraftId: 'source', expectedSourceRevision: 1, targetScope: { kind: 'task', spaceId: null }, expectedSlotRevision: 0 }) + const outcome = await f.commands.execute('composerDrafts.copy', input) + expect(outcome.receipt).toMatchObject({ decision: 'rejected', errorCode: 'RESOURCE_UNAVAILABLE' }) + expect(f.database.prepare('SELECT COUNT(*) AS count FROM composer_drafts').get()?.count).toBe(2) + expect(await f.commands.execute('composerDrafts.copy', input)).toEqual({ created: false, receipt: outcome.receipt }) + expect(f.repository.findByScope({ kind: 'global' })).toEqual(f.previous) + }) + + it('does not replace the destination slot when its CAS loses', async () => { + const f = await fixture() + const input = f.envelope('copy-stale-slot', { sourceDraftId: 'source', expectedSourceRevision: 0, targetScope: { kind: 'global' }, expectedSlotRevision: 0 }) + const rejected = await f.commands.execute('composerDrafts.copy', input) + expect(rejected.receipt).toMatchObject({ decision: 'rejected', errorCode: 'DRAFT_CONFLICT' }) + expect(f.repository.findByScope({ kind: 'global' })).toEqual(f.previous) + expect(f.database.prepare('SELECT COUNT(*) AS count FROM composer_drafts').get()?.count).toBe(2) + }) +}) diff --git a/apps/buddy/service/src/chat/__tests__/PreparedResourceSettlement.spec.ts b/apps/buddy/service/src/chat/__tests__/PreparedResourceSettlement.spec.ts new file mode 100644 index 000000000..d01da4f1d --- /dev/null +++ b/apps/buddy/service/src/chat/__tests__/PreparedResourceSettlement.spec.ts @@ -0,0 +1,73 @@ +import { mkdtemp, readFile, rm, unlink, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { CommandResultUnknownError } from '../../commands/CommandAdmissionError' +import { StorageIsolatedError } from '../../storage/StorageIsolation' +import { BuddyTransactionUncertainError } from '../../storage/transaction' +import { PreparedResourceSettlement } from '../PreparedResourceSettlement' + +const directories: string[] = [] +afterEach(async () => { + for (const directory of directories.splice(0)) await rm(directory, { recursive: true, force: true }) +}) + +async function fixture(onError?: (error: unknown) => void) { + const directory = await mkdtemp(join(tmpdir(), 'lexora-prepared-settlement-')) + directories.push(directory) + const path = join(directory, 'prepared.txt') + await writeFile(path, 'possibly referenced bytes') + const outcomes: string[] = [] + const resources = { + async commit() { outcomes.push('commit') }, + async rollback() { + outcomes.push('rollback') + await unlink(path) + }, + async retain() { outcomes.push('retain') }, + } + return { path, resources, outcomes, settlement: new PreparedResourceSettlement(resources, onError) } +} + +describe('prepared resource settlement', () => { + it.each([ + new CommandResultUnknownError(new Error('receipt unavailable')), + new StorageIsolatedError(new Error('storage unavailable')), + new BuddyTransactionUncertainError(new Error('commit unavailable'), { phase: 'commit', outcome: 'unknown' }), + ])('retains bytes for $name and does not later discard the same receipt', async (error) => { + const f = await fixture() + await f.settlement.fail(error) + await f.settlement.rollback() + await f.settlement.fail(new Error('later cleanup failure')) + expect(f.outcomes).toEqual(['retain']) + expect(await readFile(f.path, 'utf8')).toBe('possibly referenced bytes') + }) + + it('removes definitely rejected bytes once', async () => { + const f = await fixture() + await f.settlement.fail(new Error('validation rejected')) + await f.settlement.rollback() + expect(f.outcomes).toEqual(['rollback']) + await expect(readFile(f.path)).rejects.toMatchObject({ code: 'ENOENT' }) + }) + + it('keeps accepted bytes when post-commit cleanup and its reporter fail', async () => { + const failure = new Error('cleanup failed') + const errors: unknown[] = [] + const f = await fixture((error) => { + errors.push(error) + throw new Error('reporter failed') + }) + f.resources.commit = async () => { + f.outcomes.push('commit') + throw failure + } + const committed = f.settlement.commit() + await f.settlement.fail(new Error('dispatch failed')) + await f.settlement.rollback() + await committed + expect(f.outcomes).toEqual(['commit']) + expect(errors).toEqual([failure]) + expect(await readFile(f.path, 'utf8')).toBe('possibly referenced bytes') + }) +}) diff --git a/apps/buddy/service/src/chat/__tests__/PreparedRunCommandService.spec.ts b/apps/buddy/service/src/chat/__tests__/PreparedRunCommandService.spec.ts new file mode 100644 index 000000000..797cc3008 --- /dev/null +++ b/apps/buddy/service/src/chat/__tests__/PreparedRunCommandService.spec.ts @@ -0,0 +1,471 @@ +import type { DatabaseSync as SqliteDatabase } from 'node:sqlite' +import type { AppendBuddyRunEventInput } from '../../events/BuddyRunEvent' +import { chmodSync, existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { dirname, join } from 'node:path' +import process from 'node:process' +import { DatabaseSync } from 'node:sqlite' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { createBuddyUserContent } from '../../../../shared/conversation/buddyUserContent' +import { RunEventQueries } from '../../events/RunEventQueries' +import { createAttachmentPersistenceRepository } from '../../storage/attachmentPersistenceRepository' +import { createAttachmentRepository } from '../../storage/attachmentRepository' +import { createComposerDraftRepository } from '../../storage/composerDraftRepository' +import { createConversationRepository } from '../../storage/conversationRepository' +import { openBuddyMigrationCandidate, openBuddySqliteDatabase } from '../../storage/database' +import { guardStorageDatabase } from '../../storage/StorageIsolation' +import { readTransactionFailure, withTransaction } from '../../storage/transaction' +import { verifyCommandReceipt } from '../../storage/verifyCommandReceipt' +import { PreparedRunCommandService } from '../PreparedRunCommandService' + +const databases: SqliteDatabase[] = [] +const services: PreparedRunCommandService[] = [] +const directories: string[] = [] +const now = '2026-09-29T00:00:00.000Z' + +afterEach(() => { + vi.restoreAllMocks() + for (const service of services.splice(0)) + service.dispose() + for (const database of databases.splice(0)) { + if (database.isOpen) + database.close() + } + for (const directory of directories.splice(0)) + rmSync(directory, { recursive: true, force: true }) +}) + +function open(databasePath = ':memory:') { + const database = openBuddyMigrationCandidate({ databasePath }) + databases.push(database) + return database +} + +function service(database: SqliteDatabase, sourceInstanceId = 'instance-1') { + const instance = new PreparedRunCommandService({ database, sourceInstanceId }) + services.push(instance) + return instance +} + +function fixture(databasePath = ':memory:') { + const database = open(databasePath) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + const drafts = createComposerDraftRepository(database) + drafts.open({ + draftId: 'draft-1', + initialContent: createBuddyUserContent('Keep my input'), + initialExecutionConfig: { approvalPolicy: 'policy', executionProfile: 'workspace_write' }, + initialModelSelection: { modelId: 'fixture', providerId: 'fixture', reasoning: null, serviceTier: null }, + now, + scope: { kind: 'global' }, + }) + const command = { + dataGeneration: String(database.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation), + actorId: 'desktop-local', + commandId: 'command-1', + fingerprintVersion: 1, + fingerprint: 'prepared-intent-1', + } + return { database, drafts, command, owner: service(database), input: input() } +} + +function input() { + return { + attachmentBindings: [], + branchId: 'branch-1', + conversationId: 'conversation-1', + createdAt: now, + approvalPolicy: 'policy' as const, + executionProfile: 'workspace_write' as const, + draft: { draftId: 'draft-1', expectedRevision: 0 }, + model: 'fixture', + spaceId: null, + provider: 'fixture', + runInput: { attachmentIds: [], contextItems: [], prompt: 'Materialized input', reasoning: null, serviceTier: null }, + runId: 'run-1', + title: 'Keep my input', + userMessageContent: { text: 'Keep my input' }, + userMessageId: 'message-1', + } +} + +function temporaryPath() { + const directory = mkdtempSync(join(tmpdir(), 'lexora-atomic-command-')) + directories.push(directory) + return join(directory, 'candidate.sqlite3') +} + +function capture(operation: () => unknown): unknown { + try { + operation() + } + catch (error) { + return error + } + throw new Error('Expected operation to fail') +} + +function expectNoAcceptance(database: SqliteDatabase) { + for (const table of ['conversations', 'conversation_branches', 'messages', 'runs', 'run_inputs', 'turn_requests', 'run_events', 'command_receipts']) + expect(database.prepare(`SELECT count(*) AS count FROM ${table}`).get()).toEqual({ count: 0 }) + expect(createComposerDraftRepository(database).findById('draft-1')).toMatchObject({ revision: 0, content: createBuddyUserContent('Keep my input') }) +} + +function terminalEvents(): AppendBuddyRunEventInput[] { + return [ + { runId: 'run-1', type: 'run.started', payload: {}, createdAt: now }, + { runId: 'run-1', type: 'message.completed', payload: { messageId: 'answer-1', role: 'assistant', content: { text: 'Answer' }, stopReason: 'completed' }, createdAt: now }, + { runId: 'run-1', type: 'usage.recorded', payload: { + usageRecordId: 'usage-1', + sourceEntryId: 'source-1', + provider: 'fixture', + model: 'fixture', + purpose: 'turn', + inputTokens: 2, + outputTokens: 3, + cacheReadTokens: 0, + cacheWriteTokens: 0, + reasoningTokens: null, + totalTokens: 5, + inputCost: 0, + outputCost: 0, + cacheReadCost: 0, + cacheWriteCost: 0, + totalCost: 0, + }, createdAt: now }, + { runId: 'run-1', type: 'run.completed', payload: {}, createdAt: now }, + ] +} + +describe('prepared run command atomic persistence', () => { + it('refuses a pending migration instead of enabling normal admission', () => { + const database = open() + expect(() => service(database)).toThrow('authority is not ready') + expect(database.prepare('SELECT authority, migration_state FROM storage_profile').get()).toEqual({ authority: 'legacy_jsonl', migration_state: 'pending' }) + expect(database.prepare('SELECT count(*) AS count FROM command_receipts').get()).toEqual({ count: 0 }) + }) + + it('commits the input, draft, attachment binding, run fact and fixed receipt before publishing', () => { + const f = fixture() + const attachments = createAttachmentRepository(f.database) + const files = createAttachmentPersistenceRepository(f.database) + for (const path of ['/fixture/source.txt', '/fixture/message.txt']) + files.adopt({ path, sizeBytes: 1, sha256: 'a'.repeat(64) }) + attachments.create({ id: 'file-1', conversationId: null, draftId: 'draft-1', messageId: null, name: 'fixture.txt', mimeType: 'text/plain', sizeBytes: 1, storedPath: '/fixture/source.txt', createdAt: now }) + const delivered: string[] = [] + const observe = (kind: string) => { + expect(f.database.isTransaction).toBe(false) + expect(f.owner.find(f.command)?.target).toEqual({ runId: 'run-1', conversationId: 'conversation-1', branchId: 'branch-1' }) + expect(f.drafts.findById('draft-1')?.revision).toBe(1) + delivered.push(kind) + } + f.owner.onDidCommit(() => observe('turn')) + f.owner.events.onDidCommit((event) => { + observe(event.type) + }) + const accepted = f.owner.start(f.command, { + ...f.input, + runInput: { ...f.input.runInput, attachmentIds: ['file-1'] }, + attachmentBindings: [{ id: 'file-1', sourceAttachmentId: 'file-1', sourceDraftId: 'draft-1', messageId: 'message-1', storedPath: '/fixture/message.txt', createdAt: now }], + }) + expect(accepted).toMatchObject({ created: true, receipt: { + decision: 'accepted', + method: 'chat.startTurn', + sourceInstanceId: 'instance-1', + target: { runId: 'run-1', conversationId: 'conversation-1', branchId: 'branch-1' }, + result: { draftReceipt: { draftId: 'draft-1', sourceRevision: 0, committedRevision: 1 } }, + } }) + expect(delivered).toEqual(['turn', 'run.queued']) + expect(attachments.findById('file-1')).toMatchObject({ draftId: null, messageId: 'message-1' }) + expect(f.database.prepare('SELECT prompt, attachment_ids_json FROM run_inputs').get()).toEqual({ prompt: 'Materialized input', attachment_ids_json: '["file-1"]' }) + expect(f.database.prepare('SELECT status, next_event_sequence FROM runs').get()).toEqual({ status: 'queued', next_event_sequence: 2 }) + expect(new RunEventQueries(f.database).listForRuns(['run-1'])[0]).toMatchObject({ sequence: 1, type: 'run.queued' }) + }) + + it.each([ + ['run input', 'BEFORE INSERT ON run_inputs'], + ['command receipt', 'BEFORE INSERT ON command_receipts'], + ])('rolls back every accepted fact when %s fails, then accepts the original ID', (_name, trigger) => { + const f = fixture() + const delivered: unknown[] = [] + f.owner.onDidCommit(event => delivered.push(event)) + f.owner.events.onDidCommit(event => delivered.push(event)) + f.database.exec(`CREATE TRIGGER reject_commit ${trigger} BEGIN SELECT RAISE(ABORT, 'injected storage failure'); END`) + expect(() => f.owner.start(f.command, f.input)).toThrow('injected storage failure') + expectNoAcceptance(f.database) + expect(delivered).toEqual([]) + f.database.exec('DROP TRIGGER reject_commit') + expect(f.owner.start(f.command, f.input).created).toBe(true) + expect(delivered).toHaveLength(2) + }) + + it('participates in a larger owner transaction without publishing a receipt that is rolled back', () => { + const f = fixture() + const delivered: unknown[] = [] + f.owner.onDidCommit(event => delivered.push(event)) + f.owner.events.onDidCommit(event => delivered.push(event)) + expect(() => withTransaction(f.database, () => { + f.owner.start(f.command, f.input) + f.owner.events.appendBatch(terminalEvents()) + expect(delivered).toEqual([]) + expect(() => f.owner.find(f.command)).toThrow('active transaction') + throw new Error('outer owner rejected') + })).toThrow('outer owner rejected') + expectNoAcceptance(f.database) + expect(delivered).toEqual([]) + }) + + it.each(['edit', 'regenerate', 'compact'] as const)('commits %s through the same receipt boundary without damaging the source run', (method) => { + const f = fixture() + const first = f.owner.start(f.command, f.input) + f.owner.events.appendBatch(terminalEvents()) + f.database.exec('UPDATE runs SET pi_session_file = \'/fixture/session.jsonl\' WHERE id = \'run-1\'') + const command = { ...f.command, commandId: 'command-2', fingerprint: method } + const base = { ...f.input, branchId: 'branch-2', runId: 'run-2', parentBranchId: 'branch-1' } + let accept: () => ReturnType + if (method === 'regenerate') { + accept = () => f.owner.regenerate(command, { ...base, reasoning: null, serviceTier: null, expectedModelSelection: createConversationRepository(f.database).findById('conversation-1')!.modelSelection, sourceRunId: 'run-1', forkedFromMessageId: 'message-1' }) + } + else { + const opened = f.drafts.open({ + draftId: 'draft-2', + initialContent: createBuddyUserContent(method === 'edit' ? 'Edited input' : '/compact'), + initialExecutionConfig: { approvalPolicy: 'policy', executionProfile: 'workspace_write' }, + initialModelSelection: { modelId: 'fixture', providerId: 'fixture', reasoning: null, serviceTier: null }, + now, + scope: method === 'edit' + ? { kind: 'message_edit', conversationId: 'conversation-1', branchId: 'branch-1', userMessageId: 'message-1' } + : { kind: 'conversation_branch', conversationId: 'conversation-1', branchId: 'branch-1' }, + }) + const saved = f.drafts.save({ + draftId: opened.draftId, + expectedRevision: opened.revision, + content: createBuddyUserContent(method === 'edit' ? 'Edited input' : '/compact'), + executionConfig: opened.executionConfig, + modelSelection: opened.modelSelection, + now, + }) + const draft = { draftId: saved.draftId, expectedRevision: saved.revision } + accept = method === 'edit' + ? () => f.owner.edit(command, { ...base, draft, sourceUserMessageId: 'message-1', forkedFromMessageId: null, userMessageId: 'message-2' }) + : () => f.owner.compact(command, { ...base, branchId: 'branch-1', draft, command: 'compact', arguments: 'Preserve decisions' }) + } + const draftsBefore = f.database.prepare('SELECT * FROM composer_drafts ORDER BY id').all() + f.database.exec(`CREATE TRIGGER reject_receipt BEFORE INSERT ON command_receipts BEGIN SELECT RAISE(ABORT, 'receipt unavailable'); END`) + expect(accept).toThrow('receipt unavailable') + expect(f.database.prepare('SELECT id, status FROM runs').all()).toEqual([{ id: 'run-1', status: 'completed' }]) + expect(f.database.prepare('SELECT active_branch_id FROM conversations').get()).toEqual({ active_branch_id: 'branch-1' }) + expect(f.database.prepare('SELECT count(*) AS count FROM messages').get()).toEqual({ count: 2 }) + expect(f.database.prepare('SELECT * FROM composer_drafts ORDER BY id').all()).toEqual(draftsBefore) + f.database.exec('DROP TRIGGER reject_receipt') + const accepted = accept() + expect(accepted).toMatchObject({ created: true, receipt: { target: { runId: 'run-2' } } }) + expect(accept()).toEqual({ created: false, receipt: accepted.receipt }) + expect(f.owner.find(f.command)).toEqual(first.receipt) + expect(new RunEventQueries(f.database).listForRuns(['run-2'])).toMatchObject([{ sequence: 1, type: 'run.queued' }]) + expect(f.database.prepare('SELECT purpose, next_event_sequence FROM runs WHERE id = \'run-2\'').get()).toEqual({ purpose: method === 'compact' ? 'conversation.compaction' : 'chat', next_event_sequence: 2 }) + }) + + it('rolls back attachment ownership along with a failed command receipt', () => { + const f = fixture() + const attachments = createAttachmentRepository(f.database) + const files = createAttachmentPersistenceRepository(f.database) + for (const path of ['/fixture/source.txt', '/fixture/message.txt']) + files.adopt({ path, sizeBytes: 1, sha256: 'a'.repeat(64) }) + attachments.create({ id: 'file-1', conversationId: null, draftId: 'draft-1', messageId: null, name: 'fixture.txt', mimeType: 'text/plain', sizeBytes: 1, storedPath: '/fixture/source.txt', createdAt: now }) + const original = attachments.findById('file-1') + f.database.exec(`CREATE TRIGGER reject_receipt BEFORE INSERT ON command_receipts BEGIN SELECT RAISE(ABORT, 'receipt unavailable'); END`) + expect(() => f.owner.start(f.command, { + ...f.input, + runInput: { ...f.input.runInput, attachmentIds: ['file-1'] }, + attachmentBindings: [{ id: 'file-1', sourceAttachmentId: 'file-1', sourceDraftId: 'draft-1', messageId: 'message-1', storedPath: '/fixture/message.txt', createdAt: now }], + })).toThrow('receipt unavailable') + expect(attachments.findById('file-1')).toEqual(original) + expectNoAcceptance(f.database) + }) + + it('keeps the original receipt after restart and terminal state instead of consuming the draft again', () => { + const databasePath = temporaryPath() + const f = fixture(databasePath) + const accepted = f.owner.start(f.command, f.input) + f.owner.events.appendBatch(terminalEvents()) + f.owner.dispose() + f.database.close() + const database = open(databasePath) + const restarted = service(database, 'instance-2') + const replay = restarted.start(f.command, { ...f.input, runId: 'must-not-exist', draft: { draftId: 'deleted-draft', expectedRevision: 99 } }) + expect(replay).toEqual({ created: false, receipt: accepted.receipt }) + expect(database.prepare('SELECT id, status FROM runs').all()).toEqual([{ id: 'run-1', status: 'completed' }]) + expect(createComposerDraftRepository(database).findById('draft-1')?.revision).toBe(1) + expect(() => restarted.start({ ...f.command, fingerprint: 'different' }, f.input)).toThrow('recorded intent') + expect(() => restarted.start({ ...f.command, dataGeneration: 'old-generation' }, f.input)).toThrow('generation is not current') + database.exec('UPDATE storage_actors SET state = \'revoked\'') + expect(() => restarted.start(f.command, f.input)).toThrow('actor is not active') + }) + + it('retains SQLITE_FULL and restores the draft without leaving accepted rows', () => { + const databasePath = temporaryPath() + const f = fixture(databasePath) + const revisions = f.database.prepare('SELECT * FROM product_query_revisions ORDER BY table_name').all() + const pageCount = Number(f.database.prepare('PRAGMA page_count').get()!.page_count) + f.database.exec(`PRAGMA max_page_count = ${pageCount + 2}`) + const failure = capture(() => f.owner.start(f.command, { ...f.input, runInput: { ...f.input.runInput, prompt: 'x'.repeat(2_000_000) } })) + expect(failure).toMatchObject({ errcode: 13 }) + expect(readTransactionFailure(failure)).toEqual({ phase: 'operation', outcome: 'rolled_back' }) + const reopened = open(databasePath) + expectNoAcceptance(reopened) + expect(reopened.prepare('SELECT * FROM product_query_revisions ORDER BY table_name').all()).toEqual(revisions) + }) + + it('rolls back a real COMMIT BUSY without publishing accepted facts', () => { + const databasePath = temporaryPath() + const f = fixture(databasePath) + const isolation = guardStorageDatabase(f.database) + f.database.exec('PRAGMA journal_mode = DELETE; PRAGMA busy_timeout = 0') + const reader = new DatabaseSync(databasePath) + databases.push(reader) + reader.exec('BEGIN; SELECT * FROM composer_drafts') + const delivered: unknown[] = [] + f.owner.events.onDidCommit(event => delivered.push(event)) + const failure = capture(() => f.owner.start(f.command, f.input)) + expect(failure).toMatchObject({ errcode: 5 }) + expect(readTransactionFailure(failure)).toEqual({ phase: 'commit', outcome: 'rolled_back' }) + expectNoAcceptance(f.database) + expect(delivered).toEqual([]) + expect(isolation.failure).toBeNull() + reader.exec('ROLLBACK') + expect(f.owner.start(f.command, f.input).created).toBe(true) + }) + + it.skipIf(process.platform === 'win32' || process.getuid?.() === 0).each(['sqlite', 'incompatible'] as const)('isolates a real COMMIT journal deletion failure and validates %s recovery before writing', (mode) => { + const databasePath = temporaryPath() + const f = fixture(databasePath) + const isolation = guardStorageDatabase(f.database) + f.database.exec('PRAGMA journal_mode = DELETE') + if (mode === 'incompatible') + f.database.exec('PRAGMA user_version = 999') + expect(f.database.prepare('PRAGMA synchronous').get()).toEqual({ synchronous: 2 }) + const exec = f.database.exec.bind(f.database) + let physicalError: unknown + const injected = vi.spyOn(f.database, 'exec').mockImplementation((sql) => { + if (sql === 'COMMIT') + chmodSync(dirname(databasePath), 0o500) + try { + exec(sql) + } + catch (error) { + if (sql === 'COMMIT') + physicalError = error + throw error + } + }) + const delivered: unknown[] = [] + f.owner.onDidCommit(event => delivered.push(event)) + f.owner.events.onDidCommit(event => delivered.push(event)) + try { + const failure = capture(() => f.owner.start(f.command, f.input)) + expect(physicalError).toMatchObject({ errcode: 2570 }) + expect(failure).toMatchObject({ code: 'STORAGE_TRANSACTION_UNCERTAIN', cause: physicalError }) + expect(readTransactionFailure(failure)).toMatchObject({ phase: 'commit', outcome: 'unknown' }) + expect(isolation.failure?.cause).toBe(physicalError) + expect(delivered).toEqual([]) + expect(() => f.owner.start(f.command, f.input)).toThrow('requires verification') + expect(() => f.owner.find(f.command)).toThrow('requires verification') + expect(() => f.database.prepare('SELECT * FROM command_receipts')).toThrow('Storage is isolated') + injected.mockRestore() + f.database.close() + const journal = `${databasePath}-journal` + const databaseBytes = readFileSync(databasePath) + const journalBytes = readFileSync(journal) + expect(capture(() => verifyCommandReceipt({ databasePath, identity: f.command }))).toMatchObject({ errcode: 776 }) + expect(readFileSync(databasePath)).toEqual(databaseBytes) + expect(readFileSync(journal)).toEqual(journalBytes) + } + finally { + injected.mockRestore() + chmodSync(dirname(databasePath), 0o700) + } + if (mode === 'incompatible') { + const bytes = readFileSync(databasePath) + const journal = readFileSync(`${databasePath}-journal`) + for (const opener of [openBuddyMigrationCandidate, openBuddySqliteDatabase]) + expect(() => opener({ databasePath })).toThrow('newer schema version') + expect(readFileSync(databasePath)).toEqual(bytes) + expect(readFileSync(`${databasePath}-journal`)).toEqual(journal) + return + } + const database = openBuddySqliteDatabase({ databasePath }) + databases.push(database) + guardStorageDatabase(database) + expectNoAcceptance(database) + expect(existsSync(`${databasePath}-journal`)).toBe(false) + expect(verifyCommandReceipt({ databasePath, identity: f.command })).toBeNull() + expect(database.prepare('PRAGMA integrity_check').get()).toEqual({ integrity_check: 'ok' }) + expect(database.prepare('PRAGMA foreign_key_check').all()).toEqual([]) + const restarted = service(database, 'instance-2') + const accepted = restarted.start(f.command, f.input) + expect(accepted).toMatchObject({ created: true, receipt: { target: { runId: 'run-1' } } }) + expect(restarted.start(f.command, f.input)).toEqual({ created: false, receipt: accepted.receipt }) + expect(verifyCommandReceipt({ databasePath, identity: f.command })).toEqual(accepted.receipt) + expect(database.prepare('SELECT count(*) AS count FROM runs').get()).toEqual({ count: 1 }) + expect(createComposerDraftRepository(database).findById('draft-1')?.revision).toBe(1) + }) + + it('returns no early success after an uncertain COMMIT and recovers the fixed receipt on reopen', () => { + const databasePath = temporaryPath() + const f = fixture(databasePath) + const isolation = guardStorageDatabase(f.database) + const exec = f.database.exec.bind(f.database) + const failure = new Error('COMMIT response lost') + const injected = vi.spyOn(f.database, 'exec').mockImplementation((sql) => { + exec(sql) + if (sql === 'COMMIT') + throw failure + }) + const delivered: unknown[] = [] + f.owner.events.onDidCommit(event => delivered.push(event)) + expect(capture(() => f.owner.start(f.command, f.input))).toMatchObject({ code: 'STORAGE_TRANSACTION_UNCERTAIN', cause: failure }) + expect(delivered).toEqual([]) + expect(() => f.owner.start(f.command, f.input)).toThrow('requires verification') + expect(() => f.owner.find(f.command)).toThrow('requires verification') + const receipt = verifyCommandReceipt({ databasePath, identity: f.command }) + expect(receipt).toMatchObject({ target: { runId: 'run-1' }, sourceInstanceId: 'instance-1' }) + expect(() => isolation.assertAvailable()).toThrow('Storage is isolated') + expect(() => f.database.prepare('DELETE FROM runs')).toThrow('Storage is isolated') + injected.mockRestore() + f.database.close() + const database = open(databasePath) + const restored = service(database, 'instance-2').start(f.command, f.input) + expect(restored).toMatchObject({ created: false, receipt: { target: { runId: 'run-1' }, sourceInstanceId: 'instance-1' } }) + expect(restored.receipt).toEqual(receipt) + expect(database.prepare('SELECT count(*) AS count FROM runs').get()).toEqual({ count: 1 }) + }) + + it('does not expose uncommitted receipts when COMMIT and cleanup both fail', () => { + const databasePath = temporaryPath() + const f = fixture(databasePath) + guardStorageDatabase(f.database) + const exec = f.database.exec.bind(f.database) + const injected = vi.spyOn(f.database, 'exec').mockImplementation((sql) => { + if (sql === 'COMMIT' || sql === 'ROLLBACK') + throw new Error(`Unavailable ${sql}`) + exec(sql) + }) + const failure = capture(() => f.owner.start(f.command, f.input)) + expect(readTransactionFailure(failure)).toMatchObject({ phase: 'commit', outcome: 'unknown' }) + expect(f.database.isTransaction).toBe(true) + expect(() => f.database.prepare('SELECT count(*) AS count FROM command_receipts')).toThrow('Storage is isolated') + expect(() => f.owner.find(f.command)).toThrow('requires verification') + expect(verifyCommandReceipt({ databasePath, identity: f.command })).toBeNull() + const reader = new DatabaseSync(databasePath, { readOnly: true }) + databases.push(reader) + expectNoAcceptance(reader) + injected.mockRestore() + exec('ROLLBACK') + f.database.close() + const reopened = open(databasePath) + expectNoAcceptance(reopened) + expect(verifyCommandReceipt({ databasePath, identity: f.command })).toBeNull() + expect(service(reopened, 'instance-2').start(f.command, f.input).created).toBe(true) + }) +}) diff --git a/apps/buddy/service/src/chat/__tests__/persistPreparedTurn.spec.ts b/apps/buddy/service/src/chat/__tests__/persistPreparedTurn.spec.ts deleted file mode 100644 index 2fdcce8bf..000000000 --- a/apps/buddy/service/src/chat/__tests__/persistPreparedTurn.spec.ts +++ /dev/null @@ -1,61 +0,0 @@ -import type { TurnRequestRecord } from '../../storage/turnRequestRepository' -import type { PreparedTurnAttachments } from '../persistPreparedTurn' -import { describe, expect, it } from 'vitest' -import { persistPreparedTurn } from '../persistPreparedTurn' - -describe('persistPreparedTurn', () => { - it('commits the winning request and rolls back an unreferenced staged copy', async () => { - let winningState = 'staged' - const winning = await persistPreparedTurn(attachments({ - commit: () => { winningState = 'committed' }, - rollback: () => { winningState = 'rolled-back' }, - }), () => request(true)) - - let losingState = 'staged' - const losing = await persistPreparedTurn(attachments({ - commit: () => { losingState = 'committed' }, - rollback: () => { losingState = 'rolled-back' }, - }), () => request(false)) - - expect(winning.created).toBe(true) - expect(winningState).toBe('committed') - expect(losing.created).toBe(false) - expect(losingState).toBe('rolled-back') - }) - - it('rolls back staged files when product persistence fails', async () => { - let state = 'staged' - await expect(persistPreparedTurn(attachments({ - commit: () => { state = 'committed' }, - rollback: () => { state = 'rolled-back' }, - }), () => { - throw new Error('persistence failed') - })).rejects.toThrow('persistence failed') - - expect(state).toBe('rolled-back') - }) -}) - -function attachments(options: { - commit: () => void - rollback: () => void -}): PreparedTurnAttachments { - return { - bindings: [], - validate: () => {}, - commit: async () => options.commit(), - rollback: async () => options.rollback(), - } -} - -function request(created: boolean): TurnRequestRecord { - return { - branchId: 'branch-1', - conversationId: 'conversation-1', - created, - draftReceipt: null, - requestFingerprint: 'fingerprint-1', - requestId: 'request-1', - runId: 'run-1', - } -} diff --git a/apps/buddy/service/src/chat/__tests__/sessionReferenceAuthorization.spec.ts b/apps/buddy/service/src/chat/__tests__/sessionReferenceAuthorization.spec.ts index a0f278c24..8bdda576f 100644 --- a/apps/buddy/service/src/chat/__tests__/sessionReferenceAuthorization.spec.ts +++ b/apps/buddy/service/src/chat/__tests__/sessionReferenceAuthorization.spec.ts @@ -1,5 +1,5 @@ import type { ConversationRecord } from '../../storage/conversationRecord' -import { describe, expect, it, vi } from 'vitest' +import { describe, expect, it } from 'vitest' import { resolveAuthorizedSessionReferences } from '../sessionReferenceAuthorization' function conversation(id: string, spaceId: string | null, title: string | null = 'Session'): ConversationRecord { @@ -9,8 +9,6 @@ function conversation(id: string, spaceId: string | null, title: string | null = spaceId, title, deletedAt: null, - archivedAt: null, - metadataRevision: 0, activeBranchId: 'branch', approvalPolicy: 'policy', createdAt: '2026-01-01T00:00:00.000Z', @@ -22,19 +20,16 @@ function conversation(id: string, spaceId: string | null, title: string | null = } describe('session reference authorization', () => { - it('allows a global session and replaces its clipboard title with the stored title', () => { - const findById = vi.fn(() => conversation('global', null, 'Real\nTitle')) - expect(resolveAuthorizedSessionReferences([{ id: 'global', title: 'Forged title' }], { findById })) - .toEqual([{ id: 'global', title: 'Real Title' }]) - }) - - it('allows a session from a different space', () => { - const findById = vi.fn(() => conversation('scoped', 'space-b')) - expect(resolveAuthorizedSessionReferences([{ id: 'scoped', title: 'Session' }], { findById })).toHaveLength(1) + it('allows global and cross-space references using stored titles instead of clipboard titles', () => { + const findById = (id: string) => conversation(id, id === 'global' ? null : 'space-b', 'Real\nTitle') + expect(resolveAuthorizedSessionReferences([ + { id: 'global', title: 'Forged title' }, + { id: 'scoped', title: 'Forged title' }, + ], { findById })).toEqual([{ id: 'global', title: 'Real Title' }, { id: 'scoped', title: 'Real Title' }]) }) it('rejects deleted or missing sessions and silently deduplicates references', () => { - const findById = vi.fn((id: string) => id === 'missing' ? null : { ...conversation(id, null), deletedAt: id === 'deleted' ? '2026-01-01T00:00:00.000Z' : null }) + const findById = (id: string) => id === 'missing' ? null : { ...conversation(id, null), deletedAt: id === 'deleted' ? '2026-01-01T00:00:00.000Z' : null } expect(resolveAuthorizedSessionReferences([ { id: 'ok', title: 'Old title' }, { id: 'ok', title: 'Duplicate' }, diff --git a/apps/buddy/service/src/chat/persistPreparedTurn.ts b/apps/buddy/service/src/chat/persistPreparedTurn.ts index fa51a5174..e03dfc194 100644 --- a/apps/buddy/service/src/chat/persistPreparedTurn.ts +++ b/apps/buddy/service/src/chat/persistPreparedTurn.ts @@ -1,13 +1,14 @@ import type { PreparedMessageAttachments } from '../attachments/AttachmentService' import type { PreparedComposerInput } from '../attachments/ComposerResourceService' import type { TurnRequestRecord } from '../storage/turnRequestRepository' +import { PreparedResourceSettlement } from './PreparedResourceSettlement' export interface PreparedTurnAttachments extends PreparedMessageAttachments { validate: () => void } export function combinePreparedAttachments(attachments: PreparedMessageAttachments, resources: PreparedComposerInput): PreparedTurnAttachments { - const settle = async (outcome: 'commit' | 'rollback') => { + const settle = async (outcome: 'commit' | 'rollback' | 'retain') => { const failures: unknown[] = [] for (const receipt of [attachments, resources]) { try { @@ -18,25 +19,26 @@ export function combinePreparedAttachments(attachments: PreparedMessageAttachmen if (failures.length) throw new AggregateError(failures, 'TURN_ATTACHMENT_CLEANUP_FAILED') } - return Object.freeze({ bindings: attachments.bindings, validate: resources.validate, commit: () => settle('commit'), rollback: () => settle('rollback') }) + return Object.freeze({ bindings: attachments.bindings, validate: resources.validate, commit: () => settle('commit'), rollback: () => settle('rollback'), retain: () => settle('retain') }) } export async function persistPreparedTurn( attachments: PreparedTurnAttachments | null, persist: () => TurnRequestRecord, ): Promise { + const resources = new PreparedResourceSettlement(attachments) let prepared: TurnRequestRecord try { attachments?.validate() prepared = persist() } catch (error) { - await attachments?.rollback() + await resources.fail(error) throw error } if (prepared.created) - await attachments?.commit().catch(() => undefined) + await resources.commit() else - await attachments?.rollback().catch(() => undefined) + await resources.rollback() return prepared } diff --git a/apps/buddy/service/src/chat/resolveChatInput.ts b/apps/buddy/service/src/chat/resolveChatInput.ts new file mode 100644 index 000000000..5672741fa --- /dev/null +++ b/apps/buddy/service/src/chat/resolveChatInput.ts @@ -0,0 +1,24 @@ +import type { SubmittedInput } from '../../../shared/conversation/submittedInput' +import type { ComposerDraftRepository } from '../storage/composerDraftRepository' +import { createHash } from 'node:crypto' +import { BuddyServiceError } from '../rpc/runtimeRequest' + +export interface TrustedSubmittedInput extends SubmittedInput { + resourceOwnerId: string +} + +export type ChatInputSource = { draftId: string, expectedRevision: number } | { snapshot: TrustedSubmittedInput } + +export function inputResourceOwner(actorId: string, inputId: string): string { + return `input-${createHash('sha256').update(JSON.stringify([actorId, inputId])).digest('hex')}` +} + +export function resolveChatInput(source: ChatInputSource, drafts: Pick) { + if ('snapshot' in source) { + return { ...source.snapshot, draftId: source.snapshot.resourceOwnerId, revision: 0, commit: null, legacyResourceDraftId: source.snapshot.legacyResourceDraftId } + } + const draft = drafts.findById(source.draftId) + if (!draft || draft.revision !== source.expectedRevision) + throw new BuddyServiceError('DRAFT_CONFLICT') + return { ...draft, commit: { draftId: source.draftId, expectedRevision: source.expectedRevision }, legacyResourceDraftId: undefined } +} diff --git a/apps/buddy/service/src/commands/CommandAdmissionError.ts b/apps/buddy/service/src/commands/CommandAdmissionError.ts new file mode 100644 index 000000000..c303a11e6 --- /dev/null +++ b/apps/buddy/service/src/commands/CommandAdmissionError.ts @@ -0,0 +1,27 @@ +export type CommandAdmissionErrorCode + = | 'COMMAND_ID_REUSED' + | 'COMMAND_FINGERPRINT_UNSUPPORTED' + | 'COMMAND_ACTOR_UNAVAILABLE' + | 'DATA_GENERATION_CHANGED' + | 'SERVICE_INSTANCE_CHANGED' + | 'SERVICE_STOPPING' + +export class CommandAdmissionError extends Error { + readonly code: CommandAdmissionErrorCode + + constructor(code: CommandAdmissionErrorCode, message: string = code) { + super(message) + this.code = code + this.name = 'CommandAdmissionError' + } +} + +export class CommandResultUnknownError extends Error { + readonly code = 'COMMAND_RESULT_UNKNOWN' + readonly acceptance = 'unknown' + + constructor(cause: unknown) { + super('The command decision cannot be determined from this storage connection', { cause }) + this.name = 'CommandResultUnknownError' + } +} diff --git a/apps/buddy/service/src/commands/CommandAdmissionService.ts b/apps/buddy/service/src/commands/CommandAdmissionService.ts new file mode 100644 index 000000000..f65cd71bc --- /dev/null +++ b/apps/buddy/service/src/commands/CommandAdmissionService.ts @@ -0,0 +1,187 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { CommandDecision, CommandDecisionOutcome, CommandReceipt, CommandReceiptSnapshot, CommandRejection, StorageCommand } from '../storage/CommandCommitter' +import type { InputCommandIntent, InputCommandMethod, ProductCommandFingerprint } from './commandIntent' +import { z } from 'zod' +import { copyEventSnapshot } from '../../../shared/events/eventSnapshot' +import { assertSameCommand, CommandCommitter } from '../storage/CommandCommitter' +import { requireSqliteAuthority } from '../storage/sqliteAuthority' +import { getStorageIsolation } from '../storage/StorageIsolation' +import { assertCommittedRead, readTransactionFailure } from '../storage/transaction' +import { CommandAdmissionError, CommandResultUnknownError } from './CommandAdmissionError' +import { COMMAND_FINGERPRINT_VERSION, fingerprintCommandIntent, PRODUCT_COMMAND_FINGERPRINT_VERSION, productCommandFingerprint } from './commandIntent' + +const id = z.string().min(1).max(256) +const identitySchema = z.object({ dataGeneration: id, commandId: id }).strict() +const submissionSchema = identitySchema.extend({ expectedServiceInstanceId: id, intent: z.unknown() }).strict() + +export interface TrustedCommandPrincipal { + readonly actorId: string + assertAuthenticated: () => void +} + +export interface CommandResolution { + created: boolean + receipt: CommandReceiptSnapshot +} + +export type CommandLookup + = | { lookup: 'decided', receipt: CommandReceiptSnapshot } + | { lookup: 'pending' | 'not_found' } + +export function createLocalCommandPrincipal(database: DatabaseSync): TrustedCommandPrincipal { + assertCommittedRead(database) + requireSqliteAuthority(database) + const actorId = database.prepare('SELECT local_actor_id FROM storage_profile WHERE id = 1').get()?.local_actor_id + if (typeof actorId !== 'string' || !actorId) + throw new CommandAdmissionError('COMMAND_ACTOR_UNAVAILABLE') + return Object.freeze({ + actorId, + assertAuthenticated() { + const current = database.prepare('SELECT local_actor_id FROM storage_profile WHERE id = 1').get()?.local_actor_id + if (current !== actorId) + throw new CommandAdmissionError('COMMAND_ACTOR_UNAVAILABLE') + }, + }) +} + +export class CommandAdmissionService { + readonly #database: DatabaseSync + readonly #principal: TrustedCommandPrincipal + readonly #sourceInstanceId: string + readonly #committer: CommandCommitter + readonly #stopping = new AbortController() + readonly #pending = new Map }>() + + constructor(options: { database: DatabaseSync, principal: TrustedCommandPrincipal, sourceInstanceId: string }) { + this.#database = options.database + this.#principal = options.principal + this.#sourceInstanceId = id.parse(options.sourceInstanceId) + this.#committer = new CommandCommitter(options.database) + } + + get hasPendingOperations(): boolean { + return this.#pending.size > 0 + } + + lookup(input: unknown): CommandLookup { + const identity = identitySchema.parse(input) + const receipt = this.#read(identity) + if (receipt) + return { lookup: 'decided', receipt } + return { lookup: this.#pending.has(this.#key(identity)) ? 'pending' : 'not_found' } + } + + async execute( + method: Method, + input: unknown, + handlers: { + prepare: (intent: Readonly>, signal: AbortSignal) => Promise + scope?: (intent: Readonly>, operation: () => Promise) => Promise + reject?: (error: unknown) => CommandRejection | null + validate?: (prepared: Prepared, command: Readonly) => CommandRejection | null + commit: (prepared: Prepared, command: Readonly) => CommandDecision + }, + ): Promise { + const submission = submissionSchema.parse(input) + const intent = fingerprintCommandIntent(method, submission.intent) + const fingerprint = (input as { [productCommandFingerprint]?: ProductCommandFingerprint })[productCommandFingerprint] ?? intent + const command: StorageCommand = { + dataGeneration: submission.dataGeneration, + actorId: this.#principal.actorId, + commandId: submission.commandId, + method, + fingerprintVersion: fingerprint.version, + fingerprint: fingerprint.fingerprint, + sourceInstanceId: this.#sourceInstanceId, + } + const previous = this.#read(command) + if (previous) { + this.#assertSameIntent(previous, command) + return { created: false, receipt: previous } + } + this.#assertAdmission(submission.expectedServiceInstanceId) + const key = this.#key(command) + const waiting = this.#pending.get(key) + if (waiting) { + assertSameCommand(waiting.command, command) + const result = await waiting.result + return { created: false, receipt: result.receipt } + } + const snapshot = copyEventSnapshot(intent.intent as unknown) as Readonly> + const context = Object.freeze(command) + const admit = (operation: () => CommandDecisionOutcome) => { + try { + this.#principal.assertAuthenticated() + return this.#committer.decide(command, () => { + this.#principal.assertAuthenticated() + this.#assertAdmission(submission.expectedServiceInstanceId) + return operation() + }) + } + catch (cause) { + if (getStorageIsolation(this.#database).failure || readTransactionFailure(cause)?.outcome === 'unknown') + throw new CommandResultUnknownError(cause) + throw cause + } + } + const operation = async () => { + this.#assertAdmission(submission.expectedServiceInstanceId) + const prepared = await handlers.prepare(snapshot, this.#stopping.signal) + return admit(() => { + const rejection = handlers.validate?.(prepared, context) + return rejection ? { ...rejection, decision: 'rejected' } : { ...handlers.commit(prepared, context), decision: 'accepted' } + }) + } + const result = Promise.resolve().then(() => handlers.scope ? handlers.scope(snapshot, operation) : operation()).catch((error) => { + if (error instanceof CommandResultUnknownError || getStorageIsolation(this.#database).failure || readTransactionFailure(error)?.outcome === 'unknown') + throw error + const rejection = handlers.reject?.(error) + if (!rejection) + throw error + return admit(() => ({ ...rejection, decision: 'rejected' })) + }).finally(() => { + if (this.#pending.get(key)?.result === result) + this.#pending.delete(key) + }) + this.#pending.set(key, { command, result }) + return result + } + + stopAccepting(): void { + this.#stopping.abort(new CommandAdmissionError('SERVICE_STOPPING')) + } + + async dispose(): Promise { + this.stopAccepting() + await Promise.allSettled([...this.#pending.values()].map(pending => pending.result)) + } + + #assertAdmission(expectedInstance: string): void { + if (this.#stopping.signal.aborted || this.#database.prepare('SELECT 1 FROM service_admission_gates WHERE source_instance_id = ?').get(this.#sourceInstanceId)) + throw new CommandAdmissionError('SERVICE_STOPPING') + if (expectedInstance !== this.#sourceInstanceId) + throw new CommandAdmissionError('SERVICE_INSTANCE_CHANGED') + } + + #read(identity: { dataGeneration: string, commandId: string }) { + try { + this.#principal.assertAuthenticated() + return this.#committer.find({ dataGeneration: identity.dataGeneration, commandId: identity.commandId, actorId: this.#principal.actorId }) + } + catch (cause) { + if (cause instanceof CommandAdmissionError) + throw cause + throw new CommandResultUnknownError(cause) + } + } + + #assertSameIntent(receipt: Pick, command: StorageCommand): void { + if (receipt.fingerprintVersion !== COMMAND_FINGERPRINT_VERSION && receipt.fingerprintVersion !== PRODUCT_COMMAND_FINGERPRINT_VERSION) + throw new CommandAdmissionError('COMMAND_FINGERPRINT_UNSUPPORTED') + assertSameCommand(receipt, command) + } + + #key(identity: { dataGeneration: string, commandId: string }): string { + return JSON.stringify([identity.dataGeneration, this.#principal.actorId, identity.commandId]) + } +} diff --git a/apps/buddy/service/src/commands/DesktopOpenCommandService.ts b/apps/buddy/service/src/commands/DesktopOpenCommandService.ts new file mode 100644 index 000000000..b32fe77a0 --- /dev/null +++ b/apps/buddy/service/src/commands/DesktopOpenCommandService.ts @@ -0,0 +1,34 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { RuntimePlatform } from '../platform/RuntimePlatform' +import type { CommandAdmissionService } from './CommandAdmissionService' +import { ProductProtocolError } from '../../../shared/productProtocol/errors' +import { desktopOpenResultSchema } from '../../../shared/service/localServiceProtocol' + +interface Options { database: DatabaseSync, admission: CommandAdmissionService, openDesktop: RuntimePlatform['openDesktop'] } + +export class DesktopOpenCommandService { + readonly options: Options + constructor(options: Options) { + this.options = options + } + + execute(input: unknown) { + return this.options.admission.execute('desktop.open', input, { + prepare: async (intent, signal) => { + if (intent.kind === 'tasks' && intent.conversationId && !this.options.database.prepare('SELECT 1 FROM conversations WHERE id = ? AND deleted_at IS NULL').get(intent.conversationId)) + throw new ProductProtocolError('OBJECT_DELETED') + if (!this.options.openDesktop) + throw new ProductProtocolError('DESKTOP_UNAVAILABLE') + return { intent, result: desktopOpenResultSchema.parse(await this.options.openDesktop(intent, signal)) } + }, + validate: prepared => prepared.result.accepted ? null : { errorCode: 'DESKTOP_UNAVAILABLE', target: {}, result: { desktop: prepared.result } }, + commit: (prepared) => { + const target: Record = {} + if (prepared.intent.kind === 'tasks' && prepared.intent.conversationId) + target.conversationId = prepared.intent.conversationId + return { target, result: { desktop: prepared.result } } + }, + reject: error => error instanceof ProductProtocolError && (error.code === 'OBJECT_DELETED' || error.code === 'DESKTOP_UNAVAILABLE') ? { errorCode: error.code, target: {}, result: {} } : null, + }) + } +} diff --git a/apps/buddy/service/src/commands/__tests__/CommandAdmissionService.spec.ts b/apps/buddy/service/src/commands/__tests__/CommandAdmissionService.spec.ts new file mode 100644 index 000000000..3da09b1b3 --- /dev/null +++ b/apps/buddy/service/src/commands/__tests__/CommandAdmissionService.spec.ts @@ -0,0 +1,171 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { TrustedCommandPrincipal } from '../CommandAdmissionService' +import { mkdtempSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { CommandCommitter } from '../../storage/CommandCommitter' +import { openBuddyMigrationCandidate } from '../../storage/database' +import { guardStorageDatabase } from '../../storage/StorageIsolation' +import { CommandAdmissionError } from '../CommandAdmissionError' +import { CommandAdmissionService, createLocalCommandPrincipal } from '../CommandAdmissionService' + +const databases: DatabaseSync[] = [] +const directories: string[] = [] +afterEach(() => { + vi.restoreAllMocks() + for (const database of databases.splice(0)) { + if (database.isOpen) + database.close() + } + for (const path of directories.splice(0)) rmSync(path, { recursive: true, force: true }) +}) +function fixture(file = false) { + const directory = file ? mkdtempSync(join(tmpdir(), 'lexora-command-admission-')) : null + if (directory) + directories.push(directory) + const databasePath = directory ? join(directory, 'candidate.sqlite3') : ':memory:' + const database = openBuddyMigrationCandidate({ databasePath }) + databases.push(database) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'; CREATE TABLE effects (id TEXT PRIMARY KEY) STRICT') + const dataGeneration = String(database.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation) + const principal = createLocalCommandPrincipal(database) + const createService = (sourceInstanceId = 'instance-1', actor: TrustedCommandPrincipal = principal) => new CommandAdmissionService({ database, principal: actor, sourceInstanceId }) + const service = createService() + const submission = { dataGeneration, commandId: 'command-1', expectedServiceInstanceId: 'instance-1', intent: { draftId: 'draft-1', expectedRevision: 2 } } + const identity = { dataGeneration, commandId: 'command-1' } + const commit = () => { + database.prepare('INSERT INTO effects VALUES (?)').run('original-effect') + return { target: { runId: 'original-run' }, result: { acceptedRevision: 3 } } + } + return { database, databasePath, dataGeneration, principal, service, createService, submission, identity, commit } +} +function deferred() { + let resolve!: () => void + const promise = new Promise((done) => { + resolve = done + }) + return { promise, resolve } +} + +describe('trusted command admission', () => { + it('looks up immutable original decisions before stale drafts, restart instance checks and stopping', async () => { + const f = fixture() + const accepted = await f.service.execute('chat.startTurn', f.submission, { prepare: async () => {}, commit: f.commit }) + f.database.exec('DELETE FROM effects') + const restarted = f.createService('instance-2') + restarted.stopAccepting() + const duplicate = await restarted.execute('chat.startTurn', f.submission, { prepare: async () => { + throw new Error('draft was deleted') + }, commit: f.commit }) + expect(duplicate).toEqual({ created: false, receipt: accepted.receipt }) + expect(f.service.lookup(f.identity)).toEqual({ lookup: 'decided', receipt: accepted.receipt }) + await expect(restarted.execute('chat.startTurn', { ...f.submission, commandId: 'absent' }, { prepare: async () => {}, commit: f.commit })).rejects.toMatchObject({ code: 'SERVICE_STOPPING' }) + await expect(f.createService('instance-2').execute('chat.startTurn', { ...f.submission, commandId: 'absent' }, { prepare: async () => {}, commit: f.commit })).rejects.toMatchObject({ code: 'SERVICE_INSTANCE_CHANGED' }) + }) + + it('reports only live preparation as pending and coalesces identical attempts', async () => { + const f = fixture() + const gate = deferred() + expect(f.service.lookup(f.identity)).toEqual({ lookup: 'not_found' }) + const first = f.service.execute('chat.startTurn', f.submission, { prepare: async () => gate.promise, commit: f.commit }) + expect(f.service.lookup(f.identity)).toEqual({ lookup: 'pending' }) + const duplicate = f.service.execute('chat.startTurn', f.submission, { prepare: async () => { + throw new Error('must reuse original preparation') + }, commit: f.commit }) + await expect(f.service.execute('chat.startTurn', { ...f.submission, intent: { ...f.submission.intent, expectedRevision: 3 } }, { prepare: async () => {}, commit: f.commit })).rejects.toMatchObject({ code: 'COMMAND_ID_REUSED' }) + gate.resolve() + const accepted = await first + expect(await duplicate).toEqual({ created: false, receipt: accepted.receipt }) + expect(f.database.prepare('SELECT * FROM effects').all()).toEqual([{ id: 'original-effect' }]) + await expect(f.service.execute('chat.executeCommand', f.submission, { prepare: async () => {}, commit: f.commit })).rejects.toMatchObject({ code: 'COMMAND_ID_REUSED' }) + }) + + it('keeps same command IDs isolated by authenticated actor and rejects revoked or wrong generations before lookup', async () => { + const f = fixture() + await f.service.execute('chat.startTurn', f.submission, { prepare: async () => {}, commit: f.commit }) + f.database.exec('INSERT INTO storage_actors VALUES (\'device-1\', \'device\', \'active\', \'2026-01-01T00:00:00.000Z\')') + const device = f.createService('instance-1', { actorId: 'device-1', assertAuthenticated() {} }) + expect(device.lookup(f.identity)).toEqual({ lookup: 'not_found' }) + const own = await device.execute('chat.startTurn', f.submission, { prepare: async () => {}, commit: () => ({ target: { runId: 'device-run' }, result: {} }) }) + expect(own.receipt.actorId).toBe('device-1') + f.database.exec('UPDATE storage_actors SET state = \'revoked\' WHERE id = \'device-1\'') + expect(() => device.lookup(f.identity)).toThrowError(expect.objectContaining({ code: 'COMMAND_ACTOR_UNAVAILABLE' })) + expect(() => f.service.lookup({ ...f.identity, dataGeneration: 'stale-generation' })).toThrowError(expect.objectContaining({ code: 'DATA_GENERATION_CHANGED' })) + expect(() => f.service.lookup({ ...f.identity, actorId: 'device-1' })).toThrow() + }) + + it('persists explicit business rejection atomically and never converts arbitrary failures to receipts', async () => { + const f = fixture() + const rejected = await f.service.execute('chat.startTurn', f.submission, { prepare: async () => {}, validate: () => ({ errorCode: 'DRAFT_CONFLICT', target: { draftId: 'draft-1' }, result: { actualRevision: 3 } }), commit: f.commit }) + expect(rejected.receipt).toMatchObject({ decision: 'rejected', errorCode: 'DRAFT_CONFLICT', result: { actualRevision: 3 } }) + expect(f.database.prepare('SELECT * FROM effects').all()).toEqual([]) + expect(await f.service.execute('chat.startTurn', f.submission, { prepare: async () => {}, commit: f.commit })).toEqual({ created: false, receipt: rejected.receipt }) + const failure = new Error('storage write failed') + await expect(f.service.execute('chat.startTurn', { ...f.submission, commandId: 'failed' }, { prepare: async () => {}, commit: () => { + f.commit() + throw failure + } })).rejects.toBe(failure) + expect(f.service.lookup({ ...f.identity, commandId: 'failed' })).toEqual({ lookup: 'not_found' }) + expect(f.database.prepare('SELECT * FROM effects').all()).toEqual([]) + }) + + it('records an explicit commit conflict only after all attempted business changes are rolled back', async () => { + const f = fixture() + const conflict = new Error('draft conflict') + const result = await f.service.execute('chat.startTurn', f.submission, { + prepare: async () => {}, + commit: () => { + f.commit() + throw conflict + }, + reject: error => error === conflict ? { errorCode: 'DRAFT_CONFLICT', target: { draftId: 'draft-1' }, result: {} } : null, + }) + expect(result.receipt).toMatchObject({ decision: 'rejected', errorCode: 'DRAFT_CONFLICT' }) + expect(f.database.prepare('SELECT * FROM effects').all()).toEqual([]) + expect(f.service.lookup(f.identity)).toMatchObject({ lookup: 'decided', receipt: result.receipt }) + }) + + it.each(['generation', 'actor'] as const)('rechecks %s after preparation inside final acceptance', async (change) => { + const f = fixture() + const gate = deferred() + const execution = f.service.execute('chat.startTurn', f.submission, { prepare: async () => gate.promise, commit: f.commit }) + const expected = expect(execution).rejects.toMatchObject({ code: change === 'generation' ? 'DATA_GENERATION_CHANGED' : 'COMMAND_ACTOR_UNAVAILABLE' }) + if (change === 'generation') + f.database.exec('UPDATE storage_profile SET data_generation = \'replacement\'') + if (change === 'actor') + f.database.exec('UPDATE storage_actors SET state = \'revoked\'') + gate.resolve() + await expected + expect(f.database.prepare('SELECT * FROM command_receipts').all()).toEqual([]) + expect(f.database.prepare('SELECT * FROM effects').all()).toEqual([]) + }) + + it('preserves unknown acceptance after a COMMIT error until verified on a reopened connection', async () => { + const f = fixture(true) + guardStorageDatabase(f.database) + const exec = f.database.exec.bind(f.database) + const spy = vi.spyOn(f.database, 'exec').mockImplementation((sql) => { + exec(sql) + if (sql === 'COMMIT') + throw new Error('injected response loss') + }) + await expect(f.service.execute('chat.startTurn', f.submission, { prepare: async () => {}, commit: f.commit })).rejects.toMatchObject({ code: 'COMMAND_RESULT_UNKNOWN', acceptance: 'unknown' }) + expect(() => f.service.lookup(f.identity)).toThrowError(expect.objectContaining({ code: 'COMMAND_RESULT_UNKNOWN' })) + spy.mockRestore() + f.database.close() + const reopened = openBuddyMigrationCandidate({ databasePath: f.databasePath }) + databases.push(reopened) + const service = new CommandAdmissionService({ database: reopened, sourceInstanceId: 'instance-2', principal: createLocalCommandPrincipal(reopened) }) + expect(service.lookup(f.identity)).toMatchObject({ lookup: 'decided', receipt: { decision: 'accepted', target: { runId: 'original-run' } } }) + expect(reopened.prepare('SELECT * FROM effects').all()).toEqual([{ id: 'original-effect' }]) + }) + + it('fails closed on unsupported stored fingerprint versions without deriving a new identity', async () => { + const f = fixture() + new CommandCommitter(f.database).accept({ ...f.identity, actorId: f.principal.actorId, method: 'chat.startTurn', fingerprintVersion: 99, fingerprint: 'future', sourceInstanceId: 'instance-old' }, () => ({ target: {}, result: {} })) + await expect(f.service.execute('chat.startTurn', f.submission, { prepare: async () => {}, commit: f.commit })).rejects.toBeInstanceOf(CommandAdmissionError) + await expect(f.service.execute('chat.startTurn', f.submission, { prepare: async () => {}, commit: f.commit })).rejects.toMatchObject({ code: 'COMMAND_FINGERPRINT_UNSUPPORTED' }) + expect(f.database.prepare('SELECT * FROM effects').all()).toEqual([]) + }) +}) diff --git a/apps/buddy/service/src/commands/__tests__/DesktopOpenCommandService.spec.ts b/apps/buddy/service/src/commands/__tests__/DesktopOpenCommandService.spec.ts new file mode 100644 index 000000000..ef3df5c96 --- /dev/null +++ b/apps/buddy/service/src/commands/__tests__/DesktopOpenCommandService.spec.ts @@ -0,0 +1,36 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { RuntimePlatform } from '../../platform/RuntimePlatform' +import { afterEach, expect, it } from 'vitest' +import { openBuddyMigrationCandidate } from '../../storage/database' +import { CommandAdmissionService, createLocalCommandPrincipal } from '../CommandAdmissionService' +import { DesktopOpenCommandService } from '../DesktopOpenCommandService' + +const databases: DatabaseSync[] = [] +afterEach(() => { + for (const database of databases.splice(0)) + database.close() +}) +function fixture(openDesktop: RuntimePlatform['openDesktop']) { + const database = openBuddyMigrationCandidate({ databasePath: ':memory:' }) + databases.push(database) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + const dataGeneration = String(database.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation) + const principal = createLocalCommandPrincipal(database) + const create = (instance = 'instance') => new DesktopOpenCommandService({ database, admission: new CommandAdmissionService({ database, principal, sourceInstanceId: instance }), openDesktop }) + return { create, input: { dataGeneration, commandId: 'open', expectedServiceInstanceId: 'instance', intent: { kind: 'settings' } } } +} +const opened = { accepted: true, uiReady: true, targetVisible: true, visible: true, focused: false } +it('preserves the actual target acknowledgement across retries and service generations', async () => { + let opens = 0 + const f = fixture(async () => { + opens++ + return opened + }) + const first = await f.create().execute(f.input) + const replay = await f.create('next-instance').execute(f.input) + expect(replay).toEqual({ created: false, receipt: first.receipt }) + expect(replay.receipt.result).toEqual({ desktop: opened }) + expect(opens).toBe(1) + await expect(f.create('next-instance').execute({ ...f.input, commandId: 'another' })).rejects.toMatchObject({ code: 'SERVICE_INSTANCE_CHANGED' }) + expect(opens).toBe(1) +}) diff --git a/apps/buddy/service/src/commands/__tests__/commandIntent.spec.ts b/apps/buddy/service/src/commands/__tests__/commandIntent.spec.ts new file mode 100644 index 000000000..e282f0208 --- /dev/null +++ b/apps/buddy/service/src/commands/__tests__/commandIntent.spec.ts @@ -0,0 +1,30 @@ +import { describe, expect, it } from 'vitest' +import { canonicalCommandJson, fingerprintCommandIntent } from '../commandIntent' + +describe('command intent version 1', () => { + it('pins canonical bytes and SHA-256 independently of insertion order or locale', () => { + expect(canonicalCommandJson({ z: 'é', a: [1, null, false], 中: '\n', Z: -0 })).toBe('{"Z":0,"a":[1,null,false],"z":"é","中":"\\n"}') + const first = fingerprintCommandIntent('chat.startTurn', { draftId: 'draft-1', expectedRevision: 2 }) + expect(first).toMatchObject({ version: 1, fingerprint: 'a8f8296a77c88b66b8c6a847360d064bee539994f6d94c4793808483dab7acaf' }) + expect(fingerprintCommandIntent('chat.startTurn', { expectedRevision: 2, draftId: 'draft-1' })).toEqual(first) + expect(canonicalCommandJson({ é: 'é', e: 'e\u0301' })).toBe('{"e":"é","é":"é"}') + expect(canonicalCommandJson(['a', 'b'])).not.toBe(canonicalCommandJson(['b', 'a'])) + }) + + it.each([Number.NaN, Number.MAX_SAFE_INTEGER + 1, { a: undefined }])('rejects non-JSON or inexact input case %#', (value) => { + expect(() => canonicalCommandJson(value)).toThrow(TypeError) + }) + + it('whitelists business fields and preserves routing intent without adding envelope identity', () => { + const intent = { draftId: 'draft-1', expectedRevision: 2 } + for (const field of ['actorId', 'commandId', 'requestId', 'fingerprint', 'expectedServiceInstanceId', 'dataGeneration']) + expect(() => fingerprintCommandIntent('chat.startTurn', { ...intent, [field]: 'injected' })).toThrow() + const original = fingerprintCommandIntent('chat.startTurn', intent).fingerprint + expect(fingerprintCommandIntent('chat.startTurn', { ...intent, expectedRevision: 3 }).fingerprint).not.toBe(original) + expect(fingerprintCommandIntent('chat.startTurn', { ...intent, buddyId: 'buddy' }).fingerprint).not.toBe(original) + const delivery = { mode: 'current', actorId: 'conversation-actor', target: { rootRunId: 'root', runId: 'execution' } } as const + const route = fingerprintCommandIntent('chat.queue.enqueue', { ...intent, delivery }).fingerprint + expect(fingerprintCommandIntent('chat.queue.enqueue', { ...intent, delivery: { ...delivery, target: null } }).fingerprint).not.toBe(route) + expect(() => fingerprintCommandIntent('chat.queue.enqueue', { ...intent, delivery: { ...delivery, other: true } })).toThrow() + }) +}) diff --git a/apps/buddy/service/src/commands/commandIntent.ts b/apps/buddy/service/src/commands/commandIntent.ts new file mode 100644 index 000000000..43ea16574 --- /dev/null +++ b/apps/buddy/service/src/commands/commandIntent.ts @@ -0,0 +1,131 @@ +import { createHash } from 'node:crypto' +import { z } from 'zod' +import { buddyUserContentV1Schema } from '../../../shared/conversation/buddyUserContent' +import { buddyInputDeliverySchema } from '../../../shared/conversation/chatApi' +import { buddyComposerDraftDiscardSchema, buddyComposerDraftOpenSchema, buddyComposerDraftSaveSchema, buddyComposerDraftScopeSchema } from '../../../shared/conversation/composerDraft' +import { conversationRequestSchemas } from '../../../shared/conversation/conversationApi' +import { conversationArchivePolicySchema, conversationArchiveTargetSchema } from '../../../shared/conversation/conversationArchiveApi' +import { submitInputIntentSchema } from '../../../shared/conversation/submittedInput' +import { taskMarkClearSchema, taskMarkInputSchema, taskMarkReadSchema } from '../../../shared/conversation/taskMarkApi' + +import { uploadCommandIntents } from '../../../shared/privateNetwork/uploads' +import { productCommandIntents } from '../../../shared/productProtocol/methods' +import { userQuestionRpc } from '../../../shared/questions/userQuestionApi' +import { desktopOpenIntentSchema } from '../../../shared/service/localServiceProtocol' + +export const COMMAND_FINGERPRINT_VERSION = 1 as const +export const PRODUCT_COMMAND_FINGERPRINT_VERSION = 2 as const +export const productCommandFingerprint = Symbol('productCommandFingerprint') + +export interface ProductCommandFingerprint { + version: typeof PRODUCT_COMMAND_FINGERPRINT_VERSION + fingerprint: string +} + +const id = z.string().min(1).max(256) +const revision = z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER) +const queueTarget = z.object({ conversationId: id, branchId: id, id, expectedRevision: revision }).strict() +const draft = z.object({ draftId: id, expectedRevision: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER) }).strict() + +const copyTargetScope = z.union([ + buddyComposerDraftScopeSchema.options[1], + buddyComposerDraftScopeSchema.options[2], + buddyComposerDraftScopeSchema.options[3], + buddyComposerDraftScopeSchema.options[4], + buddyComposerDraftScopeSchema.options[5], + z.object({ kind: z.literal('task'), spaceId: id.nullable() }).strict(), +]) + +export const commandIntentSchemas = { + ...uploadCommandIntents, + 'questions.answer': userQuestionRpc.answer.input, + 'questions.cancel': userQuestionRpc.cancel.input, + 'questions.retry': userQuestionRpc.retry.input, + 'desktop.open': desktopOpenIntentSchema, + 'chat.submitInput': submitInputIntentSchema(buddyUserContentV1Schema), + 'uploads.create': uploadCommandIntents['uploads.create'].extend({ expectedRevision: revision }), + 'uploads.adopt': uploadCommandIntents['uploads.adopt'].extend({ expectedRevision: revision }), + 'chat.startTurn': draft.extend({ buddyId: id.optional(), delivery: buddyInputDeliverySchema.optional() }).strict(), + 'chat.editUserMessage': draft.extend({ conversationId: id, userMessageId: id }).strict(), + 'chat.regenerateAssistant': z.object({ conversationId: id, sourceRunId: id }).strict(), + 'chat.executeCommand': draft, + 'chat.queue.enqueue': draft.extend({ delivery: buddyInputDeliverySchema.optional() }).strict(), + 'chat.queue.cancel': queueTarget, + 'chat.queue.continue': queueTarget, + 'chat.queue.steer': queueTarget.extend({ targetRunId: id, expectedRunGeneration: revision }).strict(), + 'chat.cancel': z.object({ runId: id, expectedRunGeneration: revision }).strict(), + 'composerDrafts.open': buddyComposerDraftOpenSchema.unwrap().extend({ expectedSlotRevision: revision }).strict(), + 'composerDrafts.copy': z.object({ sourceDraftId: id, expectedSourceRevision: revision, targetScope: copyTargetScope, expectedSlotRevision: revision, recoveredInput: buddyComposerDraftSaveSchema.unwrap().pick({ content: true, executionConfig: true, modelSelection: true, participants: true }).optional() }).strict(), + 'composerDrafts.save': buddyComposerDraftSaveSchema, + 'composerDrafts.discard': buddyComposerDraftDiscardSchema, + 'approvals.resolve': z.object({ approvalId: id, runId: id, reviewVersion: z.string().regex(/^[a-f0-9]{64}$/), decision: z.enum(['approved', 'denied', 'approved_persistently', 'approved_for_operation', 'approved_for_source', 'approved_for_turn']) }).strict(), + 'conversations.rename': conversationRequestSchemas.conversationRename.extend({ expectedTitleRevision: revision }).strict(), + 'conversations.delete': conversationRequestSchemas.conversationId.extend({ expectedRevision: revision }).strict(), + 'conversations.archive': conversationArchiveTargetSchema.extend({ expectedRevision: revision }).strict(), + 'conversations.restoreArchive': conversationArchiveTargetSchema.extend({ expectedRevision: revision }).strict(), + 'conversations.deleteArchived': conversationArchiveTargetSchema.extend({ expectedRevision: revision }).strict(), + 'conversations.deleteLegacyRemoved': conversationArchiveTargetSchema.extend({ expectedRevision: revision }).strict(), + 'conversations.setArchivePolicy': conversationArchivePolicySchema, + 'conversations.activateBranch': conversationRequestSchemas.conversationBranchActivation.extend({ expectedRevision: revision }).strict(), + 'conversations.setPermissionSettings': conversationRequestSchemas.conversationPermissionSettings.extend({ expectedRevision: revision }).strict(), + 'conversations.setBuddy': conversationRequestSchemas.conversationBuddy.extend({ expectedRevision: revision }).strict(), + 'conversations.addMember': conversationRequestSchemas.conversationBuddy.extend({ expectedRevision: revision }).strict(), + 'conversations.removeMember': conversationRequestSchemas.conversationBuddy.extend({ expectedRevision: revision }).strict(), + 'conversations.moveToSpace': conversationRequestSchemas.conversationMoveToSpace.extend({ expectedRevision: revision }).strict(), + 'conversations.setModelSelection': conversationRequestSchemas.conversationModelSelection.extend({ expectedRevision: revision, expectedRunGeneration: revision.optional(), expectedRunModelSelection: conversationRequestSchemas.conversationModelSelection.shape.modelSelection.optional() }).strict().refine(input => input.targetRunId ? (input.expectedRunGeneration !== undefined && input.expectedRunModelSelection !== undefined) : input.expectedActiveRun !== undefined), + 'taskMarks.create': taskMarkInputSchema, + 'taskMarks.update': taskMarkInputSchema.extend({ id, expectedRevision: revision }).strict(), + 'taskMarks.delete': z.object({ id, expectedRevision: revision }).strict(), + 'taskMarks.assign': z.object({ conversationId: id, markId: id.nullable(), expectedAssignmentRevision: revision }).strict(), + 'taskMarks.setRead': taskMarkReadSchema, + 'taskMarks.clear': taskMarkClearSchema.extend({ expectedAssignmentRevision: revision }).strict(), + 'runtime.stop': z.object({ targetServiceInstanceId: id, scope: z.enum(['stop_service', 'keep_service']) }).strict(), +} as const + +export type InputCommandMethod = keyof typeof commandIntentSchemas +export type InputCommandIntent = z.output + +export function fingerprintCommandIntent(method: Method, value: unknown) { + const intent = commandIntentSchemas[method].parse(value) as InputCommandIntent + const bytes = canonicalCommandJson(intent) + return { version: COMMAND_FINGERPRINT_VERSION, fingerprint: createHash('sha256').update(bytes, 'utf8').digest('hex'), intent } +} + +export function fingerprintProductCommandIntent(method: keyof typeof productCommandIntents, value: unknown): ProductCommandFingerprint { + const intent = productCommandIntents[method].parse(value) + return { version: PRODUCT_COMMAND_FINGERPRINT_VERSION, fingerprint: createHash('sha256').update(canonicalCommandJson(intent), 'utf8').digest('hex') } +} + +export function canonicalCommandJson(value: unknown): string { + const ancestors = new Set() + const encode = (input: unknown, depth: number): string => { + if (depth > 64) + throw new TypeError('Command intent nesting exceeds its limit') + if (input === null || typeof input === 'boolean' || typeof input === 'string') + return JSON.stringify(input) + if (typeof input === 'number') { + if (!Number.isFinite(input) || (Number.isInteger(input) && !Number.isSafeInteger(input))) + throw new TypeError('Command intent numbers must be finite and exactly representable') + return JSON.stringify(input) + } + if (!input || typeof input !== 'object') + throw new TypeError('Command intent must contain JSON values') + if (ancestors.has(input)) + throw new TypeError('Command intent cannot contain cycles') + ancestors.add(input) + try { + if (Array.isArray(input)) { + return `[${Array.from(input, child => encode(child, depth + 1)).join(',')}]` + } + if (Object.getPrototypeOf(input) !== Object.prototype && Object.getPrototypeOf(input) !== null) + throw new TypeError('Command intent objects must contain plain JSON data') + if (Object.getOwnPropertySymbols(input).length) + throw new TypeError('Command intent cannot contain symbol keys') + return `{${Object.keys(input).sort().map(key => `${JSON.stringify(key)}:${encode((input as Record)[key], depth + 1)}`).join(',')}}` + } + finally { + ancestors.delete(input) + } + } + return encode(value, 0) +} diff --git a/apps/buddy/service/src/connectors/mcp/McpConnectorService.ts b/apps/buddy/service/src/connectors/mcp/McpConnectorService.ts index 09f7a5b3b..73a9ebfee 100644 --- a/apps/buddy/service/src/connectors/mcp/McpConnectorService.ts +++ b/apps/buddy/service/src/connectors/mcp/McpConnectorService.ts @@ -2,12 +2,12 @@ import type { ToolDefinition } from '@earendil-works/pi-coding-agent' import type { ConnectorCredential, OAuthConnectorCredential } from '../../../../shared/connectors/connectorCredentials' import type { ConnectorRuntimeState, ConnectorToolSummary } from '../../../../shared/connectors/connectorState' import type { Event, ListenerErrorHandler } from '../../../../shared/events/Emitter' -import type { RuntimeRpcPeerContract } from '../../../../shared/runtime/rpcPeer' import type { BuddyCapabilityResourceRevision } from '../../agent/extensions/BuddyCapability' import type { BuddyToolDisclosurePolicy } from '../../agent/extensions/discovery/toolDiscoveryContract' import type { BuddyToolClassification } from '../../approvals/toolClassification' import type { ConnectorRepository, McpServerRecord, McpServerWrite } from '../../storage/connectorRepository' import type { ManagedCapabilityRecord, ManagedCapabilityRepository } from '../../storage/managedCapabilityRepository' +import type { ConnectorSecretStore } from './McpCredentialChanges' import type { McpConnectionEvent, McpConnectorDetails, McpConnectorEvent } from './mcpEvents' import type { McpServerConfig } from './mcpSchemas' import type { McpResultWriter } from './mcpToolResults' @@ -16,20 +16,16 @@ import { connectorCredentialSchema } from '../../../../shared/connectors/connect import { managedMcpCredentialTarget } from '../../../../shared/connectors/managedMcp' import { Emitter } from '../../../../shared/events/Emitter' import { copyEventSnapshot } from '../../../../shared/events/eventSnapshot' -import { credentialMutationResultSchema, credentialReadResultSchema } from '../../../../shared/runtime/credentialProtocol' import { createMcpTools } from './createMcpTools' import { McpConnectionManager } from './McpConnectionManager' +import { McpCredentialChanges } from './McpCredentialChanges' import { McpClientError, mcpErrorCode } from './mcpErrors' import { loginMcpOAuth, McpOAuthProvider } from './McpOAuthProvider' import { mcpServerConfigSchema } from './mcpSchemas' import { McpToolNames } from './mcpToolNames' import { waitForMcpOperation } from './waitForMcpOperation' -export interface ConnectorSecretStore { - delete: (id: string) => Promise - read: (id: string) => Promise - write: (id: string, credential: ConnectorCredential) => Promise -} +export type { ConnectorSecretStore } from './McpCredentialChanges' export interface McpConnectorServiceOptions { connectors: ConnectorRepository @@ -62,7 +58,7 @@ export interface SaveMcpConnectorInput { export class McpConnectorService { readonly #connectors: ConnectorRepository readonly #options: McpConnectorServiceOptions - readonly #secrets: ConnectorSecretStore + readonly #credentials: McpCredentialChanges readonly #manager: McpConnectionManager readonly #toolNames = new McpToolNames() readonly #managedTargets = new Map() @@ -80,7 +76,7 @@ export class McpConnectorService { this.#options = options this.#events = new Emitter(options.onListenerError ?? (() => console.error('MCP_CONNECTOR_OBSERVER_FAILED'))) this.#connectors = options.connectors - this.#secrets = options.secrets + this.#credentials = new McpCredentialChanges(options.secrets, (id, event) => this.#publish(id, event)) for (const source of options.managed?.listKind('mcp') ?? []) { if (source.kind === 'mcp' && 'authentication' in source.definition) this.#managedTargets.set(source.id, managedMcpCredentialTarget(source.definition)) @@ -183,21 +179,14 @@ export class McpConnectorService { this.#options.managed!.clearConfiguration(id) return } - const previous = record.credentialRef ? await this.#secrets.read(record.credentialRef) : null - let saved: McpServerRecord - try { - if (record.credentialRef) - await this.#deleteCredential(record.credentialRef, id) - saved = this.#connectors.configureManaged( + const saved = await this.#credentials.commit({ + connectorId: id, + replacement: record.credentialRef ? { ref: record.credentialRef, value: null } : null, + commit: () => this.#connectors.configureManaged( { ...record, credentialRef: null, enabled: false, toolExposure: 'deferred', updatedAt: new Date().toISOString() }, { credentialTarget: null, userEnabled: false, unavailableReason: null }, - ) - } - catch (error) { - if (record.credentialRef) - await this.#restoreSecret(record.credentialRef, previous, id) - throw error - } + ), + }) this.#manager.clearCatalog(id) this.#configurationCommitted(record, saved) }) @@ -227,21 +216,14 @@ export class McpConnectorService { throw new McpConnectorError('VALIDATION_FAILED') if (input.credential.mode === 'replace' && (!connectorCredentialSchema.safeParse(input.credential.value).success || !credentialMatchesTransport(input.credential.value, parsed.data.transport))) throw new McpConnectorError('VALIDATION_FAILED') - const previous = previousRef && credentialChanged ? await this.#secrets.read(previousRef) : null const credentialRef = input.credential.mode === 'replace' ? previousRef ?? parsed.data.id : input.credential.mode === 'keep' ? previousRef : null - let record: McpServerRecord - try { - if (input.credential.mode === 'replace') - await this.#writeCredential(credentialRef!, input.credential.value, parsed.data.id) - else if (input.credential.mode === 'clear' && previousRef) - await this.#deleteCredential(previousRef, parsed.data.id) - record = this.#persistConfig({ ...parsed.data, credentialRef }, credentialChanged) - } - catch (error) { - if (credentialChanged) - await this.#restoreSecret(credentialRef ?? previousRef ?? parsed.data.id, previous, parsed.data.id) - throw error - } + const record = await this.#credentials.commit({ + connectorId: parsed.data.id, + replacement: input.credential.mode === 'replace' + ? { ref: credentialRef!, value: input.credential.value } + : input.credential.mode === 'clear' && previousRef ? { ref: previousRef, value: null } : null, + commit: () => this.#persistConfig({ ...parsed.data, credentialRef }, credentialChanged), + }) if (!existing || !sameExecutionTarget(existing, parsed.data) || credentialChanged) this.#manager.clearCatalog(record.id) return record @@ -294,15 +276,11 @@ export class McpConnectorService { if (!parsed.success || !credentialMatchesTransport(parsed.data, record.transport)) throw new McpConnectorError('VALIDATION_FAILED') const credentialRef = record.credentialRef ?? record.id - const previous = await this.#secrets.read(credentialRef) - try { - await this.#writeCredential(credentialRef, parsed.data, id) - this.#persistConfig(toConfig({ ...record, credentialRef, enabled: record.transport === 'stdio' ? false : record.enabled }), true) - } - catch (error) { - await this.#restoreSecret(credentialRef, previous, id) - throw error - } + await this.#credentials.commit({ + connectorId: id, + replacement: { ref: credentialRef, value: parsed.data }, + commit: () => this.#persistConfig(toConfig({ ...record, credentialRef, enabled: record.transport === 'stdio' ? false : record.enabled }), true), + }) this.#manager.clearCatalog(id) }) } @@ -317,15 +295,11 @@ export class McpConnectorService { const record = this.#requireConnector(id) if (!record.credentialRef) return - const previous = await this.#secrets.read(record.credentialRef) - try { - await this.#deleteCredential(record.credentialRef, id) - this.#persistConfig(toConfig({ ...record, credentialRef: null, enabled: record.transport === 'stdio' ? false : record.enabled }), true) - } - catch (error) { - await this.#restoreSecret(record.credentialRef, previous, id) - throw error - } + await this.#credentials.commit({ + connectorId: id, + replacement: { ref: record.credentialRef, value: null }, + commit: () => this.#persistConfig(toConfig({ ...record, credentialRef: null, enabled: record.transport === 'stdio' ? false : record.enabled }), true), + }) this.#manager.clearCatalog(id) }, () => this.#connectors.findById(id)?.credentialRef != null) } @@ -341,20 +315,12 @@ export class McpConnectorService { const record = this.#connectors.findById(id) if (!record) return false - const previous = record.credentialRef ? await this.#secrets.read(record.credentialRef) : null - if (record.credentialRef) - await this.#deleteCredential(record.credentialRef, id) - let removed: boolean - try { - removed = this.#connectors.remove(id) - } - catch (error) { - if (record.credentialRef) - await this.#restoreSecret(record.credentialRef, previous, id) - throw error - } - if (!removed && record.credentialRef) - await this.#restoreSecret(record.credentialRef, previous, id) + const removed = await this.#credentials.commit({ + connectorId: id, + replacement: record.credentialRef ? { ref: record.credentialRef, value: null } : null, + commit: () => this.#connectors.remove(id), + committed: removed => removed, + }) if (removed) { this.#publish(id, { type: 'configuration', kind: 'removed', enabled: false, executionChanged: true }) this.#manager.clearCatalog(id) @@ -503,6 +469,33 @@ export class McpConnectorService { return { resourceRevisions: copyEventSnapshot(resourceRevisions), classifications, diagnostics, tools, disclosure, available: name => availability.get(name)?.() ?? false } } + async suspendPlatform(): Promise { + this.#quiescing = true + for (const login of this.#logins.values()) + login.controller.abort() + const ids = this.#connectors.list().map(record => record.id) + for (const id of ids) this.#manager.pause(id) + const reset = Promise.allSettled(ids.map(id => this.#manager.reset(id))) + const operations = [ + ...[...this.#logins.values()].map(login => login.operation), + ...this.#mutations.values(), + ...this.#secretWrites.values(), + ...this.#tests, + ] + await Promise.allSettled(operations) + const results = await reset + const failures = results.filter(result => result.status === 'rejected').map(result => result.reason) + if (failures.length) + throw new AggregateError(failures, 'MCP_PLATFORM_CLEANUP_FAILED') + } + + resumePlatform(): void { + if (this.#closed) + throw new McpClientError('MCP_SERVER_UNAVAILABLE') + this.#quiescing = false + for (const record of this.#connectors.list()) this.#manager.resume(record.id) + } + async quiesce(): Promise { this.#quiescing = true for (const login of this.#logins.values()) @@ -595,31 +588,24 @@ export class McpConnectorService { } const ref = replacing ? existing.credentialRef ?? source.id : credential.mode === 'clear' ? null : existing.credentialRef const changed = credential.mode !== 'keep' - const previous = changed && existing.credentialRef ? await this.#secrets.read(existing.credentialRef) : null - let saved: McpServerRecord - try { - if (replacing) - await this.#writeCredential(ref!, credential.value, source.id) - else if (credential.mode === 'clear' && existing.credentialRef) - await this.#deleteCredential(existing.credentialRef, source.id) - if (replacing) - this.#assertManagedCurrent(source, true) - else if (this.#requireManaged(source.id).revision !== source.revision) - throw new McpClientError('MCP_CONNECTOR_CHANGED') - saved = this.#connectors.configureManaged( + const saved = await this.#credentials.commit({ + connectorId: source.id, + replacement: replacing ? { ref: ref!, value: credential.value } : credential.mode === 'clear' && existing.credentialRef ? { ref: existing.credentialRef, value: null } : null, + validate: () => { + if (replacing) + this.#assertManagedCurrent(source, true) + else if (this.#requireManaged(source.id).revision !== source.revision) + throw new McpClientError('MCP_CONNECTOR_CHANGED') + }, + commit: () => this.#connectors.configureManaged( { ...existing, credentialRef: ref, toolExposure: config.toolExposure ?? existing.toolExposure, enabled: false, updatedAt: new Date().toISOString() }, { credentialTarget: changed ? ref ? managedMcpCredentialTarget(definition) : null : source.credentialTarget, unavailableReason: changed ? ref || definition.authentication === 'none' ? null : 'authentication_required' : existing.managed?.unavailableReason ?? source.unavailableReason, userEnabled: config.enabled, }, - ) - } - catch (error) { - if (changed) - await this.#restoreSecret(ref ?? existing.credentialRef ?? source.id, previous, source.id) - throw error - } + ), + }) if (changed) this.#manager.clearCatalog(source.id) this.#configurationCommitted(existing, saved) @@ -646,31 +632,30 @@ export class McpConnectorService { throw new McpClientError('MCP_CONNECTOR_CHANGED') assertCurrent() const ref = record.credentialRef ?? record.id - const previous = await this.#secrets.read(ref) - try { - signal.throwIfAborted() - await this.#writeCredential(ref, credential, id) - signal.throwIfAborted() - if (generation !== this.#manager.generation(id)) - throw new McpClientError('MCP_CONNECTOR_CHANGED') - assertCurrent() - if (source) { - const current = this.#requireManaged(id) - const saved = this.#connectors.configureManaged( - { ...record, credentialRef: ref, updatedAt: new Date().toISOString() }, - { userEnabled: current.userEnabled, credentialTarget: managedMcpCredentialTarget(this.#managedDefinition(source)), unavailableReason: credential.tokens ? null : 'authentication_required' }, - ) - this.#configurationCommitted(record, saved) - } - else if (record.credentialRef !== ref) { - const saved = this.#connectors.upsert({ ...record, credentialRef: ref, updatedAt: new Date().toISOString() }) - this.#configurationCommitted(record, saved) - } - } - catch (error) { - await this.#restoreSecret(ref, previous, id) - throw error - } + await this.#credentials.commit({ + connectorId: id, + replacement: { ref, value: credential }, + validate: () => { + signal.throwIfAborted() + if (this.#closed || generation !== this.#manager.generation(id)) + throw new McpClientError('MCP_CONNECTOR_CHANGED') + assertCurrent() + }, + commit: () => { + if (source) { + const current = this.#requireManaged(id) + const saved = this.#connectors.configureManaged( + { ...record, credentialRef: ref, updatedAt: new Date().toISOString() }, + { userEnabled: current.userEnabled, credentialTarget: managedMcpCredentialTarget(this.#managedDefinition(source)), unavailableReason: credential.tokens ? null : 'authentication_required' }, + ) + this.#configurationCommitted(record, saved) + } + else if (record.credentialRef !== ref) { + const saved = this.#connectors.upsert({ ...record, credentialRef: ref, updatedAt: new Date().toISOString() }) + this.#configurationCommitted(record, saved) + } + }, + }) }) } @@ -695,30 +680,6 @@ export class McpConnectorService { return saved } - async #writeCredential(ref: string, credential: ConnectorCredential, connectorId: string): Promise { - await this.#secrets.write(ref, credential) - this.#publish(connectorId, { type: 'credential', status: 'written', presence: 'present' }) - } - - async #deleteCredential(ref: string, connectorId: string): Promise { - await this.#secrets.delete(ref) - this.#publish(connectorId, { type: 'credential', status: 'deleted', presence: 'absent' }) - } - - async #restoreSecret(ref: string, credential: ConnectorCredential | null, connectorId: string): Promise { - try { - if (credential) - await this.#secrets.write(ref, credential) - else - await this.#secrets.delete(ref) - this.#publish(connectorId, { type: 'credential', status: 'restored', presence: credential ? 'present' : 'absent' }) - } - catch (error) { - this.#publish(connectorId, { type: 'credential', status: 'restore-failed', presence: 'unknown' }) - throw error - } - } - #configurationCommitted(previous: McpServerRecord | null | undefined, current: McpServerRecord): void { this.#publish(current.id, { type: 'configuration', kind: previous ? 'updated' : 'created', enabled: current.enabled, executionChanged: !previous || !sameExecutionTarget(previous, toConfig(current)) || previous.enabled !== current.enabled || previous.executionConfirmedAt !== current.executionConfirmedAt || previous.credentialRef !== current.credentialRef }) } @@ -745,42 +706,6 @@ function enqueue(queue: Map>, id: string, action: () return operation } -export class HostConnectorSecretStore implements ConnectorSecretStore { - readonly #peer: RuntimeRpcPeerContract - - constructor(peer: RuntimeRpcPeerContract) { - this.#peer = peer - } - - async read(id: string): Promise { - const response = credentialReadResultSchema.parse(await this.#peer.request( - 'host.secrets.read', - { id, namespace: 'connectors' }, - )) - if (!response.ok) - throw new McpConnectorError(response.error.code) - return response.value === null ? null : connectorCredentialSchema.parse(response.value) - } - - async write(id: string, credential: ConnectorCredential): Promise { - const response = credentialMutationResultSchema.parse(await this.#peer.request( - 'host.secrets.write', - { id, namespace: 'connectors', value: connectorCredentialSchema.parse(credential) }, - )) - if (!response.ok) - throw new McpConnectorError(response.error.code) - } - - async delete(id: string): Promise { - const response = credentialMutationResultSchema.parse(await this.#peer.request( - 'host.secrets.delete', - { id, namespace: 'connectors' }, - )) - if (!response.ok) - throw new McpConnectorError(response.error.code) - } -} - export class McpConnectorError extends Error { readonly code: string diff --git a/apps/buddy/service/src/connectors/mcp/McpCredentialChanges.ts b/apps/buddy/service/src/connectors/mcp/McpCredentialChanges.ts new file mode 100644 index 000000000..c13380db5 --- /dev/null +++ b/apps/buddy/service/src/connectors/mcp/McpCredentialChanges.ts @@ -0,0 +1,67 @@ +import type { ConnectorCredential } from '../../../../shared/connectors/connectorCredentials' +import type { McpConnectorDetails } from './mcpEvents' + +export interface ConnectorSecretStore { + delete: (id: string) => Promise + read: (id: string) => Promise + write: (id: string, credential: ConnectorCredential) => Promise +} + +interface CredentialChange { + connectorId: string + replacement: { ref: string, value: ConnectorCredential | null } | null + validate?: () => void + commit: () => T + committed?: (result: T) => boolean +} + +type CredentialEvent = Extract + +export class McpCredentialChanges { + readonly #secrets: ConnectorSecretStore + readonly #publish: (connectorId: string, event: CredentialEvent) => void + + constructor(secrets: ConnectorSecretStore, publish: (connectorId: string, event: CredentialEvent) => void) { + this.#secrets = secrets + this.#publish = publish + } + + async commit(change: CredentialChange): Promise { + const { connectorId, replacement } = change + if (!replacement) { + change.validate?.() + return change.commit() + } + const previous = await this.#secrets.read(replacement.ref) + change.validate?.() + let result: T + try { + await this.#write(replacement.ref, replacement.value) + this.#publish(connectorId, { type: 'credential', status: replacement.value ? 'written' : 'deleted', presence: replacement.value ? 'present' : 'absent' }) + change.validate?.() + result = change.commit() + } + catch (error) { + await this.#restore(connectorId, replacement.ref, previous) + throw error + } + if (change.committed && !change.committed(result)) + await this.#restore(connectorId, replacement.ref, previous) + return result + } + + #write(ref: string, value: ConnectorCredential | null): Promise { + return value ? this.#secrets.write(ref, value) : this.#secrets.delete(ref) + } + + async #restore(connectorId: string, ref: string, previous: ConnectorCredential | null): Promise { + try { + await this.#write(ref, previous) + this.#publish(connectorId, { type: 'credential', status: 'restored', presence: previous ? 'present' : 'absent' }) + } + catch (error) { + this.#publish(connectorId, { type: 'credential', status: 'restore-failed', presence: 'unknown' }) + throw error + } + } +} diff --git a/apps/buddy/service/src/connectors/mcp/__tests__/McpCredentialChanges.spec.ts b/apps/buddy/service/src/connectors/mcp/__tests__/McpCredentialChanges.spec.ts new file mode 100644 index 000000000..26d9acb0b --- /dev/null +++ b/apps/buddy/service/src/connectors/mcp/__tests__/McpCredentialChanges.spec.ts @@ -0,0 +1,80 @@ +import type { ConnectorCredential } from '../../../../../shared/connectors/connectorCredentials' +import type { ConnectorSecretStore } from '../McpCredentialChanges' +import type { McpConnectorDetails } from '../mcpEvents' +import { describe, expect, it } from 'vitest' +import { McpCredentialChanges } from '../McpCredentialChanges' + +function fixture() { + const original: ConnectorCredential = { type: 'http', bearerToken: 'fixture-original' } + let secret: ConnectorCredential | null = original + const events: McpConnectorDetails[] = [] + const secrets: ConnectorSecretStore = { + read: async () => secret, + write: async (_, value) => { secret = value }, + delete: async () => { secret = null }, + } + const changes = new McpCredentialChanges(secrets, (_, event) => events.push(event)) + return { original, secrets, changes, events, get secret() { + return secret + } } +} + +describe('mCP credential compensation', () => { + it('restores the original secret when the target changes during a write', async () => { + const f = fixture() + let target = 'original' + const write = f.secrets.write + f.secrets.write = async (ref, value) => { + await write(ref, value) + target = 'changed' + } + let configuration = 'original' + await expect(f.changes.commit({ + connectorId: 'connector', + replacement: { ref: 'credential', value: { type: 'http', bearerToken: 'fixture-new' } }, + validate: () => { + if (target !== 'original') + throw new Error('target changed') + }, + commit: () => { configuration = 'changed' }, + })).rejects.toThrow('target changed') + expect(configuration).toBe('original') + expect(f.secret).toEqual(f.original) + expect(f.events).toEqual([ + { type: 'credential', status: 'written', presence: 'present' }, + { type: 'credential', status: 'restored', presence: 'present' }, + ]) + }) + + it('restores a deleted secret when the configuration was not removed', async () => { + const f = fixture() + const removed = await f.changes.commit({ + connectorId: 'connector', + replacement: { ref: 'credential', value: null }, + commit: () => false, + committed: value => value, + }) + expect(removed).toBe(false) + expect(f.secret).toEqual(f.original) + expect(f.events.at(-1)).toEqual({ type: 'credential', status: 'restored', presence: 'present' }) + }) + + it('reports unknown secret presence when compensation fails', async () => { + const f = fixture() + f.secrets.write = async () => { + throw new Error('secret store unavailable') + } + await expect(f.changes.commit({ + connectorId: 'connector', + replacement: { ref: 'credential', value: null }, + commit: () => { + throw new Error('configuration unavailable') + }, + })).rejects.toThrow('secret store unavailable') + expect(f.events).toEqual([ + { type: 'credential', status: 'deleted', presence: 'absent' }, + { type: 'credential', status: 'restore-failed', presence: 'unknown' }, + ]) + expect(JSON.stringify(f.events)).not.toContain('fixture-original') + }) +}) diff --git a/apps/buddy/service/src/context/ContextUsageSnapshotService.ts b/apps/buddy/service/src/context/ContextUsageSnapshotService.ts index 4c3d5b6e5..a798364bd 100644 --- a/apps/buddy/service/src/context/ContextUsageSnapshotService.ts +++ b/apps/buddy/service/src/context/ContextUsageSnapshotService.ts @@ -160,7 +160,7 @@ export class ContextUsageSnapshotService implements ContextUsageSnapshotReader { } const actorId = input.actorId ?? conversation?.buddyId ?? this.#options.getDefaultActorId() const actor = (await this.#options.definitions.capture({ buddyId: actorId, kind: 'actor', conversationId: conversation?.id ?? null, requestedModel: input.modelSelection })).definition - const threadId = conversation ? actorThreadId(conversation.id, actorId) : undefined + const threadId = conversation ? actorThreadId(conversation.id, actorId, followup?.branchId ?? input.branchId ?? undefined) : undefined const approvalPolicy = conversation?.approvalPolicy ?? input.approvalPolicy const executionProfile = conversation?.executionProfile ?? input.executionProfile const branchId = followup?.branchId ?? input.branchId ?? 'context-preview' diff --git a/apps/buddy/service/src/conversations/ConversationArchiveService.ts b/apps/buddy/service/src/conversations/ConversationArchiveService.ts index 8c1241e69..72fe5d5e8 100644 --- a/apps/buddy/service/src/conversations/ConversationArchiveService.ts +++ b/apps/buddy/service/src/conversations/ConversationArchiveService.ts @@ -1,7 +1,6 @@ import type { DatabaseSync } from 'node:sqlite' import type { ConversationArchivePolicy, ConversationArchiveQuery, ConversationArchiveTarget } from '../../../shared/conversation/conversationArchiveApi' import type { ConversationArchiveRepository } from '../storage/conversationArchiveRepository' -import type { ConversationDeletionCommit } from '../storage/conversationDeletionRepository' import type { ConversationRepository } from '../storage/conversationRepository' import type { ConversationLifecycleService } from './ConversationLifecycleService' import type { ConversationMutationGuard } from './ConversationMutationGuard' @@ -26,6 +25,7 @@ interface Options { mutations: ConversationMutationGuard lifecycle: ConversationLifecycleService onError: (error: unknown) => void + afterCommit?: (publish: () => void) => void now?: () => Date } @@ -59,67 +59,97 @@ export class ConversationArchiveService { return this.getPolicy() } - archive({ conversationId, expectedRevision }: ConversationArchiveTarget): Promise { - this.#requireActive(conversationId, expectedRevision) - return this.#options.mutations.run(conversationId, async () => { - await this.#options.lifecycle.suspendConversation(conversationId) - const changed = withTransaction(this.#options.database, () => { - this.#requireActive(conversationId, expectedRevision) - return this.#options.repository.archive(conversationId, this.#now()) - }) - if (changed) - this.#changes.fire(conversationId) - return changed - }) + withMutationScope(conversationId: string, operation: () => Promise): Promise { + this.#assertAvailable() + return this.#options.mutations.run(conversationId, operation) } - restore({ conversationId, expectedRevision }: ConversationArchiveTarget): boolean { - this.#options.mutations.assertWritable(conversationId) - const changed = withTransaction(this.#options.database, () => { - this.#requireActive(conversationId, expectedRevision) - return this.#options.repository.restore(conversationId, this.#now()) + archive({ conversationId, expectedRevision }: ConversationArchiveTarget): Promise { + this.#assertRevision(expectedRevision) + return this.withMutationScope(conversationId, async () => { + await this.prepareArchive(conversationId, expectedRevision) + return this.commitArchive(conversationId, expectedRevision) }) - if (changed) - this.#changes.fire(conversationId) - return changed } - deleteArchived(input: ConversationArchiveTarget): Promise { - return this.#delete(input, true) - } - - deletePermanently(input: ConversationArchiveTarget): Promise { - return this.#delete(input, false) + restore({ conversationId, expectedRevision }: ConversationArchiveTarget): boolean { + this.#assertRevision(expectedRevision) + return this.commitRestore(conversationId, expectedRevision) } - async #delete({ conversationId, expectedRevision }: ConversationArchiveTarget, archivedOnly: boolean): Promise { + async deleteArchived({ conversationId, expectedRevision }: ConversationArchiveTarget): Promise { + this.#assertRevision(expectedRevision) this.#options.mutations.assertWritable(conversationId) const committed = withTransaction(this.#options.database, () => { const conversation = this.#requireConversation(conversationId, expectedRevision) const job = this.#options.database.prepare('SELECT origin FROM conversation_deletion_jobs WHERE conversation_id = ?').get(conversationId) - if ((archivedOnly && !conversation.archivedAt && job?.origin !== 'conversation') || (conversation.deletedAt && job?.origin !== 'conversation')) + if (!conversation.archivedAt && job?.origin !== 'conversation') throw new ConversationArchiveConflictError('SOURCE_CHANGED') - return this.#options.lifecycle.commitDelete(conversationId) + return this.commitDelete(conversationId, expectedRevision) }) - void this.#finishDeletion(committed).catch(this.#options.onError) + this.#finishLegacyDeletion(committed) return true } async deleteLegacyRemoved({ conversationId, expectedRevision }: ConversationArchiveTarget): Promise { + this.#assertRevision(expectedRevision) this.#options.mutations.assertWritable(conversationId) - const committed = withTransaction(this.#options.database, () => { + const committed = withTransaction(this.#options.database, () => this.commitDeleteLegacyRemoved(conversationId, expectedRevision)) + this.#finishLegacyDeletion(committed) + return true + } + + #finishLegacyDeletion(committed: Parameters[0]): void { + const pending = this.finishDeletion(committed) + void this.withMutationScope(committed.conversationId, () => pending).catch(this.#options.onError) + } + + async prepareArchive(conversationId: string, expectedRevision?: number): Promise { + const conversation = this.#requireConversation(conversationId, expectedRevision) + if (conversation.deletedAt) + throw new ConversationArchiveConflictError('OBJECT_DELETED') + await this.#options.lifecycle.suspendConversation(conversationId) + } + + commitArchive(conversationId: string, expectedRevision?: number): boolean { + const changed = withTransaction(this.#options.database, () => { const conversation = this.#requireConversation(conversationId, expectedRevision) - if (!conversation.deletedAt || conversation.archivedAt) - throw new ConversationArchiveConflictError('SOURCE_CHANGED') - return this.#options.lifecycle.commitLegacyRemoved(conversationId) + if (conversation.deletedAt) + throw new ConversationArchiveConflictError('OBJECT_DELETED') + return this.#options.repository.archive(conversationId, this.#now()) }) - void this.#finishDeletion(committed).catch(this.#options.onError) - return true + if (changed) + this.#publish(conversationId) + return changed + } + + commitRestore(conversationId: string, expectedRevision?: number): boolean { + this.#options.mutations.assertWritable(conversationId) + const changed = withTransaction(this.#options.database, () => { + const conversation = this.#requireConversation(conversationId, expectedRevision) + if (conversation.deletedAt) + throw new ConversationArchiveConflictError('OBJECT_DELETED') + return this.#options.repository.restore(conversationId, this.#now()) + }) + if (changed) + this.#publish(conversationId) + return changed + } + + commitDelete(conversationId: string, expectedRevision?: number) { + this.#requireConversation(conversationId, expectedRevision) + return this.#options.lifecycle.commitDelete(conversationId) + } + + commitDeleteLegacyRemoved(conversationId: string, expectedRevision?: number) { + const conversation = this.#requireConversation(conversationId, expectedRevision) + if (!conversation.deletedAt || conversation.archivedAt) + throw new ConversationArchiveConflictError('SOURCE_CHANGED') + return this.#options.lifecycle.commitLegacyRemoved(conversationId) } - #finishDeletion(committed: ConversationDeletionCommit): Promise { - const pending = this.#options.lifecycle.finishDeletion(committed) - return this.#options.mutations.run(committed.conversationId, () => pending) + finishDeletion(committed: Parameters[0]) { + return this.#options.lifecycle.finishDeletion(committed) } start(): void { @@ -156,14 +186,13 @@ export class ConversationArchiveService { if (this.#stopping) break try { - this.#options.mutations.assertWritable(conversationId) const committed = withTransaction(this.#options.database, () => { if (!this.#options.repository.isDue(conversationId, this.#now())) return null return this.#options.lifecycle.commitDelete(conversationId) }) if (committed) - await this.#finishDeletion(committed) + await this.finishDeletion(committed) } catch (error) { this.#options.onError(error) @@ -176,26 +205,30 @@ export class ConversationArchiveService { throw new BuddyServiceError('CONVERSATION_BUSY') } - #requireConversation(conversationId: string, expectedRevision: number) { - this.#assertAvailable() + #assertRevision(expectedRevision: number): void { if (!Number.isSafeInteger(expectedRevision) || expectedRevision < 0) throw new BuddyServiceError('VALIDATION_FAILED') + } + + #requireConversation(conversationId: string, expectedRevision?: number) { + this.#assertAvailable() const conversation = this.#options.conversations.findById(conversationId) if (!conversation) throw new ConversationArchiveConflictError('OBJECT_DELETED') - if (conversation.metadataRevision !== expectedRevision) + if (expectedRevision !== undefined && conversation.metadataRevision !== expectedRevision) throw new ConversationArchiveConflictError('SOURCE_CHANGED') return conversation } - #requireActive(conversationId: string, expectedRevision: number) { - const conversation = this.#requireConversation(conversationId, expectedRevision) - if (conversation.deletedAt) - throw new ConversationArchiveConflictError('OBJECT_DELETED') - return conversation - } - #now(): string { return (this.#options.now?.() ?? new Date()).toISOString() } + + #publish(conversationId: string): void { + const publish = () => this.#changes.fire(conversationId) + if (this.#options.afterCommit) + this.#options.afterCommit(publish) + else + publish() + } } diff --git a/apps/buddy/service/src/conversations/ConversationCleanupService.ts b/apps/buddy/service/src/conversations/ConversationCleanupService.ts index 36bd88bd1..269d8e8c6 100644 --- a/apps/buddy/service/src/conversations/ConversationCleanupService.ts +++ b/apps/buddy/service/src/conversations/ConversationCleanupService.ts @@ -5,12 +5,14 @@ import type { ConversationArchiveService } from './ConversationArchiveService' import type { ConversationLifecycleService } from './ConversationLifecycleService' import { conversationCleanupDeleteSchema, conversationCleanupQuerySchema } from '../../../shared/conversation/conversationCleanupApi' import { parse } from '../rpc/runtimeRequest' +import { ExecutionResourceQueries } from '../storage/ExecutionResourceQueries' import { ConversationArchiveConflictError } from './ConversationArchiveService' interface Options { database: DatabaseSync archive: ConversationArchiveService lifecycle: ConversationLifecycleService + execute: (input: ConversationArchiveTarget) => Promise } export class ConversationCleanupService { @@ -23,13 +25,10 @@ export class ConversationCleanupService { list(input: ConversationCleanupQuery): ConversationCleanupPage { const { cursor, limit } = parse(conversationCleanupQuerySchema, input) const page = this.#options.archive.list({ cursor, limit, kind: 'removed' }) + const resources = new ExecutionResourceQueries(this.#options.database).conversations(page.items.map(item => item.id)) return { items: page.items.map((item) => { - const busy = this.#options.lifecycle.isCleanupPending(item.id) - || !!this.#options.database.prepare(`SELECT 1 FROM runs r LEFT JOIN execution_state s ON s.run_id = r.id - WHERE r.conversation_id = ? AND (r.status IN ('queued', 'running') OR s.phase <> 'closed' OR s.cleanup_state <> 'clean') LIMIT 1`).get(item.id) - || !!this.#options.database.prepare(`SELECT 1 FROM run_mutation_leases l JOIN runs r ON r.id = l.run_id - WHERE r.conversation_id = ? AND l.status <> 'released' LIMIT 1`).get(item.id) + const busy = resources.get(item.id) !== 'released' return { conversationId: item.id, title: item.title, @@ -57,7 +56,7 @@ export class ConversationCleanupService { async delete(input: ConversationCleanupDelete): Promise { const { conversationId, expectedRevision } = parse(conversationCleanupDeleteSchema, input) - if (!await this.execute({ conversationId, expectedRevision })) + if (!await this.#options.execute({ conversationId, expectedRevision })) return { status: 'failed', errorCode: 'SOURCE_CHANGED' } await this.#options.lifecycle.settleDeletion(input.conversationId).catch(() => {}) const job = this.#options.database.prepare('SELECT status, error_code FROM conversation_deletion_jobs WHERE conversation_id = ?').get(input.conversationId) diff --git a/apps/buddy/service/src/conversations/ConversationCommandService.ts b/apps/buddy/service/src/conversations/ConversationCommandService.ts new file mode 100644 index 000000000..62eae7df1 --- /dev/null +++ b/apps/buddy/service/src/conversations/ConversationCommandService.ts @@ -0,0 +1,260 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { ConversationArchivePage, ConversationArchivePolicy, ConversationArchiveQuery } from '../../../shared/conversation/conversationArchiveApi' +import type { CommandAdmissionService, CommandResolution } from '../commands/CommandAdmissionService' +import type { InputCommandIntent } from '../commands/commandIntent' +import type { CommandDecision, CommandRejection } from '../storage/CommandCommitter' +import type { ConversationCommandRepository } from '../storage/ConversationCommandRepository' +import type { ConversationDeletionCommit } from '../storage/conversationDeletionRepository' +import type { ConversationLifecycleService } from './ConversationLifecycleService' +import type { ConversationMetadataService } from './ConversationMetadataService' +import type { TaskMarkService } from './TaskMarkService' +import { ProductCommandRequiredError } from '../rpc/candidateLegacyRegistrar' +import { BuddyServiceError } from '../rpc/runtimeRequest' +import { ChatControlConflictError } from '../storage/ChatControlCommandRepository' +import { afterTransactionCommit, assertCommittedRead } from '../storage/transaction' +import { ConversationArchiveConflictError } from './ConversationArchiveService' +import { ConversationLifecycleError } from './ConversationLifecycleService' + +export type ConversationCommandMethod + = | 'conversations.rename' | 'conversations.delete' | 'conversations.activateBranch' + | 'conversations.archive' | 'conversations.restoreArchive' | 'conversations.deleteArchived' | 'conversations.deleteLegacyRemoved' | 'conversations.setArchivePolicy' + | 'conversations.setPermissionSettings' | 'conversations.setModelSelection' | 'conversations.setBuddy' + | 'conversations.addMember' | 'conversations.removeMember' | 'conversations.moveToSpace' + | 'taskMarks.create' | 'taskMarks.update' | 'taskMarks.delete' | 'taskMarks.assign' | 'taskMarks.setRead' | 'taskMarks.clear' + +type MetadataCommands = Pick + +export interface ConversationArchiveCommands { + withMutationScope: (id: string, operation: () => Promise) => Promise + prepareArchive: (id: string, expectedRevision?: number) => Promise + commitArchive: (id: string, expectedRevision?: number) => boolean + commitRestore: (id: string, expectedRevision?: number) => boolean + commitDelete: (id: string, expectedRevision?: number) => ConversationDeletionCommit + commitDeleteLegacyRemoved: (id: string, expectedRevision?: number) => ConversationDeletionCommit + finishDeletion: (commit: ConversationDeletionCommit) => Promise + list: (input: ConversationArchiveQuery) => ConversationArchivePage + getPolicy: () => ConversationArchivePolicy + setPolicy: (days: ConversationArchivePolicy['days']) => ConversationArchivePolicy +} + +interface ConversationCommandServiceOptions { + database: DatabaseSync + admission: Pick + repository: ConversationCommandRepository + archive?: ConversationArchiveCommands + metadata: MetadataCommands + lifecycle: Pick + marks: Pick + onEffectError: (error: unknown) => void +} + +interface PreparedMetadataCommand { + commit: () => CommandDecision +} + +export class ConversationCommandService { + readonly #options: ConversationCommandServiceOptions + readonly #effects = new Set>() + + constructor(options: ConversationCommandServiceOptions) { + this.#options = options + } + + execute(method: ConversationCommandMethod, input: unknown): Promise { + return this.#options.admission.execute(method, input, { + scope: (intent, operation) => { + if (method === 'conversations.archive' && 'conversationId' in intent) + return this.#archive().withMutationScope(intent.conversationId, operation) + if (method === 'conversations.moveToSpace' && 'conversationId' in intent) + return this.#options.metadata.withMutationScope(intent.conversationId, operation) + return operation() + }, + prepare: async (intent, signal) => { + signal.throwIfAborted() + const prepared = await this.#prepare(method, intent) + signal.throwIfAborted() + return prepared + }, + reject: rejectMetadata, + commit: prepared => prepared.commit(), + }) + } + + listArchived(input: ConversationArchiveQuery): ConversationArchivePage { + assertCommittedRead(this.#options.database) + return this.#archive().list(input) + } + + getArchivePolicy(): ConversationArchivePolicy { + assertCommittedRead(this.#options.database) + return this.#archive().getPolicy() + } + + #archive(): ConversationArchiveCommands { + if (!this.#options.archive) + throw new ProductCommandRequiredError() + return this.#options.archive + } + + async dispose(): Promise { + await Promise.allSettled(this.#effects) + } + + async #prepare(method: ConversationCommandMethod, intent: Readonly>): Promise { + const { repository, metadata, marks, lifecycle } = this.#options + switch (method) { + case 'conversations.archive': { + const input = intent as InputCommandIntent + const archive = this.#archive() + await archive.prepareArchive(input.conversationId, input.expectedRevision) + return { commit: () => ({ target: { conversationId: input.conversationId }, result: { archived: archive.commitArchive(input.conversationId, input.expectedRevision) } }) } + } + case 'conversations.restoreArchive': { + const input = intent as InputCommandIntent + return { commit: () => ({ target: { conversationId: input.conversationId }, result: { restored: this.#archive().commitRestore(input.conversationId, input.expectedRevision) } }) } + } + case 'conversations.deleteArchived': + case 'conversations.deleteLegacyRemoved': { + const input = intent as InputCommandIntent<'conversations.deleteArchived' | 'conversations.deleteLegacyRemoved'> + return { commit: () => { + const archive = this.#archive() + const committed = method === 'conversations.deleteLegacyRemoved' + ? archive.commitDeleteLegacyRemoved(input.conversationId, input.expectedRevision) + : archive.commitDelete(input.conversationId, input.expectedRevision) + this.#afterCommit(() => archive.finishDeletion(committed)) + return { target: { conversationId: input.conversationId }, result: { deleted: true, deletedAt: committed.deletedAt, cleanup: 'pending' } } + } } + } + case 'conversations.setArchivePolicy': { + const input = intent as InputCommandIntent + return { commit: () => ({ target: {}, result: { archivePolicy: { ...this.#archive().setPolicy(input.days) } } }) } + } + case 'conversations.rename': { + const input = intent as InputCommandIntent + return { commit: () => { + repository.assertConversation(input.conversationId, input.expectedTitleRevision, true) + metadata.rename({ id: input.conversationId, title: input.title }) + return { target: { conversationId: input.conversationId }, result: { titleRevision: repository.conversationRevision(input.conversationId, true) } } + } } + } + case 'conversations.delete': { + const input = intent as InputCommandIntent + return { commit: () => { + repository.assertConversation(input.conversationId, input.expectedRevision) + const committed = lifecycle.commitDelete(input.conversationId) + this.#afterCommit(() => lifecycle.finishDeletion(committed)) + return { target: { conversationId: input.conversationId }, result: { deletedAt: committed.deletedAt, cleanup: 'pending' } } + } } + } + case 'conversations.setModelSelection': { + const input = intent as InputCommandIntent + repository.assertConversation(input.conversationId, input.expectedRevision) + const assertRun = () => { + if (input.targetRunId) + repository.assertRunModel({ ...input, targetRunId: input.targetRunId, expectedRunGeneration: input.expectedRunGeneration!, expectedRunModelSelection: input.expectedRunModelSelection! }) + } + assertRun() + const prepared = await metadata.prepareModelSelection({ id: input.conversationId, actorId: input.actorId, runId: input.targetRunId, modelSelection: input.modelSelection }) + return { commit: () => { + repository.assertConversation(input.conversationId, input.expectedRevision) + assertRun() + if (!input.targetRunId) + repository.assertActiveRunModel({ conversationId: input.conversationId, actorId: prepared.actorId, expectedActiveRun: input.expectedActiveRun! }) + metadata.commitModelSelection(prepared) + return { target: { conversationId: input.conversationId, actorId: prepared.actorId, ...(input.targetRunId ? { runId: input.targetRunId } : {}) }, result: { revision: repository.conversationRevision(input.conversationId), modelSelection: prepared.selection ? { ...prepared.selection } : null } } + } } + } + case 'conversations.moveToSpace': { + const input = intent as InputCommandIntent + repository.assertConversation(input.conversationId, input.expectedRevision) + const prepared = await metadata.prepareSpaceMove({ id: input.conversationId, spaceId: input.spaceId }) + return { commit: () => { + repository.assertConversation(input.conversationId, input.expectedRevision) + metadata.commitSpaceMove(prepared) + return { target: { conversationId: input.conversationId }, result: { revision: repository.conversationRevision(input.conversationId) } } + } } + } + case 'conversations.activateBranch': + case 'conversations.setPermissionSettings': + case 'conversations.setBuddy': + case 'conversations.addMember': + case 'conversations.removeMember': { + const input = intent as InputCommandIntent<'conversations.activateBranch' | 'conversations.setPermissionSettings' | 'conversations.setBuddy' | 'conversations.addMember' | 'conversations.removeMember'> + return { commit: () => { + repository.assertConversation(input.conversationId, input.expectedRevision) + if ('branchId' in input) + metadata.activateBranch(input) + else if ('approvalPolicy' in input) + metadata.commitPermissionSettings({ ...input, id: input.conversationId }) + else if (method === 'conversations.addMember') + metadata.addMember({ id: input.conversationId, buddyId: input.buddyId }) + else if (method === 'conversations.removeMember') + metadata.removeMember({ id: input.conversationId, buddyId: input.buddyId }) + else + metadata.setBuddy({ id: input.conversationId, buddyId: input.buddyId }) + return { target: { conversationId: input.conversationId, ...('branchId' in input ? { branchId: input.branchId } : {}) }, result: { revision: repository.conversationRevision(input.conversationId) } } + } } + } + case 'taskMarks.create': { + const input = intent as InputCommandIntent + return { commit: () => { + const mark = marks.create(input) + return { target: { markId: mark.id }, result: { revision: repository.markRevision(mark.id) } } + } } + } + case 'taskMarks.update': { + const input = intent as InputCommandIntent + return { commit: () => { + repository.assertMark(input.id, input.expectedRevision) + marks.update(input.id, input) + return { target: { markId: input.id }, result: { revision: repository.markRevision(input.id) } } + } } + } + case 'taskMarks.delete': { + const input = intent as InputCommandIntent + return { commit: () => { + repository.assertMark(input.id, input.expectedRevision) + marks.delete(input.id) + return { target: { markId: input.id }, result: { deleted: true } } + } } + } + case 'taskMarks.assign': { + const input = intent as InputCommandIntent + return { commit: () => { + repository.assertAssignment(input.conversationId, input.expectedAssignmentRevision) + if (input.markId) + repository.markRevision(input.markId) + marks.assign(input.conversationId, input.markId) + return { target: { conversationId: input.conversationId }, result: { assignmentRevision: repository.assignmentRevision(input.conversationId) } } + } } + } + case 'taskMarks.setRead': + case 'taskMarks.clear': { + const input = intent as InputCommandIntent<'taskMarks.setRead' | 'taskMarks.clear'> + return { commit: () => { + repository.assertRead(input) + if ('expectedAssignmentRevision' in input) + repository.assertAssignment(input.conversationId, input.expectedAssignmentRevision) + const state = 'read' in input ? marks.setRead(input) : marks.clear(input) + return { target: { conversationId: input.conversationId }, result: { readRevision: state.readRevision, assignmentRevision: repository.assignmentRevision(input.conversationId), resultRunId: state.resultRunId } } + } } + } + } + } + + #afterCommit(effect: () => unknown | Promise): void { + afterTransactionCommit(this.#options.database, () => { + const pending = Promise.resolve().then(effect).then(() => {}, this.#options.onEffectError).finally(() => this.#effects.delete(pending)) + this.#effects.add(pending) + }, this.#options.onEffectError) + } +} + +function rejectMetadata(error: unknown): CommandRejection | null { + if (error instanceof ChatControlConflictError || error instanceof ConversationArchiveConflictError) + return { errorCode: error.code, target: {}, result: {} } + if ((error instanceof Error && error.name === 'ConversationRepositoryError') || error instanceof BuddyServiceError || error instanceof ConversationLifecycleError) + return { errorCode: 'SOURCE_CHANGED', target: {}, result: {} } + return null +} diff --git a/apps/buddy/service/src/conversations/ConversationLifecycleService.ts b/apps/buddy/service/src/conversations/ConversationLifecycleService.ts index 836ffa0a9..70cc20da9 100644 --- a/apps/buddy/service/src/conversations/ConversationLifecycleService.ts +++ b/apps/buddy/service/src/conversations/ConversationLifecycleService.ts @@ -5,7 +5,6 @@ import { randomUUID } from 'node:crypto' import { readDiagnosticErrorCode } from '../../../shared/diagnostics/applicationDiagnostic' import { Emitter } from '../../../shared/events/Emitter' import { copyEventSnapshot } from '../../../shared/events/eventSnapshot' -import { BuddyServiceError } from '../rpc/runtimeRequest' const conversationIdentityPattern = /^[A-Z0-9][\w-]{0,127}$/i @@ -54,7 +53,7 @@ export class ConversationLifecycleService { const current = this.#deleting.get(conversationId) if (current) return current - if (this.#completed.has(conversationId) && !this.#options.deletion.has(conversationId)) + if (this.#completed.has(conversationId)) return Promise.resolve(false) this.#options.assertWritable?.(conversationId) const result = Promise.withResolvers() @@ -82,8 +81,6 @@ export class ConversationLifecycleService { } commitLegacyRemoved(conversationId: string): ConversationDeletionCommit { - if (this.#deleting.has(conversationId)) - throw new BuddyServiceError('CONVERSATION_BUSY') if (this.#stopping || !conversationIdentityPattern.test(conversationId)) throw new ConversationLifecycleError() this.#options.assertWritable?.(conversationId) @@ -113,10 +110,6 @@ export class ConversationLifecycleService { return this.#deleting.size > 0 } - isCleanupPending(conversationId: string): boolean { - return this.#deleting.has(conversationId) - } - finishDeletion(committed: ConversationDeletionCommit): Promise { if (committed.tombstoned || committed.revokedGrantIds.length) this.#committed.fire(copyEventSnapshot({ ...committed, commitId: randomUUID() })) @@ -162,13 +155,10 @@ export class ConversationLifecycleService { async #delete(conversationId: string, reason: ConversationCleanupFact['reason']): Promise { const now = new Date().toISOString() - const shouldPurge = this.#options.deletion.has(conversationId) - const committed = shouldPurge - ? this.#options.deletion.commit(conversationId, now) - : this.#options.deletion.commitSoftDelete(conversationId, now) + const committed = this.#options.deletion.commit(conversationId, now) if (!committed) return false - if (shouldPurge && reason === 'delete') + if (reason === 'delete') this.#options.deletion.retry(conversationId, now) if (committed.tombstoned || committed.revokedGrantIds.length) this.#committed.fire(copyEventSnapshot({ ...committed, commitId: randomUUID() })) @@ -176,15 +166,13 @@ export class ConversationLifecycleService { this.#cleanup.fire(copyEventSnapshot({ conversationId, operationId, reason, status: 'started' })) try { await this.suspendConversation(conversationId) - if (shouldPurge) - await this.#options.purge?.(conversationId) + await this.#options.purge?.(conversationId) this.#completed.add(conversationId) this.#cleanup.fire(copyEventSnapshot({ conversationId, operationId, reason, status: 'completed' })) return committed.tombstoned } catch (error) { - if (shouldPurge) - this.#options.deletion.failed(conversationId, readDiagnosticErrorCode(error), new Date().toISOString()) + this.#options.deletion.failed(conversationId, readDiagnosticErrorCode(error), new Date().toISOString()) this.#cleanup.fire(copyEventSnapshot({ conversationId, operationId, reason, status: 'failed', errorCode: readDiagnosticErrorCode(error) })) throw error } diff --git a/apps/buddy/service/src/conversations/ConversationMetadataService.ts b/apps/buddy/service/src/conversations/ConversationMetadataService.ts index 786dc8c36..638acdbce 100644 --- a/apps/buddy/service/src/conversations/ConversationMetadataService.ts +++ b/apps/buddy/service/src/conversations/ConversationMetadataService.ts @@ -1,3 +1,4 @@ +import type { DatabaseSync } from 'node:sqlite' import type { EventSnapshot } from '../../../shared/events/eventTypes' import type { BuddiesService } from '../buddies/BuddiesService' import type { ConversationModelSelection, ConversationRecord } from '../storage/conversationRecord' @@ -13,6 +14,7 @@ import { copyEventSnapshot } from '../../../shared/events/eventSnapshot' import { BuddyServiceError } from '../rpc/runtimeRequest' import { requireActiveSpace } from '../spaces/requireActiveSpace' import { createSpaceExecutionContext, matchesSpaceExecutionContext } from '../spaces/spaceExecutionContext' +import { afterTransactionCommit } from '../storage/transaction' export type ConversationMetadataCommit = EventSnapshot<{ commitId: string @@ -24,6 +26,7 @@ export type ConversationMetadataCommit = EventSnapshot<{ }> export interface ConversationMetadataServiceOptions { + database?: DatabaseSync buddies?: Pick runInputs?: Pick runs?: Pick @@ -41,7 +44,6 @@ export interface ConversationMetadataServiceOptions { applyPermissionSettings?: (conversation: ConversationRecord) => Promise<{ pending: number, degraded: number }> refreshModelSelection?: (runId: string) => void } - assertWritable?: (conversationId: string) => void onObserverError?: (error: unknown) => void } @@ -49,6 +51,7 @@ export class ConversationMetadataService { readonly #options: ConversationMetadataServiceOptions readonly #committed: Emitter readonly #pendingPermissions = new Map() + readonly #spaceApplications = new Map>() readonly #pending = new Set>() readonly onDidCommit: Emitter['event'] #disposed = false @@ -64,7 +67,7 @@ export class ConversationMetadataService { } rename(input: RenameConversationInput): ConversationRecord { - this.#requireWritable(input.id) + this.#requireActive(input.id) const conversation = this.#options.repository.rename(input) this.#publish('title', conversation) return conversation @@ -73,10 +76,6 @@ export class ConversationMetadataService { renameGenerated(input: RenameConversationInput & { expectedRevision: number, userInitiated?: boolean }): ConversationRecord | null { if (this.#disposed) throw new BuddyServiceError('VALIDATION_FAILED') - const current = this.#options.repository.findById(input.id) - if (!current || current.deletedAt || current.archivedAt) - return null - this.#options.assertWritable?.(input.id) const conversation = this.#options.repository.renameGenerated(input) if (conversation) this.#publish('title', conversation) @@ -88,42 +87,49 @@ export class ConversationMetadataService { } async #setPermissionSettings(input: Omit): Promise { + const conversation = this.commitPermissionSettings(input, false) + await this.#applyPermissions(input.id) + return conversation + } + + commitPermissionSettings(input: Omit, deferApplication = true): ConversationRecord { const { id, approvalPolicy, executionProfile } = input - const current = this.#requireWritable(id) - if (current.approvalPolicy === approvalPolicy && current.executionProfile === executionProfile) { - await this.#applyPermissions(id) - return current - } - const conversation = this.#options.repository.setPermissionSettings({ id, approvalPolicy, executionProfile, updatedAt: new Date().toISOString() }) + const current = this.#requireActive(id) + const conversation = current.approvalPolicy === approvalPolicy && current.executionProfile === executionProfile + ? current + : this.#options.repository.setPermissionSettings({ id, approvalPolicy, executionProfile, updatedAt: new Date().toISOString() }) if (!conversation) throw new BuddyServiceError('VALIDATION_FAILED') - this.#pendingPermissions.set(id, Symbol('permission-invalidation')) - this.#publish('permissions', conversation) - await this.#applyPermissions(id) + if (conversation !== current) { + this.#afterCommit(() => this.#pendingPermissions.set(id, Symbol('permission-invalidation'))) + this.#publish('permissions', conversation) + } + if (deferApplication) + this.#afterCommit(() => this.#run(() => this.#applyPermissions(id))) return conversation } listMembers(conversationId: string) { - this.#requireReadable(conversationId) - return this.#options.repository.listMembers(conversationId).map(({ actorId, joinedAt }) => ({ actorId, joinedAt })) + const conversation = this.#requireReadable(conversationId) + return this.#options.repository.listMembers(conversationId).map(({ actorId, joinedAt }) => ({ actorId, joinedAt, ...(conversation.metadataRevision !== undefined ? { metadataRevision: conversation.metadataRevision } : {}) })) } addMember(input: { id: string, buddyId: string }) { - this.#requireWritable(input.id) + this.#requireActive(input.id) this.#options.repository.addMember({ ...input, updatedAt: new Date().toISOString() }) this.#publish('buddy', this.#requireActive(input.id)) return this.listMembers(input.id) } removeMember(input: { id: string, buddyId: string }) { - this.#requireWritable(input.id) + this.#requireActive(input.id) this.#options.repository.removeMember({ ...input, updatedAt: new Date().toISOString() }) this.#publish('buddy', this.#requireActive(input.id)) return this.listMembers(input.id) } setBuddy(input: { id: string, buddyId: string }): ConversationRecord { - const current = this.#requireWritable(input.id) + const current = this.#requireActive(input.id) const conversation = this.#options.repository.setBuddy({ ...input, updatedAt: new Date().toISOString() }) if (!conversation) throw new BuddyServiceError('VALIDATION_FAILED') @@ -141,7 +147,6 @@ export class ConversationMetadataService { const spaces = this.#options.spaces if (!moves || !spaces) throw new BuddyServiceError('VALIDATION_FAILED') - this.#options.assertWritable?.(input.id) return moves.mutations.run(input.id, async () => { const current = this.#requireActive(input.id) requireActiveSpace(spaces.findById(input.spaceId)) @@ -175,6 +180,63 @@ export class ConversationMetadataService { }) } + withMutationScope(conversationId: string, operation: () => Promise): Promise { + const scoped = async () => { + const result = await operation() + await this.#spaceApplications.get(conversationId)?.catch(() => {}) + return result + } + return this.#options.spaceMoves?.mutations.run(conversationId, scoped) ?? scoped() + } + + async prepareSpaceMove(input: { id: string, spaceId: string }) { + const current = this.#requireActive(input.id) + const moves = this.#options.spaceMoves + if (!moves || !this.#options.spaces) + throw new BuddyServiceError('VALIDATION_FAILED') + if (current.spaceId !== null && current.spaceId !== input.spaceId) + throw new BuddyServiceError('CONVERSATION_SPACE_CONFLICT') + this.#assertMoveIdle(input.id) + const context = createSpaceExecutionContext(requireActiveSpace(this.#options.spaces.findById(input.spaceId))) + await moves.validateTarget(current, input.spaceId) + return { ...input, context } + } + + commitSpaceMove(input: Awaited>): ConversationRecord { + const current = this.#requireActive(input.id) + this.#assertMoveIdle(input.id) + if (!this.#options.spaces || !matchesSpaceExecutionContext(requireActiveSpace(this.#options.spaces.findById(input.spaceId)), input.context)) + throw new BuddyServiceError('DIRECTORY_NOT_AUTHORIZED') + if (current.spaceId === input.spaceId) + return current + if (current.spaceId !== null) + throw new BuddyServiceError('CONVERSATION_SPACE_CONFLICT') + const conversation = this.#options.repository.setSpace({ id: input.id, spaceId: input.spaceId, updatedAt: new Date().toISOString() }) + if (!conversation) + throw new BuddyServiceError('CONVERSATION_SPACE_CONFLICT') + this.#publish('space', conversation) + this.#afterCommit(() => { + const applied = this.#run(async () => { + const result = await this.#options.sessions.invalidateConversation(input.id) + if (result.pending || result.degraded) + throw new ConversationSettingsApplicationError(result.degraded ? 'CONVERSATION_SETTINGS_FAILED' : 'CONVERSATION_SETTINGS_PENDING') + }).finally(() => { + if (this.#spaceApplications.get(input.id) === applied) + this.#spaceApplications.delete(input.id) + }) + this.#spaceApplications.set(input.id, applied) + return applied + }) + return conversation + } + + #assertMoveIdle(conversationId: string): void { + if (this.#options.runs?.findActiveForConversation(conversationId) || this.#options.spaceMoves?.hasActiveExecution(conversationId)) + throw new BuddyServiceError('CONVERSATION_BUSY') + if (this.#options.spaceMoves?.hasPendingMessages(conversationId)) + throw new BuddyServiceError('CONVERSATION_QUEUE_NOT_EMPTY') + } + getModelControls(conversationId: string, actorId?: string) { const conversation = this.#requireReadable(conversationId) const nextActorId = actorId ?? conversation.buddyId @@ -184,7 +246,8 @@ export class ConversationMetadataService { throw new BuddyServiceError('BUDDY_NOT_ACTOR') const member = this.#options.repository.findMember(conversationId, nextActorId) return { - current: run ? { runId: run.id, actorId: run.buddyId, modelSelection: this.#runModelSelection(run) } : null, + ...(conversation.metadataRevision !== undefined ? { metadataRevision: conversation.metadataRevision } : {}), + current: run ? { runId: run.id, ...(conversation.metadataRevision !== undefined ? { generation: run.generation } : {}), actorId: run.buddyId, modelSelection: this.#runModelSelection(run) } : null, next: { actorId: nextActorId, isMember: Boolean(member), modelSelection: member?.modelSelection ?? (nextActorId === conversation.buddyId ? conversation.modelSelection : null) ?? buddy?.settings.model ?? null }, } } @@ -195,23 +258,23 @@ export class ConversationMetadataService { } setModelSelection(input: { id: string, actorId?: string, runId?: string, modelSelection: ConversationModelSelection | null }): Promise { - return this.#run(() => this.#setModelSelection({ + return this.#run(async () => this.commitModelSelection(await this.prepareModelSelection({ id: input.id, actorId: input.actorId, runId: input.runId, modelSelection: input.modelSelection === null ? null : { ...input.modelSelection }, - })) + }))) } - async #setModelSelection(input: { id: string, actorId?: string, runId?: string, modelSelection: ConversationModelSelection | null }): Promise { + async prepareModelSelection(input: { id: string, actorId?: string, runId?: string, modelSelection: ConversationModelSelection | null }) { const id = input.id const actorId = input.actorId ?? this.#targetRunActor(input.runId, id) ?? this.#requireActive(id).buddyId this.#targetRunActor(input.runId, id, actorId) - this.#requireWritable(id) + this.#requireActive(id) if (!input.runId && !this.#options.repository.findMember(id, actorId)) throw new BuddyServiceError('VALIDATION_FAILED') const resolved = input.modelSelection === null ? null : await this.#options.resolveModelSelection({ ...input.modelSelection }) - const current = this.#requireWritable(id) + this.#requireActive(id) const selection: ConversationModelSelection | null = resolved === null ? null : { @@ -220,12 +283,18 @@ export class ConversationMetadataService { reasoning: resolved.reasoning, serviceTier: resolved.serviceTier, } + return { id, actorId, runId: input.runId, selection } + } + + commitModelSelection(input: Awaited>): ConversationRecord { + const { id, actorId, selection } = input + const current = this.#requireActive(id) if (input.runId) { this.#targetRunActor(input.runId, id, actorId) if (!this.#options.runs?.setModelSelection(input.runId, selection)) throw new BuddyServiceError('VALIDATION_FAILED') this.#publish('model', current, actorId) - this.#options.sessions.refreshModelSelection?.(input.runId) + this.#afterCommit(() => this.#options.sessions.refreshModelSelection?.(input.runId!)) return current } const member = this.#options.repository.findMember(id, actorId) @@ -252,7 +321,7 @@ export class ConversationMetadataService { return false if (!this.#options.runs?.setModelSelection(run.id, selection)) return false - this.#options.sessions.refreshModelSelection?.(run.id) + this.#afterCommit(() => this.#options.sessions.refreshModelSelection?.(run.id)) return true } @@ -266,7 +335,7 @@ export class ConversationMetadataService { } activateBranch(input: { conversationId: string, branchId: string }): ConversationRecord { - const current = this.#requireWritable(input.conversationId) + const current = this.#requireActive(input.conversationId) if (current.activeBranchId === input.branchId) return current const conversation = this.#options.repository.activateBranch({ ...input, updatedAt: new Date().toISOString() }) @@ -295,11 +364,6 @@ export class ConversationMetadataService { return pending.promise } - #requireWritable(id: string): ConversationRecord { - this.#options.assertWritable?.(id) - return this.#requireActive(id) - } - #requireActive(id: string): ConversationRecord { const conversation = this.#requireReadable(id) if (conversation.archivedAt) @@ -327,15 +391,24 @@ export class ConversationMetadataService { this.#pendingPermissions.delete(id) } + #afterCommit(operation: () => unknown): void { + const onError = this.#options.onObserverError ?? (() => {}) + if (this.#options.database) + afterTransactionCommit(this.#options.database, operation, onError) + else + void Promise.resolve(operation()).catch(onError) + } + #publish(kind: ConversationMetadataCommit['kind'], conversation: ConversationRecord, actorId?: string): void { const title = kind === 'title' ? this.#options.repository.getTitleState(conversation.id) : null - this.#committed.fire(copyEventSnapshot({ + const event = copyEventSnapshot({ commitId: randomUUID(), kind, conversation, ...(actorId ? { actorId } : {}), ...(title ? { titleRevision: title.revision, titleSource: title.source } : {}), - })) + }) + this.#afterCommit(() => this.#committed.fire(event)) } } diff --git a/apps/buddy/service/src/conversations/ConversationMutationGuard.ts b/apps/buddy/service/src/conversations/ConversationMutationGuard.ts index 9c80c3a5e..ab114afa8 100644 --- a/apps/buddy/service/src/conversations/ConversationMutationGuard.ts +++ b/apps/buddy/service/src/conversations/ConversationMutationGuard.ts @@ -3,10 +3,6 @@ import { BuddyServiceError } from '../rpc/runtimeRequest' export class ConversationMutationGuard { readonly #changing = new Set() - get hasPendingOperations(): boolean { - return this.#changing.size > 0 - } - assertWritable(conversationId: string): void { if (this.#changing.has(conversationId)) throw new BuddyServiceError('CONVERSATION_BUSY') diff --git a/apps/buddy/service/src/conversations/ConversationPurgeService.ts b/apps/buddy/service/src/conversations/ConversationPurgeService.ts index 2313f6d57..39c60161e 100644 --- a/apps/buddy/service/src/conversations/ConversationPurgeService.ts +++ b/apps/buddy/service/src/conversations/ConversationPurgeService.ts @@ -1,4 +1,5 @@ import type { DatabaseSync } from 'node:sqlite' +import type { ConversationStorageAuthority } from '../storage/conversationPurgeRepository' import { lstatSync, readdirSync, realpathSync, rmdirSync, unlinkSync } from 'node:fs' import { join, relative, resolve, sep } from 'node:path' import { BuddyDataPaths } from '../storage/BuddyDataPaths' @@ -8,6 +9,7 @@ import { ConversationPurgeError, ConversationPurgeRepository, isPurgeDescendant export interface ConversationPurgeOptions { database: DatabaseSync home: string + storageAuthority: ConversationStorageAuthority } export class ConversationPurgeService { @@ -17,7 +19,7 @@ export class ConversationPurgeService { constructor(options: ConversationPurgeOptions) { this.paths = new BuddyDataPaths(options.home) - this.repository = new ConversationPurgeRepository(options.database, this.paths) + this.repository = new ConversationPurgeRepository(options.database, this.paths, options.storageAuthority) this.deletion = createConversationDeletionRepository(options.database) } @@ -29,10 +31,22 @@ export class ConversationPurgeService { if (!manifest) return const directories = this.repository.directories(id, manifest) + const stagingFiles = this.repository.stagingFiles(manifest) for (const directory of directories) this.inspect(directory) + for (const path of stagingFiles) { + this.assertPath(path) + const stat = this.stat(path) + if (stat && !stat.isFile()) + throw new ConversationPurgeError('CONVERSATION_FILE_BOUNDARY') + } for (const directory of directories) this.remove(directory) + for (const path of stagingFiles) { + this.assertPath(path) + if (this.stat(path)) + unlinkSync(path) + } this.repository.finish(id, manifest) } catch (error) { diff --git a/apps/buddy/service/src/conversations/ExtensionTaskContext.ts b/apps/buddy/service/src/conversations/ExtensionTaskContext.ts index e323024db..1ee421e7b 100644 --- a/apps/buddy/service/src/conversations/ExtensionTaskContext.ts +++ b/apps/buddy/service/src/conversations/ExtensionTaskContext.ts @@ -45,9 +45,9 @@ export class ExtensionTaskContext { return false if (cause.type === 'user') return true - if (cause.data.conversationId !== conversationId || task.activeBranchId !== cause.data.branchId) + if (cause.data.conversationId !== conversationId) return false - const inputId = this.history.latestInput(conversationId, task.activeBranchId) + const inputId = this.history.latestInput(conversationId, cause.data.branchId) return cause.type === 'task:turn:completed' ? inputId === cause.data.triggeringMessageId || (!!inputId && this.inputs.findByMessageId(inputId)?.runId === cause.data.runId) : inputId === cause.data.messageId @@ -59,7 +59,9 @@ export class ExtensionTaskContext { const task = this.tasks.findById(conversationId) if (!task || task.deletedAt !== null || task.archivedAt || !task.activeBranchId || (scope.action && !this.valid(conversationId, scope.action.cause))) throw new Error('EXTENSION_TASK_UNAVAILABLE') - const branchId = task.activeBranchId + const branchId = scope.runId ? this.runs.findById(scope.runId)?.branchId : scope.action?.cause.type !== 'user' && scope.action ? scope.action.cause.data.branchId : task.activeBranchId + if (!branchId) + throw new Error('EXTENSION_TASK_UNAVAILABLE') const inputMessageId = this.history.latestInput(conversationId, branchId) const runId = scope.runId ?? (scope.action && scope.action.cause.type !== 'user' ? scope.action.cause.data.runId : inputMessageId ? this.inputs.findByMessageId(inputMessageId)?.runId : null) const run = runId ? this.runs.findById(runId) : null @@ -70,7 +72,7 @@ export class ExtensionTaskContext { const assertCurrent = () => { scope.signal.throwIfAborted() const current = this.tasks.findById(conversationId) - if (!current || current.deletedAt !== null || current.archivedAt || current.activeBranchId !== branchId || this.history.latestInput(conversationId, branchId) !== inputMessageId || (!scope.action && this.runs.findById(scope.runId!)?.status !== 'running')) + if (!current || current.deletedAt !== null || current.archivedAt || this.history.latestInput(conversationId, branchId) !== inputMessageId || (!scope.action && this.runs.findById(scope.runId!)?.status !== 'running')) throw new Error('EXTENSION_TASK_UNAVAILABLE') } return { conversationId, branchId, model: model ? { providerId: model.providerId, modelId: model.modelId } : null, assertCurrent } @@ -79,7 +81,7 @@ export class ExtensionTaskContext { captureAction(conversationId: string, cause: ExtensionActionCause) { if (!this.valid(conversationId, cause)) return null - const branchId = this.tasks.findById(conversationId)!.activeBranchId! + const branchId = cause.type === 'user' ? this.tasks.findById(conversationId)!.activeBranchId! : cause.data.branchId return { branchId, sourceMessageId: this.history.latestInput(conversationId, branchId) } } } diff --git a/apps/buddy/service/src/conversations/SessionReferenceAccess.ts b/apps/buddy/service/src/conversations/SessionReferenceAccess.ts index bd08f2a27..12e4a72b7 100644 --- a/apps/buddy/service/src/conversations/SessionReferenceAccess.ts +++ b/apps/buddy/service/src/conversations/SessionReferenceAccess.ts @@ -107,7 +107,7 @@ export class SessionReferenceAccessResolver { if (!root) return result const current = this.#conversations.findById(root.conversationId) - if (!current || current.deletedAt || current.activeBranchId !== root.branchId) + if (!current || current.deletedAt) return result const blocked = new Map>() const excludes = (id: string, branchId: string, target: string) => { diff --git a/apps/buddy/service/src/conversations/TaskMarkService.ts b/apps/buddy/service/src/conversations/TaskMarkService.ts index 48e9b9930..ac6cd3fa8 100644 --- a/apps/buddy/service/src/conversations/TaskMarkService.ts +++ b/apps/buddy/service/src/conversations/TaskMarkService.ts @@ -1,3 +1,4 @@ +import type { DatabaseSync } from 'node:sqlite' import type { LocalTaskMarkState, TaskMarkClearInput, TaskMarkInput, TaskMarkReadInput } from '../../../shared/conversation/taskMarkApi' import type { EventSnapshot } from '../../../shared/events/eventTypes' import type { TaskMarkRepository } from '../storage/taskMarkRepository' @@ -5,6 +6,7 @@ import { randomUUID } from 'node:crypto' import { Emitter } from '../../../shared/events/Emitter' import { copyEventSnapshot } from '../../../shared/events/eventSnapshot' import { BuddyServiceError } from '../rpc/runtimeRequest' +import { afterTransactionCommit } from '../storage/transaction' export type TaskMarkCommit = EventSnapshot<{ commitId: string } & ( | { kind: 'created' | 'updated', markId: string } @@ -13,12 +15,16 @@ export type TaskMarkCommit = EventSnapshot<{ commitId: string } & ( )> export class TaskMarkService { + readonly #database?: DatabaseSync + readonly #onObserverError: (error: unknown) => void readonly #repository: TaskMarkRepository readonly #committed: Emitter readonly onDidCommit: Emitter['event'] #disposed = false - constructor(repository: TaskMarkRepository, onObserverError: (error: unknown) => void = () => {}) { + constructor(repository: TaskMarkRepository, onObserverError: (error: unknown) => void = () => {}, database?: DatabaseSync) { + this.#database = database + this.#onObserverError = onObserverError this.#repository = repository this.#committed = new Emitter(onObserverError) this.onDidCommit = this.#committed.event @@ -27,7 +33,7 @@ export class TaskMarkService { create(input: TaskMarkInput) { this.#requireActive() const mark = this.#repository.create(input) - this.#committed.fire(copyEventSnapshot({ commitId: randomUUID(), kind: 'created', markId: mark.id })) + this.#publish(copyEventSnapshot({ commitId: randomUUID(), kind: 'created', markId: mark.id })) return mark } @@ -37,7 +43,7 @@ export class TaskMarkService { if (current?.name === input.name && current.description === input.description && current.color === input.color) return current const mark = this.#repository.update(id, input) - this.#committed.fire(copyEventSnapshot({ commitId: randomUUID(), kind: 'updated', markId: mark.id })) + this.#publish(copyEventSnapshot({ commitId: randomUUID(), kind: 'updated', markId: mark.id })) return mark } @@ -45,7 +51,7 @@ export class TaskMarkService { this.#requireActive() const result = this.#repository.deleteWithReceipt(id) if (result.deleted) - this.#committed.fire(copyEventSnapshot({ commitId: randomUUID(), kind: 'deleted', markId: id, conversationIds: result.conversationIds })) + this.#publish(copyEventSnapshot({ commitId: randomUUID(), kind: 'deleted', markId: id, conversationIds: result.conversationIds })) return result.deleted } @@ -80,6 +86,13 @@ export class TaskMarkService { this.#committed.dispose() } + #publish(event: TaskMarkCommit): void { + if (this.#database) + afterTransactionCommit(this.#database, () => this.#committed.fire(event), this.#onObserverError) + else + this.#committed.fire(event) + } + #requireActive(): void { if (this.#disposed) throw new BuddyServiceError('VALIDATION_FAILED') @@ -89,6 +102,6 @@ export class TaskMarkService { const assignmentChanged = before.markId !== state.markId const readChanged = before.readRevision !== state.readRevision if (assignmentChanged || readChanged) - this.#committed.fire(copyEventSnapshot({ commitId: randomUUID(), kind: 'attention', conversationId: state.conversationId, assignmentChanged, readChanged, state })) + this.#publish(copyEventSnapshot({ commitId: randomUUID(), kind: 'attention', conversationId: state.conversationId, assignmentChanged, readChanged, state })) } } diff --git a/apps/buddy/service/src/conversations/__tests__/ConversationArchiveService.spec.ts b/apps/buddy/service/src/conversations/__tests__/ConversationArchiveService.spec.ts index 5b9685594..c60b9cbde 100644 --- a/apps/buddy/service/src/conversations/__tests__/ConversationArchiveService.spec.ts +++ b/apps/buddy/service/src/conversations/__tests__/ConversationArchiveService.spec.ts @@ -6,7 +6,7 @@ import { afterEach, describe, expect, it } from 'vitest' import { createConversationArchiveRepository } from '../../storage/conversationArchiveRepository' import { createConversationDeletionRepository } from '../../storage/conversationDeletionRepository' import { createConversationRepository } from '../../storage/conversationRepository' -import { openBuddyDatabase } from '../../storage/database' +import { openBuddyDatabase, openBuddySqliteDatabase } from '../../storage/database' import { ConversationArchiveService } from '../ConversationArchiveService' import { ConversationLifecycleService } from '../ConversationLifecycleService' import { ConversationMutationGuard } from '../ConversationMutationGuard' @@ -19,8 +19,8 @@ afterEach(() => { for (const directory of directories.splice(0)) rmSync(directory, { recursive: true, force: true }) }) -function fixture(stop: () => Promise = async () => 0) { - const database = openBuddyDatabase({ databasePath: ':memory:' }) +function fixture(storageAuthority: 'legacy_jsonl' | 'sqlite', stop: () => Promise = async () => 0) { + const database = (storageAuthority === 'sqlite' ? openBuddySqliteDatabase : openBuddyDatabase)({ databasePath: ':memory:' }) databases.push(database) const home = mkdtempSync(join(tmpdir(), 'lexora-archive-service-')) directories.push(home) @@ -28,7 +28,7 @@ function fixture(stop: () => Promise = async () => 0) { const repository = createConversationArchiveRepository(database) const deletion = createConversationDeletionRepository(database) const mutations = new ConversationMutationGuard() - const purge = new ConversationPurgeService({ database, home }) + const purge = new ConversationPurgeService({ database, home, storageAuthority }) const lifecycle = new ConversationLifecycleService({ conversations, deletion, assertWritable: id => mutations.assertWritable(id), runner: { cancelAndWaitForConversation: stop }, cancelQueuedRuns: async () => {}, sessions: { invalidateConversation: async () => ({ pending: 0, degraded: 0 }) }, purge: id => purge.purge(id) }) const service = new ConversationArchiveService({ database, repository, conversations, mutations, lifecycle, now: () => new Date('2026-11-15T00:00:00.000Z'), onError: () => {} }) const seed = (id: string) => { @@ -48,9 +48,9 @@ function fixture(stop: () => Promise = async () => 0) { return { database, home, conversations, repository, deletion, mutations, lifecycle, service, seed } } -describe('conversation archive lifecycle', () => { +describe.each(['legacy_jsonl', 'sqlite'] as const)('conversation archive lifecycle (%s)', (storageAuthority) => { it('does not clear archives by default and expires only explicitly archived tasks', async () => { - const f = fixture() + const f = fixture(storageAuthority) const expired = f.seed('expired') const recent = f.seed('recent') const legacy = f.seed('legacy') @@ -72,28 +72,9 @@ describe('conversation archive lifecycle', () => { await f.lifecycle.dispose() }) - it('holds the mutation gate while stopping and rejects stale restore without losing archive data', async () => { - const stop = Promise.withResolvers() - const f = fixture(() => stop.promise) - const directory = f.seed('task') - const revision = f.conversations.findById('task')!.metadataRevision! - const archiving = f.service.archive({ conversationId: 'task', expectedRevision: revision }) - expect(() => f.mutations.assertWritable('task')).toThrow() - expect(f.conversations.findById('task')!.archivedAt).toBeNull() - stop.resolve(0) - expect(await archiving).toBe(true) - expect(existsSync(directory)).toBe(true) - expect(() => f.service.restore({ conversationId: 'task', expectedRevision: revision })).toThrow() - expect(f.conversations.listRecent()).toEqual([]) - expect(f.service.restore({ conversationId: 'task', expectedRevision: f.conversations.findById('task')!.metadataRevision })).toBe(true) - expect(f.conversations.listRecent().map(item => item.id)).toEqual(['task']) - await f.service.dispose() - await f.lifecycle.dispose() - }) - it('persists a stop failure and never turns a legacy tombstone into a purge during recovery', async () => { let fail = true - const f = fixture(async () => { + const f = fixture(storageAuthority, async () => { if (fail) throw new Error('fixture stop failure') return 0 @@ -101,34 +82,16 @@ describe('conversation archive lifecycle', () => { const directory = f.seed('task') const legacy = f.seed('legacy') f.conversations.markDeleted('legacy', '2026-10-01T00:00:00.000Z') - await expect(f.service.deletePermanently({ conversationId: 'task', expectedRevision: f.conversations.findById('task')!.metadataRevision })).resolves.toBe(true) - await f.lifecycle.settleDeletion('task').catch(() => {}) + await expect(f.lifecycle.delete('task')).rejects.toThrow() expect(f.repository.list({ kind: 'deleting' }).items).toMatchObject([{ id: 'task', deletionStatus: 'failed' }]) expect(existsSync(directory)).toBe(true) fail = false await f.lifecycle.recover('legacy') expect(existsSync(legacy)).toBe(true) expect(f.deletion.has('legacy')).toBe(false) - await f.service.deletePermanently({ conversationId: 'task', expectedRevision: f.conversations.findById('task')!.metadataRevision }) - await f.lifecycle.settleDeletion('task') + await f.lifecycle.delete('task') expect(existsSync(directory)).toBe(false) await f.service.dispose() await f.lifecycle.dispose() }) - - it('restricts legacy cleanup to the confirmed removed revision', async () => { - const f = fixture() - const active = f.seed('active') - const legacy = f.seed('legacy') - await expect(f.service.deleteLegacyRemoved({ conversationId: 'active', expectedRevision: f.conversations.findById('active')!.metadataRevision })).rejects.toThrow() - f.conversations.markDeleted('legacy', '2026-10-01T00:00:00.000Z') - const revision = f.conversations.findById('legacy')!.metadataRevision! - await expect(f.service.deleteLegacyRemoved({ conversationId: 'legacy', expectedRevision: revision + 1 })).rejects.toThrow() - expect(await f.service.deleteLegacyRemoved({ conversationId: 'legacy', expectedRevision: revision })).toBe(true) - await f.lifecycle.settleDeletion('legacy') - expect(existsSync(legacy)).toBe(false) - expect(existsSync(active)).toBe(true) - await f.service.dispose() - await f.lifecycle.dispose() - }) }) diff --git a/apps/buddy/service/src/conversations/__tests__/ConversationCleanupService.spec.ts b/apps/buddy/service/src/conversations/__tests__/ConversationCleanupService.spec.ts index 4da02eed4..84ca8c456 100644 --- a/apps/buddy/service/src/conversations/__tests__/ConversationCleanupService.spec.ts +++ b/apps/buddy/service/src/conversations/__tests__/ConversationCleanupService.spec.ts @@ -25,7 +25,7 @@ function fixture() { const repository = createConversationArchiveRepository(database) const deletion = createConversationDeletionRepository(database) const mutations = new ConversationMutationGuard() - const purge = new ConversationPurgeService({ database, home }) + const purge = new ConversationPurgeService({ database, home, storageAuthority: 'legacy_jsonl' }) let fail = false const lifecycle = new ConversationLifecycleService({ conversations, deletion, assertWritable: id => mutations.assertWritable(id), runner: { cancelAndWaitForConversation: async () => { if (fail) @@ -33,7 +33,7 @@ function fixture() { return 0 } }, cancelQueuedRuns: async () => {}, sessions: { invalidateConversation: async () => ({ pending: 0, degraded: 0 }) }, purge: id => purge.purge(id) }) const archive = new ConversationArchiveService({ database, repository, conversations, mutations, lifecycle, onError: () => {} }) - const service = new ConversationCleanupService({ database, archive, lifecycle }) + const service: ConversationCleanupService = new ConversationCleanupService({ database, archive, lifecycle, execute: input => service.execute(input) }) const cleanup = createExtensionTaskCleanup({ list: async input => service.list(input), delete: input => service.delete(input) }) disposers.push(async () => { await archive.dispose() @@ -50,13 +50,27 @@ function fixture() { const path = join(home, 'conversations', id) mkdirSync(path, { recursive: true }) writeFileSync(join(path, 'owned.txt'), 'fixture') - return { conversationId: id, expectedRevision: conversations.findById(id)!.metadataRevision } + return { conversationId: id, expectedRevision: conversations.findById(id)!.metadataRevision! } } - return { database, home, seed, cleanup, deletion, setFailure: (value: boolean) => { + return { database, home, seed, cleanup, service, deletion, setFailure: (value: boolean) => { fail = value } } } +it('reports a removed task with missing execution evidence as blocked and preserves its files', async () => { + const f = fixture() + const target = f.seed('legacy', 'active') + f.database.exec(`INSERT INTO messages (id, conversation_id, branch_id, role, content_json, created_at) + VALUES ('input', 'legacy', 'branch-legacy', 'user', '{}', '2026-01-01T00:00:00Z'); + INSERT INTO runs (id, conversation_id, branch_id, triggering_message_id, provider, model, purpose, status, started_at) + VALUES ('run-legacy', 'legacy', 'branch-legacy', 'input', 'fixture', 'fixture', 'chat', 'queued', '2026-01-01T00:00:00Z')`) + f.database.exec('UPDATE runs SET status = \'completed\'; DELETE FROM execution_state; UPDATE conversations SET deleted_at = \'2026-01-02T00:00:00Z\'') + expect(f.service.list({ scope: 'legacy_removed', limit: 10 }).items).toMatchObject([{ conversationId: 'legacy', canDelete: false, blockedReason: 'CONVERSATION_BUSY' }]) + expect(await f.cleanup.delete(target)).toEqual({ status: 'failed', errorCode: 'SOURCE_CHANGED' }) + expect(f.deletion.has('legacy')).toBe(false) + expect(existsSync(join(f.home, 'conversations', 'legacy', 'owned.txt'))).toBe(true) +}) + it('cancellation creates no jobs and confirmation uses frozen CAS against a real database', async () => { const f = fixture() const target = f.seed('legacy', 'removed') diff --git a/apps/buddy/service/src/conversations/__tests__/ConversationCommandService.spec.ts b/apps/buddy/service/src/conversations/__tests__/ConversationCommandService.spec.ts new file mode 100644 index 000000000..c3c178310 --- /dev/null +++ b/apps/buddy/service/src/conversations/__tests__/ConversationCommandService.spec.ts @@ -0,0 +1,233 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { ConversationModelSelection } from '../../storage/conversationRecord' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { CommandAdmissionService, createLocalCommandPrincipal } from '../../commands/CommandAdmissionService' +import { projectCommandReceipt } from '../../productProtocol/projectCommandReceipt' +import { createBuddyRepository } from '../../storage/buddyRepository' +import { ConversationCommandRepository } from '../../storage/ConversationCommandRepository' +import { createConversationDeletionRepository } from '../../storage/conversationDeletionRepository' +import { createConversationHistoryStore } from '../../storage/conversationHistoryRepository' +import { createConversationRepository } from '../../storage/conversationRepository' +import { openBuddyMigrationCandidate } from '../../storage/database' +import { createRunInputRepository } from '../../storage/runInputRepository' +import { createRunRepository } from '../../storage/runRepository' +import { createSpaceRepository } from '../../storage/spaceRepository' +import { createTaskMarkRepository } from '../../storage/taskMarkRepository' +import { ConversationCommandService } from '../ConversationCommandService' +import { ConversationLifecycleService } from '../ConversationLifecycleService' +import { ConversationMetadataService } from '../ConversationMetadataService' +import { ConversationMutationGuard } from '../ConversationMutationGuard' +import { TaskMarkService } from '../TaskMarkService' + +const fixtures: Array<{ database: DatabaseSync, admission: CommandAdmissionService, commands: ConversationCommandService, metadata: ConversationMetadataService, lifecycle: ConversationLifecycleService, marks: TaskMarkService }> = [] +afterEach(async () => { + vi.restoreAllMocks() + for (const f of fixtures.splice(0)) { + await f.admission.dispose() + await f.commands.dispose() + await f.metadata.dispose() + await f.lifecycle.dispose() + f.marks.dispose() + f.database.close() + } +}) +const now = '2026-11-07T00:00:00.000Z' +const selection: ConversationModelSelection = { providerId: 'fixture', modelId: 'next-model', reasoning: null, serviceTier: null } + +function fixture() { + const database: DatabaseSync = openBuddyMigrationCandidate({ databasePath: ':memory:' }) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + const conversations = createConversationRepository(database) + conversations.create({ id: 'task', branchId: 'branch', spaceId: null, title: 'Initial', approvalPolicy: 'policy', executionProfile: 'workspace_write', createdAt: now }) + database.exec('UPDATE conversations SET title_source = \'fallback\', title_revision = 0 WHERE id = \'task\'') + const repository = new ConversationCommandRepository(database) + const runs = createRunRepository(database) + const markRepository = createTaskMarkRepository(database) + const principal = createLocalCommandPrincipal(database) + const admission = new CommandAdmissionService({ database, principal, sourceInstanceId: 'instance' }) + const resolveModelSelection = vi.fn(async (input: ConversationModelSelection) => input) + const invalidate = vi.fn(async () => ({ pending: 0, degraded: 0 })) + const refresh = vi.fn() + const effects: unknown[] = [] + const spaces = createSpaceRepository(database) + const mutations = new ConversationMutationGuard() + const metadata = new ConversationMetadataService({ database, spaces, spaceMoves: { mutations, hasPendingMessages: () => false, hasActiveExecution: () => false, validateTarget: async () => {} }, repository: conversations, buddies: createBuddyRepository(database), runs, runInputs: createRunInputRepository(database), resolveModelSelection, sessions: { invalidateConversation: invalidate, refreshModelSelection: refresh }, onObserverError: error => effects.push(error) }) + const marks = new TaskMarkService(markRepository, error => effects.push(error), database) + const cancel = vi.fn(async () => 0) + const lifecycle = new ConversationLifecycleService({ conversations, deletion: createConversationDeletionRepository(database), runner: { cancelAndWaitForConversation: cancel }, cancelQueuedRuns: async () => {}, sessions: { invalidateConversation: invalidate } }) + const commands = new ConversationCommandService({ database, admission, repository, metadata, lifecycle, marks, onEffectError: error => effects.push(error) }) + const command = (commandId: string, intent: unknown) => ({ commandId, intent, expectedServiceInstanceId: 'instance', dataGeneration: String(database.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation) }) + const f = { database, spaces, mutations, conversations, repository, runs, admission, resolveModelSelection, invalidate, refresh, metadata, marks, markRepository, lifecycle, cancel, commands, command, effects } + fixtures.push(f) + return f +} + +describe('conditional task metadata commands', () => { + it('protects manual titles with their independent revision and publishes only after the receipt commits', async () => { + const f = fixture() + const metadataRevision = f.repository.conversationRevision('task') + f.metadata.renameGenerated({ id: 'task', title: 'Generated', expectedRevision: 0 }) + expect(f.repository.conversationRevision('task')).toBe(metadataRevision) + const observed: string[] = [] + f.metadata.onDidCommit((event) => { + expect(f.database.isTransaction).toBe(false) + expect(f.database.prepare('SELECT count(*) AS count FROM command_receipts').get()?.count).toBeGreaterThan(0) + observed.push(event.kind) + }) + const rejected = await f.commands.execute('conversations.rename', f.command('stale-title', { conversationId: 'task', title: 'Old edit', expectedTitleRevision: 0 })) + expect(rejected.receipt).toMatchObject({ decision: 'rejected', errorCode: 'SOURCE_CHANGED' }) + const request = f.command('manual-title', { conversationId: 'task', title: 'My title', expectedTitleRevision: 1 }) + const accepted = await f.commands.execute('conversations.rename', request) + expect(accepted.receipt).toMatchObject({ decision: 'accepted', result: { titleRevision: 2 } }) + expect(observed).toEqual(['title']) + expect(f.metadata.renameGenerated({ id: 'task', title: 'Late generated', expectedRevision: 2 })).toBeNull() + expect(f.conversations.findById('task')).toMatchObject({ title: 'My title', updatedAt: now }) + expect(f.repository.conversationRevision('task')).toBe(metadataRevision) + expect(await f.commands.execute('conversations.rename', request)).toEqual({ created: false, receipt: accepted.receipt }) + }) + + it('persists the exact activated branch in receipts and replays it without switching a later selection', async () => { + const f = fixture() + const history = createConversationHistoryStore(f.database).repository + history.createMessage({ id: 'fork-point', conversationId: 'task', branchId: 'branch', runId: null, role: 'user', content: { text: 'Original input' }, createdAt: now }) + history.createBranch({ id: 'fork', conversationId: 'task', parentBranchId: 'branch', forkedFromMessageId: 'fork-point', activate: false, createdAt: now }) + const revision = f.repository.conversationRevision('task') + const request = f.command('activate-fork', { conversationId: 'task', branchId: 'fork', expectedRevision: revision }) + const accepted = await f.commands.execute('conversations.activateBranch', request) + expect(accepted.receipt).toMatchObject({ decision: 'accepted', target: { conversationId: 'task', branchId: 'fork' } }) + expect(projectCommandReceipt(accepted.receipt).target).toEqual({ conversationId: 'task', branchId: 'fork' }) + expect(f.conversations.findById('task')?.activeBranchId).toBe('fork') + const restored = await f.commands.execute('conversations.activateBranch', f.command('restore-original', { conversationId: 'task', branchId: 'branch', expectedRevision: f.repository.conversationRevision('task') })) + expect(restored.receipt).toMatchObject({ decision: 'accepted', target: { conversationId: 'task', branchId: 'branch' } }) + expect(f.admission.lookup({ dataGeneration: request.dataGeneration, commandId: request.commandId })).toEqual({ lookup: 'decided', receipt: accepted.receipt }) + expect(await f.commands.execute('conversations.activateBranch', request)).toEqual({ created: false, receipt: accepted.receipt }) + expect(f.conversations.findById('task')?.activeBranchId).toBe('branch') + const stale = await f.commands.execute('conversations.activateBranch', f.command('stale-fork', { conversationId: 'task', branchId: 'fork', expectedRevision: revision })) + expect(stale.receipt).toMatchObject({ decision: 'rejected', errorCode: 'SOURCE_CHANGED' }) + expect(f.conversations.findById('task')?.activeBranchId).toBe('branch') + }) + + it('rejects a model choice whose metadata revision changed while resolving its model', async () => { + const f = fixture() + const revision = f.repository.conversationRevision('task') + const gate = Promise.withResolvers() + f.resolveModelSelection.mockReturnValueOnce(gate.promise) + const request = f.command('model', { conversationId: 'task', expectedRevision: revision, expectedActiveRun: null, modelSelection: selection }) + const model = f.commands.execute('conversations.setModelSelection', request) + await vi.waitFor(() => expect(f.resolveModelSelection).toHaveBeenCalled()) + f.invalidate.mockImplementationOnce(async () => { + expect(f.database.isTransaction).toBe(false) + expect(f.admission.lookup({ dataGeneration: request.dataGeneration, commandId: 'permissions' }).lookup).toBe('decided') + return { pending: 0, degraded: 0 } + }) + const permission = await f.commands.execute('conversations.setPermissionSettings', f.command('permissions', { conversationId: 'task', expectedRevision: revision, approvalPolicy: 'manual', executionProfile: 'workspace_write' })) + expect(permission.receipt.decision).toBe('accepted') + gate.resolve(selection) + const rejected = await model + expect(rejected.receipt).toMatchObject({ decision: 'rejected', errorCode: 'SOURCE_CHANGED' }) + expect(f.conversations.findById('task')).toMatchObject({ approvalPolicy: 'manual', modelSelection: null }) + expect(await f.commands.execute('conversations.setModelSelection', request)).toEqual({ created: false, receipt: rejected.receipt }) + }) + + it('rejects an actor model change if a run starts during asynchronous preparation', async () => { + const f = fixture() + const gate = Promise.withResolvers() + f.resolveModelSelection.mockReturnValueOnce(gate.promise) + const request = f.command('new-active-run', { conversationId: 'task', actorId: 'default', expectedRevision: f.repository.conversationRevision('task'), expectedActiveRun: null, modelSelection: selection }) + const pending = f.commands.execute('conversations.setModelSelection', request) + await vi.waitFor(() => expect(f.resolveModelSelection).toHaveBeenCalled()) + seedRun(f) + gate.resolve(selection) + const rejected = await pending + expect(rejected.receipt).toMatchObject({ decision: 'rejected', errorCode: 'SOURCE_CHANGED' }) + expect(f.conversations.findById('task')?.modelSelection).toBeNull() + expect(f.runs.findById('run')?.modelOverride).toBeNull() + expect(await f.commands.execute('conversations.setModelSelection', request)).toEqual({ created: false, receipt: rejected.receipt }) + }) + + it('projects candidate read versions and atomically protects both actor and live model choices', async () => { + const f = fixture() + seedRun(f) + const controls = f.metadata.getModelControls('task', 'default') + expect(controls).toMatchObject({ metadataRevision: f.repository.conversationRevision('task'), current: { runId: 'run', generation: 0 } }) + expect(f.conversations.listRecent(10)[0]).toMatchObject({ metadataRevision: controls.metadataRevision, titleRevision: 0 }) + expect(f.metadata.listMembers('task')[0]).toMatchObject({ metadataRevision: controls.metadataRevision }) + const request = f.command('actor-and-live-model', { conversationId: 'task', actorId: 'default', expectedRevision: controls.metadataRevision, expectedActiveRun: { runId: controls.current!.runId, generation: controls.current!.generation, modelSelection: controls.current!.modelSelection }, modelSelection: selection }) + f.runs.setModelSelection('run', { ...selection, modelId: 'changed-elsewhere' }) + expect((await f.commands.execute('conversations.setModelSelection', request)).receipt).toMatchObject({ decision: 'rejected', errorCode: 'SOURCE_CHANGED' }) + expect(f.conversations.findById('task')?.modelSelection).toBeNull() + const fresh = f.metadata.getModelControls('task', 'default') + const accepted = await f.commands.execute('conversations.setModelSelection', f.command('fresh-actor-and-live-model', { conversationId: 'task', actorId: 'default', expectedRevision: fresh.metadataRevision, modelSelection: selection, expectedActiveRun: { runId: fresh.current!.runId, generation: fresh.current!.generation, modelSelection: fresh.current!.modelSelection } })) + expect(accepted.receipt.decision).toBe('accepted') + expect(f.conversations.findById('task')?.modelSelection).toEqual(selection) + expect(f.runs.findById('run')).toMatchObject({ model: 'accepted-model', modelOverride: selection }) + }) + + it('keeps read and mark assignment revisions independent and refuses stale clear or read', async () => { + const f = fixture() + const first = await f.commands.execute('taskMarks.create', f.command('new-mark', { name: 'First', description: '', color: '#3979d6' })) + const markId = String(first.receipt.target.markId) + await f.commands.execute('taskMarks.assign', f.command('assign', { conversationId: 'task', markId, expectedAssignmentRevision: 0 })) + expect(f.repository.assignmentRevision('task')).toBe(1) + const staleClear = await f.commands.execute('taskMarks.clear', f.command('stale-clear', { conversationId: 'task', expectedAssignmentRevision: 0, readRevision: 0, resultRunId: null })) + expect(staleClear.receipt).toMatchObject({ decision: 'rejected', errorCode: 'SOURCE_CHANGED' }) + await f.commands.execute('taskMarks.setRead', f.command('unread', { conversationId: 'task', readRevision: 0, resultRunId: null, read: false })) + expect(f.repository.assignmentRevision('task')).toBe(1) + const staleRead = await f.commands.execute('taskMarks.setRead', f.command('stale-read', { conversationId: 'task', readRevision: 0, resultRunId: null, read: true })) + expect(staleRead.receipt).toMatchObject({ decision: 'rejected', errorCode: 'SOURCE_CHANGED' }) + expect(f.markRepository.getState('task')).toMatchObject({ markId, readRevision: 1, unread: true }) + await f.commands.execute('taskMarks.clear', f.command('clear', { conversationId: 'task', expectedAssignmentRevision: 1, readRevision: 1, resultRunId: null })) + expect(f.markRepository.getState('task')).toMatchObject({ markId: null, readRevision: 2, unread: false }) + expect(f.repository.assignmentRevision('task')).toBe(2) + }) + + it('fixes the tombstone receipt before cleanup and replays it without resurrecting the task', async () => { + const f = fixture() + const request = f.command('delete', { conversationId: 'task', expectedRevision: f.repository.conversationRevision('task') }) + f.cancel.mockImplementationOnce(async () => { + expect(f.database.isTransaction).toBe(false) + expect(f.conversations.findById('task')?.deletedAt).toBeTypeOf('string') + expect(f.admission.lookup({ dataGeneration: request.dataGeneration, commandId: request.commandId }).lookup).toBe('decided') + throw new Error('Cleanup pending') + }) + const accepted = await f.commands.execute('conversations.delete', request) + await f.commands.dispose() + expect(accepted.receipt).toMatchObject({ decision: 'accepted', result: { cleanup: 'pending' } }) + expect(f.effects).toHaveLength(1) + expect(await f.commands.execute('conversations.delete', request)).toEqual({ created: false, receipt: accepted.receipt }) + expect(f.database.prepare('SELECT count(*) AS count FROM conversations WHERE id = \'task\'').get()).toEqual({ count: 1 }) + expect(f.cancel).toHaveBeenCalledTimes(1) + }) + + it('holds the task mutation guard until committed space selection finishes releasing its session', async () => { + const f = fixture() + f.spaces.create({ id: 'space', name: 'Workspace', createdAt: now, memoryScope: 'space_only', additionalDirectories: [], primaryDirectory: { id: 'directory', root: '/fixture/workspace', canonicalRoot: '/fixture/workspace', accessGrantedAt: now, resourcesTrustedAt: now } }) + const release = Promise.withResolvers<{ pending: number, degraded: number }>() + f.invalidate.mockReturnValueOnce(release.promise) + const request = f.command('move', { conversationId: 'task', spaceId: 'space', expectedRevision: f.repository.conversationRevision('task') }) + const moving = f.commands.execute('conversations.moveToSpace', request) + await vi.waitFor(() => expect(f.invalidate).toHaveBeenCalled()) + expect(f.conversations.findById('task')?.spaceId).toBe('space') + expect(f.admission.lookup({ dataGeneration: request.dataGeneration, commandId: request.commandId }).lookup).toBe('decided') + expect(() => f.mutations.assertWritable('task')).toThrow() + release.resolve({ pending: 0, degraded: 0 }) + expect((await moving).receipt.decision).toBe('accepted') + expect(() => f.mutations.assertWritable('task')).not.toThrow() + }) + + it('rolls back metadata and suppresses notifications when receipt insertion fails', async () => { + const f = fixture() + f.database.exec('CREATE TRIGGER reject_metadata_receipt BEFORE INSERT ON command_receipts BEGIN SELECT RAISE(ABORT, \'receipt unavailable\'); END') + const changes: unknown[] = [] + f.metadata.onDidCommit(event => changes.push(event)) + await expect(f.commands.execute('conversations.rename', f.command('failed-title', { conversationId: 'task', title: 'Cannot commit', expectedTitleRevision: 0 }))).rejects.toThrow('receipt unavailable') + expect(f.conversations.findById('task')?.title).toBe('Initial') + expect(f.repository.conversationRevision('task', true)).toBe(0) + expect(changes).toEqual([]) + }) +}) + +function seedRun(f: ReturnType) { + f.database.prepare('INSERT INTO messages (id, conversation_id, branch_id, role, content_json, created_at) VALUES (\'message\', \'task\', \'branch\', \'user\', \'{}\', ?)').run(now) + f.runs.create({ id: 'run', conversationId: 'task', branchId: 'branch', triggeringMessageId: 'message', provider: 'fixture', model: 'accepted-model', purpose: 'chat', status: 'running', piSessionFile: null, approvalPolicy: 'policy', executionProfile: 'workspace_write', startedAt: now }) +} diff --git a/apps/buddy/service/src/conversations/__tests__/ConversationMetadataService.spec.ts b/apps/buddy/service/src/conversations/__tests__/ConversationMetadataService.spec.ts index 1d795471c..732c20743 100644 --- a/apps/buddy/service/src/conversations/__tests__/ConversationMetadataService.spec.ts +++ b/apps/buddy/service/src/conversations/__tests__/ConversationMetadataService.spec.ts @@ -19,72 +19,16 @@ afterEach(() => { }) describe('conversation metadata ownership', () => { - it('keeps archived members and model snapshots readable without permitting metadata changes', async () => { - const f = fixture() - const actor = f.buddies.create({ name: 'Historical actor' }) - f.service.addMember({ id: 'task-1', buddyId: actor.id }) - f.service.setBuddy({ id: 'task-1', buddyId: actor.id }) - f.buddies.update({ buddyId: actor.id, expectedRevision: actor.revision, enabled: false }) - f.database.prepare('UPDATE conversations SET archived_at = ? WHERE id = ?').run('2026-11-15T00:00:00.000Z', 'task-1') - const before = f.repository.findById('task-1') - expect(f.service.listMembers('task-1').map(member => member.actorId)).toContain(actor.id) - expect(f.service.getModelControls('task-1').next).toMatchObject({ actorId: actor.id, isMember: true }) - expect(() => f.service.rename({ id: 'task-1', title: 'Changed' })).toThrow() - expect(() => f.service.setBuddy({ id: 'task-1', buddyId: 'default' })).toThrow() - expect(() => f.service.addMember({ id: 'task-1', buddyId: actor.id })).toThrow() - expect(() => f.service.removeMember({ id: 'task-1', buddyId: actor.id })).toThrow() - expect(() => f.service.activateBranch({ conversationId: 'task-1', branchId: 'branch-1' })).toThrow() - expect(f.service.renameGenerated({ id: 'task-1', title: 'Late title', expectedRevision: f.repository.getTitleState('task-1')!.revision })).toBeNull() - await expect(f.service.setModelSelection({ id: 'task-1', modelSelection: null })).rejects.toMatchObject({ code: 'VALIDATION_FAILED' }) - await expect(f.service.setPermissionSettings({ id: 'task-1', approvalPolicy: 'manual', executionProfile: 'workspace_write' })).rejects.toMatchObject({ code: 'VALIDATION_FAILED' }) - await expect(f.service.moveToSpace({ id: 'task-1', spaceId: 'space-1' })).rejects.toMatchObject({ code: 'VALIDATION_FAILED' }) - expect(f.repository.findById('task-1')).toEqual(before) - }) - - it('blocks metadata writes while archive preparation holds the gate and rechecks asynchronous model preparation', async () => { - const f = fixture() - const selection = { providerId: 'fixture', modelId: 'model', reasoning: null, serviceTier: null } - const resolved = Promise.withResolvers() - f.resolveModel.mockImplementationOnce(() => resolved.promise) - const pending = f.service.setModelSelection({ id: 'task-1', modelSelection: selection }) - await f.mutations.run('task-1', async () => { - expect(f.service.listMembers('task-1')).toHaveLength(1) - expect(() => f.service.rename({ id: 'task-1', title: 'Changed' })).toThrow(expect.objectContaining({ code: 'CONVERSATION_BUSY' })) - await expect(f.service.setPermissionSettings({ id: 'task-1', approvalPolicy: 'manual', executionProfile: 'workspace_write' })).rejects.toMatchObject({ code: 'CONVERSATION_BUSY' }) - await expect(f.service.moveToSpace({ id: 'task-1', spaceId: 'space-1' })).rejects.toMatchObject({ code: 'CONVERSATION_BUSY' }) - resolved.resolve(selection) - await expect(pending).rejects.toMatchObject({ code: 'CONVERSATION_BUSY' }) - }) - expect(f.repository.findById('task-1')).toMatchObject({ title: 'Original', spaceId: null, modelSelection: null }) - await expect(f.service.moveToSpace({ id: 'task-1', spaceId: 'space-1' })).resolves.toMatchObject({ spaceId: 'space-1' }) - }) - it('previews a directly mentioned actor model without joining them or changing the leader', () => { const f = fixture() const actor = f.buddies.create({ name: 'Reviewer' }) const selection = { providerId: 'fixture', modelId: 'review-model', reasoning: 'high' as const, serviceTier: null } f.buddies.update({ buddyId: actor.id, expectedRevision: actor.revision, settings: buddySettingsSchema.parse({ ...actor.settings, model: selection }) }) - expect(f.service.getModelControls('task-1', actor.id)).toEqual({ current: null, next: { actorId: actor.id, isMember: false, modelSelection: selection } }) + expect(f.service.getModelControls('task-1', actor.id)).toEqual({ metadataRevision: f.repository.findById('task-1')!.metadataRevision, current: null, next: { actorId: actor.id, isMember: false, modelSelection: selection } }) expect(f.members.find('task-1', actor.id)).toBeNull() expect(f.repository.findById('task-1')?.buddyId).toBe('default') }) - it('publishes the actual title CAS commits and protects event snapshots', () => { - const f = fixture() - const changes: ConversationMetadataCommit[] = [] - f.service.onDidCommit(event => changes.push(event)) - expect(f.service.renameGenerated({ id: 'task-1', title: 'Generated', expectedRevision: 0 })).not.toBeNull() - f.service.rename({ id: 'task-1', title: 'Generated' }) - expect(f.service.renameGenerated({ id: 'task-1', title: 'Late generated', expectedRevision: 1 })).toBeNull() - expect(changes.map(event => [event.kind, event.titleSource, event.titleRevision])).toEqual([ - ['title', 'generated', 1], - ['title', 'manual', 2], - ]) - expect(Reflect.set(changes[0]!.conversation, 'title', 'External mutation')).toBe(false) - expect(f.repository.getTitleState('task-1')).toMatchObject({ title: 'Generated', source: 'manual', revision: 2 }) - expect(f.repository.findById('task-1')?.updatedAt).toBe('2026-09-28T00:00:00.000Z') - }) - it('keeps a committed permission change when its required invalidation fails and retries only application', async () => { const f = fixture() const changes: ConversationMetadataCommit[] = [] @@ -96,9 +40,6 @@ describe('conversation metadata ownership', () => { expect(changes.map(event => event.kind)).toEqual(['permissions']) await expect(f.service.setPermissionSettings(input)).resolves.toMatchObject({ approvalPolicy: 'manual' }) expect(changes).toHaveLength(1) - expect(f.invalidate).toHaveBeenCalledTimes(2) - await f.service.setPermissionSettings(input) - expect(f.invalidate).toHaveBeenCalledTimes(2) }) it('persists and publishes model controls without changing task activity or history order', async () => { @@ -127,24 +68,6 @@ describe('conversation metadata ownership', () => { expect(changes).toHaveLength(selections.length) }) - it('rechecks the captured task after model resolution without publishing a no-op', async () => { - const f = fixture() - const selection = { providerId: 'provider-1', modelId: 'model-1', reasoning: null, serviceTier: null } - const changes: ConversationMetadataCommit[] = [] - f.service.onDidCommit(event => changes.push(event)) - await f.service.setModelSelection({ id: 'task-1', modelSelection: selection }) - await f.service.setModelSelection({ id: 'task-1', modelSelection: selection }) - expect(changes).toHaveLength(1) - const gate = Promise.withResolvers() - f.resolveModel.mockImplementationOnce(() => gate.promise) - const operation = f.service.setModelSelection({ id: 'task-1', modelSelection: { ...selection, modelId: 'model-2' } }) - f.repository.markDeleted('task-1', new Date().toISOString()) - gate.resolve({ ...selection, modelId: 'model-2' }) - await expect(operation).rejects.toMatchObject({ code: 'VALIDATION_FAILED' }) - expect(f.repository.findById('task-1')?.modelSelection?.modelId).toBe('model-1') - expect(changes).toHaveLength(1) - }) - it('writes a pending model change to the captured actor after a protagonist switch', async () => { const f = fixture() const selection = { providerId: 'fixture', modelId: 'model-2', reasoning: null, serviceTier: null } @@ -184,11 +107,11 @@ describe('conversation metadata ownership', () => { expect(f.members.find('task-1', other.id)?.modelSelection).toBeNull() const nextSelection = { ...selection, modelId: 'next-model' } await f.service.setModelSelection({ id: 'task-1', modelSelection: nextSelection }) - expect(f.service.getModelControls('task-1')).toEqual({ current: null, next: { actorId: other.id, isMember: true, modelSelection: nextSelection } }) + expect(f.service.getModelControls('task-1')).toEqual({ metadataRevision: f.repository.findById('task-1')!.metadataRevision, current: null, next: { actorId: other.id, isMember: true, modelSelection: nextSelection } }) f.service.addMember({ id: 'task-1', buddyId: 'default' }) f.service.setBuddy({ id: 'task-1', buddyId: 'default' }) await f.service.setModelSelection({ id: 'task-1', modelSelection: nextSelection }) - expect(f.service.getModelControls('task-1')).toEqual({ current: { runId: 'run-1', actorId: 'default', modelSelection: nextSelection }, next: { actorId: 'default', isMember: true, modelSelection: nextSelection } }) + expect(f.service.getModelControls('task-1')).toEqual({ metadataRevision: f.repository.findById('task-1')!.metadataRevision, current: { runId: 'run-1', generation: f.runs.findById('run-1')!.generation, actorId: 'default', modelSelection: nextSelection }, next: { actorId: 'default', isMember: true, modelSelection: nextSelection } }) await expect(f.service.setModelSelection({ id: 'task-1', actorId: other.id, runId: 'run-1', modelSelection: selection })).rejects.toMatchObject({ code: 'VALIDATION_FAILED' }) const gate = Promise.withResolvers() f.resolveModel.mockImplementationOnce(() => gate.promise) @@ -221,38 +144,27 @@ describe('conversation metadata ownership', () => { expect(f.runs.findById('run-1')).toMatchObject({ status: 'completed', modelOverride: selection }) }) - it('moves a space-less task into an active space once and releases its sessions', async () => { - const f = fixture() - const changes: ConversationMetadataCommit[] = [] - f.service.onDidCommit(event => changes.push(event)) - await expect(f.service.moveToSpace({ id: 'task-1', spaceId: 'space-1' })).resolves.toMatchObject({ id: 'task-1', spaceId: 'space-1' }) - expect(f.repository.findById('task-1')?.spaceId).toBe('space-1') - expect(changes.map(event => event.kind)).toEqual(['space']) - expect(f.invalidate).toHaveBeenCalledWith('task-1') - await expect(f.service.moveToSpace({ id: 'task-1', spaceId: 'space-2' })).rejects.toMatchObject({ code: 'CONVERSATION_SPACE_CONFLICT' }) - expect(f.repository.findById('task-1')?.spaceId).toBe('space-1') - await expect(f.service.moveToSpace({ id: 'task-1', spaceId: 'space-1' })).resolves.toMatchObject({ spaceId: 'space-1' }) - expect(changes).toHaveLength(1) - }) - it('refuses to move a task into a revoked space or while it has an active run', async () => { const f = fixture() await expect(f.service.moveToSpace({ id: 'task-1', spaceId: 'revoked' })).rejects.toMatchObject({ code: 'SPACE_UNAVAILABLE' }) f.runs.create({ id: 'run-1', conversationId: 'task-1', branchId: 'branch-1', triggeringMessageId: 'message-1', provider: 'fixture', model: 'model-1', purpose: 'chat', status: 'running', piSessionFile: null, approvalPolicy: 'policy', executionProfile: 'workspace_write', startedAt: '2026-10-11T00:00:00.000Z' }) await expect(f.service.moveToSpace({ id: 'task-1', spaceId: 'space-1' })).rejects.toMatchObject({ code: 'CONVERSATION_BUSY' }) expect(f.repository.findById('task-1')?.spaceId).toBeNull() - expect(f.invalidate).not.toHaveBeenCalled() + f.runs.reconcileTerminal('run-1', 'completed', '2026-10-11T00:01:00.000Z', null) + f.hasActiveExecution.mockReturnValueOnce(true) + await expect(f.service.moveToSpace({ id: 'task-1', spaceId: 'space-1' })).rejects.toMatchObject({ code: 'CONVERSATION_BUSY' }) + expect(f.repository.findById('task-1')?.spaceId).toBeNull() }) - it.each(['waiting', 'paused'])('keeps %s queue inputs unchanged and refuses to move across any branch', async (state) => { + it('keeps a paused queue input unchanged and refuses to move the task', async () => { const f = fixture() - f.database.prepare(`INSERT INTO chat_queue (id, conversation_id, branch_id, request_id, request_fingerprint, prepared_json, state, created_at) VALUES ('queued', 'task-1', 'branch-1', 'request', 'fingerprint', '{"spaceId":null}', ?, 'now')`).run(state) + f.database.prepare(`INSERT INTO chat_queue (id, conversation_id, branch_id, request_id, request_fingerprint, prepared_json, state, created_at) VALUES ('queued', 'task-1', 'branch-1', 'request', 'fingerprint', '{"spaceId":null}', ?, 'now')`).run('paused') await expect(f.service.moveToSpace({ id: 'task-1', spaceId: 'space-1' })).rejects.toMatchObject({ code: 'CONVERSATION_QUEUE_NOT_EMPTY' }) - expect(f.database.prepare('SELECT state, prepared_json FROM chat_queue').get()).toEqual({ state, prepared_json: '{"spaceId":null}' }) + expect(f.database.prepare('SELECT state, prepared_json FROM chat_queue').get()).toEqual({ state: 'paused', prepared_json: '{"spaceId":null}' }) expect(f.repository.findById('task-1')?.spaceId).toBeNull() }) - it('does not commit or publish a move when session cleanup fails and permits a safe retry', async () => { + it('preserves a task when session cleanup fails and retries its one-time space move safely', async () => { const f = fixture() const changes: ConversationMetadataCommit[] = [] f.service.onDidCommit(event => changes.push(event)) @@ -261,6 +173,11 @@ describe('conversation metadata ownership', () => { expect(f.repository.findById('task-1')?.spaceId).toBeNull() expect(changes).toEqual([]) await expect(f.service.moveToSpace({ id: 'task-1', spaceId: 'space-1' })).resolves.toMatchObject({ spaceId: 'space-1' }) + expect(f.repository.findById('task-1')?.spaceId).toBe('space-1') + expect(changes.map(event => event.kind)).toEqual(['space']) + await expect(f.service.moveToSpace({ id: 'task-1', spaceId: 'space-2' })).rejects.toMatchObject({ code: 'CONVERSATION_SPACE_CONFLICT' }) + expect(f.repository.findById('task-1')?.spaceId).toBe('space-1') + await expect(f.service.moveToSpace({ id: 'task-1', spaceId: 'space-1' })).resolves.toMatchObject({ spaceId: 'space-1' }) expect(changes).toHaveLength(1) }) @@ -278,13 +195,6 @@ describe('conversation metadata ownership', () => { expect(() => f.mutations.assertWritable('task-1')).not.toThrow() }) - it('refuses to switch while a terminal run still owns execution resources', async () => { - const f = fixture() - f.hasActiveExecution.mockReturnValueOnce(true) - await expect(f.service.moveToSpace({ id: 'task-1', spaceId: 'space-1' })).rejects.toMatchObject({ code: 'CONVERSATION_BUSY' }) - expect(f.repository.findById('task-1')?.spaceId).toBeNull() - }) - it('revalidates a target disabled during model validation and records explicit provenance for an unchanged legacy value', async () => { const f = fixture() const other = f.buddies.create({ name: 'Other' }) @@ -320,6 +230,6 @@ function fixture() { seedSpace.run('revoked', 'Revoked', '2026-09-07T00:00:00.000Z') const mutations = new ConversationMutationGuard() const hasActiveExecution = vi.fn(() => false) - const service = new ConversationMetadataService({ buddies, runs, repository, assertWritable: id => mutations.assertWritable(id), spaces: createSpaceRepository(database), spaceMoves: { mutations, hasPendingMessages: id => createChatQueueRepository(database).hasPending(id), hasActiveExecution, validateTarget: async () => {} }, resolveModelSelection: resolveModel, sessions: { invalidateConversation: invalidate } }) + const service = new ConversationMetadataService({ buddies, runs, repository, spaces: createSpaceRepository(database), spaceMoves: { mutations, hasPendingMessages: id => createChatQueueRepository(database).hasPending(id), hasActiveExecution, validateTarget: async () => {} }, resolveModelSelection: resolveModel, sessions: { invalidateConversation: invalidate } }) return { database, runs, service, repository, invalidate, mutations, hasActiveExecution, resolveModel, buddies, members: createConversationMemberRepository(database) } } diff --git a/apps/buddy/service/src/conversations/__tests__/extensionReferenceCapabilities.spec.ts b/apps/buddy/service/src/conversations/__tests__/extensionReferenceCapabilities.spec.ts index e0dac8194..8d43c158b 100644 --- a/apps/buddy/service/src/conversations/__tests__/extensionReferenceCapabilities.spec.ts +++ b/apps/buddy/service/src/conversations/__tests__/extensionReferenceCapabilities.spec.ts @@ -12,7 +12,6 @@ import { createRunInputRepository } from '../../storage/runInputRepository' import { createRunRepository } from '../../storage/runRepository' import { createExtensionReferenceCapabilities } from '../extensionReferenceCapabilities' import { ExtensionTaskContext } from '../ExtensionTaskContext' -import { ReferenceSnapshotAccess } from '../ReferenceSnapshotAccess' const databases: DatabaseSync[] = [] afterEach(() => databases.splice(0).forEach(database => database.close())) @@ -63,17 +62,12 @@ function fixture() { } describe('run-owned extension references', () => { - it.each(['text', 'locator', 'target'])('binds repeated reference ids to a message and rejects ambiguous %s lookups', async (change) => { + it('binds repeated reference ids to a message and rejects ambiguous locators', async () => { const f = fixture() const quote = f.quote() const first = f.message([quote]) const changed = structuredClone(quote) - if (change === 'text') - changed.text = 'A different selected region' - if (change === 'locator') - changed.locator.data.revision = 8 - if (change === 'target') - changed.source.target.viewId = randomUUID() + changed.locator.data.revision = 8 const second = f.message([changed]) const call = f.invocation(second.runId) await expect(call.handlers['references.resolve']({ referenceId: quote.id }, call.context)).rejects.toThrow('EXTENSION_REFERENCE_AMBIGUOUS') @@ -129,33 +123,29 @@ describe('run-owned extension references', () => { await expect(call.handlers['references.resolve']({ referenceId: quote.id }, call.context)).rejects.toThrow('EXTENSION_REFERENCE_UNAVAILABLE') }) - it.each(['actor_child', 'npc_child'] as const)('keeps %s plugin and generic reference scope frozen when its parent accepts later input', async (kind) => { + it.each(['actor_child', 'npc_child'] as const)('keeps %s plugin reference scope frozen when its parent accepts later input', async (kind) => { const f = fixture() const original = f.quote() const root = f.message([original]) const privateQuote = f.quote() const child = f.child(root.runId, kind, privateQuote) const future = f.quote() - const accepted = f.acceptFollowup(root.runId, future) + f.acceptFollowup(root.runId, future) const call = f.invocation(child.runId) expect(await call.handlers['references.list'](null, call.context)).toMatchObject({ items: [{ referenceId: original.id }] }) for (const quote of [privateQuote, future]) await expect(call.handlers['references.resolve']({ referenceId: quote.id }, call.context)).rejects.toThrow('EXTENSION_REFERENCE_UNAVAILABLE') - const generic = new ReferenceSnapshotAccess({ conversationId: 'task', getRunId: () => child.runId, references: f.references, signal: f.controller.signal }) - expect(() => generic.read({ referenceId: future.id, sourceMessageId: accepted.id, kind: 'resource' })).toThrow('REFERENCE_NOT_AUTHORIZED') const parent = f.invocation(root.runId) expect(await parent.handlers['references.resolve']({ referenceId: future.id }, parent.context)).toMatchObject({ referenceId: future.id }) }) - it.each(['new input', 'branch', 'delete', 'abort', 'run complete'])('revokes list and resolve after %s', async (change) => { + it.each(['new input', 'delete', 'abort', 'run complete'])('revokes list and resolve after %s', async (change) => { const f = fixture() const quote = f.quote() const input = f.message([quote]) const call = f.invocation(input.runId) if (change === 'new input') f.message([]) - if (change === 'branch') - f.tasks.createBranch({ id: 'other', conversationId: 'task', parentBranchId: 'branch', forkedFromMessageId: input.id, createdAt: '2026-10-24T00:01:00.000Z', activate: true }) if (change === 'delete') f.tasks.markDeleted('task', '2026-10-24T00:01:00.000Z') if (change === 'abort') @@ -166,6 +156,19 @@ describe('run-owned extension references', () => { await expect(call.handlers['references.resolve']({ referenceId: quote.id }, call.context)).rejects.toThrow() }) + it('keeps run-owned references on their original branch after another branch becomes the default', async () => { + const f = fixture() + const quote = f.quote() + const input = f.message([quote]) + const call = f.invocation(input.runId) + f.tasks.createBranch({ id: 'other', conversationId: 'task', parentBranchId: null, forkedFromMessageId: null, createdAt: '2026-10-24T00:01:00.000Z', activate: true }) + const foreign = f.quote() + const other = f.message([foreign], { branch: 'other' }) + expect(await call.handlers['references.list'](null, call.context)).toMatchObject({ items: [{ referenceId: quote.id, sourceMessageId: input.id }] }) + expect(await call.handlers['references.resolve']({ referenceId: quote.id }, call.context)).toMatchObject({ referenceId: quote.id, scope: { taskId: 'task', branchId: 'branch' } }) + await expect(call.handlers['references.resolve']({ referenceId: foreign.id, sourceMessageId: other.id }, call.context)).rejects.toThrow('EXTENSION_REFERENCE_UNAVAILABLE') + }) + it('rejects actions and guessed message-local quote numbers', async () => { const f = fixture() const quote = f.quote() @@ -176,8 +179,10 @@ describe('run-owned extension references', () => { await expect(call.handlers['references.resolve']({ referenceId: '1' }, call.context)).rejects.toThrow() }) - it('keeps worst-case Unicode and escaped text listings inside the normal RPC budget', async () => { + it('bounds Unicode listings while preserving access to older references', async () => { const f = fixture() + const oldest = f.quote() + f.message([oldest]) let input for (let index = 0; index < 100; index++) { const quote = f.quote() @@ -187,20 +192,6 @@ describe('run-owned extension references', () => { const result = await call.handlers['references.list'](null, call.context) expect(extensionJsonSchema.safeParse(result).success).toBe(true) expect(result).toMatchObject({ truncated: true }) - }) - - it('bounds listings and resolves older references beyond the list', async () => { - const f = fixture() - const oldest = f.quote() - f.message([oldest]) - let input - for (let index = 0; index < 105; index++) - input = f.message([{ ...f.quote(), text: 'x'.repeat(1000) }]) - const call = f.invocation(input!.runId) - const result = await call.handlers['references.list'](null, call.context) as { items: { excerpt: string }[], truncated: boolean } - expect(result.items).toHaveLength(100) - expect(result.items.every(item => item.excerpt.length === 512)).toBe(true) - expect(result.truncated).toBe(true) expect(await call.handlers['references.resolve']({ referenceId: oldest.id }, call.context)).toMatchObject({ referenceId: oldest.id }) }) diff --git a/apps/buddy/service/src/conversations/__tests__/extensionTaskCapabilities.spec.ts b/apps/buddy/service/src/conversations/__tests__/extensionTaskCapabilities.spec.ts index 72ba1dc9f..504feb299 100644 --- a/apps/buddy/service/src/conversations/__tests__/extensionTaskCapabilities.spec.ts +++ b/apps/buddy/service/src/conversations/__tests__/extensionTaskCapabilities.spec.ts @@ -68,22 +68,27 @@ describe('invocation-owned task capabilities', () => { expect(f.tasks.findById('task')?.title).toBe('User wins') }) - it.each(['new input', 'branch', 'delete', 'abort'])('rejects writes and message reads after %s', async (change) => { + it('rejects writes and message reads after a newer input', async () => { const f = fixture() const invocation = f.invocation(true) - if (change === 'new input') - f.tasks.createMessage({ id: 'next', conversationId: 'task', branchId: 'branch', runId: null, role: 'user', content: 'New goal', createdAt: '2026-09-28T00:00:01.000Z' }) - if (change === 'branch') - f.tasks.createBranch({ id: 'other', conversationId: 'task', parentBranchId: 'branch', forkedFromMessageId: 'input', createdAt: '2026-09-28T00:00:01.000Z', activate: true }) - if (change === 'delete') - f.tasks.markDeleted('task', '2026-09-28T00:00:01.000Z') - if (change === 'abort') - f.controller.abort() + f.tasks.createMessage({ id: 'next', conversationId: 'task', branchId: 'branch', runId: null, role: 'user', content: 'New goal', createdAt: '2026-09-28T00:00:01.000Z' }) await expect(invocation.handlers['task.rename']({ title: 'Late' }, invocation.context)).rejects.toThrow() await expect(invocation.handlers['task.messages'](null, invocation.context)).rejects.toThrow() expect(f.tasks.findById('task')?.title).toBe('Original') }) + it('retains captured branch reads and title revision after a default branch change', async () => { + const f = fixture() + const invocation = f.invocation(true) + f.tasks.createBranch({ id: 'other', conversationId: 'task', parentBranchId: null, forkedFromMessageId: null, createdAt: '2026-09-28T00:00:01.000Z', activate: true }) + f.tasks.createMessage({ id: 'other-input', conversationId: 'task', branchId: 'other', runId: null, role: 'user', content: 'OTHER_BRANCH_INPUT', createdAt: '2026-09-28T00:00:02.000Z' }) + const messages = await invocation.handlers['task.messages'](null, invocation.context) + expect(JSON.stringify(messages)).toContain('Initial goal') + expect(JSON.stringify(messages)).not.toContain('OTHER_BRANCH_INPUT') + expect(await invocation.handlers['task.rename']({ title: 'Captured title' }, invocation.context)).toEqual({ applied: true }) + expect(f.tasks.findById('task')?.activeBranchId).toBe('other') + }) + it('returns bounded visible text while excluding tool output and inactive branches', () => { const f = fixture() f.tasks.createBranch({ id: 'hidden', conversationId: 'task', parentBranchId: 'branch', forkedFromMessageId: 'input', createdAt: '2026-09-28T00:00:01.000Z', activate: false }) diff --git a/apps/buddy/service/src/conversations/__tests__/referenceSnapshots.spec.ts b/apps/buddy/service/src/conversations/__tests__/referenceSnapshots.spec.ts index f7ace4757..8caf6c523 100644 --- a/apps/buddy/service/src/conversations/__tests__/referenceSnapshots.spec.ts +++ b/apps/buddy/service/src/conversations/__tests__/referenceSnapshots.spec.ts @@ -173,7 +173,7 @@ describe('saved reference access', () => { expect(f.access().list({}).items.map(item => item.referenceId)).toEqual(['initial']) }) - it.each(['delete', 'branch', 'cancel', 'finish', 'message deleted'])('revalidates %s after policy classification and before tool delivery', async (change) => { + it.each(['cancel', 'finish', 'message deleted'])('revalidates %s after policy classification and before tool delivery', async (change) => { const f = await fixture() const input = f.message([f.view()]) const runId = f.run(input) @@ -182,10 +182,6 @@ describe('saved reference access', () => { await capability.extension.factory({ registerTool: (tool: ToolDefinition) => tools.set(tool.name, tool) } as unknown as ExtensionAPI) const args = { sourceMessageId: input.id, referenceId: 'reference', kind: 'resource' } expect(await capability.classify({ type: 'tool_call', toolName: REFERENCE_TOOLS.read.name, toolCallId: 'call', input: args } as ToolCallEvent, f.controller.signal)).toEqual({ access: 'read', paths: [] }) - if (change === 'delete') - f.conversations.markDeleted('task', '2026-10-24T00:10:00.000Z') - if (change === 'branch') - f.conversations.createBranch({ id: 'changed-branch', conversationId: 'task', parentBranchId: null, forkedFromMessageId: null, createdAt: '2026-10-24T00:10:00.000Z', activate: true }) if (change === 'cancel') f.controller.abort() if (change === 'finish') @@ -195,15 +191,19 @@ describe('saved reference access', () => { await expect(tools.get(REFERENCE_TOOLS.read.name)!.execute('call', args, f.controller.signal, undefined, {} as ExtensionToolContext)).rejects.toThrow() }) - it('rejects invalid identities and unbounded pagination at the tool boundary', async () => { + it('delivers the classified original-branch reference after a default branch change', async () => { const f = await fixture() const input = f.message([f.view()]) f.run(input) const capability = createReferenceSnapshotCapability(f.options()) - const tools: ToolDefinition[] = [] - await capability.extension.factory({ registerTool: (tool: ToolDefinition) => tools.push(tool) } as unknown as ExtensionAPI) - for (const args of [{ sourceMessageId: input.id, referenceId: 'reference' }, { sourceMessageId: input.id, referenceId: 'reference', kind: 'resource', limit: 16001 }, { sourceMessageId: input.id, referenceId: 'reference', kind: 'resource', offset: -1 }]) - await expect(tools.find(tool => tool.name === REFERENCE_TOOLS.read.name)!.execute('call', args, f.controller.signal, undefined, {} as ExtensionToolContext)).rejects.toThrow('VALIDATION_FAILED') - await expect(tools.find(tool => tool.name === REFERENCE_TOOLS.read.name)!.execute('call', { sourceMessageId: input.id, referenceId: '1', kind: 'resource' }, f.controller.signal, undefined, {} as ExtensionToolContext)).rejects.toThrow('REFERENCE_NOT_AUTHORIZED') + const tools = new Map() + await capability.extension.factory({ registerTool: (tool: ToolDefinition) => tools.set(tool.name, tool) } as unknown as ExtensionAPI) + const args = { sourceMessageId: input.id, referenceId: 'reference', kind: 'resource' } + await capability.classify({ type: 'tool_call', toolName: REFERENCE_TOOLS.read.name, toolCallId: 'call', input: args } as ToolCallEvent, f.controller.signal) + f.conversations.createBranch({ id: 'changed-branch', conversationId: 'task', parentBranchId: null, forkedFromMessageId: null, createdAt: '2026-10-24T00:10:00.000Z', activate: true }) + const other = f.message([f.view('foreign')], { branchId: 'changed-branch' }) + const result = await tools.get(REFERENCE_TOOLS.read.name)!.execute('call', args, f.controller.signal, undefined, {} as ExtensionToolContext) + expect(JSON.stringify(result)).toContain('Frozen original') + await expect(tools.get(REFERENCE_TOOLS.read.name)!.execute('foreign-call', { ...args, sourceMessageId: other.id, referenceId: 'foreign' }, f.controller.signal, undefined, {} as ExtensionToolContext)).rejects.toThrow('REFERENCE_NOT_AUTHORIZED') }) }) diff --git a/apps/buddy/service/src/conversations/__tests__/registerConversationArchiveRpc.spec.ts b/apps/buddy/service/src/conversations/__tests__/registerConversationArchiveRpc.spec.ts index ea5394dc6..2c647d54d 100644 --- a/apps/buddy/service/src/conversations/__tests__/registerConversationArchiveRpc.spec.ts +++ b/apps/buddy/service/src/conversations/__tests__/registerConversationArchiveRpc.spec.ts @@ -13,12 +13,10 @@ import { openBuddyDatabase } from '../../storage/database' import { ConversationArchiveService } from '../ConversationArchiveService' import { ConversationCleanupService } from '../ConversationCleanupService' import { ConversationLifecycleService } from '../ConversationLifecycleService' -import { ConversationMetadataService } from '../ConversationMetadataService' import { ConversationMutationGuard } from '../ConversationMutationGuard' import { ConversationPurgeService } from '../ConversationPurgeService' import { registerConversationArchiveRpc } from '../registerConversationArchiveRpc' import { registerConversationCleanupRpc } from '../registerConversationCleanupRpc' -import { registerConversationRpc } from '../registerConversationRpc' const cleanups: Array<() => Promise> = [] afterEach(async () => { @@ -26,7 +24,7 @@ afterEach(async () => { }) const now = '2026-11-15T00:00:00.000Z' -function fixture(options: { stop?: () => Promise } = {}) { +function fixture(options: { legacyWrites?: boolean, stop?: () => Promise } = {}) { const home = mkdtempSync(join(tmpdir(), 'lexora-legacy-archive-rpc-')) const path = join(home, 'buddy.sqlite3') const database = openBuddyDatabase({ databasePath: path }) @@ -34,7 +32,7 @@ function fixture(options: { stop?: () => Promise } = {}) { const repository = createConversationArchiveRepository(database) const mutations = new ConversationMutationGuard() const deletion = createConversationDeletionRepository(database) - const purge = new ConversationPurgeService({ database, home }) + const purge = new ConversationPurgeService({ database, home, storageAuthority: 'legacy_jsonl' }) const errors: unknown[] = [] const lifecycle = new ConversationLifecycleService({ conversations, @@ -46,7 +44,7 @@ function fixture(options: { stop?: () => Promise } = {}) { purge: id => purge.purge(id), }) const archive = new ConversationArchiveService({ database, repository, conversations, mutations, lifecycle, onError: error => errors.push(error), now: () => new Date(now) }) - const cleanup = new ConversationCleanupService({ database, archive, lifecycle }) + const cleanup: ConversationCleanupService = new ConversationCleanupService({ database, archive, lifecycle, execute: input => cleanup.execute(input) }) const handlers = new Map() const rpc = { onRequest: (method: string, handler: RuntimeRequestHandler) => { handlers.set(method, handler) @@ -54,27 +52,11 @@ function fixture(options: { stop?: () => Promise } = {}) { handlers.delete(method) } } } - const dispose = registerConversationArchiveRpc({ archive, rpc }) + const dispose = registerConversationArchiveRpc({ archive, legacyWrites: options.legacyWrites ?? true, rpc }) const disposeCleanup = registerConversationCleanupRpc({ rpc, cleanup }) - const metadata = new ConversationMetadataService({ repository: conversations, assertWritable: id => mutations.assertWritable(id), resolveModelSelection: async input => input, sessions: { invalidateConversation: async () => ({ pending: 0, degraded: 0 }) } }) - const disposeConversations = registerConversationRpc({ - rpc, - conversations, - metadata, - deleteConversation: id => lifecycle.delete(id), - isDeleting: id => lifecycle.isDeleting(id), - artifacts: { listForConversation: () => [] }, - attachments: { listForConversation: () => [] }, - changes: { listSummariesForRuns: () => [] }, - eventLog: { verifyRun: async () => {}, listForRuns: () => [] }, - runInputs: { findByRunId: () => null }, - runs: { listForTimeline: () => [] }, - }) cleanups.push(async () => { dispose() disposeCleanup() - disposeConversations() - await metadata.dispose() await archive.dispose() await lifecycle.dispose() database.close() @@ -89,9 +71,8 @@ function fixture(options: { stop?: () => Promise } = {}) { } const target = (conversationId: string) => ({ conversationId, expectedRevision: conversations.findById(conversationId)!.metadataRevision! }) const invoke = (method: keyof typeof conversationArchiveRpc, input: unknown) => Promise.resolve().then(() => handlers.get(conversationArchiveRpc[method].method)!(input, new AbortController().signal)) - const invokeRaw = (method: string, input: unknown) => Promise.resolve().then(() => handlers.get(method)!(input, new AbortController().signal)) const invokeCleanup = (method: keyof typeof conversationCleanupRpc, input: unknown) => Promise.resolve().then(() => handlers.get(conversationCleanupRpc[method].method)!(input, new AbortController().signal)) - return { database, path, conversations, repository, mutations, deletion, lifecycle, archive, cleanup, invokeCleanup, errors, seed, target, invoke, invokeRaw } + return { database, path, conversations, repository, mutations, deletion, lifecycle, archive, cleanup, invokeCleanup, errors, seed, target, invoke } } describe('legacy conversation archive RPC', () => { @@ -119,10 +100,9 @@ describe('legacy conversation archive RPC', () => { expect(existsSync(archived)).toBe(true) }) - it('archives, restores, saves policy and deletes through the actual v42 runtime lifecycle', async () => { + it('archives, restores, saves policy and deletes while preserving task data until deletion', async () => { const f = fixture() const directory = f.seed('task') - expect(f.database.prepare('PRAGMA user_version').get()!.user_version).toBe(42) const initial = f.target('task') await expect(f.invoke('archive', initial)).resolves.toBe(true) expect(existsSync(directory)).toBe(true) @@ -178,83 +158,17 @@ describe('legacy conversation archive RPC', () => { expect(() => f.mutations.assertWritable('task')).not.toThrow() }) - it('requires a fixed numeric revision for every raw task mutation', async () => { - const f = fixture() + it('rejects raw writes when legacy mode is not explicitly enabled', async () => { + const f = fixture({ legacyWrites: false }) f.seed('task') - for (const method of ['archive', 'restoreArchive', 'deleteArchived', 'deletePermanently'] as const) { - for (const expectedRevision of [undefined, -1, 1.5, '0', Number.MAX_SAFE_INTEGER + 1]) { - await expect(f.invoke(method, { conversationId: 'task', expectedRevision })).rejects.toMatchObject({ code: 'VALIDATION_FAILED' }) - } + for (const method of ['archive', 'restoreArchive', 'deleteArchived', 'setArchivePolicy'] as const) { + await expect(f.invoke(method, method === 'setArchivePolicy' ? { days: 3 } : f.target('task'))).rejects.toMatchObject({ code: 'METHOD_NOT_AVAILABLE' }) } - await expect(f.invoke('setArchivePolicy', { days: 1 })).rejects.toMatchObject({ code: 'VALIDATION_FAILED' }) - expect(f.archive.getPolicy()).toEqual({ days: 0 }) + await expect(f.invoke('listArchived', { kind: 'archived' })).resolves.toEqual({ items: [], nextCursor: null }) + await expect(f.invoke('getArchivePolicy', {})).resolves.toEqual({ days: 0 }) expect(f.conversations.findById('task')!.archivedAt).toBeNull() }) - it('permanently deletes active tasks only at the confirmed revision and excludes legacy tombstones', async () => { - const f = fixture() - const active = f.seed('active') - const removed = f.seed('removed') - const input = f.target('active') - await expect(f.invoke('deletePermanently', { ...input, expectedRevision: input.expectedRevision + 1 })).rejects.toMatchObject({ code: 'SOURCE_CHANGED' }) - expect(f.deletion.has('active')).toBe(false) - expect(existsSync(active)).toBe(true) - f.conversations.markDeleted('removed', now) - await expect(f.invoke('deletePermanently', f.target('removed'))).rejects.toMatchObject({ code: 'SOURCE_CHANGED' }) - expect(f.deletion.has('removed')).toBe(false) - await expect(f.invoke('deletePermanently', input)).resolves.toBe(true) - await f.lifecycle.settleDeletion('active') - expect(f.conversations.findById('active')).toBeNull() - expect(existsSync(active)).toBe(false) - expect(existsSync(removed)).toBe(true) - }) - - it('retries a failed active-task deletion from the archive manager without broadening legacy cleanup', async () => { - let fail = true - const f = fixture({ stop: async () => { - if (fail) - throw new Error('Stop failed') - return 0 - } }) - const directory = f.seed('active') - await expect(f.invoke('deletePermanently', f.target('active'))).resolves.toBe(true) - await f.lifecycle.settleDeletion('active') - expect(f.conversations.findById('active')!.archivedAt).toBeNull() - expect(f.database.prepare('SELECT status, origin FROM conversation_deletion_jobs').get()).toMatchObject({ status: 'failed', origin: 'conversation' }) - expect(existsSync(directory)).toBe(true) - expect(f.cleanup.list({ scope: 'legacy_removed' }).items).toEqual([]) - fail = false - await expect(f.invoke('deleteArchived', f.target('active'))).resolves.toBe(true) - await f.lifecycle.settleDeletion('active') - expect(f.conversations.findById('active')).toBeNull() - expect(existsSync(directory)).toBe(false) - }) - - it('preserves old raw delete as soft removal and permits explicit plugin cleanup afterwards', async () => { - const started = Promise.withResolvers() - const stop = Promise.withResolvers() - const f = fixture({ stop: () => { - started.resolve() - return stop.promise - } }) - const directory = f.seed('task') - const pending = f.invokeRaw('conversations.delete', { conversationId: 'task' }) - await started.promise - try { - await expect(f.cleanup.execute(f.target('task'))).rejects.toMatchObject({ code: 'CONVERSATION_BUSY' }) - expect(f.cleanup.list({ scope: 'legacy_removed' }).items).toMatchObject([{ conversationId: 'task', canDelete: false, blockedReason: 'CONVERSATION_BUSY' }]) - expect(f.deletion.has('task')).toBe(false) - } - finally { stop.resolve(0) } - await expect(pending).resolves.toBe(true) - expect(f.conversations.findById('task')!.deletedAt).not.toBeNull() - expect(f.deletion.has('task')).toBe(false) - expect(existsSync(directory)).toBe(true) - await expect(f.invoke('deletePermanently', f.target('task'))).rejects.toMatchObject({ code: 'SOURCE_CHANGED' }) - await expect(f.cleanup.delete({ ...f.target('task'), scope: 'legacy_removed' })).resolves.toEqual({ status: 'deleted' }) - expect(existsSync(directory)).toBe(false) - }) - it('returns deletion admission before physical cleanup while holding the mutation gate until completion', async () => { const stop = Promise.withResolvers() const f = fixture({ stop: () => stop.promise }) @@ -279,13 +193,9 @@ describe('legacy conversation archive RPC', () => { started.resolve() return stop.promise } }) - const active = f.seed('active') const removed = f.seed('removed') - await expect(f.cleanup.execute(f.target('active'))).resolves.toBe(false) f.conversations.markDeleted('removed', now) const input = f.target('removed') - await expect(f.cleanup.execute({ ...input, expectedRevision: input.expectedRevision - 1 })).resolves.toBe(false) - expect(f.deletion.has('removed')).toBe(false) await f.mutations.run('removed', async () => { await expect(f.cleanup.execute(input)).rejects.toMatchObject({ code: 'CONVERSATION_BUSY' }) }) @@ -301,24 +211,5 @@ describe('legacy conversation archive RPC', () => { stop.resolve(0) await expect(pending).resolves.toEqual({ status: 'deleted' }) expect(existsSync(removed)).toBe(false) - expect(existsSync(active)).toBe(true) - }) - - it('keeps failed cleanup durable and retryable without claiming data was deleted', async () => { - let fail = true - const f = fixture({ stop: async () => { - if (fail) - throw new Error('Stop failed') - return 0 - } }) - const directory = f.seed('removed') - f.conversations.markDeleted('removed', now) - await expect(f.cleanup.delete({ ...f.target('removed'), scope: 'legacy_removed' })).resolves.toMatchObject({ status: 'failed' }) - expect(existsSync(directory)).toBe(true) - expect(f.database.prepare('SELECT status, origin FROM conversation_deletion_jobs').get()).toMatchObject({ status: 'failed', origin: 'legacy_removed' }) - expect(f.cleanup.list({ scope: 'legacy_removed' }).items).toMatchObject([{ conversationId: 'removed', canDelete: true, expectedRevision: f.target('removed').expectedRevision }]) - fail = false - await expect(f.cleanup.delete({ ...f.target('removed'), scope: 'legacy_removed' })).resolves.toEqual({ status: 'deleted' }) - expect(existsSync(directory)).toBe(false) }) }) diff --git a/apps/buddy/service/src/conversations/__tests__/registerConversationRpc.spec.ts b/apps/buddy/service/src/conversations/__tests__/registerConversationRpc.spec.ts index 0c80d841a..8ea0b780c 100644 --- a/apps/buddy/service/src/conversations/__tests__/registerConversationRpc.spec.ts +++ b/apps/buddy/service/src/conversations/__tests__/registerConversationRpc.spec.ts @@ -3,13 +3,12 @@ import type { RuntimeRequestHandler } from '../../../../shared/runtime/rpcPeer' import type { RuntimeRequestRegistrar } from '../../rpc/runtimeRequest' import type { RegisterConversationRpcOptions } from '../registerConversationRpc' import { afterEach, describe, expect, it } from 'vitest' -import { conversationResponseSchemas } from '../../../../shared/conversation/conversationApi' import { RunEventCorruptionError } from '../../events/RunEventFailure' import { RunEventProjector } from '../../events/RunEventProjector' import { createBuddyRepository } from '../../storage/buddyRepository' import { createConversationArchiveRepository } from '../../storage/conversationArchiveRepository' import { createConversationRepository } from '../../storage/conversationRepository' -import { openBuddyDatabase } from '../../storage/database' +import { openBuddyDatabase, openBuddyMigrationCandidate } from '../../storage/database' import { createRunRepository } from '../../storage/runRepository' import { ConversationMetadataService } from '../ConversationMetadataService' import { registerConversationRpc } from '../registerConversationRpc' @@ -23,55 +22,6 @@ afterEach(() => { }) describe('registerConversationRpc', () => { - it('persists explicit model selection without resolved token limits and rejects invalid reads', async () => { - const database = openBuddyDatabase({ databasePath: ':memory:' }) - databases.push(database) - const conversations = createConversationRepository(database) - conversations.create({ - branchId: 'branch-1', - createdAt: '2026-08-27T00:00:00.000Z', - approvalPolicy: 'policy' as const, - executionProfile: 'workspace_write', - id: 'conversation-1', - spaceId: null, - title: 'Original title', - }) - const harness = createRpcHarness() - registerConversationRpc({ - ...emptyConversationDependencies(conversations), - rpc: harness.rpc, - metadata: new ConversationMetadataService({ - repository: conversations, - resolveModelSelection: async selection => ({ ...selection, contextWindow: 128_000, maxTokens: 16_384 }), - sessions: { invalidateConversation: async () => ({ pending: 0, degraded: 0 }) }, - }), - }) - - const modelSelection = { - modelId: 'model-1', - providerId: 'provider-1', - reasoning: 'high' as const, - serviceTier: 'priority' as const, - } - await expect(harness.invoke('conversations.setModelSelection', { - conversationId: 'conversation-1', - modelSelection, - })).resolves.toMatchObject({ modelSelection }) - const storedSelection = database.prepare(` - SELECT model_selection_json, model_selection_source FROM conversation_members WHERE conversation_id = ? AND actor_id = 'default' - `).get('conversation-1') as { model_selection_json: string, model_selection_source: string } - expect(JSON.parse(storedSelection.model_selection_json)).toEqual(modelSelection) - expect(storedSelection.model_selection_source).toBe('explicit') - - conversations.markDeleted('conversation-1', '2026-08-27T00:05:00.000Z') - await expect(harness.invoke('conversations.get', { - conversationId: 'conversation-1', - })).rejects.toMatchObject({ code: 'VALIDATION_FAILED' }) - await expect(harness.invoke('conversations.list', { unexpected: true })) - .rejects - .toMatchObject({ code: 'VALIDATION_FAILED' }) - }) - it('projects attachments, public actor replies, root events and artifacts while paging through the root input', async () => { const database = openBuddyDatabase({ databasePath: ':memory:' }) databases.push(database) @@ -207,7 +157,6 @@ describe('registerConversationRpc', () => { conversationId: 'conversation-1', limit: 2, }) as Record - expect(conversationResponseSchemas.timelinePage.safeParse(timeline).success).toBe(true) expect(timeline).toMatchObject({ items: [ { @@ -252,7 +201,7 @@ describe('registerConversationRpc', () => { }) it('reads archived details, models, members and usage without permitting writes or exposing deleted history', async () => { - const database = openBuddyDatabase({ databasePath: ':memory:' }) + const database = openBuddyMigrationCandidate({ databasePath: ':memory:' }) databases.push(database) const now = '2026-11-07T00:00:00.000Z' const conversations = createConversationRepository(database) diff --git a/apps/buddy/service/src/conversations/registerConversationArchiveRpc.ts b/apps/buddy/service/src/conversations/registerConversationArchiveRpc.ts index 5300fdb08..05b10d7fc 100644 --- a/apps/buddy/service/src/conversations/registerConversationArchiveRpc.ts +++ b/apps/buddy/service/src/conversations/registerConversationArchiveRpc.ts @@ -3,18 +3,43 @@ import type { ConversationArchiveService } from './ConversationArchiveService' import { conversationArchiveRpc } from '../../../shared/conversation/conversationArchiveApi' import { registerRuntimeRequest } from '../rpc/runtimeRequest' +class ArchiveCommandRequiredError extends Error { + readonly code = 'METHOD_NOT_AVAILABLE' + + constructor() { + super('This operation requires the product command protocol') + this.name = 'ArchiveCommandRequiredError' + } +} + export function registerConversationArchiveRpc(options: { rpc: RuntimeRequestRegistrar - archive: Pick + archive: Pick + legacyWrites?: boolean }): () => void { + function assertLegacyWrites(): void { + if (options.legacyWrites !== true) + throw new ArchiveCommandRequiredError() + } const disposers = [ registerRuntimeRequest(options.rpc, conversationArchiveRpc.listArchived, input => options.archive.list(input)), registerRuntimeRequest(options.rpc, conversationArchiveRpc.getArchivePolicy, () => options.archive.getPolicy()), - registerRuntimeRequest(options.rpc, conversationArchiveRpc.archive, input => options.archive.archive(input)), - registerRuntimeRequest(options.rpc, conversationArchiveRpc.restoreArchive, input => options.archive.restore(input)), - registerRuntimeRequest(options.rpc, conversationArchiveRpc.deleteArchived, input => options.archive.deleteArchived(input)), - registerRuntimeRequest(options.rpc, conversationArchiveRpc.deletePermanently, input => options.archive.deletePermanently(input)), - registerRuntimeRequest(options.rpc, conversationArchiveRpc.setArchivePolicy, input => options.archive.setPolicy(input.days)), + registerRuntimeRequest(options.rpc, conversationArchiveRpc.archive, (input) => { + assertLegacyWrites() + return options.archive.archive(input) + }), + registerRuntimeRequest(options.rpc, conversationArchiveRpc.restoreArchive, (input) => { + assertLegacyWrites() + return options.archive.restore(input) + }), + registerRuntimeRequest(options.rpc, conversationArchiveRpc.deleteArchived, (input) => { + assertLegacyWrites() + return options.archive.deleteArchived(input) + }), + registerRuntimeRequest(options.rpc, conversationArchiveRpc.setArchivePolicy, (input) => { + assertLegacyWrites() + return options.archive.setPolicy(input.days) + }), ] return () => disposers.splice(0).forEach(dispose => dispose()) } diff --git a/apps/buddy/service/src/conversations/registerConversationRpc.ts b/apps/buddy/service/src/conversations/registerConversationRpc.ts index bd02d7aff..8b2f6b214 100644 --- a/apps/buddy/service/src/conversations/registerConversationRpc.ts +++ b/apps/buddy/service/src/conversations/registerConversationRpc.ts @@ -69,7 +69,7 @@ export function registerConversationRpc(options: RegisterConversationRpcOptions) disposers.push(registerRuntimeRequest(options.rpc, conversationHistoryRpc.listHistory, input => options.conversations.listHistory(input))) disposers.push(registerRuntimeRequest(options.rpc, conversationHistoryRpc.getHistoryEntries, input => options.conversations.getHistoryEntries(input.ids))) disposers.push(registerRuntimeRequest(options.rpc, conversationsRpc.get, (input) => { - return requireReadableConversation(options, input.conversationId) + return requireActiveConversation(options, input.conversationId) })) disposers.push(registerRuntimeRequest(options.rpc, conversationsRpc.getModelControls, input => options.metadata.getModelControls(input.conversationId, input.actorId))) disposers.push(registerRuntimeRequest(options.rpc, conversationsRpc.rename, (input) => { @@ -102,13 +102,13 @@ export function registerConversationRpc(options: RegisterConversationRpcOptions) return options.metadata.activateBranch(input) })) disposers.push(registerRuntimeRequest(options.rpc, conversationsRpc.listBranches, (input) => { - requireReadableConversation(options, input.conversationId) + requireActiveConversation(options, input.conversationId) if (options.isDeleting(input.conversationId)) throw new BuddyServiceError('VALIDATION_FAILED') return options.conversations.listBranches(input.conversationId) })) disposers.push(registerRuntimeRequest(options.rpc, conversationsRpc.listMessages, async (input) => { - const conversation = requireReadableConversation(options, input.conversationId) + const conversation = requireActiveConversation(options, input.conversationId) const branchId = input.branchId ?? requireValue(conversation.activeBranchId) const readPage = () => options.conversations.listMessagePage( input.conversationId, @@ -124,7 +124,7 @@ export function registerConversationRpc(options: RegisterConversationRpcOptions) }, ) const page = await readVerifiedHistoryPage(options, readPage, page => page.items.flatMap(item => item.runId ? [item.runId] : [])) - requireReadableConversation(options, input.conversationId) + requireActiveConversation(options, input.conversationId) return { items: withMessageAttachments( page.items, @@ -140,7 +140,7 @@ export function registerConversationRpc(options: RegisterConversationRpcOptions) } })) disposers.push(registerRuntimeRequest(options.rpc, conversationsRpc.listTimeline, async (input) => { - const conversation = requireReadableConversation(options, input.conversationId) + const conversation = requireActiveConversation(options, input.conversationId) const branchId = input.branchId ?? requireValue(conversation.activeBranchId) const readPage = () => options.conversations.listTimelinePage( input.conversationId, @@ -165,7 +165,7 @@ export function registerConversationRpc(options: RegisterConversationRpcOptions) ...runs.map(run => run.id), ...page.items.flatMap(item => item.kind === 'message' && item.runId ? [item.runId] : []), ], true) - requireReadableConversation(options, input.conversationId) + requireActiveConversation(options, input.conversationId) const items = withMessageAttachments( page.items, options.attachments.listForConversation(input.conversationId), @@ -225,7 +225,7 @@ async function readVerifiedHistoryPage( throw new BuddyServiceError('VALIDATION_FAILED') } -function requireReadableConversation( +function requireActiveConversation( options: Pick, conversationId: string, ) { diff --git a/apps/buddy/service/src/conversations/sessionReferenceExtension.ts b/apps/buddy/service/src/conversations/sessionReferenceExtension.ts index b00ed68ff..357903bc7 100644 --- a/apps/buddy/service/src/conversations/sessionReferenceExtension.ts +++ b/apps/buddy/service/src/conversations/sessionReferenceExtension.ts @@ -29,8 +29,8 @@ export function createSessionReferenceCapability(options: SessionReferenceServic } const message = messageId ? options.conversations.findMessageById(messageId) : null const sequence = message ? options.references.messageSequence(options.conversationId, message.id) : null - return access.resolve(current && !current.deletedAt && current.activeBranchId && message?.conversationId === current.id && message.role === 'user' && sequence !== null - ? { conversationId: current.id, branchId: current.activeBranchId, throughSequence: sequence } + return access.resolve(current && !current.deletedAt && message?.conversationId === current.id && message.role === 'user' && sequence !== null + ? { conversationId: current.id, branchId: message.branchId, throughSequence: sequence } : null) } function requireTarget(sessionId: string, branchId?: string) { diff --git a/apps/buddy/service/src/createBuddyCapabilityFactory.ts b/apps/buddy/service/src/createBuddyCapabilityFactory.ts index 9a6921a64..49ac3fb4c 100644 --- a/apps/buddy/service/src/createBuddyCapabilityFactory.ts +++ b/apps/buddy/service/src/createBuddyCapabilityFactory.ts @@ -1,7 +1,6 @@ import type { ContextPanelSource } from '../../shared/context-panel/contextPanel' import type { ApplicationDiagnosticReporter } from '../../shared/diagnostics/applicationDiagnostic' import type { BuddyFeatureId, BuddyPlatform } from '../../shared/platform' -import type { RuntimeRpcPeerContract } from '../../shared/runtime/rpcPeer' import type { BuddyCapability, BuddyCapabilityContext, BuddyCapabilityFactory } from './agent/extensions/BuddyCapability' import type { ArtifactService } from './artifacts/ArtifactService' import type { AttachmentService } from './attachments/AttachmentService' @@ -17,6 +16,7 @@ import type { ImageRequestStore } from './images/ImageRequestStore' import type { ImageTransformService } from './images/ImageTransformService' import type { PetActionService } from './pet/PetActionService' import type { PluginAuthoringService } from './plugins/PluginAuthoringService' +import type { PluginAuthoringHost } from './plugins/PluginHost' import type { SkillAuthoringServices } from './skills/skillAuthoringCapability' import type { ExtensionReferenceRepository } from './storage/extensionReferenceRepository' import type { SessionReferenceRepository } from './storage/sessionReferenceRepository' @@ -47,7 +47,7 @@ export interface BuddyCapabilityServices { getRunBuddyId: (runId: string) => string | null record?: ApplicationDiagnosticReporter pluginCapabilities?: (context: BuddyCapabilityContext) => Promise - pluginAuthoring: Pick + pluginAuthoring: PluginAuthoringHost pluginBuilder: PluginAuthoringService skillAuthoring: Omit artifactService: ImageGenerationServiceOptions['artifactService'] & Pick & SessionReferenceServices['artifacts'] diff --git a/apps/buddy/service/src/events/RunEventFailure.ts b/apps/buddy/service/src/events/RunEventFailure.ts index 14ba8fe05..ef4f1e82a 100644 --- a/apps/buddy/service/src/events/RunEventFailure.ts +++ b/apps/buddy/service/src/events/RunEventFailure.ts @@ -1,4 +1,5 @@ import type { BuddyRunEvent } from './BuddyRunEvent' +import { readTransactionFailure } from '../storage/transaction' export interface RunEventFailureScope { firstSequence: number | null @@ -94,6 +95,19 @@ export class RunEventStorageError extends RunEventLogFatalError { } } +export class RunEventDatabaseError extends RunEventLogFatalError { + readonly code = 'EVENT_STORAGE_FAILED' + readonly commitState: 'not_applicable' | 'unknown' + readonly operation: 'append' | 'checkpoint' | 'compact' | 'read' + + constructor(runId: string, operation: RunEventDatabaseError['operation'], cause: unknown) { + super('Lexora Buddy run event database failed', runEventFailureScope(runId), { cause }) + this.name = 'RunEventDatabaseError' + this.operation = operation + this.commitState = readTransactionFailure(cause)?.outcome === 'unknown' ? 'unknown' : 'not_applicable' + } +} + export function runEventFailureScope( runId: string, events: readonly BuddyRunEvent[] = [], diff --git a/apps/buddy/service/src/events/RunEventLog.ts b/apps/buddy/service/src/events/RunEventLog.ts index 3da0ddb83..0eabf23db 100644 --- a/apps/buddy/service/src/events/RunEventLog.ts +++ b/apps/buddy/service/src/events/RunEventLog.ts @@ -13,13 +13,12 @@ import type { RunEventStore } from './RunEventStore' import { performance } from 'node:perf_hooks' import { Emitter } from '../../../shared/events/Emitter' import { copyEventSnapshot } from '../../../shared/events/eventSnapshot' -import { executionMailboxDraftSchema, executionMailboxMessageSchema } from '../../../shared/executions/executionApi' -import { hashExecutionMailboxMessage } from '../storage/mailboxRepository' import { buddyRunEventSchema, buddyRunIdSchema, isTerminalRunEventType, } from './BuddyRunEvent' +import { prepareEventPayload } from './prepareEventPayload' import { createRunEventCompactionPlan } from './RunEventCompaction' import { RunEventCheckpointError, @@ -715,11 +714,3 @@ export class RunEventLog implements RunEventLogPort { function nextEventSequence(events: readonly BuddyRunEvent[]): number { return (events.at(-1)?.sequence ?? 0) + 1 } - -export function prepareEventPayload(input: AppendBuddyRunEventInput, sequence: number): unknown { - if (input.type !== 'execution.mailbox.sent') - return input.payload ?? null - const draft = executionMailboxDraftSchema.parse(input.payload) - const message = { ...draft, sourceRunId: input.runId, sourceSequence: sequence } - return executionMailboxMessageSchema.parse({ ...message, payloadHash: hashExecutionMailboxMessage(message) }) -} diff --git a/apps/buddy/service/src/events/RunEventPorts.ts b/apps/buddy/service/src/events/RunEventPorts.ts index cc03e89f7..0b367874a 100644 --- a/apps/buddy/service/src/events/RunEventPorts.ts +++ b/apps/buddy/service/src/events/RunEventPorts.ts @@ -36,8 +36,6 @@ export interface RunEventMaintenance { compactTerminalRun: (runId: string) => Promise compactTerminalRuns: () => Promise recoverAll: (options?: RunEventRecoveryOptions) => Promise> - replay: (runId: string) => Promise - replayAll: () => Promise } export interface RunEventObservation { diff --git a/apps/buddy/service/src/events/RunEventProjector.ts b/apps/buddy/service/src/events/RunEventProjector.ts index 9611f2571..40ab8b5de 100644 --- a/apps/buddy/service/src/events/RunEventProjector.ts +++ b/apps/buddy/service/src/events/RunEventProjector.ts @@ -10,6 +10,7 @@ import { approvalReviewPayloadSchema, } from '../../../shared/permissions/approvalReviewPayload' import { buddyAssistantTextPhaseSchema } from '../../../shared/runs/assistantTextPhase' +import { approvalReviewSchema } from '../approvals/approvalReview' import { projectConversationPublication } from '../storage/conversationPublicationRepository' import { withTransaction } from '../storage/database' import { createMailboxRepository } from '../storage/mailboxRepository' @@ -19,6 +20,7 @@ import { canCertifyBootstrapCheckpoint, RUN_EVENT_COMPACTION_VERSION, RUN_EVENT_ const messageIdSchema = z.string().min(1).max(256) const approvalRequestPayloadSchema = z.object({ + review: approvalReviewSchema.optional(), createdAt: z.iso.datetime(), id: z.string().min(1), kind: z.enum(APPROVAL_REVIEW_KINDS), @@ -33,6 +35,8 @@ const approvalRequestPayloadSchema = z.object({ { path: ['payload'] }, ) const approvalResolutionPayloadSchema = z.object({ + actorId: z.string().min(1).optional(), + cancellationReason: z.enum(['cancelled', 'expired', 'recovery', 'shutdown']).optional(), id: z.string().min(1), resolution: z.enum(['approved', 'approved_persistently', 'approved_for_operation', 'approved_for_source', 'approved_for_turn', 'cancelled', 'denied']).optional(), resolvedAt: z.iso.datetime(), @@ -111,6 +115,17 @@ export class RunEventProjector { this.#database = database } + append(events: readonly BuddyRunEvent[]): number { + return withTransaction(this.#database, () => { + if (events.length === 0) + return 0 + if (events.some(event => event.runId !== events[0]!.runId)) + throw new Error('A run event batch must belong to one run') + this.validateNewFacts(events) + return events.reduce((count, event) => count + this.#project(event, 'new'), 0) + }) + } + project(events: readonly BuddyRunEvent[], checkpoint?: RunEventCheckpoint): number { return withTransaction(this.#database, () => { const count = events.reduce((count, event) => count + this.#project(event), 0) @@ -287,11 +302,11 @@ export class RunEventProjector { } } - #project(event: BuddyRunEvent, mode: 'append' | 'rebuild' = 'append'): number { + #project(event: BuddyRunEvent, mode: 'append' | 'rebuild' | 'new' = 'append'): number { const result = this.#database.prepare(` INSERT INTO run_events (run_id, sequence, event_type, payload_json, created_at) VALUES (?, ?, ?, ?, ?) - ON CONFLICT (run_id, sequence) DO NOTHING + ${mode === 'new' ? '' : 'ON CONFLICT (run_id, sequence) DO NOTHING'} `).run( event.runId, event.sequence, @@ -300,8 +315,8 @@ export class RunEventProjector { event.createdAt, ) createMailboxRepository(this.#database).project(event) - projectRunStatus(this.#database, event, mode) - projectProductMessage(this.#database, event, mode) + projectRunStatus(this.#database, event, mode === 'rebuild' ? 'rebuild' : 'append') + projectProductMessage(this.#database, event, mode === 'rebuild' ? 'rebuild' : 'append') projectConversationPublication(this.#database, event) projectApprovalRequest(this.#database, event) projectApprovalResolution(this.#database, event) @@ -418,8 +433,13 @@ function projectApprovalResolution(database: DatabaseSync, event: BuddyRunEvent) UPDATE approvals SET status = ?, resolved_at = ? WHERE id = ? AND run_id = ? AND status = 'pending' `).run(resolution.status, resolution.resolvedAt, resolution.id, event.runId) - if (Number(result.changes) === 1) + if (Number(result.changes) === 1) { + if (database.prepare('SELECT 1 FROM sqlite_master WHERE type = \'table\' AND name = \'approval_decisions\'').get()) { + database.prepare('INSERT INTO approval_decisions (approval_id, resolution, actor_id, cancellation_reason, decided_at) VALUES (?, ?, ?, ?, ?)') + .run(resolution.id, resolution.resolution ?? null, resolution.actorId ?? null, resolution.cancellationReason ?? null, resolution.resolvedAt) + } return + } throw approvalConflict(resolution.id) } @@ -444,8 +464,11 @@ function projectApprovalRequest(database: DatabaseSync, event: BuddyRunEvent): v payloadJson, approval.createdAt, ) - if (Number(result.changes) === 1) + if (Number(result.changes) === 1) { + if (approval.review) + database.prepare('INSERT INTO approval_reviews (approval_id, review_json) VALUES (?, ?)').run(approval.id, JSON.stringify(approval.review)) return + } throw approvalConflict(approval.id) } diff --git a/apps/buddy/service/src/events/RunEventQueries.ts b/apps/buddy/service/src/events/RunEventQueries.ts index 790953569..fa55caecb 100644 --- a/apps/buddy/service/src/events/RunEventQueries.ts +++ b/apps/buddy/service/src/events/RunEventQueries.ts @@ -10,13 +10,17 @@ interface RunEventRow { event_type: string payload_json: string created_at: string + format_version?: number } export class RunEventQueries { readonly #database: DatabaseSync - constructor(database: DatabaseSync) { + readonly #order: 'event_time' | 'run_sequence' + + constructor(database: DatabaseSync, order: 'event_time' | 'run_sequence' = 'event_time') { this.#database = database + this.#order = order } findLatest(runId: string, eventTypes: readonly string[]): BuddyRunEvent | null { @@ -62,19 +66,15 @@ export class RunEventQueries { const rows = this.#database.prepare(` SELECT * FROM ( SELECT - events.run_id, - events.sequence, - events.event_type, - events.payload_json, - events.created_at + events.*, runs.started_at AS run_started_at FROM run_events AS events INNER JOIN runs ON runs.id = events.run_id WHERE runs.conversation_id = ? AND runs.execution_kind IN ('root', 'maintenance') ${excluded.length ? `AND runs.id NOT IN (${excluded.map(() => '?').join(', ')})` : ''} - ORDER BY events.created_at DESC, events.run_id DESC, events.sequence DESC + ORDER BY ${this.#order === 'run_sequence' ? 'runs.started_at' : 'events.created_at'} DESC, events.run_id DESC, events.sequence DESC LIMIT ? ) - ORDER BY created_at, run_id, sequence + ORDER BY ${this.#order === 'run_sequence' ? 'run_started_at' : 'created_at'}, run_id, sequence `).all(conversationId, ...excluded, limit) return (rows as unknown as RunEventRow[]).map(toRunEvent) } @@ -92,12 +92,13 @@ export class RunEventQueries { SELECT run_id, (SELECT MAX(sequence) FROM run_events WHERE run_events.run_id = requested.run_id AND event_type = 'run.progress') FROM requested ) SELECT events.* FROM positions JOIN run_events events ON events.run_id = positions.run_id AND events.sequence = positions.sequence - ORDER BY events.created_at, events.run_id, events.sequence + JOIN runs ON runs.id = events.run_id + ORDER BY ${this.#order === 'run_sequence' ? 'runs.started_at' : 'events.created_at'}, events.run_id, events.sequence `).all(...ids) : this.#database.prepare(` - SELECT * FROM run_events - WHERE run_id IN (${placeholders}) - ORDER BY created_at, run_id, sequence + SELECT events.* FROM run_events events JOIN runs ON runs.id = events.run_id + WHERE events.run_id IN (${placeholders}) + ORDER BY ${this.#order === 'run_sequence' ? 'runs.started_at' : 'events.created_at'}, events.run_id, events.sequence `).all(...ids) return (rows as unknown as RunEventRow[]).map(toRunEvent) } @@ -229,6 +230,8 @@ export class RunEventQueries { } function toRunEvent(row: RunEventRow): BuddyRunEvent { + if (row.format_version !== undefined && row.format_version !== 1) + throw new Error('Run event format is not supported') return buddyRunEventSchema.parse({ createdAt: row.created_at, payload: JSON.parse(row.payload_json), diff --git a/apps/buddy/service/src/events/SqliteRunEventLog.ts b/apps/buddy/service/src/events/SqliteRunEventLog.ts new file mode 100644 index 000000000..6e4ef8351 --- /dev/null +++ b/apps/buddy/service/src/events/SqliteRunEventLog.ts @@ -0,0 +1,247 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { AppendBuddyRunEventInput, BuddyRunEvent, ListBuddyRunEventsOptions } from './BuddyRunEvent' +import type { RunEventMaintenanceOptions } from './RunEventMaintenanceService' +import type { RunEventLogPort } from './RunEventPorts' +import type { RunEventRecoveryFailure, RunEventRecoveryOptions, RunEventRecoverySummary } from './RunEventRecovery' +import { performance } from 'node:perf_hooks' +import { Emitter } from '../../../shared/events/Emitter' +import { checkpointSqliteStorage } from '../storage/checkpointSqliteStorage' +import { requireSqliteAuthority } from '../storage/sqliteAuthority' +import { getStorageIsolation } from '../storage/StorageIsolation' +import { assertCommittedRead } from '../storage/transaction' +import { buddyRunIdSchema } from './BuddyRunEvent' +import { RunEventDatabaseError } from './RunEventFailure' +import { RunEventMaintenanceService } from './RunEventMaintenanceService' +import { RunEventQueries } from './RunEventQueries' +import { SqliteRunEventWriter } from './SqliteRunEventWriter' + +export interface SqliteRunEventLogOptions { + database: DatabaseSync + onObserverError?: (error: unknown) => void + onFatalFailure?: (error: RunEventDatabaseError) => void +} + +export class SqliteRunEventLog implements RunEventLogPort { + readonly #database: DatabaseSync + readonly #queries: RunEventQueries + readonly #writer: SqliteRunEventWriter + readonly #maintenance: RunEventMaintenanceService + readonly #quarantineChanges = new Emitter>(() => {}) + readonly #onFatalFailure?: SqliteRunEventLogOptions['onFatalFailure'] + readonly onDidCommit: RunEventLogPort['onDidCommit'] + readonly onDidQuarantine = this.#quarantineChanges.event + #closed = false + #closePromise: Promise | undefined + #failure: RunEventDatabaseError | null = null + + constructor(options: SqliteRunEventLogOptions) { + assertCommittedRead(options.database) + requireSqliteAuthority(options.database) + this.#database = options.database + this.#queries = new RunEventQueries(options.database, 'run_sequence') + this.#writer = new SqliteRunEventWriter(options.database, options.onObserverError) + this.#onFatalFailure = options.onFatalFailure + this.onDidCommit = this.#writer.onDidCommit + this.#maintenance = new RunEventMaintenanceService({ + list: (after, limit) => this.#execute('', 'read', () => this.#queries.listCompactableTerminalRunIds(after, limit)), + compact: runId => this.compactTerminalRun(runId), + onFailure: () => false, + }) + this.onDidCommit(() => this.#maintenance.wake()) + } + + get transactionWriter(): SqliteRunEventWriter { + return this.#writer + } + + get state(): RunEventLogPort['state'] { + return this.#closed ? 'closed' : this.#failure || getStorageIsolation(this.#database).failure ? 'failed' : 'open' + } + + assertConversationWritable(_conversationId: string): void { + this.#execute('', 'read', () => {}, false) + } + + assertRunWritable(runId: string): void { + this.#execute(runId, 'read', () => this.#assertSequences([runId]), false) + } + + assertRunExecutable(runId: string): void { + this.assertRunWritable(runId) + } + + getRecoveryFailures(): readonly Readonly[] { + return [] + } + + async verifyRun(runId: string): Promise { + await this.read(runId) + } + + async findLatest(runId: string, eventTypes: readonly string[]): Promise { + buddyRunIdSchema.parse(runId) + return this.#execute(runId, 'read', () => { + this.#assertSequences([runId]) + return this.#queries.findLatest(runId, eventTypes) + }) + } + + async append(input: AppendBuddyRunEventInput): Promise { + return this.#execute(input.runId, 'append', () => this.#writer.append(input)) + } + + async appendBatch(inputs: readonly AppendBuddyRunEventInput[]): Promise { + return this.#execute(inputs[0]?.runId ?? '', 'append', () => this.#writer.appendBatch(inputs)) + } + + async read(runId: string): Promise { + buddyRunIdSchema.parse(runId) + return this.#execute(runId, 'read', () => { + this.#assertSequences([runId]) + return this.#queries.read(runId) + }) + } + + async list(runId: string, options: ListBuddyRunEventsOptions = {}): Promise { + buddyRunIdSchema.parse(runId) + assertPage(options) + return this.#execute(runId, 'read', () => { + this.#assertSequences([runId]) + return this.#queries.list(runId, options) + }) + } + + listForConversation(conversationId: string, options: Pick = {}): BuddyRunEvent[] { + assertPage(options) + return this.#execute('', 'read', () => { + const runs = this.#database.prepare('SELECT id FROM runs WHERE conversation_id = ?').all(conversationId) as Array<{ id: string }> + this.#assertSequences(runs.map(run => run.id)) + return this.#queries.listForConversation(conversationId, options) + }) + } + + listForRuns(runIds: readonly string[], options: { latestActivityOnly?: boolean } = {}): BuddyRunEvent[] { + for (const runId of runIds) + buddyRunIdSchema.parse(runId) + return this.#execute('', 'read', () => { + this.#assertSequences(runIds) + return this.#queries.listForRuns(runIds, options) + }) + } + + listToolCounts(runIds: readonly string[]): ReadonlyMap { + return this.#execute('', 'read', () => { + this.#assertSequences(runIds) + return this.#queries.listToolCounts(runIds) + }) + } + + async compactTerminalRun(runId: string): Promise { + return this.#execute(runId, 'compact', () => this.#writer.compactTerminalRun(runId)) + } + + async compactTerminalRuns(): Promise { + let after = '' + let removed = 0 + while (true) { + const runIds = this.#execute('', 'read', () => this.#queries.listCompactableTerminalRunIds(after)) + if (!runIds.length) + return removed + for (const runId of runIds) { + removed += await this.compactTerminalRun(runId) + after = runId + } + } + } + + startMaintenance(options?: RunEventMaintenanceOptions): void { + this.#execute('', 'read', () => this.#maintenance.start(options)) + } + + stopMaintenance(): Promise { + return this.#maintenance.stop() + } + + async recoverAll(options: RunEventRecoveryOptions = {}): Promise> { + const started = performance.now() + const runIds = this.#execute('', 'read', () => this.#queries.listRunIds()) + const enumerationMs = performance.now() - started + let events = 0 + const progress = (completed: number) => { + try { + options.onProgress?.({ completed, total: runIds.length }) + } + catch {} + } + progress(0) + for (const [index, runId] of runIds.entries()) { + events += (await this.read(runId)).length + progress(index + 1) + } + const durationMs = performance.now() - started + return Object.freeze({ durationMs, enumerationMs, inspectionMs: 0, readMs: durationMs - enumerationMs, projectionMs: 0, scanned: runIds.length, skipped: runIds.length, rebuilt: 0, events, failed: 0, failures: [] }) + } + + close(): Promise { + return this.#closePromise ??= (async () => { + await this.stopMaintenance() + try { + if (this.state === 'open' && this.#database.isOpen) + this.#execute('', 'checkpoint', () => checkpointSqliteStorage(this.#database)) + } + finally { + this.#closed = true + this.#writer.dispose() + this.#quarantineChanges.dispose() + } + })() + } + + #assertSequences(runIds: readonly string[]): void { + const invalid = this.#database.prepare(` + SELECT 1 FROM runs WHERE id = ? AND ( + next_event_sequence IS NULL OR next_event_sequence > ? OR + EXISTS (SELECT 1 FROM run_events WHERE run_id = runs.id AND sequence >= runs.next_event_sequence) + ) + `) + const unsupported = this.#database.prepare('SELECT 1 FROM run_events WHERE run_id = ? AND format_version != 1 LIMIT 1') + for (const runId of new Set(runIds)) { + if (invalid.get(runId, Number.MAX_SAFE_INTEGER)) + throw new Error('Run event sequence is unverified or inconsistent') + if (unsupported.get(runId)) + throw new Error('Run event format is not supported') + } + } + + #execute(runId: string, operation: RunEventDatabaseError['operation'], callback: () => T, committedRead = true): T { + if (this.#failure) + throw this.#failure + try { + if (this.#closed) + throw new Error('SQLite run event log is closed') + if (committedRead) + assertCommittedRead(this.#database) + else + getStorageIsolation(this.#database).assertAvailable() + requireSqliteAuthority(this.#database) + return callback() + } + catch (cause) { + this.#failure = new RunEventDatabaseError(runId, operation, cause) + if (!this.#closed) + getStorageIsolation(this.#database).isolate(this.#failure) + try { + this.#onFatalFailure?.(this.#failure) + } + catch {} + throw this.#failure + } + } +} + +function assertPage(options: ListBuddyRunEventsOptions): void { + if (options.limit !== undefined && (!Number.isSafeInteger(options.limit) || options.limit <= 0)) + throw new TypeError('Run event limit must be a positive safe integer') + if (options.afterSequence !== undefined && (!Number.isSafeInteger(options.afterSequence) || options.afterSequence < 0)) + throw new TypeError('Run event cursor must be a nonnegative safe integer') +} diff --git a/apps/buddy/service/src/events/SqliteRunEventWriter.ts b/apps/buddy/service/src/events/SqliteRunEventWriter.ts new file mode 100644 index 000000000..db40954ce --- /dev/null +++ b/apps/buddy/service/src/events/SqliteRunEventWriter.ts @@ -0,0 +1,106 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { EventSnapshot } from '../../../shared/events/eventTypes' +import type { AppendBuddyRunEventInput, BuddyRunEvent } from './BuddyRunEvent' +import { Emitter } from '../../../shared/events/Emitter' +import { copyEventSnapshot } from '../../../shared/events/eventSnapshot' +import { requireSqliteAuthority } from '../storage/sqliteAuthority' +import { afterTransactionCommit, assertCommittedRead, withTransaction } from '../storage/transaction' +import { buddyRunEventSchema, buddyRunIdSchema, isTerminalRunEventType } from './BuddyRunEvent' +import { prepareEventPayload } from './prepareEventPayload' +import { createRunEventCompactionPlan } from './RunEventCompaction' +import { RunEventProjector } from './RunEventProjector' +import { RunEventQueries } from './RunEventQueries' +import { RUN_EVENT_COMPACTION_VERSION, RUN_EVENT_PROJECTION_VERSION } from './RunEventRecovery' + +export class SqliteRunEventWriter { + readonly #database: DatabaseSync + readonly #projector: RunEventProjector + readonly #queries: RunEventQueries + readonly #committed: Emitter> + readonly #onObserverError: (error: unknown) => void + readonly onDidCommit: Emitter>['event'] + #disposed = false + + constructor(database: DatabaseSync, onObserverError: (error: unknown) => void = () => {}) { + requireSqliteAuthority(database) + this.#database = database + this.#projector = new RunEventProjector(database) + this.#queries = new RunEventQueries(database) + this.#onObserverError = onObserverError + this.#committed = new Emitter(onObserverError) + this.onDidCommit = this.#committed.event + } + + append(input: AppendBuddyRunEventInput): BuddyRunEvent { + return this.appendBatch([input])[0]! + } + + appendBatch(inputs: readonly AppendBuddyRunEventInput[]): BuddyRunEvent[] { + return withTransaction(this.#database, () => { + this.#assertWritable() + if (inputs.length === 0) + return [] + const runId = inputs[0]!.runId + if (inputs.some(input => input.runId !== runId)) + throw new Error('A run event batch must belong to one run') + const allocation = this.#database.prepare(` + UPDATE runs SET next_event_sequence = next_event_sequence + ? + WHERE id = ? AND next_event_sequence <= ? + AND NOT EXISTS (SELECT 1 FROM run_events WHERE run_id = runs.id AND sequence >= runs.next_event_sequence) + RETURNING next_event_sequence - ? AS first_sequence + `).get(inputs.length, runId, Number.MAX_SAFE_INTEGER - inputs.length, inputs.length) + if (!allocation || typeof allocation.first_sequence !== 'number') + throw new Error('Run event sequence is missing, unverified, inconsistent or exhausted') + const firstSequence = allocation.first_sequence + const events = inputs.map((input, index) => buddyRunEventSchema.parse(JSON.parse(JSON.stringify({ + ...input, + sequence: firstSequence + index, + payload: prepareEventPayload(input, firstSequence + index), + createdAt: input.createdAt ?? new Date().toISOString(), + })))) + this.#projector.append(events) + this.#database.prepare('UPDATE run_event_maintenance SET recovery_pending = 0, projection_version = ? WHERE run_id = ?').run(RUN_EVENT_PROJECTION_VERSION, runId) + const snapshots = events.map(event => copyEventSnapshot(event)) + afterTransactionCommit(this.#database, () => this.#committed.fireBatch(snapshots), this.#onObserverError) + return events + }) + } + + compactTerminalRun(runId: string): number { + assertCommittedRead(this.#database) + return withTransaction(this.#database, () => { + this.#assertWritable() + buddyRunIdSchema.parse(runId) + const run = this.#database.prepare(` + SELECT next_event_sequence IS NOT NULL AS verified, + EXISTS (SELECT 1 FROM run_events + WHERE run_id = runs.id AND sequence >= runs.next_event_sequence) AS invalid_sequence + FROM runs WHERE id = ? AND status IN ('completed', 'failed', 'cancelled') + `).get(runId) + if (!run) + return 0 + if (!run.verified || run.invalid_sequence) + throw new Error('Run event sequence is unverified or inconsistent') + if (this.#database.prepare('SELECT 1 FROM run_events WHERE run_id = ? AND format_version != 1 LIMIT 1').get(runId)) + throw new Error('Run event format is not supported for compaction') + const events = this.#queries.read(runId) + if (!events.some(event => isTerminalRunEventType(event.type))) + return 0 + const { removed } = createRunEventCompactionPlan(events) + this.#projector.removeEventRows(runId, removed.map(event => event.sequence)) + this.#database.prepare('UPDATE run_event_maintenance SET recovery_pending = 0, projection_version = ?, compaction_pending = 0, compaction_version = ? WHERE run_id = ?').run(RUN_EVENT_PROJECTION_VERSION, RUN_EVENT_COMPACTION_VERSION, runId) + return removed.length + }) + } + + dispose(): void { + this.#disposed = true + this.#committed.dispose() + } + + #assertWritable(): void { + if (this.#disposed) + throw new Error('SQLite run event writer is stopped') + requireSqliteAuthority(this.#database) + } +} diff --git a/apps/buddy/service/src/events/__tests__/RunEventLog.spec.ts b/apps/buddy/service/src/events/__tests__/RunEventLog.spec.ts index a13002377..3afe464e2 100644 --- a/apps/buddy/service/src/events/__tests__/RunEventLog.spec.ts +++ b/apps/buddy/service/src/events/__tests__/RunEventLog.spec.ts @@ -71,7 +71,7 @@ describe('runEventLog', () => { expect((await fixture.log.read('run-1')).map(event => event.type)).toEqual(['message.completed']) }) - it('publishes immutable committed facts after projection and retains observers after failure', async () => { + it('publishes committed facts after projection and retains observers after failure', async () => { const fixture = await createFixture() const delivered: number[] = [] fixture.log.onDidCommit(() => { @@ -82,8 +82,6 @@ describe('runEventLog', () => { }) fixture.log.onDidCommit((event) => { expect(fixture.database.prepare('SELECT sequence FROM run_events WHERE run_id = ? AND sequence = ?').get(event.runId, event.sequence)).toBeDefined() - expect(Object.isFrozen(event)).toBe(true) - expect(Object.isFrozen(event.payload)).toBe(true) delivered.push(event.sequence) }) await fixture.log.appendBatch([ @@ -104,8 +102,8 @@ describe('runEventLog', () => { let recordOperation: ((input: unknown) => unknown) | undefined const dispose = registerContextPanelRpc({ rpc: { onRequest: (method, handler) => { - expect(method).toBe(contextPanelRpc.recordOperation) - recordOperation = handler + if (method === contextPanelRpc.recordOperation) + recordOperation = handler return () => {} } }, runs: createRunRepository(fixture.database), @@ -351,7 +349,7 @@ describe('runEventLog', () => { throw new Error('Compaction did not reach file replacement') })]) createConversationDeletionRepository(fixture.database).commit('conversation-1', new Date().toISOString()) - const service = new ConversationPurgeService({ database: fixture.database, home: fixture.root }) + const service = new ConversationPurgeService({ database: fixture.database, home: fixture.root, storageAuthority: 'legacy_jsonl' }) const purging = fixture.log.withConversationPurge('conversation-1', () => service.purge('conversation-1')) await expect(fixture.log.append({ runId: 'run-1', type: 'audit.late', payload: {} })).rejects.toMatchObject({ code: 'CONVERSATION_BUSY' }) expect(await readFile(fixture.eventFile, 'utf8')).toContain('transient') diff --git a/apps/buddy/service/src/events/__tests__/RunEventMaintenance.spec.ts b/apps/buddy/service/src/events/__tests__/RunEventMaintenance.spec.ts index 2e8c03f5c..400927c0f 100644 --- a/apps/buddy/service/src/events/__tests__/RunEventMaintenance.spec.ts +++ b/apps/buddy/service/src/events/__tests__/RunEventMaintenance.spec.ts @@ -284,33 +284,21 @@ describe('durable event maintenance', () => { ])('preserves legacy %s facts when both the event index and log are missing or empty', async (table, sql) => { const f = await fixture({}, true) f.database.exec(`UPDATE runs SET status = 'completed' WHERE id = 'run-1'; ${sql};`) - const before = f.database.prepare(`SELECT * FROM ${table}`).all().map(row => table === 'usage_records' ? { ...row, detached: 0 } : row) + const before = f.database.prepare(`SELECT * FROM ${table}`).all() + const columns = Object.keys(before[0]!).join(', ') await f.reopen() expect(maintenance(f.database)).toMatchObject({ recovery_pending: 1 }) await expect(f.log.verifyRun('run-1')).rejects.toMatchObject({ code: 'EVENT_LOG_CORRUPTED' }) expect(await f.log.recoverAll()).toMatchObject({ scanned: 1, failed: 1, rebuilt: 0 }) - expect(f.database.prepare(`SELECT * FROM ${table}`).all()).toEqual(before) + expect(f.database.prepare(`SELECT ${columns} FROM ${table}`).all()).toEqual(before) await mkdir(join(f.root, 'conversations', 'conversation-1', 'events'), { recursive: true }) await writeFile(f.file(), '') await f.reopen() expect(await f.log.recoverAll()).toMatchObject({ failed: 1, rebuilt: 0 }) - expect(f.database.prepare(`SELECT * FROM ${table}`).all()).toEqual(before) + expect(f.database.prepare(`SELECT ${columns} FROM ${table}`).all()).toEqual(before) expect(maintenance(f.database)).toMatchObject({ recovery_pending: 1 }) }) - it('certifies an empty terminal run without rebuilding or repeating recovery on reopen', async () => { - const f = await fixture() - f.database.exec('UPDATE runs SET status = \'completed\' WHERE id = \'run-1\'') - const before = f.database.prepare('SELECT * FROM runs WHERE id = ?').get('run-1') - expect(await f.log.recoverAll({ force: true })).toMatchObject({ scanned: 1, skipped: 1, rebuilt: 0, events: 0, failed: 0 }) - expect(f.database.prepare('SELECT * FROM runs WHERE id = ?').get('run-1')).toEqual(before) - expect(maintenance(f.database)).toMatchObject({ recovery_pending: 0, compaction_pending: 0 }) - await f.reopen() - expect(await f.log.recoverAll()).toMatchObject({ scanned: 0, rebuilt: 0, failed: 0 }) - expect(await f.log.read('run-1')).toEqual([]) - expect(await f.log.append({ runId: 'run-1', type: 'future.audit', payload: {} })).toMatchObject({ sequence: 1 }) - }) - it('does not compact active runs and rechecks a terminal run only after compaction-version invalidation', async () => { const f = await fixture() await f.log.append({ runId: 'run-1', type: 'tool.updated', payload: { toolCallId: 'tool-1' } }) @@ -322,10 +310,8 @@ describe('durable event maintenance', () => { await f.log.append({ runId: 'run-1', type: 'run.completed', payload: {} }) expect(await f.log.compactTerminalRuns()).toBe(1) await f.reopen() - const inspect = vi.spyOn(RunEventStore.prototype, 'fingerprint') expect(await f.log.recoverAll()).toMatchObject({ scanned: 0 }) expect(await f.log.compactTerminalRuns()).toBe(0) - expect(inspect).not.toHaveBeenCalled() expect(await f.log.compactTerminalRun('run-1')).toBe(0) f.database.exec('UPDATE run_event_maintenance SET compaction_version = 0') expect(await f.log.compactTerminalRuns()).toBe(0) diff --git a/apps/buddy/service/src/events/__tests__/SqliteRunEventLog.spec.ts b/apps/buddy/service/src/events/__tests__/SqliteRunEventLog.spec.ts new file mode 100644 index 000000000..066c75ec2 --- /dev/null +++ b/apps/buddy/service/src/events/__tests__/SqliteRunEventLog.spec.ts @@ -0,0 +1,188 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { BuddyRunEvent } from '../BuddyRunEvent' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { sqliteRunFixtureTime as now, seedSqliteRun } from '../../storage/__tests__/sqliteRunFixture' +import { openBuddyMigrationCandidate } from '../../storage/database' +import { withTransaction } from '../../storage/transaction' +import { createRunEventLog } from '../createRunEventLog' +import { SqliteRunEventLog } from '../SqliteRunEventLog' +import { SqliteRunEventWriter } from '../SqliteRunEventWriter' + +const databases: DatabaseSync[] = [] +const logs: SqliteRunEventLog[] = [] + +afterEach(async () => { + vi.restoreAllMocks() + for (const log of logs.splice(0)) + await log.close() + for (const database of databases.splice(0)) { + if (database.isOpen) + database.close() + } +}) + +function fixture(ready = true) { + const database = openBuddyMigrationCandidate({ databasePath: ':memory:' }) + databases.push(database) + if (ready) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + return database +} + +function log(database: DatabaseSync, onFatalFailure?: () => void) { + const result = new SqliteRunEventLog({ database, onFatalFailure }) + logs.push(result) + return result +} + +describe('sqlite run event queries', () => { + it('finishes all bounded maintenance pages and verifies committed facts without replaying them', async () => { + const database = fixture() + const events = log(database) + for (let index = 0; index < 19; index++) { + const runId = `run-${index}` + seedSqliteRun(database, runId) + await events.appendBatch([ + { runId, type: 'tool.preparing', payload: { toolCallId: `tool-${index}` } }, + { runId, type: 'tool.started', payload: { toolCallId: `tool-${index}` } }, + { runId, type: 'tool.completed', payload: { toolCallId: `tool-${index}` } }, + { runId, type: 'run.completed', payload: {} }, + ]) + } + expect(await events.compactTerminalRuns()).toBe(19) + expect(database.prepare('SELECT count(*) AS count FROM run_event_maintenance WHERE recovery_pending != 0 OR compaction_pending != 0').get()).toEqual({ count: 0 }) + const before = database.prepare('SELECT * FROM run_events ORDER BY run_id, sequence').all() + expect(await events.recoverAll()).toMatchObject({ scanned: 19, rebuilt: 0, events: 57, failed: 0 }) + expect(database.prepare('SELECT * FROM run_events ORDER BY run_id, sequence').all()).toEqual(before) + expect(events.listToolCounts(['run-1', 'run-2'])).toEqual(new Map([['run-1', 1], ['run-2', 1]])) + expect(await events.findLatest('run-1', ['run.completed'])).toMatchObject({ sequence: 4 }) + expect(events.listForRuns(['run-1'], { latestActivityOnly: true })).toMatchObject([{ sequence: 4 }]) + }) + + it('reads the full authority in sequence order and pages across compaction gaps without a time cursor', async () => { + const database = fixture() + seedSqliteRun(database, 'run-1') + const events = log(database) + await events.appendBatch(Array.from({ length: 602 }, (_, index) => ({ + runId: 'run-1', + type: 'future.fact', + payload: { index, nested: [null, { value: 'retained' }] }, + createdAt: new Date(Date.parse(now) - index).toISOString(), + }))) + database.exec('DELETE FROM run_events WHERE sequence IN (2, 501)') + expect(await events.read('run-1')).toHaveLength(600) + expect((await events.list('run-1', { limit: 3 })).map(event => event.sequence)).toEqual([600, 601, 602]) + expect((await events.list('run-1', { afterSequence: 0, limit: 3 })).map(event => event.sequence)).toEqual([1, 3, 4]) + expect((await events.list('run-1', { afterSequence: 500, limit: 2 })).map(event => event.sequence)).toEqual([502, 503]) + await expect(events.list('run-1', { afterSequence: 602 })).resolves.toEqual([]) + await expect(events.read('missing')).resolves.toEqual([]) + const copy = (await events.read('run-1'))[0]! + copy.payload = null + expect((await events.read('run-1'))[0]?.payload).toEqual({ index: 0, nested: [null, { value: 'retained' }] }) + expect(database.prepare('SELECT next_event_sequence FROM runs').get()?.next_event_sequence).toBe(603) + }) + + it('keeps each run ordered within conversation and multi-run reads even when wall time moves backwards', async () => { + const database = fixture() + for (const id of ['run-a', 'run-b']) + seedSqliteRun(database, id) + seedSqliteRun(database, 'other', { conversationId: 'other-task' }) + const events = log(database) + for (const id of ['run-b', 'run-a', 'other']) { + await events.appendBatch([ + { runId: id, type: 'future.first', payload: {}, createdAt: '2026-09-29T00:00:02.000Z' }, + { runId: id, type: 'future.second', payload: {}, createdAt: now }, + ]) + } + const ids = (items: BuddyRunEvent[]) => items.map(event => `${event.runId}:${event.sequence}`) + expect(ids(events.listForRuns(['run-b', 'run-a', 'run-a']))).toEqual(['run-a:1', 'run-a:2', 'run-b:1', 'run-b:2']) + expect(ids(events.listForConversation('task-1'))).toEqual(['run-a:1', 'run-a:2', 'run-b:1', 'run-b:2']) + expect(ids(events.listForConversation('task-1', { limit: 3 }))).toEqual(['run-a:2', 'run-b:1', 'run-b:2']) + expect(events.listForRuns([])).toEqual([]) + expect(events.listForConversation('missing')).toEqual([]) + }) + + it('rejects pending authority and prevents both pending and ready candidates from entering legacy replay', () => { + const database = fixture(false) + expect(() => log(database)).toThrow('not ready') + const legacy = () => createRunEventLog({ database, conversationsDirectory: '/unused-candidate-history' }) + expect(legacy).toThrow('Legacy JSONL replay') + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + expect(legacy).toThrow('Legacy JSONL replay') + expect(log(database).state).toBe('open') + }) + + it.each(['read', 'list', 'conversation', 'runs'] as const)('refuses unknown durable formats on %s without repairing or replacing facts', async (method) => { + const database = fixture() + seedSqliteRun(database, 'run-1') + const events = log(database, () => { + throw new Error('reporter failed') + }) + await events.append({ runId: 'run-1', type: 'future.fact', payload: { retained: true }, createdAt: now }) + database.exec('UPDATE run_events SET format_version = 2') + const before = database.prepare('SELECT * FROM run_events').all() + const read = () => method === 'read' + ? events.read('run-1') + : method === 'list' + ? events.list('run-1') + : method === 'conversation' ? events.listForConversation('task-1') : events.listForRuns(['run-1']) + await expect(Promise.resolve().then(read)).rejects.toMatchObject({ code: 'EVENT_STORAGE_FAILED', operation: 'read', cause: { message: 'Run event format is not supported' } }) + expect(events.state).toBe('failed') + await expect(events.append({ runId: 'run-1', type: 'run.failed', payload: {} })).rejects.toMatchObject({ code: 'EVENT_STORAGE_FAILED' }) + expect(database.prepare('SELECT * FROM run_events').all()).toEqual(before) + expect(database.prepare('SELECT status FROM runs').get()?.status).toBe('queued') + }) + + it.each(['unverified', 'conflicting', 'overflow'] as const)('does not return an empty history for a run with %s sequence metadata', async (kind) => { + const database = fixture() + if (kind === 'unverified') + database.exec('UPDATE storage_profile SET authority = \'legacy_jsonl\', migration_state = \'pending\'') + seedSqliteRun(database, 'run-1', { nextSequence: kind === 'unverified' ? null : 1 }) + if (kind === 'unverified') + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + if (kind === 'conflicting') + database.prepare('INSERT INTO run_events (run_id, sequence, event_type, payload_json, created_at) VALUES (?, 1, ?, ?, ?)').run('run-1', 'future.fact', '{}', now) + if (kind === 'overflow') + database.exec('UPDATE runs SET next_event_sequence = 9223372036854775807') + await expect(log(database).read('run-1')).rejects.toMatchObject({ code: 'EVENT_STORAGE_FAILED', operation: 'read' }) + }) + + it('does not expose same-connection uncommitted events as durable history', async () => { + const database = fixture() + seedSqliteRun(database, 'run-1') + const events = log(database) + const writer = new SqliteRunEventWriter(database) + expect(() => withTransaction(database, () => { + writer.append({ runId: 'run-1', type: 'future.fact', payload: {} }) + events.listForRuns(['run-1']) + })).toThrow('run event database failed') + expect(database.prepare('SELECT * FROM run_events').all()).toEqual([]) + expect(events.state).toBe('failed') + writer.dispose() + }) + + it('refuses reads after uncertain commit, closed connection, and explicit close', async () => { + const database = fixture() + seedSqliteRun(database, 'run-1') + const events = log(database) + const exec = database.exec.bind(database) + const failure = new Error('commit acknowledgement unavailable') + vi.spyOn(database, 'exec').mockImplementation((sql) => { + exec(sql) + if (sql === 'COMMIT') + throw failure + }) + await expect(events.append({ runId: 'run-1', type: 'future.fact', payload: {} })).rejects.toMatchObject({ commitState: 'unknown', cause: { code: 'STORAGE_TRANSACTION_UNCERTAIN', cause: failure } }) + await expect(events.read('run-1')).rejects.toMatchObject({ commitState: 'unknown' }) + expect(() => log(database)).toThrow('requires verification') + vi.restoreAllMocks() + const closedDatabase = fixture() + const closed = log(closedDatabase) + closedDatabase.close() + await expect(closed.read('run-1')).rejects.toMatchObject({ code: 'EVENT_STORAGE_FAILED' }) + const stopped = log(fixture()) + await stopped.close() + expect(stopped.state).toBe('closed') + await expect(stopped.read('run-1')).rejects.toMatchObject({ code: 'EVENT_STORAGE_FAILED' }) + }) +}) diff --git a/apps/buddy/service/src/events/__tests__/SqliteRunEventWriter.spec.ts b/apps/buddy/service/src/events/__tests__/SqliteRunEventWriter.spec.ts new file mode 100644 index 000000000..92cc7769c --- /dev/null +++ b/apps/buddy/service/src/events/__tests__/SqliteRunEventWriter.spec.ts @@ -0,0 +1,445 @@ +import type { DatabaseSync as SqliteDatabase } from 'node:sqlite' +import type { AppendBuddyRunEventInput, BuddyRunEvent } from '../BuddyRunEvent' +import { mkdtempSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { DatabaseSync } from 'node:sqlite' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { executionMailboxMessageSchema } from '../../../../shared/executions/executionApi' +import { createExecutionStorageFixture } from '../../storage/__tests__/executionFixture' +import { openBuddyMigrationCandidate } from '../../storage/database' +import { createMailboxRepository, hashExecutionMailboxMessage } from '../../storage/mailboxRepository' +import { BUDDY_V43_PERSISTENCE_SCHEMA_SQL } from '../../storage/migrations/v43Persistence' +import { BUDDY_MIGRATION_SCHEMA_VERSION } from '../../storage/schema' +import { readTransactionFailure, withTransaction } from '../../storage/transaction' +import { RunEventQueries } from '../RunEventQueries' +import { SqliteRunEventWriter } from '../SqliteRunEventWriter' + +const databases: SqliteDatabase[] = [] +const writers: SqliteRunEventWriter[] = [] +const directories: string[] = [] +const now = '2026-09-29T00:00:00.000Z' + +afterEach(() => { + vi.restoreAllMocks() + for (const writer of writers.splice(0)) + writer.dispose() + for (const database of databases.splice(0)) { + if (database.isOpen) + database.close() + } + for (const directory of directories.splice(0)) + rmSync(directory, { recursive: true, force: true }) +}) + +function open(databasePath = ':memory:') { + const database = openBuddyMigrationCandidate({ databasePath }) + databases.push(database) + return database +} + +function writer(database: SqliteDatabase, onObserverError?: (error: unknown) => void) { + const events = new SqliteRunEventWriter(database, onObserverError) + writers.push(events) + return events +} + +function seedRun(database: SqliteDatabase, runId: string, nextSequence: number | null = 1) { + database.prepare('INSERT INTO conversations (id, created_at, updated_at) VALUES (?, ?, ?)').run(`task-${runId}`, now, now) + database.prepare('INSERT INTO conversation_branches (id, conversation_id, created_at) VALUES (?, ?, ?)').run(`branch-${runId}`, `task-${runId}`, now) + database.prepare('INSERT INTO messages (id, conversation_id, branch_id, role, content_json, created_at) VALUES (?, ?, ?, \'user\', ?, ?)').run(`input-${runId}`, `task-${runId}`, `branch-${runId}`, '{"text":"Fixture input"}', now) + database.prepare(` + INSERT INTO runs (id, conversation_id, branch_id, triggering_message_id, provider, model, purpose, status, started_at, next_event_sequence) + VALUES (?, ?, ?, ?, 'fixture', 'fixture', 'chat', 'queued', ?, ?) + `).run(runId, `task-${runId}`, `branch-${runId}`, `input-${runId}`, now, nextSequence) +} + +function fixture(databasePath = ':memory:', nextSequence: number | null = 1) { + const database = open(databasePath) + seedRun(database, 'run-1', nextSequence) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + return { database, writer: writer(database), queries: new RunEventQueries(database) } +} + +function temporaryPath() { + const directory = mkdtempSync(join(tmpdir(), 'lexora-sqlite-events-')) + directories.push(directory) + return join(directory, 'candidate.sqlite3') +} + +function event(type: string, payload: unknown = {}, createdAt = now): AppendBuddyRunEventInput { + return { runId: 'run-1', type, payload, createdAt } +} + +function history(): AppendBuddyRunEventInput[] { + return [ + event('run.started'), + event('run.progress', { stage: 'thinking' }), + event('message.delta', { messageId: 'answer-1', text: 'Answer' }), + event('message.completed', { messageId: 'answer-1', role: 'assistant', content: { text: 'Answer' }, stopReason: 'completed' }), + event('tool.preparing', { toolCallId: 'tool-1' }), + event('tool.updated', { toolCallId: 'tool-1', text: 'Working' }), + event('tool.completed', { toolCallId: 'tool-1' }), + event('approval.requested', { + id: 'approval-1', + runId: 'run-1', + toolCallId: 'tool-1', + kind: 'shell', + status: 'pending', + summary: 'Fixture approval', + payload: { card: 'shell', toolName: 'bash', command: 'fixture', allowForTurn: false }, + createdAt: now, + resolvedAt: null, + }), + event('approval.resolved', { id: 'approval-1', status: 'denied', resolvedAt: now }), + event('usage.recorded', { + usageRecordId: 'usage-1', + sourceEntryId: 'source-1', + provider: 'fixture', + model: 'fixture', + purpose: 'turn', + inputTokens: 2, + outputTokens: 3, + cacheReadTokens: 0, + cacheWriteTokens: 0, + reasoningTokens: null, + totalTokens: 5, + inputCost: 0, + outputCost: 0, + cacheReadCost: 0, + cacheWriteCost: 0, + totalCost: 0, + }), + event('future.opaque', { retained: [null, { value: 'unchanged' }] }), + event('run.completed'), + event('run.progress', { stage: 'settled' }), + ] +} + +function projections(database: SqliteDatabase) { + return Object.fromEntries(['runs', 'messages', 'approvals', 'usage_records', 'command_receipts'].map(table => [table, database.prepare(`SELECT * FROM ${table}`).all()])) +} + +function watermark(database: SqliteDatabase) { + return database.prepare('SELECT CAST(next_event_sequence AS TEXT) AS value FROM runs WHERE id = \'run-1\'').get()!.value +} + +function capture(operation: () => unknown): unknown { + try { + operation() + } + catch (error) { + return error + } + throw new Error('Expected operation to fail') +} + +describe('sQLite run event sequence and commit notifications', () => { + it('commits child results, mailbox identity and projections together without publishing private replies', () => { + const f = createExecutionStorageFixture() + databases.push(f.database) + f.executions.admitChild(f.child('child')) + withTransaction(f.database, () => { + f.database.exec(BUDDY_V43_PERSISTENCE_SCHEMA_SQL) + f.database.exec(`PRAGMA user_version = ${BUDDY_MIGRATION_SCHEMA_VERSION}`) + f.database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'; UPDATE runs SET next_event_sequence = 1') + }) + const events = writer(f.database) + const delivered: number[] = [] + events.onDidCommit(event => delivered.push(event.sequence)) + const batch = [ + { runId: 'child', type: 'message.completed', payload: { role: 'assistant', messageId: 'private-answer', content: { text: 'Private result' }, stopReason: 'completed' } }, + { runId: 'child', type: 'execution.mailbox.sent', payload: { kind: 'result', messageId: 'result-child', targetRunId: 'root', targetGeneration: 0, text: 'Research result' } }, + { runId: 'child', type: 'run.completed', payload: {} }, + ] + f.database.exec('CREATE TRIGGER reject_mailbox BEFORE INSERT ON execution_mailbox_sources BEGIN SELECT RAISE(ABORT, \'projection unavailable\'); END') + expect(() => events.appendBatch(batch)).toThrow('projection unavailable') + expect(f.database.prepare('SELECT sequence FROM run_events').all()).toEqual([]) + expect(f.database.prepare('SELECT next_event_sequence FROM runs WHERE id = \'child\'').get()).toEqual({ next_event_sequence: 1 }) + expect(delivered).toEqual([]) + f.database.exec('DROP TRIGGER reject_mailbox') + const committed = events.appendBatch(batch) + const message = executionMailboxMessageSchema.parse(committed[1]!.payload) + expect(message).toMatchObject({ sourceRunId: 'child', sourceSequence: 2 }) + expect(message.payloadHash).toBe(hashExecutionMailboxMessage(message)) + expect(createMailboxRepository(f.database).listPending('root')).toEqual([message]) + expect(f.database.prepare('SELECT id FROM messages WHERE id = \'private-answer\'').get()).toBeUndefined() + expect(f.executions.find('child')?.phase).toBe('closed') + expect(delivered).toEqual([1, 2, 3]) + const receipt = { sourceRunId: message.sourceRunId, messageId: message.messageId, payloadHash: message.payloadHash, contextEntryId: 'entry-result' } + events.appendBatch([ + { runId: 'root', type: 'execution.mailbox.accepted', payload: receipt }, + { runId: 'root', type: 'execution.mailbox.consumed', payload: receipt }, + ]) + expect(createMailboxRepository(f.database).find(message.messageId)?.state).toBe('consumed') + }) + + it('preserves the durable watermark and projections when the highest event is compacted, including after reopen', () => { + const databasePath = temporaryPath() + const f = fixture(databasePath) + f.writer.appendBatch(history()) + const before = projections(f.database) + const delivered: number[] = [] + f.writer.onDidCommit(event => delivered.push(event.sequence)) + expect(f.writer.compactTerminalRun('run-1')).toBe(5) + expect(f.queries.read('run-1').map(event => event.sequence)).toEqual([1, 4, 7, 8, 9, 10, 11, 12]) + expect(f.queries.read('run-1').find(event => event.type === 'future.opaque')?.payload).toEqual({ retained: [null, { value: 'unchanged' }] }) + expect(projections(f.database)).toEqual(before) + expect(watermark(f.database)).toBe('14') + expect(f.writer.compactTerminalRun('run-1')).toBe(0) + expect(delivered).toEqual([]) + f.writer.dispose() + f.database.close() + + const database = open(databasePath) + const reopened = writer(database) + expect(reopened.compactTerminalRun('run-1')).toBe(0) + expect(reopened.append(event('desktop.panel.changed', { action: 'close' })).sequence).toBe(14) + expect(watermark(database)).toBe('15') + expect(new RunEventQueries(database).read('run-1').map(event => event.sequence)).toEqual([1, 4, 7, 8, 9, 10, 11, 12, 14]) + }) + + it('uses sequence order instead of wall time to retain the latest tool replacement', () => { + const f = fixture() + f.writer.appendBatch([ + event('tool.updated', { toolCallId: 'tool-1', text: 'Old' }, '2026-09-29T00:00:02.000Z'), + event('tool.updated', { toolCallId: 'tool-1', text: 'Latest' }, '2026-09-29T00:00:01.000Z'), + event('run.completed'), + ]) + expect(f.writer.compactTerminalRun('run-1')).toBe(1) + expect(f.queries.read('run-1')).toMatchObject([ + { sequence: 2, type: 'tool.updated', payload: { text: 'Latest' } }, + { sequence: 3, type: 'run.completed' }, + ]) + expect(watermark(f.database)).toBe('4') + }) + + it('compacts the entire history without using a latest-page limit', () => { + const f = fixture() + f.writer.appendBatch([...Array.from({ length: 600 }, (_, index) => event('run.progress', { index })), event('run.completed')]) + expect(f.writer.compactTerminalRun('run-1')).toBe(600) + expect(f.queries.read('run-1')).toMatchObject([{ sequence: 601, type: 'run.completed' }]) + expect(f.writer.append(event('desktop.panel.changed')).sequence).toBe(602) + }) + + it.each(['queued', 'running'])('preserves events while the run is %s even if a terminal fact is present', (status) => { + const f = fixture() + f.writer.appendBatch(history()) + f.database.prepare('UPDATE runs SET status = ? WHERE id = \'run-1\'').run(status) + const before = f.queries.read('run-1') + expect(f.writer.compactTerminalRun('run-1')).toBe(0) + expect(f.queries.read('run-1')).toEqual(before) + }) + + it('requires a durable terminal fact before deleting events', () => { + const f = fixture() + f.writer.append(event('run.progress')) + f.database.exec('UPDATE runs SET status = \'completed\' WHERE id = \'run-1\'') + expect(f.writer.compactTerminalRun('run-1')).toBe(0) + expect(f.queries.read('run-1')).toHaveLength(1) + }) + + it('rolls back every deletion when a later compaction row fails', () => { + const f = fixture() + f.writer.appendBatch(history()) + const before = f.queries.read('run-1') + f.database.exec(`CREATE TRIGGER refuse_tail BEFORE DELETE ON run_events WHEN OLD.sequence = 13 + BEGIN SELECT RAISE(ABORT, 'tail retained'); END`) + expect(() => f.writer.compactTerminalRun('run-1')).toThrow('tail retained') + expect(f.queries.read('run-1')).toEqual(before) + expect(watermark(f.database)).toBe('14') + f.database.exec('DROP TRIGGER refuse_tail') + expect(f.writer.compactTerminalRun('run-1')).toBe(5) + }) + + it('allows compaction only after staged events have committed and become observable', () => { + const f = fixture() + f.writer.appendBatch(history()) + const delivered: number[] = [] + f.writer.onDidCommit(event => delivered.push(event.sequence)) + withTransaction(f.database, () => { + f.writer.append(event('run.progress')) + expect(() => f.writer.compactTerminalRun('run-1')).toThrow('active transaction') + expect(delivered).toEqual([]) + }) + expect(delivered).toEqual([14]) + expect(f.queries.read('run-1')).toHaveLength(14) + expect(f.writer.compactTerminalRun('run-1')).toBe(6) + expect(watermark(f.database)).toBe('15') + expect(f.writer.append(event('desktop.panel.changed')).sequence).toBe(15) + }) + + it('refuses unverified and inconsistent sequence metadata before compaction can erase the evidence', () => { + const unverified = fixture(':memory:', null) + unverified.database.exec('UPDATE runs SET status = \'completed\' WHERE id = \'run-1\'') + expect(() => unverified.writer.append(event('audit.recorded'))).toThrow('unverified') + expect(() => unverified.writer.compactTerminalRun('run-1')).toThrow('unverified') + expect(watermark(unverified.database)).toBeNull() + + const inconsistent = fixture() + inconsistent.writer.appendBatch(history()) + inconsistent.database.prepare('INSERT INTO run_events (run_id, sequence, event_type, payload_json, created_at) VALUES (?, 14, ?, ?, ?)').run('run-1', 'run.progress', '{}', now) + expect(() => inconsistent.writer.compactTerminalRun('run-1')).toThrow('inconsistent') + expect(() => inconsistent.writer.append(event('audit.recorded'))).toThrow('inconsistent') + expect(inconsistent.queries.read('run-1')).toHaveLength(14) + expect(watermark(inconsistent.database)).toBe('14') + }) + + it('refuses to append below an existing higher fact even when the next sequence itself is unused', () => { + const f = fixture() + f.database.prepare('INSERT INTO run_events (run_id, sequence, event_type, payload_json, created_at) VALUES (?, 8, ?, ?, ?)').run('run-1', 'audit.original', '{}', now) + expect(() => f.writer.append(event('audit.recorded'))).toThrow('inconsistent') + expect(watermark(f.database)).toBe('1') + expect(f.queries.read('run-1')).toMatchObject([{ sequence: 8, type: 'audit.original' }]) + }) + + it('preserves unsupported event formats and stops maintenance after disposal or loss of authority', () => { + const f = fixture() + f.writer.appendBatch(history()) + f.database.exec('UPDATE run_events SET format_version = 2 WHERE sequence = 2') + const before = f.database.prepare('SELECT * FROM run_events ORDER BY sequence').all() + expect(() => f.writer.compactTerminalRun('run-1')).toThrow('format is not supported') + expect(f.database.prepare('SELECT * FROM run_events ORDER BY sequence').all()).toEqual(before) + f.database.exec('UPDATE storage_profile SET authority = \'legacy_jsonl\', migration_state = \'pending\'') + expect(() => f.writer.compactTerminalRun('run-1')).toThrow('authority is not ready') + f.writer.dispose() + expect(() => f.writer.compactTerminalRun('run-1')).toThrow('writer is stopped') + expect(f.database.prepare('SELECT * FROM run_events ORDER BY sequence').all()).toEqual(before) + }) + + it('allocates exactly representable sequences and rejects a batch that would exhaust the watermark', () => { + const f = fixture() + f.database.prepare('UPDATE runs SET next_event_sequence = ?').run(Number.MAX_SAFE_INTEGER - 1) + expect(() => f.writer.appendBatch([event('audit.first'), event('audit.second')])).toThrow('exhausted') + expect(f.queries.read('run-1')).toEqual([]) + expect(watermark(f.database)).toBe(String(Number.MAX_SAFE_INTEGER - 1)) + expect(f.writer.append(event('audit.last')).sequence).toBe(Number.MAX_SAFE_INTEGER - 1) + expect(watermark(f.database)).toBe(String(Number.MAX_SAFE_INTEGER)) + expect(() => f.writer.append(event('audit.overflow'))).toThrow('exhausted') + f.database.prepare('UPDATE runs SET next_event_sequence = ?').run(9223372036854775807n) + expect(() => f.writer.append(event('audit.int64'))).toThrow('exhausted') + expect(watermark(f.database)).toBe('9223372036854775807') + expect(f.queries.read('run-1')).toHaveLength(1) + }) + + it('allocates from SQLite across independent connections and keeps different runs independent', () => { + const databasePath = temporaryPath() + const f = fixture(databasePath) + const second = open(databasePath) + const other = writer(second) + expect(f.writer.appendBatch([event('audit.first'), event('audit.second')]).map(event => event.sequence)).toEqual([1, 2]) + expect(other.append(event('audit.third')).sequence).toBe(3) + expect(f.writer.append(event('audit.fourth')).sequence).toBe(4) + seedRun(f.database, 'run-2') + expect(other.append({ ...event('audit.independent'), runId: 'run-2' }).sequence).toBe(1) + expect(f.queries.read('run-1').map(event => event.sequence)).toEqual([1, 2, 3, 4]) + expect(watermark(f.database)).toBe('5') + }) + + it('publishes committed batches in FIFO order even when observers append and report failures', async () => { + const databasePath = temporaryPath() + const f = fixture(databasePath) + const reader = new DatabaseSync(databasePath, { readOnly: true }) + databases.push(reader) + const failures: unknown[] = [] + const events = writer(f.database, (error) => { + failures.push(error) + throw new Error('Reporter unavailable') + }) + const first: number[] = [] + const second: number[] = [] + const syncFailure = new Error('Sync observer unavailable') + const asyncFailure = new Error('Async observer unavailable') + events.onDidCommit((committed) => { + expect(f.database.isTransaction).toBe(false) + expect(reader.prepare('SELECT 1 FROM run_events WHERE run_id = ? AND sequence = ?').get(committed.runId, committed.sequence)).toBeDefined() + first.push(committed.sequence) + if (committed.sequence === 1) + events.append(event('audit.reentrant')) + if (committed.sequence === 2) + throw syncFailure + if (committed.sequence === 3) + return Promise.reject(asyncFailure) + }) + events.onDidCommit(committed => second.push(committed.sequence)) + withTransaction(f.database, () => { + events.appendBatch([event('audit.first'), event('audit.second')]) + events.append(event('audit.third')) + expect(first).toEqual([]) + expect(second).toEqual([]) + }) + expect(first).toEqual([1, 2, 3, 4]) + expect(second).toEqual([1, 2, 3, 4]) + await Promise.resolve() + expect(failures).toEqual([syncFailure, asyncFailure]) + expect(watermark(f.database)).toBe('5') + }) + + it('publishes frozen persisted snapshots even when inputs and returned values change before commit', () => { + const f = fixture() + const source = { nested: { value: 'persisted' } } + const delivered: unknown[] = [] + f.writer.onDidCommit(committed => delivered.push(committed)) + withTransaction(f.database, () => { + const result = f.writer.append(event('audit.snapshot', source)) + source.nested.value = 'changed input' + ;(result.payload as typeof source).nested.value = 'changed return value' + expect(delivered).toEqual([]) + }) + const committed = delivered[0] as BuddyRunEvent + expect(committed.payload).toEqual({ nested: { value: 'persisted' } }) + expect(Object.isFrozen(committed)).toBe(true) + expect(Object.isFrozen((committed.payload as typeof source).nested)).toBe(true) + expect(f.queries.read('run-1')[0]).toEqual(committed) + }) + + it('does not advance the sequence or notify when a real COMMIT BUSY rolls back', () => { + const databasePath = temporaryPath() + const f = fixture(databasePath) + f.writer.append(event('audit.first')) + f.database.exec('PRAGMA journal_mode = DELETE; PRAGMA busy_timeout = 0') + const reader = new DatabaseSync(databasePath) + databases.push(reader) + reader.exec('BEGIN; SELECT * FROM run_events') + const delivered: number[] = [] + f.writer.onDidCommit(event => delivered.push(event.sequence)) + const failure = capture(() => f.writer.append(event('audit.busy'))) + expect(failure).toMatchObject({ errcode: 5 }) + expect(readTransactionFailure(failure)).toEqual({ phase: 'commit', outcome: 'rolled_back' }) + expect(watermark(f.database)).toBe('2') + expect(f.queries.read('run-1')).toHaveLength(1) + expect(delivered).toEqual([]) + reader.exec('ROLLBACK') + expect(f.writer.append(event('audit.retry')).sequence).toBe(2) + expect(delivered).toEqual([2]) + }) + + it('retains a committed but unacknowledged sequence across reopen without publishing early success', () => { + const databasePath = temporaryPath() + const f = fixture(databasePath) + f.writer.append(event('audit.first')) + const exec = f.database.exec.bind(f.database) + const injected = vi.spyOn(f.database, 'exec').mockImplementation((sql) => { + exec(sql) + if (sql === 'COMMIT') + throw new Error('COMMIT response lost') + }) + const delivered: number[] = [] + f.writer.onDidCommit(event => delivered.push(event.sequence)) + const failure = capture(() => f.writer.append(event('audit.unacknowledged'))) + expect(readTransactionFailure(failure)).toMatchObject({ phase: 'commit', outcome: 'unknown' }) + expect(delivered).toEqual([]) + expect(() => f.writer.append(event('audit.blocked'))).toThrow('requires verification') + expect(() => f.writer.compactTerminalRun('run-1')).toThrow('requires verification') + injected.mockRestore() + f.writer.dispose() + f.database.close() + const database = open(databasePath) + expect(writer(database).append(event('audit.after-reopen')).sequence).toBe(3) + expect(new RunEventQueries(database).read('run-1').map(event => [event.sequence, event.type])).toEqual([ + [1, 'audit.first'], + [2, 'audit.unacknowledged'], + [3, 'audit.after-reopen'], + ]) + }) +}) diff --git a/apps/buddy/service/src/events/createRunEventLog.ts b/apps/buddy/service/src/events/createRunEventLog.ts index 058e9b4c8..07ac90b82 100644 --- a/apps/buddy/service/src/events/createRunEventLog.ts +++ b/apps/buddy/service/src/events/createRunEventLog.ts @@ -11,6 +11,8 @@ export interface CreateRunEventLogOptions extends RunEventLogCallbacks { } export function createRunEventLog(options: CreateRunEventLogOptions): RunEventLog { + if (options.database.prepare('SELECT 1 FROM sqlite_master WHERE type = \'table\' AND name = \'storage_profile\'').get()) + throw new Error('Legacy JSONL replay cannot open a migration candidate or SQLite authority') const queries = new RunEventQueries(options.database) return new RunEventLog({ onObserverError: options.onObserverError, diff --git a/apps/buddy/service/src/events/prepareEventPayload.ts b/apps/buddy/service/src/events/prepareEventPayload.ts new file mode 100644 index 000000000..27236d934 --- /dev/null +++ b/apps/buddy/service/src/events/prepareEventPayload.ts @@ -0,0 +1,11 @@ +import type { AppendBuddyRunEventInput } from './BuddyRunEvent' +import { executionMailboxDraftSchema, executionMailboxMessageSchema } from '../../../shared/executions/executionApi' +import { hashExecutionMailboxMessage } from '../storage/mailboxRepository' + +export function prepareEventPayload(input: AppendBuddyRunEventInput, sequence: number): unknown { + if (input.type !== 'execution.mailbox.sent') + return input.payload ?? null + const draft = executionMailboxDraftSchema.parse(input.payload) + const message = { ...draft, sourceRunId: input.runId, sourceSequence: sequence } + return executionMailboxMessageSchema.parse({ ...message, payloadHash: hashExecutionMailboxMessage(message) }) +} diff --git a/apps/buddy/service/src/images/createPluginImageBackends.ts b/apps/buddy/service/src/images/createPluginImageBackends.ts index bb23bda2b..edf392103 100644 --- a/apps/buddy/service/src/images/createPluginImageBackends.ts +++ b/apps/buddy/service/src/images/createPluginImageBackends.ts @@ -10,7 +10,7 @@ import { extensionInvocationContext } from '../plugins/extensionInvocationContex import { ImageGenerationError } from './ImageGenerationGateway' import { ImageReadingError, validateReadingImage } from './ImageReadingService' -export function createPluginImageBackends(rpc: RuntimeRpcPeerContract, providers: Pick, gateway: PluginImageGateway): { reading: ImageReadingBackend, generation: ImageGenerationBackend } { +export function createPluginImageBackends(rpc: RuntimeRpcPeerContract, providers: Pick, gateway: Pick): { reading: ImageReadingBackend, generation: ImageGenerationBackend } { const readers = async (signal?: AbortSignal) => extensionImageReaderSchema.array().max(512).parse(await rpc.request(extensionImageReadingRpc.list, {}, 30000, signal)) return { reading: { diff --git a/apps/buddy/service/src/index.ts b/apps/buddy/service/src/index.ts index 67fef7a44..a85bb4d65 100644 --- a/apps/buddy/service/src/index.ts +++ b/apps/buddy/service/src/index.ts @@ -1,220 +1,174 @@ -import type { BuddyServiceFailureCode } from '../../shared/runtime/runtimeProtocol' -import { isAbsolute, join } from 'node:path' +import type { BuddyServiceParentPort } from './rpc/BuddyServiceRpcServer' +import type { ServiceClientRole } from './rpc/ServiceClientConnections' +import { EventEmitter } from 'node:events' import process from 'node:process' import { z } from 'zod' -import { establishWindowsRuntimeGuard } from '../../platform/windows/runtimeGuard' -import { APPLICATION_DIAGNOSTIC_METHOD, readDiagnosticErrorCode } from '../../shared/diagnostics/applicationDiagnostic' -import { ServiceHost } from '../../shared/lifecycle/ServiceHost' -import { SERVICE_LIFECYCLE_METHOD } from '../../shared/lifecycle/serviceLifecycle' -import { ServiceLifecycleSource } from '../../shared/lifecycle/ServiceLifecycleSource' import { ApplicationEvents } from '../../shared/observability/ApplicationEvents' -import { observeLifecycleDiagnostics } from '../../shared/observability/lifecycleDiagnostics' -import { OPERATING_SYSTEM } from '../../shared/platform/identifiers' -import { toPublicRunEvent } from '../../shared/runs/publicRunEvent' -import { runNotifications } from '../../shared/runs/runApi' -import { buddyServiceFailureCodeSchema } from '../../shared/runtime/runtimeProtocol' -import { startBuddyService } from './BuddyService' -import { DiagnosticForwarder } from './diagnostics/DiagnosticForwarder' -import { createRunEventLog } from './events/createRunEventLog' -import { RunEventLogFatalError } from './events/RunEventFailure' -import { startRuntimeNetwork } from './network/runtimeNetwork' -import { - createBuddyService, - notifyBuddyServiceFailure, - notifyBuddyServiceReady, -} from './rpc/BuddyServiceRpcServer' -import { openBuddyDatabase, resolveBuddyDatabasePath } from './storage/database' +import { BUDDY_SERVICE_PROTOCOL_VERSION } from '../../shared/runtime/runtimeProtocol' +import { startLocalServiceSupervisor } from './LocalServiceSupervisor' +import { createRpcRuntimePlatform } from './platform/createRpcRuntimePlatform' +import { nodeServiceConfigSchema, prepareNodeService } from './platform/nodeServiceConfig' +import { BuddyServiceRpcServer, notifyBuddyServiceReady } from './rpc/BuddyServiceRpcServer' +import { registerServiceManagementRpc } from './rpc/registerServiceManagementRpc' +import { readBuddyServiceFailureCode, startServiceRuntime } from './startServiceRuntime' -const parentPort = process.parentPort +const channelId = z.string().min(1).max(128).refine(value => value !== 'host') +const incomingSchema = z.discriminatedUnion('type', [ + z.object({ type: z.literal('start'), protocolVersion: z.literal(BUDDY_SERVICE_PROTOCOL_VERSION), config: nodeServiceConfigSchema }).strict(), + z.object({ type: z.literal('connect'), channel: channelId, role: z.enum(['product', 'management']) }).strict(), + z.object({ type: z.literal('disconnect'), channel: channelId }).strict(), + z.object({ type: z.literal('rpc'), channel: z.string().min(1).max(128), message: z.unknown() }).strict(), +]) -if (!parentPort) { - process.stderr.write('Buddy Local Service requires an Electron utility process parent\n') +if (process.argv[2] === '--config' && process.argv.length === 4) { + const supervisor = startLocalServiceSupervisor(process.argv[3]!) + const stop = () => { + void supervisor.stop().catch(() => { + process.stderr.write('SERVICE_STOP_FAILED\n') + }) + } + process.once('SIGINT', stop) + process.once('SIGTERM', stop) + void supervisor.ready.catch(() => { + process.stderr.write('SERVICE_START_FAILED\n') + process.exitCode = 1 + }) +} +else if (!process.send) { + process.stderr.write('Lexora Server requires an inherited local IPC channel\n') process.exitCode = 1 } else { - process.once('SIGINT', () => {}) - - void runBuddyService().catch((error: unknown) => { - const failureCode = readBuddyServiceFailureCode(error) - process.stderr.write( - `Buddy Local Service failed to start: ${failureCode} (${readErrorName(error)})\n`, - ) - process.exit(1) + const events = new ApplicationEvents() + const channelIds = new Set() + const channels = new Map() + let started: ReturnType | undefined + let runtime: Awaited> | undefined + let stopping: Promise | undefined + const host = createChannel('host') + const timeout = setTimeout(() => { + host.close(new Error('SERVICE_START_TIMEOUT')) + void shutdown(1) + }, 30000) + process.once('disconnect', () => { + host.close(new Error('SERVICE_OWNER_DISCONNECTED')) + void shutdown(1) + }) + process.once('SIGTERM', () => { + void shutdown(0) + }) + process.once('SIGINT', () => { + void shutdown(0) + }) + process.on('message', (raw) => { + try { + const input = incomingSchema.parse(raw) + if (input.type === 'rpc') { + channels.get(input.channel)?.port.emit('message', { data: input.message }) + } + else if (input.type === 'start') { + if (started || stopping) + throw new Error('SERVICE_ALREADY_STARTED') + started = start(input.config) + void started.catch(fail) + } + else if (input.type === 'connect') { + connect(input.channel, input.role) + } + else { + channels.get(input.channel)?.peer.close(new Error('SERVICE_CLIENT_DISCONNECTED')) + } + } + catch (error) { + fail(error) + } }) -} -async function runBuddyService(): Promise { - if (!parentPort) - return + function send(message: unknown) { + if (process.connected) { + process.send!(message as object, (error) => { + if (error) + void shutdown(1) + }) + } + } - const buddyHome = z.string().refine(isAbsolute).parse(process.env.LEXORA_BUDDY_HOME) - let database: ReturnType | null = null - let serviceServer: ReturnType | null = null - const events = new ApplicationEvents() - const diagnostics = new DiagnosticForwarder(event => serviceServer?.notify(APPLICATION_DIAGNOSTIC_METHOD, event)) - events.subscribe(event => diagnostics.record(event)) - const record = events.publish - const lifecycle = new ServiceLifecycleSource(() => record({ event: 'observer.failed', component: 'runtime.lifecycle', level: 'warn' })) - const host = new ServiceHost(lifecycle) - const stopLifecycleDiagnostics = observeLifecycleDiagnostics(host.lifecycle, events) - const lifecycleDelivery = host.lifecycle.onDidChange(change => serviceServer?.notify(SERVICE_LIFECYCLE_METHOD, change)) - let serviceFailureNotified = false - let isDatabaseClosed = false - const closeDatabase = () => { - if (isDatabaseClosed || !database) - return - isDatabaseClosed = true - database.close() + function createChannel(id: string) { + const port = Object.assign(new EventEmitter(), { postMessage: (message: unknown) => send({ type: 'rpc', channel: id, message }) }) + const peer = new BuddyServiceRpcServer({ recordDiagnostic: events.publish, port, onFatalError: () => { + if (id === 'host') + void shutdown(1) + } }) + channelIds.add(id) + channels.set(id, { port, peer }) + peer.onDidClose(() => { + channels.delete(id) + }) + return peer } - let serviceHandle: Awaited> | null = null - let eventLog: ReturnType | null = null - let isShuttingDown = false - const notifyFailure = (code: BuddyServiceFailureCode) => { - if (!serviceServer || serviceFailureNotified) - return - serviceFailureNotified = true - try { - notifyBuddyServiceFailure(serviceServer, code) - } - catch {} + + async function start(config: z.infer) { + const builtinSkillsDirectories = await prepareNodeService(config) + const preferences = structuredClone(config.preferences) + const service = await startServiceRuntime({ + events, + buddyHome: config.buddyHome, + builtinSkillsDirectories, + proxy: config.proxy, + storageAuthority: config.storageAuthority, + platform: createRpcRuntimePlatform(host, { display: config.display, preferences: { get: async () => structuredClone(preferences), subscribe: () => () => {} } }), + notify: (method, params) => host.notify(method, params), + onFatalFailure: fail, + }) + runtime = service + clearTimeout(timeout) + if (!stopping) + send({ type: 'ready', protocolVersion: BUDDY_SERVICE_PROTOCOL_VERSION }) + service.startMaintenance() + return service } - const shutdown = async (exitCode: number) => { - if (isShuttingDown) + + function connect(channel: string, role: ServiceClientRole) { + if (!runtime || stopping || channelIds.has(channel)) { + send({ type: 'rejected', channel, code: !runtime || stopping ? 'SERVICE_NOT_READY' : 'SERVICE_CHANNEL_EXISTS' }) return - isShuttingDown = true - record({ event: 'service.stopping', level: 'info', component: 'local_service' }) - let failed = false + } + const peer = createChannel(channel) try { - await host.stop() + runtime.connectClient(role, peer) + if (role === 'management') { + registerServiceManagementRpc(peer, () => { + void shutdown(0) + }, runtime.prepareIdleShutdown) + } + notifyBuddyServiceReady(peer) } catch { - failed = true + peer.close(new Error('SERVICE_CLIENT_FAILED')) + send({ type: 'rejected', channel, code: 'SERVICE_CLIENT_FAILED' }) } - record({ event: failed ? 'service.stop_failed' : 'service.stopped', level: failed ? 'error' : 'info', component: 'runtime.service' }) - lifecycleDelivery.dispose() - stopLifecycleDiagnostics() - diagnostics.dispose() - serviceServer?.close(new Error('Buddy Local Service is shutting down')) - process.exit(failed ? 1 : exitCode) } - serviceServer = createBuddyService({ - recordDiagnostic: record, - announceReady: false, - port: parentPort, - prepareIdleShutdown: () => serviceHandle?.prepareIdleShutdown() ?? false, - scheduleShutdown() { - void shutdown(0) - }, - onFatalError(error) { - process.stderr.write(`Buddy Local Service protocol failure: ${error.name}\n`) - void shutdown(1) - }, - }) - process.once('exit', closeDatabase) - try { - const builtinSkillsDirectories = await host.step('runtime.guard', async () => { - if (process.platform === OPERATING_SYSTEM.Windows) - await establishWindowsRuntimeGuard() - return z.array(z.string().min(1)).parse(JSON.parse(process.env.LEXORA_BUDDY_SKILLS_DIRS ?? '[]')) - }) - await host.start('runtime.network', ({ defer }) => { - defer(startRuntimeNetwork()) - }) - const openedDatabase = await host.start('runtime.database', ({ defer }) => { - database = openBuddyDatabase({ databasePath: resolveBuddyDatabasePath(buddyHome) }) - defer(closeDatabase) - return database - }) - eventLog = await host.start('runtime.event_log', ({ defer }) => { - const log = createRunEventLog({ - conversationsDirectory: join(buddyHome, 'conversations'), - database: openedDatabase, - onObserverError: error => record({ event: 'run.observer_failed', level: 'warn', errorCode: readDiagnosticErrorCode(error) }), - onMaintenance: (summary) => { - if (!summary.checked && !summary.failed) - return - record({ event: 'run.maintenance.checked', level: 'info', durationMs: summary.durationMs, count: summary.checked }) - record({ event: 'run.maintenance.removed', level: 'info', count: summary.removed }) - if (summary.failed) - record({ event: 'run.maintenance.failed', level: 'warn', count: summary.failed }) - }, - onRecovery: (summary) => { - record({ event: 'run.recovery.enumerated', level: 'info', durationMs: summary.enumerationMs, count: summary.scanned }) - record({ event: 'run.recovery.inspected', level: 'info', durationMs: summary.inspectionMs, count: summary.scanned }) - record({ event: 'run.recovery.read', level: 'info', durationMs: summary.readMs, count: summary.events }) - record({ event: 'run.recovery.projected', level: 'info', durationMs: summary.projectionMs, count: summary.rebuilt }) - record({ event: 'run.recovery.skipped', level: 'info', count: summary.skipped }) - record({ event: 'run.recovery.finished', level: summary.failed ? 'error' : 'info', durationMs: summary.durationMs, count: summary.failed }) - }, - onRunQuarantined: failure => record({ event: 'run.recovery.quarantined', level: 'error', runId: failure.runId, ...(failure.conversationId ? { conversationId: failure.conversationId } : {}), errorCode: failure.code }), - onFatalFailure: (error) => { - record({ event: 'run.storage_failed', level: 'error', runId: error.runId, errorCode: error.code }) - notifyFailure(readBuddyServiceFailureCode(error)) - const operation = 'operation' in error ? ` ${error.operation}` : '' - const stage = 'stage' in error ? ` ${error.stage}` : '' - const range = error.firstSequence === null || error.lastSequence === null - ? error.runId - : `${error.runId}#${error.firstSequence}-${error.lastSequence}` - process.stderr.write( - `Lexora Buddy event log fatal failure: ${error.code} ${error.commitState}${operation}${stage} ${range}\n`, - ) - void shutdown(1) - }, - }) - const delivery = log.onDidCommit((event) => { - try { - serviceServer?.notify(runNotifications.event.method, toPublicRunEvent(event)) - } - catch (error) { - record({ event: 'run.notification_failed', level: 'warn', runId: event.runId, errorCode: readDiagnosticErrorCode(error) }) - } - }) - defer(async () => { - await log.close() - delivery.dispose() - }) - return log - }, ['runtime.database']) - await host.step('runtime.event_replay', ({ reportProgress }) => eventLog!.recoverAll({ - force: process.env.LEXORA_BUDDY_EVENT_RECOVERY === 'full', - onProgress: reportProgress, - })) - await host.start('runtime.services', async ({ defer }) => { - serviceHandle = await startBuddyService({ - buddyHome, - builtinSkillsDirectories, - database: openedDatabase, - eventLog: eventLog!, - rpc: serviceServer, - events, - lifecycle, - }) - defer(() => serviceHandle!.dispose()) - }) - notifyBuddyServiceReady(serviceServer) - serviceHandle!.startMaintenance() - } - catch (error) { - notifyFailure(readBuddyServiceFailureCode(error)) - await host.stop().catch(() => {}) - lifecycleDelivery.dispose() - stopLifecycleDiagnostics() - diagnostics.dispose() - serviceServer.close(new Error('Buddy Local Service startup failed')) - closeDatabase() - throw error - } -} -function readBuddyServiceFailureCode(error: unknown): BuddyServiceFailureCode { - if (error instanceof RunEventLogFatalError) { - const parsed = buddyServiceFailureCodeSchema.safeParse(error.code) - if (parsed.success) - return parsed.data + function fail(error: unknown) { + const code = readBuddyServiceFailureCode(error) + send({ type: 'failed', code }) + process.stderr.write(`Lexora Server failed: ${code} (${error instanceof Error ? error.name : 'unknown'})\n`) + void shutdown(1) } - return 'RUNTIME_START_FAILED' -} -function readErrorName(error: unknown): string { - return error instanceof Error && error.name ? error.name : 'unknown error' + function shutdown(code: number): Promise { + return stopping ??= (async () => { + clearTimeout(timeout) + const service = await started?.catch(() => undefined) + try { + await service?.dispose() + } + catch { + code = 1 + } + for (const { peer } of channels.values()) + peer.close(new Error('SERVICE_STOPPED')) + process.exit(code) + })() + } } diff --git a/apps/buddy/service/src/network/runtimeNetwork.ts b/apps/buddy/service/src/network/runtimeNetwork.ts index 0670502aa..321d37c66 100644 --- a/apps/buddy/service/src/network/runtimeNetwork.ts +++ b/apps/buddy/service/src/network/runtimeNetwork.ts @@ -1,8 +1,58 @@ import { EnvHttpProxyAgent, install, setGlobalDispatcher } from 'undici' -export function startRuntimeNetwork(): () => Promise { - const dispatcher = new EnvHttpProxyAgent({ allowH2: false, proxyTunnel: true }) - setGlobalDispatcher(dispatcher) - install() - return () => dispatcher.destroy() +export interface RuntimeProxy { + httpProxy: string + httpsProxy: string + noProxy: string +} + +export function readRuntimeProxy(environment: Record): RuntimeProxy { + return { + httpProxy: environment.http_proxy ?? environment.HTTP_PROXY ?? '', + httpsProxy: environment.https_proxy ?? environment.HTTPS_PROXY ?? '', + noProxy: environment.no_proxy ?? environment.NO_PROXY ?? '', + } +} + +export class RuntimeNetwork { + #dispatcher: EnvHttpProxyAgent | undefined + #disposed = false + #disposing: Promise | undefined + readonly #retiring = new Set>() + + async replace(proxy: RuntimeProxy): Promise { + if (this.#disposed) + throw new Error('RUNTIME_NETWORK_STOPPED') + const dispatcher = new EnvHttpProxyAgent({ ...proxy, allowH2: false, proxyTunnel: true }) + const previous = this.#dispatcher + this.#dispatcher = dispatcher + setGlobalDispatcher(dispatcher) + install() + await this.#release(previous) + } + + async invalidate(): Promise { + const dispatcher = this.#dispatcher + this.#dispatcher = undefined + await this.#release(dispatcher) + await Promise.all([...this.#retiring]) + } + + dispose(): Promise { + this.#disposed = true + return this.#disposing ??= this.invalidate() + } + + async #release(dispatcher?: EnvHttpProxyAgent): Promise { + if (!dispatcher) + return + const closing = dispatcher.destroy() + this.#retiring.add(closing) + try { + await closing + } + finally { + this.#retiring.delete(closing) + } + } } diff --git a/apps/buddy/service/src/pet/PetActionService.ts b/apps/buddy/service/src/pet/PetActionService.ts index f03ba5845..83f0f9186 100644 --- a/apps/buddy/service/src/pet/PetActionService.ts +++ b/apps/buddy/service/src/pet/PetActionService.ts @@ -1,6 +1,6 @@ import type { PetExecuteSequenceResult } from '../../../shared/runtime/petProtocol' -import type { RuntimeRpcPeerContract } from '../../../shared/runtime/rpcPeer' import type { AppendBuddyRunEventInput } from '../events/BuddyRunEvent' +import type { RuntimePlatform } from '../platform/RuntimePlatform' import type { PetMacroId } from './petMacroCatalog' import { randomUUID } from 'node:crypto' import { Emitter } from '../../../shared/events/Emitter' @@ -12,15 +12,13 @@ import { import { compilePetMacro } from './petMacroCatalog' import { createPetToolPresentation, PET_TOOL_NAME } from './petToolContract' -const PET_HOST_TIMEOUT_MS = 20_000 - export interface PetActionEventSink { (event: AppendBuddyRunEventInput): Promise | unknown } export interface PetActionServiceOptions { eventSink?: PetActionEventSink - peer: Pick + executeSequence: RuntimePlatform['executePetSequence'] } export interface ExecutePetActionInput { @@ -45,11 +43,11 @@ export class PetActionService { #revision = 0 #disposing: Promise | undefined readonly #eventSink?: PetActionEventSink - readonly #peer: Pick + readonly #executeSequence: RuntimePlatform['executePetSequence'] constructor(options: PetActionServiceOptions) { this.#eventSink = options.eventSink - this.#peer = options.peer + this.#executeSequence = options.executeSequence } execute(input: ExecutePetActionInput): Promise { @@ -71,11 +69,7 @@ export class PetActionService { let confirmed: PetExecuteSequenceResult | null = null this.#changes.fire(copyEventSnapshot({ ...identity, phase: 'requested', revision: ++this.#revision })) try { - result = petExecuteSequenceResultSchema.parse(await this.#peer.request( - 'host.pet.executeSequence', - request, - PET_HOST_TIMEOUT_MS, - )) + result = await this.#executeSequence(request) confirmed = result this.#changes.fire(copyEventSnapshot({ ...identity, phase: 'host-confirmed', result, revision: ++this.#revision })) } diff --git a/apps/buddy/service/src/pet/__tests__/PetActionService.spec.ts b/apps/buddy/service/src/pet/__tests__/PetActionService.spec.ts index 01963e2b7..7c2ec64b8 100644 --- a/apps/buddy/service/src/pet/__tests__/PetActionService.spec.ts +++ b/apps/buddy/service/src/pet/__tests__/PetActionService.spec.ts @@ -4,9 +4,9 @@ import type { RuntimeRpcPeerContract } from '../../../../shared/runtime/rpcPeer' import type { PetActionChange } from '../PetActionService' import { describe, expect, it, vi } from 'vitest' import { applicationDiagnosticSchema } from '../../../../shared/diagnostics/applicationDiagnostic' +import { createRpcRuntimePlatform } from '../../platform/createRpcRuntimePlatform' import { observePetActionDiagnostics } from '../observePetActionDiagnostics' import { PetActionService } from '../PetActionService' -import { classifyPetTool, PET_TOOL_NAME } from '../petToolContract' function createPeer(result: unknown): RuntimeRpcPeerContract { return { @@ -24,7 +24,7 @@ describe('petActionService', () => { { status: 'failed', completedSteps: 1, code: 'PET_STEP_FAILED' }, { status: 'interrupted', completedSteps: 1, reasonCode: 'admission.preemptedByHigherPriorityPlan' }, ] satisfies PetExecuteSequenceResult[])('preserves a confirmed $status result in safe diagnostics', async (result) => { - const service = new PetActionService({ peer: createPeer(result), eventSink: () => {} }) + const service = new PetActionService({ executeSequence: sequence(createPeer(result)), eventSink: () => {} }) const facts: PetActionChange[] = [] const diagnostics: ApplicationDiagnostic[] = [] service.onDidChange(change => facts.push(change)) @@ -36,19 +36,22 @@ describe('petActionService', () => { expect(diagnostics[1]!.errorCode).toBe(result.status === 'failed' ? result.code : undefined) expect(new Set(diagnostics.map(event => event.operationId)).size).toBe(1) expect(JSON.stringify(diagnostics)).not.toContain('awaitApproval') - expect(Object.isFrozen(facts[1]) && 'result' in facts[1]! && Object.isFrozen(facts[1]!.result)).toBe(true) await service.dispose() }) it('keeps transport loss unknown even after recording the legacy tool response', async () => { const peer = createPeer(null) vi.mocked(peer.request).mockRejectedValue(new Error('private transport details')) - const service = new PetActionService({ peer, eventSink: () => {} }) + const service = new PetActionService({ executeSequence: sequence(peer), eventSink: () => {} }) const facts: PetActionChange[] = [] const diagnostics: ApplicationDiagnostic[] = [] service.onDidChange(change => facts.push(change)) observePetActionDiagnostics(service, event => diagnostics.push(applicationDiagnosticSchema.parse(event))) - await service.execute({ macro: 'thinking', runId: 'run-1', toolCallId: 'tool-1' }) + await expect(service.execute({ macro: 'thinking', runId: 'run-1', toolCallId: 'tool-1' })).resolves.toEqual({ + code: 'PET_UNAVAILABLE', + completedSteps: 0, + status: 'failed', + }) expect(facts.map(change => change.phase)).toEqual(['requested', 'transport-unknown', 'progress-recorded']) expect(facts.at(-1)).toMatchObject({ phase: 'progress-recorded', result: null }) expect(diagnostics.map(event => event.event)).toEqual(['pet.action.requested', 'pet.action.transport_unknown', 'pet.action.progress_recorded.unknown']) @@ -60,7 +63,7 @@ describe('petActionService', () => { it('preserves host success while propagating a required progress write failure', async () => { const failure = new Error('private writer failure') const result = { status: 'completed', completedSteps: 1 } as const - const service = new PetActionService({ peer: createPeer(result), eventSink: () => { + const service = new PetActionService({ executeSequence: sequence(createPeer(result)), eventSink: () => { throw failure } }) const facts: PetActionChange[] = [] @@ -82,7 +85,7 @@ describe('petActionService', () => { const progressStarted = Promise.withResolvers() const peer = createPeer(null) vi.mocked(peer.request).mockReturnValue(host.promise) - const service = new PetActionService({ peer, eventSink: async () => { + const service = new PetActionService({ executeSequence: sequence(peer), eventSink: async () => { progressStarted.resolve() await progress.promise } }) @@ -105,12 +108,12 @@ describe('petActionService', () => { expect(phases).toEqual(['requested', 'host-confirmed', 'progress-recorded']) }) - it('compiles approval into a bounded primitive and restores idle afterwards', async () => { + it('records a confirmed approval action as a run tool update', async () => { const peer = createPeer({ status: 'completed', completedSteps: 1 }) const events: unknown[] = [] const service = new PetActionService({ eventSink: event => events.push(event), - peer, + executeSequence: sequence(peer), }) await expect(service.execute({ @@ -119,26 +122,6 @@ describe('petActionService', () => { toolCallId: 'tool-1', })).resolves.toEqual({ status: 'completed', completedSteps: 1 }) - expect(peer.request).toHaveBeenCalledWith( - 'host.pet.executeSequence', - expect.objectContaining({ - priority: 30, - requestId: expect.stringMatching(/^pet_/), - steps: [{ - animation: 'approval', - completionBehavior: 'restoreIdle', - interruptPolicy: 'interruptible', - kind: 'playAction', - playback: { - clipDurationMs: 2540, - durationMs: 5000, - kind: 'loopForDuration', - }, - timeoutMs: 6000, - }], - }), - 20_000, - ) expect(events).toEqual([ expect.objectContaining({ runId: 'run-1', @@ -158,23 +141,8 @@ describe('petActionService', () => { }), ]) }) - - it('classifies only the owned tool as a first-party visual action', () => { - expect(classifyPetTool({ toolName: PET_TOOL_NAME })).toEqual({ - access: 'visual', - }) - expect(classifyPetTool({ toolName: 'read' })).toBeNull() - }) - - it('normalizes an unavailable host without leaking its raw error', async () => { - const peer = createPeer(null) - vi.mocked(peer.request).mockRejectedValue(new Error('secret host detail')) - const service = new PetActionService({ peer }) - - await expect(service.execute({ macro: 'thinking' })).resolves.toEqual({ - code: 'PET_UNAVAILABLE', - completedSteps: 0, - status: 'failed', - }) - }) }) + +function sequence(peer: { request: (method: string, params: unknown, timeout?: number | null) => Promise }) { + return createRpcRuntimePlatform({ ...peer, close() {}, notify() {}, onNotification: () => () => {}, onRequest: () => () => {} }).executePetSequence +} diff --git a/apps/buddy/service/src/platform/HostConnectorSecretStore.ts b/apps/buddy/service/src/platform/HostConnectorSecretStore.ts new file mode 100644 index 000000000..698f1bc9d --- /dev/null +++ b/apps/buddy/service/src/platform/HostConnectorSecretStore.ts @@ -0,0 +1,42 @@ +import type { ConnectorCredential } from '../../../shared/connectors/connectorCredentials' +import type { RuntimeRpcPeerContract } from '../../../shared/runtime/rpcPeer' +import type { ConnectorSecretStore } from '../connectors/mcp/McpConnectorService' +import { connectorCredentialSchema } from '../../../shared/connectors/connectorCredentials' +import { credentialMutationResultSchema, credentialReadResultSchema } from '../../../shared/runtime/credentialProtocol' +import { McpConnectorError } from '../connectors/mcp/McpConnectorService' + +export class HostConnectorSecretStore implements ConnectorSecretStore { + readonly #peer: Pick + + constructor(peer: Pick) { + this.#peer = peer + } + + async read(id: string): Promise { + const response = credentialReadResultSchema.parse(await this.#peer.request( + 'host.secrets.read', + { id, namespace: 'connectors' }, + )) + if (!response.ok) + throw new McpConnectorError(response.error.code) + return response.value === null ? null : connectorCredentialSchema.parse(response.value) + } + + async write(id: string, credential: ConnectorCredential): Promise { + const response = credentialMutationResultSchema.parse(await this.#peer.request( + 'host.secrets.write', + { id, namespace: 'connectors', value: connectorCredentialSchema.parse(credential) }, + )) + if (!response.ok) + throw new McpConnectorError(response.error.code) + } + + async delete(id: string): Promise { + const response = credentialMutationResultSchema.parse(await this.#peer.request( + 'host.secrets.delete', + { id, namespace: 'connectors' }, + )) + if (!response.ok) + throw new McpConnectorError(response.error.code) + } +} diff --git a/apps/buddy/service/src/platform/RuntimePlatform.ts b/apps/buddy/service/src/platform/RuntimePlatform.ts new file mode 100644 index 000000000..a33f48178 --- /dev/null +++ b/apps/buddy/service/src/platform/RuntimePlatform.ts @@ -0,0 +1,75 @@ +import type { ContextPanelSource } from '../../../shared/context-panel/contextPanel' +import type { ExtensionCapabilityDescriptor, ExtensionCapabilityImpact } from '../../../shared/extensions/extensionCapabilities' +import type { ExtensionSkillDescriptor } from '../../../shared/extensions/extensionSkills' +import type { ImagePreferences } from '../../../shared/images/imagePreferences' +import type { PetExecuteSequenceParams, PetExecuteSequenceResult } from '../../../shared/runtime/petProtocol' +import type { RuntimePreferences } from '../../../shared/runtime/runtimePreferences' +import type { DesktopOpenIntent, DesktopOpenResult } from '../../../shared/service/localServiceProtocol' +import type { SkillInstallPreview } from '../../../shared/skills/skillApi' +import type { registerAttachmentRpc } from '../attachments/registerAttachmentRpc' +import type { BrowserCapabilityHost } from '../browser/BrowserCapabilityService' +import type { ConnectorSecretStore } from '../connectors/mcp/McpConnectorService' +import type { createPluginImageBackends } from '../images/createPluginImageBackends' +import type { ImageReadingService } from '../images/ImageReadingService' +import type { PluginImageGateway } from '../images/PluginImageGateway' +import type { NetworkPermissionsStore } from '../permissions/NetworkPermissionsClient' +import type { createExtensionResourceCapabilities } from '../plugins/extensionResourceCapabilities' +import type { PluginAgentHost, PluginAuthoringHost } from '../plugins/PluginHost' +import type { registerExtensionConditionRpc } from '../plugins/registerExtensionConditionRpc' +import type { HostCredentialStore } from '../providers/HostCredentialStore' +import type { PluginModelRegistry } from '../providers/PluginModelRegistry' +import type { refreshProviderSpeechModels, registerProviderSpeechRpc } from '../providers/registerProviderSpeechRpc' +import type { ShellSandboxClient } from '../sandbox/ShellSandboxClient' +import type { ExtensionShellRegistry } from '../sandbox/ShellSelection' +import type { WebHostClient } from '../web/WebHostClient' + +export interface RuntimePreferenceSource { + get: () => Promise + subscribe: (apply: (preferences: RuntimePreferences) => void) => () => void +} + +export interface WebSecretStore { + read: () => Promise + write: (key: string | null) => Promise +} + +export interface RuntimePlatform { + openDesktop?: (intent: DesktopOpenIntent, signal?: AbortSignal) => Promise + privateNetworkSecrets?: { read: (id: string) => Promise, write: (id: string, value: unknown) => Promise } + createCredentials: () => Pick + connectorSecrets: ConnectorSecretStore + webSecrets: WebSecretStore + browser: BrowserCapabilityHost + createWeb: () => Omit + createSandbox: () => Pick + createPluginImages: () => Pick + createPluginImageBackends: (...args: [Parameters[1], Parameters[2]]) => ReturnType + createImageReading: (options: Omit) => Pick + createPluginModels: (providers: ConstructorParameters[1], failed: () => void) => Pick + createExtensionResources: (artifacts: Parameters[0]['artifacts'], task: Parameters[1], scope: Parameters[2]) => ReturnType + imagePreferences: (signal?: AbortSignal) => Promise + shells: Pick + networkPermissions: NetworkPermissionsStore + skills: { list: () => Promise, subscribe: (changed: () => void) => () => void } + capabilities: { + list: () => Promise + subscribe: (changed: () => void) => () => void + bind: (handlers: { impact: (extensionId: string) => Promise, apply: (descriptors: ExtensionCapabilityDescriptor[], clearExtensionId: string | undefined, signal?: AbortSignal) => Promise }) => () => void + } + subscribeShellChanges: (changed: () => void) => () => void + subscribeSandboxReady: (ready: () => void) => () => void + reviewSkill: (preview: SkillInstallPreview) => void + refreshProviderSpeech: (options: Omit[0], 'rpc'>) => Promise + plugins: PluginAgentHost + authoring: PluginAuthoringHost + preferences: RuntimePreferenceSource + openExternal: (url: string) => Promise + presentBrowser?: (source: ContextPanelSource) => Promise + executePetSequence: (input: PetExecuteSequenceParams) => Promise + subscribeWake: (wake: () => void) => () => void + bindInternal: (services: { + attachments: Parameters[0]['service'] + conditions: Omit[0], 'rpc'> + speech: Omit[0], 'rpc'> + }) => () => void +} diff --git a/apps/buddy/service/src/platform/RuntimePlatformLifecycle.ts b/apps/buddy/service/src/platform/RuntimePlatformLifecycle.ts new file mode 100644 index 000000000..b54ef8776 --- /dev/null +++ b/apps/buddy/service/src/platform/RuntimePlatformLifecycle.ts @@ -0,0 +1,58 @@ +import type { ApplicationDiagnosticReporter } from '../../../shared/diagnostics/applicationDiagnostic' + +export interface RuntimePlatformRecovery { + pauseContinuations: () => void + incompleteRunIds: () => readonly string[] + cancelRun: (id: string, reason: string) => Promise + suspendConnectors: () => Promise + resumeConnectors: () => void + invalidateSessions: () => Promise + isStopping: () => boolean +} + +export class RuntimePlatformLifecycle { + readonly #record: ApplicationDiagnosticReporter + #failure: string | null = null + #cleanup: Promise | undefined + + constructor(record: ApplicationDiagnosticReporter) { + this.#record = record + } + + get available(): boolean { return this.#failure === null } + + assertAvailable(): void { + if (this.#failure) + throw Object.assign(new Error(this.#failure), { code: this.#failure }) + } + + suspend(recovery: RuntimePlatformRecovery, reason = 'PLATFORM_HOST_UNAVAILABLE'): Promise { + this.#failure = reason + if (this.#cleanup) + return this.#cleanup + recovery.pauseContinuations() + const affected = recovery.incompleteRunIds() + this.#cleanup = (async () => { + const results = await Promise.allSettled([ + ...affected.map(id => recovery.cancelRun(id, reason)), + recovery.suspendConnectors(), + ]) + const sessions = await Promise.allSettled([recovery.invalidateSessions()]) + const failures = [...results, ...sessions].filter(result => result.status === 'rejected').map(result => result.reason) + if (failures.length) + throw new AggregateError(failures, 'PLATFORM_CLEANUP_INCOMPLETE') + this.#record({ event: 'platform.execution_suspended', component: 'runtime.platform', level: 'warn', errorCode: reason, count: affected.length }) + })() + return this.#cleanup + } + + async resume(recovery: RuntimePlatformRecovery): Promise { + if (recovery.isStopping()) + throw new Error('RUNTIME_UNAVAILABLE') + await this.#cleanup + recovery.resumeConnectors() + this.#failure = null + this.#cleanup = undefined + this.#record({ event: 'platform.execution_available', component: 'runtime.platform', level: 'info' }) + } +} diff --git a/apps/buddy/service/src/platform/__tests__/runtimePlatform.spec.ts b/apps/buddy/service/src/platform/__tests__/runtimePlatform.spec.ts new file mode 100644 index 000000000..a8b741adf --- /dev/null +++ b/apps/buddy/service/src/platform/__tests__/runtimePlatform.spec.ts @@ -0,0 +1,27 @@ +import type { RuntimePreferences } from '../../../../shared/runtime/runtimePreferences' +import { describe, expect, it } from 'vitest' +import { DEFAULT_RUNTIME_PREFERENCES } from '../../../../shared/runtime/runtimePreferences' +import { bindRuntimePreferences } from '../../agent/sessions/bindRuntimePreferences' + +describe('runtime platform capabilities', () => { + it('keeps a preference update that overtakes its initial snapshot and releases the subscription', async () => { + const snapshot = Promise.withResolvers() + const applied: unknown[] = [] + let listening = false + const binding = bindRuntimePreferences({ + get: () => snapshot.promise, + subscribe(apply) { + listening = true + apply({ ...DEFAULT_RUNTIME_PREFERENCES, cacheWarming: 'streaming', modelRetryLimit: 'unlimited' }) + return () => { + listening = false + } + }, + }, value => applied.push(value)) + snapshot.resolve({ ...DEFAULT_RUNTIME_PREFERENCES, cacheWarming: 'off', modelRetryLimit: 0 }) + const dispose = await binding + expect(applied).toEqual([{ ...DEFAULT_RUNTIME_PREFERENCES, cacheWarming: 'streaming', modelRetryLimit: 'unlimited' }]) + dispose() + expect(listening).toBe(false) + }) +}) diff --git a/apps/buddy/service/src/platform/createRpcRuntimePlatform.ts b/apps/buddy/service/src/platform/createRpcRuntimePlatform.ts new file mode 100644 index 000000000..19b64e64c --- /dev/null +++ b/apps/buddy/service/src/platform/createRpcRuntimePlatform.ts @@ -0,0 +1,179 @@ +import type { RuntimeRpcPeerContract } from '../../../shared/runtime/rpcPeer' +import type { RuntimePlatform, RuntimePreferenceSource } from './RuntimePlatform' +import { z } from 'zod' +import { automationNotifications } from '../../../shared/automation/automationApi' +import { contextPanelRpc, contextPanelStateSchema } from '../../../shared/context-panel/contextPanel' +import { extensionAgentCatalogSchema, extensionAgentDescriptorSchema, extensionAgentRequestSchema, extensionAgentRpc } from '../../../shared/extensions/extensionAgent' +import { extensionJsonSchema } from '../../../shared/extensions/extensionApi' +import { EXTENSION_CAPABILITIES_RPC, EXTENSION_IDENTITY_RPC, EXTENSION_INSPECT_RPC, EXTENSION_PACKAGE_TRANSFER_RPC, EXTENSION_REVIEW_REQUEST, extensionCapabilitiesSchema, extensionIdentitySchema, extensionInspectionSchema } from '../../../shared/extensions/extensionAuthoring' +import { extensionCapabilitiesRpc, extensionCapabilityApplyRpc, extensionCapabilityDescriptorSchema, extensionCapabilityImpactRpc } from '../../../shared/extensions/extensionCapabilities' +import { extensionIdSchema } from '../../../shared/extensions/extensionManifest' +import { extensionShellRpc } from '../../../shared/extensions/extensionShell' +import { extensionSkillDescriptorSchema, extensionSkillsRpc } from '../../../shared/extensions/extensionSkills' +import { imagePreferencesSchema, imageRpc } from '../../../shared/images/imagePreferences' +import { credentialMutationResultSchema, credentialReadResultSchema, openExternalResultSchema } from '../../../shared/runtime/credentialProtocol' +import { petExecuteSequenceResultSchema } from '../../../shared/runtime/petProtocol' +import { runtimePreferencesRpc, runtimePreferencesSchema } from '../../../shared/runtime/runtimePreferences' +import { desktopOpenResultSchema } from '../../../shared/service/localServiceProtocol' +import { skillReviewRequested } from '../../../shared/skills/skillApi' +import { registerAttachmentRpc } from '../attachments/registerAttachmentRpc' +import { BrowserHostClient } from '../browser/BrowserHostClient' +import { createPluginImageBackends } from '../images/createPluginImageBackends' +import { ImageReadingService } from '../images/ImageReadingService' +import { PluginImageGateway } from '../images/PluginImageGateway' +import { NetworkPermissionsClient } from '../permissions/NetworkPermissionsClient' +import { createExtensionResourceCapabilities } from '../plugins/extensionResourceCapabilities' +import { registerExtensionConditionRpc } from '../plugins/registerExtensionConditionRpc' +import { HostCredentialStore, HostCredentialStoreError } from '../providers/HostCredentialStore' +import { PluginModelRegistry } from '../providers/PluginModelRegistry' +import { refreshProviderSpeechModels, registerProviderSpeechRpc } from '../providers/registerProviderSpeechRpc' +import { registerRuntimeRequest } from '../rpc/runtimeRequest' +import { ShellSandboxClient } from '../sandbox/ShellSandboxClient' +import { ExtensionShellRegistry } from '../sandbox/ShellSelection' +import { WebHostClient } from '../web/WebHostClient' +import { HostConnectorSecretStore } from './HostConnectorSecretStore' + +export function createRpcRuntimePlatform(peer: RuntimeRpcPeerContract, options: { + display?: boolean + preferences?: RuntimePreferenceSource +} = {}): RuntimePlatform { + const display = options.display ?? true + const subscribe = (method: string, changed: () => void) => peer.onNotification((event) => { + if (event === method) + changed() + }) + return { + openDesktop: async (intent, signal) => desktopOpenResultSchema.parse(await peer.request('host.openDesktop', intent, 25000, signal)), + privateNetworkSecrets: { + async read(id) { + const result = credentialReadResultSchema.parse(await peer.request('host.secrets.read', { namespace: 'private-network', id })) + if (!result.ok) + throw new HostCredentialStoreError(result.error.code) + return result.value + }, + async write(id, value) { + const result = credentialMutationResultSchema.parse(await peer.request('host.secrets.write', { namespace: 'private-network', id, value })) + if (!result.ok) + throw new HostCredentialStoreError(result.error.code) + }, + }, + createCredentials: () => new HostCredentialStore(peer), + connectorSecrets: new HostConnectorSecretStore(peer), + webSecrets: { + async read() { + const result = credentialReadResultSchema.parse(await peer.request('host.secrets.read', { namespace: 'web', id: 'tavily' })) + if (!result.ok) + throw new HostCredentialStoreError(result.error.code) + return typeof result.value === 'string' && result.value ? result.value : null + }, + async write(key) { + const result = credentialMutationResultSchema.parse(await peer.request(key === null ? 'host.secrets.delete' : 'host.secrets.write', { namespace: 'web', id: 'tavily', ...(key === null ? {} : { value: key }) })) + if (!result.ok) + throw new HostCredentialStoreError(result.error.code) + }, + }, + browser: new BrowserHostClient(peer), + createWeb: () => new WebHostClient(peer), + createSandbox: () => new ShellSandboxClient(peer), + createPluginImages: () => new PluginImageGateway(peer), + createPluginImageBackends: (providers, gateway) => createPluginImageBackends(peer, providers, gateway), + createImageReading: options => new ImageReadingService({ ...options, rpc: peer }), + createPluginModels: (providers, failed) => new PluginModelRegistry(peer, providers, failed), + createExtensionResources: (artifacts, task, scope) => createExtensionResourceCapabilities({ rpc: peer, artifacts }, task, scope), + imagePreferences: async signal => imagePreferencesSchema.parse(await peer.request(imageRpc.preferences, {}, 30000, signal)), + shells: new ExtensionShellRegistry(peer), + networkPermissions: new NetworkPermissionsClient(peer), + skills: { + list: async () => extensionSkillDescriptorSchema.array().parse(await peer.request(extensionSkillsRpc.list, {})), + subscribe: changed => subscribe(extensionSkillsRpc.changed, changed), + }, + capabilities: { + list: async () => extensionCapabilityDescriptorSchema.array().parse(await peer.request(extensionCapabilitiesRpc.list, {})), + subscribe: changed => subscribe(extensionCapabilitiesRpc.changed, changed), + bind(handlers) { + const impact = registerRuntimeRequest(peer, extensionCapabilityImpactRpc, input => handlers.impact(input.extensionId)) + const apply = registerRuntimeRequest(peer, extensionCapabilityApplyRpc, async (input, signal) => { + await handlers.apply(input.descriptors, input.clearExtensionId, signal) + return null + }) + return () => { + impact() + apply() + } + }, + }, + subscribeShellChanges: changed => subscribe(extensionShellRpc.changed, changed), + subscribeSandboxReady: ready => subscribe('host.sandbox.ready', ready), + reviewSkill(preview) { + if (!display) + throw new Error('DESKTOP_UNAVAILABLE') + peer.notify(skillReviewRequested.method, preview) + }, + refreshProviderSpeech: options => refreshProviderSpeechModels({ ...options, rpc: peer }), + plugins: { + releaseRun: async (runId) => { await peer.request(extensionAgentRpc.releaseRun, { runId }) }, + list: async signal => z.array(extensionAgentDescriptorSchema.extend({ id: extensionIdSchema })).parse(await peer.request(extensionAgentRpc.list, {}, 10000, signal)), + invoke: async (input, signal) => extensionJsonSchema.parse(await peer.request(extensionAgentRpc.invoke, input, null, signal)), + subscribe: apply => peer.onNotification((method, params) => { + if (method === extensionAgentRpc.changed) + apply(extensionAgentCatalogSchema.parse(params)) + }), + bindInference(handlers) { + const acquire = peer.onRequest('extensions.inference.acquire', (raw, signal) => handlers.acquire(z.object({ extensionId: extensionIdSchema, invocationId: z.uuid(), parentInvocationId: z.uuid().optional() }).strict().parse(raw), signal)) + const release = peer.onRequest('extensions.inference.release', raw => handlers.release(z.object({ invocationId: z.uuid() }).strict().parse(raw).invocationId)) + return () => { + acquire() + release() + } + }, + bind: handler => peer.onRequest(extensionAgentRpc.request, (input, signal) => handler(extensionAgentRequestSchema.parse(input), signal)), + }, + authoring: { + transfer: (input, signal) => peer.request(EXTENSION_PACKAGE_TRANSFER_RPC, input, input.action === 'build' ? 45000 : 10000, signal), + review(path) { + if (!display) + throw new Error('DESKTOP_UNAVAILABLE') + peer.notify(EXTENSION_REVIEW_REQUEST, { path }) + }, + identity: async (input, signal) => extensionIdentitySchema.parse(await peer.request(EXTENSION_IDENTITY_RPC, input, 10000, signal)), + capabilities: async (input, signal) => extensionCapabilitiesSchema.parse(await peer.request(EXTENSION_CAPABILITIES_RPC, input, 10000, signal)), + inspect: async (id, signal) => extensionInspectionSchema.parse(await peer.request(EXTENSION_INSPECT_RPC, { id }, 10000, signal)), + }, + preferences: options.preferences ?? { + get: async () => runtimePreferencesSchema.parse(await peer.request(runtimePreferencesRpc.get, {})), + subscribe: apply => peer.onNotification((method, params) => { + if (method === runtimePreferencesRpc.changed) + apply(runtimePreferencesSchema.parse(params)) + }), + }, + async openExternal(url) { + if (!display) + throw new Error('DESKTOP_UNAVAILABLE') + const result = openExternalResultSchema.parse(await peer.request('host.openExternal', { url })) + if (!result.ok) + throw new Error('DESKTOP_UNAVAILABLE') + }, + presentBrowser: display ? async (source) => { contextPanelStateSchema.parse(await peer.request(contextPanelRpc.presentBrowser, source)) } : undefined, + executePetSequence: async input => display + ? petExecuteSequenceResultSchema.parse(await peer.request('host.pet.executeSequence', input, 20000)) + : { code: 'PET_UNAVAILABLE', completedSteps: 0, status: 'failed' }, + subscribeWake: wake => peer.onNotification((method, params) => { + if (method === automationNotifications.wake.method && automationNotifications.wake.params.safeParse(params).success) + wake() + }), + bindInternal(services) { + const registrations: (() => void)[] = [] + const dispose = () => registrations.splice(0).reverse().forEach(unregister => unregister()) + try { + registrations.push(registerExtensionConditionRpc({ rpc: peer, ...services.conditions })) + registrations.push(registerAttachmentRpc({ rpc: peer, service: services.attachments })) + registrations.push(registerProviderSpeechRpc({ rpc: peer, ...services.speech })) + return dispose + } + catch (error) { + dispose() + throw error + } + }, + } +} diff --git a/apps/buddy/service/src/platform/nodeServiceConfig.ts b/apps/buddy/service/src/platform/nodeServiceConfig.ts new file mode 100644 index 000000000..df75da46b --- /dev/null +++ b/apps/buddy/service/src/platform/nodeServiceConfig.ts @@ -0,0 +1,50 @@ +import { access } from 'node:fs/promises' +import { isAbsolute, join } from 'node:path' +import process from 'node:process' +import { z } from 'zod' +import { createBuddyNativeEnvironment } from '../../../platform/native/nativeHost' +import searchTools from '../../../platform/native/searchTools.json' +import { resolveBuddyTarget } from '../../../platform/target' +import { establishWindowsRuntimeGuard } from '../../../platform/windows/runtimeGuard' +import { BUDDY_BUILTIN_SKILLS, BUDDY_FEATURES, resolveBuddyPlatform } from '../../../shared/platform' +import { runtimePreferencesSchema } from '../../../shared/runtime/runtimePreferences' +import { prepareRuntimeEnvironment } from './prepareRuntimeEnvironment' + +const absolutePath = z.string().min(1).refine(isAbsolute) +export const nodeServiceConfigSchema = z.object({ + profile: z.enum(['stable', 'development', 'test']), + buddyHome: absolutePath, + resourcesRoot: absolutePath, + proxy: z.object({ httpProxy: z.string(), httpsProxy: z.string(), noProxy: z.string() }).strict(), + preferences: runtimePreferencesSchema, + display: z.boolean(), + storageAuthority: z.enum(['legacy_jsonl', 'sqlite']).optional(), +}).strict() +export type NodeServiceConfig = z.infer + +export async function prepareNodeService(config: NodeServiceConfig): Promise { + const target = resolveBuddyTarget(process.platform, process.arch) + const native = createBuddyNativeEnvironment({ appPath: config.resourcesRoot, resourcesPath: config.resourcesRoot, isPackaged: true }) + const searchDirectory = join(config.resourcesRoot, 'search-tools') + const platform = resolveBuddyPlatform(process.platform) + const skills = [...BUDDY_BUILTIN_SKILLS, ...platform.features.flatMap(id => BUDDY_FEATURES[id].skills)] + .map(name => join(config.resourcesRoot, 'skills', name)) + for (const file of [...Object.values(native), ...Object.values(searchTools.tools).map(tool => join(searchDirectory, tool.targets[target.id].binary)), ...skills.map(directory => join(directory, 'SKILL.md'))]) { + try { + await access(file) + } + catch { + throw new Error(`SERVICE_RESOURCE_MISSING: ${file}`) + } + } + Object.assign(process.env, native, { + LEXORA_BUDDY_HOME: config.buddyHome, + LEXORA_BUDDY_PROFILE: config.profile, + PI_CODING_AGENT_DIR: join(config.buddyHome, 'agent'), + PI_TOOLS_DIR: searchDirectory, + }) + if (process.platform === 'win32') + await establishWindowsRuntimeGuard() + await prepareRuntimeEnvironment() + return skills +} diff --git a/apps/buddy/service/src/platform/prepareRuntimeEnvironment.ts b/apps/buddy/service/src/platform/prepareRuntimeEnvironment.ts new file mode 100644 index 000000000..e8d2fa132 --- /dev/null +++ b/apps/buddy/service/src/platform/prepareRuntimeEnvironment.ts @@ -0,0 +1,10 @@ +import process from 'node:process' +import { currentPlatform } from '../../../platform/currentPlatform' +import { resolveWindowsPowerShell } from '../../../platform/windows/powerShell' +import { clearAmbientProviderCredentials } from '../providers/providerEnvironment' + +export async function prepareRuntimeEnvironment(): Promise { + clearAmbientProviderCredentials(process.env) + if (currentPlatform.shell === 'powershell') + process.env.PI_POWERSHELL_PATH ??= await resolveWindowsPowerShell() +} diff --git a/apps/buddy/service/src/plugins/ExtensionAgentRuntime.ts b/apps/buddy/service/src/plugins/ExtensionAgentRuntime.ts index c9fd6c008..7b016c561 100644 --- a/apps/buddy/service/src/plugins/ExtensionAgentRuntime.ts +++ b/apps/buddy/service/src/plugins/ExtensionAgentRuntime.ts @@ -2,22 +2,20 @@ import type { ListenerErrorHandler } from '../../../shared/events/Emitter' import type { EventSnapshot } from '../../../shared/events/eventTypes' import type { ExtensionAgentCatalog, ExtensionAgentDescriptor, ExtensionAgentInvocation } from '../../../shared/extensions/extensionAgent' import type { ExtensionAgentMethod } from '../../../shared/extensions/extensionAgentCapabilities' -import type { RuntimeRpcPeerContract } from '../../../shared/runtime/rpcPeer' import type { JsonValue } from '../../../shared/workbench/workbenchState' import type { BuddyCapability, BuddyCapabilityContext } from '../agent/extensions/BuddyCapability' import type { ExtensionAgentChange, ExtensionAgentFact, ExtensionCapabilityProjection } from './ExtensionAgentEvents' import type { ExtensionAgentHandlers, ExtensionInvocationScope } from './extensionAgentHandlers' +import type { PluginAgentHost } from './PluginHost' import { createHash, randomUUID } from 'node:crypto' import { defineTool } from '@earendil-works/pi-coding-agent' import { Check } from 'typebox/value' import { z } from 'zod' import { Emitter } from '../../../shared/events/Emitter' import { copyEventSnapshot } from '../../../shared/events/eventSnapshot' -import { extensionAgentCatalogSchema, extensionAgentDescriptorSchema, extensionAgentRequestSchema, extensionAgentRpc } from '../../../shared/extensions/extensionAgent' +import { extensionAgentRequestSchema } from '../../../shared/extensions/extensionAgent' import { extensionAgentCapabilities } from '../../../shared/extensions/extensionAgentCapabilities' -import { extensionJsonSchema } from '../../../shared/extensions/extensionApi' import { ExtensionInvocationQueue } from '../../../shared/extensions/ExtensionInvocationQueue' -import { extensionIdSchema } from '../../../shared/extensions/extensionManifest' import { extensionToolInputSchema } from '../../../shared/extensions/extensionToolParameters' import { extensionInvocationContext } from './extensionInvocationContext' import { extensionToolSchema } from './extensionToolSchema' @@ -37,7 +35,7 @@ interface ProjectionState { release: () => void } interface ExtensionAgentRuntimeOptions { - rpc: RuntimeRpcPeerContract + host: PluginAgentHost createHandlers: (scope: ExtensionInvocationScope) => ExtensionAgentHandlers onObserverError?: ListenerErrorHandler } @@ -67,9 +65,8 @@ export class ExtensionAgentRuntime { } bind(): () => void { - const request = this.#options.rpc.onRequest(extensionAgentRpc.request, (input, signal) => this.#request(input, signal)) - const acquire = this.#options.rpc.onRequest('extensions.inference.acquire', async (raw, signal) => { - const input = z.object({ extensionId: extensionIdSchema, invocationId: z.uuid(), parentInvocationId: z.uuid().optional() }).strict().parse(raw) + const request = this.#options.host.bind((input, signal) => this.#request(input, signal)) + const inference = this.#options.host.bindInference({ acquire: async (input, signal) => { const abort = AbortSignal.any([this.#stop.signal, ...signal ? [signal] : []]) const parent = input.parentInvocationId ? this.#invocations.get(input.parentInvocationId) : undefined if (input.parentInvocationId && (!parent?.accepting || parent.scope.signal.aborted)) @@ -83,28 +80,22 @@ export class ExtensionAgentRuntime { } this.#inferencePermits.set(input.invocationId, release) return null - }) - const release = this.#options.rpc.onRequest('extensions.inference.release', (raw) => { - const { invocationId } = z.object({ invocationId: z.uuid() }).strict().parse(raw) + }, release: (invocationId) => { this.#inferencePermits.get(invocationId)?.() this.#inferencePermits.delete(invocationId) return null - }) - const changed = this.#options.rpc.onNotification((method, params) => { - if (method === extensionAgentRpc.changed) - this.#setCatalog(extensionAgentCatalogSchema.parse(params)) - }) + } }) + const changed = this.#options.host.subscribe(catalog => this.#setCatalog(catalog)) return () => { request() - acquire() - release() + inference() changed() } } async descriptors(signal: AbortSignal): Promise { signal.throwIfAborted() - return z.array(extensionAgentDescriptorSchema.extend({ id: extensionIdSchema })).parse(await this.#options.rpc.request(extensionAgentRpc.list, {}, 10000, AbortSignal.any([signal, this.#stop.signal]))) + return this.#options.host.list(AbortSignal.any([signal, this.#stop.signal])) } async invoke(descriptor: ExtensionAgentDescriptor, request: InvocationRequest, scope: Omit, signal: AbortSignal, invocationId = randomUUID()): Promise { @@ -134,7 +125,7 @@ export class ExtensionAgentRuntime { this.#publish({ kind: 'invocation', stage: 'started', ...identity }) try { const input: ExtensionAgentInvocation = { extensionId: descriptor.id, revision: descriptor.revision, configurationRevision: descriptor.configurationRevision, invocationId, context: { taskId: scope.conversationId, runId: scope.runId }, ...request } - const result = extensionJsonSchema.parse(await this.#options.rpc.request(extensionAgentRpc.invoke, input, null, abort)) + const result = await this.#options.host.invoke(input, abort) abort.throwIfAborted() return result } @@ -180,6 +171,10 @@ export class ExtensionAgentRuntime { } } + async releaseRun(runId: string): Promise { + await this.#options.host.releaseRun?.(runId) + } + dispose(): Promise { if (this.#disposing) return this.#disposing diff --git a/apps/buddy/service/src/plugins/PluginAuthoringService.ts b/apps/buddy/service/src/plugins/PluginAuthoringService.ts index af1089c20..1c62ba433 100644 --- a/apps/buddy/service/src/plugins/PluginAuthoringService.ts +++ b/apps/buddy/service/src/plugins/PluginAuthoringService.ts @@ -1,6 +1,6 @@ import type { ListenerErrorHandler } from '../../../shared/events/Emitter' -import type { RuntimeRpcPeerContract } from '../../../shared/runtime/rpcPeer' import type { BuddyCapabilityContext } from '../agent/extensions/BuddyCapability' +import type { PluginAuthoringHost } from './PluginHost' import { Buffer } from 'node:buffer' import { createHash } from 'node:crypto' import { link, mkdtemp, open, rm } from 'node:fs/promises' @@ -10,7 +10,7 @@ import { readExtensionDirectory, sha256 } from '../../../platform/extensions/ext import { containsCanonicalPath } from '../../../platform/filesystem/filePaths' import { Emitter } from '../../../shared/events/Emitter' import { copyEventSnapshot } from '../../../shared/events/eventSnapshot' -import { EXTENSION_PACKAGE_TRANSFER_RPC, EXTENSION_REVIEW_REQUEST, extensionTransferChunkSchema, extensionTransferOffsetSchema, extensionTransferredBuildResultSchema, extensionTransferSessionSchema } from '../../../shared/extensions/extensionAuthoring' +import { extensionTransferChunkSchema, extensionTransferOffsetSchema, extensionTransferredBuildResultSchema, extensionTransferSessionSchema } from '../../../shared/extensions/extensionAuthoring' import { EXTENSION_PACKAGE_CHUNK_BYTES, validateExtensionArchiveSize } from '../../../shared/extensions/extensionPackageBudget' import { resolveGrantedPath } from '../directories/resolveGrantedPath' @@ -25,14 +25,14 @@ export interface PluginAuthoringChange { } export class PluginAuthoringService { - readonly #peer: Pick + readonly #peer: Pick readonly #changes: Emitter readonly onDidChange readonly #pending = new Set & { ok: boolean }>>() readonly #stop = new AbortController() #disposing: Promise | undefined - constructor(peer: Pick, onObserverError: ListenerErrorHandler = () => console.error('PLUGIN_AUTHORING_OBSERVER_FAILED')) { + constructor(peer: Pick, onObserverError: ListenerErrorHandler = () => console.error('PLUGIN_AUTHORING_OBSERVER_FAILED')) { this.#peer = peer this.#changes = new Emitter(onObserverError) this.onDidChange = this.#changes.event @@ -74,15 +74,15 @@ export class PluginAuthoringService { const archive = zipSync(Object.fromEntries(files), { level: 6 }) files.clear() validateExtensionArchiveSize(archive.byteLength) - transferId = extensionTransferSessionSchema.parse(await this.#peer.request(EXTENSION_PACKAGE_TRANSFER_RPC, { action: 'begin', size: archive.byteLength, sha256: sha256(archive) }, 10000, abort)).transferId + transferId = extensionTransferSessionSchema.parse(await this.#peer.transfer({ action: 'begin', size: archive.byteLength, sha256: sha256(archive) }, abort)).transferId for (let offset = 0; offset < archive.byteLength; offset += EXTENSION_PACKAGE_CHUNK_BYTES) { abort.throwIfAborted() const chunk = archive.subarray(offset, offset + EXTENSION_PACKAGE_CHUNK_BYTES) - const uploaded = extensionTransferOffsetSchema.parse(await this.#peer.request(EXTENSION_PACKAGE_TRANSFER_RPC, { action: 'upload', transferId, offset, data: Buffer.from(chunk).toString('base64') }, 10000, abort)) + const uploaded = extensionTransferOffsetSchema.parse(await this.#peer.transfer({ action: 'upload', transferId, offset, data: Buffer.from(chunk).toString('base64') }, abort)) if (uploaded.offset !== offset + chunk.byteLength) throw new Error('EXTENSION_TRANSFER_ORDER') } - const result = extensionTransferredBuildResultSchema.parse(await this.#peer.request(EXTENSION_PACKAGE_TRANSFER_RPC, { action: 'build', transferId }, 45000, abort)) + const result = extensionTransferredBuildResultSchema.parse(await this.#peer.transfer({ action: 'build', transferId }, abort)) diagnostics.push(...result.diagnostics) if (!result.ok) return { ok: false, code: result.code, diagnostics } @@ -99,7 +99,7 @@ export class PluginAuthoringService { let offset = 0 while (offset < result.package.size) { abort.throwIfAborted() - const chunk = extensionTransferChunkSchema.parse(await this.#peer.request(EXTENSION_PACKAGE_TRANSFER_RPC, { action: 'read', transferId, offset }, 10000, abort)) + const chunk = extensionTransferChunkSchema.parse(await this.#peer.transfer({ action: 'read', transferId, offset }, abort)) const bytes = Buffer.from(chunk.data, 'base64') if (!bytes.length || bytes.length > EXTENSION_PACKAGE_CHUNK_BYTES || bytes.toString('base64') !== chunk.data || chunk.offset !== offset + bytes.length || chunk.offset > result.package.size) throw new Error('EXTENSION_TRANSFER_ORDER') @@ -126,7 +126,7 @@ export class PluginAuthoringService { if (input.review) { try { abort.throwIfAborted() - this.#peer.notify(EXTENSION_REVIEW_REQUEST, { path: output.canonicalPath }) + this.#peer.review(output.canonicalPath) reviewRequested = true this.#changes.fire(copyEventSnapshot({ kind: 'review-requested', ...identity })) } @@ -143,7 +143,7 @@ export class PluginAuthoringService { } finally { if (transferId) - await this.#peer.request(EXTENSION_PACKAGE_TRANSFER_RPC, { action: 'dispose', transferId }, 10000).catch(() => {}) + await this.#peer.transfer({ action: 'dispose', transferId }).catch(() => {}) } } } diff --git a/apps/buddy/service/src/plugins/PluginHost.ts b/apps/buddy/service/src/plugins/PluginHost.ts new file mode 100644 index 000000000..935c92930 --- /dev/null +++ b/apps/buddy/service/src/plugins/PluginHost.ts @@ -0,0 +1,21 @@ +import type { z } from 'zod' +import type { ExtensionAgentCatalog, ExtensionAgentDescriptor, ExtensionAgentInvocation, extensionAgentRequestSchema } from '../../../shared/extensions/extensionAgent' +import type { extensionCapabilitiesRequestSchema, extensionCapabilitiesSchema, extensionIdentityRequestSchema, extensionIdentitySchema, ExtensionInspection, extensionTransferRequestSchema } from '../../../shared/extensions/extensionAuthoring' +import type { JsonValue } from '../../../shared/workbench/workbenchState' + +export interface PluginAgentHost { + releaseRun?: (runId: string) => Promise + list: (signal: AbortSignal) => Promise + invoke: (input: ExtensionAgentInvocation, signal: AbortSignal) => Promise + subscribe: (apply: (catalog: ExtensionAgentCatalog) => void) => () => void + bindInference: (handlers: { acquire: (input: { extensionId: string, invocationId: string, parentInvocationId?: string }, signal?: AbortSignal) => Promise, release: (invocationId: string) => null }) => () => void + bind: (handler: (input: z.infer, signal?: AbortSignal) => Promise) => () => void +} + +export interface PluginAuthoringHost { + transfer: (input: z.infer, signal?: AbortSignal) => Promise + review: (path: string) => void + identity: (input: z.infer, signal: AbortSignal) => Promise> + capabilities: (input: z.infer, signal: AbortSignal) => Promise> + inspect: (id: string, signal: AbortSignal) => Promise +} diff --git a/apps/buddy/service/src/plugins/__tests__/ExtensionActionService.spec.ts b/apps/buddy/service/src/plugins/__tests__/ExtensionActionService.spec.ts index f903922e9..df04844ab 100644 --- a/apps/buddy/service/src/plugins/__tests__/ExtensionActionService.spec.ts +++ b/apps/buddy/service/src/plugins/__tests__/ExtensionActionService.spec.ts @@ -5,6 +5,7 @@ import { describe, expect, it, vi } from 'vitest' import { RuntimeRpcPeer } from '../../../../platform/ipc/runtimeRpcPeer' import { extensionActionRpc } from '../../../../shared/extensions/extensionActionApi' import { extensionAgentRpc } from '../../../../shared/extensions/extensionAgent' +import { createRpcRuntimePlatform } from '../../platform/createRpcRuntimePlatform' import { createConversationRepository } from '../../storage/conversationRepository' import { openBuddyDatabase } from '../../storage/database' import { createExtensionInvocationRepository } from '../../storage/extensionInvocationRepository' @@ -57,7 +58,7 @@ describe('extension action dispatch', () => { signal!.addEventListener('abort', cancel, { once: true }) }) }) - const runtime = new ExtensionAgentRuntime({ rpc: peer, createHandlers: () => ({ 'task.scope': async () => ({ taskId: 'conversation-1', branchId: 'branch-1', runId: 'run-1' }), 'artifacts.publish': async () => null, 'results.addImage': async () => null, 'task.get': async () => null, 'task.messages': async () => [], 'task.rename': async () => null, 'models.generateText': async () => null, 'references.list': async () => ({ items: [], truncated: false }), 'references.resolve': async () => null }) }) + const runtime = new ExtensionAgentRuntime({ host: createRpcRuntimePlatform(peer).plugins, createHandlers: () => ({ 'task.scope': async () => ({ taskId: 'conversation-1', branchId: 'branch-1', runId: 'run-1' }), 'artifacts.publish': async () => null, 'results.addImage': async () => null, 'task.get': async () => null, 'task.messages': async () => [], 'task.rename': async () => null, 'models.generateText': async () => null, 'references.list': async () => ({ items: [], truncated: false }), 'references.resolve': async () => null }) }) const service = new ExtensionActionService({ runtime, repository: createExtensionInvocationRepository(database), capture: () => ({ branchId: 'branch', sourceMessageId: null }) }) peer.onRequest(extensionActionRpc.invoke.method, (input, signal) => service.invoke(extensionActionRpc.invoke.input.parse(input), signal)) try { @@ -139,7 +140,7 @@ describe('extension action dispatch', () => { finally { active-- } }, } - const runtime = new ExtensionAgentRuntime({ rpc, createHandlers: () => ({ 'task.scope': async () => ({ taskId: 'conversation-1', branchId: 'branch-1', runId: 'run-1' }), 'artifacts.publish': async () => null, 'results.addImage': async () => null, 'task.get': async () => null, 'task.messages': async () => [], 'task.rename': async () => null, 'models.generateText': async () => null, 'references.list': async () => ({ items: [], truncated: false }), 'references.resolve': async () => null }) }) + const runtime = new ExtensionAgentRuntime({ host: createRpcRuntimePlatform(rpc).plugins, createHandlers: () => ({ 'task.scope': async () => ({ taskId: 'conversation-1', branchId: 'branch-1', runId: 'run-1' }), 'artifacts.publish': async () => null, 'results.addImage': async () => null, 'task.get': async () => null, 'task.messages': async () => [], 'task.rename': async () => null, 'models.generateText': async () => null, 'references.list': async () => ({ items: [], truncated: false }), 'references.resolve': async () => null }) }) const service = new ExtensionActionService({ runtime, repository: createExtensionInvocationRepository(database), capture: () => ({ branchId: 'branch', sourceMessageId: null }) }) const errors: string[] = [] service.onDidFail(event => errors.push(event.errorCode)) diff --git a/apps/buddy/service/src/plugins/__tests__/ExtensionAgentRuntime.spec.ts b/apps/buddy/service/src/plugins/__tests__/ExtensionAgentRuntime.spec.ts index 7536115a2..6a6ad303f 100644 --- a/apps/buddy/service/src/plugins/__tests__/ExtensionAgentRuntime.spec.ts +++ b/apps/buddy/service/src/plugins/__tests__/ExtensionAgentRuntime.spec.ts @@ -10,6 +10,7 @@ import { describe, expect, it } from 'vitest' import { extensionAgentRpc } from '../../../../shared/extensions/extensionAgent' import { createBuddyRunOutputs } from '../../agent/events/toolPresentation' import { ToolDisclosure } from '../../agent/extensions/discovery/ToolDisclosure' +import { createRpcRuntimePlatform } from '../../platform/createRpcRuntimePlatform' import { ExtensionAgentRuntime } from '../ExtensionAgentRuntime' const descriptor: ExtensionAgentDescriptor = { id: 'tests.reader', name: 'Fixture', revision: 'package-revision', configurationRevision: 'config-revision', agent: { actions: [], instructions: 'fixture-private-instructions', tools: [{ id: 'tests.reader.read', title: 'Read', description: 'fixture-private-description', parameters: { type: 'object', properties: {}, required: [], additionalProperties: false } }] } } @@ -48,7 +49,7 @@ function fixture() { return invoke(input, signal) }, } - const runtime = new ExtensionAgentRuntime({ rpc, createHandlers: () => handlers }) + const runtime = new ExtensionAgentRuntime({ host: createRpcRuntimePlatform(rpc).plugins, createHandlers: () => handlers }) const facts: ExtensionAgentChange[] = [] runtime.onDidChange(event => facts.push(event)) runtime.bind() @@ -277,6 +278,11 @@ describe('extension agent capability and invocation lifetimes', () => { it('retains invocation ownership until accepted child handlers actually finish and fences new requests after return', async () => { const f = fixture() + const returned = deferred() + f.runtime.onDidChange((event) => { + if (event.kind === 'invocation' && event.stage === 'returned') + returned.resolve() + }) const childStarted = deferred() const childCommitted = deferred() let child!: Promise @@ -295,9 +301,7 @@ describe('extension agent capability and invocation lifetimes', () => { const execute = await f.tool() const executing = execute() await childStarted.promise - await Promise.resolve() - await Promise.resolve() - await Promise.resolve() + await returned.promise expect(f.facts).toContainEqual(expect.objectContaining({ kind: 'invocation', stage: 'returned', outcome: 'completed' })) expect(f.facts.some(fact => fact.kind === 'invocation' && fact.stage === 'settled')).toBe(false) expect(f.runtime.snapshot.activeInvocations).toMatchObject([{ invocationId, accepting: false, pendingRequests: 1 }]) diff --git a/apps/buddy/service/src/plugins/__tests__/pluginAuthoringCapability.spec.ts b/apps/buddy/service/src/plugins/__tests__/pluginAuthoringCapability.spec.ts index c563874e7..b7bfc57f6 100644 --- a/apps/buddy/service/src/plugins/__tests__/pluginAuthoringCapability.spec.ts +++ b/apps/buddy/service/src/plugins/__tests__/pluginAuthoringCapability.spec.ts @@ -7,6 +7,7 @@ import { afterEach, expect, it } from 'vitest' import { compileExtensionSource } from '../../../../platform/extensions/compileExtensionSource' import { unpackExtension } from '../../../../platform/extensions/extensionFiles' import { ExtensionPackageTransfer } from '../../../../platform/extensions/ExtensionPackageTransfer' +import { createRpcRuntimePlatform } from '../../platform/createRpcRuntimePlatform' import { createPluginAuthoringCapability } from '../pluginAuthoringCapability' import { PluginAuthoringService } from '../PluginAuthoringService' @@ -52,10 +53,11 @@ async function fixture() { notifications.push(input) }, } - const builder = new PluginAuthoringService(peer) + const host = createRpcRuntimePlatform({ ...peer, onRequest: () => () => {}, onNotification: () => () => {}, close() {} }).authoring + const builder = new PluginAuthoringService(host) const facts: unknown[] = [] builder.onDidChange(event => facts.push(event)) - const capability = createPluginAuthoringCapability({ conversationId: 'task', cwd: root, executionProfile: 'workspace_write', getRunId: () => 'run', grants: [{ kind: 'workspace', canonicalRoot: root, root, grantId: 'workspace' }], sessionMode: 'interactive', signal: new AbortController().signal }, peer, builder) + const capability = createPluginAuthoringCapability({ conversationId: 'task', cwd: root, executionProfile: 'workspace_write', getRunId: () => 'run', grants: [{ kind: 'workspace', canonicalRoot: root, root, grantId: 'workspace' }], sessionMode: 'interactive', signal: new AbortController().signal }, host, builder) await capability.extension.factory({ registerTool(value: ToolDefinition) { if (value.name === 'lexora_plugin_build') tool = value diff --git a/apps/buddy/service/src/plugins/pluginAuthoringCapability.ts b/apps/buddy/service/src/plugins/pluginAuthoringCapability.ts index 69e3f0ea9..3700e6f82 100644 --- a/apps/buddy/service/src/plugins/pluginAuthoringCapability.ts +++ b/apps/buddy/service/src/plugins/pluginAuthoringCapability.ts @@ -1,10 +1,10 @@ -import type { RuntimeRpcPeerContract } from '../../../shared/runtime/rpcPeer' import type { BuddyCapability, BuddyCapabilityContext } from '../agent/extensions/BuddyCapability' import type { PluginAuthoringService } from './PluginAuthoringService' +import type { PluginAuthoringHost } from './PluginHost' import { defineTool } from '@earendil-works/pi-coding-agent' import { Type } from 'typebox' import { Check } from 'typebox/value' -import { EXTENSION_CAPABILITIES_RPC, EXTENSION_IDENTITY_RPC, EXTENSION_INSPECT_RPC, extensionCapabilitiesSchema, extensionIdentityRequestSchema, extensionIdentitySchema, extensionInspectionSchema } from '../../../shared/extensions/extensionAuthoring' +import { extensionIdentityRequestSchema } from '../../../shared/extensions/extensionAuthoring' import { workbenchCapabilityKinds } from '../../../shared/workbench/workbenchContributionCatalog' import { toPiToolGroup } from '../agent/extensions/piToolGroup' @@ -29,7 +29,7 @@ const parameters = Type.Object({ review: Type.Optional(Type.Boolean({ description: 'Show the user an installation review after building. Never installs automatically.' })), }, { additionalProperties: false }) -export function createPluginAuthoringCapability(context: BuddyCapabilityContext, peer: Pick, builder: PluginAuthoringService): BuddyCapability { +export function createPluginAuthoringCapability(context: BuddyCapabilityContext, host: PluginAuthoringHost, builder: PluginAuthoringService): BuddyCapability { return { classify(event) { if (event.toolName === identityName) @@ -59,7 +59,7 @@ export function createPluginAuthoringCapability(context: BuddyCapabilityContext, return response({ ok: false, code: 'EXTENSION_IDENTITY_INVALID', diagnostics: parsed.error.issues.map(issue => `${issue.path.join('.')}: ${issue.message}`), nextAction: 'Explain the invalid fields and ask the user to choose suitable names before retrying. Do not silently replace or truncate them.' }) try { const abort = signal ? AbortSignal.any([signal, context.signal]) : context.signal - const identity = extensionIdentitySchema.parse(await peer.request(EXTENSION_IDENTITY_RPC, parsed.data, 10000, abort)) + const identity = await host.identity(parsed.data, abort) return response({ ok: true, ...identity }) } catch { return response({ ok: false, code: 'EXTENSION_IDENTITY_FAILED' }) } @@ -73,7 +73,7 @@ export function createPluginAuthoringCapability(context: BuddyCapabilityContext, async execute(_toolCallId, input, signal) { try { const abort = signal ? AbortSignal.any([signal, context.signal]) : context.signal - const capabilities = extensionCapabilitiesSchema.parse(await peer.request(EXTENSION_CAPABILITIES_RPC, input, 10000, abort)) + const capabilities = await host.capabilities(input, abort) return response({ ok: true, ...capabilities }) } catch { return response({ ok: false, code: 'EXTENSION_CAPABILITIES_FAILED' }) } @@ -87,7 +87,7 @@ export function createPluginAuthoringCapability(context: BuddyCapabilityContext, async execute(_toolCallId, input, signal) { try { const abort = signal ? AbortSignal.any([signal, context.signal]) : context.signal - const status = extensionInspectionSchema.parse(await peer.request(EXTENSION_INSPECT_RPC, input, 10000, abort)) + const status = await host.inspect(input.id, abort) return response({ ok: true, ...status }) } catch { return response({ ok: false, code: 'EXTENSION_INSPECTION_FAILED' }) } diff --git a/apps/buddy/service/src/privateNetwork/DeviceAuthorizationService.ts b/apps/buddy/service/src/privateNetwork/DeviceAuthorizationService.ts new file mode 100644 index 000000000..eaccd6140 --- /dev/null +++ b/apps/buddy/service/src/privateNetwork/DeviceAuthorizationService.ts @@ -0,0 +1,278 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { z } from 'zod' +import type { TrustedProductContext } from '../../../shared/productProtocol/identity' +import type { TrustedCommandPrincipal } from '../commands/CommandAdmissionService' +import { Buffer } from 'node:buffer' +import { createHash, randomBytes, randomUUID, timingSafeEqual } from 'node:crypto' +import { Emitter } from '../../../shared/events/Emitter' +import { PRIVATE_NETWORK_LIMITS as limits } from '../../../shared/privateNetwork/limits' +import { deviceCredentialSchema, deviceStateSchema, pairingPrepareResultSchema, pairingPrepareSchema, pairingQrSchema, pairingRecoverySchema, pairingRedeemSchema, PrivateNetworkError } from '../../../shared/privateNetwork/pairing' +import { encodeProductRevision } from '../../../shared/productProtocol/scalars' +import { CommandAdmissionError } from '../commands/CommandAdmissionError' +import { requireSqliteAuthority } from '../storage/sqliteAuthority' +import { afterTransactionCommit, withTransaction } from '../storage/transaction' + +type Row = Record +export interface AuthenticatedDevice { + deviceId: string + credentialId: string + grantGeneration: string + ownerPrincipalId: string + dataGeneration: string +} + +export class DeviceAuthorizationService { + readonly #database: DatabaseSync + readonly #identity: { serviceId: string, profileId: string, serviceInstanceId: string } + readonly #clock: () => number + readonly #invalidated = new Emitter<{ deviceId: string }>(() => {}) + readonly onDidInvalidate = this.#invalidated.event + #observed = 0 + + constructor(options: { database: DatabaseSync, serviceId: string, profileId: string, serviceInstanceId: string, clock?: () => number }) { + this.#database = options.database + this.#identity = options + this.#clock = options.clock ?? Date.now + requireSqliteAuthority(this.#database) + this.sweep() + withTransaction(this.#database, () => this.#database.prepare('UPDATE pairing_tickets SET state = \'cancelled\' WHERE state = \'pending\' AND issuer_instance_id != ?').run(options.serviceInstanceId)) + } + + issue(endpoint: string, spkiPin: string) { + this.sweep() + return withTransaction(this.#database, () => { + const now = this.#now() + const ticketId = randomUUID() + const ticketSecret = secret() + const result = pairingQrSchema.parse({ format: 'lexora-pairing', version: 1, serviceId: this.#identity.serviceId, profileId: this.#identity.profileId, issuerInstanceId: this.#identity.serviceInstanceId, endpoint, spkiPin, ticketId, ticketSecret, expiresAt: iso(now + limits.ticketLifetimeMs) }) + this.#database.prepare('UPDATE pairing_tickets SET state = \'cancelled\' WHERE state = \'pending\'').run() + this.#database.prepare('INSERT INTO pairing_tickets(id,secret_hash,issuer_instance_id,state,expires_ms) VALUES (?,?,?,\'pending\',?)').run(ticketId, hash(ticketSecret), this.#identity.serviceInstanceId, now + limits.ticketLifetimeMs) + return result + }) + } + + cancelTicket(ticketId?: string): void { + withTransaction(this.#database, () => this.#database.prepare('UPDATE pairing_tickets SET state = \'cancelled\' WHERE state = \'pending\' AND (? IS NULL OR id = ?)').run(ticketId ?? null, ticketId ?? null)) + } + + prepare(raw: unknown) { + const input = pairingPrepareSchema.parse(raw) + this.sweep() + return withTransaction(this.#database, () => { + this.#ticket(input) + const now = this.#now() + if (Number(this.#database.prepare('SELECT count(*) AS n FROM pairing_attempts WHERE closed_ms IS NULL').get()!.n) >= limits.pendingAttempts) + throw new PrivateNetworkError('LIMIT_EXCEEDED') + const attemptId = randomUUID() + const recoverySecret = secret() + const expires = now + limits.recoveryLifetimeMs + this.#database.prepare('INSERT INTO pairing_attempts(id,ticket_id,recovery_hash,device_name,expires_ms) VALUES (?,?,?,?,?)').run(attemptId, input.ticketId, hash(recoverySecret), input.deviceName, expires) + return pairingPrepareResultSchema.parse({ attemptId, recoverySecret, expiresAt: iso(expires) }) + }) + } + + redeem(raw: unknown) { + const input = pairingRedeemSchema.parse(raw) + this.sweep() + return withTransaction(this.#database, () => { + this.#context(input) + const attempt = this.#attempt(input.attemptId, input.recoverySecret) + if (attempt.ticket_id !== input.ticketId) + throw new PrivateNetworkError('NOT_AUTHORIZED') + if (attempt.device_id) + throw new PrivateNetworkError('RECOVERY_REQUIRED') + const ticket = this.#ticket(input) + if (Number(this.#database.prepare('SELECT count(*) AS n FROM remote_devices WHERE state IN (\'pending\',\'active\')').get()!.n) >= limits.devices) + throw new PrivateNetworkError('LIMIT_EXCEEDED') + const now = this.#now() + const deviceId = randomUUID() + const profile = this.#profile() + this.#database.prepare('INSERT INTO storage_actors(id,kind,state,created_at) VALUES (?,\'device\',\'active\',?)').run(deviceId, iso(now)) + this.#database.prepare('INSERT INTO remote_devices(id,owner_actor_id,data_generation,state,name,created_ms,pending_expires_ms,idle_expires_ms,absolute_expires_ms) VALUES (?,?,?,\'pending\',?,?,?,?,?)').run(deviceId, String(profile.local_actor_id), String(profile.data_generation), String(attempt.device_name), now, Number(attempt.expires_ms), now + limits.credentialIdleMs, now + limits.credentialAbsoluteMs) + this.#database.prepare('UPDATE pairing_attempts SET device_id = ? WHERE id = ?').run(deviceId, input.attemptId) + this.#database.prepare('UPDATE pairing_tickets SET state = \'consumed\', attempt_id = ? WHERE id = ? AND state = \'pending\'').run(input.attemptId, String(ticket.id)) + return this.#issueCredential(input.attemptId, this.#device(deviceId)!) + }) + } + + recover(raw: unknown) { + const input = pairingRecoverySchema.parse(raw) + this.sweep() + return withTransaction(this.#database, () => { + this.#context(input) + const attempt = this.#attempt(input.attemptId, input.recoverySecret) + const device = attempt.device_id ? this.#device(String(attempt.device_id)) : null + if (!device || device.state !== 'pending' || device.data_generation !== this.#profile().data_generation) + throw new PrivateNetworkError('RECOVERY_CLOSED') + if (Number(attempt.credential_count) >= limits.siblingCredentials) + throw new PrivateNetworkError('LIMIT_EXCEEDED') + return this.#issueCredential(input.attemptId, device) + }) + } + + confirm(bearer: string) { + this.sweep() + return withTransaction(this.#database, () => { + const { device } = this.#credential(bearer, true) + if (device.state === 'pending') { + const now = this.#now() + this.#database.prepare('UPDATE remote_devices SET state = \'active\', confirmed_ms = ?, last_seen_ms = ? WHERE id = ? AND state = \'pending\'').run(now, now, String(device.id)) + this.#database.prepare('UPDATE pairing_attempts SET closed_ms = ? WHERE device_id = ? AND closed_ms IS NULL').run(now, String(device.id)) + } + return this.#publicDevice(this.#device(String(device.id))!) + }) + } + + authenticate(bearer: string): AuthenticatedDevice { + this.sweep() + const { credential, device } = this.#credential(bearer, false) + return { deviceId: String(device.id), credentialId: String(credential.id), grantGeneration: encodeProductRevision(Number(device.grant_generation)), ownerPrincipalId: String(device.owner_actor_id), dataGeneration: String(device.data_generation) } + } + + assertCurrent(identity: AuthenticatedDevice): void { + this.sweep() + const device = this.#device(identity.deviceId) + const credential = this.#database.prepare('SELECT * FROM remote_credentials WHERE id = ? AND device_id = ?').get(identity.credentialId, identity.deviceId) + if (!device || !credential || device.state !== 'active' || encodeProductRevision(Number(device.grant_generation)) !== identity.grantGeneration || Number(credential.grant_generation) !== Number(device.grant_generation) || device.owner_actor_id !== identity.ownerPrincipalId || device.data_generation !== identity.dataGeneration || device.data_generation !== this.#profile().data_generation) + throw new PrivateNetworkError('NOT_AUTHORIZED') + } + + principal(identity: AuthenticatedDevice): TrustedCommandPrincipal { + return Object.freeze({ actorId: identity.deviceId, assertAuthenticated: () => { + try { + this.assertCurrent(identity) + } + catch { throw new CommandAdmissionError('COMMAND_ACTOR_UNAVAILABLE') } + } }) + } + + context(identity: AuthenticatedDevice): TrustedProductContext { + this.assertCurrent(identity) + return { ...identity, ...this.#identity, connectionId: randomUUID(), clientSessionId: randomUUID(), role: 'owner' } + } + + state(bearer: string) { + const identity = this.authenticate(bearer) + return this.#publicDevice(this.#device(identity.deviceId)!) + } + + renew(bearer: string) { + const identity = this.authenticate(bearer) + return withTransaction(this.#database, () => { + this.assertCurrent(identity) + const now = this.#now() + this.#database.prepare('UPDATE remote_devices SET last_seen_ms = ?, idle_expires_ms = MIN(absolute_expires_ms, ?) WHERE id = ?').run(now, now + limits.credentialIdleMs, identity.deviceId) + return this.#publicDevice(this.#device(identity.deviceId)!) + }) + } + + revoke(deviceId: string): void { + this.sweep() + withTransaction(this.#database, () => { + const device = this.#device(deviceId) + if (!device) + throw new PrivateNetworkError('NOT_AUTHORIZED') + if (device.state === 'revoked' || device.state === 'expired') + return + this.#invalidate(deviceId, 'revoked', this.#now()) + }) + } + + revokeAll(): void { + withTransaction(this.#database, () => { + for (const row of this.#database.prepare('SELECT id FROM remote_devices WHERE state IN (\'pending\',\'active\')').all()) this.#invalidate(String(row.id), 'revoked', this.#now()) + this.cancelTicket() + }) + } + + list(): z.infer[] { + this.sweep() + return this.#database.prepare('SELECT * FROM remote_devices ORDER BY created_ms DESC LIMIT 256').all().map(row => this.#publicDevice(row)) + } + + sweep(): void { + withTransaction(this.#database, () => { + const now = this.#now() + this.#database.prepare('UPDATE pairing_tickets SET state = \'expired\' WHERE state = \'pending\' AND expires_ms <= ?').run(now) + this.#database.prepare('UPDATE pairing_attempts SET closed_ms = ? WHERE closed_ms IS NULL AND expires_ms <= ?').run(now, now) + const rows = this.#database.prepare('SELECT id FROM remote_devices WHERE state IN (\'pending\',\'active\') AND (data_generation != ? OR absolute_expires_ms <= ? OR idle_expires_ms <= ? OR (state = \'pending\' AND pending_expires_ms <= ?))').all(String(this.#profile().data_generation), now, now, now) + for (const row of rows) this.#invalidate(String(row.id), 'expired', now) + }) + } + + #invalidate(deviceId: string, state: 'expired' | 'revoked', now: number) { + this.#database.prepare('UPDATE remote_devices SET state = ?, grant_generation = grant_generation + 1 WHERE id = ?').run(state, deviceId) + this.#database.prepare('UPDATE storage_actors SET state = \'revoked\' WHERE id = ?').run(deviceId) + this.#database.prepare('UPDATE pairing_attempts SET closed_ms = COALESCE(closed_ms, ?) WHERE device_id = ?').run(now, deviceId) + afterTransactionCommit(this.#database, () => this.#invalidated.fire({ deviceId }), () => {}) + } + + #credential(bearer: string, allowPending: boolean) { + const match = /^([\w-]{1,128})\.([\w-]{43})$/.exec(bearer) + if (!match) + throw new PrivateNetworkError('NOT_AUTHORIZED') + const credential = this.#database.prepare('SELECT * FROM remote_credentials WHERE id = ?').get(match[1]!) + const device = credential ? this.#device(String(credential.device_id)) : null + if (!credential || !device || !matches(String(credential.secret_hash), match[2]!) || (device.state !== 'active' && !(allowPending && device.state === 'pending')) || Number(credential.grant_generation) !== Number(device.grant_generation) || device.data_generation !== this.#profile().data_generation) + throw new PrivateNetworkError('NOT_AUTHORIZED') + return { credential, device } + } + + #issueCredential(attemptId: string, device: Row) { + const now = this.#now() + const credentialId = randomUUID() + const credentialSecret = secret() + this.#database.prepare('INSERT INTO remote_credentials(id,device_id,secret_hash,grant_generation,created_ms) VALUES (?,?,?,?,?)').run(credentialId, String(device.id), hash(credentialSecret), Number(device.grant_generation), now) + this.#database.prepare('UPDATE pairing_attempts SET credential_count = credential_count + 1 WHERE id = ?').run(attemptId) + return deviceCredentialSchema.parse({ deviceId: device.id, credentialId, credentialSecret, grantGeneration: encodeProductRevision(Number(device.grant_generation)), expiresAt: iso(Number(device.idle_expires_ms)), absoluteExpiresAt: iso(Number(device.absolute_expires_ms)) }) + } + + #attempt(id: string, recovery: string) { + const attempt = this.#database.prepare('SELECT * FROM pairing_attempts WHERE id = ?').get(id) + if (!attempt || !matches(String(attempt.recovery_hash), recovery)) + throw new PrivateNetworkError('NOT_AUTHORIZED') + if (attempt.closed_ms !== null || Number(attempt.expires_ms) <= this.#now()) + throw new PrivateNetworkError('RECOVERY_CLOSED') + return attempt + } + + #ticket(input: { serviceId: string, profileId: string, issuerInstanceId: string, ticketId: string, ticketSecret: string }) { + this.#context(input) + const ticket = this.#database.prepare('SELECT * FROM pairing_tickets WHERE id = ?').get(input.ticketId) + if (!ticket || !matches(String(ticket.secret_hash), input.ticketSecret) || ticket.issuer_instance_id !== this.#identity.serviceInstanceId || input.issuerInstanceId !== ticket.issuer_instance_id || Number(ticket.expires_ms) <= this.#now()) + throw new PrivateNetworkError('PAIRING_UNAVAILABLE') + if (ticket.state !== 'pending') + throw new PrivateNetworkError(ticket.state === 'consumed' ? 'PAIRING_USED' : 'PAIRING_UNAVAILABLE') + return ticket + } + + #context(input: { serviceId: string, profileId: string }) { + if (input.serviceId !== this.#identity.serviceId || input.profileId !== this.#identity.profileId) + throw new PrivateNetworkError('NOT_AUTHORIZED') + } + + #now(): number { + this.#observed = Math.max(this.#observed, this.#clock(), Number(this.#database.prepare('SELECT last_observed_ms FROM private_network_state WHERE id = 1').get()!.last_observed_ms)) + this.#database.prepare('UPDATE private_network_state SET last_observed_ms = ? WHERE id = 1 AND last_observed_ms < ?').run(this.#observed, this.#observed) + return this.#observed + } + + #profile() { return this.#database.prepare('SELECT * FROM storage_profile WHERE id = 1').get()! } + #device(id: string) { return this.#database.prepare('SELECT * FROM remote_devices WHERE id = ?').get(id) } + #publicDevice(device: Row) { + return deviceStateSchema.parse({ deviceId: device.id, name: device.name, state: device.state, grantGeneration: encodeProductRevision(Number(device.grant_generation)), createdAt: iso(Number(device.created_ms)), confirmedAt: device.confirmed_ms === null ? null : iso(Number(device.confirmed_ms)), lastSeenAt: device.last_seen_ms === null ? null : iso(Number(device.last_seen_ms)), expiresAt: iso(Number(device.idle_expires_ms)), absoluteExpiresAt: iso(Number(device.absolute_expires_ms)) }) + } +} +function secret() { + return randomBytes(32).toString('base64url') +} +function hash(value: string) { + return createHash('sha256').update(value).digest('hex') +} +function matches(expected: string, value: string): boolean { + const actual = hash(value) + return expected.length === actual.length && timingSafeEqual(Buffer.from(expected), Buffer.from(actual)) +} +function iso(value: number) { + return new Date(value).toISOString() +} diff --git a/apps/buddy/service/src/privateNetwork/PrivateNetworkController.ts b/apps/buddy/service/src/privateNetwork/PrivateNetworkController.ts new file mode 100644 index 000000000..9835570a5 --- /dev/null +++ b/apps/buddy/service/src/privateNetwork/PrivateNetworkController.ts @@ -0,0 +1,156 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { z } from 'zod' +import type { RuntimePlatform } from '../platform/RuntimePlatform' +import type { RuntimeRequestRegistrar } from '../rpc/runtimeRequest' +import type { AuthenticatedDevice } from './DeviceAuthorizationService' +import type { PrivateNetworkServerOptions } from './PrivateNetworkServer' +import { privateNetworkManagement, privateNetworkStatusSchema } from '../../../shared/privateNetwork/management' +import { PrivateNetworkError } from '../../../shared/privateNetwork/pairing' +import { registerRuntimeRequest } from '../rpc/runtimeRequest' +import { withTransaction } from '../storage/transaction' +import { DeviceAuthorizationService } from './DeviceAuthorizationService' +import { selectPrivateNetworkAddress } from './privateNetworkAddress' +import { PrivateNetworkServer } from './PrivateNetworkServer' +import { isPrivateNetworkAddress, loadPrivateNetworkTlsIdentity } from './PrivateNetworkTlsIdentity' + +interface PrivateNetworkControllerOptions { + database: DatabaseSync + serviceId: string + profileId: string + serviceInstanceId: string + secrets: RuntimePlatform['privateNetworkSecrets'] + connect: PrivateNetworkServerOptions['connect'] + bytes?: PrivateNetworkServerOptions['bytes'] +} +export class PrivateNetworkController { + readonly auth: DeviceAuthorizationService + readonly #options: PrivateNetworkControllerOptions + #server: PrivateNetworkServer | null = null + #endpoint: string | null = null + #error: z.infer['errorCode'] = null + #pending = Promise.resolve() + #closed = false + + constructor(options: PrivateNetworkControllerOptions) { + this.#options = options + this.auth = new DeviceAuthorizationService(options) + } + + status() { + const stored = this.#options.database.prepare('SELECT * FROM private_network_state WHERE id = 1').get()! + const enabled = stored.enabled === 1 + return privateNetworkStatusSchema.parse({ enabled, state: this.#server ? 'listening' : enabled ? 'unavailable' : 'disabled', bindAddress: stored.bind_address, port: stored.port, endpoint: this.#endpoint, spkiPin: stored.tls_pin, errorCode: this.#error }) + } + + async restore(): Promise { + await this.#serialize(async () => { + if (this.status().enabled) + await this.#listen() + }) + } + + configure(input: z.infer) { + return this.#serialize(async () => { + const previous = this.status() + const configuredAddress = input.bindAddress === '' ? null : input.bindAddress ?? previous.bindAddress + const address = configuredAddress ?? (input.enabled ? selectPrivateNetworkAddress() : null) + const port = input.port ?? previous.port ?? 19443 + if (address !== null && !isPrivateNetworkAddress(address)) + throw new PrivateNetworkError('INVALID_REQUEST') + withTransaction(this.#options.database, () => this.#options.database.prepare('UPDATE private_network_state SET enabled = ?, bind_address = ?, port = ? WHERE id = 1').run(Number(input.enabled), address, port)) + this.auth.cancelTicket() + await this.#stopListener() + if (input.enabled) + await this.#listen() + return this.status() + }) + } + + resetIdentity() { + return this.#serialize(async () => { + this.auth.revokeAll() + await this.#stopListener() + if (this.status().enabled) { + await this.#listen(true) + } + else { + const { database, profileId, secrets } = this.#options + if (!secrets) + throw new PrivateNetworkError('TLS_IDENTITY_UNAVAILABLE') + await loadPrivateNetworkTlsIdentity({ database, profileId, secrets, addresses: [this.status().bindAddress ?? '127.0.0.1'], reset: true }) + } + return this.status() + }) + } + + register(rpc: RuntimeRequestRegistrar): void { + registerRuntimeRequest(rpc, privateNetworkManagement.status, () => this.status()) + registerRuntimeRequest(rpc, privateNetworkManagement.configure, input => this.configure(input)) + registerRuntimeRequest(rpc, privateNetworkManagement.issuePairing, () => { + const status = this.status() + if (!status.endpoint || !status.spkiPin) + throw new PrivateNetworkError('PAIRING_UNAVAILABLE') + return this.auth.issue(status.endpoint, status.spkiPin) + }) + registerRuntimeRequest(rpc, privateNetworkManagement.cancelPairing, (input) => { + this.auth.cancelTicket(input.ticketId) + return { ok: true as const } + }) + registerRuntimeRequest(rpc, privateNetworkManagement.devices, () => this.auth.list()) + registerRuntimeRequest(rpc, privateNetworkManagement.revoke, (input) => { + this.auth.revoke(input.deviceId) + return { ok: true as const } + }) + registerRuntimeRequest(rpc, privateNetworkManagement.resetIdentity, () => this.resetIdentity()) + } + + async close(): Promise { + this.#closed = true + await this.#pending.catch(() => {}) + await this.#stopListener() + } + + async #listen(reset = false) { + const { database, profileId, serviceId, secrets, connect, bytes } = this.#options + const status = this.status() + if (!secrets) { + this.#error = 'CREDENTIAL_STORE_UNAVAILABLE' + return + } + if (!status.bindAddress) { + this.#error = 'ADDRESS_UNAVAILABLE' + return + } + let server: PrivateNetworkServer | null = null + try { + const tls = await loadPrivateNetworkTlsIdentity({ database, profileId, secrets, addresses: [status.bindAddress!], reset }) + server = new PrivateNetworkServer({ tls, auth: this.auth, identity: { serviceId, profileId }, connect, bytes }) + this.#endpoint = await server.listen(status.bindAddress!, status.port!) + this.#server = server + this.#error = null + } + catch (error) { + await server?.close() + const code = (error as { code?: string }).code + this.#error = code === 'EADDRINUSE' ? 'ADDRESS_IN_USE' : code === 'EADDRNOTAVAIL' ? 'ADDRESS_UNAVAILABLE' : code?.startsWith('CREDENTIAL_') ? 'CREDENTIAL_STORE_UNAVAILABLE' : code === 'TLS_IDENTITY_UNAVAILABLE' ? 'TLS_IDENTITY_UNAVAILABLE' : 'LISTEN_FAILED' + } + } + + async #stopListener() { + const server = this.#server + this.#server = null + this.#endpoint = null + this.#error = null + await server?.close() + } + + #serialize(operation: () => Promise): Promise { + if (this.#closed) + return Promise.reject(new PrivateNetworkError('PAIRING_UNAVAILABLE')) + const result = this.#pending.catch(() => {}).then(operation) + this.#pending = result.then(() => {}, () => {}) + return result + } +} + +export type PrivateNetworkConnectionFactory = (identity: AuthenticatedDevice, transport: Parameters[1]) => ReturnType diff --git a/apps/buddy/service/src/privateNetwork/PrivateNetworkServer.ts b/apps/buddy/service/src/privateNetwork/PrivateNetworkServer.ts new file mode 100644 index 000000000..ea71f36de --- /dev/null +++ b/apps/buddy/service/src/privateNetwork/PrivateNetworkServer.ts @@ -0,0 +1,293 @@ +import type { IncomingMessage, ServerResponse } from 'node:http' +import type { Server } from 'node:https' +import type { Socket } from 'node:net' +import type { ProductConnection } from '../productProtocol/ProductConnection' +import type { AuthenticatedDevice, DeviceAuthorizationService } from './DeviceAuthorizationService' +import type { PrivateNetworkTlsIdentity } from './PrivateNetworkTlsIdentity' +import { Buffer } from 'node:buffer' +import { createServer } from 'node:https' +import { WebSocket, WebSocketServer } from 'ws' +import { z } from 'zod' +import { PRIVATE_NETWORK_LIMITS as limits } from '../../../shared/privateNetwork/limits' +import { pairingConfirmSchema, PrivateNetworkError, privateNetworkErrorSchema } from '../../../shared/privateNetwork/pairing' +import { PRODUCT_PROTOCOL_VERSION } from '../../../shared/productProtocol/identity' +import { PRODUCT_LIMITS } from '../../../shared/productProtocol/limits' +import { isPrivateNetworkAddress } from './PrivateNetworkTlsIdentity' + +interface ConnectionTransport { + send: (frame: string) => boolean + close: () => void +} +export interface PrivateNetworkServerOptions { + auth: DeviceAuthorizationService + identity: { serviceId: string, profileId: string } + tls: PrivateNetworkTlsIdentity + connect: (identity: AuthenticatedDevice, transport: ConnectionTransport) => ProductConnection + bytes?: (request: IncomingMessage, response: ServerResponse, identity: AuthenticatedDevice) => Promise +} + +export class PrivateNetworkServer { + readonly #options: PrivateNetworkServerOptions + readonly #server: Server + readonly #websocket = new WebSocketServer({ noServer: true, maxPayload: PRODUCT_LIMITS.frameBytes, perMessageDeflate: false, clientTracking: false }) + readonly #connections = new Map() + readonly #requests = new Map() + readonly #sockets = new Set() + readonly #rates = new Map() + readonly #unsubscribe: { dispose: () => void } + readonly #expiration: ReturnType + #inFlight = 0 + #closed = false + + constructor(options: PrivateNetworkServerOptions) { + this.#options = options + this.#server = createServer({ key: options.tls.privateKey, cert: options.tls.certificate, minVersion: 'TLSv1.2', maxHeaderSize: limits.requestBytes, requestTimeout: limits.requestTimeoutMs, headersTimeout: limits.requestTimeoutMs, keepAliveTimeout: 5000 }, (request, response) => { + response.setHeader('Cache-Control', 'no-store') + response.setHeader('X-Content-Type-Options', 'nosniff') + response.setHeader('Content-Security-Policy', 'default-src \'none\'; sandbox') + void this.#handle(request, response).catch(error => this.#failure(response, error)) + }) + this.#server.maxConnections = limits.httpRequests + limits.connections + this.#server.on('connection', (socket) => { + this.#sockets.add(socket) + socket.once('close', () => this.#sockets.delete(socket)) + }) + this.#server.on('upgrade', (request, socket, head) => { + try { + this.#rate(request, false) + if (this.#closed || request.url !== '/v1/rpc' || request.method !== 'GET') + throw new PrivateNetworkError('INVALID_REQUEST') + const identity = options.auth.authenticate(bearer(request)) + if (this.#connections.size >= limits.connections || [...this.#connections.values()].filter(item => item.identity.deviceId === identity.deviceId).length >= limits.connectionsPerDevice) + throw new PrivateNetworkError('LIMIT_EXCEEDED') + this.#websocket.handleUpgrade(request, socket, head, websocket => this.#connected(websocket, identity)) + } + catch { + socket.end('HTTP/1.1 401 Unauthorized\r\nConnection: close\r\nContent-Length: 0\r\n\r\n') + } + }) + this.#unsubscribe = options.auth.onDidInvalidate(({ deviceId }) => { + for (const [socket, connection] of this.#connections) { + if (connection.identity.deviceId === deviceId) + socket.terminate() + } + for (const [response, owner] of this.#requests) { + if (owner === deviceId) + response.destroy() + } + }) + this.#expiration = setInterval(() => { + try { + options.auth.sweep() + } + catch { + for (const socket of this.#connections.keys()) socket.terminate() + for (const response of this.#requests.keys()) response.destroy() + } + }, 1000) + this.#expiration.unref() + } + + async listen(address: string, port: number): Promise { + if (!isPrivateNetworkAddress(address) || !Number.isInteger(port) || port < 0 || port > 65535 || this.#closed) + throw new PrivateNetworkError('INVALID_REQUEST') + await new Promise((resolve, reject) => { + this.#server.once('error', reject) + this.#server.listen(port, address, () => { + this.#server.off('error', reject) + resolve() + }) + }) + const actual = this.#server.address() + if (!actual || typeof actual === 'string') + throw new PrivateNetworkError('INVALID_REQUEST') + return `https://${actual.family === 'IPv6' ? `[${actual.address}]` : actual.address}:${actual.port}` + } + + async close(): Promise { + if (this.#closed) + return + this.#closed = true + clearInterval(this.#expiration) + this.#unsubscribe.dispose() + for (const [socket, connection] of this.#connections) { + connection.product.close() + socket.terminate() + } + for (const response of this.#requests.keys()) response.destroy() + this.#websocket.close() + const finished = new Promise(resolve => this.#server.close(() => resolve())) + for (const socket of this.#sockets) socket.destroy() + await finished + } + + #connected(socket: WebSocket, identity: AuthenticatedDevice) { + try { + this.#options.auth.assertCurrent(identity) + const product = this.#options.connect(identity, { + send: (frame) => { + try { + this.#options.auth.assertCurrent(identity) + } + catch { + socket.terminate() + return false + } + if (socket.readyState !== WebSocket.OPEN || socket.bufferedAmount + Buffer.byteLength(frame) > PRODUCT_LIMITS.connectionBytes) { + socket.terminate() + return false + } + socket.send(frame, (error) => { + if (error) + socket.terminate() + }) + return true + }, + close: () => socket.terminate(), + }) + this.#connections.set(socket, { identity, product }) + socket.on('message', (data, binary) => { + if (binary) { + socket.terminate() + return + } + void product.receive(data.toString()).catch(() => socket.terminate()) + }) + socket.on('error', () => socket.terminate()) + socket.once('close', () => { + product.close() + this.#connections.delete(socket) + }) + } + catch { socket.terminate() } + } + + async #handle(request: IncomingMessage, response: ServerResponse) { + if (this.#closed || this.#inFlight >= limits.httpRequests) + throw new PrivateNetworkError('LIMIT_EXCEEDED') + this.#inFlight++ + try { + const route = request.url ?? '' + this.#rate(request, route.startsWith('/v1/pairing/')) + if (route.includes('?') || route.includes('#')) + throw new PrivateNetworkError('INVALID_REQUEST') + if (route === '/v1/connection-info' && (request.method === 'GET' || request.method === 'HEAD')) { + this.#json(response, { ...this.#options.identity, protocol: PRODUCT_PROTOCOL_VERSION, pairingVersion: 1 }, request.method === 'HEAD') + return + } + if (route.startsWith('/v1/pairing/')) { + if (request.method !== 'POST') + throw new PrivateNetworkError('INVALID_REQUEST') + const input = await readJson(request) + let result: unknown + switch (route) { + case '/v1/pairing/prepare': + result = this.#options.auth.prepare(input) + break + case '/v1/pairing/redeem': + result = this.#options.auth.redeem(input) + break + case '/v1/pairing/recover': + result = this.#options.auth.recover(input) + break + case '/v1/pairing/confirm': + pairingConfirmSchema.parse(input) + result = this.#options.auth.confirm(bearer(request)) + break + default: throw new PrivateNetworkError('INVALID_REQUEST') + } + this.#json(response, result) + return + } + const token = bearer(request) + const identity = this.#options.auth.authenticate(token) + this.#requests.set(response, identity.deviceId) + if (route === '/v1/device' && request.method === 'GET') { + this.#json(response, this.#options.auth.state(token)) + } + else if (['/v1/device/renew', '/v1/device/logout'].includes(route) && request.method === 'POST') { + pairingConfirmSchema.parse(await readJson(request)) + if (route.endsWith('/renew')) { + this.#json(response, this.#options.auth.renew(token)) + } + else { + this.#requests.delete(response) + this.#options.auth.revoke(identity.deviceId) + this.#json(response, { revoked: true }) + } + } + else if (!await this.#options.bytes?.(request, response, identity)) { + throw new PrivateNetworkError('INVALID_REQUEST') + } + } + finally { + this.#requests.delete(response) + this.#inFlight-- + } + } + + #rate(request: IncomingMessage, pairing: boolean) { + const key = request.socket.remoteAddress ?? 'unknown' + const now = Date.now() + for (const [address, entry] of this.#rates) { + if (now - entry.start >= 60_000) + this.#rates.delete(address) + } + let entry = this.#rates.get(key) + if (!entry) { + if (this.#rates.size >= 256) + throw new PrivateNetworkError('LIMIT_EXCEEDED') + entry = { start: now, requests: 0, pairing: 0 } + this.#rates.set(key, entry) + } + entry.requests++ + if (pairing) + entry.pairing++ + if (entry.requests > limits.requestsPerMinute || entry.pairing > limits.pairingRequestsPerMinute) + throw new PrivateNetworkError('LIMIT_EXCEEDED') + } + + #json(response: ServerResponse, value: unknown, head = false) { + if (response.destroyed) + return + response.setHeader('Content-Type', 'application/json; charset=utf-8') + response.end(head ? undefined : JSON.stringify(value)) + } + + #failure(response: ServerResponse, error: unknown) { + if (response.destroyed) + return + if (response.headersSent) { + response.destroy() + return + } + const safe = privateNetworkErrorSchema.shape.error.safeParse(error && typeof error === 'object' && 'code' in error ? error.code : undefined) + const code = safe.success ? safe.data : error instanceof z.ZodError ? 'INVALID_REQUEST' : 'STORAGE_UNAVAILABLE' + response.statusCode = code === 'NOT_AUTHORIZED' ? 401 : code === 'LIMIT_EXCEEDED' ? 429 : code === 'STORAGE_UNAVAILABLE' ? 503 : code === 'VERSION_CHANGED' ? 412 : code === 'RANGE_INVALID' ? 416 : code === 'UPLOAD_CONFLICT' ? 409 : code === 'RESOURCE_UNAVAILABLE' ? 404 : 400 + this.#json(response, { error: code }) + } +} + +function bearer(request: IncomingMessage): string { + const authorization = request.headers.authorization + if (typeof authorization !== 'string' || !/^Bearer [\w.-]{1,256}$/.test(authorization)) + throw new PrivateNetworkError('NOT_AUTHORIZED') + return authorization.slice(7) +} +async function readJson(request: IncomingMessage): Promise { + if (request.headers['content-type']?.split(';')[0] !== 'application/json' || Number(request.headers['content-length'] ?? 0) > limits.requestBytes) + throw new PrivateNetworkError('INVALID_REQUEST') + const chunks: Buffer[] = [] + let bytes = 0 + for await (const part of request) { + const chunk = Buffer.isBuffer(part) ? part : Buffer.from(part) + bytes += chunk.byteLength + if (bytes > limits.requestBytes) + throw new PrivateNetworkError('LIMIT_EXCEEDED') + chunks.push(chunk) + } + try { + return JSON.parse(Buffer.concat(chunks).toString('utf8')) + } + catch { throw new PrivateNetworkError('INVALID_REQUEST') } +} diff --git a/apps/buddy/service/src/privateNetwork/PrivateNetworkTlsIdentity.ts b/apps/buddy/service/src/privateNetwork/PrivateNetworkTlsIdentity.ts new file mode 100644 index 000000000..9756d034d --- /dev/null +++ b/apps/buddy/service/src/privateNetwork/PrivateNetworkTlsIdentity.ts @@ -0,0 +1,83 @@ +import type { webcrypto } from 'node:crypto' +import type { DatabaseSync } from 'node:sqlite' +import type { RuntimePlatform } from '../platform/RuntimePlatform' +import { Buffer } from 'node:buffer' +import { createHash, createPrivateKey, createPublicKey, randomBytes, X509Certificate } from 'node:crypto' +import { isIP } from 'node:net' +import { z } from 'zod' +import { PRIVATE_NETWORK_LIMITS } from '../../../shared/privateNetwork/limits' +import { PrivateNetworkError, privateNetworkPinSchema } from '../../../shared/privateNetwork/pairing' +import { withTransaction } from '../storage/transaction' +import 'reflect-metadata' + +const storedIdentitySchema = z.object({ version: z.literal(1), profileId: z.string().min(1), privateKey: z.string().min(1).max(16384), certificate: z.string().min(1).max(16384), spkiPin: privateNetworkPinSchema }).strict() +export type PrivateNetworkTlsIdentity = z.infer + +export async function loadPrivateNetworkTlsIdentity(options: { database: DatabaseSync, profileId: string, secrets: NonNullable, addresses: string[], reset?: boolean }): Promise { + const { ExtendedKeyUsage, ExtendedKeyUsageExtension, KeyUsageFlags, KeyUsagesExtension, SubjectAlternativeNameExtension, X509CertificateGenerator } = await import('@peculiar/x509') + const { database, profileId, secrets } = options + const addresses = [...new Set(options.addresses)] + if (!addresses.length || addresses.some(address => !isPrivateNetworkAddress(address))) + throw new PrivateNetworkError('INVALID_REQUEST') + const marker = database.prepare('SELECT tls_pin FROM private_network_state WHERE id = 1').get()?.tls_pin + const stored = await secrets.read(`tls:${profileId}`) + let identity = stored === null ? null : storedIdentitySchema.parse(stored) + if (options.reset) { + const active = database.prepare('SELECT 1 FROM remote_devices WHERE state IN (\'pending\',\'active\') LIMIT 1').get() + if (active) + throw new PrivateNetworkError('NOT_AUTHORIZED') + identity = null + } + else if ((!identity && marker) || (identity && (identity.profileId !== profileId || (marker && identity.spkiPin !== marker)))) { + throw new PrivateNetworkError('TLS_IDENTITY_UNAVAILABLE') + } + let keys: webcrypto.CryptoKeyPair + if (identity) { + const privateKey = createPrivateKey(identity.privateKey) + const publicKey = createPublicKey(privateKey) + const publicBytes = publicKey.export({ type: 'spki', format: 'der' }) + const certificate = new X509Certificate(identity.certificate) + const pin = `sha256/${createHash('sha256').update(publicBytes).digest('base64')}` + if (pin !== identity.spkiPin || !certificate.publicKey.export({ type: 'spki', format: 'der' }).equals(publicBytes)) + throw new PrivateNetworkError('TLS_IDENTITY_UNAVAILABLE') + const now = Date.now() + if (Date.parse(certificate.validFrom) <= now && Date.parse(certificate.validTo) > now + 7 * 24 * 60 * 60 * 1000 && addresses.every(address => certificate.checkIP(address))) { + withTransaction(database, () => database.prepare('UPDATE private_network_state SET tls_pin = ? WHERE id = 1').run(pin)) + return identity + } + keys = { + privateKey: await crypto.subtle.importKey('pkcs8', new Uint8Array(privateKey.export({ type: 'pkcs8', format: 'der' })), { name: 'ECDSA', namedCurve: 'P-256' }, true, ['sign']), + publicKey: await crypto.subtle.importKey('spki', new Uint8Array(publicBytes), { name: 'ECDSA', namedCurve: 'P-256' }, true, ['verify']), + } + } + else { + keys = await crypto.subtle.generateKey({ name: 'ECDSA', namedCurve: 'P-256' }, true, ['sign', 'verify']) + } + const now = Date.now() + const certificate = await X509CertificateGenerator.createSelfSigned({ + serialNumber: randomBytes(16).toString('hex'), + name: 'CN=Lexora Private Service', + notBefore: new Date(now - 300_000), + notAfter: new Date(now + PRIVATE_NETWORK_LIMITS.tlsCertificateDays * 24 * 60 * 60 * 1000), + signingAlgorithm: { name: 'ECDSA', hash: 'SHA-256' }, + keys, + extensions: [new KeyUsagesExtension(KeyUsageFlags.digitalSignature, true), new ExtendedKeyUsageExtension([ExtendedKeyUsage.serverAuth]), new SubjectAlternativeNameExtension(addresses.map(value => ({ type: 'ip', value })))], + }) + const privateBytes = Buffer.from(await crypto.subtle.exportKey('pkcs8', keys.privateKey)) + const publicBytes = Buffer.from(await crypto.subtle.exportKey('spki', keys.publicKey)) + const result = storedIdentitySchema.parse({ version: 1, profileId, privateKey: createPrivateKey({ key: privateBytes, format: 'der', type: 'pkcs8' }).export({ type: 'pkcs8', format: 'pem' }).toString(), certificate: certificate.toString('pem'), spkiPin: `sha256/${createHash('sha256').update(publicBytes).digest('base64')}` }) + await secrets.write(`tls:${profileId}`, result) + withTransaction(database, () => database.prepare('UPDATE private_network_state SET tls_pin = ? WHERE id = 1').run(result.spkiPin)) + return result +} + +export function isPrivateNetworkAddress(address: string): boolean { + const family = isIP(address) + if (family === 4) { + const [a, b] = address.split('.').map(Number) + return a === 10 || a === 127 || (a === 172 && b! >= 16 && b! <= 31) || (a === 192 && b === 168) || (a === 100 && b! >= 64 && b! <= 127) + } + if (family === 6) + return address === '::1' || /^f[cd][\da-f]{2}:/i.test(address) + return false +} diff --git a/apps/buddy/service/src/privateNetwork/RemoteAvatarService.ts b/apps/buddy/service/src/privateNetwork/RemoteAvatarService.ts new file mode 100644 index 000000000..c743eb3c4 --- /dev/null +++ b/apps/buddy/service/src/privateNetwork/RemoteAvatarService.ts @@ -0,0 +1,35 @@ +import type { IncomingMessage, ServerResponse } from 'node:http' +import type { DatabaseSync } from 'node:sqlite' +import type { TrustedCommandPrincipal } from '../commands/CommandAdmissionService' +import { PrivateNetworkError } from '../../../shared/privateNetwork/pairing' +import { productIdSchema } from '../../../shared/productProtocol/scalars' +import { ProductBuddyAvatars } from '../productProtocol/ProductBuddyAvatars' + +export class RemoteAvatarService { + readonly #avatars: ProductBuddyAvatars + constructor(database: DatabaseSync) { this.#avatars = new ProductBuddyAvatars(database) } + + bytes(request: IncomingMessage, response: ServerResponse, principal: TrustedCommandPrincipal): boolean { + const match = /^\/v1\/avatars\/([^/]+)\/([a-f0-9]{64})$/.exec(request.url ?? '') + if (!match) + return false + if (request.method !== 'GET' && request.method !== 'HEAD') + throw new PrivateNetworkError('INVALID_REQUEST') + let buddyId: string + try { + buddyId = productIdSchema.parse(decodeURIComponent(match[1]!)) + } + catch { throw new PrivateNetworkError('INVALID_REQUEST') } + principal.assertAuthenticated() + const { avatar, bytes } = this.#avatars.read(buddyId) + if (avatar.kind !== 'image' || !bytes) + throw new PrivateNetworkError('RESOURCE_UNAVAILABLE') + if (avatar.sha256 !== match[2]) + throw new PrivateNetworkError('VERSION_CHANGED') + response.setHeader('Content-Type', avatar.mimeType) + response.setHeader('Content-Length', bytes.length) + response.setHeader('ETag', `"${avatar.sha256}"`) + response.end(request.method === 'HEAD' ? undefined : bytes) + return true + } +} diff --git a/apps/buddy/service/src/privateNetwork/RemoteResourceService.ts b/apps/buddy/service/src/privateNetwork/RemoteResourceService.ts new file mode 100644 index 000000000..6b8f48a85 --- /dev/null +++ b/apps/buddy/service/src/privateNetwork/RemoteResourceService.ts @@ -0,0 +1,357 @@ +import type { Buffer } from 'node:buffer' +import type { IncomingMessage, ServerResponse } from 'node:http' +import type { DatabaseSync } from 'node:sqlite' +import type { z } from 'zod' +import type { resourcePrepareSchema } from '../../../shared/privateNetwork/resources' +import type { ArtifactService } from '../artifacts/ArtifactService' +import type { TrustedCommandPrincipal } from '../commands/CommandAdmissionService' +import type { ProductReadViews } from '../productProtocol/ProductReadViews' +import { createHash, randomUUID } from 'node:crypto' +import { constants } from 'node:fs' +import { mkdir, open, stat, statfs, writeFile } from 'node:fs/promises' +import { dirname, join } from 'node:path' +import { pipeline } from 'node:stream/promises' +import { fileStorage } from '../../../platform/filesystem/fileStorage' +import { PRIVATE_NETWORK_LIMITS as limits } from '../../../shared/privateNetwork/limits' +import { PrivateNetworkError } from '../../../shared/privateNetwork/pairing' +import { resourceDownloadSchema } from '../../../shared/privateNetwork/resources' +import { createAttachmentPersistenceRepository } from '../storage/attachmentPersistenceRepository' +import { createAttachmentRepository } from '../storage/attachmentRepository' +import { withTransaction } from '../storage/transaction' + +interface ExportRow { + id: string + actor_id: string + source_id: string + source_version: string + file_id: string + path: string + temporary_path: string | null + size_bytes: number + sha256: string | null + name: string + mime_type: string + state: 'preparing' | 'ready' | 'expired' | 'failed' + expires_ms: number +} +interface Options { + database: DatabaseSync + root: string + instanceId: string + views: (actorId: string) => ProductReadViews + artifacts: Pick +} +export class RemoteResourceService { + readonly #options: Options + readonly #files: ReturnType + readonly #active = new Map() + readonly #operations = new Set>() + readonly #reviews = new Map() + readonly #controllers = new Set() + readonly #timer: ReturnType + #preparing = 0 + #streams = 0 + #closed = false + + constructor(options: Options) { + this.#options = options + this.#files = createAttachmentPersistenceRepository(options.database) + options.database.prepare('UPDATE remote_exports SET state = \'failed\' WHERE state = \'preparing\'').run() + this.sweep() + this.#timer = setInterval(() => { + try { + this.sweep() + } + catch {} + }, 1000) + this.#timer.unref() + } + + hasReview(actorId: string, approvalId: string, version: string): boolean { + return (this.#reviews.get(`${actorId}:review:${approvalId}:${version}`) ?? 0) > this.#now() + } + + prepare(input: z.infer, principal: TrustedCommandPrincipal) { + if (this.#closed || this.#preparing >= limits.resourceStreams) + return Promise.reject(new PrivateNetworkError('LIMIT_EXCEEDED')) + this.#preparing++ + const operation = this.#prepare(input, principal).finally(() => { + this.#preparing-- + this.#operations.delete(operation) + }) + this.#operations.add(operation) + return operation + } + + bytes(request: IncomingMessage, response: ServerResponse, principal: TrustedCommandPrincipal): Promise { + const operation = this.#bytes(request, response, principal).finally(() => this.#operations.delete(operation)) + this.#operations.add(operation) + return operation + } + + async #bytes(request: IncomingMessage, response: ServerResponse, principal: TrustedCommandPrincipal): Promise { + const match = /^\/v1\/resources\/([\w-]{1,128})$/.exec(request.url ?? '') + if (!match) + return false + if (!['GET', 'HEAD'].includes(request.method ?? '')) + throw new PrivateNetworkError('INVALID_REQUEST') + if (this.#closed || this.#streams >= limits.resourceStreams) + throw new PrivateNetworkError('LIMIT_EXCEEDED') + const id = match[1]! + const row = this.#row(id) + this.#authorize(row, principal) + if (row.state !== 'ready' || row.expires_ms <= this.#now() || !row.sha256) + throw new PrivateNetworkError('RESOURCE_UNAVAILABLE') + if (request.headers['if-match'] !== `"${row.sha256}"`) + throw new PrivateNetworkError('VERSION_CHANGED') + let start = 0 + let end = row.size_bytes - 1 + if (request.headers.range) { + const range = /^bytes=(0|[1-9]\d*)-(0|[1-9]\d*)?$/.exec(request.headers.range) + if (!range) + throw new PrivateNetworkError('RANGE_INVALID') + start = Number(range[1]) + end = range[2] === undefined ? end : Number(range[2]) + if (![start, end].every(Number.isSafeInteger) || start > end || end >= row.size_bytes) + throw new PrivateNetworkError('RANGE_INVALID') + } + this.#streams++ + this.#active.set(id, (this.#active.get(id) ?? 0) + 1) + const controller = new AbortController() + this.#controllers.add(controller) + const timeout = setTimeout(() => controller.abort(), 60_000) + const disconnect = () => { + if (!response.writableFinished) + controller.abort() + } + response.once('close', disconnect) + const authorization = setInterval(() => { + try { + this.#authorize(row, principal) + } + catch { controller.abort() } + }, 250) + let source: Awaited> | undefined + try { + if (row.source_id.startsWith('artifact:')) + await this.#options.artifacts.locate(row.source_id.slice('artifact:'.length)) + source = await open(row.path, constants.O_RDONLY | constants.O_NOFOLLOW) + const before = await source.stat() + if (!before.isFile() || before.size !== row.size_bytes) + throw new PrivateNetworkError('RESOURCE_UNAVAILABLE') + const hash = createHash('sha256') + for await (const bytes of source.createReadStream({ autoClose: false, start: 0, signal: controller.signal })) hash.update(bytes) + if (hash.digest('hex') !== row.sha256) { + this.#files.unavailable(row.path) + throw new PrivateNetworkError('VERSION_CHANGED') + } + this.#authorize(row, principal) + response.setHeader('Content-Type', row.mime_type) + response.setHeader('Content-Disposition', `attachment; filename="resource"; filename*=UTF-8''${encodeURIComponent(row.name)}`) + response.setHeader('ETag', `"${row.sha256}"`) + response.setHeader('Accept-Ranges', 'bytes') + response.setHeader('Content-Length', Math.max(0, end - start + 1)) + if (request.headers.range) { + response.statusCode = 206 + response.setHeader('Content-Range', `bytes ${start}-${end}/${row.size_bytes}`) + } + if (request.method === 'HEAD' || !row.size_bytes) + response.end() + else await pipeline(source.createReadStream({ autoClose: false, start, end }), response, { signal: controller.signal }) + if (request.method === 'GET' && start === 0 && end === row.size_bytes - 1 && row.source_id.startsWith('review:')) { + if (this.#reviews.size >= 128) + this.#reviews.delete(this.#reviews.keys().next().value!) + this.#reviews.set(`${principal.actorId}:${row.source_id}`, row.expires_ms) + } + } + finally { + await source?.close() + this.#controllers.delete(controller) + clearTimeout(timeout) + clearInterval(authorization) + response.off('close', disconnect) + this.#streams-- + const remaining = this.#active.get(id)! - 1 + if (remaining) + this.#active.set(id, remaining) + else this.#active.delete(id) + this.sweep() + } + return true + } + + sweep(): void { + const { database } = this.#options + withTransaction(database, () => { + database.prepare(`UPDATE remote_exports SET state = 'expired' WHERE state IN ('preparing','ready') AND + (expires_ms <= ? OR data_generation <> (SELECT data_generation FROM storage_profile WHERE id = 1) + OR NOT EXISTS (SELECT 1 FROM storage_actors a WHERE a.id = actor_id AND a.state = 'active') + OR EXISTS (SELECT 1 FROM remote_devices d WHERE d.id = actor_id AND (d.state <> 'active' OR d.grant_generation <> remote_exports.grant_generation)))`).run(this.#now()) + for (const entry of database.prepare(`SELECT id FROM remote_exports e WHERE state IN ('expired','failed') AND ( + EXISTS (SELECT 1 FROM file_references f WHERE f.kind = 'export' AND f.owner_id = e.id) + OR EXISTS (SELECT 1 FROM file_preparations p WHERE p.id = e.file_id AND p.state IN ('preparing','published'))) LIMIT 128`).all()) { + const id = String(entry.id) + if (this.#active.has(id)) + continue + const row = this.#row(id) + this.#files.releaseReference({ kind: 'export', ownerId: id, bindingId: id }) + this.#files.settle([row.path], 'rollback') + } + }) + for (const [id, expires] of this.#reviews) { + if (expires <= this.#now()) + this.#reviews.delete(id) + } + } + + async close(): Promise { + this.#closed = true + clearInterval(this.#timer) + for (const controller of this.#controllers) controller.abort() + await Promise.allSettled(this.#operations) + } + + async #prepare(input: z.infer, principal: TrustedCommandPrincipal) { + this.sweep() + principal.assertAuthenticated() + const { database } = this.#options + const views = this.#options.views(principal.actorId) + const resource = views.resource(input.resourceId) + if (!resource || resource.availability === 'unavailable') + throw new PrivateNetworkError('RESOURCE_UNAVAILABLE') + if (resource.revision !== input.expectedVersion) + throw new PrivateNetworkError('VERSION_CHANGED') + const size = Number(resource.sizeBytes) + if (!Number.isSafeInteger(size) || size > limits.uploadBytes) + throw new PrivateNetworkError('LIMIT_EXCEEDED') + const directory = join(this.#options.root, 'managed-files', 'exports') + await mkdir(directory, { recursive: true, mode: 0o700 }) + const capacity = await statfs(directory) + const available = Math.min(Number.MAX_SAFE_INTEGER, capacity.bavail * capacity.bsize) + const id = randomUUID() + const isAttachment = input.resourceId.startsWith('attachment:') + const selected = isAttachment ? createAttachmentRepository(database).findVisibleById(input.resourceId.slice('attachment:'.length)) : null + let sourcePath: string | undefined + let document: ReturnType + if (input.resourceId.startsWith('artifact:')) { + const location = await this.#options.artifacts.locate(input.resourceId.slice('artifact:'.length)) + if (location.kind !== 'file') + throw new PrivateNetworkError('RESOURCE_UNAVAILABLE') + sourcePath = location.path + } + else if (!isAttachment) { + document = views.document(input.resourceId) + if (!document || document.revision !== input.expectedVersion || document.bytes.length !== size) + throw new PrivateNetworkError('VERSION_CHANGED') + } + const row = withTransaction(database, () => { + principal.assertAuthenticated() + if (views.resource(input.resourceId)?.revision !== input.expectedVersion) + throw new PrivateNetworkError('VERSION_CHANGED') + const totals = database.prepare('SELECT COALESCE(SUM(size_bytes),0) AS bytes, COALESCE(SUM(actor_id = ?),0) AS count FROM remote_exports WHERE state IN (\'preparing\',\'ready\')').get(principal.actorId)! + if (Number(totals.count) >= limits.resourceStreams || Number(totals.bytes) + size > limits.uploadReservedBytes) + throw new PrivateNetworkError('LIMIT_EXCEEDED') + const file = selected ? this.#files.get(selected.storedPath)! : this.#files.prepare({ actorId: principal.actorId, operationId: id, availableBytes: available, stagingDirectory: join(directory, 'staging'), ownerInstance: this.#options.instanceId, kind: 'export', targetOwnerId: id, path: join(directory, id), sizeBytes: size }) + if (selected && (!file || file.state !== 'ready' || file.sha256 !== input.expectedVersion)) + throw new PrivateNetworkError('RESOURCE_UNAVAILABLE') + const generation = database.prepare('SELECT data_generation FROM storage_profile WHERE id = 1').get()!.data_generation + const grant = Number(database.prepare('SELECT grant_generation FROM remote_devices WHERE id = ?').get(principal.actorId)?.grant_generation ?? 0) + database.prepare(`INSERT INTO remote_exports (id,actor_id,data_generation,grant_generation,source_id,source_version,file_id,name,mime_type,size_bytes,state,expires_ms) + VALUES (?,?,?,?,?,?,?,?,?,?,?,?)`).run(id, principal.actorId, generation, grant, input.resourceId, input.expectedVersion, file.id, resource.name, resource.mimeType, size, selected ? 'ready' : 'preparing', this.#now() + limits.resourceLifetimeMs) + if (selected) + this.#files.reference(file.id, { kind: 'export', ownerId: id, bindingId: id }) + return this.#row(id) + }) + this.#active.set(id, 1) + try { + if (!selected) { + await mkdir(dirname(row.temporary_path!), { recursive: true, mode: 0o700 }) + const digest = sourcePath ? await snapshotFile(sourcePath, row.temporary_path!, size, () => principal.assertAuthenticated()) : await snapshotDocument(document!.bytes, row.temporary_path!) + principal.assertAuthenticated() + if (sourcePath && (await this.#options.artifacts.locate(input.resourceId.slice('artifact:'.length))).path !== sourcePath) + throw new PrivateNetworkError('VERSION_CHANGED') + await fileStorage.replace(row.temporary_path!, row.path) + await fileStorage.syncDirectory(dirname(row.path)) + await fileStorage.syncDirectory(dirname(row.temporary_path!)) + withTransaction(database, () => { + this.#authorize(row, principal) + if (this.#row(id).state !== 'preparing' || views.resource(input.resourceId)?.revision !== input.expectedVersion) + throw new PrivateNetworkError('VERSION_CHANGED') + this.#files.publish(row.path, { sizeBytes: size, sha256: digest, durability: fileStorage.durability }) + this.#files.reference(row.file_id, { kind: 'export', ownerId: id, bindingId: id }) + this.#files.settle([row.path], 'commit') + database.prepare('UPDATE remote_exports SET state = \'ready\' WHERE id = ?').run(id) + }) + } + const current = this.#row(id) + return resourceDownloadSchema.parse({ downloadId: id, resourceId: current.source_id, contentVersion: current.sha256, sizeBytes: String(size), mimeType: current.mime_type, name: current.name, expiresAt: new Date(current.expires_ms).toISOString() }) + } + catch (error) { + withTransaction(database, () => database.prepare('UPDATE remote_exports SET state = \'failed\' WHERE id = ? AND state = \'preparing\'').run(id)) + throw error + } + finally { + this.#active.delete(id) + this.sweep() + } + } + + #authorize(row: ExportRow, principal: TrustedCommandPrincipal) { + principal.assertAuthenticated() + if (row.actor_id !== principal.actorId) + throw new PrivateNetworkError('NOT_AUTHORIZED') + if (row.expires_ms <= this.#now()) + throw new PrivateNetworkError('RESOURCE_UNAVAILABLE') + if (!this.#options.views(principal.actorId).resource(row.source_id)) + throw new PrivateNetworkError('RESOURCE_UNAVAILABLE') + } + + #row(id: string): ExportRow { + const row = this.#options.database.prepare('SELECT e.*, f.path, f.temporary_path, f.sha256 FROM remote_exports e JOIN managed_files f ON f.id = e.file_id WHERE e.id = ?').get(id) + if (!row) + throw new PrivateNetworkError('RESOURCE_UNAVAILABLE') + return row as unknown as ExportRow + } + + #now() { return Math.max(Date.now(), Number(this.#options.database.prepare('SELECT last_observed_ms FROM private_network_state WHERE id = 1').get()!.last_observed_ms)) } +} + +async function snapshotDocument(bytes: Buffer, path: string) { + await writeFile(path, bytes, { flag: 'wx', mode: 0o600, flush: true }) + return createHash('sha256').update(bytes).digest('hex') +} +async function snapshotFile(source: string, target: string, size: number, assertCurrent: () => void) { + const input = await open(source, constants.O_RDONLY | constants.O_NOFOLLOW) + try { + const before = await input.stat() + if (!before.isFile() || before.size !== size) + throw new PrivateNetworkError('VERSION_CHANGED') + const output = await open(target, 'wx', 0o600) + const hash = createHash('sha256') + let position = 0 + try { + for await (const chunk of input.createReadStream({ autoClose: false })) { + assertCurrent() + if (position + chunk.length > size) + throw new PrivateNetworkError('VERSION_CHANGED') + hash.update(chunk) + let written = 0 + while (written < chunk.length) { + const result = await output.write(chunk, written, chunk.length - written, position + written) + if (!result.bytesWritten) + throw new PrivateNetworkError('STORAGE_UNAVAILABLE') + written += result.bytesWritten + } + position += chunk.length + } + const after = await input.stat() + const current = await stat(source) + if (position !== size || after.mtimeMs !== before.mtimeMs || current.ino !== before.ino || current.dev !== before.dev || current.mtimeMs !== before.mtimeMs) + throw new PrivateNetworkError('VERSION_CHANGED') + await output.sync() + } + finally { await output.close() } + return hash.digest('hex') + } + finally { await input.close() } +} diff --git a/apps/buddy/service/src/privateNetwork/RemoteUploadService.ts b/apps/buddy/service/src/privateNetwork/RemoteUploadService.ts new file mode 100644 index 000000000..fab9941c1 --- /dev/null +++ b/apps/buddy/service/src/privateNetwork/RemoteUploadService.ts @@ -0,0 +1,437 @@ +import type { IncomingMessage, ServerResponse } from 'node:http' +import type { DatabaseSync } from 'node:sqlite' +import type { SubmittedInput } from '../../../shared/conversation/submittedInput' +import type { UploadCommandMethod } from '../../../shared/privateNetwork/uploads' +import type { ComposerDraftService } from '../chat/ComposerDraftService' +import type { CommandAdmissionService, TrustedCommandPrincipal } from '../commands/CommandAdmissionService' +import type { InputCommandIntent } from '../commands/commandIntent' +import type { CommandDecision } from '../storage/CommandCommitter' +import { Buffer } from 'node:buffer' +import { createHash, randomUUID } from 'node:crypto' +import { createReadStream } from 'node:fs' +import { mkdir, open, readFile, stat, statfs } from 'node:fs/promises' +import { dirname, join } from 'node:path' +import { fileStorage } from '../../../platform/filesystem/fileStorage' +import { isDocumentMimeType } from '../../../shared/conversation/attachmentFormats' +import { BUDDY_ATTACHMENT_COUNT_LIMIT, BUDDY_ATTACHMENT_TOTAL_BYTES_LIMIT } from '../../../shared/conversation/attachmentPolicy' +import { getBuddyUserContentResourceIds } from '../../../shared/conversation/buddyUserContent' +import { PRIVATE_NETWORK_LIMITS as limits } from '../../../shared/privateNetwork/limits' +import { PrivateNetworkError } from '../../../shared/privateNetwork/pairing' +import { uploadDigestSchema, uploadStateSchema } from '../../../shared/privateNetwork/uploads' +import { ProductProtocolError } from '../../../shared/productProtocol/errors' +import { validateResourceBytes } from '../attachments/validateResourceBytes' +import { inputResourceOwner } from '../chat/resolveChatInput' +import { createAttachmentPersistenceRepository } from '../storage/attachmentPersistenceRepository' +import { createAttachmentRepository } from '../storage/attachmentRepository' +import { findPersonalComposerDraft } from '../storage/composerDraftRepository' +import { createComposerResourceRepository } from '../storage/composerResourceRepository' +import { afterTransactionCommit, withTransaction } from '../storage/transaction' +import { validateDocumentBytes } from './uploadValidation' + +interface UploadRow { + id: string + actor_id: string + data_generation: string + grant_generation: number + input_id: string | null + draft_id: string + expected_revision: number + file_id: string + path: string + temporary_path: string + name: string + mime_type: string + size_bytes: number + sha256: string + state: 'receiving' | 'ready' | 'adopted' | 'cancelled' | 'expired' | 'failed' + confirmed_bytes: number + resource_id: string + attachment_id: string + expires_ms: number +} +interface Options { database: DatabaseSync, root: string, instanceId: string, drafts: Pick } +export class RemoteUploadService { + readonly #options: Options + readonly #files: ReturnType + readonly #busy = new Map>() + readonly #verified = new Set() + readonly #timer: ReturnType + #closed = false + + constructor(options: Options) { + this.#options = options + this.#files = createAttachmentPersistenceRepository(options.database) + this.sweep() + this.#timer = setInterval(() => { + try { + this.sweep() + } + catch {} + }, 1000) + this.#timer.unref() + } + + commands(principal: TrustedCommandPrincipal, admission: CommandAdmissionService) { + return { + execute: (method: UploadCommandMethod, input: unknown) => admission.execute(method, input, { + scope: (intent, operation) => { + const run = () => 'uploadId' in intent ? this.#exclusive(intent.uploadId, operation) : operation() + return 'draftId' in intent ? this.#options.drafts.withDraftLock(intent.draftId, run) : run() + }, + prepare: async intent => this.#prepare(method, intent, principal), + commit: prepared => prepared(), + reject: error => error instanceof ProductProtocolError && ['DRAFT_CONFLICT', 'RESOURCE_UNAVAILABLE'].includes(error.code) + ? { errorCode: error.code as 'DRAFT_CONFLICT' | 'RESOURCE_UNAVAILABLE', target: {}, result: {} } + : null, + }), + get: async (uploadId: string) => this.#exclusive(uploadId, async () => { + const row = this.#owned(uploadId, principal) + if (row.state === 'receiving') + await this.#recover(row) + return this.#view(this.#owned(uploadId, principal)) + }), + } + } + + async bytes(request: IncomingMessage, response: ServerResponse, principal: TrustedCommandPrincipal): Promise { + const match = /^\/v1\/uploads\/([\w-]{1,128})$/.exec(request.url ?? '') + if (!match) + return false + if (request.method !== 'PUT') + throw new PrivateNetworkError('INVALID_REQUEST') + const id = match[1]! + await this.#exclusive(id, async () => { + const row = this.#owned(id, principal) + if (row.state !== 'receiving') + throw new PrivateNetworkError('UPLOAD_CONFLICT') + const range = /^bytes (0|[1-9]\d*)-(0|[1-9]\d*)\/(0|[1-9]\d*)$/.exec(String(request.headers['content-range'] ?? '')) + const digest = uploadDigestSchema.safeParse(request.headers['x-content-sha256']) + if (!range || !digest.success) + throw new PrivateNetworkError('INVALID_REQUEST') + const [start, end, total] = range.slice(1).map(Number) as [number, number, number] + const length = end - start + 1 + if (![start, end, total].every(Number.isSafeInteger) || total !== row.size_bytes || start < 0 || end >= total || length <= 0 || length > limits.uploadChunkBytes || String(length) !== request.headers['content-length']) + throw new PrivateNetworkError('RANGE_INVALID') + if (start > row.confirmed_bytes) + throw new PrivateNetworkError('UPLOAD_CONFLICT') + const bytes = await readChunk(request, length) + if (createHash('sha256').update(bytes).digest('hex') !== digest.data) + throw new PrivateNetworkError('UPLOAD_CONFLICT') + await this.#recover(row) + this.#owned(id, principal) + if (start < row.confirmed_bytes) { + const old = this.#options.database.prepare('SELECT length, sha256 FROM remote_upload_chunks WHERE upload_id = ? AND start = ?').get(id, start) + if (old?.length !== length || old.sha256 !== digest.data) + throw new PrivateNetworkError('UPLOAD_CONFLICT') + } + else { + const file = await open(row.temporary_path, 'r+') + try { + let written = 0 + while (written < bytes.length) { + const result = await file.write(bytes, written, bytes.length - written, start + written) + if (!result.bytesWritten) + throw new PrivateNetworkError('STORAGE_UNAVAILABLE') + written += result.bytesWritten + } + await file.sync() + } + finally { await file.close() } + withTransaction(this.#options.database, () => { + if (this.#owned(id, principal).state !== 'receiving') + throw new PrivateNetworkError('UPLOAD_CONFLICT') + this.#options.database.prepare('INSERT INTO remote_upload_chunks (upload_id, start, length, sha256) VALUES (?, ?, ?, ?)').run(id, start, length, digest.data) + this.#options.database.prepare('UPDATE remote_uploads SET confirmed_bytes = ? WHERE id = ? AND confirmed_bytes = ?').run(end + 1, id, start) + }) + } + principal.assertAuthenticated() + response.setHeader('Content-Type', 'application/json') + response.end(JSON.stringify({ uploadId: id, confirmedBytes: this.#row(id).confirmed_bytes })) + }) + return true + } + + sweep(): void { + const { database } = this.#options + withTransaction(database, () => { + database.prepare(`UPDATE remote_uploads SET state = CASE WHEN expires_ms <= ? THEN 'expired' ELSE 'cancelled' END + WHERE state IN ('receiving', 'ready') AND (expires_ms <= ? + OR data_generation <> (SELECT data_generation FROM storage_profile WHERE id = 1) + OR NOT EXISTS (SELECT 1 FROM storage_actors a WHERE a.id = actor_id AND a.state = 'active') + OR EXISTS (SELECT 1 FROM remote_devices d WHERE d.id = actor_id AND (d.state <> 'active' OR d.grant_generation <> remote_uploads.grant_generation)))`).run(this.#now(), this.#now()) + for (const row of database.prepare('SELECT id FROM remote_uploads u WHERE state IN (\'cancelled\',\'expired\',\'failed\') AND (EXISTS (SELECT 1 FROM file_preparations p WHERE p.id = u.file_id AND p.state IN (\'preparing\',\'published\')) OR EXISTS (SELECT 1 FROM file_references f WHERE f.kind = \'export\' AND f.owner_id = u.id)) LIMIT 128').all()) { + if (!this.#busy.has(String(row.id))) + this.#release(this.#row(String(row.id))) + } + }) + } + + async close(): Promise { + this.#closed = true + clearInterval(this.#timer) + await Promise.allSettled(this.#busy.values()) + } + + async #prepare(method: UploadCommandMethod, input: InputCommandIntent, principal: TrustedCommandPrincipal): Promise<() => CommandDecision> { + principal.assertAuthenticated() + const { database } = this.#options + if (method === 'uploads.create' || method === 'uploads.stage') { + const source = input as InputCommandIntent<'uploads.create' | 'uploads.stage'> + const inputId = 'inputId' in source ? source.inputId : null + const intent = 'draftId' in source ? source : { draftId: inputResourceOwner(principal.actorId, source.inputId), expectedRevision: 0, file: source.file } + if (!inputId) + this.#draft(intent.draftId, intent.expectedRevision, principal) + const directory = join(this.#options.root, 'managed-files', 'remote') + await mkdir(directory, { recursive: true, mode: 0o700 }) + const capacity = await statfs(directory) + const available = Math.min(Number.MAX_SAFE_INTEGER, capacity.bavail * capacity.bsize) + return (): CommandDecision => { + this.sweep() + if (!inputId) + this.#draft(intent.draftId, intent.expectedRevision, principal) + const totals = database.prepare('SELECT COALESCE(SUM(size_bytes),0) AS bytes, COALESCE(SUM(actor_id = ? AND (state = \'receiving\' OR input_id IS NULL)),0) AS count FROM remote_uploads WHERE state IN (\'receiving\',\'ready\')').get(principal.actorId)! + if (Number(totals.bytes) + intent.file.sizeBytes > limits.uploadReservedBytes || Number(totals.count) >= limits.uploadsPerDevice) + throw new PrivateNetworkError('LIMIT_EXCEEDED') + const id = randomUUID() + const file = this.#files.prepare({ actorId: principal.actorId, operationId: id, availableBytes: available, stagingDirectory: join(directory, 'staging'), ownerInstance: this.#options.instanceId, kind: 'import', targetOwnerId: intent.draftId, path: join(directory, id), sizeBytes: intent.file.sizeBytes }) + const generation = database.prepare('SELECT data_generation FROM storage_profile WHERE id = 1').get()!.data_generation + const grant = Number(database.prepare('SELECT grant_generation FROM remote_devices WHERE id = ?').get(principal.actorId)?.grant_generation ?? 0) + database.prepare(`INSERT INTO remote_uploads (id,actor_id,data_generation,grant_generation,draft_id,expected_revision,file_id,name,mime_type,size_bytes,sha256,state,resource_id,attachment_id,created_ms,expires_ms,input_id) + VALUES (?,?,?,?,?,?,?,?,?,?,?,'receiving',?,?,?,?,?)`).run(id, principal.actorId, generation, grant, intent.draftId, intent.expectedRevision, file.id, intent.file.name, intent.file.mimeType, intent.file.sizeBytes, intent.file.sha256, randomUUID(), randomUUID(), this.#now(), this.#now() + limits.uploadLifetimeMs, inputId) + return { target: { uploadId: id, draftId: intent.draftId }, result: { uploadId: id } } + } + } + const intent = input as InputCommandIntent<'uploads.complete' | 'uploads.cancel' | 'uploads.adopt'> + const row = this.#owned(intent.uploadId, principal) + if (method === 'uploads.cancel') { + return (): CommandDecision => { + const current = this.#owned(row.id, principal) + if (current.state === 'receiving' || current.state === 'ready') + database.prepare('UPDATE remote_uploads SET state = \'cancelled\' WHERE id = ?').run(row.id) + return { target: { uploadId: row.id }, result: { cancelled: current.state !== 'adopted' } } + } + } + if (method === 'uploads.complete') { + if (row.state === 'receiving') { + if (row.confirmed_bytes !== row.size_bytes) + throw new PrivateNetworkError('UPLOAD_CONFLICT') + await this.#recover(row) + const path = await exists(row.path) ? row.path : row.temporary_path + if (await hashFile(path) !== row.sha256) { + this.#fail(row.id) + throw new ProductProtocolError('RESOURCE_UNAVAILABLE') + } + if (isDocumentMimeType(row.mime_type)) + await validateDocumentBytes(path, row.mime_type) + else await validateResourceBytes({ mimeType: row.mime_type, sizeBytes: row.size_bytes }, await readFile(path)) + this.#owned(row.id, principal) + if (path !== row.path) { + await fileStorage.replace(path, row.path) + await fileStorage.syncDirectory(dirname(row.path)) + await fileStorage.syncDirectory(dirname(path)) + } + } + return (): CommandDecision => { + const current = this.#owned(row.id, principal) + if (current.state === 'receiving') { + this.#files.publish(row.path, { sizeBytes: row.size_bytes, sha256: row.sha256, durability: fileStorage.durability }) + this.#files.reference(row.file_id, { kind: 'export', ownerId: row.id, bindingId: row.id }) + this.#files.settle([row.path], 'commit') + if (row.input_id) + this.#registerResource(row) + database.prepare('UPDATE remote_uploads SET state = \'ready\' WHERE id = ?').run(row.id) + } + else if (current.state !== 'ready' && current.state !== 'adopted') { + throw new ProductProtocolError('RESOURCE_UNAVAILABLE') + } + return { target: { uploadId: row.id }, result: { resourceId: row.resource_id, contentVersion: row.sha256 } } + } + } + const adoption = input as InputCommandIntent<'uploads.adopt'> + return (): CommandDecision => { + const current = this.#owned(row.id, principal) + if (current.state !== 'ready') + throw new ProductProtocolError('RESOURCE_UNAVAILABLE') + if (row.input_id || adoption.draftId !== row.draft_id) + throw new ProductProtocolError('DRAFT_CONFLICT') + this.#draft(adoption.draftId, adoption.expectedRevision, principal) + const totals = database.prepare('SELECT COUNT(*) AS count, COALESCE(SUM(size_bytes),0) AS bytes FROM composer_resources WHERE draft_id = ?').get(row.draft_id)! + if (Number(totals.count) >= BUDDY_ATTACHMENT_COUNT_LIMIT || Number(totals.bytes) + row.size_bytes > BUDDY_ATTACHMENT_TOTAL_BYTES_LIMIT) + throw new PrivateNetworkError('LIMIT_EXCEEDED') + const now = new Date().toISOString() + this.#registerResource(row) + database.prepare('UPDATE composer_drafts SET revision = revision + 1, updated_at = ? WHERE id = ?').run(now, row.draft_id) + database.prepare('UPDATE remote_uploads SET state = \'adopted\' WHERE id = ?').run(row.id) + this.#files.releaseReference({ kind: 'export', ownerId: row.id, bindingId: row.id }) + const draft = findPersonalComposerDraft(database, row.draft_id)! + afterTransactionCommit(database, () => this.#options.drafts.notifyCommitted('saved', draft), () => {}) + return { target: { uploadId: row.id, draftId: row.draft_id }, result: { resourceId: row.resource_id, attachmentId: row.attachment_id, contentVersion: row.sha256, revision: draft.revision } } + } + } + + #registerResource(row: UploadRow) { + const database = this.#options.database + const totals = database.prepare('SELECT COUNT(*) AS count, COALESCE(SUM(size_bytes),0) AS bytes FROM composer_resources WHERE draft_id = ?').get(row.draft_id)! + if (Number(totals.count) >= BUDDY_ATTACHMENT_COUNT_LIMIT || Number(totals.bytes) + row.size_bytes > BUDDY_ATTACHMENT_TOTAL_BYTES_LIMIT) + throw new PrivateNetworkError('LIMIT_EXCEEDED') + const now = new Date().toISOString() + createAttachmentRepository(database).create({ id: row.attachment_id, draftId: row.draft_id, messageId: null, conversationId: null, storedPath: row.path, name: row.name, mimeType: row.mime_type, sizeBytes: row.size_bytes, createdAt: now }) + const resources = createComposerResourceRepository(database) + resources.accept(row.draft_id, [{ resourceId: row.resource_id, name: row.name, mimeType: row.mime_type, sizeBytes: row.size_bytes }], now) + resources.finish({ resourceId: row.resource_id, draftId: row.draft_id, attachmentId: row.attachment_id, contentHash: row.sha256, now }) + } + + commitInput(principal: TrustedCommandPrincipal, input: SubmittedInput): void { + principal.assertAuthenticated() + const database = this.#options.database + const owner = inputResourceOwner(principal.actorId, input.inputId) + for (const resourceId of getBuddyUserContentResourceIds(input.content)) { + const row = database.prepare('SELECT id, state FROM remote_uploads WHERE actor_id = ? AND input_id = ? AND resource_id = ?').get(principal.actorId, input.inputId, resourceId) + if (!row) + continue + if (row.state !== 'ready') + throw new ProductProtocolError('RESOURCE_UNAVAILABLE') + database.prepare('UPDATE remote_uploads SET state = \'adopted\' WHERE id = ?').run(row.id) + createComposerResourceRepository(database).remove(owner, [resourceId]) + this.#files.releaseReference({ kind: 'export', ownerId: String(row.id), bindingId: String(row.id) }) + } + } + + #draft(id: string, revision: number, principal: TrustedCommandPrincipal) { + principal.assertAuthenticated() + const row = this.#options.database.prepare('SELECT revision FROM composer_drafts WHERE id = ? AND owner_actor_id = ?').get(id, principal.actorId) + if (!row || row.revision !== revision) + throw new ProductProtocolError('DRAFT_CONFLICT') + } + + #row(id: string): UploadRow { + const row = this.#options.database.prepare('SELECT u.*, f.path, f.temporary_path FROM remote_uploads u JOIN managed_files f ON f.id = u.file_id WHERE u.id = ?').get(id) + if (!row) + throw new ProductProtocolError('RESOURCE_UNAVAILABLE') + return row as unknown as UploadRow + } + + #owned(id: string, principal: TrustedCommandPrincipal): UploadRow { + principal.assertAuthenticated() + const row = this.#row(id) + if (row.actor_id !== principal.actorId) + throw new ProductProtocolError('NOT_AUTHORIZED') + if (row.expires_ms <= this.#now() && ['receiving', 'ready'].includes(row.state)) { + this.sweep() + return this.#row(id) + } + return row + } + + #view(row: UploadRow) { + return uploadStateSchema.parse({ uploadId: row.id, ...(row.input_id ? { inputId: row.input_id } : {}), draftId: row.draft_id, expectedRevision: String(row.expected_revision), state: row.state, confirmedBytes: row.confirmed_bytes, sizeBytes: row.size_bytes, sha256: row.sha256, expiresAt: new Date(row.expires_ms).toISOString(), resourceId: ['ready', 'adopted'].includes(row.state) ? row.resource_id : null, contentVersion: ['ready', 'adopted'].includes(row.state) ? row.sha256 : null, attachmentId: row.state === 'adopted' || (row.input_id && row.state === 'ready') ? row.attachment_id : null }) + } + + async #recover(row: UploadRow) { + if (this.#verified.has(row.id)) + return + await mkdir(dirname(row.temporary_path), { recursive: true, mode: 0o700 }) + const path = await exists(row.path) ? row.path : row.temporary_path + if (!await exists(path)) { + if (row.confirmed_bytes) { + this.#fail(row.id) + throw new ProductProtocolError('RESOURCE_UNAVAILABLE') + } + const file = await open(path, 'wx', 0o600) + try { + await file.sync() + } + finally { await file.close() } + await fileStorage.syncDirectory(dirname(path)) + } + if ((await stat(path)).size < row.confirmed_bytes) { + this.#fail(row.id) + throw new ProductProtocolError('RESOURCE_UNAVAILABLE') + } + let position = 0 + for (const chunk of this.#options.database.prepare('SELECT start, length, sha256 FROM remote_upload_chunks WHERE upload_id = ? ORDER BY start').all(row.id)) { + if (Number(chunk.start) !== position || await hashFile(path, position, Number(chunk.length)) !== chunk.sha256) { + this.#fail(row.id) + throw new ProductProtocolError('RESOURCE_UNAVAILABLE') + } + position += Number(chunk.length) + } + if (position !== row.confirmed_bytes) { + this.#fail(row.id) + throw new ProductProtocolError('RESOURCE_UNAVAILABLE') + } + const file = await open(path, 'r+') + try { + await file.truncate(row.confirmed_bytes) + await file.sync() + } + finally { await file.close() } + this.#verified.add(row.id) + } + + #fail(id: string) { + withTransaction(this.#options.database, () => this.#options.database.prepare('UPDATE remote_uploads SET state = \'failed\' WHERE id = ? AND state = \'receiving\'').run(id)) + } + + #release(row: UploadRow) { + if (row.input_id) { + createComposerResourceRepository(this.#options.database).remove(row.draft_id, [row.resource_id]) + const attachments = createAttachmentRepository(this.#options.database) + if (attachments.findById(row.attachment_id)?.draftId === row.draft_id) + attachments.removeDraft(row.attachment_id) + } + this.#files.releaseReference({ kind: 'export', ownerId: row.id, bindingId: row.id }) + this.#files.settle([row.path], 'rollback') + this.#verified.delete(row.id) + } + + #now() { return Math.max(Date.now(), Number(this.#options.database.prepare('SELECT last_observed_ms FROM private_network_state WHERE id = 1').get()!.last_observed_ms)) } + + #exclusive(id: string, operation: () => Promise): Promise { + if (this.#closed || this.#busy.has(id) || this.#busy.size >= limits.httpRequests) + return Promise.reject(new PrivateNetworkError('LIMIT_EXCEEDED')) + const promise = Promise.resolve().then(operation).catch((error) => { + this.#verified.delete(id) + throw error + }).finally(() => { + this.#busy.delete(id) + const row = this.#row(id) + if (['cancelled', 'expired', 'failed'].includes(row.state)) + this.#release(row) + }) + this.#busy.set(id, promise) + return promise + } +} + +async function exists(path: string) { + try { + await stat(path) + return true + } + catch (error) { + if ((error as NodeJS.ErrnoException).code === 'ENOENT') + return false + throw error + } +} +async function hashFile(path: string, start?: number, length?: number) { + const hash = createHash('sha256') + if (length !== 0) { + for await (const chunk of createReadStream(path, start === undefined ? {} : { start, end: start + length! - 1 })) hash.update(chunk) + } + return hash.digest('hex') +} +async function readChunk(request: IncomingMessage, length: number) { + const chunks: Buffer[] = [] + let size = 0 + for await (const chunk of request) { + size += chunk.length + if (size > length) + throw new PrivateNetworkError('RANGE_INVALID') + chunks.push(Buffer.from(chunk)) + } + if (size !== length) + throw new PrivateNetworkError('RANGE_INVALID') + return Buffer.concat(chunks, length) +} diff --git a/apps/buddy/service/src/privateNetwork/__tests__/DeviceAuthorizationService.spec.ts b/apps/buddy/service/src/privateNetwork/__tests__/DeviceAuthorizationService.spec.ts new file mode 100644 index 000000000..213b4aae8 --- /dev/null +++ b/apps/buddy/service/src/privateNetwork/__tests__/DeviceAuthorizationService.spec.ts @@ -0,0 +1,188 @@ +import type { DatabaseSync } from 'node:sqlite' +import { mkdtempSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { PRIVATE_NETWORK_LIMITS } from '../../../../shared/privateNetwork/limits' +import { deviceBearer } from '../../../../shared/privateNetwork/pairing' +import { CommandAdmissionService } from '../../commands/CommandAdmissionService' +import { ProductConnection } from '../../productProtocol/ProductConnection' +import { ProductQueries } from '../../productProtocol/ProductQueries' +import { createConversationRepository } from '../../storage/conversationRepository' +import { openBuddyMigrationCandidate } from '../../storage/database' +import { DeviceAuthorizationService } from '../DeviceAuthorizationService' + +const directories: string[] = [] +const databases: DatabaseSync[] = [] +afterEach(() => { + for (const database of databases.splice(0)) { + if (database.isOpen) + database.close() + } + for (const directory of directories.splice(0)) rmSync(directory, { recursive: true, force: true }) +}) +function fixture() { + const directory = mkdtempSync(join(tmpdir(), 'lexora-device-auth-')) + directories.push(directory) + const path = join(directory, 'buddy.sqlite3') + const database = openBuddyMigrationCandidate({ databasePath: path }) + databases.push(database) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + let time = Date.parse('2026-11-07T00:00:00.000Z') + const clock = () => time + const auth = new DeviceAuthorizationService({ database, serviceId: 'service', profileId: 'profile', serviceInstanceId: 'instance', clock }) + const offer = () => { + const qr = auth.issue('https://127.0.0.1:9443', `sha256/${'A'.repeat(43)}=`) + const { serviceId, profileId, issuerInstanceId, ticketId, ticketSecret } = qr + return { serviceId, profileId, issuerInstanceId, ticketId, ticketSecret } + } + const pair = () => { + const context = offer() + const recovery = auth.prepare({ ...context, deviceName: '隔离测试手机' }) + const credential = auth.redeem({ ...context, attemptId: recovery.attemptId, recoverySecret: recovery.recoverySecret }) + return { context, recovery, credential, bearer: deviceBearer(credential) } + } + return { database, path, auth, offer, pair, clock, advance: (ms: number) => time += ms } +} + +describe('durable device authorization', () => { + it('keeps task and message pagination valid across authenticated requests and authorization sweeps', async () => { + const f = fixture() + const conversations = createConversationRepository(f.database) + for (const id of ['first', 'second']) { + conversations.create({ id, branchId: `branch-${id}`, title: id, spaceId: null, createdAt: '2026-01-01T00:00:00.000Z', approvalPolicy: 'policy', executionProfile: 'workspace_write' }) + for (let index = 0; index < 3; index++) + conversations.createMessage({ id: `${id}-${index}`, conversationId: id, branchId: `branch-${id}`, runId: null, role: 'user', content: `message ${index}`, createdAt: `2026-01-01T00:00:0${index}.000Z` }) + } + const paired = f.pair() + f.auth.confirm(paired.bearer) + const identity = f.auth.authenticate(paired.bearer) + const frames: any[] = [] + const connection = new ProductConnection({ + context: f.auth.context(identity), + assertCurrent: () => f.auth.assertCurrent(identity), + phase: () => 'ready', + handlers: new ProductQueries(f.database, identity.deviceId, 'instance').handlers(), + send: (raw) => { + frames.push(JSON.parse(raw)) + return true + }, + close() {}, + }) + const request = async (method: string, params: unknown) => { + await connection.receive(JSON.stringify({ jsonrpc: '2.0', id: String(frames.length), method, params })) + const response = frames.at(-1) + expect(response.error).toBeUndefined() + return response.result + } + try { + await request('initialize', { protocol: { major: 1, minMinor: 0, maxMinor: 0 }, client: { kind: 'mobile', version: '1' }, supportedContentKinds: ['buddy-user-content-v1'], supportedReviewKinds: [] }) + const tasks = await request('conversations.list', { limit: 1, cursor: null }) + const input = { conversationId: 'first', branchId: 'branch-first', limit: 1, cursor: null } + const messages = await request('conversations.listMessages', input) + expect(tasks.nextCursor).toEqual(expect.any(String)) + expect(messages.nextCursor).toEqual(expect.any(String)) + f.advance(1000) + f.auth.sweep() + f.advance(10) + expect(await request('conversations.list', { limit: 1, cursor: tasks.nextCursor })).toMatchObject({ items: [{ id: 'second' }], nextCursor: null }) + f.advance(10) + expect(await request('conversations.listMessages', { ...input, cursor: messages.nextCursor })).toMatchObject({ items: [{ id: 'first-1' }] }) + f.database.prepare('UPDATE messages SET content_json = ? WHERE id = ?').run(JSON.stringify('changed'), 'first-1') + await connection.receive(JSON.stringify({ jsonrpc: '2.0', id: 'changed-history', method: 'conversations.listMessages', params: { ...input, cursor: messages.nextCursor } })) + expect(frames.at(-1)).toMatchObject({ error: { code: 'CURSOR_EXPIRED' } }) + } + finally { connection.close() } + }) + + it('consumes one ticket for one device and only the original recovery proof can recover it', () => { + const f = fixture() + const context = f.offer() + const a = f.auth.prepare({ ...context, deviceName: 'A' }) + const b = f.auth.prepare({ ...context, deviceName: 'B' }) + const first = f.auth.redeem({ ...context, attemptId: a.attemptId, recoverySecret: a.recoverySecret }) + expect(() => f.auth.authenticate(deviceBearer(first))).toThrow('NOT_AUTHORIZED') + expect(() => f.auth.redeem({ ...context, attemptId: b.attemptId, recoverySecret: b.recoverySecret })).toThrow('PAIRING_USED') + expect(() => f.auth.redeem({ ...context, attemptId: a.attemptId, recoverySecret: a.recoverySecret })).toThrow('RECOVERY_REQUIRED') + const sibling = f.auth.recover({ serviceId: 'service', profileId: 'profile', attemptId: a.attemptId, recoverySecret: a.recoverySecret }) + expect(sibling.deviceId).toBe(first.deviceId) + expect(f.auth.confirm(deviceBearer(first)).state).toBe('active') + expect(f.auth.confirm(deviceBearer(first)).state).toBe('active') + expect(f.auth.authenticate(deviceBearer(sibling)).deviceId).toBe(first.deviceId) + expect(() => f.auth.recover({ serviceId: 'service', profileId: 'profile', attemptId: a.attemptId, recoverySecret: a.recoverySecret })).toThrow('RECOVERY_CLOSED') + const rows = JSON.stringify(['pairing_tickets', 'pairing_attempts', 'remote_credentials'].map(table => f.database.prepare(`SELECT * FROM ${table}`).all())) + for (const secret of [context.ticketSecret, a.recoverySecret, first.credentialSecret, sibling.credentialSecret]) expect(rows).not.toContain(secret) + expect(f.auth.list()).toHaveLength(1) + }) + + it('keeps a consumed attempt across restart but invalidates old unconsumed QR and bounds siblings', () => { + const f = fixture() + const paired = f.pair() + const pending = f.offer() + const next = new DeviceAuthorizationService({ database: f.database, serviceId: 'service', profileId: 'profile', serviceInstanceId: 'next', clock: f.clock }) + expect(() => next.prepare({ ...pending, deviceName: 'late' })).toThrow('PAIRING_UNAVAILABLE') + for (let index = 1; index < PRIVATE_NETWORK_LIMITS.siblingCredentials; index++) next.recover({ serviceId: 'service', profileId: 'profile', attemptId: paired.recovery.attemptId, recoverySecret: paired.recovery.recoverySecret }) + expect(() => next.recover({ serviceId: 'service', profileId: 'profile', attemptId: paired.recovery.attemptId, recoverySecret: paired.recovery.recoverySecret })).toThrow('LIMIT_EXCEEDED') + next.confirm(paired.bearer) + expect(next.authenticate(paired.bearer).deviceId).toBe(paired.credential.deviceId) + }) + + it('rolls back the ticket, device and attempt if credential persistence fails', () => { + const f = fixture() + const context = f.offer() + const attempt = f.auth.prepare({ ...context, deviceName: 'failure' }) + f.database.exec('CREATE TRIGGER credential_fault BEFORE INSERT ON remote_credentials BEGIN SELECT RAISE(ABORT, \'fixture fault\'); END') + const input = { ...context, attemptId: attempt.attemptId, recoverySecret: attempt.recoverySecret } + expect(() => f.auth.redeem(input)).toThrow('fixture fault') + expect(f.auth.list()).toHaveLength(0) + expect(f.database.prepare('SELECT state FROM pairing_tickets').get()!.state).toBe('pending') + expect(f.database.prepare('SELECT device_id FROM pairing_attempts').get()!.device_id).toBeNull() + f.database.exec('DROP TRIGGER credential_fault') + expect(f.auth.redeem(input).deviceId).toBeTruthy() + }) + + it('commits revocation before closing transports and rejects an asynchronously prepared command at commit', async () => { + const f = fixture() + const pair = f.pair() + f.auth.confirm(pair.bearer) + const identity = f.auth.authenticate(pair.bearer) + const admission = new CommandAdmissionService({ database: f.database, principal: f.auth.principal(identity), sourceInstanceId: 'instance' }) + let finish!: () => void + let started!: () => void + const prepared = new Promise(resolve => started = resolve) + const pending = admission.execute('composerDrafts.discard', { dataGeneration: identity.dataGeneration, commandId: 'blocked', expectedServiceInstanceId: 'instance', intent: { draftId: 'draft', expectedRevision: 0 } }, { prepare: async () => { + started() + await new Promise(resolve => finish = resolve) + }, commit: () => ({ target: {}, result: {} }) }) + const rejected = expect(pending).rejects.toThrow('COMMAND_ACTOR_UNAVAILABLE') + await prepared + let observed = false + f.auth.onDidInvalidate(({ deviceId }) => { + expect(deviceId).toBe(identity.deviceId) + expect(f.database.isTransaction).toBe(false) + expect(() => f.auth.authenticate(pair.bearer)).toThrow('NOT_AUTHORIZED') + observed = true + }) + f.auth.revoke(identity.deviceId) + finish() + await rejected + expect(observed).toBe(true) + expect(f.database.prepare('SELECT count(*) AS n FROM command_receipts').get()!.n).toBe(0) + expect(() => f.database.prepare('UPDATE remote_devices SET state = \'active\' WHERE id = ?').run(identity.deviceId)).toThrow('DEVICE_STATE_TERMINAL') + }) + + it('does not revive expired credentials after wall-clock rollback, restart or renewal', () => { + const f = fixture() + const paired = f.pair() + f.auth.confirm(paired.bearer) + f.advance(PRIVATE_NETWORK_LIMITS.credentialIdleMs + 1) + expect(() => f.auth.renew(paired.bearer)).toThrow('NOT_AUTHORIZED') + f.advance(-PRIVATE_NETWORK_LIMITS.credentialIdleMs) + f.database.close() + const reopened = openBuddyMigrationCandidate({ databasePath: f.path }) + databases.push(reopened) + const next = new DeviceAuthorizationService({ database: reopened, serviceId: 'service', profileId: 'profile', serviceInstanceId: 'next', clock: f.clock }) + expect(() => next.authenticate(paired.bearer)).toThrow('NOT_AUTHORIZED') + expect(next.list()[0]?.state).toBe('expired') + }) +}) diff --git a/apps/buddy/service/src/privateNetwork/__tests__/PrivateNetworkController.spec.ts b/apps/buddy/service/src/privateNetwork/__tests__/PrivateNetworkController.spec.ts new file mode 100644 index 000000000..bbda8748a --- /dev/null +++ b/apps/buddy/service/src/privateNetwork/__tests__/PrivateNetworkController.spec.ts @@ -0,0 +1,69 @@ +import { mkdtempSync, rmSync } from 'node:fs' +import { createServer } from 'node:net' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { openBuddyMigrationCandidate } from '../../storage/database' +import * as addresses from '../privateNetworkAddress' +import { PrivateNetworkController } from '../PrivateNetworkController' + +const cleanup: (() => Promise)[] = [] +afterEach(async () => { + for (const close of cleanup.splice(0)) await close() + vi.restoreAllMocks() +}) +function fixture() { + const directory = mkdtempSync(join(tmpdir(), 'lexora-network-controller-')) + const database = openBuddyMigrationCandidate({ databasePath: join(directory, 'buddy.sqlite3') }) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + let stored: unknown = null + const controller = new PrivateNetworkController({ + database, + serviceId: 'service', + profileId: 'profile', + serviceInstanceId: 'instance', + secrets: { + read: async () => stored, + write: async (_, value) => { stored = structuredClone(value) }, + }, + connect: () => { throw new Error('Unexpected product connection') }, + }) + cleanup.push(async () => { + await controller.close() + database.close() + rmSync(directory, { recursive: true, force: true }) + }) + return controller +} +async function freePort() { + const server = createServer() + await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)) + const address = server.address() + if (!address || typeof address === 'string') + throw new Error('Missing port') + await new Promise((resolve, reject) => server.close(error => error ? reject(error) : resolve())) + return address.port +} +describe('mobile connection configuration', () => { + it('starts without a supplied address and keeps its TLS identity after disabling and reenabling', async () => { + vi.spyOn(addresses, 'selectPrivateNetworkAddress').mockReturnValue('127.0.0.1') + const controller = fixture() + const port = await freePort() + const listening = await controller.configure({ enabled: true, port }) + expect(listening).toMatchObject({ enabled: true, state: 'listening', bindAddress: '127.0.0.1', endpoint: `https://127.0.0.1:${port}`, errorCode: null }) + expect(listening.spkiPin).toMatch(/^sha256\//) + expect(await controller.configure({ enabled: false })).toMatchObject({ enabled: false, state: 'disabled', endpoint: null }) + expect(await controller.configure({ enabled: true })).toMatchObject({ state: 'listening', spkiPin: listening.spkiPin, port }) + await expect(controller.configure({ enabled: true, bindAddress: '0.0.0.0' })).rejects.toThrow('INVALID_REQUEST') + expect(controller.status()).toMatchObject({ state: 'listening', spkiPin: listening.spkiPin }) + }) + it('reports missing networks without opening a listener and can retry when a private network appears', async () => { + const selection = vi.spyOn(addresses, 'selectPrivateNetworkAddress').mockReturnValue(null) + const controller = fixture() + expect(await controller.configure({ enabled: true })).toMatchObject({ state: 'unavailable', bindAddress: null, port: 19443, endpoint: null, errorCode: 'ADDRESS_UNAVAILABLE' }) + expect(controller.auth.list()).toEqual([]) + await controller.configure({ enabled: false }) + selection.mockReturnValue('127.0.0.1') + expect(await controller.configure({ enabled: true, port: await freePort() })).toMatchObject({ state: 'listening', errorCode: null }) + }) +}) diff --git a/apps/buddy/service/src/privateNetwork/__tests__/PrivateNetworkServer.spec.ts b/apps/buddy/service/src/privateNetwork/__tests__/PrivateNetworkServer.spec.ts new file mode 100644 index 000000000..24a778ef8 --- /dev/null +++ b/apps/buddy/service/src/privateNetwork/__tests__/PrivateNetworkServer.spec.ts @@ -0,0 +1,114 @@ +import type { DatabaseSync } from 'node:sqlite' +import { Buffer } from 'node:buffer' +import { createHash, X509Certificate } from 'node:crypto' +import { mkdtempSync, rmSync } from 'node:fs' +import { request } from 'node:https' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { checkServerIdentity } from 'node:tls' +import { afterEach, describe, expect, it } from 'vitest' +import { WebSocket } from 'ws' +import { deviceBearer } from '../../../../shared/privateNetwork/pairing' +import { ProductConnection } from '../../productProtocol/ProductConnection' +import { openBuddyMigrationCandidate } from '../../storage/database' +import { DeviceAuthorizationService } from '../DeviceAuthorizationService' +import { PrivateNetworkServer } from '../PrivateNetworkServer' +import { isPrivateNetworkAddress, loadPrivateNetworkTlsIdentity } from '../PrivateNetworkTlsIdentity' + +const directories: string[] = [] +const databases: DatabaseSync[] = [] +const servers: PrivateNetworkServer[] = [] +afterEach(async () => { + for (const server of servers.splice(0)) await server.close() + for (const database of databases.splice(0)) { + if (database.isOpen) + database.close() + } + for (const directory of directories.splice(0)) rmSync(directory, { recursive: true, force: true }) +}) +async function fixture() { + const directory = mkdtempSync(join(tmpdir(), 'lexora-private-network-')) + directories.push(directory) + const database = openBuddyMigrationCandidate({ databasePath: join(directory, 'buddy.sqlite3') }) + databases.push(database) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + let stored: unknown | null = null + const secrets = { read: async () => stored, write: async (_id: string, value: unknown) => { + stored = structuredClone(value) + } } + const tls = await loadPrivateNetworkTlsIdentity({ database, profileId: 'profile', secrets, addresses: ['127.0.0.1'] }) + const auth = new DeviceAuthorizationService({ database, serviceId: 'service', profileId: 'profile', serviceInstanceId: 'instance' }) + const server = new PrivateNetworkServer({ auth, identity: { serviceId: 'service', profileId: 'profile' }, tls, connect: (identity, transport) => new ProductConnection({ ...transport, context: auth.context(identity), assertCurrent: () => auth.assertCurrent(identity), phase: () => 'ready', handlers: { 'conversations.list': { handle: () => ({ items: [], nextCursor: null }) } } }) }) + servers.push(server) + const endpoint = await server.listen('127.0.0.1', 0) + const call = (path: string, body?: unknown, bearer?: string, pin = tls.spkiPin) => new Promise<{ status: number, body: any }>((resolve, reject) => { + const payload = body === undefined ? undefined : JSON.stringify(body) + const req = request(`${endpoint}${path}`, { method: payload === undefined ? 'GET' : 'POST', ca: tls.certificate, checkServerIdentity: (host, cert) => { + const failure = checkServerIdentity(host, cert) + if (failure) + return failure + const actual = `sha256/${createHash('sha256').update(new X509Certificate(cert.raw).publicKey.export({ format: 'der', type: 'spki' })).digest('base64')}` + return actual === pin ? undefined : new Error('PIN_MISMATCH') + }, headers: { ...(payload ? { 'Content-Type': 'application/json', 'Content-Length': String(Buffer.byteLength(payload)) } : {}), ...(bearer ? { Authorization: `Bearer ${bearer}` } : {}) } }, (response) => { + let value = '' + response.setEncoding('utf8') + response.on('data', chunk => value += chunk) + response.on('end', () => resolve({ status: response.statusCode!, body: value ? JSON.parse(value) : null })) + }) + req.on('error', reject) + req.end(payload) + }) + return { database, secrets, tls, auth, server, endpoint, call } +} + +describe('private TLS transport', () => { + it('reuses its protected key across certificates and rejects missing keys or public bindings', async () => { + const f = await fixture() + const renewed = await loadPrivateNetworkTlsIdentity({ database: f.database, profileId: 'profile', secrets: f.secrets, addresses: ['127.0.0.1', '::1'] }) + expect(renewed.spkiPin).toBe(f.tls.spkiPin) + expect(new X509Certificate(renewed.certificate).checkIP('::1')).toBe('::1') + await f.secrets.write('', null) + await expect(loadPrivateNetworkTlsIdentity({ database: f.database, profileId: 'profile', secrets: f.secrets, addresses: ['127.0.0.1'] })).rejects.toThrow('TLS_IDENTITY_UNAVAILABLE') + expect(['0.0.0.0', '::', '8.8.8.8', 'example.com', '127.0.0.1.example.com'].map(isPrivateNetworkAddress)).toEqual([false, false, false, false, false]) + expect(['127.0.0.1', '10.0.0.2', '192.168.1.2', '100.100.1.1', '::1', 'fd00::1'].every(isPrivateNetworkAddress)).toBe(true) + }) + + it('uses one authenticated HTTPS/WSS identity, activates only after confirm and closes revoked connections', async () => { + const f = await fixture() + const qr = f.auth.issue(f.endpoint, f.tls.spkiPin) + const { serviceId, profileId, issuerInstanceId, ticketId, ticketSecret } = qr + const context = { serviceId, profileId, issuerInstanceId, ticketId, ticketSecret } + await expect(f.call('/v1/pairing/prepare', { ...context, deviceName: 'Wrong pin' }, undefined, `sha256/${'B'.repeat(43)}=`)).rejects.toThrow('PIN_MISMATCH') + expect(f.database.prepare('SELECT count(*) AS n FROM pairing_attempts').get()!.n).toBe(0) + expect((await f.call('/v1/pairing/redeem')).status).toBe(400) + const prepared = await f.call('/v1/pairing/prepare', { ...context, deviceName: 'Native fixture' }) + expect(prepared.status).toBe(200) + const redeemed = await f.call('/v1/pairing/redeem', { ...context, attemptId: prepared.body.attemptId, recoverySecret: prepared.body.recoverySecret }) + const bearer = deviceBearer(redeemed.body) + expect((await f.call('/v1/device', undefined, bearer)).status).toBe(401) + expect((await f.call('/v1/pairing/confirm', {}, bearer)).body.state).toBe('active') + const socket = new WebSocket(`${f.endpoint.replace('https:', 'wss:')}/v1/rpc`, { ca: f.tls.certificate, headers: { Authorization: `Bearer ${bearer}` } }) + await new Promise((resolve, reject) => { + socket.once('open', resolve) + socket.once('error', reject) + }) + const receive = () => new Promise((resolve, reject) => { + socket.once('message', data => resolve(JSON.parse(data.toString()))) + socket.once('error', reject) + }) + let response = receive() + socket.send(JSON.stringify({ jsonrpc: '2.0', id: 'before', method: 'conversations.list', params: {} })) + expect(await response).toMatchObject({ error: { code: 'NOT_INITIALIZED' } }) + response = receive() + socket.send(JSON.stringify({ jsonrpc: '2.0', id: 'init', method: 'initialize', params: { protocol: { major: 1, minMinor: 0, maxMinor: 0 }, client: { kind: 'desktop', version: 'fixture' }, supportedContentKinds: [], supportedReviewKinds: [] } })) + expect(await response).toMatchObject({ result: { deviceId: redeemed.body.deviceId, profileId: 'profile' } }) + response = receive() + socket.send(JSON.stringify({ jsonrpc: '2.0', id: 'list', method: 'conversations.list', params: {} })) + expect(await response).toMatchObject({ result: { items: [], nextCursor: null } }) + const closed = new Promise(resolve => socket.once('close', () => resolve())) + f.auth.revoke(redeemed.body.deviceId) + await closed + expect((await f.call('/v1/device', undefined, bearer)).status).toBe(401) + expect((await f.call('/v1/device?token=forbidden', undefined, bearer)).status).toBe(400) + }) +}) diff --git a/apps/buddy/service/src/privateNetwork/__tests__/RemoteAvatarService.spec.ts b/apps/buddy/service/src/privateNetwork/__tests__/RemoteAvatarService.spec.ts new file mode 100644 index 000000000..5c757987a --- /dev/null +++ b/apps/buddy/service/src/privateNetwork/__tests__/RemoteAvatarService.spec.ts @@ -0,0 +1,64 @@ +import type { TrustedCommandPrincipal } from '../../commands/CommandAdmissionService' +import { Buffer } from 'node:buffer' +import { createHash } from 'node:crypto' +import { once } from 'node:events' +import { createServer } from 'node:http' +import { DatabaseSync } from 'node:sqlite' +import { afterEach, expect, it } from 'vitest' +import { PrivateNetworkError } from '../../../../shared/privateNetwork/pairing' +import { RemoteAvatarService } from '../RemoteAvatarService' + +const image = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO/aU1cAAAAASUVORK5CYII=', 'base64') +const hash = createHash('sha256').update(image).digest('hex') +const cleanups: Array<() => void | Promise> = [] +afterEach(async () => { + for (const cleanup of cleanups.splice(0).reverse()) await cleanup() +}) +function fixture() { + const database = new DatabaseSync(':memory:') + database.exec('CREATE TABLE buddies (id TEXT PRIMARY KEY, avatar TEXT, settings_json TEXT, deleted_at TEXT)') + const set = (id: string, avatar: string | null, deleted = false) => database.prepare('INSERT OR REPLACE INTO buddies VALUES (?, ?, ?, ?)').run(id, avatar, '{"private":"not-public"}', deleted ? '2026-01-01' : null) + set('custom', `data:image/png;base64,${image.toString('base64')}`) + cleanups.push(() => database.close()) + return { database, set } +} + +it('serves authenticated exact image versions and rejects changed, reset, or revoked access', async () => { + const f = fixture() + let active = true + const principal = { assertAuthenticated: () => { + if (!active) + throw new PrivateNetworkError('NOT_AUTHORIZED') + } } as TrustedCommandPrincipal + const service = new RemoteAvatarService(f.database) + const server = createServer((request, response) => { + try { + if (!service.bytes(request, response, principal)) { + response.statusCode = 404 + response.end() + } + } + catch (error) { + response.statusCode = 409 + response.end((error as Error).message) + } + }) + server.listen(0, '127.0.0.1') + await once(server, 'listening') + cleanups.push(async () => { + server.closeAllConnections() + await new Promise(resolve => server.close(() => resolve())) + }) + const url = `http://127.0.0.1:${(server.address() as { port: number }).port}/v1/avatars/custom/${hash}` + const response = await fetch(url) + expect(response.headers.get('content-type')).toBe('image/png') + expect(response.headers.get('etag')).toBe(`"${hash}"`) + expect(Buffer.from(await response.arrayBuffer())).toEqual(image) + expect((await fetch(url, { method: 'HEAD' })).headers.get('content-length')).toBe(String(image.length)) + expect((await fetch(url.replace(hash, 'a'.repeat(64)))).status).toBe(409) + active = false + expect((await fetch(url)).status).toBe(409) + active = true + f.set('custom', null) + expect((await fetch(url)).status).toBe(409) +}) diff --git a/apps/buddy/service/src/privateNetwork/__tests__/RemoteResourceService.spec.ts b/apps/buddy/service/src/privateNetwork/__tests__/RemoteResourceService.spec.ts new file mode 100644 index 000000000..27a9061ed --- /dev/null +++ b/apps/buddy/service/src/privateNetwork/__tests__/RemoteResourceService.spec.ts @@ -0,0 +1,134 @@ +import { Buffer } from 'node:buffer' +import { createHash } from 'node:crypto' +import { once } from 'node:events' +import { mkdir, mkdtemp, readFile, rm, symlink, writeFile } from 'node:fs/promises' +import { createServer } from 'node:http' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { ArtifactService } from '../../artifacts/ArtifactService' +import { createLocalCommandPrincipal } from '../../commands/CommandAdmissionService' +import { ProductQueries } from '../../productProtocol/ProductQueries' +import { seedSqliteRun } from '../../storage/__tests__/sqliteRunFixture' +import { createArtifactRepository } from '../../storage/artifactRepository' +import { createAttachmentPersistenceRepository } from '../../storage/attachmentPersistenceRepository' +import { BuddyDataPaths } from '../../storage/BuddyDataPaths' +import { openBuddyMigrationCandidate } from '../../storage/database' +import { RemoteResourceService } from '../RemoteResourceService' + +const cleanups: (() => Promise)[] = [] +afterEach(async () => { + for (const cleanup of cleanups.splice(0).reverse()) await cleanup() +}) +async function fixture() { + const root = await mkdtemp(join(tmpdir(), 'lexora-resources-')) + const database = openBuddyMigrationCandidate({ databasePath: join(root, 'buddy.sqlite3') }) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + seedSqliteRun(database, 'run') + const local = createLocalCommandPrincipal(database) + let active = true + const principal = { actorId: local.actorId, assertAuthenticated: () => { + if (!active) + throw new Error('REVOKED') + local.assertAuthenticated() + } } + const paths = new BuddyDataPaths(root) + const workspace = paths.conversationWorkspace('task-1') + await mkdir(workspace, { recursive: true }) + const source = join(workspace, 'result.txt') + await writeFile(source, 'original bytes') + const artifacts = createArtifactRepository(database) + artifacts.save({ id: 'artifact', conversationId: 'task-1', currentPath: source, directoryRoot: workspace, directoryGrantId: 'task-1', relativePath: 'result.txt', kind: 'file', name: 'result.txt', mimeType: 'text/plain', sizeBytes: 14, sourceArtifactId: null, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() }) + const artifactService = new ArtifactService({ repository: artifacts }) + const views = (actor: string) => new ProductQueries(database, actor, 'instance').views + const service = new RemoteResourceService({ database, root, instanceId: 'instance', views, artifacts: artifactService }) + const prepare = (id = 'artifact:artifact') => service.prepare({ resourceId: id, expectedVersion: views(local.actorId).resource(id)!.revision }, principal) + const server = createServer((request, response) => { + void service.bytes(request, response, principal).catch((error) => { + if (response.headersSent) { + response.destroy() + } + else { + response.statusCode = 409 + response.end(JSON.stringify({ error: error.code ?? error.message })) + } + }) + }) + server.listen(0, '127.0.0.1') + await once(server, 'listening') + const endpoint = `http://127.0.0.1:${(server.address() as { port: number }).port}` + const get = (resource: { downloadId: string, contentVersion: string }, headers: Record = {}) => fetch(`${endpoint}/v1/resources/${resource.downloadId}`, { headers: { 'If-Match': `"${resource.contentVersion}"`, ...headers } }) + cleanups.push(async () => { + server.closeAllConnections() + await service.close() + await artifactService.dispose() + await new Promise(resolve => server.close(() => resolve())) + database.close() + await rm(root, { recursive: true, force: true }) + }) + return { database, root, source, service, prepare, get, revoke: () => active = false } +} + +describe('immutable authorized resource downloads', () => { + it('exports a registered artifact as fixed bytes and rejects mixed versions and ranges', async () => { + const f = await fixture() + const resource = await f.prepare() + expect(JSON.stringify(resource)).not.toContain(f.source) + expect(resource.contentVersion).toBe(createHash('sha256').update('original bytes').digest('hex')) + await writeFile(f.source, 'newer content!') + const partial = await f.get(resource, { Range: 'bytes=0-7' }) + expect(partial.status).toBe(206) + expect(partial.headers.get('content-range')).toBe('bytes 0-7/14') + expect(await partial.text()).toBe('original') + const rest = await f.get(resource, { Range: 'bytes=8-' }) + expect(await rest.text()).toBe(' bytes') + expect((await f.get(resource, { 'If-Match': '"wrong"' })).status).toBe(409) + expect((await f.get(resource, { Range: 'bytes=0-2,5-8' })).status).toBe(409) + expect((await f.get(resource, { Range: 'bytes=14-' })).status).toBe(409) + const changed = await f.prepare() + expect(changed.contentVersion).not.toBe(resource.contentVersion) + }) + + it('does not export a substituted symlink outside the authorized workspace', async () => { + const f = await fixture() + const outside = join(f.root, 'outside.txt') + await writeFile(outside, 'unpublished bytes') + await rm(f.source) + await symlink(outside, f.source) + await expect(f.prepare()).rejects.toThrow() + expect(f.database.prepare('SELECT COUNT(*) AS n FROM remote_exports').get()!.n).toBe(0) + }) + + it('protects a fixed export from GC and releases it after expiry without deleting its source', async () => { + const f = await fixture() + const resource = await f.prepare() + const files = createAttachmentPersistenceRepository(f.database) + expect(files.candidates()).toEqual([]) + f.database.prepare('UPDATE private_network_state SET last_observed_ms = ?').run(Date.parse(resource.expiresAt) + 1) + f.service.sweep() + expect(files.candidates()).toHaveLength(1) + expect((await f.get(resource)).status).toBe(409) + expect(await readFile(f.source, 'utf8')).toBe('original bytes') + }) + + it('checks current object visibility and authorization again before each download', async () => { + const f = await fixture() + const resource = await f.prepare() + f.database.prepare('UPDATE conversations SET deleted_at = ? WHERE id = ?').run(new Date().toISOString(), 'task-1') + expect((await f.get(resource)).status).toBe(409) + f.database.prepare('UPDATE conversations SET deleted_at = NULL WHERE id = ?').run('task-1') + f.revoke() + expect((await f.get(resource)).status).toBe(409) + }) + + it('detects corruption of fixed bytes before it emits a successful response', async () => { + const f = await fixture() + const resource = await f.prepare() + const path = String(f.database.prepare('SELECT f.path FROM remote_exports e JOIN managed_files f ON f.id = e.file_id').get()!.path) + await writeFile(path, Buffer.from('tampered bytes')) + const response = await f.get(resource) + expect(response.status).toBe(409) + expect(await response.json()).toMatchObject({ error: 'VERSION_CHANGED' }) + expect(f.database.prepare('SELECT state FROM managed_files WHERE path = ?').get(path)!.state).toBe('unavailable') + }) +}) diff --git a/apps/buddy/service/src/privateNetwork/__tests__/RemoteUploadService.spec.ts b/apps/buddy/service/src/privateNetwork/__tests__/RemoteUploadService.spec.ts new file mode 100644 index 000000000..554a3e4aa --- /dev/null +++ b/apps/buddy/service/src/privateNetwork/__tests__/RemoteUploadService.spec.ts @@ -0,0 +1,194 @@ +import { Buffer } from 'node:buffer' +import { createHash } from 'node:crypto' +import { once } from 'node:events' +import { appendFile, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' +import { createServer } from 'node:http' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { createBuddyUserContent } from '../../../../shared/conversation/buddyUserContent' +import { ComposerDraftService } from '../../chat/ComposerDraftService' +import { CommandAdmissionService, createLocalCommandPrincipal } from '../../commands/CommandAdmissionService' +import { ProductContentReferences } from '../../productProtocol/ProductContentReferences' +import { ProductReadViews } from '../../productProtocol/ProductReadViews' +import { createAttachmentPersistenceRepository } from '../../storage/attachmentPersistenceRepository' +import { createAttachmentRepository } from '../../storage/attachmentRepository' +import { createComposerDraftRepository } from '../../storage/composerDraftRepository' +import { openBuddyMigrationCandidate } from '../../storage/database' +import { RemoteUploadService } from '../RemoteUploadService' + +const cleanups: (() => Promise)[] = [] +afterEach(async () => { + for (const cleanup of cleanups.splice(0).reverse()) await cleanup() +}) +const hash = (bytes: Uint8Array) => createHash('sha256').update(bytes).digest('hex') +async function fixture() { + const root = await mkdtemp(join(tmpdir(), 'lexora-upload-')) + const database = openBuddyMigrationCandidate({ databasePath: join(root, 'buddy.sqlite3') }) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + const repository = createComposerDraftRepository(database) + const draft = repository.open({ draftId: 'draft', scope: { kind: 'task', draftId: 'draft', spaceId: null }, initialContent: createBuddyUserContent('保留输入'), initialExecutionConfig: { approvalPolicy: 'policy', executionProfile: 'read_only' }, initialModelSelection: null, now: new Date().toISOString() }) + const drafts = new ComposerDraftService(repository, async () => {}) + const local = createLocalCommandPrincipal(database) + let active = true + const principal = { actorId: local.actorId, assertAuthenticated: () => { + if (!active) + throw new Error('REVOKED') + local.assertAuthenticated() + } } + const admission = new CommandAdmissionService({ database, principal, sourceInstanceId: 'instance' }) + let service = new RemoteUploadService({ database, root, instanceId: 'instance', drafts }) + const commands = () => service.commands(principal, admission) + const envelope = (commandId: string, intent: unknown) => ({ dataGeneration: database.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation, expectedServiceInstanceId: 'instance', commandId, intent }) + const create = async (bytes: Uint8Array, id = 'create') => { + const result = await commands().execute('uploads.create', envelope(id, { draftId: 'draft', expectedRevision: repository.findById('draft')!.revision, file: { name: '示例.txt', mimeType: 'text/plain', sizeBytes: bytes.length, sha256: hash(bytes) } })) + return String(result.receipt.result.uploadId) + } + const server = createServer((request, response) => { + void service.bytes(request, response, principal).catch((error) => { + response.statusCode = 409 + response.end(JSON.stringify({ error: error.code ?? error.message })) + }) + }) + server.listen(0, '127.0.0.1') + await once(server, 'listening') + const address = server.address() as { port: number } + const put = async (id: string, bytes: Uint8Array, start: number, total: number, digest = hash(bytes)) => { + const response = await fetch(`http://127.0.0.1:${address.port}/v1/uploads/${id}`, { method: 'PUT', headers: { 'Content-Range': `bytes ${start}-${start + bytes.length - 1}/${total}`, 'Content-Length': String(bytes.length), 'X-Content-SHA256': digest }, body: Buffer.from(bytes) }) + return { status: response.status, body: await response.json() } + } + const restart = async () => { + await service.close() + createAttachmentPersistenceRepository(database).recover('next') + service = new RemoteUploadService({ database, root, instanceId: 'next', drafts }) + } + cleanups.push(async () => { + await service.close() + await admission.dispose() + await drafts.dispose() + server.closeAllConnections() + await new Promise(resolve => server.close(() => resolve())) + database.close() + await rm(root, { recursive: true, force: true }) + }) + return { root, database, repository, draft, commands, envelope, create, put, restart, service: () => service, revoke: () => active = false } +} + +describe('durable remote uploads', () => { + it('acknowledges exact chunks, resumes only the proven prefix, and publishes the same immutable resource on replay', async () => { + const f = await fixture() + const bytes = Buffer.from('first second') + const id = await f.create(bytes) + expect(await f.put(id, bytes.subarray(0, 6), 0, bytes.length)).toMatchObject({ status: 200, body: { confirmedBytes: 6 } }) + expect(await f.put(id, bytes.subarray(0, 6), 0, bytes.length)).toMatchObject({ status: 200, body: { confirmedBytes: 6 } }) + expect((await f.put(id, Buffer.from('other '), 0, bytes.length)).status).toBe(409) + const path = String(f.database.prepare('SELECT temporary_path FROM managed_files').get()!.temporary_path) + await appendFile(path, 'unconfirmed') + await f.restart() + expect(await f.commands().get(id)).toMatchObject({ state: 'receiving', confirmedBytes: 6 }) + expect(await readFile(path, 'utf8')).toBe('first ') + expect((await f.put(id, bytes.subarray(6), 6, bytes.length)).status).toBe(200) + const envelope = f.envelope('complete', { uploadId: id }) + const ready = await f.commands().execute('uploads.complete', envelope) + expect(ready.receipt.decision).toBe('accepted') + expect(await f.commands().execute('uploads.complete', envelope)).toEqual({ created: false, receipt: ready.receipt }) + expect(await f.commands().get(id)).toMatchObject({ state: 'ready', contentVersion: hash(bytes) }) + expect(f.database.prepare('SELECT state FROM file_preparations').get()!.state).toBe('settled') + expect(createAttachmentPersistenceRepository(f.database).candidates()).toEqual([]) + }) + + it('stages input attachments without creating or modifying a composer draft and releases cancelled bytes', async () => { + const f = await fixture() + const before = f.repository.findById('draft') + const bytes = Buffer.from('Local input attachment 😀') + const request = f.envelope('stage-local', { inputId: 'local-input', file: { name: 'local.txt', mimeType: 'text/plain', sizeBytes: bytes.length, sha256: hash(bytes) } }) + const created = await f.commands().execute('uploads.stage', request) + const id = String(created.receipt.result.uploadId) + expect(await f.commands().execute('uploads.stage', request)).toEqual({ created: false, receipt: created.receipt }) + expect((await f.put(id, bytes, 0, bytes.length)).status).toBe(200) + await f.commands().execute('uploads.complete', f.envelope('complete-local', { uploadId: id })) + const ready = await f.commands().get(id) + expect(ready).toMatchObject({ state: 'ready', inputId: 'local-input', confirmedBytes: bytes.length, contentVersion: hash(bytes) }) + expect(ready.attachmentId).toBeTruthy() + const owner = createLocalCommandPrincipal(f.database).actorId + const ownViews = new ProductReadViews(f.database, owner, new ProductContentReferences(f.database, owner)) + const otherViews = new ProductReadViews(f.database, 'another-device', new ProductContentReferences(f.database, 'another-device')) + expect(ownViews.resource(`attachment:${ready.attachmentId}`)).toMatchObject({ availability: 'ready' }) + expect(otherViews.resource(`attachment:${ready.attachmentId}`)).toBeNull() + expect(() => f.repository.open({ ...f.draft, draftId: ready.draftId, scope: { kind: 'task', draftId: ready.draftId, spaceId: null }, initialContent: createBuddyUserContent('collision'), initialModelSelection: null, initialExecutionConfig: f.draft.executionConfig, now: new Date().toISOString() })).toThrow('draft revision conflicts') + expect(f.repository.findById('draft')).toEqual(before) + expect(f.database.prepare('SELECT count(*) AS count FROM composer_drafts').get()).toEqual({ count: 1 }) + expect(f.database.prepare('SELECT draft_id FROM composer_resources WHERE id = ?').get(ready.resourceId)).toEqual({ draft_id: ready.draftId }) + const forbidden = await f.commands().execute('uploads.adopt', f.envelope('wrong-adoption', { uploadId: id, draftId: 'draft', expectedRevision: 0 })) + expect(forbidden.receipt).toMatchObject({ decision: 'rejected', errorCode: 'DRAFT_CONFLICT' }) + await f.commands().execute('uploads.cancel', f.envelope('cancel-local', { uploadId: id })) + f.service().sweep() + expect(createAttachmentRepository(f.database).findById(ready.attachmentId!)).toBeNull() + expect(f.database.prepare('SELECT id FROM composer_resources WHERE id = ?').get(ready.resourceId)).toBeUndefined() + expect(f.repository.findById('draft')).toEqual(before) + }) + + it('keeps ready bytes on draft conflict and adopts once without overwriting text', async () => { + const f = await fixture() + const bytes = Buffer.from('attached') + const id = await f.create(bytes) + await f.put(id, bytes, 0, bytes.length) + await f.commands().execute('uploads.complete', f.envelope('complete', { uploadId: id })) + f.repository.save({ draftId: 'draft', expectedRevision: 0, content: createBuddyUserContent('新输入'), executionConfig: f.draft.executionConfig, modelSelection: null, now: new Date().toISOString() }) + const conflict = await f.commands().execute('uploads.adopt', f.envelope('stale', { uploadId: id, draftId: 'draft', expectedRevision: 0 })) + expect(conflict.receipt).toMatchObject({ decision: 'rejected', errorCode: 'DRAFT_CONFLICT' }) + expect(await f.commands().get(id)).toMatchObject({ state: 'ready' }) + const envelope = f.envelope('adopt', { uploadId: id, draftId: 'draft', expectedRevision: 1 }) + const adopted = await f.commands().execute('uploads.adopt', envelope) + expect(adopted.receipt).toMatchObject({ decision: 'accepted', result: { revision: 2 } }) + expect(await f.commands().execute('uploads.adopt', envelope)).toEqual({ created: false, receipt: adopted.receipt }) + expect(f.repository.findById('draft')!.content).toEqual(createBuddyUserContent('新输入')) + const attachment = createAttachmentRepository(f.database).findById(String(adopted.receipt.result.attachmentId))! + expect(await readFile(attachment.storedPath)).toEqual(bytes) + await f.commands().execute('uploads.cancel', f.envelope('cancel', { uploadId: id })) + expect(await f.commands().get(id)).toMatchObject({ state: 'adopted' }) + expect(createAttachmentPersistenceRepository(f.database).candidates()).toEqual([]) + }) + + it('rejects a corrupt confirmed prefix on restart and releases only its own file to GC', async () => { + const f = await fixture() + const bytes = Buffer.from('original') + const id = await f.create(bytes) + await f.put(id, bytes, 0, bytes.length) + const path = String(f.database.prepare('SELECT temporary_path FROM managed_files').get()!.temporary_path) + await writeFile(path, 'corrupt!') + await f.restart() + await expect(f.commands().get(id)).rejects.toThrow('RESOURCE_UNAVAILABLE') + expect(await f.commands().get(id)).toMatchObject({ state: 'failed' }) + expect(createAttachmentPersistenceRepository(f.database).candidates()).toHaveLength(1) + expect(f.database.prepare('SELECT reserved_bytes FROM file_preparations').get()!.reserved_bytes).toBe(0) + }) + + it('keeps bytes but confirms no prefix if the chunk transaction fails', async () => { + const f = await fixture() + const bytes = Buffer.from('transaction') + const id = await f.create(bytes) + f.database.exec('CREATE TRIGGER fail_chunk BEFORE INSERT ON remote_upload_chunks BEGIN SELECT RAISE(ABORT, \'fixture\'); END') + expect((await f.put(id, bytes, 0, bytes.length)).status).toBe(409) + expect(f.database.prepare('SELECT confirmed_bytes FROM remote_uploads').get()!.confirmed_bytes).toBe(0) + f.database.exec('DROP TRIGGER fail_chunk') + await f.restart() + expect(await f.commands().get(id)).toMatchObject({ confirmedBytes: 0 }) + expect((await f.put(id, bytes, 0, bytes.length)).status).toBe(200) + }) + + it('bounds reservations, rejects foreign owners and refuses acceptance after authorization loss', async () => { + const f = await fixture() + const bytes = Buffer.from('small') + const ids = await Promise.all([0, 1, 2, 3].map(index => f.create(bytes, `create-${index}`))) + await expect(f.create(bytes, 'overflow')).rejects.toThrow('LIMIT_EXCEEDED') + const reservation = f.database.prepare('SELECT SUM(reserved_bytes) AS bytes FROM file_preparations WHERE state = \'preparing\'').get()!.bytes + expect(reservation).toBe(20) + await f.commands().execute('uploads.cancel', f.envelope('cancel', { uploadId: ids[0] })) + await f.create(bytes, 'replacement') + f.revoke() + expect((await f.put(ids[1]!, bytes, 0, bytes.length)).status).toBe(409) + await expect(f.commands().execute('uploads.complete', f.envelope('revoked', { uploadId: ids[1] }))).rejects.toThrow() + expect(f.database.prepare('SELECT 1 FROM command_receipts WHERE command_id = \'revoked\'').get()).toBeUndefined() + }) +}) diff --git a/apps/buddy/service/src/privateNetwork/__tests__/privateNetworkAddress.spec.ts b/apps/buddy/service/src/privateNetwork/__tests__/privateNetworkAddress.spec.ts new file mode 100644 index 000000000..4c586c2a1 --- /dev/null +++ b/apps/buddy/service/src/privateNetwork/__tests__/privateNetworkAddress.spec.ts @@ -0,0 +1,20 @@ +import type { NetworkInterfaceInfo } from 'node:os' +import { describe, expect, it } from 'vitest' +import { selectPrivateNetworkAddress } from '../privateNetworkAddress' + +function address(value: string, internal = false): NetworkInterfaceInfo { + return { address: value, internal, family: value.includes(':') ? 'IPv6' : 'IPv4', netmask: '', mac: '', cidr: null } as NetworkInterfaceInfo +} +describe('automatic mobile connection address', () => { + it('prefers a LAN address over VPN and container interfaces', () => { + expect(selectPrivateNetworkAddress({ docker0: [address('172.17.0.1')], tailscale0: [address('100.88.0.1')], wlan0: [address('192.168.1.20')] })).toBe('192.168.1.20') + }) + it('supports a VPN-only host and private IPv6', () => { + expect(selectPrivateNetworkAddress({ tailscale0: [address('100.88.0.1')] })).toBe('100.88.0.1') + expect(selectPrivateNetworkAddress({ en0: [address('fd00::2')] })).toBe('fd00::2') + }) + it('does not expose a public, wildcard, link-local or loopback listener', () => { + expect(selectPrivateNetworkAddress({ lo: [address('127.0.0.1'), address('::1', true)], en0: ['8.8.8.8', '0.0.0.0', '::', 'fe80::1'].map(value => address(value)) })).toBeNull() + expect(selectPrivateNetworkAddress({})).toBeNull() + }) +}) diff --git a/apps/buddy/service/src/privateNetwork/privateNetworkAddress.ts b/apps/buddy/service/src/privateNetwork/privateNetworkAddress.ts new file mode 100644 index 000000000..24722b4ba --- /dev/null +++ b/apps/buddy/service/src/privateNetwork/privateNetworkAddress.ts @@ -0,0 +1,13 @@ +import type { NetworkInterfaceInfo } from 'node:os' +import { networkInterfaces } from 'node:os' +import { isPrivateNetworkAddress } from './PrivateNetworkTlsIdentity' + +export function selectPrivateNetworkAddress(interfaces: NodeJS.Dict = networkInterfaces()): string | null { + return Object.entries(interfaces) + .filter(([name]) => !/^(?:docker\d*|veth|br-|virbr|vmnet|vboxnet)/i.test(name)) + .flatMap(([, addresses]) => addresses ?? []) + .filter(item => !item.internal && !item.address.startsWith('127.') && item.address !== '::1' && isPrivateNetworkAddress(item.address)) + .sort((a, b) => Number(a.family !== 'IPv4') - Number(b.family !== 'IPv4') + || Number(/^100\./.test(a.address)) - Number(/^100\./.test(b.address)))[0] + ?.address ?? null +} diff --git a/apps/buddy/service/src/privateNetwork/revokeRestoredDeviceGrants.ts b/apps/buddy/service/src/privateNetwork/revokeRestoredDeviceGrants.ts new file mode 100644 index 000000000..ae9c20fbf --- /dev/null +++ b/apps/buddy/service/src/privateNetwork/revokeRestoredDeviceGrants.ts @@ -0,0 +1,19 @@ +import type { DatabaseSync } from 'node:sqlite' + +export function revokeRestoredDeviceGrants(database: DatabaseSync, now = Date.now()): void { + if (!database.prepare('SELECT 1 FROM sqlite_master WHERE type = \'table\' AND name = \'remote_devices\'').get()) + return + database.prepare('UPDATE remote_devices SET state = \'revoked\', grant_generation = grant_generation + 1 WHERE state IN (\'pending\',\'active\')').run() + database.prepare('UPDATE storage_actors SET state = \'revoked\' WHERE kind = \'device\'').run() + database.prepare('UPDATE pairing_attempts SET closed_ms = COALESCE(closed_ms, ?)').run(now) + database.prepare('UPDATE pairing_tickets SET state = \'cancelled\' WHERE state = \'pending\'').run() + if (database.prepare('SELECT 1 FROM sqlite_master WHERE type = \'table\' AND name = \'remote_uploads\'').get()) { + database.prepare('UPDATE remote_uploads SET state = \'cancelled\' WHERE state IN (\'receiving\',\'ready\')').run() + database.prepare('DELETE FROM file_references WHERE kind = \'export\' AND owner_id IN (SELECT id FROM remote_uploads WHERE state = \'cancelled\')').run() + } + if (database.prepare('SELECT 1 FROM sqlite_master WHERE type = \'table\' AND name = \'remote_exports\'').get()) { + database.prepare('UPDATE remote_exports SET state = \'expired\' WHERE state IN (\'preparing\',\'ready\')').run() + database.prepare('DELETE FROM file_references WHERE kind = \'export\' AND owner_id IN (SELECT id FROM remote_exports)').run() + } + database.prepare('UPDATE private_network_state SET last_observed_ms = MAX(last_observed_ms, ?) WHERE id = 1').run(now) +} diff --git a/apps/buddy/service/src/privateNetwork/uploadValidation.ts b/apps/buddy/service/src/privateNetwork/uploadValidation.ts new file mode 100644 index 000000000..f614a0932 --- /dev/null +++ b/apps/buddy/service/src/privateNetwork/uploadValidation.ts @@ -0,0 +1,15 @@ +import { Buffer } from 'node:buffer' +import { open } from 'node:fs/promises' +import { ProductProtocolError } from '../../../shared/productProtocol/errors' +import { hasDocumentSignature } from '../attachments/validateDocumentBytes' + +export async function validateDocumentBytes(path: string, mimeType: string): Promise { + const file = await open(path, 'r') + try { + const bytes = Buffer.alloc(1024) + const { bytesRead } = await file.read(bytes) + if (!hasDocumentSignature(mimeType, bytes.subarray(0, bytesRead))) + throw new ProductProtocolError('RESOURCE_UNAVAILABLE') + } + finally { await file.close() } +} diff --git a/apps/buddy/service/src/productProtocol/ProductActorCommandScope.ts b/apps/buddy/service/src/productProtocol/ProductActorCommandScope.ts new file mode 100644 index 000000000..bb4577c8b --- /dev/null +++ b/apps/buddy/service/src/productProtocol/ProductActorCommandScope.ts @@ -0,0 +1,116 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { ApprovalService } from '../approvals/ApprovalService' +import type { TrustedCommandPrincipal } from '../commands/CommandAdmissionService' +import type { RemoteUploadService } from '../privateNetwork/RemoteUploadService' +import type { UserQuestionService } from '../questions/UserQuestionService' +import { ProductProtocolError } from '../../../shared/productProtocol/errors' +import { ChatControlCommandService } from '../chat/ChatControlCommandService' +import { ChatInputCommandService } from '../chat/ChatInputCommandService' +import { ComposerDraftCommandService } from '../chat/ComposerDraftCommandService' +import { PreparedRunCommandService } from '../chat/PreparedRunCommandService' +import { CommandAdmissionService } from '../commands/CommandAdmissionService' +import { DesktopOpenCommandService } from '../commands/DesktopOpenCommandService' +import { ConversationCommandService } from '../conversations/ConversationCommandService' +import { ConversationCommandRepository } from '../storage/ConversationCommandRepository' + +export interface ProductActorCommands { + identity: { dataGeneration: string, serviceInstanceId: string } + admission: CommandAdmissionService + desktop: DesktopOpenCommandService + input: ChatInputCommandService + drafts: ComposerDraftCommandService + control: ChatControlCommandService + metadata: ConversationCommandService + uploads: ReturnType + approvals: Pick + questions: { execute: (method: 'questions.answer' | 'questions.cancel' | 'questions.retry', input: unknown) => ReturnType } +} + +type InputOptions = ConstructorParameters[0] +type DraftOptions = ConstructorParameters[0] +type ControlOptions = ConstructorParameters[0] +type ConversationOptions = ConstructorParameters[0] + +interface ProductActorCommandDependencies { + runs: Required[0], 'turns' | 'events'>> + input: Pick + drafts: Pick & { access: DraftOptions['drafts'] & InputOptions['drafts'] } + conversations: Pick + control: Pick & { stopExecution: () => Promise } + uploads: Pick + questions: Pick + approvals: Pick + openDesktop: ConstructorParameters[0]['openDesktop'] + onError: (error: unknown) => void +} + +export class ProductActorCommandScope { + readonly admission: CommandAdmissionService + readonly #database: DatabaseSync + readonly #principal: TrustedCommandPrincipal + readonly #identity: ProductActorCommands['identity'] + #commands: ProductActorCommands | undefined + #preparedRuns: PreparedRunCommandService | undefined + #disposed: Promise | undefined + + constructor(database: DatabaseSync, principal: TrustedCommandPrincipal, identity: ProductActorCommands['identity']) { + this.#database = database + this.#principal = principal + this.#identity = identity + this.admission = new CommandAdmissionService({ database, principal, sourceInstanceId: identity.serviceInstanceId }) + } + + get hasPendingOperations(): boolean { + return this.admission.hasPendingOperations + } + + initialize(dependencies: ProductActorCommandDependencies): ProductActorCommands { + if (this.#disposed) + throw new ProductProtocolError('SERVICE_NOT_READY') + if (this.#commands) + return this.#commands + const database = this.#database + const principal = this.#principal + const admission = this.admission + const onError = dependencies.onError + const preparedRuns = this.#preparedRuns = new PreparedRunCommandService({ database, sourceInstanceId: this.#identity.serviceInstanceId, ...dependencies.runs, onObserverError: onError }) + const drafts = new ComposerDraftCommandService({ database, actorId: principal.actorId, admission, drafts: dependencies.drafts.access, prepareAttachments: dependencies.drafts.prepareAttachments, releaseResources: dependencies.drafts.releaseResources, onCleanupError: onError }) + const input = new ChatInputCommandService({ database, actorId: principal.actorId, admission, drafts: dependencies.drafts.access, ...dependencies.input, preparedRuns, commitSubmittedInput: input => dependencies.uploads.commitInput(principal, input), onDispatchError: onError }) + const metadata = new ConversationCommandService({ database, admission, repository: new ConversationCommandRepository(database), ...dependencies.conversations, onEffectError: onError }) + const control = new ChatControlCommandService({ database, admission, repository: dependencies.control.repository, queue: dependencies.control.queue, onDispatchError: onError, onServiceStop: async () => { + this.stopAccepting() + await dependencies.control.stopExecution() + } }) + this.#commands = { + identity: this.#identity, + admission, + desktop: new DesktopOpenCommandService({ database, admission, openDesktop: dependencies.openDesktop }), + input, + drafts, + control, + metadata, + uploads: dependencies.uploads.commands(principal, admission), + questions: { execute: (method, input) => dependencies.questions.executeCommand(method, input, admission) }, + approvals: { getReview: id => dependencies.approvals.getReview(id), resolveCommand: value => dependencies.approvals.resolveCommand(value, admission) }, + } + return this.#commands + } + + stopAccepting(): void { + this.admission.stopAccepting() + } + + dispose(): Promise { + return this.#disposed ??= this.#dispose() + } + + async #dispose(): Promise { + this.stopAccepting() + await this.admission.dispose() + if (this.#commands) { + const { input, control, drafts, metadata } = this.#commands + await Promise.all([input.waitForDispatches(), control.waitForEffects(), drafts.dispose(), metadata.dispose()]) + } + this.#preparedRuns?.dispose() + } +} diff --git a/apps/buddy/service/src/productProtocol/ProductBuddyAvatars.ts b/apps/buddy/service/src/productProtocol/ProductBuddyAvatars.ts new file mode 100644 index 000000000..af5bed9b6 --- /dev/null +++ b/apps/buddy/service/src/productProtocol/ProductBuddyAvatars.ts @@ -0,0 +1,32 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { z } from 'zod' +import type { productAvatarSchema } from '../../../shared/productProtocol/avatars' +import { Buffer } from 'node:buffer' +import { createHash } from 'node:crypto' +import { BUDDY_AVATAR_MAX_BYTES, buddyAvatarSchema } from '../../../shared/buddies/buddyApi' + +export class ProductBuddyAvatars { + readonly #database: DatabaseSync + constructor(database: DatabaseSync) { + this.#database = database + } + + metadata(buddyIds: string[]) { + return { items: [...new Set(buddyIds)].map(buddyId => ({ buddyId, avatar: this.read(buddyId).avatar })) } + } + + read(buddyId: string): { avatar: z.infer, bytes?: Buffer } { + const row = this.#database.prepare('SELECT avatar FROM buddies WHERE id = ?').get(buddyId) + const parsed = buddyAvatarSchema.safeParse(row?.avatar ?? null) + const source = parsed.success ? parsed.data : null + if (!source) + return { avatar: { kind: 'default' } } + if (source.startsWith('preset:')) + return { avatar: { kind: 'preset', preset: source.slice(7) as Extract, { kind: 'preset' }>['preset'] } } + const [header, data] = source.split(',') + const bytes = Buffer.from(data!, 'base64') + if (!bytes.length || bytes.length > BUDDY_AVATAR_MAX_BYTES || bytes.toString('base64') !== data) + return { avatar: { kind: 'default' } } + return { avatar: { kind: 'image', sha256: createHash('sha256').update(bytes).digest('hex'), mimeType: header!.slice(5, -7) as 'image/png' | 'image/jpeg' | 'image/webp', sizeBytes: bytes.length }, bytes } + } +} diff --git a/apps/buddy/service/src/productProtocol/ProductConnection.ts b/apps/buddy/service/src/productProtocol/ProductConnection.ts new file mode 100644 index 000000000..3159160f0 --- /dev/null +++ b/apps/buddy/service/src/productProtocol/ProductConnection.ts @@ -0,0 +1,180 @@ +import type { z } from 'zod' +import type { TrustedProductContext } from '../../../shared/productProtocol/identity' +import type { ProductMethod } from '../../../shared/productProtocol/methods' +import type { ProductSyncPublisher } from './ProductSyncPublisher' +import { z as schema } from 'zod' +import { ProductProtocolError, projectProductError } from '../../../shared/productProtocol/errors' +import { PRODUCT_PROTOCOL_VERSION, productIdentitySchema, productInitializeResultSchema, productInitializeSchema } from '../../../shared/productProtocol/identity' +import { encodeProductFrame, PRODUCT_LIMITS, productByteLength } from '../../../shared/productProtocol/limits' +import { getProductMethod } from '../../../shared/productProtocol/methods' +import { productAckSchema, productSnapshotPageSchema, productSubscribeSchema, productUnsubscribeSchema } from '../../../shared/productProtocol/sync' + +const requestSchema = schema.object({ jsonrpc: schema.literal('2.0'), id: schema.string().min(1).max(256), method: schema.string().min(1).max(256), params: schema.unknown() }).strict() +export type ProductClientCapabilities = Pick, 'supportedContentKinds' | 'supportedReviewKinds'> +export interface ProductRequestHandler { + handle: (input: unknown, context: Readonly, client: ProductClientCapabilities) => unknown | Promise +} +export interface ProductConnectionOptions { + context: TrustedProductContext + assertCurrent: () => void + phase: () => 'starting' | 'recovering' | 'ready' | 'stopping' + handlers: Partial> + sync?: ProductSyncPublisher + authorizeTopic?: (topic: string, context: Readonly) => void + send: (frame: string) => boolean + close: () => void +} + +export class ProductConnection { + readonly #options: ProductConnectionOptions + readonly #context: Readonly + readonly #pending = new Set() + readonly #disconnectSync?: () => void + #pendingBytes = 0 + #client: ProductClientCapabilities | null = null + #closed = false + + constructor(options: ProductConnectionOptions) { + this.#options = options + this.#context = Object.freeze({ ...options.context }) + productIdentitySchema.parse(Object.fromEntries(Object.keys(productIdentitySchema.shape).map(key => [key, options.context[key as keyof TrustedProductContext]]))) + this.#disconnectSync = options.sync?.connect(this.#context.connectionId, { send: frame => this.#send(frame), close: () => this.close() }) + } + + async receive(raw: string): Promise { + if (this.#closed) + return + let id: string | null = null + let dispatched = false + let completed = false + let tracked = false + let bytes = 0 + try { + if (raw.length > PRODUCT_LIMITS.frameBytes || productByteLength(raw) > PRODUCT_LIMITS.frameBytes) + throw new ProductProtocolError('FRAME_TOO_LARGE') + bytes = productByteLength(raw) + if (this.#pendingBytes + bytes > PRODUCT_LIMITS.connectionBytes) + throw new ProductProtocolError('FRAME_TOO_LARGE') + let parsed: unknown + try { + parsed = JSON.parse(raw) + } + catch { throw new ProductProtocolError('INVALID_REQUEST') } + const request = requestSchema.safeParse(parsed) + if (!request.success) + throw new ProductProtocolError('INVALID_REQUEST') + id = request.data.id + if (this.#pending.has(id)) + throw new ProductProtocolError('INVALID_REQUEST', 'unknown') + if (this.#pending.size >= PRODUCT_LIMITS.inFlightRequests) + throw new ProductProtocolError('INVALID_REQUEST') + this.#pending.add(id) + tracked = true + this.#pendingBytes += bytes + this.#options.assertCurrent() + const { method, params } = request.data + if (method === 'initialize') { + if (this.#client) + throw new ProductProtocolError('ALREADY_INITIALIZED') + const initialized = productInitializeSchema.safeParse(params) + if (!initialized.success) + throw new ProductProtocolError('INVALID_PARAMS') + if (initialized.data.protocol.major !== PRODUCT_PROTOCOL_VERSION.major || initialized.data.protocol.minMinor > PRODUCT_PROTOCOL_VERSION.minor || initialized.data.protocol.maxMinor < PRODUCT_PROTOCOL_VERSION.minor) + throw new ProductProtocolError('PROTOCOL_INCOMPATIBLE') + this.#client = Object.freeze({ supportedContentKinds: [...initialized.data.supportedContentKinds], supportedReviewKinds: [...initialized.data.supportedReviewKinds] }) + const identity = productIdentitySchema.parse(Object.fromEntries(Object.keys(productIdentitySchema.shape).map(key => [key, this.#context[key as keyof TrustedProductContext]]))) + const capabilities = [...Object.keys(this.#options.handlers), ...(this.#options.sync ? ['sync.subscribe', 'sync.snapshotPage', 'sync.ack', 'sync.unsubscribe'] : [])].filter(method => this.#context.role === 'owner' || getProductMethod(method)?.role === 'reader' || method.startsWith('sync.')) + this.#reply(id, productInitializeResultSchema.parse({ ...identity, protocol: PRODUCT_PROTOCOL_VERSION, serverTime: new Date().toISOString(), phase: this.#options.phase(), capabilities, limits: PRODUCT_LIMITS })) + return + } + if (!this.#client) + throw new ProductProtocolError('NOT_INITIALIZED') + if (method === 'sync.subscribe' && this.#options.sync) { + const request = productSubscribeSchema.safeParse(params) + if (!request.success) + throw new ProductProtocolError('INVALID_PARAMS') + if (!this.#options.authorizeTopic) + throw new ProductProtocolError('NOT_AUTHORIZED') + this.#options.authorizeTopic(request.data.topic, this.#context) + const subscription = this.#options.sync.subscribe(this.#context.connectionId, request.data) + if (this.#reply(id, subscription.snapshot)) + subscription.activate() + return + } + if (method === 'sync.snapshotPage' && this.#options.sync) { + const request = productSnapshotPageSchema.safeParse(params) + if (!request.success) + throw new ProductProtocolError('INVALID_PARAMS') + const page = this.#options.sync.snapshotPage(this.#context.connectionId, request.data) + if (this.#reply(id, page.snapshot)) + page.activate() + return + } + if (method === 'sync.ack' && this.#options.sync) { + const request = productAckSchema.safeParse(params) + if (!request.success) + throw new ProductProtocolError('INVALID_PARAMS') + this.#options.sync.ack(this.#context.connectionId, request.data) + this.#reply(id, { acknowledged: true }) + return + } + if (method === 'sync.unsubscribe' && this.#options.sync) { + const request = productUnsubscribeSchema.safeParse(params) + if (!request.success) + throw new ProductProtocolError('INVALID_PARAMS') + this.#options.sync.unsubscribe(this.#context.connectionId, request.data.subscriptionId) + this.#reply(id, { unsubscribed: true }) + return + } + const contract = getProductMethod(method) + const handler = this.#options.handlers[method as ProductMethod] + if (!contract || !handler) + throw new ProductProtocolError('METHOD_NOT_AVAILABLE') + if (contract.role === 'owner' && this.#context.role !== 'owner') + throw new ProductProtocolError('NOT_AUTHORIZED') + if (contract.ready && this.#options.phase() !== 'ready') + throw new ProductProtocolError('SERVICE_NOT_READY') + const input = contract.input.safeParse(params) + if (!input.success) + throw new ProductProtocolError('INVALID_PARAMS') + dispatched = contract.command || method === 'commands.get' + const output = await handler.handle(input.data, this.#context, this.#client) + completed = true + this.#options.assertCurrent() + this.#reply(id, contract.output.parse(output)) + } + catch (error) { + this.#send(encodeProductFrame({ jsonrpc: '2.0', id, error: projectProductError(error, dispatched, completed && dispatched) })) + } + finally { + if (tracked && id) { + this.#pendingBytes -= bytes + this.#pending.delete(id) + } + } + } + + close(): void { + if (this.#closed) + return + this.#closed = true + this.#disconnectSync?.() + this.#options.close() + } + + #reply(id: string, result: unknown): boolean { + return this.#send(encodeProductFrame({ jsonrpc: '2.0', id, result })) + } + + #send(frame: string): boolean { + if (this.#closed) + return false + try { + if (this.#options.send(frame)) + return true + } + catch {} + this.close() + return false + } +} diff --git a/apps/buddy/service/src/productProtocol/ProductContentReferences.ts b/apps/buddy/service/src/productProtocol/ProductContentReferences.ts new file mode 100644 index 000000000..9b7b522e5 --- /dev/null +++ b/apps/buddy/service/src/productProtocol/ProductContentReferences.ts @@ -0,0 +1,70 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { BuddyResourceQuote, BuddyUserContentV1 } from '../../../shared/conversation/buddyUserContent' +import { createHash } from 'node:crypto' +import { buddyResourceQuoteSchema } from '../../../shared/conversation/buddyUserContent' +import { projectProductContent } from '../../../shared/productProtocol/content' +import { ProductProtocolError } from '../../../shared/productProtocol/errors' +import { canonicalCommandJson } from '../commands/commandIntent' +import { requireSqliteAuthority } from '../storage/sqliteAuthority' +import { assertCommittedRead, withTransaction } from '../storage/transaction' + +export interface ProductContentReferenceSource { + kind: 'conversation' | 'draft' + id: string +} + +export class ProductContentReferences { + readonly #database: DatabaseSync + readonly #actorId: string + + constructor(database: DatabaseSync, actorId: string) { + this.#database = database + this.#actorId = actorId + } + + project(content: BuddyUserContentV1, source?: ProductContentReferenceSource) { + return withTransaction(this.#database, () => { + const owner = source ? this.#owner(source) : null + return projectProductContent(content, (quote) => { + const dataGeneration = requireSqliteAuthority(this.#database).dataGeneration + const bytes = canonicalCommandJson(buddyResourceQuoteSchema.parse(quote)) + const resourceId = createHash('sha256').update(canonicalCommandJson([this.#actorId, dataGeneration, quote])).digest('hex') + const existing = this.#database.prepare('SELECT quote_json, owner_actor_id, data_generation FROM product_content_references WHERE id = ?').get(resourceId) + if (existing && (existing.quote_json !== bytes || existing.owner_actor_id !== this.#actorId || existing.data_generation !== dataGeneration)) + throw new ProductProtocolError('SOURCE_CHANGED') + if (!existing) { + this.#database.prepare(`INSERT INTO product_content_references(id, owner_actor_id, data_generation, quote_json, created_at) + VALUES (?, ?, ?, ?, ?)`).run(resourceId, this.#actorId, dataGeneration, bytes, new Date().toISOString()) + } + if (owner) { + this.#database.prepare(`INSERT OR IGNORE INTO product_content_reference_owners (reference_id, owner_kind, owner_id) + VALUES (?, ?, ?)`).run(resourceId, owner.kind, owner.id) + } + return { resourceId, title: 'title' in quote.source ? quote.source.title : '引用' } + }) + }) + } + + #owner(source: ProductContentReferenceSource): ProductContentReferenceSource { + if (source.kind === 'draft') { + const draft = this.#database.prepare('SELECT conversation_id FROM composer_drafts WHERE id = ?').get(source.id) + if (!draft) + throw new ProductProtocolError('RESOURCE_UNAVAILABLE') + if (!draft.conversation_id) + return source + source = { kind: 'conversation', id: String(draft.conversation_id) } + } + if (!this.#database.prepare('SELECT 1 FROM conversations WHERE id = ? AND deleted_at IS NULL').get(source.id)) + throw new ProductProtocolError('RESOURCE_UNAVAILABLE') + return source + } + + resolve(resourceId: string): BuddyResourceQuote { + assertCommittedRead(this.#database) + const dataGeneration = requireSqliteAuthority(this.#database).dataGeneration + const record = this.#database.prepare('SELECT quote_json FROM product_content_references WHERE id = ? AND owner_actor_id = ? AND data_generation = ?').get(resourceId, this.#actorId, dataGeneration) + if (!record) + throw new ProductProtocolError('RESOURCE_UNAVAILABLE') + return buddyResourceQuoteSchema.parse(JSON.parse(String(record.quote_json))) + } +} diff --git a/apps/buddy/service/src/productProtocol/ProductDraftReadModel.ts b/apps/buddy/service/src/productProtocol/ProductDraftReadModel.ts new file mode 100644 index 000000000..c5741edd7 --- /dev/null +++ b/apps/buddy/service/src/productProtocol/ProductDraftReadModel.ts @@ -0,0 +1,46 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { BuddyComposerDraft } from '../../../shared/conversation/composerDraft' +import type { ProductContentReferences } from './ProductContentReferences' +import type { ProductReadViews } from './ProductReadViews' +import { PRODUCT_LIMITS, productByteLength } from '../../../shared/productProtocol/limits' +import { productDraftSchema } from '../../../shared/productProtocol/queries' +import { encodeProductRevision } from '../../../shared/productProtocol/scalars' +import { createComposerDraftRepository, findPersonalComposerDraft } from '../storage/composerDraftRepository' +import { createDraftOwnershipRepository } from '../storage/draftOwnershipRepository' + +export class ProductDraftReadModel { + readonly #database: DatabaseSync + readonly #actorId: string + readonly #references: ProductContentReferences + readonly #resource: ProductReadViews['resource'] + + constructor(database: DatabaseSync, actorId: string, references: ProductContentReferences, resource: ProductReadViews['resource']) { + this.#database = database + this.#actorId = actorId + this.#references = references + this.#resource = resource + } + + get(id: string) { + const draft = findPersonalComposerDraft(this.#database, id) + return draft ? this.#projectDraft(draft) : null + } + + find(scope: BuddyComposerDraft['scope']) { + const slot = createDraftOwnershipRepository(this.#database, this.#actorId).findSlot(scope) + const draft = slot?.draftId ? createComposerDraftRepository(this.#database, this.#actorId).findById(slot.draftId) : null + return { draft: draft ? this.#projectDraft(draft) : null, slotRevision: encodeProductRevision(slot?.slotRevision ?? 0) } + } + + #projectDraft(draft: BuddyComposerDraft) { + const content = this.#references.project(draft.content, { kind: 'draft', id: draft.draftId }) + const projected = productByteLength(JSON.stringify(content)) > PRODUCT_LIMITS.pageBytes / 2 + ? { kind: 'unsupported' as const, contentReference: `draft:${draft.draftId}:${draft.revision}`, version: 1, summary: '', readOnly: true as const } + : { kind: 'editable' as const, content } + const resources = this.#database.prepare('SELECT id, attachment_id FROM composer_resources WHERE draft_id = ? ORDER BY created_at, id LIMIT 16').all(draft.draftId).flatMap((row) => { + const resource = row.attachment_id ? this.#resource(`attachment:${String(row.attachment_id)}`) : null + return resource ? [{ ...resource, bindingId: String(row.id) }] : [] + }) + return productDraftSchema.parse({ ...draft, resources, ownerDeviceId: String(this.#database.prepare('SELECT owner_actor_id FROM composer_drafts WHERE id = ?').get(draft.draftId)!.owner_actor_id), writable: Boolean(this.#database.prepare('SELECT 1 FROM composer_drafts WHERE id = ? AND owner_actor_id = ?').get(draft.draftId, this.#actorId)), revision: encodeProductRevision(draft.revision), content: projected }) + } +} diff --git a/apps/buddy/service/src/productProtocol/ProductPageReader.ts b/apps/buddy/service/src/productProtocol/ProductPageReader.ts new file mode 100644 index 000000000..fa7becda4 --- /dev/null +++ b/apps/buddy/service/src/productProtocol/ProductPageReader.ts @@ -0,0 +1,50 @@ +import type { DatabaseSync, SQLInputValue } from 'node:sqlite' +import { createHash } from 'node:crypto' +import { ProductProtocolError } from '../../../shared/productProtocol/errors' +import { encodeProductFrame, PRODUCT_LIMITS, productByteLength } from '../../../shared/productProtocol/limits' +import { decodeProductCursor, encodeProductCursor, encodeProductRevision } from '../../../shared/productProtocol/scalars' +import { productQueryRevision } from './ProductQueryRevisions' + +export class ProductPageReader { + readonly #database: DatabaseSync + readonly #instance: string + + constructor(database: DatabaseSync, instance: string) { + this.#database = database + this.#instance = instance + } + + read(scope: string, input: Record, table: string, condition: string, params: SQLInputValue[], project: ((id: string) => unknown) | { batch: (ids: readonly string[]) => ReadonlyMap }, fixedRevision?: string) { + const revision = fixedRevision ?? productQueryRevision(this.#database, scope.split(':')[0]!, input) + const cursorScope = createHash('sha256').update(JSON.stringify([scope, this.#instance, params.map(value => typeof value === 'bigint' ? value.toString() : value)])).digest('hex') + let upperBound = encodeProductRevision(Number(this.#database.prepare(`SELECT COALESCE(MAX(rowid),0) AS value FROM ${table}`).get()!.value)) + let position = '0' + if (input.cursor) { + try { + const cursor = decodeProductCursor(String(input.cursor), { scope: cursorScope, branchId: null, revision }) + upperBound = cursor.upperBound + position = cursor.position + } + catch { throw new ProductProtocolError('CURSOR_EXPIRED') } + } + const rows = this.#database.prepare(`SELECT id, rowid AS position FROM ${table} WHERE (${condition}) AND rowid > ? AND rowid <= ? ORDER BY rowid LIMIT ?`).all(...params, BigInt(position), BigInt(upperBound), Number(input.limit) + 1) + const selected = rows.slice(0, Number(input.limit)) + const projected = typeof project === 'function' ? null : project.batch(selected.map(row => String(row.id))) + const items: unknown[] = [] + for (const row of selected) { + const item = typeof project === 'function' ? project(String(row.id)) : projected!.get(String(row.id)) ?? null + if (item !== null && productByteLength(JSON.stringify([...items, item])) > PRODUCT_LIMITS.pageBytes - 4096) { + if (!items.length) + throw new ProductProtocolError('PAGE_TOO_LARGE') + break + } + if (item !== null) + items.push(item) + position = encodeProductRevision(Number(row.position)) + } + const more = rows.some(row => BigInt(Number(row.position)) > BigInt(position)) + const page = { items, nextCursor: more ? encodeProductCursor({ version: 1, scope: cursorScope, branchId: null, revision, upperBound, position }) : null } + encodeProductFrame(page, PRODUCT_LIMITS.pageBytes) + return page + } +} diff --git a/apps/buddy/service/src/productProtocol/ProductQueries.ts b/apps/buddy/service/src/productProtocol/ProductQueries.ts new file mode 100644 index 000000000..26e4c86e2 --- /dev/null +++ b/apps/buddy/service/src/productProtocol/ProductQueries.ts @@ -0,0 +1,261 @@ +import type { BuddyResourceQuote } from '../../../shared/conversation/buddyUserContent' +import type { BuddyComposerDraft } from '../../../shared/conversation/composerDraft' +import type { ProductMethod } from '../../../shared/productProtocol/methods' +import type { ProductTopic } from '../../../shared/productProtocol/topics' +import type { ProductRequestHandler } from './ProductConnection' +import { createHash } from 'node:crypto' +import { hostname, platform } from 'node:os' +import { DatabaseSync } from 'node:sqlite' +import { productErrorCodeSchema, ProductProtocolError } from '../../../shared/productProtocol/errors' +import { PRODUCT_LIMITS, productByteLength } from '../../../shared/productProtocol/limits' +import { PRODUCT_QUERY_METHODS, productRunSchema } from '../../../shared/productProtocol/queries' +import { decodeProductCursor, encodeProductCursor, encodeProductRevision } from '../../../shared/productProtocol/scalars' +import { parseProductTopic } from '../../../shared/productProtocol/topics' +import { toPublicRun } from '../runs/publicRun' +import { ConversationCommandRepository } from '../storage/ConversationCommandRepository' +import { createConversationRepository } from '../storage/conversationRepository' +import { createRunInputRepository } from '../storage/runInputRepository' +import { createRunRepository } from '../storage/runRepository' +import { createTaskMarkRepository } from '../storage/taskMarkRepository' +import { assertCommittedRead, withTransaction } from '../storage/transaction' +import { ProductBuddyAvatars } from './ProductBuddyAvatars' +import { ProductContentReferences } from './ProductContentReferences' +import { ProductDraftReadModel } from './ProductDraftReadModel' +import { ProductPageReader } from './ProductPageReader' +import { productQueryRevision } from './ProductQueryRevisions' +import { ProductReadViews } from './ProductReadViews' +import { ProductStreamingMessages } from './ProductStreamingMessages' +import { ProductTaskReadModel } from './ProductTaskReadModel' + +export class ProductQueries { + readonly #database: DatabaseSync + readonly #actorId: string + readonly #instance: string + readonly #references: ProductContentReferences + readonly #hasReview: (approvalId: string, version: string) => boolean + readonly views: ProductReadViews + readonly streaming: ProductStreamingMessages + readonly #tasks: ProductTaskReadModel + readonly #pages: ProductPageReader + readonly #drafts: ProductDraftReadModel + + constructor(database: DatabaseSync, actorId: string, serviceInstanceId: string, references?: ProductContentReferences, hasReview: (approvalId: string, version: string) => boolean = () => false) { + this.#database = database + this.#actorId = actorId + this.#hasReview = hasReview + this.#instance = serviceInstanceId + this.#references = references ?? new ProductContentReferences(database, actorId) + this.views = new ProductReadViews(database, actorId, this.#references, hasReview) + this.streaming = new ProductStreamingMessages(database) + this.#tasks = new ProductTaskReadModel(database, message => this.views.messagePreview(message)) + this.#pages = new ProductPageReader(database, serviceInstanceId) + this.#drafts = new ProductDraftReadModel(database, actorId, this.#references, id => this.views.resource(id)) + } + + resolveQuote(resourceId: string): BuddyResourceQuote { + return this.#references.resolve(resourceId) + } + + draft(id: string) { + return this.#drafts.get(id) + } + + task(id: string, summary = false) { + return summary ? this.#tasks.summaries([id]).get(id) ?? null : this.#tasks.get(id) + } + + openSnapshot(name: string) { + const topic = parseProductTopic(name) + assertCommittedRead(this.#database) + const file = this.#database.prepare('PRAGMA database_list').all().find(row => row.name === 'main')?.file + if (typeof file !== 'string' || !file) + throw new ProductProtocolError('RESYNC_REQUIRED') + const database = new DatabaseSync(file, { readOnly: true }) + let closed = false + const close = () => { + if (closed) + return + closed = true + try { + database.exec('ROLLBACK') + } + finally { database.close() } + } + try { + database.exec('PRAGMA query_only = ON') + database.exec('BEGIN') + database.prepare('SELECT data_generation FROM storage_profile WHERE id = 1').get() + const queries = new ProductQueries(database, this.#actorId, this.#instance, this.#references, this.#hasReview) + return { close, read: (cursor: string | null) => { + if (closed) + throw new ProductProtocolError('CURSOR_EXPIRED') + const page = queries.#topicPage(topic, cursor) + return { ...page, complete: page.nextCursor === null } + } } + } + catch (error) { + close() + throw error + } + } + + snapshot(name: string) { + const topic = parseProductTopic(name) + assertCommittedRead(this.#database) + return withTransaction(this.#database, () => { + const page = this.#topicPage(topic, null) + return { ...page, complete: page.nextCursor === null } + }) + } + + read(method: keyof typeof PRODUCT_QUERY_METHODS, input: Record): unknown { + assertCommittedRead(this.#database) + return withTransaction(this.#database, () => this.#read(method, input)) + } + + 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))) + throw new ProductProtocolError('OBJECT_DELETED') + } + + #topicPage(topic: ProductTopic, cursor: string | null): { items: unknown[], nextCursor: string | null } { + if (topic.kind === 'draft') { + const draft = this.draft(topic.draftId) + return { items: draft ? [draft] : [], nextCursor: null } + } + if (topic.kind === 'serviceState') + 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 !== '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 } + } + let phase = 0 + let nested: string | null = null + if (cursor) { + const match = /^([0-5])\.(.*)$/.exec(cursor) + if (!match) + throw new ProductProtocolError('CURSOR_EXPIRED') + phase = Number(match[1]) + nested = match[2] ? decodeURIComponent(match[2]) : null + } + const stages = ['task', 'run', 'queue', 'approval', 'message', 'message'] as const + const input = { conversationId: topic.conversationId, branchId: topic.branchId, limit: PRODUCT_LIMITS.pageItems, cursor: nested } + const entity = stages[phase]! + const page = phase === 0 + ? { items: [this.task(topic.conversationId)].filter(Boolean), nextCursor: null } + : phase === 5 + ? this.streaming.page(topic.conversationId, topic.branchId, nested) + : this.#read(phase === 1 ? 'runs.list' : phase === 2 ? 'chat.queue.list' : phase === 3 ? 'approvals.list' : 'conversations.listMessages', input) as { items: unknown[], nextCursor: string | null } + return { items: page.items.map(value => ({ entity, value })), nextCursor: page.nextCursor ? `${phase}.${encodeURIComponent(page.nextCursor)}` : phase < 5 ? `${phase + 1}.` : null } + } + + 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) => { + assertCommittedRead(this.#database) + return withTransaction(this.#database, () => this.#read(method, input as Record)) + } } + } + return handlers + } + + #read(method: keyof typeof PRODUCT_QUERY_METHODS, input: Record): unknown { + switch (method) { + case 'service.host': return { platform: ['darwin', 'win32', 'linux'].includes(platform()) ? platform() : 'other' } + case 'service.info': return { name: hostname() || 'Lexora' } + 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)) + case 'conversations.listMessages': return this.#messagePage(input) + case 'runs.listTranscriptEvents': return this.#run(String(input.runId))?.executionKind === 'root' + ? this.#pages.read(`${method}:${input.runId}`, input, '(SELECT rowid, CAST(sequence AS TEXT) AS id, run_id, sequence FROM run_events)', 'run_id = ? AND sequence > ? AND sequence <= ?', [String(input.runId), BigInt(String(input.afterSequence)), BigInt(String(input.throughSequence))], sequence => this.views.transcriptEvent(String(input.runId), sequence), String(input.throughSequence)) + : { items: [], nextCursor: null } + case 'runs.listEvents': return this.#run(String(input.runId)) ? this.#pages.read(`${method}:${input.runId}`, input, `(SELECT rowid, CAST(sequence AS TEXT) AS id, run_id, sequence FROM run_events)`, 'run_id = ? AND (? IS NULL OR sequence <= ?)', [String(input.runId), input.throughSequence ? BigInt(String(input.throughSequence)) : null, input.throughSequence ? BigInt(String(input.throughSequence)) : null], sequence => this.views.event(String(input.runId), sequence)) : { items: [], nextCursor: null } + case 'chat.queue.list': return this.#pages.read(`${method}:${input.conversationId}:${input.branchId}`, input, 'chat_queue', 'conversation_id = ? AND branch_id = ? AND state IN (\'waiting\',\'paused\')', [String(input.conversationId), String(input.branchId)], id => this.views.queue(id)) + case 'approvals.getDecision': return this.views.approvalDecision(String(input.approvalId)) + case 'approvals.get': return this.views.approval(String(input.approvalId)) + case 'approvals.list': return this.#pages.read(method, input, 'approvals', '(? IS NULL OR run_id = ?) AND (? IS NULL OR status = ?) AND (? IS NULL OR EXISTS (SELECT 1 FROM runs WHERE runs.id = approvals.run_id AND runs.conversation_id = ?)) AND (? IS NULL OR EXISTS (SELECT 1 FROM runs WHERE runs.id = approvals.run_id AND runs.branch_id = ?))', [input.runId as string ?? null, input.runId as string ?? null, input.status as string ?? null, input.status as string ?? null, input.conversationId as string ?? null, input.conversationId as string ?? null, input.branchId as string ?? null, input.branchId as string ?? null], id => this.views.approval(id)) + case 'resources.get': return this.views.resource(String(input.resourceId)) + case 'resources.list': return this.#pages.read(method, input, `(SELECT a.rowid * 2 AS rowid, 'attachment:' || a.id AS id, m.conversation_id FROM attachments a JOIN messages m ON m.id = a.message_id UNION ALL SELECT rowid * 2 + 1 AS rowid, 'artifact:' || id AS id, conversation_id FROM artifacts)`, 'conversation_id = ?', [String(input.conversationId)], id => this.views.resource(id)) + case 'taskMarks.list': return this.#pages.read(method, input, 'task_marks', '1', [], (id) => { + const mark = createTaskMarkRepository(this.#database).list().find(mark => mark.id === id)! + return { ...mark, revision: encodeProductRevision(new ConversationCommandRepository(this.#database).markRevision(id)) } + }) + case 'taskMarks.states': return (input.conversationIds as string[]).map((id) => { + const state = createTaskMarkRepository(this.#database).getState(id) + return { ...state, readRevision: encodeProductRevision(state.readRevision), assignmentRevision: encodeProductRevision(new ConversationCommandRepository(this.#database).assignmentRevision(id)) } + }) + case 'conversations.get': return this.task(String(input.conversationId)) + case 'composerDrafts.get': return this.draft(String(input.draftId)) + case 'composerDrafts.find': return this.#drafts.find(input.scope as BuddyComposerDraft['scope']) + case 'runs.get': return this.#run(String(input.runId)) + case 'runs.getExecutionState': return this.views.runExecution(String(input.runId)) + case 'conversations.list': return this.#pages.read(method, input, 'conversations', 'deleted_at IS NULL AND archived_at IS NULL AND (? IS NULL OR space_id = ?)', [input.spaceId as string ?? null, input.spaceId as string ?? null], { batch: ids => this.#tasks.summaries(ids) }) + case 'composerDrafts.list': return this.#pages.read(method, input, 'composer_drafts', '1', [], id => this.draft(id)) + case 'runs.list': return this.#pages.read(`${method}:${input.conversationId}:${input.branchId}`, input, 'runs', 'conversation_id = ? AND branch_id = ? AND execution_kind IN (\'root\', \'actor_child\', \'maintenance\') AND EXISTS (SELECT 1 FROM conversations c WHERE c.id = runs.conversation_id AND c.deleted_at IS NULL)', [String(input.conversationId), String(input.branchId)], id => this.#run(id)) + case 'conversations.listBranches': return this.#pages.read(`${method}:${input.conversationId}`, input, 'conversation_branches', 'conversation_id = ? AND EXISTS (SELECT 1 FROM conversations c WHERE c.id = conversation_branches.conversation_id AND c.deleted_at IS NULL)', [String(input.conversationId)], (id) => { + const branch = this.#database.prepare('SELECT * FROM conversation_branches WHERE id = ?').get(id)! + const counts = this.#database.prepare(`SELECT + (SELECT COUNT(*) FROM user_questions q JOIN runs r ON r.id = q.run_id WHERE r.conversation_id = ? AND r.branch_id = ? AND json_extract(q.request_json, '$.status') = 'pending') AS pendingQuestionCount, + (SELECT COUNT(*) FROM approvals a JOIN runs r ON r.id = a.run_id WHERE r.conversation_id = ? AND r.branch_id = ? AND a.status = 'pending') AS pendingApprovalCount, + (SELECT COUNT(*) FROM runs WHERE conversation_id = ? AND branch_id = ? AND execution_kind IN ('root', 'actor_child') AND status IN ('queued', 'running')) AS runningCount, + (SELECT COUNT(*) FROM chat_queue WHERE conversation_id = ? AND branch_id = ? AND state IN ('waiting', 'paused')) AS queuedCount`).get(...Array.from({ length: 4 }, () => [String(branch.conversation_id), id]).flat())! + return { id: branch.id, conversationId: branch.conversation_id, parentBranchId: branch.parent_branch_id, forkedFromMessageId: branch.forked_from_message_id, createdAt: branch.created_at, ...Object.fromEntries(Object.entries(counts).map(([name, count]) => [name, Number(count)])) } + }) + } + } + + #run(id: string) { + const run = createRunRepository(this.#database).findById(id) + if (!run || run.executionKind === 'npc_child' || !this.task(run.conversationId)) + return null + const input = createRunInputRepository(this.#database).findByRunId(id) + const code = productErrorCodeSchema.safeParse(run.errorCode) + 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) { + const conversationId = String(input.conversationId) + const branchId = String(input.branchId) + const conversations = createConversationRepository(this.#database) + if (!this.task(conversationId)) + throw new ProductProtocolError('OBJECT_DELETED') + const revision = productQueryRevision(this.#database, 'conversations.listMessages', input) + const scope = digestScope(['messages', this.#instance, conversationId, branchId]) + let upperBound = encodeProductRevision(Number(this.#database.prepare('SELECT COUNT(*) AS value FROM messages').get()!.value)) + let position = 0n + let beforeMessageId: string | undefined + if (input.cursor) { + try { + const cursor = decodeProductCursor(String(input.cursor), { scope, branchId, revision }) + beforeMessageId = cursor.anchorId + position = BigInt(cursor.position) + upperBound = cursor.upperBound + } + catch { throw new ProductProtocolError('CURSOR_EXPIRED') } + } + const source = conversations.listMessagePage(conversationId, branchId, { limit: Number(input.limit), beforeMessageId }) + const items: ReturnType[] = [] + for (const message of [...source.items].reverse()) { + const item = this.views.message(message) + if (productByteLength(JSON.stringify([...items, item])) > PRODUCT_LIMITS.pageBytes - 4096) + break + items.push(item) + } + if (source.items.length && !items.length) + throw new ProductProtocolError('PAGE_TOO_LARGE') + items.reverse() + position += BigInt(items.length) + const more = items.length < source.items.length || source.nextBeforeMessageId !== null + return { items, nextCursor: more ? encodeProductCursor({ version: 1, scope, branchId, revision, upperBound, position: encodeProductRevision(position), anchorId: items[0]?.id }) : null } + } +} + +function digestScope(value: unknown): string { + return createHash('sha256').update(JSON.stringify(value)).digest('hex') +} diff --git a/apps/buddy/service/src/productProtocol/ProductQueryRevisions.ts b/apps/buddy/service/src/productProtocol/ProductQueryRevisions.ts new file mode 100644 index 000000000..430c31584 --- /dev/null +++ b/apps/buddy/service/src/productProtocol/ProductQueryRevisions.ts @@ -0,0 +1,93 @@ +import type { DatabaseSync } from 'node:sqlite' +import { createHash } from 'node:crypto' +import { encodeProductRevision, PRODUCT_INTEGER_MAX } from '../../../shared/productProtocol/scalars' +import { createConversationHistoryStore } from '../storage/conversationHistoryRepository' + +export function productQueryRevision(database: DatabaseSync, method: string, input: Record): string { + const task = (id: unknown) => JSON.stringify(['task', String(id)]) + const branch = (conversationId: unknown, branchId: unknown) => JSON.stringify(['branch', String(conversationId), String(branchId)]) + const run = (id: unknown) => JSON.stringify(['run', String(id)]) + const dependencies: Array<[string, string]> = [] + const fingerprint = createHash('sha256') + const add = (scope: string, ...tables: string[]) => dependencies.push(...tables.map(table => [table, scope] as [string, string])) + const addVisibility = (conversationId: unknown) => add(task(conversationId), 'conversation_visibility') + switch (method) { + case 'spaces.list': + add('', 'spaces') + break + case 'conversations.list': + add('', 'conversations', 'approvals', 'run_summaries', 'automation_occurrences', 'user_questions', 'task_attention', 'publication_summaries', 'message_summaries') + break + case 'conversations.listMessages': { + addVisibility(input.conversationId) + const segments = createConversationHistoryStore(database).lineage.listVisibleSegments(String(input.conversationId), String(input.branchId)) + for (const segment of segments) { + if (!segment.throughMessage) { + add(branch(input.conversationId, segment.branchId), 'history_branches') + continue + } + const boundary = segment.throughMessage + const rows = database.prepare(`SELECT m.id, m.created_at, CAST(COALESCE(r.revision, 0) AS TEXT) AS revision + FROM messages m LEFT JOIN product_query_revisions r ON r.table_name = 'history_messages' AND r.scope = json_array('message', m.id) + WHERE m.conversation_id = ? AND m.branch_id = ? AND (m.created_at < ? OR (m.created_at = ? AND m.id <= ?)) + AND (m.run_id IS NULL OR EXISTS (SELECT 1 FROM conversation_publications WHERE message_id = m.id) + OR EXISTS (SELECT 1 FROM runs WHERE id = m.run_id AND (execution_kind = 'root' OR (execution_kind = 'actor_child' AND m.role = 'assistant')))) + ORDER BY m.created_at, m.id`).iterate(String(input.conversationId), segment.branchId, boundary.createdAt, boundary.createdAt, boundary.id) + fingerprint.update(JSON.stringify([segment.branchId, boundary])) + for (const row of rows) fingerprint.update(JSON.stringify(row)) + } + const ancestors = database.prepare(`WITH RECURSIVE ancestors(id, parent_branch_id) AS ( + SELECT id, parent_branch_id FROM conversation_branches WHERE id = ? AND conversation_id = ? + UNION SELECT b.id, b.parent_branch_id FROM conversation_branches b JOIN ancestors a ON b.id = a.parent_branch_id WHERE b.conversation_id = ? + ) SELECT id FROM ancestors`).all(String(input.branchId), String(input.conversationId), String(input.conversationId)) + for (const ancestor of ancestors) add(branch(input.conversationId, ancestor.id), 'conversation_branches') + break + } + case 'conversations.listBranches': + addVisibility(input.conversationId) + add(task(input.conversationId), 'conversation_branches', 'user_questions', 'approvals', 'run_summaries', 'chat_queue') + break + case 'questions.list': + case 'runs.list': + case 'chat.queue.list': + addVisibility(input.conversationId) + add(branch(input.conversationId, input.branchId), ...method === 'questions.list' ? ['user_questions', 'run_history'] : method === 'runs.list' ? ['runs', 'run_inputs'] : ['chat_queue', 'run_summaries']) + break + case 'runs.listEvents': { + const owner = database.prepare('SELECT conversation_id FROM runs WHERE id = ?').get(String(input.runId)) + if (owner) + addVisibility(owner.conversation_id) + add(run(input.runId), 'run_events', 'run_history') + break + } + case 'approvals.list': { + const owner = input.runId + ? database.prepare('SELECT conversation_id, branch_id FROM runs WHERE id = ?').get(String(input.runId)) + : input.branchId ? database.prepare('SELECT conversation_id FROM conversation_branches WHERE id = ?').get(String(input.branchId)) : null + const conversationId = input.conversationId ?? owner?.conversation_id + if (conversationId) + addVisibility(conversationId) + else add('', 'conversation_visibility') + const scope = input.runId ? run(input.runId) : input.branchId ? branch(conversationId, input.branchId) : conversationId ? task(conversationId) : '' + add(scope, 'approvals', 'approval_reviews', 'approval_decisions', 'run_history') + break + } + case 'resources.list': + addVisibility(input.conversationId) + add(task(input.conversationId), 'attachments', 'artifacts', 'messages', 'run_history', 'managed_files') + break + case 'taskMarks.list': + add('', 'task_marks', 'task_attention', 'conversation_visibility') + break + case 'composerDrafts.list': + add('', 'composer_drafts', 'composer_resources', 'attachments', 'managed_files', 'conversation_visibility') + break + default: throw new Error('PRODUCT_QUERY_REVISION_UNDEFINED') + } + const statement = database.prepare('SELECT revision FROM product_query_revisions WHERE table_name = ? AND scope = ?') + statement.setReadBigInts(true) + const unique = new Map(dependencies.map(value => [JSON.stringify(value), value])) + for (const [, [table, scope]] of [...unique.entries()].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)) + fingerprint.update(JSON.stringify([table, scope, String(statement.get(table, scope)?.revision ?? 0)])) + return encodeProductRevision(fingerprint.digest().readBigUInt64BE() & PRODUCT_INTEGER_MAX) +} diff --git a/apps/buddy/service/src/productProtocol/ProductReadViews.ts b/apps/buddy/service/src/productProtocol/ProductReadViews.ts new file mode 100644 index 000000000..47457a3a6 --- /dev/null +++ b/apps/buddy/service/src/productProtocol/ProductReadViews.ts @@ -0,0 +1,372 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { z } from 'zod' +import type { BuddyUserContentV1 } from '../../../shared/conversation/buddyUserContent' +import type { MessageRecord } from '../storage/conversationHistoryRepository' +import type { ProductContentReferences } from './ProductContentReferences' +import { Buffer } from 'node:buffer' +import { createHash } from 'node:crypto' +import { buddyUserContentToText, buddyUserMessageContentV1Schema } from '../../../shared/conversation/buddyUserContent' +import { redactSensitiveText } from '../../../shared/permissions/approvalReviewPayload' +import { productErrorCodeSchema, ProductProtocolError } from '../../../shared/productProtocol/errors' +import { productByteLength } from '../../../shared/productProtocol/limits' +import { productQuestionSchema, productRunExecutionSchema } from '../../../shared/productProtocol/queries' +import { encodeProductRevision } from '../../../shared/productProtocol/scalars' +import { productApprovalDecisionSchema, productApprovalSchema, productMessageSchema, productQueueItemSchema, productResourceSchema, productRunEventSchema } from '../../../shared/productProtocol/views' +import { toPublicRunEvent } from '../../../shared/runs/publicRunEvent' +import { buddyToolPresentationSchema } from '../../../shared/runs/runEventPresentation' +import { createApprovalRepository } from '../storage/approvalRepository' +import { createArtifactRepository } from '../storage/artifactRepository' +import { createAttachmentRepository } from '../storage/attachmentRepository' +import { findPersonalComposerDraft } from '../storage/composerDraftRepository' +import { createConversationRepository } from '../storage/conversationRepository' +import { ExecutionResourceQueries } from '../storage/ExecutionResourceQueries' + +const inlineBytes = 131_072 +const timestamp = '1970-01-01T00:00:00.000Z' + +export class ProductReadViews { + readonly #database: DatabaseSync + readonly #actorId: string + readonly #references: ProductContentReferences + readonly #hasReview: (approvalId: string, version: string) => boolean + + constructor(database: DatabaseSync, actorId: string, references: ProductContentReferences, hasReview: (approvalId: string, version: string) => boolean = () => false) { + this.#database = database + this.#actorId = actorId + this.#references = references + this.#hasReview = hasReview + } + + content(content: BuddyUserContentV1, reference: string, conversationId: string) { + const portable = this.#references.project(content, { kind: 'conversation', id: conversationId }) + return productByteLength(JSON.stringify(portable)) > inlineBytes + ? { kind: 'unsupported' as const, contentReference: reference, version: 1, summary: '', readOnly: true as const } + : { kind: 'editable' as const, content: portable } + } + + question(id: string) { + const row = this.#database.prepare('SELECT q.request_json, r.conversation_id, r.branch_id FROM user_questions q JOIN runs r ON r.id = q.run_id JOIN conversations c ON c.id = r.conversation_id WHERE q.id = ? AND c.deleted_at IS NULL').get(id) + if (!row) + return null + const source = JSON.parse(String(row.request_json)) as Record + if (source.conversationId !== row.conversation_id || source.branchId !== row.branch_id) + return null + return productQuestionSchema.parse(Object.fromEntries(Object.keys(productQuestionSchema.shape).map(key => [key, source[key]]))) + } + + messagePreview(message: Pick): string { + if (message.role !== 'user' && message.role !== 'assistant') + return '' + const user = message.role === 'user' ? buddyUserMessageContentV1Schema.safeParse(message.content) : null + const text = user?.success ? buddyUserContentToText(user.data.userContent) : publicMessageText(message.content) + return text === null ? '' : truncateText(text.replace(/\s+/g, ' ').trim(), 240) + } + + message(message: MessageRecord) { + const source = message.content && typeof message.content === 'object' && !Array.isArray(message.content) ? message.content as Record : null + const user = buddyUserMessageContentV1Schema.safeParse(message.content) + let body: z.infer['body'] + const resources: z.infer[] = [] + const tool = message.role === 'tool' && typeof source?.toolName === 'string' && typeof source.isError === 'boolean' + ? { name: source.toolName.slice(0, 256), isError: source.isError } + : undefined + if (user.success) { + body = { kind: 'user', content: this.content(user.data.userContent, `message:${message.id}`, message.conversationId) } + for (const binding of user.data.resourceSnapshots) { + if (binding.attachmentId) { + const resource = this.resource(`attachment:${binding.attachmentId}`) + if (resource) + resources.push({ ...resource, bindingId: binding.resourceId }) + } + else { + resources.push({ resourceId: `material:${message.id}:${binding.resourceId}`, bindingId: binding.resourceId, kind: 'reference', name: '引用', mimeType: 'application/octet-stream', sizeBytes: '0', revision: digest(binding), availability: 'export_required' }) + } + } + } + else if (publicMessageText(message.content) !== null) { + const text = publicMessageText(message.content)! + body = productByteLength(text) <= inlineBytes + ? { kind: 'text', text, interrupted: source?.state === 'interrupted', truncated: source?.truncated === true } + : { kind: 'resource', resourceId: `message:${message.id}`, previewText: truncateText(text, 4096), readOnly: true, interrupted: source?.state === 'interrupted', truncated: source?.truncated === true } + if (Array.isArray(source?.attachmentIds)) { + for (const id of source.attachmentIds) { + if (typeof id !== 'string') + continue + const resource = this.resource(`attachment:${id}`) + if (resource) + resources.push(resource) + } + } + } + else { + body = { kind: 'resource', resourceId: `message:${message.id}`, previewText: typeof source?.text === 'string' ? truncateText(source.text, 4096) : '', readOnly: true } + } + if (tool && body.kind === 'resource') { + const presentation = this.#toolPresentation(message, source) + if (presentation) { + const fields = presentation as Record + const summary = ['description', 'command', 'path', 'query', 'glob', 'target', 'operation', 'tool'] + .map(key => fields[key]) + .find(value => typeof value === 'string' && value.trim()) + if (typeof summary === 'string') + body.previewText = truncateText(redactSensitiveText(summary).replace(/\s+/g, ' ').trim(), 240) + } + } + let author: z.infer['author'] + if (message.role === 'assistant' && message.runId) { + const run = this.#database.prepare(`SELECT r.actor_snapshot_json FROM runs r JOIN conversations c ON c.id = r.conversation_id + WHERE r.id = ? AND r.conversation_id = ? AND r.branch_id = ? AND r.execution_kind IN ('root', 'actor_child') AND c.deleted_at IS NULL`) + .get(message.runId, message.conversationId, message.branchId) + if (typeof run?.actor_snapshot_json === 'string') { + try { + const snapshot = JSON.parse(run.actor_snapshot_json) as Record | null + const identity = productMessageSchema.shape.author.unwrap().safeParse({ id: snapshot?.id, name: snapshot?.name }) + if (identity.success) + author = identity.data + } + catch {} + } + } + return productMessageSchema.parse({ id: message.id, conversationId: message.conversationId, branchId: message.branchId, runId: message.runId, role: message.role, createdAt: message.createdAt, body, resources, ...(tool ? { tool } : {}), ...(author ? { author } : {}) }) + } + + approval(id: string, localEffects = false) { + const approval = createApprovalRepository(this.#database).findById(id) + if (!approval || !this.#visibleRun(approval.runId)) + return null + const scope = this.#database.prepare('SELECT conversation_id, branch_id FROM runs WHERE id = ?').get(approval.runId)! + const review = approval.review + let projected: z.infer['review'] = null + if (review) { + const document = reviewDocument(review.body) + const inline = productByteLength(document) <= inlineBytes + const localManagementRequired = review.persistentEffect && !localEffects + const complete = review.complete && (inline || this.#hasReview(id, review.reviewVersion)) && Date.parse(review.expiresAt) > Date.now() + projected = { + contentVersion: review.contentVersion, + operationDigest: review.operationDigest, + reviewVersion: review.reviewVersion, + expiresAt: review.expiresAt, + persistentEffect: review.persistentEffect, + complete, + allowedDecisions: complete && !localManagementRequired ? review.allowedDecisions : ['denied'], + document: inline ? { format: 'json', text: document } : null, + resourceId: inline ? null : `review:${id}:${review.reviewVersion}`, + localManagementRequired, + } + } + return productApprovalSchema.parse({ id, runId: approval.runId, conversationId: scope.conversation_id, branchId: scope.branch_id, kind: approval.kind, summary: redactSensitiveText(approval.summary).slice(0, 4096), status: approval.status, createdAt: approval.createdAt, resolvedAt: approval.resolvedAt, resolution: approval.resolution ?? null, review: projected }) + } + + approvalDecision(id: string) { + const approval = createApprovalRepository(this.#database).findById(id) + if (!approval || approval.status === 'pending' || !this.#visibleRun(approval.runId)) + return null + const localActor = this.#database.prepare('SELECT local_actor_id FROM storage_profile WHERE id = 1').get()!.local_actor_id + const actor = approval.decisionActorId + ? { id: approval.decisionActorId, kind: approval.decisionActorId === localActor ? 'desktop' : 'device', current: approval.decisionActorId === this.#actorId } + : null + return productApprovalDecisionSchema.parse({ approvalId: id, runId: approval.runId, reviewVersion: approval.review?.reviewVersion ?? null, status: approval.status, resolution: approval.resolution ?? null, resolvedAt: approval.resolvedAt, actor, cancellationReason: approval.cancellationReason ?? null }) + } + + queue(id: string) { + const row = this.#database.prepare('SELECT * FROM chat_queue WHERE id = ?').get(id) + if (!row || !this.#visibleTask(String(row.conversation_id))) + return null + const input = JSON.parse(String(row.prepared_json)) as Record + const user = buddyUserMessageContentV1Schema.safeParse(input.userMessageContent) + if (!user.success) + throw new ProductProtocolError('CONTENT_UNSUPPORTED') + const active = this.#database.prepare(`SELECT id, generation FROM runs WHERE conversation_id = ? AND branch_id = ? AND execution_kind = 'root' AND status IN ('running','queued') ORDER BY started_at DESC LIMIT 1`).get(String(row.conversation_id), String(row.branch_id)) + return productQueueItemSchema.parse({ id, conversationId: row.conversation_id, branchId: row.branch_id, revision: encodeProductRevision(Number(row.revision)), state: row.state, createdAt: row.created_at, sourceRunId: typeof input.sourceRunId === 'string' ? input.sourceRunId : null, content: this.content(user.data.userContent, `queue:${id}`, String(row.conversation_id)), steerTarget: active ? { runId: String(active.id), generation: encodeProductRevision(Number(active.generation)) } : null }) + } + + runExecution(runId: string) { + if (!this.#publicRun(runId)) + return null + const row = this.#database.prepare('SELECT r.generation, e.phase, e.cancel_requested_at, e.cleanup_state FROM runs r JOIN execution_state e ON e.run_id = r.id WHERE r.id = ?').get(runId) + if (!row) + return null + const resources = new ExecutionResourceQueries(this.#database).subtree(runId) + return productRunExecutionSchema.parse({ runId, generation: encodeProductRevision(Number(row.generation)), phase: row.phase, cancelRequested: row.cancel_requested_at !== null, resources }) + } + + transcriptEvent(runId: string, sequence: string) { + if (!this.#publicRun(runId)) + return null + const run = this.#database.prepare('SELECT execution_kind FROM runs WHERE id = ?').get(runId) + if (run?.execution_kind !== 'root') + return null + const row = this.#database.prepare('SELECT * FROM run_events WHERE run_id = ? AND sequence = ?').get(runId, BigInt(sequence)) + if (!row) + return null + const event = toPublicRunEvent({ runId, sequence: Number(sequence), createdAt: String(row.created_at), type: String(row.event_type), payload: JSON.parse(String(row.payload_json)) }) + const redact = (value: unknown): unknown => { + if (typeof value === 'string') + return redactSensitiveText(value) + if (Array.isArray(value)) + return value.map(redact) + if (value && typeof value === 'object') + return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, redact(item)])) + return value + } + return { ...event, payload: redact(event.payload) } + } + + event(runId: string, sequence: string) { + if (!this.#publicRun(runId)) + return null + const row = this.#database.prepare('SELECT * FROM run_events WHERE run_id = ? AND sequence = ?').get(runId, BigInt(sequence)) + if (!row) + return null + const payload = JSON.parse(String(row.payload_json)) as Record + let detail: z.infer['detail'] = { kind: 'status' } + const run = this.#database.prepare('SELECT execution_kind FROM runs WHERE id = ?').get(runId) + const publicText = run?.execution_kind === 'root' || (run?.execution_kind === 'actor_child' && typeof payload.messageId === 'string' && Boolean(createConversationRepository(this.#database).findMessageById(payload.messageId))) + if (publicText && row.event_type === 'message.delta' && typeof payload.messageId === 'string' && typeof payload.delta === 'string' && productByteLength(payload.delta) <= 65_536) { + detail = { kind: 'text_delta', messageId: payload.messageId, delta: payload.delta, phase: payload.phase === 'commentary' || payload.phase === 'final_answer' ? payload.phase : null } + } + else { + const fields: Record = {} + for (const key of publicText ? ['messageId', 'toolCallId', 'toolName'] : []) { + if (typeof payload[key] === 'string') + fields[key] = payload[key].slice(0, 256) + } + if (String(row.event_type).startsWith('approval.') && typeof payload.id === 'string') + fields.approvalId = payload.id + const code = productErrorCodeSchema.safeParse(payload.errorCode) + if (code.success) + fields.errorCode = code.data + detail = { kind: 'status', ...fields } + } + return productRunEventSchema.parse({ runId, sequence, createdAt: row.created_at, type: row.event_type, detail }) + } + + resource(resourceId: string): z.infer | null { + const [kind, id] = resourceId.split(':') + if (!id) + return null + if (kind === 'attachment') { + const repository = createAttachmentRepository(this.#database) + const attachment = repository.findVisibleById(id) + if (!attachment) + return null + if (attachment.messageId && !createConversationRepository(this.#database).findMessageById(attachment.messageId)) + return null + if (attachment.draftId && !findPersonalComposerDraft(this.#database, attachment.draftId) && !this.#database.prepare('SELECT 1 FROM remote_uploads WHERE actor_id = ? AND input_id IS NOT NULL AND attachment_id = ? AND state = \'ready\' AND expires_ms > ?').get(this.#actorId, id, Date.now()) && !this.#database.prepare('SELECT 1 FROM chat_queue q JOIN conversations c ON c.id = q.conversation_id WHERE q.id = ? AND c.deleted_at IS NULL').get(attachment.draftId)) + return null + const file = repository.persistence!.get(attachment.storedPath) + return productResourceSchema.parse({ resourceId, kind, name: displayName(attachment.name), mimeType: attachment.mimeType, sizeBytes: encodeProductRevision(attachment.sizeBytes), revision: file?.sha256 ?? 'unavailable', availability: file?.state === 'ready' ? 'ready' : 'unavailable' }) + } + if (kind === 'artifact') { + const artifact = createArtifactRepository(this.#database).findVisibleById(id) + if (!artifact) + return null + return productResourceSchema.parse({ resourceId, kind, name: displayName(artifact.name), mimeType: artifact.mimeType, sizeBytes: encodeProductRevision(artifact.sizeBytes), revision: digest([artifact.id, artifact.updatedAt, artifact.sizeBytes]), availability: 'export_required' }) + } + if (kind === 'message') { + const message = createConversationRepository(this.#database).findMessageById(id) + if (!message || !this.#visibleTask(message.conversationId)) + return null + const bytes = this.document(resourceId) + return productResourceSchema.parse({ resourceId, kind, name: '消息内容', mimeType: bytes?.mimeType ?? 'application/json', sizeBytes: encodeProductRevision(bytes?.bytes.byteLength ?? 0), revision: digest(message.content), availability: bytes ? 'ready' : 'unavailable' }) + } + if (kind === 'review') { + const approval = createApprovalRepository(this.#database).findById(id) + if (!approval?.review || !this.#visibleRun(approval.runId) || resourceId !== `review:${id}:${approval.review.reviewVersion}`) + return null + return productResourceSchema.parse({ resourceId, kind, name: '完整审批内容', mimeType: 'application/json', sizeBytes: encodeProductRevision(productByteLength(reviewDocument(approval.review.body))), revision: approval.review.reviewVersion, availability: approval.review.complete ? 'ready' : 'unavailable' }) + } + return null + } + + document(resourceId: string): { bytes: Buffer, mimeType: string, revision: string } | null { + const [kind, id] = resourceId.split(':') + if (!id) + return null + if (kind === 'review') { + const approval = createApprovalRepository(this.#database).findById(id) + if (!approval?.review?.complete || !this.#visibleRun(approval.runId) || resourceId !== `review:${id}:${approval.review.reviewVersion}`) + return null + return { bytes: Buffer.from(reviewDocument(approval.review.body)), mimeType: 'application/json', revision: approval.review.reviewVersion } + } + if (kind !== 'message') + return null + const message = createConversationRepository(this.#database).findMessageById(id) + if (!message || !this.#visibleTask(message.conversationId)) + return null + const user = buddyUserMessageContentV1Schema.safeParse(message.content) + if (user.success) + return { bytes: Buffer.from(JSON.stringify(this.#references.project(user.data.userContent, { kind: 'conversation', id: message.conversationId }))), mimeType: 'application/json', revision: digest(message.content) } + const source = message.content && typeof message.content === 'object' && !Array.isArray(message.content) ? message.content as Record : null + if (typeof message.content === 'string' || (source && typeof source.text === 'string' && Object.keys(source).every(key => ['text', 'attachmentIds', 'modelId', 'providerId', 'state', 'truncated'].includes(key)))) + return { bytes: Buffer.from(typeof message.content === 'string' ? message.content : String(source!.text)), mimeType: 'text/plain; charset=utf-8', revision: digest(message.content) } + if (message.role === 'tool' && message.runId && typeof source?.toolCallId === 'string' && typeof source.toolName === 'string') { + const presentation = this.#toolPresentation(message, source) + const lines = [`工具:${source.toolName}`, `结果:${source.isError === true ? '执行失败' : '已完成'}`] + if (presentation) { + const labels: Record = { description: '说明', command: '命令', cwd: '工作目录', path: '文件', query: '查询', glob: '文件匹配', target: '目标', operation: '操作', tool: '调用', connector: '连接器', exitCode: '退出码', signal: '信号', diff: '文件变更', output: '输出', status: '状态', errorCode: '错误' } + const fields = presentation as Record + for (const [key, label] of Object.entries(labels)) { + const value = fields[key] + if ((typeof value === 'string' && value) || typeof value === 'number') + lines.push(`${label}:\n${redactSensitiveText(String(value))}`) + } + if (fields.truncated === true) + lines.push('此调用保存的输出已截断;以上是当前可读取的全部内容。') + } + else { + lines.push('这条历史调用未保存可读取的参数和输出。') + } + const text = lines.join('\n\n') + return { bytes: Buffer.from(text), mimeType: 'text/plain; charset=utf-8', revision: digest(message.content) } + } + return null + } + + #toolPresentation(message: MessageRecord, source: Record | null) { + if (!message.runId || typeof source?.toolCallId !== 'string' || !this.#visibleRun(message.runId)) + return null + const row = this.#database.prepare(`SELECT payload_json FROM run_events WHERE run_id = ? AND event_type = 'tool.completed' + AND json_extract(payload_json, '$.toolCallId') = ? ORDER BY sequence DESC LIMIT 1`).get(message.runId, source.toolCallId) + const payload = row ? JSON.parse(String(row.payload_json)) as Record : null + const presentation = buddyToolPresentationSchema.safeParse(payload?.presentation) + return presentation.success ? presentation.data : null + } + + #publicRun(id: string): boolean { + return this.#visibleRun(id) && !!this.#database.prepare('SELECT 1 FROM runs WHERE id = ? AND execution_kind IN (\'root\', \'actor_child\', \'maintenance\')').get(id) + } + + #visibleTask(id: string): boolean { + return Boolean(this.#database.prepare('SELECT 1 FROM conversations WHERE id = ? AND deleted_at IS NULL').get(id)) + } + + #visibleRun(id: string): boolean { + return Boolean(this.#database.prepare('SELECT 1 FROM runs r JOIN conversations c ON c.id = r.conversation_id WHERE r.id = ? AND c.deleted_at IS NULL').get(id)) + } +} + +function digest(value: unknown): string { + return createHash('sha256').update(JSON.stringify(value)).digest('hex') +} +function publicMessageText(value: unknown): string | null { + if (typeof value === 'string') + return value + if (!value || typeof value !== 'object' || Array.isArray(value)) + return null + const source = value as Record + return typeof source.text === 'string' && Object.keys(source).every(key => ['text', 'attachmentIds', 'modelId', 'providerId', 'state', 'truncated'].includes(key)) ? source.text : null +} +function truncateText(value: string, limit: number): string { + const end = value.charCodeAt(limit - 1) + return value.slice(0, end >= 0xD800 && end <= 0xDBFF ? limit - 1 : limit) +} +function displayName(value: string): string { + return value.replaceAll('\\', '/').split('/').at(-1)!.slice(0, 512) +} +function reviewDocument(value: unknown): string { + const internal = new Set(['lifetimeId', 'pageId', 'sessionId', 'observationId', 'actionDigest']) + const sensitive = /password|passwd|secret|token|credential|authorization|cookie|api[_-]?key|private[_-]?key/i + return JSON.stringify(value ?? { createdAt: timestamp }, (key, item) => internal.has(key) ? undefined : sensitive.test(key) ? '[redacted]' : typeof item === 'string' ? redactSensitiveText(item) : item, 2) +} diff --git a/apps/buddy/service/src/productProtocol/ProductStreamingMessages.ts b/apps/buddy/service/src/productProtocol/ProductStreamingMessages.ts new file mode 100644 index 000000000..739bd2374 --- /dev/null +++ b/apps/buddy/service/src/productProtocol/ProductStreamingMessages.ts @@ -0,0 +1,60 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { z } from 'zod' +import { ProductProtocolError } from '../../../shared/productProtocol/errors' +import { PRODUCT_LIMITS, productByteLength } from '../../../shared/productProtocol/limits' +import { productMessageSchema } from '../../../shared/productProtocol/views' + +export class ProductStreamingMessages { + readonly #database: DatabaseSync + + constructor(database: DatabaseSync) { + this.#database = database + } + + start(runId: string, messageId: string, createdAt: string): z.infer | null { + const run = this.#database.prepare(`SELECT r.conversation_id, r.branch_id FROM runs r JOIN conversations c ON c.id = r.conversation_id + WHERE r.id = ? AND r.execution_kind = 'root' AND r.status IN ('queued', 'running') AND c.deleted_at IS NULL`).get(runId) + if (!run || this.#database.prepare('SELECT 1 FROM messages WHERE id = ?').get(messageId)) + return null + return productMessageSchema.parse({ id: messageId, conversationId: run.conversation_id, branchId: run.branch_id, runId, role: 'assistant', createdAt, body: { kind: 'text', text: '', interrupted: false, truncated: false }, resources: [] }) + } + + page(conversationId: string, branchId: string, cursor: string | null) { + if (cursor !== null && !/^(?:0|[1-9]\d{0,18})$/.test(cursor)) + throw new ProductProtocolError('CURSOR_EXPIRED') + const rows = this.#database.prepare(`SELECT e.rowid AS position, e.run_id, e.sequence, e.created_at, json_extract(e.payload_json, '$.messageId') AS id + FROM run_events e JOIN runs r ON r.id = e.run_id JOIN conversations c ON c.id = r.conversation_id + WHERE r.conversation_id = ? AND r.branch_id = ? AND r.execution_kind = 'root' AND r.status IN ('queued', 'running') AND c.deleted_at IS NULL + AND e.rowid > ? AND e.event_type = 'message.started' AND json_extract(e.payload_json, '$.role') = 'assistant' + AND NOT EXISTS (SELECT 1 FROM messages m WHERE m.id = json_extract(e.payload_json, '$.messageId')) + AND NOT EXISTS (SELECT 1 FROM run_events t WHERE t.run_id = e.run_id AND t.event_type IN ('message.completed', 'message.interrupted') AND json_extract(t.payload_json, '$.messageId') = json_extract(e.payload_json, '$.messageId')) + ORDER BY e.rowid LIMIT ?`).all(conversationId, branchId, BigInt(cursor ?? '0'), PRODUCT_LIMITS.pageItems + 1) + const items: z.infer[] = [] + let position = cursor ?? '0' + for (const row of rows.slice(0, PRODUCT_LIMITS.pageItems)) { + const message = this.start(String(row.run_id), String(row.id), String(row.created_at))! + let text = '' + let truncated = false + const deltas = this.#database.prepare(`SELECT json_extract(payload_json, '$.delta') AS delta FROM run_events + WHERE run_id = ? AND sequence > ? AND event_type = 'message.delta' AND json_extract(payload_json, '$.messageId') = ? ORDER BY sequence`) + for (const event of deltas.iterate(String(row.run_id), row.sequence!, String(row.id))) { + if (typeof event.delta !== 'string') + continue + const available = 131_072 - text.length + text += event.delta.slice(0, available) + if (event.delta.length > available) { + truncated = true + if (/[\uD800-\uDBFF]$/.test(text)) + text = text.slice(0, -1) + break + } + } + message.body = { kind: 'text', text, interrupted: false, truncated } + if (productByteLength(JSON.stringify([...items, message])) > PRODUCT_LIMITS.pageBytes - 4096) + break + items.push(message) + position = String(row.position) + } + return { items, nextCursor: rows.some(row => BigInt(String(row.position)) > BigInt(position)) ? position : null } + } +} diff --git a/apps/buddy/service/src/productProtocol/ProductSyncPublisher.ts b/apps/buddy/service/src/productProtocol/ProductSyncPublisher.ts new file mode 100644 index 000000000..340de95b4 --- /dev/null +++ b/apps/buddy/service/src/productProtocol/ProductSyncPublisher.ts @@ -0,0 +1,401 @@ +import type { ZodType } from 'zod' +import type { ProductSyncCursor, ProductSyncSnapshot } from '../../../shared/productProtocol/sync' +import { randomUUID } from 'node:crypto' +import { ProductProtocolError } from '../../../shared/productProtocol/errors' +import { encodeProductFrame, PRODUCT_LIMITS, productByteLength } from '../../../shared/productProtocol/limits' +import { encodeProductRevision } from '../../../shared/productProtocol/scalars' +import { productAckSchema, productSnapshotPageSchema, productSubscribeSchema } from '../../../shared/productProtocol/sync' + +type JsonValue = string | number | boolean | null | readonly JsonValue[] | { readonly [key: string]: JsonValue | undefined } +export interface ProductSnapshotPage { + items: readonly JsonValue[] + complete: boolean + nextCursor: string | null +} +export interface ProductSnapshotReader { + read: (cursor: string | null) => ProductSnapshotPage + close: () => void +} +interface TopicDefinition { + item: ZodType + change: ZodType + readSnapshot: () => ProductSnapshotPage + openSnapshot?: () => ProductSnapshotReader +} +interface Change { + from: bigint + to: bigint + changes: JsonValue[] + bytes: number + count: number +} +interface Topic { + definition: TopicDefinition + epoch: string + seq: bigint + retained: Change[] + items: number + bytes: number + valid: boolean + reading: boolean +} +interface Subscription { + id: string + topic: Topic + pending: Change[] + items: number + bytes: number + ack: bigint + sent: bigint + active: boolean + cursor: ProductSyncCursor + reader?: ProductSnapshotReader + nextCursor: string | null + timer?: ReturnType + cachedPage?: { requestCursor: string | null, snapshot: ProductSyncSnapshot, bytes: number } +} +export interface ProductSyncTransport { + send: (frame: string) => boolean + close: () => void +} +interface Connection { + transport: ProductSyncTransport + subscriptions: Map + bytes: number +} + +export class ProductSyncPublisher { + readonly #topics = new Map() + readonly #connections = new Map() + + register(topic: string, definition: TopicDefinition): () => void { + if (this.#topics.has(topic)) + throw new Error('Topic already registered') + this.#topics.set(topic, { definition, epoch: randomUUID(), seq: 0n, retained: [], items: 0, bytes: 0, valid: true, reading: false }) + return () => { + this.invalidate(topic) + this.#topics.delete(topic) + } + } + + hasTopic(name: string): boolean { + return this.#topics.has(name) + } + + subscriptionCount(name: string): number { + const topic = this.#topics.get(name) + let count = 0 + for (const connection of this.#connections.values()) { + for (const subscription of connection.subscriptions.values()) { + if (subscription.topic === topic) + count++ + } + } + return count + } + + connect(connectionId: string, transport: ProductSyncTransport): () => void { + if (this.#connections.has(connectionId)) + throw new ProductProtocolError('SYNC_INVALID') + this.#connections.set(connectionId, { transport, subscriptions: new Map(), bytes: 0 }) + return () => { + const connection = this.#connections.get(connectionId) + if (connection) { + for (const subscription of connection.subscriptions.values()) this.#releaseSnapshot(connection, subscription) + this.#connections.delete(connectionId) + } + } + } + + subscribe(connectionId: string, input: unknown): { snapshot: ProductSyncSnapshot, activate: () => void } { + const request = productSubscribeSchema.parse(input) + const connection = this.#requireConnection(connectionId) + const topic = this.#topics.get(request.topic) + if (!topic || connection.subscriptions.has(request.subscriptionId) || connection.subscriptions.size >= PRODUCT_LIMITS.subscriptionsPerConnection) + throw new ProductProtocolError('SYNC_INVALID') + const floor = topic.retained[0]?.from ?? topic.seq + const base = request.base && BigInt(request.base.seq) + const resume = topic.valid && request.base?.syncEpoch === topic.epoch && base !== undefined && base >= floor && base <= topic.seq + const captured = resume ? base as bigint : topic.seq + const cursor = { syncEpoch: topic.epoch, seq: encodeProductRevision(captured) } + let snapshot: ProductSyncSnapshot + let reader: ProductSnapshotReader | undefined + if (resume) { + snapshot = { subscriptionId: request.subscriptionId, mode: 'resume', cursor } + } + else { + if (topic.reading) + throw new ProductProtocolError('SYNC_INVALID') + topic.reading = true + try { + reader = topic.definition.openSnapshot?.() + const value = reader ? reader.read(null) : topic.definition.readSnapshot() + if (!value.complete && !reader) + throw new ProductProtocolError('RESYNC_REQUIRED') + if ('then' in value || value.items.length > PRODUCT_LIMITS.pageItems || value.complete !== (value.nextCursor === null)) + throw new ProductProtocolError('PAGE_TOO_LARGE') + snapshot = { subscriptionId: request.subscriptionId, mode: 'snapshot', cursor, items: value.items.map(item => topic.definition.item.parse(item)), complete: value.complete, nextCursor: value.nextCursor } + encodeProductFrame(snapshot, PRODUCT_LIMITS.pageBytes) + topic.valid = true + } + catch (error) { + reader?.close() + this.invalidate(request.topic) + throw error + } + finally { + topic.reading = false + } + } + const subscription: Subscription = { id: request.subscriptionId, topic, pending: [], items: 0, bytes: 0, ack: captured, sent: captured, active: false, cursor, reader, nextCursor: snapshot.mode === 'snapshot' ? snapshot.nextCursor : null } + connection.subscriptions.set(subscription.id, subscription) + if (snapshot.mode === 'snapshot') { + if (snapshot.complete) { + reader?.close() + subscription.reader = undefined + } + else { + try { + this.#cacheSnapshot(connection, subscription, null, snapshot) + } + catch (error) { + this.#resync(connection, subscription) + throw error + } + subscription.timer = setTimeout(() => this.#resync(connection, subscription), PRODUCT_LIMITS.snapshotLifetimeMs) + subscription.timer.unref() + } + } + for (const change of topic.retained.filter(change => change.to > captured)) { + if (!this.#enqueue(connection, subscription, change)) + throw new ProductProtocolError('RESYNC_REQUIRED') + } + return { snapshot, activate: () => { + if (connection.subscriptions.get(subscription.id) !== subscription) + return + if (!subscription.reader) { + subscription.active = true + this.#flush(connection, subscription) + } + } } + } + + snapshotPage(connectionId: string, input: unknown): { snapshot: ProductSyncSnapshot, activate: () => void } { + const request = productSnapshotPageSchema.parse(input) + const connection = this.#requireConnection(connectionId) + const subscription = connection.subscriptions.get(request.subscriptionId) + if (!subscription) + throw new ProductProtocolError('RESYNC_REQUIRED') + const activate = () => { + if (!subscription.reader && connection.subscriptions.get(subscription.id) === subscription) { + subscription.active = true + this.#flush(connection, subscription) + } + } + if (subscription.cachedPage?.requestCursor === request.cursor) + return { snapshot: subscription.cachedPage.snapshot, activate } + if (!subscription.reader || subscription.nextCursor !== request.cursor) + throw new ProductProtocolError('CURSOR_EXPIRED') + try { + const value = subscription.reader.read(request.cursor) + if (value.items.length > PRODUCT_LIMITS.pageItems || value.complete !== (value.nextCursor === null)) + throw new ProductProtocolError('PAGE_TOO_LARGE') + const snapshot: ProductSyncSnapshot = { subscriptionId: subscription.id, mode: 'snapshot', cursor: subscription.cursor, ...value, items: value.items.map(item => subscription.topic.definition.item.parse(item)) } + this.#cacheSnapshot(connection, subscription, request.cursor, snapshot) + subscription.nextCursor = value.nextCursor + if (value.complete) { + subscription.reader.close() + subscription.reader = undefined + clearTimeout(subscription.timer) + } + return { snapshot, activate } + } + catch (error) { + this.#resync(connection, subscription) + throw error + } + } + + publishCommitted(topicName: string, project: () => readonly JsonValue[]): void { + const topic = this.#topics.get(topicName) + if (!topic) + throw new ProductProtocolError('SYNC_INVALID') + if (topic.reading) { + this.invalidate(topicName) + throw new ProductProtocolError('SYNC_INVALID') + } + if (!topic.valid) + return + try { + const changes = project().map(change => topic.definition.change.parse(change)) + if (!changes.length) + return + const to = topic.seq + 1n + const encoded = encodeProductFrame({ syncEpoch: topic.epoch, fromSeq: encodeProductRevision(topic.seq), toSeq: encodeProductRevision(to), changes }, PRODUCT_LIMITS.subscriptionBytes - 1024) + const change: Change = { from: topic.seq, to, changes, count: changes.length, bytes: productByteLength(encoded) + 512 } + topic.seq = to + topic.retained.push(change) + topic.bytes += change.bytes + topic.items += change.count + while (topic.items > PRODUCT_LIMITS.topicItems || topic.bytes > PRODUCT_LIMITS.topicBytes) { + const dropped = topic.retained.shift()! + topic.bytes -= dropped.bytes + topic.items -= dropped.count + } + for (const connection of this.#connections.values()) { + for (const subscription of connection.subscriptions.values()) { + if (subscription.topic === topic && this.#enqueue(connection, subscription, change)) + this.#flush(connection, subscription) + } + } + } + catch { + this.invalidate(topicName) + } + } + + ack(connectionId: string, input: unknown): void { + const request = productAckSchema.parse(input) + const connection = this.#requireConnection(connectionId) + const subscription = connection.subscriptions.get(request.subscriptionId) + const seq = BigInt(request.seq) + if (!subscription) + throw new ProductProtocolError('RESYNC_REQUIRED') + if (!subscription.active || request.syncEpoch !== subscription.topic.epoch || seq > subscription.sent) + throw new ProductProtocolError('SYNC_INVALID') + this.#clearCachedPage(connection, subscription) + if (seq <= subscription.ack) + return + subscription.ack = seq + while (subscription.pending[0] && subscription.pending[0].to <= seq) { + const change = subscription.pending.shift()! + subscription.items -= change.count + subscription.bytes -= change.bytes + connection.bytes -= change.bytes + } + } + + unsubscribe(connectionId: string, subscriptionId: string): void { + const connection = this.#requireConnection(connectionId) + const subscription = connection.subscriptions.get(subscriptionId) + if (subscription) { + this.#releaseSnapshot(connection, subscription) + connection.bytes -= subscription.bytes + connection.subscriptions.delete(subscriptionId) + } + } + + invalidate(topicName: string): void { + const topic = this.#topics.get(topicName) + if (!topic) + return + topic.valid = false + topic.epoch = randomUUID() + topic.seq = 0n + topic.retained = [] + topic.items = 0 + topic.bytes = 0 + for (const connection of this.#connections.values()) { + for (const subscription of connection.subscriptions.values()) { + if (subscription.topic === topic) + this.#resync(connection, subscription) + } + } + } + + inspect() { + return { + topics: [...this.#topics.values()].map(topic => ({ retainedItems: topic.items, bytes: topic.bytes, valid: topic.valid })), + connections: [...this.#connections.values()].map(connection => ({ subscriptions: connection.subscriptions.size, bytes: connection.bytes })), + } + } + + #cacheSnapshot(connection: Connection, subscription: Subscription, requestCursor: string | null, snapshot: ProductSyncSnapshot): void { + const bytes = productByteLength(encodeProductFrame(snapshot, PRODUCT_LIMITS.pageBytes)) + this.#clearCachedPage(connection, subscription) + if (connection.bytes + bytes > PRODUCT_LIMITS.connectionBytes) + throw new ProductProtocolError('RESYNC_REQUIRED') + subscription.cachedPage = { requestCursor, snapshot, bytes } + connection.bytes += bytes + } + + #clearCachedPage(connection: Connection, subscription: Subscription): void { + if (subscription.cachedPage) { + connection.bytes -= subscription.cachedPage.bytes + subscription.cachedPage = undefined + } + } + + #releaseSnapshot(connection: Connection, subscription: Subscription): void { + clearTimeout(subscription.timer) + subscription.reader?.close() + subscription.reader = undefined + this.#clearCachedPage(connection, subscription) + } + + #requireConnection(id: string): Connection { + const connection = this.#connections.get(id) + if (!connection) + throw new ProductProtocolError('SYNC_INVALID') + return connection + } + + #enqueue(connection: Connection, subscription: Subscription, change: Change): boolean { + if (subscription.items + change.count > PRODUCT_LIMITS.subscriptionItems || subscription.bytes + change.bytes > PRODUCT_LIMITS.subscriptionBytes) { + this.#resync(connection, subscription) + return false + } + if (connection.bytes + change.bytes > PRODUCT_LIMITS.connectionBytes) { + this.#close(connection) + return false + } + subscription.pending.push(change) + subscription.items += change.count + subscription.bytes += change.bytes + connection.bytes += change.bytes + return true + } + + #flush(connection: Connection, subscription: Subscription): void { + if (!subscription.active) + return + for (const change of subscription.pending) { + if (change.to <= subscription.sent) + continue + const frame = encodeProductFrame({ jsonrpc: '2.0', method: 'sync.delta', params: { subscriptionId: subscription.id, syncEpoch: subscription.topic.epoch, fromSeq: encodeProductRevision(change.from), toSeq: encodeProductRevision(change.to), changes: change.changes } }) + if (!this.#send(connection, frame)) + return + subscription.sent = change.to + } + } + + #resync(connection: Connection, subscription: Subscription): void { + if (connection.subscriptions.get(subscription.id) !== subscription) + return + this.#releaseSnapshot(connection, subscription) + connection.bytes -= subscription.bytes + connection.subscriptions.delete(subscription.id) + this.#send(connection, encodeProductFrame({ jsonrpc: '2.0', method: 'sync.resyncRequired', params: { subscriptionId: subscription.id, reason: 'RESYNC_REQUIRED' } })) + } + + #send(connection: Connection, frame: string): boolean { + try { + if (connection.transport.send(frame)) + return true + } + catch {} + this.#close(connection) + return false + } + + #close(connection: Connection): void { + for (const [id, candidate] of this.#connections) { + if (candidate === connection) + this.#connections.delete(id) + } + for (const subscription of connection.subscriptions.values()) this.#releaseSnapshot(connection, subscription) + connection.subscriptions.clear() + connection.bytes = 0 + connection.transport.close() + } +} diff --git a/apps/buddy/service/src/productProtocol/ProductTaskReadModel.ts b/apps/buddy/service/src/productProtocol/ProductTaskReadModel.ts new file mode 100644 index 000000000..da645755a --- /dev/null +++ b/apps/buddy/service/src/productProtocol/ProductTaskReadModel.ts @@ -0,0 +1,88 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { z } from 'zod' +import type { MessageRecord } from '../storage/conversationHistoryRepository' +import type { ConversationRecord, ConversationRow } from '../storage/conversationRecord' +import { productTaskSchema, productTaskSummarySchema } from '../../../shared/productProtocol/queries' +import { encodeProductRevision } from '../../../shared/productProtocol/scalars' +import { CONVERSATION_CURRENT_MODEL_SELECTION_SQL, toConversationRecord } from '../storage/conversationRecord' +import { createTaskMarkRepository } from '../storage/taskMarkRepository' + +type PreviewMessage = Pick +type TaskSummary = z.infer + +export class ProductTaskReadModel { + readonly #database: DatabaseSync + readonly #marks: ReturnType + readonly #preview: (message: PreviewMessage) => string + + constructor(database: DatabaseSync, preview: (message: PreviewMessage) => string) { + this.#database = database + this.#marks = createTaskMarkRepository(database) + this.#preview = preview + } + + get(id: string) { + const row = this.#database.prepare(`SELECT conversations.*, ${CONVERSATION_CURRENT_MODEL_SELECTION_SQL} AS actor_model_selection_json FROM conversations WHERE id = ? AND deleted_at IS NULL`).get(id) as ConversationRow | undefined + return row ? this.#project(toConversationRecord(row)) : null + } + + summaries(ids: readonly string[]): ReadonlyMap { + if (!ids.length) + return new Map() + const selected = JSON.stringify([...new Set(ids)]) + const tasks = this.#database.prepare(`SELECT conversations.*, ${CONVERSATION_CURRENT_MODEL_SELECTION_SQL} AS actor_model_selection_json FROM conversations + WHERE id IN (SELECT value FROM json_each(?)) AND deleted_at IS NULL AND archived_at IS NULL`).all(selected) as unknown as ConversationRow[] + const states = new Map(this.#marks.states(tasks.map(task => task.id)).map(state => [state.conversationId, state])) + const facts = new Map(this.#database.prepare(`SELECT c.id, + CASE WHEN EXISTS (SELECT 1 FROM approvals a JOIN runs r ON r.id = a.run_id WHERE r.conversation_id = c.id AND a.status = 'pending') THEN 'awaiting_approval' + WHEN EXISTS (SELECT 1 FROM runs WHERE conversation_id = c.id AND status IN ('queued', 'running')) THEN 'running' ELSE 'idle' END AS activity, + (SELECT COUNT(*) FROM user_questions q JOIN runs r ON r.id = q.run_id WHERE r.conversation_id = c.id AND json_extract(q.request_json, '$.status') = 'pending') AS pending_question_count, + last_run.status AS last_run_status, occurrence.automation_id, occurrence.id AS occurrence_id, occurrence.scheduled_for + FROM conversations c + LEFT JOIN runs last_run ON last_run.id = (SELECT id FROM runs WHERE conversation_id = c.id AND execution_kind = 'root' AND purpose <> 'conversation.compaction' ORDER BY COALESCE(completed_at, started_at) DESC, rowid DESC LIMIT 1) + LEFT JOIN automation_occurrences occurrence ON occurrence.id = (SELECT id FROM automation_occurrences WHERE conversation_id = c.id AND deleted_at IS NULL ORDER BY scheduled_for DESC, id DESC LIMIT 1) + WHERE c.id IN (SELECT value FROM json_each(?)) AND c.deleted_at IS NULL AND c.archived_at IS NULL`).all(selected).map(row => [String(row.id), row])) + const resultIds = [...states.values()].flatMap(state => state.resultRunId ? [state.resultRunId] : []) + const results = new Map(this.#database.prepare('SELECT id, conversation_id, branch_id FROM runs WHERE id IN (SELECT value FROM json_each(?))').all(JSON.stringify(resultIds)).map(row => [String(row.id), row])) + const previews = this.#database.prepare(`SELECT c.id, ( + SELECT json_group_array(json_object('branchId', message.branch_id, 'role', message.role, 'content', json(message.content_json))) + FROM ( + SELECT m.branch_id, m.role, m.content_json FROM conversation_publications p JOIN messages m ON m.id = p.message_id + LEFT JOIN runs r ON r.id = m.run_id WHERE p.conversation_id = c.id AND p.kind = 'message' AND m.role IN ('user', 'assistant') + AND (m.run_id IS NULL OR r.execution_kind IN ('root', 'actor_child')) ORDER BY p.sequence DESC LIMIT 20 + ) message + ) AS messages FROM conversations c WHERE c.id IN (SELECT value FROM json_each(?)) AND c.deleted_at IS NULL AND c.archived_at IS NULL`).all(selected) + const text = new Map() + for (const row of previews) { + const messages = JSON.parse(String(row.messages)) as Array + for (const message of messages) { + const previewText = this.#preview(message) + if (previewText) { + text.set(String(row.id), { previewText, previewBranchId: message.branchId }) + break + } + } + } + return new Map(tasks.map((task) => { + const state = states.get(task.id)! + const fact = facts.get(task.id)! + const result = state.resultRunId ? results.get(state.resultRunId) : undefined + return [task.id, productTaskSummarySchema.parse({ + ...this.#project(toConversationRecord(task)), + activity: fact.activity, + unread: state.unread, + previewText: '', + ...text.get(task.id), + ...(result?.conversation_id === task.id ? { resultBranchId: result.branch_id, resultRunId: result.id } : {}), + lastRunStatus: fact.last_run_status ?? null, + pendingQuestionCount: Number(fact.pending_question_count), + automationOccurrence: fact.occurrence_id ? { automationId: fact.automation_id, occurrenceId: fact.occurrence_id, scheduledFor: fact.scheduled_for } : null, + })] + })) + } + + #project(task: ConversationRecord) { + const fields = Object.fromEntries(Object.keys(productTaskSchema.shape).filter(key => key in task).map(key => [key, task[key as keyof typeof task]])) + return productTaskSchema.parse({ ...fields, revision: encodeProductRevision(task.metadataRevision!), titleRevision: encodeProductRevision(task.titleRevision!) }) + } +} diff --git a/apps/buddy/service/src/productProtocol/__tests__/ConversationArchiveCommands.spec.ts b/apps/buddy/service/src/productProtocol/__tests__/ConversationArchiveCommands.spec.ts new file mode 100644 index 000000000..c8d5040da --- /dev/null +++ b/apps/buddy/service/src/productProtocol/__tests__/ConversationArchiveCommands.spec.ts @@ -0,0 +1,284 @@ +import type { RuntimeRequestHandler } from '../../../../shared/runtime/rpcPeer' +import type { BuddyServiceCommands } from '../../BuddyService' +import { mkdtempSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { DatabaseSync } from 'node:sqlite' +import { afterEach, describe, expect, it } from 'vitest' +import { ProductProtocolError } from '../../../../shared/productProtocol/errors' +import { CommandAdmissionService, createLocalCommandPrincipal } from '../../commands/CommandAdmissionService' +import { ConversationArchiveService } from '../../conversations/ConversationArchiveService' +import { ConversationCommandService } from '../../conversations/ConversationCommandService' +import { ConversationLifecycleService } from '../../conversations/ConversationLifecycleService' +import { ConversationMetadataService } from '../../conversations/ConversationMetadataService' +import { ConversationMutationGuard } from '../../conversations/ConversationMutationGuard' +import { registerConversationArchiveRpc } from '../../conversations/registerConversationArchiveRpc' +import { TaskMarkService } from '../../conversations/TaskMarkService' +import { createBuddyRepository } from '../../storage/buddyRepository' +import { createConversationArchiveRepository } from '../../storage/conversationArchiveRepository' +import { ConversationCommandRepository } from '../../storage/ConversationCommandRepository' +import { createConversationDeletionRepository } from '../../storage/conversationDeletionRepository' +import { createConversationRepository } from '../../storage/conversationRepository' +import { openBuddyMigrationCandidate } from '../../storage/database' +import { createRunInputRepository } from '../../storage/runInputRepository' +import { createRunRepository } from '../../storage/runRepository' +import { createSpaceRepository } from '../../storage/spaceRepository' +import { createTaskMarkRepository } from '../../storage/taskMarkRepository' +import { afterTransactionCommit } from '../../storage/transaction' +import { createProductCommandHandlers } from '../createProductCommandHandlers' +import { createProductSynchronization } from '../createProductSynchronization' +import { ProductConnection } from '../ProductConnection' +import { ProductQueries } from '../ProductQueries' + +const cleanup: Array<() => Promise> = [] +afterEach(async () => { + for (const dispose of cleanup.splice(0)) await dispose() +}) +const now = '2026-11-07T00:00:00.000Z' +const initialize = { protocol: { major: 1, minMinor: 0, maxMinor: 0 }, client: { kind: 'mobile', version: '1' }, supportedContentKinds: ['buddy-user-content-v1'], supportedReviewKinds: [] } + +function fixture(prepare: () => Promise = async () => 0) { + const directory = mkdtempSync(join(tmpdir(), 'lexora-archive-protocol-')) + const database: DatabaseSync = openBuddyMigrationCandidate({ databasePath: join(directory, 'buddy.sqlite3') }) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + const conversations = createConversationRepository(database) + conversations.create({ id: 'task', branchId: 'branch', spaceId: null, title: 'Archive fixture', approvalPolicy: 'policy', executionProfile: 'workspace_write', createdAt: now }) + const mutations = new ConversationMutationGuard() + const effects: unknown[] = [] + const onError = (error: unknown) => effects.push(error) + const sessions = { invalidateConversation: async () => ({ pending: 0, degraded: 0 }), refreshModelSelection: () => {} } + const lifecycle = new ConversationLifecycleService({ conversations, deletion: createConversationDeletionRepository(database), assertWritable: id => mutations.assertWritable(id), runner: { cancelAndWaitForConversation: prepare }, cancelQueuedRuns: async () => {}, sessions, onObserverError: onError }) + const archive = new ConversationArchiveService({ database, repository: createConversationArchiveRepository(database), conversations, mutations, lifecycle, onError, afterCommit: publish => afterTransactionCommit(database, publish, onError), now: () => new Date(now) }) + const metadata = new ConversationMetadataService({ database, repository: conversations, buddies: createBuddyRepository(database), runs: createRunRepository(database), runInputs: createRunInputRepository(database), spaces: createSpaceRepository(database), spaceMoves: { mutations, hasPendingMessages: () => false, hasActiveExecution: () => false, validateTarget: async () => {} }, resolveModelSelection: async input => input, sessions, onObserverError: onError }) + const marks = new TaskMarkService(createTaskMarkRepository(database), onError, database) + const principal = createLocalCommandPrincipal(database) + const admission = new CommandAdmissionService({ database, principal, sourceInstanceId: 'instance' }) + const commands = new ConversationCommandService({ database, admission, repository: new ConversationCommandRepository(database), metadata, lifecycle, archive, marks, onEffectError: onError }) + const dataGeneration = String(database.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation) + const envelope = (commandId: string, intent: unknown) => ({ commandId, intent, dataGeneration, expectedServiceInstanceId: 'instance' }) + const handlers = createProductCommandHandlers({ commands: { admission, metadata: commands } as BuddyServiceCommands, assertOwner: (context) => { + if (context.role !== 'owner') + throw new ProductProtocolError('NOT_AUTHORIZED') + }, allowedActions: new Set(), resolveQuote: () => { throw new Error('Unexpected quote') } }) + const idle = { onDidCommit: () => ({ dispose() {} }) } + const queries = new ProductQueries(database, principal.actorId, 'instance') + const sync = createProductSynchronization({ database, queries, archive, metadata, lifecycle, drafts: idle, events: idle }) + const clients: ProductConnection[] = [] + const connect = async (role: 'reader' | 'owner' = 'owner') => { + const frames: Array<{ id: string, result?: Record, error?: { code: string } }> = [] + const connection = new ProductConnection({ context: { serviceId: 'service', profileId: 'profile', dataGeneration, serviceInstanceId: 'instance', connectionId: `connection-${clients.length}`, clientSessionId: 'session', deviceId: 'device', ownerPrincipalId: 'owner', credentialId: 'credential', grantGeneration: '1', role }, assertCurrent: () => {}, phase: () => 'ready', handlers: { ...queries.handlers(), ...handlers }, sync: sync.publisher, authorizeTopic: sync.authorizeTopic, send: (frame) => { + frames.push(JSON.parse(frame)) + return true + }, close: () => {} }) + clients.push(connection) + let nextId = 0 + const send = async (method: string, params: unknown) => { + const id = String(nextId++) + await connection.receive(JSON.stringify({ jsonrpc: '2.0', id, method, params })) + return frames.find(frame => frame.id === id)! + } + await send('initialize', initialize) + return { send, frames } + } + cleanup.push(async () => { + for (const client of clients) client.close() + sync.dispose() + await admission.dispose() + await commands.dispose() + await archive.dispose() + await metadata.dispose() + await lifecycle.dispose() + marks.dispose() + database.close() + rmSync(directory, { recursive: true, force: true }) + }) + const revision = () => conversations.findById('task')!.metadataRevision! + return { database, conversations, archive, commands, admission, envelope, connect, revision, effects, handlers, sync, path: join(directory, 'buddy.sqlite3') } +} + +describe('archive product commands', () => { + it('keeps asynchronous preparation outside the receipt transaction and publishes only committed archive facts', async () => { + const started = Promise.withResolvers() + const release = Promise.withResolvers() + const f = fixture(() => { + started.resolve() + return release.promise + }) + const client = await f.connect() + const initialRevision = f.revision() + const observed: boolean[] = [] + f.archive.onDidChange(() => { + observed.push(!f.database.isTransaction && f.admission.lookup({ dataGeneration: f.envelope('archive', {}).dataGeneration, commandId: 'archive' }).lookup === 'decided') + }) + const request = f.envelope('archive', { conversationId: 'task', expectedRevision: String(f.revision()) }) + const pending = client.send('conversations.archive', request) + await started.promise + expect(f.conversations.findById('task')?.archivedAt).toBeNull() + expect(f.admission.lookup({ dataGeneration: request.dataGeneration, commandId: 'archive' })).toEqual({ lookup: 'pending' }) + expect(f.database.isTransaction).toBe(false) + const duplicate = client.send('conversations.archive', request) + release.resolve(0) + const accepted = await pending + expect((await duplicate).result?.receipt).toEqual(accepted.result?.receipt) + expect(f.revision()).toBe(initialRevision + 1) + expect(accepted.result?.receipt).toMatchObject({ decision: 'accepted', result: { archived: true } }) + expect(observed).toEqual([true]) + const listed = await client.send('conversations.listArchived', { kind: 'archived' }) + expect(listed.result?.items).toMatchObject([{ id: 'task', archivedAt: now, revision: String(f.revision()) }]) + expect(new ProductQueries(f.database, 'desktop-local', 'instance').read('conversations.list', { limit: 100, cursor: null })).toEqual({ items: [], nextCursor: null }) + const restored = await client.send('conversations.restoreArchive', f.envelope('restore', { conversationId: 'task', expectedRevision: String(f.revision()) })) + expect(restored.result?.receipt.result).toEqual({ restored: true }) + expect((await client.send('conversations.archive', request)).result?.receipt).toEqual(accepted.result?.receipt) + expect(f.conversations.findById('task')?.archivedAt).toBeNull() + const reopened = new DatabaseSync(f.path, { readOnly: true }) + try { + const admission = new CommandAdmissionService({ database: reopened, principal: createLocalCommandPrincipal(reopened), sourceInstanceId: 'reopened-instance' }) + expect(admission.lookup({ dataGeneration: request.dataGeneration, commandId: 'archive' })).toMatchObject({ lookup: 'decided', receipt: { decision: 'accepted', result: { archived: true } } }) + await admission.dispose() + } + finally { reopened.close() } + expect(f.effects).toEqual([]) + }) + + it('keeps archived history and exact subscriptions readable while rejecting metadata writes and deleted history', async () => { + const f = fixture() + const runs = createRunRepository(f.database) + runs.create({ id: 'historical-run', conversationId: 'task', branchId: 'branch', triggeringMessageId: 'input', provider: 'fixture', model: 'model', purpose: 'chat', status: 'completed', startedAt: now, completedAt: now, approvalPolicy: 'policy', executionProfile: 'workspace_write', piSessionFile: null }) + f.conversations.createMessage({ id: 'historical-answer', conversationId: 'task', branchId: 'branch', runId: 'historical-run', role: 'assistant', content: { text: 'Archived answer' }, createdAt: now }) + const client = await f.connect() + await client.send('conversations.archive', f.envelope('archive-readable', { conversationId: 'task', expectedRevision: String(f.revision()) })) + expect((await client.send('conversations.list', {})).result?.items).toEqual([]) + expect((await client.send('conversations.get', { conversationId: 'task' })).result).toMatchObject({ id: 'task', revision: String(f.revision()) }) + expect((await client.send('conversations.listBranches', { conversationId: 'task' })).result?.items).toMatchObject([{ id: 'branch' }]) + expect((await client.send('conversations.listMessages', { conversationId: 'task', branchId: 'branch' })).result?.items).toMatchObject([{ id: 'historical-answer', body: { text: 'Archived answer' } }]) + expect((await client.send('runs.get', { runId: 'historical-run' })).result).toMatchObject({ id: 'historical-run' }) + expect((await client.send('runs.list', { conversationId: 'task', branchId: 'branch' })).result?.items).toMatchObject([{ id: 'historical-run' }]) + const snapshot = await client.send('sync.subscribe', { subscriptionId: 'archived-detail', topic: 'conversation:task:branch' }) + expect(snapshot.error).toBeUndefined() + expect(snapshot.result).toMatchObject({ mode: 'snapshot', items: [{ entity: 'task', value: { id: 'task' } }] }) + const denied = await client.send('conversations.rename', f.envelope('rename-archived', { conversationId: 'task', title: 'Must not change', expectedTitleRevision: String(f.conversations.findById('task')!.titleRevision) })) + expect(denied.result?.receipt).toMatchObject({ decision: 'rejected', errorCode: 'SOURCE_CHANGED' }) + expect(f.conversations.findById('task')?.title).toBe('Archive fixture') + f.conversations.markDeleted('task', now) + expect((await client.send('conversations.get', { conversationId: 'task' })).result).toBeNull() + expect((await client.send('conversations.listBranches', { conversationId: 'task' })).result?.items).toEqual([]) + expect((await client.send('conversations.listMessages', { conversationId: 'task', branchId: 'branch' })).error?.code).toBe('OBJECT_DELETED') + expect((await client.send('runs.get', { runId: 'historical-run' })).result).toBeNull() + expect((await client.send('sync.subscribe', { subscriptionId: 'deleted-detail', topic: 'conversation:task:branch' })).error?.code).toBe('OBJECT_DELETED') + }) + + it('rejects stale revisions after preparation without committing an archive or changing the fixed rejection', async () => { + const started = Promise.withResolvers() + const release = Promise.withResolvers() + const f = fixture(() => { + started.resolve() + return release.promise + }) + const client = await f.connect() + const request = f.envelope('stale', { conversationId: 'task', expectedRevision: String(f.revision()) }) + const pending = client.send('conversations.archive', request) + await started.promise + f.database.prepare('UPDATE conversations SET metadata_revision = metadata_revision + 1 WHERE id = ?').run('task') + release.resolve(0) + const rejected = await pending + expect(rejected.result?.receipt).toMatchObject({ decision: 'rejected', errorCode: 'SOURCE_CHANGED' }) + expect(f.conversations.findById('task')?.archivedAt).toBeNull() + expect((await client.send('conversations.archive', request)).result?.receipt).toEqual(rejected.result?.receipt) + }) + + it('requires owner role and an explicit decimal revision for optional archive capabilities', async () => { + const f = fixture() + const reader = await f.connect('reader') + expect(reader.frames[0]?.result?.capabilities).not.toContain('conversations.listArchived') + for (const [method, params] of [ + ['conversations.listArchived', { kind: 'archived' }], + ['conversations.getArchivePolicy', {}], + ['conversations.archive', f.envelope('reader-archive', { conversationId: 'task', expectedRevision: String(f.revision()) })], + ['conversations.setArchivePolicy', f.envelope('reader-policy', { days: 3 })], + ] as const) expect((await reader.send(method, params)).error?.code).toBe('NOT_AUTHORIZED') + const owner = await f.connect() + expect((await owner.send('conversations.archive', f.envelope('no-cas', { conversationId: 'task' }))).error?.code).toBe('INVALID_PARAMS') + expect((await owner.send('conversations.archive', f.envelope('numeric-cas', { conversationId: 'task', expectedRevision: f.revision() }))).error?.code).toBe('INVALID_PARAMS') + expect(f.database.prepare('SELECT count(*) AS count FROM command_receipts').get()!.count).toBe(0) + expect(f.conversations.findById('task')?.archivedAt).toBeNull() + }) + + it('publishes archive removal and restore through the existing task index without changing old task DTOs', async () => { + const f = fixture() + const client = await f.connect() + const frames: any[] = [] + const disconnect = f.sync.publisher.connect('phone', { send: (frame) => { + frames.push(JSON.parse(frame)) + return true + }, close() {} }) + try { + const subscription = f.sync.publisher.subscribe('phone', { subscriptionId: 'tasks', topic: 'taskIndex' }) + subscription.activate() + f.sync.publisher.ack('phone', { subscriptionId: 'tasks', ...subscription.snapshot.cursor }) + await client.send('conversations.archive', f.envelope('archive', { conversationId: 'task', expectedRevision: String(f.revision()) })) + await client.send('conversations.restoreArchive', f.envelope('restore', { conversationId: 'task', expectedRevision: String(f.revision()) })) + const changes = frames.flatMap(frame => frame.params.changes ?? []) + expect(changes).toMatchObject([{ kind: 'remove', id: 'task' }, { kind: 'upsert', value: { id: 'task' } }]) + expect(changes[1].value).not.toHaveProperty('archivedAt') + expect(f.effects).toEqual([]) + } + finally { disconnect() } + }) + + it('refuses raw runtime writes instead of bypassing durable command admission', async () => { + const f = fixture() + const handlers = new Map() + const dispose = registerConversationArchiveRpc({ archive: f.archive, rpc: { onRequest: (method, handler) => { + handlers.set(method, handler) + return () => { + handlers.delete(method) + } + } } }) + try { + for (const method of ['archive', 'restoreArchive', 'deleteArchived', 'setArchivePolicy']) { + const input = method === 'setArchivePolicy' ? { days: 3 } : { conversationId: 'task', expectedRevision: f.revision() } + await expect(Promise.resolve().then(() => handlers.get(`conversations.${method}`)!(input, new AbortController().signal))).rejects.toMatchObject({ code: 'METHOD_NOT_AVAILABLE' }) + } + expect(f.archive.getPolicy()).toEqual({ days: 0 }) + expect(f.database.prepare('SELECT count(*) AS count FROM command_receipts').get()!.count).toBe(0) + expect(f.conversations.findById('task')?.archivedAt).toBeNull() + } + finally { dispose() } + }) + + it('commits deletion acceptance with its purge job and never restores an accepted deletion', async () => { + const f = fixture() + const client = await f.connect() + await client.send('conversations.archive', f.envelope('archive', { conversationId: 'task', expectedRevision: String(f.revision()) })) + const request = f.envelope('delete', { conversationId: 'task', expectedRevision: String(f.revision()) }) + const accepted = await client.send('conversations.deleteArchived', request) + expect(accepted.result?.receipt).toMatchObject({ decision: 'accepted', result: { deleted: true } }) + expect(f.database.prepare('SELECT status, origin FROM conversation_deletion_jobs WHERE conversation_id = ?').get('task')).toMatchObject({ status: 'pending', origin: 'conversation' }) + expect((await client.send('conversations.listArchived', { kind: 'deleting' })).result?.items).toMatchObject([{ id: 'task', deletionStatus: 'pending' }]) + expect((await client.send('conversations.deleteArchived', request)).result?.receipt).toEqual(accepted.result?.receipt) + expect((await client.send('conversations.restoreArchive', f.envelope('restore-deleting', { conversationId: 'task', expectedRevision: String(f.revision()) }))).result?.receipt).toMatchObject({ decision: 'rejected', errorCode: 'OBJECT_DELETED' }) + expect(f.effects).toEqual([]) + }) + + it('restricts legacy cleanup to removed tasks and preserves its original durable receipt', async () => { + const f = fixture() + const client = await f.connect() + const active = await client.send('conversations.deleteLegacyRemoved', f.envelope('active-legacy', { conversationId: 'task', expectedRevision: String(f.revision()) })) + expect(active.result?.receipt).toMatchObject({ decision: 'rejected', errorCode: 'SOURCE_CHANGED' }) + expect(f.database.prepare('SELECT count(*) AS count FROM conversation_deletion_jobs').get()!.count).toBe(0) + f.conversations.create({ id: 'ordinary', branchId: 'ordinary-branch', spaceId: null, title: 'Ordinary deletion', approvalPolicy: 'policy', executionProfile: 'workspace_write', createdAt: now }) + createConversationDeletionRepository(f.database).commit('ordinary', now) + const ordinaryRequest = f.envelope('ordinary-legacy', { conversationId: 'ordinary', expectedRevision: String(f.conversations.findById('ordinary')!.metadataRevision) }) + const rejected = await client.send('conversations.deleteLegacyRemoved', ordinaryRequest) + expect(rejected.result?.receipt).toMatchObject({ decision: 'rejected', errorCode: 'SOURCE_CHANGED' }) + expect((await client.send('conversations.deleteLegacyRemoved', ordinaryRequest)).result?.receipt).toEqual(rejected.result?.receipt) + expect(f.database.prepare('SELECT origin FROM conversation_deletion_jobs WHERE conversation_id = ?').get('ordinary')?.origin).toBe('conversation') + f.conversations.markDeleted('task', now) + const request = f.envelope('legacy', { conversationId: 'task', expectedRevision: String(f.revision()) }) + const accepted = await client.send('conversations.deleteLegacyRemoved', request) + expect(accepted.result?.receipt).toMatchObject({ decision: 'accepted', result: { deleted: true } }) + expect(f.database.prepare('SELECT status, origin FROM conversation_deletion_jobs WHERE conversation_id = ?').get('task')).toMatchObject({ status: 'pending', origin: 'legacy_removed' }) + expect((await client.send('conversations.deleteLegacyRemoved', request)).result?.receipt).toEqual(accepted.result?.receipt) + expect(f.effects).toEqual([]) + }) +}) diff --git a/apps/buddy/service/src/productProtocol/__tests__/ProductConnection.spec.ts b/apps/buddy/service/src/productProtocol/__tests__/ProductConnection.spec.ts new file mode 100644 index 000000000..2b7b90805 --- /dev/null +++ b/apps/buddy/service/src/productProtocol/__tests__/ProductConnection.spec.ts @@ -0,0 +1,51 @@ +import type { TrustedProductContext } from '../../../../shared/productProtocol/identity' +import { describe, expect, it } from 'vitest' +import { ProductConnection } from '../ProductConnection' + +const context: TrustedProductContext = { serviceId: 'service', profileId: 'profile', dataGeneration: 'data', serviceInstanceId: 'instance', connectionId: 'connection', clientSessionId: 'session', deviceId: 'device', ownerPrincipalId: 'owner', credentialId: 'credential', grantGeneration: '1', role: 'owner' } +const initialize = { protocol: { major: 1, minMinor: 0, maxMinor: 0 }, client: { kind: 'desktop', version: '1' }, supportedContentKinds: ['buddy-user-content-v1'], supportedReviewKinds: [] } +const command = { dataGeneration: 'data', commandId: 'command', expectedServiceInstanceId: 'instance', intent: { draftId: 'draft', expectedRevision: '0' } } +const receipt = { dataGeneration: 'data', commandId: 'command', method: 'chat.startTurn', decision: 'accepted', decidedAt: '2026-01-01T00:00:00.000Z', target: { runId: 'run' }, result: {} } +function setup(options: { handle?: () => unknown | Promise } = {}) { + const frames: Record[] = [] + const connection = new ProductConnection({ + context, + assertCurrent() {}, + phase: () => 'ready', + handlers: { 'chat.startTurn': { handle: options.handle ?? (() => ({ receipt })) }, 'commands.get': { handle: () => ({ lookup: 'pending' }) } }, + send: (raw) => { + frames.push(JSON.parse(raw)) + return true + }, + close() {}, + }) + const send = (method: string, params: unknown, id = 'request') => connection.receive(JSON.stringify({ jsonrpc: '2.0', id, method, params })) + return { frames, send } +} + +describe('productConnection', () => { + it('requires one compatible initialization without trusting claimed identity', async () => { + const client = setup() + await client.send('chat.startTurn', command) + expect(client.frames.at(-1)?.error.code).toBe('NOT_INITIALIZED') + await client.send('initialize', { ...initialize, ownerPrincipalId: 'forged' }) + expect(client.frames.at(-1)?.error.code).toBe('INVALID_PARAMS') + await client.send('initialize', { ...initialize, protocol: { major: 2, minMinor: 0, maxMinor: 0 } }) + expect(client.frames.at(-1)?.error.code).toBe('PROTOCOL_INCOMPATIBLE') + await client.send('initialize', initialize) + expect(client.frames.at(-1)?.result.connectionId).toBe(context.connectionId) + expect(JSON.stringify(client.frames.at(-1))).not.toContain('credential') + await client.send('initialize', initialize) + expect(client.frames.at(-1)?.error.code).toBe('ALREADY_INITIALIZED') + }) + + it('retains uncertain command outcome and hides internal causes', async () => { + const client = setup({ handle: () => { + throw new Error('SECRET /private/fixture token=do-not-emit') + } }) + await client.send('initialize', initialize) + await client.send('chat.startTurn', command) + expect(client.frames.at(-1)?.error).toMatchObject({ code: 'INTERNAL_ERROR', acceptance: 'unknown', recovery: 'query_command' }) + expect(JSON.stringify(client.frames)).not.toContain('SECRET') + }) +}) diff --git a/apps/buddy/service/src/productProtocol/__tests__/ProductPersistence.spec.ts b/apps/buddy/service/src/productProtocol/__tests__/ProductPersistence.spec.ts new file mode 100644 index 000000000..00d602ec3 --- /dev/null +++ b/apps/buddy/service/src/productProtocol/__tests__/ProductPersistence.spec.ts @@ -0,0 +1,494 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { BuddyUserContentV1 } from '../../../../shared/conversation/buddyUserContent' +import { mkdtempSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { z } from 'zod' +import { createBuddyUserContent } from '../../../../shared/conversation/buddyUserContent' +import { restoreProductContent } from '../../../../shared/productProtocol/content' +import { PRODUCT_QUERY_METHODS, productDraftSchema, productPageSchema } from '../../../../shared/productProtocol/queries' +import { publicRunEventSchema } from '../../../../shared/runs/publicRunEvent' +import { ComposerDraftCommandService } from '../../chat/ComposerDraftCommandService' +import { ComposerDraftService } from '../../chat/ComposerDraftService' +import { CommandAdmissionService, createLocalCommandPrincipal } from '../../commands/CommandAdmissionService' +import { ConversationPurgeService } from '../../conversations/ConversationPurgeService' +import { TaskMarkService } from '../../conversations/TaskMarkService' +import { SqliteRunEventWriter } from '../../events/SqliteRunEventWriter' +import { seedSqliteRun } from '../../storage/__tests__/sqliteRunFixture' +import { createComposerDraftRepository } from '../../storage/composerDraftRepository' +import { createConversationDeletionRepository } from '../../storage/conversationDeletionRepository' +import { createConversationRepository } from '../../storage/conversationRepository' +import { openBuddyMigrationCandidate } from '../../storage/database' +import { createTaskMarkRepository } from '../../storage/taskMarkRepository' +import { withTransaction } from '../../storage/transaction' +import { createProductCommandHandlers } from '../createProductCommandHandlers' +import { createProductSynchronization } from '../createProductSynchronization' +import { ProductContentReferences } from '../ProductContentReferences' +import { ProductQueries } from '../ProductQueries' +import { ProductReadViews } from '../ProductReadViews' +import { ProductSyncPublisher } from '../ProductSyncPublisher' +import { projectCommandReceipt } from '../projectCommandReceipt' + +const directories: string[] = [] +const databases = new Set() +afterEach(() => { + for (const database of databases) database.close() + databases.clear() + for (const directory of directories.splice(0)) rmSync(directory, { recursive: true, force: true }) +}) +function fixture() { + const directory = mkdtempSync(join(tmpdir(), 'lexora-product-persistence-')) + directories.push(directory) + const path = join(directory, 'buddy.sqlite3') + const database = openBuddyMigrationCandidate({ databasePath: path }) + databases.add(database) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + return { path, database } +} +const quoted: BuddyUserContentV1 = { + ...createBuddyUserContent('你好😀'), + resourceQuotes: [{ id: 'quote-1', text: '正文', textOffset: 4, source: { kind: 'artifact', title: '报告', artifactId: 'artifact', conversationId: 'conversation', runId: 'run', path: '/fixture/private/report.md', format: 'markdown', updatedAt: '2026-01-01T00:00:00.000Z' } }], +} + +function createDraftCommands(db: DatabaseSync, instance: string) { + const admission = new CommandAdmissionService({ database: db, principal: createLocalCommandPrincipal(db), sourceInstanceId: instance }) + const drafts = new ComposerDraftService(createComposerDraftRepository(db), async () => {}) + const commands = new ComposerDraftCommandService({ database: db, actorId: 'desktop-local', admission, drafts, prepareAttachments: async () => { + throw new Error('Unexpected attachment copy') + }, releaseResources: async () => {}, onCleanupError: (error) => { + throw error + } }) + const refs = new ProductContentReferences(db, 'desktop-local') + const handlers = createProductCommandHandlers({ commands: { admission, drafts: commands } as never, assertOwner: () => {}, allowedActions: new Set(), resolveQuote: id => refs.resolve(id) }) + return { handlers, commands, admission } +} + +describe('product persistence projections', () => { + it('projects bounded task-wide previews and result targets without private child or reasoning content', () => { + const { database } = fixture() + const conversations = createConversationRepository(database) + seedSqliteRun(database, 'summary-root') + seedSqliteRun(database, 'summary-private') + seedSqliteRun(database, 'summary-compaction') + seedSqliteRun(database, 'summary-other') + database.prepare(`UPDATE runs SET status = 'completed', completed_at = '2026-10-01T00:00:00.000Z' WHERE id = 'summary-root'`).run() + database.prepare(`UPDATE runs SET execution_kind = 'npc_child', status = 'failed' WHERE id = 'summary-private'`).run() + database.prepare(`UPDATE runs SET purpose = 'conversation.compaction', status = 'completed' WHERE id = 'summary-compaction'`).run() + const prefix = '中'.repeat(239) + const visible = conversations.createMessage({ id: 'summary-visible', conversationId: 'task-1', branchId: 'branch-task-1', runId: 'summary-root', role: 'assistant', content: { text: `${prefix}😀继续`, truncated: false }, createdAt: '2026-10-01T00:00:00.000Z' }) + conversations.createBranch({ id: 'summary-other-branch', conversationId: 'task-1', parentBranchId: 'branch-task-1', forkedFromMessageId: visible.id, activate: false, createdAt: '2026-10-02T00:00:00.000Z' }) + database.prepare(`UPDATE runs SET branch_id = 'summary-other-branch', status = 'failed', completed_at = '2026-10-03T00:00:00.000Z' WHERE id = 'summary-other'`).run() + conversations.createMessage({ id: 'summary-other-message', conversationId: 'task-1', branchId: 'summary-other-branch', runId: 'summary-other', role: 'assistant', content: '另一分支正文', createdAt: '2026-10-03T00:00:00.000Z' }) + const insert = database.prepare('INSERT INTO messages (id, conversation_id, branch_id, run_id, role, content_json, created_at) VALUES (?, \'task-1\', \'branch-task-1\', ?, ?, ?, \'2026-10-04T00:00:00.000Z\')') + insert.run('private-summary', 'summary-private', 'assistant', JSON.stringify({ text: 'private child result' })) + insert.run('reasoning-summary', 'summary-root', 'assistant', JSON.stringify({ text: 'private reasoning', kind: 'reasoning' })) + insert.run('tool-summary', 'summary-root', 'tool', JSON.stringify({ text: 'tool output' })) + const queries = new ProductQueries(database, 'desktop-local', 'instance') + const before = database.prepare('SELECT total_changes() AS value').get()!.value + expect(queries.task('task-1', true)).toMatchObject({ unread: true, previewText: '另一分支正文', previewBranchId: 'summary-other-branch', resultBranchId: 'summary-other-branch', resultRunId: 'summary-other', lastRunStatus: 'failed' }) + expect(database.prepare('SELECT total_changes() AS value').get()!.value).toBe(before) + for (const status of ['queued', 'running', 'completed', 'failed', 'cancelled']) { + database.prepare('UPDATE runs SET status = ?, started_at = \'2026-10-05T00:00:00.000Z\', completed_at = CASE WHEN ? IN (\'queued\', \'running\') THEN NULL ELSE \'2026-10-05T00:00:00.000Z\' END WHERE id = \'summary-root\'').run(status, status) + expect(queries.task('task-1', true)).toMatchObject({ lastRunStatus: status }) + } + database.prepare(`UPDATE conversations SET active_branch_id = 'summary-other-branch' WHERE id = 'task-1'`).run() + expect(queries.task('task-1', true)).toMatchObject({ previewText: '另一分支正文', lastRunStatus: 'cancelled', resultBranchId: 'summary-other-branch', resultRunId: 'summary-other' }) + database.prepare(`UPDATE conversations SET active_branch_id = 'branch-task-1' WHERE id = 'task-1'`).run() + expect(queries.task('task-1', true)).toMatchObject({ previewText: '另一分支正文', previewBranchId: 'summary-other-branch', lastRunStatus: 'cancelled' }) + expect(conversations.findById('task-1')?.activeBranchId).toBe('branch-task-1') + database.prepare('DELETE FROM messages WHERE id = ?').run('summary-other-message') + expect(queries.task('task-1', true)).toMatchObject({ previewText: prefix, previewBranchId: 'branch-task-1' }) + conversations.markDeleted('task-1', '2026-10-05T00:00:00.000Z') + expect(queries.task('task-1', true)).toBeNull() + expect(queries.read('conversations.list', { limit: 100, cursor: null })).toEqual({ items: [], nextCursor: null }) + }) + + it('isolates query revisions from unrelated writes and rollbacks while observing other connections', () => { + const { database, path } = fixture() + const conversations = createConversationRepository(database) + for (const id of ['first', 'second']) + conversations.create({ id, branchId: `branch-${id}`, title: id, spaceId: null, createdAt: '2026-01-01T00:00:00.000Z', approvalPolicy: 'policy', executionProfile: 'workspace_write' }) + const queries = new ProductQueries(database, 'desktop-local', 'instance') + const read = (cursor: string | null) => PRODUCT_QUERY_METHODS['conversations.list'].output.parse(queries.read('conversations.list', { limit: 1, cursor })) as { items: unknown[], nextCursor: string | null } + const first = read(null) + expect(first.nextCursor).toEqual(expect.any(String)) + const writer = openBuddyMigrationCandidate({ databasePath: path }) + databases.add(writer) + writer.prepare('UPDATE private_network_state SET last_observed_ms = last_observed_ms + 1').run() + expect(() => withTransaction(writer, () => { + writer.prepare('UPDATE conversations SET title = ? WHERE id = ?').run('rolled back', 'second') + throw new Error('ROLLBACK_FIXTURE') + })).toThrow('ROLLBACK_FIXTURE') + expect(read(first.nextCursor)).toMatchObject({ items: [{ id: 'second', title: 'second' }] }) + writer.prepare('UPDATE conversations SET title = ? WHERE id = ?').run('changed', 'second') + expect(() => read(first.nextCursor)).toThrow('CURSOR_EXPIRED') + }) + + it('publishes committed unread and completed-preview changes without streaming every token into the task index', () => { + const { database } = fixture() + seedSqliteRun(database, 'index-root') + const errors: unknown[] = [] + const writer = new SqliteRunEventWriter(database, error => errors.push(error)) + const repository = createTaskMarkRepository(database) + const marks = new TaskMarkService(repository, error => errors.push(error), database) + const queries = new ProductQueries(database, 'desktop-local', 'instance') + const idle = { onDidCommit: () => ({ dispose() {} }) } + const sync = createProductSynchronization({ database, queries, drafts: idle, metadata: idle, lifecycle: idle, marks, events: writer }) + const frames: any[] = [] + const disconnect = sync.publisher.connect('phone-index', { send: (frame) => { + frames.push(JSON.parse(frame)) + return true + }, close() {} }) + try { + const subscription = sync.publisher.subscribe('phone-index', { subscriptionId: 'index', topic: 'taskIndex' }) + subscription.activate() + sync.publisher.ack('phone-index', { subscriptionId: 'index', ...subscription.snapshot.cursor }) + expect(subscription.snapshot).toMatchObject({ items: [{ unread: false, lastRunStatus: 'queued' }] }) + writer.append({ runId: 'index-root', type: 'message.started', payload: { messageId: 'index-reply', role: 'assistant' } }) + writer.append({ runId: 'index-root', type: 'message.delta', payload: { messageId: 'index-reply', delta: '完成的公开回复😀' } }) + expect(frames).toEqual([]) + writer.append({ runId: 'index-root', type: 'message.completed', payload: { messageId: 'index-reply', role: 'assistant', content: { text: '完成的公开回复😀' }, stopReason: 'completed' } }) + expect(frames.at(-1)?.params.changes).toMatchObject([{ kind: 'upsert', value: { previewText: '完成的公开回复😀', lastRunStatus: 'queued' } }]) + writer.append({ runId: 'index-root', type: 'run.completed', payload: {} }) + expect(frames.at(-1)?.params.changes).toMatchObject([{ kind: 'upsert', value: { unread: true, lastRunStatus: 'completed' } }]) + const state = repository.getState('task-1') + marks.setRead({ conversationId: 'task-1', read: true, resultRunId: state.resultRunId, readRevision: state.readRevision }) + expect(frames.at(-1)?.params.changes).toMatchObject([{ kind: 'upsert', value: { unread: false, lastRunStatus: 'completed' } }]) + expect(errors).toEqual([]) + } + finally { + disconnect() + sync.dispose() + writer.dispose() + marks.dispose() + } + }) + + it('exports public tool detail from the matching run without leaking raw event fields', () => { + const { database } = fixture() + seedSqliteRun(database, 'tool-root') + const conversations = createConversationRepository(database) + const message = conversations.createMessage({ id: 'tool-message', conversationId: 'task-1', branchId: 'branch-task-1', runId: 'tool-root', role: 'tool', content: { toolName: 'bash', toolCallId: 'call-one', isError: true }, createdAt: '2026-01-01T00:00:00.000Z' }) + const views = new ProductReadViews(database, 'desktop-local', new ProductContentReferences(database, 'desktop-local')) + const payload = { toolCallId: 'call-one', toolName: 'bash', secret: 'raw-secret-must-not-leak', reasoning: 'private-reasoning', presentation: { card: 'terminal', command: 'printf hello', cwd: '.', exitCode: 1, signal: null, description: '检查工作目录', output: '可读取的结果😀', truncated: false } } + database.prepare(`INSERT INTO run_events (run_id, sequence, event_type, payload_json, created_at) VALUES (?, 1, 'tool.completed', ?, '2026-01-01T00:00:00.000Z')`).run('tool-root', JSON.stringify(payload)) + expect(views.message(message)).toMatchObject({ tool: { name: 'bash', isError: true }, body: { kind: 'resource', resourceId: 'message:tool-message', previewText: '检查工作目录' } }) + const document = views.document('message:tool-message')! + expect(document.bytes.toString()).toContain('printf hello') + expect(document.bytes.toString()).toContain('可读取的结果😀') + expect(document.bytes.toString()).toContain('执行失败') + expect(document.bytes.toString()).not.toContain('raw-secret') + expect(document.bytes.toString()).not.toContain('private-reasoning') + expect(views.resource('message:tool-message')).toMatchObject({ availability: 'ready', sizeBytes: String(document.bytes.byteLength), revision: document.revision }) + seedSqliteRun(database, 'private-tool') + database.prepare('UPDATE runs SET execution_kind = \'npc_child\' WHERE id = ?').run('private-tool') + database.prepare(`INSERT INTO messages (id, conversation_id, branch_id, run_id, role, content_json, created_at) VALUES ('private-tool-message','task-1','branch-task-1','private-tool','tool',?, '2026-01-01T00:00:00.000Z')`).run(JSON.stringify({ toolName: 'bash', toolCallId: 'call-one', isError: false })) + expect(views.document('message:private-tool-message')).toBeNull() + database.prepare('UPDATE conversations SET deleted_at = \'2026-01-02T00:00:00.000Z\' WHERE id = \'task-1\'').run() + expect(views.document('message:tool-message')).toBeNull() + }) + + it('pages desktop transcript payloads while excluding private runs and raw credentials', () => { + const { database } = fixture() + seedSqliteRun(database, 'transcript-root') + seedSqliteRun(database, 'transcript-private') + database.prepare('UPDATE runs SET execution_kind = \'npc_child\' WHERE id = \'transcript-private\'').run() + const insert = database.prepare('INSERT INTO run_events (run_id, sequence, event_type, payload_json, created_at) VALUES (?, ?, ?, ?, \'2026-01-01T00:00:00.000Z\')') + insert.run('transcript-root', 1, 'message.block.completed', JSON.stringify({ messageId: 'reason', kind: 'reasoning', contentIndex: 0, content: 'Checking file layout', rawProviderResponse: 'never-expose' })) + insert.run('transcript-root', 2, 'tool.completed', JSON.stringify({ toolCallId: 'shell', toolName: 'bash', isError: true, secret: 'never-expose', presentation: { card: 'terminal', command: 'uname -s', cwd: '.', exitCode: 1, signal: null, description: '环境检查', output: 'Authorization: Bearer fixture-private-token', truncated: false } })) + insert.run('transcript-root', 3, 'context.compaction.completed', JSON.stringify({ tokensBefore: 257111, estimatedTokensAfter: 45832 })) + insert.run('transcript-private', 1, 'message.block.completed', JSON.stringify({ messageId: 'private', kind: 'reasoning', contentIndex: 0, content: 'private child reasoning' })) + const queries = new ProductQueries(database, 'desktop-local', 'instance') + const input = { runId: 'transcript-root', afterSequence: '0', throughSequence: '3', limit: 2, cursor: null } + const read = (params: Record) => productPageSchema(publicRunEventSchema).parse(queries.read('runs.listTranscriptEvents', params)) + const page = read(input) + expect(page.items).toMatchObject([{ sequence: 1, payload: { kind: 'reasoning', content: 'Checking file layout' } }, { sequence: 2, payload: { toolCallId: 'shell', isError: true, presentation: { card: 'terminal', command: 'uname -s' } } }]) + expect(JSON.stringify(page)).not.toMatch(/never-expose|fixture-private-token/) + expect(page.nextCursor).not.toBeNull() + insert.run('transcript-root', 4, 'run.completed', '{}') + expect(read({ ...input, cursor: page.nextCursor }).items).toMatchObject([{ sequence: 3, payload: { tokensBefore: 257111, estimatedTokensAfter: 45832 } }]) + expect(read({ ...input, afterSequence: '2' }).items.map(event => event.sequence)).toEqual([3]) + expect(read({ ...input, runId: 'transcript-private' })).toEqual({ items: [], nextCursor: null }) + database.prepare('UPDATE conversations SET deleted_at = \'2026-01-02T00:00:00.000Z\' WHERE id = \'task-1\'').run() + expect(read(input)).toEqual({ items: [], nextCursor: null }) + }) + + it('reports execution resources separately from terminal run status and hides deleted tasks', async () => { + const { database } = fixture() + seedSqliteRun(database, 'run-execution') + database.prepare('UPDATE runs SET status = \'cancelled\' WHERE id = ?').run('run-execution') + const query = new ProductQueries(database, 'desktop-local', 'instance').handlers()['runs.getExecutionState']! + const read = async () => PRODUCT_QUERY_METHODS['runs.getExecutionState'].output.parse(await query.handle({ runId: 'run-execution' }, {} as never, { supportedContentKinds: [], supportedReviewKinds: [] })) + for (const [phase, cleanup, resources] of [['closing', 'pending', 'occupied'], ['closed', 'pending', 'occupied'], ['closed', 'degraded', 'unknown'], ['closed', 'clean', 'released']]) { + database.prepare('UPDATE execution_state SET phase = ?, cleanup_state = ?, cancel_requested_at = ? WHERE run_id = ?').run(phase!, cleanup!, '2026-11-08T00:00:00.000Z', 'run-execution') + expect(await read()).toEqual({ runId: 'run-execution', generation: '0', phase, cancelRequested: true, resources }) + } + seedSqliteRun(database, 'child-execution') + database.prepare('UPDATE runs SET parent_run_id = ?, root_run_id = ? WHERE id = ?').run('run-execution', 'run-execution', 'child-execution') + expect(await read()).toMatchObject({ phase: 'closed', resources: 'occupied' }) + database.prepare('UPDATE execution_state SET phase = \'closed\', cleanup_state = \'degraded\' WHERE run_id = ?').run('child-execution') + expect(await read()).toMatchObject({ phase: 'closed', resources: 'unknown' }) + database.prepare('UPDATE execution_state SET cleanup_state = \'clean\' WHERE run_id = ?').run('child-execution') + expect(await read()).toMatchObject({ phase: 'closed', resources: 'occupied' }) + database.prepare('UPDATE runs SET status = \'completed\' WHERE id = ?').run('child-execution') + expect(await read()).toMatchObject({ phase: 'closed', resources: 'released' }) + database.prepare('UPDATE conversations SET deleted_at = ? WHERE id = ?').run('2026-11-08T00:01:00.000Z', 'task-1') + expect(await read()).toBeNull() + }) + + it('restores the exact reference after reopening and rejects mutation or cross-owner adoption', () => { + const { database, path } = fixture() + const references = new ProductContentReferences(database, 'desktop-local') + const portable = references.project(quoted) + expect(JSON.stringify(portable)).not.toContain('/fixture/private') + const id = portable.resourceQuotes![0]!.resourceId + expect(references.project(quoted)).toEqual(portable) + expect(() => database.prepare('UPDATE product_content_references SET quote_json = ? WHERE id = ?').run('{}', id)).toThrow('CONTENT_REFERENCE_IMMUTABLE') + expect(() => database.prepare('DELETE FROM product_content_references WHERE id = ?').run(id)).toThrow('CONTENT_REFERENCE_IMMUTABLE') + database.close() + databases.delete(database) + const reopened = openBuddyMigrationCandidate({ databasePath: path }) + databases.add(reopened) + const restored = new ProductContentReferences(reopened, 'desktop-local') + expect(restoreProductContent(portable, id => restored.resolve(id))).toEqual(quoted) + expect(() => new ProductContentReferences(reopened, 'other-owner').resolve(id)).toThrow('RESOURCE_UNAVAILABLE') + }) + + it.each([1, 2])('replays a version %s portable command after reopening before checking editor capabilities', async (version) => { + const { database, path } = fixture() + const references = new ProductContentReferences(database, 'desktop-local') + const portable = references.project(quoted) + const { handlers, commands } = createDraftCommands(database, 'first-instance') + const dataGeneration = String(database.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation) + const input = { dataGeneration, commandId: 'open-original', expectedServiceInstanceId: 'first-instance', intent: { draftId: 'original-draft', scope: { kind: 'global' }, initialContent: portable, initialModelSelection: null, initialExecutionConfig: { approvalPolicy: 'policy', executionProfile: 'workspace_write' }, expectedSlotRevision: '0' } } + const first = version === 1 + ? { receipt: projectCommandReceipt((await commands.execute('composerDrafts.open', { ...input, intent: { ...input.intent, expectedSlotRevision: 0, initialContent: quoted } })).receipt) } + : await handlers['composerDrafts.open']!.handle(input, {} as never, { supportedContentKinds: ['buddy-user-content-v1'], supportedReviewKinds: [] }) + expect(database.prepare('SELECT fingerprint_version AS version FROM command_receipts').get()!.version).toBe(version) + database.close() + databases.delete(database) + const reopened = openBuddyMigrationCandidate({ databasePath: path }) + databases.add(reopened) + const second = await createDraftCommands(reopened, 'second-instance').handlers['composerDrafts.open']!.handle(input, {} as never, { supportedContentKinds: [], supportedReviewKinds: [] }) + expect(second).toEqual(first) + expect(reopened.prepare('SELECT COUNT(*) AS count FROM command_receipts').get()!.count).toBe(1) + expect(reopened.prepare('SELECT COUNT(*) AS count FROM composer_drafts').get()!.count).toBe(1) + }) + + it.each(['accepted', 'rejected'])('replays a %s save after task purge and restart without restoring private quotes', async (decision) => { + const { database, path } = fixture() + const now = '2026-11-01T00:00:00.000Z' + const executionConfig = { approvalPolicy: 'policy', executionProfile: 'workspace_write' } as const + createConversationRepository(database).create({ id: 'task', branchId: 'branch-task', spaceId: null, title: 'task', createdAt: now, ...executionConfig }) + const drafts = createComposerDraftRepository(database) + drafts.open({ draftId: 'draft', scope: { kind: 'conversation_branch', conversationId: 'task', branchId: 'branch-task' }, initialContent: createBuddyUserContent(), initialExecutionConfig: executionConfig, initialModelSelection: null, now }) + const references = new ProductContentReferences(database, 'desktop-local') + const content = references.project(quoted, { kind: 'conversation', id: 'task' }) + const dataGeneration = String(database.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation) + const input = { dataGeneration, commandId: 'save-original', expectedServiceInstanceId: 'first-instance', intent: { draftId: 'draft', expectedRevision: decision === 'accepted' ? '0' : '999', content, modelSelection: null, executionConfig } } + const client = { supportedContentKinds: ['buddy-user-content-v1'], supportedReviewKinds: [] } + const first = await createDraftCommands(database, 'first-instance').handlers['composerDrafts.save']!.handle(input, {} as never, client) + expect(first).toMatchObject({ receipt: { decision } }) + createConversationDeletionRepository(database).commit('task', now) + await new ConversationPurgeService({ database, home: join(path, '..'), storageAuthority: 'sqlite' }).purge('task') + expect(() => references.resolve(content.resourceQuotes![0]!.resourceId)).toThrow('RESOURCE_UNAVAILABLE') + database.close() + databases.delete(database) + const reopened = openBuddyMigrationCandidate({ databasePath: path }) + databases.add(reopened) + const { handlers } = createDraftCommands(reopened, 'second-instance') + expect(await handlers['composerDrafts.save']!.handle(input, {} as never, client)).toEqual(first) + expect(await handlers['commands.get']!.handle({ dataGeneration, commandId: input.commandId }, {} as never, client)).toEqual({ lookup: 'decided', ...(first as object) }) + for (const intent of [ + { ...input.intent, expectedRevision: '1' }, + { ...input.intent, draftId: 'another-draft' }, + { ...input.intent, content: { ...content, body: createBuddyUserContent('changed').body } }, + { ...input.intent, content: { ...content, resourceQuotes: content.resourceQuotes!.map(quote => ({ ...quote, resourceId: 'another-resource' })) } }, + ]) { + await expect(handlers['composerDrafts.save']!.handle({ ...input, intent }, {} as never, client)).rejects.toMatchObject({ code: 'COMMAND_ID_REUSED' }) + } + expect(reopened.prepare('SELECT COUNT(*) AS count FROM command_receipts').get()!.count).toBe(1) + expect(reopened.prepare('SELECT COUNT(*) AS count FROM product_content_references').get()!.count).toBe(0) + }) + + it('continues one frozen SQLite snapshot across pages while later commits wait behind its barrier', () => { + const { database } = fixture() + const drafts = createComposerDraftRepository(database) + for (let index = 0; index < 101; index++) { + drafts.open({ draftId: `draft-${index}`, scope: { kind: 'task', draftId: `draft-${index}`, spaceId: null }, initialContent: createBuddyUserContent('before'), initialModelSelection: null, initialExecutionConfig: { approvalPolicy: 'policy', executionProfile: 'workspace_write' }, now: '2026-01-01T00:00:00.000Z' }) + } + const queries = new ProductQueries(database, 'desktop-local', 'instance') + const publisher = new ProductSyncPublisher() + const frames: any[] = [] + const disconnect = publisher.connect('connection', { send: (frame) => { + frames.push(JSON.parse(frame)) + return true + }, close: () => {} }) + publisher.register('draftIndex', { item: productDraftSchema, change: z.object({ draftId: z.string(), revision: z.string() }).strict(), readSnapshot: () => { + throw new Error('Frozen reader required') + }, openSnapshot: () => { + const reader = queries.openSnapshot('draftIndex') + return { close: reader.close, read: (cursor) => { + const page = reader.read(cursor) + return { ...page, items: page.items.map(item => productDraftSchema.parse(item)) } + } } + } }) + const first = publisher.subscribe('connection', { subscriptionId: 'drafts', topic: 'draftIndex' }) + first.activate() + expect(first.snapshot).toMatchObject({ complete: false }) + expect(() => publisher.ack('connection', { subscriptionId: 'drafts', ...first.snapshot.cursor })).toThrow('SYNC_INVALID') + const draft = drafts.findById('draft-100')! + drafts.save({ ...draft, content: createBuddyUserContent('after'), expectedRevision: draft.revision, now: '2026-01-02T00:00:00.000Z' }) + publisher.publishCommitted('draftIndex', () => [{ draftId: 'draft-100', revision: '1' }]) + expect(frames).toHaveLength(0) + if (first.snapshot.mode !== 'snapshot' || !first.snapshot.nextCursor) + throw new Error('Expected a continuation cursor') + const last = publisher.snapshotPage('connection', { subscriptionId: 'drafts', cursor: first.snapshot.nextCursor }) + expect(last.snapshot).toMatchObject({ complete: true, cursor: first.snapshot.cursor, items: [{ draftId: 'draft-100', revision: '0' }] }) + expect(publisher.snapshotPage('connection', { subscriptionId: 'drafts', cursor: first.snapshot.nextCursor }).snapshot).toEqual(last.snapshot) + last.activate() + expect(frames).toHaveLength(1) + expect(frames[0]?.params).toMatchObject({ fromSeq: '0', toSeq: '1', changes: [{ draftId: 'draft-100', revision: '1' }] }) + publisher.ack('connection', { subscriptionId: 'drafts', syncEpoch: first.snapshot.cursor.syncEpoch, seq: '1' }) + disconnect() + expect(database.prepare('PRAGMA wal_checkpoint(TRUNCATE)').get()?.busy).toBe(0) + }) + + it('projects public history and status events without exposing private child text or native handles', () => { + const { database } = fixture() + seedSqliteRun(database, 'root') + seedSqliteRun(database, 'private') + database.prepare('UPDATE runs SET execution_kind = \'npc_child\' WHERE id = ?').run('private') + database.prepare(`INSERT INTO messages (id, conversation_id, branch_id, run_id, role, content_json, created_at) VALUES ('private-output','task-1','branch-task-1','private','assistant','{"text":"private reasoning"}','2026-01-01T00:00:00.000Z')`).run() + database.prepare(`INSERT INTO run_events (run_id, sequence, event_type, payload_json, created_at) VALUES (?, 1, 'message.delta', ?, '2026-01-01T00:00:00.000Z')`).run('private', JSON.stringify({ messageId: 'private-output', delta: 'private reasoning', secret: 'must-not-leak' })) + database.prepare(`INSERT INTO run_events (run_id, sequence, event_type, payload_json, created_at) VALUES (?, 1, 'message.delta', ?, '2026-01-01T00:00:00.000Z')`).run('root', JSON.stringify({ messageId: 'public-output', delta: '你好😀', nativePath: '/fixture/private/key' })) + const queries = new ProductQueries(database, 'desktop-local', 'instance') + const history = PRODUCT_QUERY_METHODS['conversations.listMessages'].output.parse(queries.read('conversations.listMessages', { conversationId: 'task-1', branchId: 'branch-task-1', limit: 100, cursor: null })) + expect(JSON.stringify(history)).not.toContain('private reasoning') + const privateEvents = PRODUCT_QUERY_METHODS['runs.listEvents'].output.parse(queries.read('runs.listEvents', { runId: 'private', limit: 100, cursor: null, throughSequence: '1' })) + expect(privateEvents).toEqual({ items: [], nextCursor: null }) + expect(JSON.stringify(privateEvents)).not.toMatch(/private reasoning|must-not-leak|private-output/) + const publicEvents = PRODUCT_QUERY_METHODS['runs.listEvents'].output.parse(queries.read('runs.listEvents', { runId: 'root', limit: 100, cursor: null })) + expect(publicEvents).toMatchObject({ items: [{ type: 'message.delta', detail: { kind: 'text_delta', delta: '你好😀' } }] }) + expect(JSON.stringify(publicEvents)).not.toContain('/fixture/private') + }) + + it('keeps a dynamic conversation snapshot pinned across entity phases and message pages', () => { + const { database } = fixture() + seedSqliteRun(database, 'root') + const conversations = createConversationRepository(database) + for (let index = 0; index < 105; index++) conversations.createMessage({ id: `message-${String(index).padStart(3, '0')}`, conversationId: 'task-1', branchId: 'branch-task-1', runId: 'root', role: 'assistant', content: { text: `before-${index}` }, createdAt: '2026-10-01T00:00:00.000Z' }) + const queries = new ProductQueries(database, 'desktop-local', 'instance') + const reader = queries.openSnapshot('conversation:task-1:branch-task-1') + try { + let page = reader.read(null) + expect(page.items).toMatchObject([{ entity: 'task', value: { id: 'task-1' } }]) + database.prepare('UPDATE messages SET content_json = ? WHERE id = ?').run('{"text":"after"}', 'message-001') + const items = [...page.items] + while (page.nextCursor) { + page = reader.read(page.nextCursor) + items.push(...page.items) + } + expect(items.filter(item => (item as any).entity === 'message')).toHaveLength(106) + expect(items.find(item => (item as any).value.id === 'message-001')).toMatchObject({ value: { body: { text: 'before-1' } } }) + expect(items.find(item => (item as any).entity === 'run')).toMatchObject({ value: { lastEventSequence: '0', generation: '0' } }) + } + finally { reader.close() } + expect(database.prepare('PRAGMA wal_checkpoint(TRUNCATE)').get()?.busy).toBe(0) + }) + it('restores an unfinished public reply from the same frozen snapshot without exposing reasoning or private runs', () => { + const { database } = fixture() + seedSqliteRun(database, 'stream-root') + seedSqliteRun(database, 'stream-private') + database.prepare('UPDATE runs SET execution_kind = \'npc_child\', root_run_id = \'stream-root\', parent_run_id = \'stream-root\' WHERE id = \'stream-private\'').run() + const writer = new SqliteRunEventWriter(database) + const append = (runId: string, type: string, payload: unknown) => writer.append({ runId, type, payload }) + append('stream-root', 'message.started', { messageId: 'live', role: 'assistant' }) + append('stream-root', 'message.block.delta', { messageId: 'live', kind: 'reasoning', contentIndex: 0, delta: 'private reasoning' }) + append('stream-root', 'message.delta', { messageId: 'live', delta: '开头😀' }) + append('stream-private', 'message.started', { messageId: 'private', role: 'assistant' }) + append('stream-private', 'message.delta', { messageId: 'private', delta: 'private subagent' }) + const queries = new ProductQueries(database, 'desktop-local', 'instance') + const reader = queries.openSnapshot('conversation:task-1:branch-task-1') + try { + let page = reader.read(null) + append('stream-root', 'message.delta', { messageId: 'live', delta: '后来内容' }) + const items = [...page.items] as any[] + while (page.nextCursor) { + page = reader.read(page.nextCursor) + items.push(...page.items) + } + expect(items.find(item => item.value.id === 'live')).toMatchObject({ entity: 'message', value: { role: 'assistant', runId: 'stream-root', body: { kind: 'text', text: '开头😀', interrupted: false, truncated: false } } }) + expect(JSON.stringify(items)).not.toContain('private reasoning') + expect(JSON.stringify(items)).not.toContain('private subagent') + } + finally { reader.close() } + expect(queries.streaming.page('task-1', 'branch-task-1', null).items[0]?.body).toMatchObject({ text: '开头😀后来内容' }) + expect(queries.views.event('stream-root', '2')?.detail.kind).toBe('status') + append('stream-root', 'message.completed', { messageId: 'live', role: 'assistant', content: { text: '最终正文' }, stopReason: 'completed' }) + expect(queries.streaming.page('task-1', 'branch-task-1', null).items).toEqual([]) + writer.dispose() + }) + + it('publishes a message before its live deltas and replaces it with one final message', () => { + const { database } = fixture() + seedSqliteRun(database, 'live-root') + const writer = new SqliteRunEventWriter(database) + const queries = new ProductQueries(database, 'desktop-local', 'instance') + const idle = { onDidCommit: () => ({ dispose() {} }) } + const sync = createProductSynchronization({ database, queries, drafts: idle, metadata: idle, lifecycle: idle, events: writer }) + const topic = 'conversation:task-1:branch-task-1' + sync.authorizeTopic(topic) + const frames: any[] = [] + const disconnect = sync.publisher.connect('phone', { send: (frame) => { + frames.push(JSON.parse(frame)) + return true + }, close() {} }) + try { + let result = sync.publisher.subscribe('phone', { subscriptionId: 'live', topic }) + result.activate() + while (result.snapshot.mode === 'snapshot' && result.snapshot.nextCursor) { + result = sync.publisher.snapshotPage('phone', { subscriptionId: 'live', cursor: result.snapshot.nextCursor }) + result.activate() + } + sync.publisher.ack('phone', { subscriptionId: 'live', ...result.snapshot.cursor }) + writer.append({ runId: 'live-root', type: 'message.started', payload: { messageId: 'reply', role: 'assistant' } }) + writer.append({ runId: 'live-root', type: 'message.delta', payload: { messageId: 'reply', delta: '现在可以看见😀' } }) + writer.append({ runId: 'live-root', type: 'message.block.delta', payload: { messageId: 'reply', kind: 'reasoning', contentIndex: 0, delta: 'private reasoning' } }) + writer.append({ runId: 'live-root', type: 'message.completed', payload: { messageId: 'reply', role: 'assistant', content: { text: '现在可以看见😀' }, stopReason: 'completed' } }) + const changes = frames.flatMap(frame => frame.params.changes ?? []) + const messages = changes.filter(change => change.kind === 'upsert' && change.item.entity === 'message') + expect(messages.map(change => change.item.value.body.text)).toEqual(['', '现在可以看见😀']) + expect(messages.map(change => change.item.value.id)).toEqual(['reply', 'reply']) + expect(changes.filter(change => change.event?.detail.kind === 'text_delta').map(change => change.event.detail.delta)).toEqual(['现在可以看见😀']) + expect(JSON.stringify(frames)).not.toContain('private reasoning') + expect(changes.indexOf(messages[0])).toBeLessThan(changes.findIndex(change => change.event?.detail.kind === 'text_delta')) + } + finally { + disconnect() + sync.dispose() + writer.dispose() + } + }) + + it('bounds unfinished text without cutting an emoji or losing pagination', () => { + const { database } = fixture() + seedSqliteRun(database, 'bounded-root') + const writer = new SqliteRunEventWriter(database) + const prefix = '中'.repeat(131071) + writer.append({ runId: 'bounded-root', type: 'message.started', payload: { messageId: 'large', role: 'assistant' } }) + for (const delta of [prefix, '😀不能出现半个字符']) writer.append({ runId: 'bounded-root', type: 'message.delta', payload: { messageId: 'large', delta } }) + writer.append({ runId: 'bounded-root', type: 'message.started', payload: { messageId: 'large-next', role: 'assistant' } }) + writer.append({ runId: 'bounded-root', type: 'message.delta', payload: { messageId: 'large-next', delta: prefix } }) + const queries = new ProductQueries(database, 'desktop-local', 'instance') + const page = queries.streaming.page('task-1', 'branch-task-1', null) + expect(page.items[0]?.body).toEqual({ kind: 'text', text: prefix, interrupted: false, truncated: true }) + expect(page.items).toHaveLength(1) + expect(page.nextCursor).not.toBeNull() + const next = queries.streaming.page('task-1', 'branch-task-1', page.nextCursor) + expect(next.items.map(item => item.id)).toEqual(['large-next']) + expect(next.nextCursor).toBeNull() + writer.dispose() + }) +}) diff --git a/apps/buddy/service/src/productProtocol/__tests__/ProductQueryRevisions.spec.ts b/apps/buddy/service/src/productProtocol/__tests__/ProductQueryRevisions.spec.ts new file mode 100644 index 000000000..5f8aa670f --- /dev/null +++ b/apps/buddy/service/src/productProtocol/__tests__/ProductQueryRevisions.spec.ts @@ -0,0 +1,90 @@ +import type { DatabaseSync } from 'node:sqlite' +import { afterEach, describe, expect, it } from 'vitest' +import { createBuddyUserContent } from '../../../../shared/conversation/buddyUserContent' +import { SqliteRunEventWriter } from '../../events/SqliteRunEventWriter' +import { seedSqliteRun } from '../../storage/__tests__/sqliteRunFixture' +import { createConversationRepository } from '../../storage/conversationRepository' +import { openBuddyMigrationCandidate } from '../../storage/database' +import { ProductQueries } from '../ProductQueries' + +interface Page { items: Record[], nextCursor: string | null } +const databases: DatabaseSync[] = [] +afterEach(() => { + for (const database of databases.splice(0)) database.close() +}) + +function fixture() { + const database = openBuddyMigrationCandidate({ databasePath: ':memory:' }) + databases.push(database) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + seedSqliteRun(database, 'main-run', { conversationId: 'main' }) + seedSqliteRun(database, 'other-run', { conversationId: 'other' }) + const conversations = createConversationRepository(database) + const queries = new ProductQueries(database, 'desktop-local', 'instance') + const writer = new SqliteRunEventWriter(database, (error) => { + throw error + }) + const append = (runId: string) => writer.append({ runId, type: 'message.delta', payload: { messageId: `${runId}-reply`, delta: 'stream' } }) + const message = (id: string, branchId = 'branch-main', runId: string | null = null) => conversations.createMessage({ id, conversationId: 'main', branchId, runId, role: 'assistant', content: { text: id }, createdAt: `2026-10-01T00:00:${id.slice(-2)}.000Z` }) + for (const id of ['message-00', 'message-01', 'message-02']) message(id) + const history = (branchId = 'branch-main', cursor: string | null = null) => queries.read('conversations.listMessages', { conversationId: 'main', branchId, cursor, limit: 1 }) as Page + return { database, conversations, queries, writer, append, message, history } +} + +describe('product query revision scopes', () => { + it('keeps history, task summaries and branch counts pageable while any run streams', () => { + const f = fixture() + f.conversations.createBranch({ id: 'sibling', conversationId: 'main', parentBranchId: 'branch-main', forkedFromMessageId: 'message-01', activate: false, createdAt: '2026-10-01T00:00:03.000Z' }) + const tasks = (cursor: string | null = null) => f.queries.read('conversations.list', { cursor, limit: 1 }) as Page + const branches = (cursor: string | null = null) => f.queries.read('conversations.listBranches', { conversationId: 'main', cursor, limit: 1 }) as Page + const taskPage = tasks() + const branchPage = branches() + const historyPage = f.history() + for (const runId of ['other-run', 'main-run']) { + f.append(runId) + expect(tasks(taskPage.nextCursor).items).toMatchObject([{ id: 'other' }]) + expect(branches(branchPage.nextCursor).items).toMatchObject([{ id: 'sibling' }]) + expect(f.history('branch-main', historyPage.nextCursor).items).toMatchObject([{ id: 'message-01' }]) + } + f.database.prepare('UPDATE runs SET status = \'completed\' WHERE id = \'main-run\'').run() + expect(() => tasks(taskPage.nextCursor)).toThrow('CURSOR_EXPIRED') + expect(() => branches(branchPage.nextCursor)).toThrow('CURSOR_EXPIRED') + expect(f.history('branch-main', historyPage.nextCursor).items).toMatchObject([{ id: 'message-01' }]) + }) + + it('tracks only the inherited prefix when the parent branch keeps changing', () => { + const f = fixture() + f.conversations.createBranch({ id: 'child', conversationId: 'main', parentBranchId: 'branch-main', forkedFromMessageId: 'message-01', activate: false, createdAt: '2026-10-01T00:00:03.000Z' }) + f.message('child-04', 'child') + f.message('child-05', 'child') + const page = f.history('child') + f.message('message-06') + f.database.prepare('UPDATE messages SET content_json = ? WHERE id = ?').run(JSON.stringify({ text: 'outside inherited history' }), 'message-02') + expect(f.history('child', page.nextCursor).items).toMatchObject([{ id: 'child-04' }]) + f.database.prepare('UPDATE messages SET content_json = ? WHERE id = ?').run(JSON.stringify({ text: 'changed inherited history' }), 'message-00') + expect(() => f.history('child', page.nextCursor)).toThrow('CURSOR_EXPIRED') + const refreshed = f.history('child') + f.database.prepare('DELETE FROM messages WHERE id = ?').run('message-00') + expect(() => f.history('child', refreshed.nextCursor)).toThrow('CURSOR_EXPIRED') + }) + + it('invalidates inherited resource snapshots when their file becomes unavailable', () => { + const f = fixture() + const now = '2026-10-01T00:00:00.000Z' + f.database.prepare(`INSERT INTO managed_files (id, path, size_bytes, sha256, state, durability, created_at, updated_at) + VALUES ('file', '/fixture/resource', 1, ?, 'ready', 'legacy-verified', ?, ?)`).run('a'.repeat(64), now, now) + f.database.prepare(`INSERT INTO attachments (id, message_id, stored_path, name, mime_type, size_bytes, created_at) + VALUES ('attachment', 'message-02', '/fixture/resource', 'example.txt', 'text/plain', 1, ?)`).run(now) + const content = { userContent: { ...createBuddyUserContent('inherited resource'), panelResourceIds: ['resource'] }, resourceSnapshots: [{ resourceId: 'resource', attachmentId: 'attachment' }] } + f.database.prepare('UPDATE messages SET content_json = ? WHERE id = ?').run(JSON.stringify(content), 'message-00') + f.conversations.createBranch({ id: 'child', conversationId: 'main', parentBranchId: 'branch-main', forkedFromMessageId: 'message-01', activate: false, createdAt: now }) + f.message('child-05', 'child') + f.conversations.createMessage({ id: 'opaque', conversationId: 'other', branchId: 'branch-other', runId: null, role: 'assistant', content: { resourceSnapshots: ['opaque historical value'] }, createdAt: now }) + const page = f.history('child') + const resource = () => (f.queries.read('conversations.listMessages', { conversationId: 'main', branchId: 'child', limit: 100 }) as Page).items.find(message => message.id === 'message-00')?.resources + expect(resource()).toMatchObject([{ resourceId: 'attachment:attachment', availability: 'ready' }]) + f.database.prepare('UPDATE managed_files SET state = \'unavailable\' WHERE id = \'file\'').run() + expect(() => f.history('child', page.nextCursor)).toThrow('CURSOR_EXPIRED') + expect(resource()).toMatchObject([{ resourceId: 'attachment:attachment', availability: 'unavailable' }]) + }) +}) diff --git a/apps/buddy/service/src/productProtocol/__tests__/ProductServiceConnections.spec.ts b/apps/buddy/service/src/productProtocol/__tests__/ProductServiceConnections.spec.ts new file mode 100644 index 000000000..de8bc02a5 --- /dev/null +++ b/apps/buddy/service/src/productProtocol/__tests__/ProductServiceConnections.spec.ts @@ -0,0 +1,146 @@ +import type { DesktopOpenResult } from '../../../../shared/service/localServiceProtocol' +import type { RuntimePlatform } from '../../platform/RuntimePlatform' +import { mkdtemp, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { describe, expect, it } from 'vitest' +import { DEFAULT_NETWORK_PERMISSIONS } from '../../../../shared/permissions/networkPermissions' +import { DEFAULT_RUNTIME_PREFERENCES } from '../../../../shared/runtime/runtimePreferences' +import { startBuddyService } from '../../BuddyService' +import { SqliteRunEventLog } from '../../events/SqliteRunEventLog' +import { createRpcRuntimePlatform } from '../../platform/createRpcRuntimePlatform' +import { createServiceConnection } from '../../rpc/__tests__/serviceConnectionHarness' +import { openBuddyMigrationCandidate } from '../../storage/database' + +describe('product service transport ownership', () => { + it('keeps legacy reads while requiring product admission for SQLite writes', async () => { + const { service, peer, close } = await fixture() + try { + service.connectClient('product', peer.server) + service.connectClient('management', peer.server) + expect(await peer.client.request('conversations.list', {})).toEqual([]) + expect(await peer.client.request('composerResources.list', { draftId: 'missing' })).toEqual([]) + for (const method of ['composerDrafts.save', 'chat.startTurn', 'conversations.rename', 'approvals.resolve', 'approvals.approve', 'questions.answer', 'questions.cancel', 'questions.retry']) + await expect(peer.client.request(method, {})).rejects.toMatchObject({ data: { code: 'METHOD_NOT_AVAILABLE' } }) + expect(await peer.client.request('questions.list', { conversationId: 'missing', branchId: 'missing' })).toEqual([]) + const frames: unknown[] = [] + peer.client.onNotification((method, params) => { + if (method === 'product.frame') + frames.push(JSON.parse((params as { frame: string }).frame)) + }) + await peer.client.request('product.exchange', { frame: JSON.stringify({ jsonrpc: '2.0', id: 'initialize', method: 'initialize', params: { protocol: { major: 1, minMinor: 0, maxMinor: 0 }, client: { kind: 'desktop', version: 'test' }, supportedContentKinds: [], supportedReviewKinds: [] } }) }) + expect(frames).toMatchObject([{ result: { profileId: 'test-profile' } }]) + await peer.client.request('product.exchange', { frame: JSON.stringify({ jsonrpc: '2.0', id: 'host', method: 'service.host', params: {} }) }) + expect(frames.at(-1)).toMatchObject({ result: { platform: process.platform } }) + await peer.client.request('product.exchange', { frame: JSON.stringify({ jsonrpc: '2.0', id: 'info', method: 'service.info', params: {} }) }) + expect(Object.keys((frames.at(-1) as { result: object }).result)).toEqual(['name']) + } + finally { + await close() + } + }) + it('shares actor admission across connections and drains every actor before stopping', async () => { + const opened: Array> & { signal?: AbortSignal }> = [] + const visible = { accepted: true, uiReady: true, targetVisible: true, visible: true, focused: true } + const f = await fixture(async (_intent, signal) => { + const request = { ...Promise.withResolvers(), signal } + opened.push(request) + return request.promise + }) + try { + const identity = f.service.commands!.identity + const owner = String(f.database.prepare('SELECT local_actor_id FROM storage_profile').get()!.local_actor_id) + f.database.exec('INSERT INTO storage_actors VALUES (\'remote-test\', \'device\', \'active\', \'2026-11-14T00:00:00.000Z\')') + let connectionId = 0 + const connect = async (actorId: string) => { + const frames: Array<{ result?: unknown, error?: unknown }> = [] + const connection = f.service.createProductConnection({ + context: { ...identity, serviceId: 'test-service', profileId: 'test-profile', connectionId: String(++connectionId), clientSessionId: String(connectionId), deviceId: actorId, ownerPrincipalId: owner, credentialId: actorId, grantGeneration: '0', role: 'owner' }, + trustedPrincipal: { actorId, assertAuthenticated() {} }, + assertCurrent() {}, + send: (frame) => { + frames.push(JSON.parse(frame)) + return true + }, + close() {}, + }) + let requestId = 0 + const send = (method: string, params: unknown) => connection.receive(JSON.stringify({ jsonrpc: '2.0', id: String(++requestId), method, params })) + await send('initialize', { protocol: { major: 1, minMinor: 0, maxMinor: 0 }, client: { kind: 'desktop', version: 'test' }, supportedContentKinds: [], supportedReviewKinds: [] }) + return { send, connection, result: () => frames.at(-1)?.result } + } + const first = await connect(owner) + const second = await connect(owner) + const remote = await connect('remote-test') + const envelope = { dataGeneration: identity.dataGeneration, expectedServiceInstanceId: identity.serviceInstanceId, commandId: 'shared-command', intent: { kind: 'settings' } } + const lookup = { dataGeneration: identity.dataGeneration, commandId: envelope.commandId } + const pending = first.send('desktop.open', envelope) + await expect.poll(() => opened.length).toBe(1) + await second.send('commands.get', lookup) + expect(second.result()).toEqual({ lookup: 'pending' }) + await remote.send('commands.get', lookup) + expect(remote.result()).toEqual({ lookup: 'not_found' }) + first.connection.close() + const reconnected = await connect(owner) + await reconnected.send('commands.get', lookup) + expect(reconnected.result()).toEqual({ lookup: 'pending' }) + const duplicate = reconnected.send('desktop.open', envelope) + opened[0]!.resolve(visible) + await Promise.all([pending, duplicate]) + expect(opened).toHaveLength(1) + await second.send('commands.get', lookup) + expect(second.result()).toMatchObject({ lookup: 'decided', receipt: { decision: 'accepted', result: { desktop: visible } } }) + const separate = remote.send('desktop.open', envelope) + await expect.poll(() => opened.length).toBe(2) + opened[1]!.resolve(visible) + await separate + expect(f.database.prepare('SELECT actor_id FROM command_receipts WHERE command_id = ? ORDER BY actor_id').all(envelope.commandId)).toEqual([{ actor_id: owner }, { actor_id: 'remote-test' }].sort((a, b) => a.actor_id.localeCompare(b.actor_id))) + const interrupted = remote.send('desktop.open', { ...envelope, commandId: 'shutdown-command' }) + await expect.poll(() => opened.length).toBe(3) + expect(f.service.prepareIdleShutdown()).toBe(false) + let stopped = false + const stopping = f.service.dispose().then(() => { + stopped = true + }) + expect(opened[2]!.signal?.aborted).toBe(true) + await new Promise(resolve => setTimeout(resolve, 20)) + expect(stopped).toBe(false) + opened[2]!.resolve(visible) + await Promise.all([interrupted, stopping]) + expect(f.database.prepare('SELECT command_id FROM command_receipts WHERE command_id = ?').all('shutdown-command')).toEqual([]) + } + finally { + for (const request of opened) request.resolve(visible) + await f.close() + } + }) +}) + +async function fixture(openDesktop?: RuntimePlatform['openDesktop']) { + const root = await mkdtemp(join(tmpdir(), 'lexora-product-peers-')) + const buddyHome = join(root, 'buddy') + const database = openBuddyMigrationCandidate({ databasePath: join(buddyHome, 'buddy.sqlite3') }) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + const events = new SqliteRunEventLog({ database }) + const peer = createServiceConnection() + for (const method of ['extensions.agent.list', 'extensions.skills.list', 'host.extensions.shells.list']) + peer.client.onRequest(method, () => []) + peer.client.onRequest('host.networkPermissions.get', () => structuredClone(DEFAULT_NETWORK_PERMISSIONS)) + peer.client.onRequest('host.images.preferences', () => ({})) + peer.client.onRequest('host.runtimePreferences.get', () => ({ ...DEFAULT_RUNTIME_PREFERENCES, cacheWarming: 'off', codemode: 'off' })) + peer.client.onRequest('host.credentials.list', () => ({ ok: true, providers: [] })) + peer.client.onRequest('host.credentials.read', () => ({ ok: true, value: null })) + const service = await startBuddyService({ buddyHome, database, eventLog: events, platform: { ...createRpcRuntimePlatform(peer.server), ...(openDesktop ? { openDesktop } : {}) }, builtinSkillsDirectories: [], productIdentity: { serviceId: 'test-service', profileId: 'test-profile' } }) + return { + database, + peer, + service, + async close() { + await service.dispose() + await events.close() + peer.close() + database.close() + await rm(root, { recursive: true, force: true }) + }, + } +} diff --git a/apps/buddy/service/src/productProtocol/__tests__/ProductSyncPublisher.spec.ts b/apps/buddy/service/src/productProtocol/__tests__/ProductSyncPublisher.spec.ts new file mode 100644 index 000000000..f96be80dc --- /dev/null +++ b/apps/buddy/service/src/productProtocol/__tests__/ProductSyncPublisher.spec.ts @@ -0,0 +1,99 @@ +import { describe, expect, it } from 'vitest' +import { z } from 'zod' +import { PRODUCT_LIMITS } from '../../../../shared/productProtocol/limits' +import { ProductSyncPublisher } from '../ProductSyncPublisher' + +function fixture(topics = 1) { + const publisher = new ProductSyncPublisher() + let state = 0 + for (let index = 0; index < topics; index++) + publisher.register(`topic-${index}`, { item: z.number(), change: z.union([z.number(), z.string()]), readSnapshot: () => ({ items: [state], complete: true, nextCursor: null }) }) + const frames: any[] = [] + let closed = false + publisher.connect('connection', { send: (frame) => { + frames.push(JSON.parse(frame)) + return true + }, close: () => { + closed = true + } }) + return { publisher, frames, setState: (value: number) => { + state = value + }, closed: () => closed } +} + +describe('productSyncPublisher', () => { + it('buffers committed changes until the exact snapshot response has been queued', () => { + const { publisher, frames, setState } = fixture() + const { snapshot, activate } = publisher.subscribe('connection', { subscriptionId: 'sub', topic: 'topic-0' }) + setState(1) + publisher.publishCommitted('topic-0', () => [1]) + setState(2) + publisher.publishCommitted('topic-0', () => [2]) + expect(frames).toEqual([]) + expect(snapshot).toMatchObject({ mode: 'snapshot', items: [0], cursor: { seq: '0' } }) + activate() + expect(frames.map(frame => [frame.params.fromSeq, frame.params.toSeq])).toEqual([['0', '1'], ['1', '2']]) + publisher.ack('connection', { subscriptionId: 'sub', syncEpoch: snapshot.cursor.syncEpoch, seq: '2' }) + expect(() => publisher.ack('connection', { subscriptionId: 'sub', syncEpoch: snapshot.cursor.syncEpoch, seq: '3' })).toThrow('SYNC_INVALID') + }) + + it('resumes only retained continuous epochs and bounds slow subscriptions', () => { + const { publisher, frames } = fixture() + const initial = publisher.subscribe('connection', { subscriptionId: 'sub', topic: 'topic-0' }) + initial.activate() + for (let index = 0; index <= PRODUCT_LIMITS.subscriptionItems; index++) + publisher.publishCommitted('topic-0', () => [index]) + expect(frames.at(-1)?.method).toBe('sync.resyncRequired') + const resumed = publisher.subscribe('connection', { subscriptionId: 'fresh', topic: 'topic-0', base: { syncEpoch: initial.snapshot.cursor.syncEpoch, seq: '256' } }) + expect(resumed.snapshot.mode).toBe('resume') + resumed.activate() + expect(frames.at(-1)?.params.toSeq).toBe('257') + publisher.unsubscribe('connection', 'fresh') + for (let index = 0; index < PRODUCT_LIMITS.topicItems; index++) + publisher.publishCommitted('topic-0', () => [index]) + const expired = publisher.subscribe('connection', { subscriptionId: 'expired', topic: 'topic-0', base: initial.snapshot.cursor }) + expect(expired.snapshot.mode).toBe('snapshot') + }) + + it('rebuilds a fresh snapshot when projection failure races an in-flight acknowledgement', () => { + const { publisher, frames, setState } = fixture() + const initial = publisher.subscribe('connection', { subscriptionId: 'sub', topic: 'topic-0' }) + initial.activate() + publisher.publishCommitted('topic-0', () => [1]) + expect(frames.at(-1)?.params.toSeq).toBe('1') + setState(2) + publisher.publishCommitted('topic-0', () => { + throw new Error('projection failed') + }) + expect(frames.at(-1)?.method).toBe('sync.resyncRequired') + expect(() => publisher.ack('connection', { subscriptionId: 'sub', syncEpoch: initial.snapshot.cursor.syncEpoch, seq: '1' })).toThrow('RESYNC_REQUIRED') + const fresh = publisher.subscribe('connection', { subscriptionId: 'fresh', topic: 'topic-0' }) + fresh.activate() + expect(fresh.snapshot).toMatchObject({ mode: 'snapshot', items: [2] }) + publisher.ack('connection', { subscriptionId: 'fresh', ...fresh.snapshot.cursor }) + expect(() => publisher.ack('connection', { subscriptionId: 'fresh', ...initial.snapshot.cursor })).toThrow('SYNC_INVALID') + }) + + it('closes only the over-budget connection while commits continue', () => { + const { publisher, closed } = fixture(6) + const healthy: any[] = [] + publisher.connect('healthy', { send: (frame) => { + healthy.push(JSON.parse(frame)) + return true + }, close: () => { + throw new Error('Healthy client must remain') + } }) + const healthySub = publisher.subscribe('healthy', { subscriptionId: 'healthy-sub', topic: 'topic-0' }) + healthySub.activate() + for (let index = 0; index < 6; index++) { + const sub = publisher.subscribe('connection', { subscriptionId: `sub-${index}`, topic: `topic-${index}` }) + sub.activate() + } + for (let index = 0; index < 6; index++) + publisher.publishCommitted(`topic-${index}`, () => ['x'.repeat(800_000)]) + expect(closed()).toBe(true) + publisher.ack('healthy', { subscriptionId: 'healthy-sub', syncEpoch: healthySub.snapshot.cursor.syncEpoch, seq: '1' }) + publisher.publishCommitted('topic-0', () => [7]) + expect(healthy.at(-1)?.params.changes).toEqual([7]) + }) +}) diff --git a/apps/buddy/service/src/productProtocol/__tests__/ProductTaskReads.spec.ts b/apps/buddy/service/src/productProtocol/__tests__/ProductTaskReads.spec.ts new file mode 100644 index 000000000..cbdd4ae1d --- /dev/null +++ b/apps/buddy/service/src/productProtocol/__tests__/ProductTaskReads.spec.ts @@ -0,0 +1,43 @@ +import type { DatabaseSync } from 'node:sqlite' +import { mkdtempSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { seedSqliteRun } from '../../storage/__tests__/sqliteRunFixture' +import { openBuddyMigrationCandidate } from '../../storage/database' +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 }) +}) + +describe('product task reads', () => { + it('keeps batched summaries pinned to one SQLite snapshot across pages', () => { + const directory = mkdtempSync(join(tmpdir(), 'lexora-task-read-')) + directories.push(directory) + const database = openBuddyMigrationCandidate({ databasePath: join(directory, 'buddy.sqlite3') }) + databases.push(database) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + for (let index = 0; index < 101; index++) + seedSqliteRun(database, `run-${index}`, { conversationId: `task-${index}` }) + const queries = new ProductQueries(database, 'desktop-local', 'instance') + const snapshot = queries.openSnapshot('taskIndex') + try { + const first = snapshot.read(null) + expect(first.items).toHaveLength(100) + expect(first.nextCursor).not.toBeNull() + database.prepare('UPDATE conversations SET title = ? WHERE id = ?').run('Changed after snapshot', 'task-100') + database.prepare('UPDATE runs SET status = ?, completed_at = ? WHERE id = ?').run('completed', '2026-11-01T00:00:00.000Z', 'run-100') + const last = snapshot.read(first.nextCursor) + expect(last).toMatchObject({ complete: true, nextCursor: null, items: [{ id: 'task-100', title: null, activity: 'running', unread: false, lastRunStatus: 'queued', previewText: 'Fixture input' }] }) + expect(queries.task('task-100', true)).toMatchObject({ title: 'Changed after snapshot', activity: 'idle', unread: true, lastRunStatus: 'completed' }) + } + finally { + snapshot.close() + } + expect(database.prepare('PRAGMA wal_checkpoint(TRUNCATE)').get()?.busy).toBe(0) + }) +}) diff --git a/apps/buddy/service/src/productProtocol/createProductCommandHandlers.ts b/apps/buddy/service/src/productProtocol/createProductCommandHandlers.ts new file mode 100644 index 000000000..a08686b3b --- /dev/null +++ b/apps/buddy/service/src/productProtocol/createProductCommandHandlers.ts @@ -0,0 +1,171 @@ +import type { BuddyResourceQuote } from '../../../shared/conversation/buddyUserContent' +import type { TrustedProductContext } from '../../../shared/productProtocol/identity' +import type { ProductMethod } from '../../../shared/productProtocol/methods' +import type { BuddyServiceCommands } from '../BuddyService' +import type { CommandResolution } from '../commands/CommandAdmissionService' +import type { ProductRequestHandler } from './ProductConnection' +import { conversationArchiveQuerySchema } from '../../../shared/conversation/conversationArchiveApi' +import { assertProductContentActions, productEditableContentSchema, restoreProductContent } from '../../../shared/productProtocol/content' +import { ProductProtocolError } from '../../../shared/productProtocol/errors' +import { productByteLength } from '../../../shared/productProtocol/limits' +import { productCommandIntents } from '../../../shared/productProtocol/methods' +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' +import { assertSameCommand } from '../storage/CommandCommitter' +import { projectCommandReceipt } from './projectCommandReceipt' + +export function createProductCommandHandlers(options: { + commands: BuddyServiceCommands + assertOwner: (context: Readonly) => void + resolveQuote: (resourceId: string, context: Readonly) => BuddyResourceQuote + allowedActions: ReadonlySet + allowLocalEffects?: boolean + assertExecutionAvailable?: () => void + hasReview?: (approvalId: string, version: string) => boolean +}): Partial> { + const { commands } = options + const handlers: Partial> = { + '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 })) + return { ...page, items: page.items.map(({ metadataRevision, ...item }) => ({ ...item, revision: encodeProductRevision(metadataRevision!) })) } + } }, + 'conversations.getArchivePolicy': { handle: (_input, context) => { + options.assertOwner(context) + return commands.metadata.getArchivePolicy() + } }, + 'uploads.get': { handle: (input, context) => { + options.assertOwner(context) + return commands.uploads.get((input as { uploadId: string }).uploadId) + } }, + 'commands.get': { handle: (input, context) => { + options.assertOwner(context) + const lookup = commands.admission.lookup(input) + return lookup.lookup === 'decided' ? { lookup: 'decided', receipt: projectCommandReceipt(lookup.receipt) } : lookup + } }, + } + for (const method of Object.keys(productCommandIntents) as (keyof typeof productCommandIntents)[]) { + handlers[method] = { handle: async (input, context, client) => { + options.assertOwner(context) + const envelope = input as { dataGeneration: string, commandId: string, expectedServiceInstanceId: string, intent: Record } + const previous = commands.admission.lookup({ dataGeneration: envelope.dataGeneration, commandId: envelope.commandId }) + const fingerprint = fingerprintProductCommandIntent(method, envelope.intent) + if (previous.lookup === 'decided' && previous.receipt.fingerprintVersion !== COMMAND_FINGERPRINT_VERSION) { + if (previous.receipt.fingerprintVersion !== PRODUCT_COMMAND_FINGERPRINT_VERSION) + throw new ProductProtocolError('COMMAND_FINGERPRINT_UNSUPPORTED') + try { + assertSameCommand(previous.receipt, { method, fingerprint: fingerprint.fingerprint, fingerprintVersion: fingerprint.version }) + } + catch (error) { + if (error instanceof CommandAdmissionError) + throw new ProductProtocolError(error.code) + throw error + } + return { receipt: projectCommandReceipt(previous.receipt) } + } + const intent = { ...envelope.intent } + for (const key of ['expectedRevision', 'expectedSourceRevision', 'expectedSlotRevision', 'expectedRunGeneration', 'expectedTitleRevision', 'expectedAssignmentRevision', 'readRevision']) { + if (typeof intent[key] === 'string') { + try { + intent[key] = decodeSafeProductRevision(intent[key]) + } + catch { throw new ProductProtocolError('INVALID_PARAMS') } + } + } + if (intent.expectedActiveRun && typeof intent.expectedActiveRun === 'object') { + const active = intent.expectedActiveRun as { runId: string, generation: string, modelSelection: unknown } + try { + intent.expectedActiveRun = { ...active, generation: decodeSafeProductRevision(active.generation) } + } + catch { throw new ProductProtocolError('INVALID_PARAMS') } + } + if (method === 'composerDrafts.save' || method === 'composerDrafts.open') { + if (previous.lookup === 'not_found' && !client.supportedContentKinds.includes('buddy-user-content-v1')) + throw new ProductProtocolError('CONTENT_UNSUPPORTED') + const contentKey = method === 'composerDrafts.open' ? 'initialContent' : 'content' + const content = productEditableContentSchema.parse(intent[contentKey]) + if (previous.lookup === 'not_found') + assertProductContentActions(content, options.allowedActions) + intent[contentKey] = restoreProductContent(content, resourceId => options.resolveQuote(resourceId, context)) + } + if (method === 'composerDrafts.copy' && intent.recoveredInput) { + const recovered = intent.recoveredInput as Record + const content = productEditableContentSchema.parse(recovered.content) + if (previous.lookup === 'not_found') { + if (!client.supportedContentKinds.includes('buddy-user-content-v1')) + throw new ProductProtocolError('CONTENT_UNSUPPORTED') + assertProductContentActions(content, options.allowedActions) + } + intent.recoveredInput = { ...recovered, content: restoreProductContent(content, resourceId => options.resolveQuote(resourceId, context)) } + } + if (method === 'chat.submitInput') { + const snapshot = intent.input as Record + const content = productEditableContentSchema.parse(snapshot.content) + if (previous.lookup === 'not_found') { + if (!client.supportedContentKinds.includes('buddy-user-content-v1')) + throw new ProductProtocolError('CONTENT_UNSUPPORTED') + assertProductContentActions(content, options.allowedActions) + } + intent.input = { ...snapshot, content: restoreProductContent(content, resourceId => options.resolveQuote(resourceId, context)) } + } + if (previous.lookup === 'decided') { + try { + const fingerprint = fingerprintCommandIntent(method, intent) + assertSameCommand(previous.receipt, { method, fingerprint: fingerprint.fingerprint, fingerprintVersion: fingerprint.version }) + } + catch (error) { + if (error instanceof CommandAdmissionError) + throw new ProductProtocolError(error.code) + throw error + } + return { receipt: projectCommandReceipt(previous.receipt) } + } + if (previous.lookup === 'not_found' && ['chat.submitInput', 'chat.startTurn', 'chat.editUserMessage', 'chat.regenerateAssistant', 'chat.executeCommand', 'chat.queue.enqueue', 'chat.queue.continue', 'chat.queue.steer'].includes(method)) + options.assertExecutionAvailable?.() + if (previous.lookup === 'not_found' && method === 'approvals.resolve' && intent.decision !== 'denied') { + const approval = commands.approvals.getReview(String(intent.approvalId)) + if (!approval || !approval.review?.complete || !client.supportedReviewKinds.includes(approval.kind) || (!options.allowLocalEffects && productByteLength(JSON.stringify(approval.review.body, null, 2)) > 131_072 && !options.hasReview?.(String(intent.approvalId), approval.review.reviewVersion))) + throw new ProductProtocolError('CONTENT_UNSUPPORTED') + if (approval.review.persistentEffect && !options.allowLocalEffects) + throw new ProductProtocolError('NOT_AUTHORIZED') + } + const submission = { ...envelope, intent, [productCommandFingerprint]: fingerprint } + let result: CommandResolution + try { + switch (method) { + case 'desktop.open': + result = await commands.desktop.execute(submission) + break + case 'chat.submitInput': case 'chat.startTurn': case 'chat.editUserMessage': case 'chat.regenerateAssistant': case 'chat.executeCommand': case 'chat.queue.enqueue': + result = await commands.input.execute(method, submission) + break + case 'chat.queue.continue': case 'chat.queue.cancel': case 'chat.queue.steer': case 'chat.cancel': + result = await commands.control.execute(method, submission) + break + case 'composerDrafts.open': case 'composerDrafts.save': case 'composerDrafts.discard': case 'composerDrafts.copy': + result = await commands.drafts.execute(method, submission) + break + case 'uploads.stage': case 'uploads.create': case 'uploads.complete': case 'uploads.cancel': case 'uploads.adopt': + result = await commands.uploads.execute(method, submission) + break + case 'questions.answer': case 'questions.cancel': case 'questions.retry': + result = await commands.questions.execute(method, submission) + break + case 'approvals.resolve': + result = await commands.approvals.resolveCommand(submission) + break + default: + result = await commands.metadata.execute(method, submission) + } + } + catch (error) { + if (error instanceof CommandAdmissionError) + throw new ProductProtocolError(error.code) + throw error + } + return { receipt: projectCommandReceipt(result.receipt) } + } } + } + return handlers +} diff --git a/apps/buddy/service/src/productProtocol/createProductModelQueries.ts b/apps/buddy/service/src/productProtocol/createProductModelQueries.ts new file mode 100644 index 000000000..e9a143b10 --- /dev/null +++ b/apps/buddy/service/src/productProtocol/createProductModelQueries.ts @@ -0,0 +1,55 @@ +import type { ZodType } from 'zod' +import type { LocalConversation } from '../../../shared/conversation/conversationApi' +import type { ProductMethod } from '../../../shared/productProtocol/methods' +import type { ProviderService } from '../providers/ProviderService' +import type { ProductRequestHandler } from './ProductConnection' +import { createHash } from 'node:crypto' +import { ProductProtocolError } from '../../../shared/productProtocol/errors' +import { PRODUCT_LIMITS, productByteLength } from '../../../shared/productProtocol/limits' +import { decodeProductCursor, encodeProductCursor, encodeProductRevision } from '../../../shared/productProtocol/scalars' +import { productModelSchema, productProviderSchema } from '../../../shared/productProtocol/views' + +export function createProductModelQueries(providers: ProviderService, inputSelection: (conversationId?: string) => LocalConversation['modelSelection']): Partial> { + const paginate = (scope: string, input: unknown, items: unknown[], schema: ZodType) => { + const request = input as { cursor: string | null, limit: number } + const publicItems = items.map(item => schema.parse(item)) + const hash = createHash('sha256').update(JSON.stringify(publicItems)).digest('hex') + const cursorScope = createHash('sha256').update(JSON.stringify([scope, hash])).digest('hex') + let position = 0 + if (request.cursor) { + try { + position = Number(decodeProductCursor(request.cursor, { scope: cursorScope, branchId: null, revision: '0' }).position) + } + catch { throw new ProductProtocolError('CURSOR_EXPIRED') } + } + const page: unknown[] = [] + for (const item of publicItems.slice(position, position + request.limit)) { + if (productByteLength(JSON.stringify([...page, item])) > PRODUCT_LIMITS.pageBytes - 4096) + break + page.push(item) + } + if (position < publicItems.length && !page.length) + throw new ProductProtocolError('PAGE_TOO_LARGE') + position += page.length + return { items: page, nextCursor: position < publicItems.length ? encodeProductCursor({ version: 1, scope: cursorScope, branchId: null, revision: '0', upperBound: encodeProductRevision(publicItems.length), position: encodeProductRevision(position) }) : null } + } + return { + 'providers.getInputSelection': { handle: async (input) => { + const selection = inputSelection((input as { conversationId?: string }).conversationId) ?? await providers.getDefaultModel() + return { modelSelection: selection ? { ...selection, serviceTier: 'serviceTier' in selection ? selection.serviceTier : null } : null } + } }, + 'providers.list': { handle: async input => paginate('providers', input, (await providers.listProviders()).map(provider => Object.fromEntries(Object.keys(productProviderSchema.shape).map(key => [key, provider[key as keyof typeof provider]]))), productProviderSchema) }, + 'providers.listModels': { handle: async (input) => { + const providerId = (input as { providerId?: string }).providerId + const models = await providers.listModels(providerId) + return paginate(`models:${providerId ?? ''}`, input, models.map(model => Object.fromEntries(Object.keys(productModelSchema.shape).map((key) => { + if (key === 'modelId') + return [key, model.id] + if (key === 'serviceTiers') + return [key, model.type === 'chat' ? providers.executionModels.getServiceTiers({ api: model.api, modelId: model.id, providerId: model.providerId }) : []] + return [key, model[key as keyof typeof model]] + }))), productModelSchema) + } }, + 'providers.getModelSnapshot': { handle: () => providers.getModelSnapshot() }, + } +} diff --git a/apps/buddy/service/src/productProtocol/createProductSynchronization.ts b/apps/buddy/service/src/productProtocol/createProductSynchronization.ts new file mode 100644 index 000000000..5c36aa73d --- /dev/null +++ b/apps/buddy/service/src/productProtocol/createProductSynchronization.ts @@ -0,0 +1,197 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { ChatQueueService } from '../chat/ChatQueueService' +import type { ComposerDraftService } from '../chat/ComposerDraftService' +import type { ConversationArchiveService } from '../conversations/ConversationArchiveService' +import type { ConversationLifecycleService } from '../conversations/ConversationLifecycleService' +import type { ConversationMetadataService } from '../conversations/ConversationMetadataService' +import type { TaskMarkService } from '../conversations/TaskMarkService' +import type { RunEventLogPort } from '../events/RunEventPorts' +import type { UserQuestionService } from '../questions/UserQuestionService' +import type { ProductQueries } from './ProductQueries' +import { ProductProtocolError } from '../../../shared/productProtocol/errors' +import { PRODUCT_LIMITS } from '../../../shared/productProtocol/limits' +import { encodeProductRevision } from '../../../shared/productProtocol/scalars' +import { formatProductTopic, parseProductTopic, PRODUCT_TOPIC_SCHEMAS } from '../../../shared/productProtocol/topics' +import { createConversationRepository } from '../storage/conversationRepository' +import { ProductSyncPublisher } from './ProductSyncPublisher' + +export function createProductSynchronization(options: { + database: DatabaseSync + queries: ProductQueries + drafts: Pick + metadata: Pick + lifecycle: Pick + archive?: Pick + marks?: Pick + questions?: Pick + events: Pick + queue?: Pick +}) { + const publisher = new ProductSyncPublisher() + const { queries, database } = options + const dynamic = new Map void>() + function register(name: string) { + const topic = parseProductTopic(name) + const schemas = PRODUCT_TOPIC_SCHEMAS[topic.kind] + return publisher.register(name, { ...schemas, openSnapshot: () => { + const reader = queries.openSnapshot(name) + return { close: reader.close, read: (cursor) => { + const page = reader.read(cursor) + return { ...page, items: page.items.map(item => schemas.item.parse(item)) } + } } + }, readSnapshot: () => { + const page = queries.snapshot(name) + return { ...page, items: page.items.map(item => schemas.item.parse(item)) } + } }) + } + for (const name of ['taskIndex', 'draftIndex', 'approvalInbox', 'serviceState']) register(name) + function authorizeTopic(name: string) { + let topic + try { + topic = parseProductTopic(name) + } + catch { throw new ProductProtocolError('NOT_AUTHORIZED') } + queries.assertTopic(topic) + if (publisher.hasTopic(name)) + return + if (dynamic.size >= PRODUCT_LIMITS.dynamicTopics) { + const idle = [...dynamic].find(([key]) => publisher.subscriptionCount(key) === 0) + if (!idle) + throw new ProductProtocolError('SYNC_INVALID') + idle[1]() + dynamic.delete(idle[0]) + } + dynamic.set(name, register(name)) + } + const publish = (name: string, changes: () => unknown[]) => { + if (!publisher.hasTopic(name)) + return + publisher.publishCommitted(name, () => { + const schema = PRODUCT_TOPIC_SCHEMAS[parseProductTopic(name).kind].change + return changes().map(change => schema.parse(change)) + }) + } + const conversationTopics = (conversationId: string) => [...dynamic.keys()].filter((name) => { + const topic = parseProductTopic(name) + return topic.kind === 'conversation' && topic.conversationId === conversationId + }) + const publishTask = (id: string) => { + publish('taskIndex', () => { + const value = queries.task(id, true) + return [value ? { kind: 'upsert', value } : { kind: 'remove', id }] + }) + for (const name of conversationTopics(id)) { + publish(name, () => { + const value = queries.task(id) + return [value ? { kind: 'upsert', item: { entity: 'task', value } } : { kind: 'remove', entity: 'task', id }] + }) + } + } + const publishVisibility = (conversationId: string) => { + publishTask(conversationId) + for (const name of conversationTopics(conversationId)) publisher.invalidate(name) + for (const name of dynamic.keys()) { + const topic = parseProductTopic(name) + if (topic.kind === 'questions' && topic.conversationId === conversationId) + publisher.invalidate(name) + } + publisher.invalidate('approvalInbox') + } + const subscriptions = [ + options.drafts.onDidCommit(({ draftId }) => { + const changes = () => { + const value = queries.draft(draftId) + return [value ? { kind: 'upsert', value } : { kind: 'remove', id: draftId }] + } + publish('draftIndex', changes) + publish(formatProductTopic({ kind: 'draft', draftId }), changes) + }), + options.metadata.onDidCommit(({ conversation }) => publishTask(conversation.id)), + options.lifecycle.onDidCommit(({ conversationId }) => publishVisibility(conversationId)), + options.events.onDidCommit(({ runId, type, sequence, payload, createdAt }) => { + const row = database.prepare('SELECT conversation_id, branch_id FROM runs WHERE id = ?').get(runId) + if (!row) + return + const conversationId = String(row.conversation_id) + 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, () => { + 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') { + const value = queries.streaming.start(runId, payload.messageId, createdAt) + if (value) + changes.push({ kind: 'upsert', item: { entity: 'message', value } }) + } + if (type.startsWith('run.')) { + const value = queries.read('runs.get', { runId }) + if (value) + changes.push({ kind: 'upsert', item: { entity: 'run', value } }) + } + if (['message.created', 'message.completed', 'message.block.completed', 'run.completed', 'run.failed', 'run.cancelled'].includes(type)) { + const rows = database.prepare('SELECT id FROM messages WHERE run_id = ? LIMIT ?').all(runId, PRODUCT_LIMITS.subscriptionItems + 1) + if (rows.length > PRODUCT_LIMITS.subscriptionItems) + throw new ProductProtocolError('RESYNC_REQUIRED') + const conversations = createConversationRepository(database) + for (const row of rows) { + const message = conversations.findMessageById(String(row.id)) + if (message) + changes.push({ kind: 'upsert', item: { entity: 'message', value: queries.views.message(message) } }) + } + } + if (type.startsWith('approval.')) { + for (const row of database.prepare('SELECT id FROM approvals WHERE run_id = ? LIMIT ?').all(runId, PRODUCT_LIMITS.subscriptionItems + 1)) { + const value = queries.views.approval(String(row.id)) + if (value) + changes.push({ kind: 'upsert', item: { entity: 'approval', value } }) + } + } + return changes + }) + if (type.startsWith('approval.')) { + publish('approvalInbox', () => database.prepare('SELECT id FROM approvals WHERE run_id = ? LIMIT ?').all(runId, PRODUCT_LIMITS.subscriptionItems + 1).map((row) => { + const id = String(row.id) + const value = queries.views.approval(id) + return value?.status === 'pending' ? { kind: 'upsert', value } : { kind: 'remove', id } + })) + } + }), + ] + if (options.archive) + subscriptions.push(options.archive.onDidChange(publishVisibility)) + if (options.questions) { + subscriptions.push(options.questions.onDidCommit(({ conversationId, branchId, requestId }) => { + publishTask(conversationId) + publish(formatProductTopic({ kind: 'questions', conversationId, branchId }), () => { + const value = queries.views.question(requestId) + return [value ? { kind: 'upsert', value } : { kind: 'remove', id: requestId }] + }) + })) + } + if (options.marks) { + subscriptions.push(options.marks.onDidCommit((event) => { + if (event.kind === 'attention') + publishTask(event.conversationId) + else if (event.kind === 'deleted') + event.conversationIds.forEach(publishTask) + })) + } + 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) + return + } + publish(name, () => { + const value = queries.views.queue(committed.queueId) + return value && ['waiting', 'paused'].includes(value.state) + ? [{ kind: 'upsert', item: { entity: 'queue', value } }] + : [{ kind: 'remove', entity: 'queue', id: committed.queueId }] + }) + })) + } + return { publisher, authorizeTopic, stopping: () => publish('serviceState', () => [{ phase: 'stopping' }]), dispose: () => subscriptions.forEach(subscription => subscription.dispose()) } +} diff --git a/apps/buddy/service/src/productProtocol/projectCommandReceipt.ts b/apps/buddy/service/src/productProtocol/projectCommandReceipt.ts new file mode 100644 index 000000000..7df94bf5c --- /dev/null +++ b/apps/buddy/service/src/productProtocol/projectCommandReceipt.ts @@ -0,0 +1,32 @@ +import type { CommandReceiptSnapshot } from '../storage/CommandCommitter' +import { productCommandReceiptSchema, productReceiptResultSchema, productReceiptTargetSchema } from '../../../shared/productProtocol/receipts' +import { encodeProductRevision } from '../../../shared/productProtocol/scalars' + +export function projectCommandReceipt(receipt: CommandReceiptSnapshot) { + const target = Object.fromEntries(Object.keys(productReceiptTargetSchema.shape).filter(key => receipt.target[key] !== undefined).map(key => [key, receipt.target[key]])) + const result: Record = {} + for (const [key, valueSchema] of Object.entries(productReceiptResultSchema.shape)) { + let value: unknown = receipt.result[key] + if (value === undefined || (key === 'draftReceipt' && value === null)) + continue + if (['revision', 'slotRevision', 'sourceRevision', 'titleRevision', 'assignmentRevision', 'readRevision'].includes(key)) + value = encodeProductRevision(value as number) + if (key === 'draftReceipt' && value && typeof value === 'object') { + const draft = value as { draftId: string, committedRevision: number } + value = { draftId: draft.draftId, committedRevision: encodeProductRevision(draft.committedRevision) } + } + if (key === 'reason' && !valueSchema.safeParse(value).success) + continue + result[key] = value + } + return productCommandReceiptSchema.parse({ + dataGeneration: receipt.dataGeneration, + commandId: receipt.commandId, + method: receipt.method, + decision: receipt.decision, + decidedAt: receipt.decidedAt, + target, + result, + ...(receipt.decision === 'rejected' ? { errorCode: receipt.errorCode } : {}), + }) +} diff --git a/apps/buddy/service/src/productProtocol/registerLocalProductConnection.ts b/apps/buddy/service/src/productProtocol/registerLocalProductConnection.ts new file mode 100644 index 000000000..97e607c87 --- /dev/null +++ b/apps/buddy/service/src/productProtocol/registerLocalProductConnection.ts @@ -0,0 +1,26 @@ +import type { ServiceClientPeer } from '../rpc/ServiceClientConnections' +import type { ProductConnection } from './ProductConnection' +import { productExchange, productFrameNotification } from '../../../shared/productProtocol/transport' +import { registerRuntimeRequest } from '../rpc/runtimeRequest' + +export function registerLocalProductConnection(peer: ServiceClientPeer, create: (transport: { send: (frame: string) => boolean, close: () => void }) => ProductConnection): () => void { + const connection = create({ + send(frame) { + if (peer.closed) + return false + peer.notify(productFrameNotification.method, { frame }) + return true + }, + close: () => peer.close(new Error('PRODUCT_CONNECTION_CLOSED')), + }) + const unregister = registerRuntimeRequest(peer, productExchange, async ({ frame }) => { + await connection.receive(frame) + return { received: true as const } + }) + const closed = peer.onDidClose(() => connection.close()) + return () => { + unregister() + closed.dispose() + connection.close() + } +} diff --git a/apps/buddy/service/src/providers/HostCredentialStore.ts b/apps/buddy/service/src/providers/HostCredentialStore.ts index af6c87936..a089858fd 100644 --- a/apps/buddy/service/src/providers/HostCredentialStore.ts +++ b/apps/buddy/service/src/providers/HostCredentialStore.ts @@ -9,6 +9,7 @@ import { randomUUID } from 'node:crypto' import { Emitter } from '../../../shared/events/Emitter' import { copyEventSnapshot } from '../../../shared/events/eventSnapshot' import { pluginProviderPrefix } from '../../../shared/extensions/extensionChat' +import { AsyncOperationScope } from '../../../shared/lifecycle/AsyncOperationScope' import { credentialMutationResultSchema, credentialProviderListResultSchema, @@ -38,9 +39,8 @@ export type CredentialChange = { readonly kind: 'observation', readonly previous | { readonly kind: 'store-availability', readonly revision: number, readonly status: 'known' | 'unknown' } export class HostCredentialStore implements CredentialStore { - readonly #pending = new Set>() - #stopping = false - readonly #peer: RuntimeRpcPeerContract + readonly #operations = new AsyncOperationScope(() => new HostCredentialStoreError('CREDENTIAL_STORE_UNAVAILABLE')) + readonly #peer: Omit readonly #providerLocks = new Map>() readonly #observations = new Map() readonly #observedRequests = new Map() @@ -55,12 +55,12 @@ export class HostCredentialStore implements CredentialStore { return copyEventSnapshot({ revision: this.#revision, availability: this.#availability, providers: [...this.#observations.values()] }) } - constructor(peer: RuntimeRpcPeerContract) { + constructor(peer: Omit) { this.#peer = peer } read(providerId: string, options?: AuthOperationOptions): Promise { - return this.#operate(() => this.#read(providerId, options)) + return this.#operations.track(() => this.#read(providerId, options)) } async #read(providerId: string, options?: AuthOperationOptions): Promise { @@ -84,7 +84,7 @@ export class HostCredentialStore implements CredentialStore { } list(options?: AuthOperationOptions): Promise { - return this.#operate(() => this.#list(options)) + return this.#operations.track(() => this.#list(options)) } async #list(options?: AuthOperationOptions): Promise { @@ -154,25 +154,11 @@ export class HostCredentialStore implements CredentialStore { } async dispose(): Promise { - this.#stopping = true - while (this.#pending.size) - await Promise.allSettled([...this.#pending]) + this.#operations.stopAccepting() + await this.#operations.drain() this.#changes.dispose() } - #operate(operation: () => Promise): Promise { - if (this.#stopping) - return Promise.reject(new HostCredentialStoreError('CREDENTIAL_STORE_UNAVAILABLE')) - const pending = Promise.withResolvers() - this.#pending.add(pending.promise) - try { - void operation().then(pending.resolve, pending.reject) - } - catch (error) { pending.reject(error) } - void pending.promise.then(() => this.#pending.delete(pending.promise), () => this.#pending.delete(pending.promise)) - return pending.promise - } - #observe(providerId: string, type: 'api_key' | 'oauth' | 'unknown' | null, request: number): void { if (request < (this.#observedRequests.get(providerId) ?? 0)) return @@ -198,10 +184,10 @@ export class HostCredentialStore implements CredentialStore { } #withProviderLock(providerId: string, operation: () => Promise): Promise { - if (this.#stopping) + if (!this.#operations.isAccepting) return Promise.reject(new HostCredentialStoreError('CREDENTIAL_STORE_UNAVAILABLE')) const previous = this.#providerLocks.get(providerId) ?? Promise.resolve() - const result = this.#operate(() => previous.catch(() => {}).then(operation)) + const result = this.#operations.track(() => previous.catch(() => {}).then(operation)) const tail = result.then(() => {}, () => {}) this.#providerLocks.set(providerId, tail) void tail.then(() => { diff --git a/apps/buddy/service/src/providers/ProviderModelCatalog.ts b/apps/buddy/service/src/providers/ProviderModelCatalog.ts index 8ac7df7b5..09c870b5d 100644 --- a/apps/buddy/service/src/providers/ProviderModelCatalog.ts +++ b/apps/buddy/service/src/providers/ProviderModelCatalog.ts @@ -15,12 +15,12 @@ import type { ProviderModelStateRepository, } from '../storage/providerModelStateRepository' import type { InputModel } from './modelCapabilities' -import type { CatalogModel, ModelMetadataCatalog } from './ModelsDevCatalog' +import type { ModelMetadataCatalog } from './ModelsDevCatalog' import type { ProviderModelDefinition, ProviderModelDiscoveryResult } from './ProviderModelDiscovery' +import type { ModelReconciliationPolicy, ObservedProviderModel } from './providerModelReconciliation' +import type { ModelSourceMetadata } from './providerModelSource' import type { BuddyModel, ModelParametersOverride } from './providerSchemas' -import { createHash } from 'node:crypto' import { getSupportedThinkingLevels } from '@earendil-works/pi-ai' -import { BUDDY_THINKING_LEVELS } from '../../../shared/conversation/modelSelection' import { pluginChatApi } from '../../../shared/extensions/extensionChat' import { supportsPdfInputApi } from '../../../shared/providers/pdfInput' import { modelCapabilityOverridesSchema } from '../../../shared/providers/providerCapabilities' @@ -33,6 +33,8 @@ import { serializeModelMetadata } from './modelMetadata' import { MODEL_RESOLUTION_VERSION, modelResolutionFingerprint } from './modelResolution' import { normalizeProviderBaseUrl, ProviderCatalogMatcher } from './ProviderCatalogMatcher' import { ProviderUnavailableError, ProviderValidationError } from './ProviderFailure' +import { planModelReconciliation } from './providerModelReconciliation' +import { createSourceMetadata, emptyModelCost, nextSourceRevision, sourceMetadataFromCatalogModel, sourceParametersChanged } from './providerModelSource' import { buddyModelSchema, modelParametersOverrideSchema } from './providerSchemas' export interface ProviderRegistration { @@ -85,10 +87,7 @@ export class ProviderModelCatalog { this.#configs = options.configs this.#catalogMatcher = new ProviderCatalogMatcher(options.metadata) this.#modelRuntime = options.modelRuntime - this.#models = { - ...options.models, - upsert: record => options.models.upsert({ ...record, resolution: effectiveType(record) === 'chat' ? this.#resolveSource(record) : null }), - } + this.#models = options.models this.#supportsOperation = options.supportsOperation ?? (() => true) } @@ -102,7 +101,7 @@ export class ProviderModelCatalog { continue const resolution = this.#resolveSource(model) if (resolution !== model.resolution) - this.#models.upsert({ ...model, resolution }) + this.#saveModel({ ...model, resolution }) } } @@ -184,7 +183,7 @@ export class ProviderModelCatalog { sourceMaxTokens: model.maxTokens, thinkingLevelMap: model.thinkingLevelMap ?? null, }) - this.#models.upsert({ + this.#saveModel({ catalogSelection: model.catalogSelection ?? null, capabilityOverrides: null, acknowledgedSourceRevision: null, @@ -211,43 +210,18 @@ export class ProviderModelCatalog { } reconcilePluginModels(providerId: string, models: readonly Model[]): void { - const now = new Date().toISOString() - for (const model of models) { - const current = this.#models.find(providerId, model.id) - const source = createSourceMetadata({ api: model.api, catalogProviderId: null, catalogModelId: null, compat: null, cost: model.cost, displayName: model.name, input: [...model.input], reasoning: model.reasoning, samplingParams: null, sourceContextWindow: model.contextWindow, sourceMaxTokens: model.maxTokens, thinkingLevelMap: model.thinkingLevelMap ?? null }) - this.#models.upsert({ - catalogSelection: null, - capabilityOverrides: current?.capabilityOverrides ?? null, - acknowledgedSourceRevision: current?.acknowledgedSourceRevision ?? null, - available: true, - createdAt: current?.createdAt ?? now, - enabled: current?.enabled ?? true, - lastSeenAt: now, - modelId: model.id, - providerId, - overrideContextWindow: null, - overrideMaxTokens: null, - ...source, - source: 'synced', - sourceRevision: sourceParametersChanged(current, source) ? nextSourceRevision(current?.sourceRevision, now) : current?.sourceRevision ?? now, - updatedAt: now, - }) - } - for (const current of this.#models.list(providerId)) { - if (effectiveType(current) === 'chat' && !models.some(model => model.id === current.modelId)) - this.#models.upsert({ ...current, available: false, updatedAt: now }) - } + const observations = models.map(model => ({ + providerId, + modelId: model.id, + metadata: createSourceMetadata({ api: model.api, catalogProviderId: null, catalogModelId: null, compat: null, cost: model.cost, displayName: model.name, input: [...model.input], reasoning: model.reasoning, samplingParams: null, sourceContextWindow: model.contextWindow, sourceMaxTokens: model.maxTokens, thinkingLevelMap: model.thinkingLevelMap ?? null }), + })) + this.#reconcileModels(providerId, observations, 'plugin-chat') } reconcileBuiltinModels(providerId: string, builtinProviderId = providerId, options: { models?: readonly Model[], metadataOnly?: boolean, preserveUnchanged?: boolean } = {}): void { - const now = new Date().toISOString() const runtimeModels = options.models ?? this.#modelRuntime.getModels(providerId) - const seen = new Set(runtimeModels.map(model => model.id)) const channel = Boolean(this.#modelRuntime.getProviders().find(provider => provider.id === providerId)?.auth.oauth) - for (const model of runtimeModels) { - const current = this.#models.find(providerId, model.id) - if (current?.source === 'manual' || (options.metadataOnly && !current)) - continue + const observations = runtimeModels.map((model) => { const match = this.#catalogMatcher.match({ api: model.api, baseUrl: model.baseUrl, modelId: model.id, providerId: builtinProviderId }) const metadata = match.status === 'matched' ? match.model : null const source = createSourceMetadata({ @@ -264,48 +238,14 @@ export class ProviderModelCatalog { sourceMaxTokens: channel ? model.maxTokens : metadata?.maxTokens ?? model.maxTokens, thinkingLevelMap: channel ? model.thinkingLevelMap ?? null : metadata?.thinkingLevelMap ?? model.thinkingLevelMap ?? null, }) - this.#upsertReconciled({ - catalogSelection: null, - typeOverride: current?.typeOverride ?? null, - capabilityOverrides: current?.capabilityOverrides ?? null, - acknowledgedSourceRevision: current?.acknowledgedSourceRevision ?? null, - available: options.metadataOnly ? current!.available : true, - createdAt: current?.createdAt ?? now, - enabled: current?.enabled ?? false, - lastSeenAt: options.metadataOnly ? current!.lastSeenAt : now, - modelId: model.id, - overrideContextWindow: current?.overrideContextWindow ?? null, - overrideMaxTokens: current?.overrideMaxTokens ?? null, - providerId, - ...source, - source: 'builtin', - sourceRevision: sourceParametersChanged(current, source) - ? nextSourceRevision(current?.sourceRevision, now) - : current?.sourceRevision ?? now, - updatedAt: now, - }, current, options.preserveUnchanged) - } - if (options.metadataOnly) - return - for (const current of this.#models.list(providerId)) { - if (current.source === 'manual' || effectiveType(current) !== 'chat' || seen.has(current.modelId)) - continue - if (current.available) - this.#models.upsert({ ...current, available: false, updatedAt: now }) - } + return { providerId, modelId: model.id, metadata: source } + }) + this.#reconcileModels(providerId, observations, options.metadataOnly ? 'builtin-metadata' : 'builtin', options.preserveUnchanged) } - reconcileSyncedModels( - provider: ProviderConfigRecord, - result: ProviderModelDiscoveryResult, - ): void { - const definitions = result.models - const now = new Date().toISOString() - const seen = new Set(definitions.map(model => model.id)) - for (const definition of definitions) { + reconcileSyncedModels(provider: ProviderConfigRecord, result: ProviderModelDiscoveryResult): void { + const observations = result.models.map((definition) => { const current = this.#models.find(provider.id, definition.id) - if (current?.source === 'manual') - continue const serviceMetadata = definition.metadata ? serviceModelMetadataSchema.parse({ api: provider.api, @@ -313,35 +253,13 @@ export class ProviderModelCatalog { metadata: definition.metadata, }) : null - const source = this.#syncedSourceMetadata(provider, definition, current, serviceMetadata) - this.#models.upsert({ - catalogSelection: current?.catalogSelection ?? null, - typeOverride: current?.typeOverride ?? null, - capabilityOverrides: current?.capabilityOverrides ?? null, - acknowledgedSourceRevision: current?.acknowledgedSourceRevision ?? null, - available: true, - createdAt: current?.createdAt ?? now, - enabled: current?.enabled ?? false, - lastSeenAt: now, - modelId: definition.id, - overrideContextWindow: current?.overrideContextWindow ?? null, - overrideMaxTokens: current?.overrideMaxTokens ?? null, + return { providerId: provider.id, - ...source, - source: 'synced', - sourceRevision: sourceParametersChanged(current, source) - ? nextSourceRevision(current?.sourceRevision, now) - : current?.sourceRevision ?? now, - updatedAt: now, - }) - } - if (!result.complete) - return - for (const current of this.#models.list(provider.id)) { - if (current.source !== 'synced' || seen.has(current.modelId)) - continue - this.#models.upsert({ ...current, available: false, updatedAt: now }) - } + modelId: definition.id, + metadata: this.#syncedSourceMetadata(provider, definition, current, serviceMetadata), + } + }) + this.#reconcileModels(provider.id, observations, result.complete ? 'synced-complete' : 'synced-partial') } reconcileSyncedModelMetadata(provider: ProviderConfigRecord): boolean { @@ -356,7 +274,7 @@ export class ProviderModelCatalog { }, current) if (current.sourceFingerprint === source.sourceFingerprint) continue - this.#models.upsert({ + this.#saveModel({ ...current, ...source, sourceRevision: sourceParametersChanged(current, source) @@ -387,7 +305,7 @@ export class ProviderModelCatalog { sourceMaxTokens: model.maxTokens, thinkingLevelMap: null, }) - const next = this.#models.upsert({ + const next = this.#saveModel({ catalogSelection: null, capabilityOverrides: current?.capabilityOverrides ?? null, acknowledgedSourceRevision: current?.acknowledgedSourceRevision ?? null, @@ -417,7 +335,7 @@ export class ProviderModelCatalog { if (!providerModelTypeSchema.nullable().safeParse(type).success) throw new ProviderValidationError() const nextType = type ?? current.type ?? 'chat' - const next = this.#models.upsert({ + const next = this.#saveModel({ ...current, typeOverride: type, sourceContextWindow: nextType === 'chat' ? current.sourceContextWindow ?? 128_000 : current.sourceContextWindow, @@ -429,57 +347,30 @@ export class ProviderModelCatalog { reconcileCatalogModels(providerId: string, baseUrl: string, catalogProviderId: string, preserveUnchanged = false): void { const entries = this.#metadata.getMediaModels?.(catalogProviderId) ?? [] - const seen = new Set() - const now = new Date().toISOString() + const observations: ObservedProviderModel[] = [] for (const entry of entries) { if (!entry.type || entry.type === 'chat' || entry.api !== 'openai-audio' || normalizeProviderBaseUrl(entry.baseUrl ?? '') !== normalizeProviderBaseUrl(baseUrl)) continue - seen.add(entry.id) const current = this.#models.find(providerId, entry.id) - if (current?.source === 'manual') - continue if (!current && !this.#supportsOperation(providerId, entry.type)) continue - const source = createSourceMetadata({ type: entry.type, api: 'openai-audio', catalogProviderId, catalogModelId: entry.id, compat: null, cost: emptyModelCost(), displayName: entry.name, input: ['text'], reasoning: false, samplingParams: null, sourceContextWindow: null, sourceMaxTokens: null, thinkingLevelMap: null }) - this.#upsertReconciled({ - ...source, + observations.push({ providerId, modelId: entry.id, - typeOverride: current?.typeOverride ?? null, - sourceContextWindow: current?.typeOverride === 'chat' ? current.sourceContextWindow : null, - sourceMaxTokens: current?.typeOverride === 'chat' ? current.sourceMaxTokens : null, - catalogSelection: null, - capabilityOverrides: current?.capabilityOverrides ?? null, - acknowledgedSourceRevision: current?.acknowledgedSourceRevision ?? null, - available: true, - enabled: current?.enabled ?? false, - source: 'builtin', - overrideContextWindow: current?.overrideContextWindow ?? null, - overrideMaxTokens: current?.overrideMaxTokens ?? null, - createdAt: current?.createdAt ?? now, - updatedAt: now, - lastSeenAt: now, - sourceRevision: current?.sourceRevision ?? now, - }, current, preserveUnchanged) - } - for (const current of this.#models.list(providerId)) { - if (current.source === 'builtin' && current.api === 'openai-audio' && current.available && !seen.has(current.modelId)) - this.#models.upsert({ ...current, available: false, updatedAt: now }) + metadata: createSourceMetadata({ type: entry.type, api: 'openai-audio', catalogProviderId, catalogModelId: entry.id, compat: null, cost: emptyModelCost(), displayName: entry.name, input: ['text'], reasoning: false, samplingParams: null, sourceContextWindow: null, sourceMaxTokens: null, thinkingLevelMap: null }), + }) } + this.#reconcileModels(providerId, observations, 'catalog-media', preserveUnchanged) } reconcileCapabilityModels(models: readonly import('../../../shared/providers/providerCapabilityModel').ProviderCapabilityModel[]): void { - const now = new Date().toISOString() - const catalogModels = models.filter(model => model.kind !== 'image.read') - for (const model of catalogModels) { - const current = this.#models.find(model.providerId, model.modelId) - const source = createSourceMetadata({ type: model.kind === 'speech.transcribe' ? 'transcription' : model.kind === 'speech.synthesize' ? 'speech' : 'image-generation', api: model.kind.startsWith('image.') ? 'plugin-image' : 'plugin-speech', catalogProviderId: null, compat: null, cost: emptyModelCost(), displayName: model.title ?? model.modelId, input: ['text'], reasoning: false, samplingParams: null, sourceContextWindow: null, sourceMaxTokens: null, thinkingLevelMap: null }) - this.#models.upsert({ ...source, providerId: model.providerId, modelId: model.modelId, typeOverride: null, catalogSelection: null, capabilityOverrides: current?.capabilityOverrides ?? null, acknowledgedSourceRevision: null, available: model.available, enabled: current?.enabled ?? true, source: 'synced', overrideContextWindow: null, overrideMaxTokens: null, createdAt: current?.createdAt ?? now, updatedAt: now, lastSeenAt: now, sourceRevision: current?.sourceRevision ?? now }) - } - for (const current of this.#models.list()) { - if (['plugin-speech', 'plugin-image'].includes(current.api) && !catalogModels.some(model => model.providerId === current.providerId && model.modelId === current.modelId)) - this.#models.upsert({ ...current, available: false, updatedAt: now }) - } + const observations = models.filter(model => model.kind !== 'image.read').map(model => ({ + providerId: model.providerId, + modelId: model.modelId, + available: model.available, + metadata: createSourceMetadata({ type: model.kind === 'speech.transcribe' ? 'transcription' : model.kind === 'speech.synthesize' ? 'speech' : 'image-generation', api: model.kind.startsWith('image.') ? 'plugin-image' : 'plugin-speech', catalogProviderId: null, compat: null, cost: emptyModelCost(), displayName: model.title ?? model.modelId, input: ['text'], reasoning: false, samplingParams: null, sourceContextWindow: null, sourceMaxTokens: null, thinkingLevelMap: null }), + })) + this.#reconcileModels(undefined, observations, 'plugin-capability') } setCatalogSource(provider: ProviderConfigRecord, modelId: string, selection: ModelCatalogReference | null): BuddyModel { @@ -499,7 +390,7 @@ export class ProviderModelCatalog { catalogSelection: selection, }) const now = new Date().toISOString() - const next = this.#models.upsert({ + const next = this.#saveModel({ ...current, ...source, catalogSelection: selection, @@ -518,7 +409,7 @@ export class ProviderModelCatalog { const current = this.#requireModelState(providerId, modelId) if (effectiveType(current) !== 'chat') throw new ProviderValidationError() - return this.#toBuddyModel(this.#models.upsert({ + return this.#toBuddyModel(this.#saveModel({ ...current, capabilityOverrides: parsed.data && Object.values(parsed.data).some(value => value !== undefined) ? parsed.data : null, updatedAt: new Date().toISOString(), @@ -537,7 +428,7 @@ export class ProviderModelCatalog { if (current.api === pluginChatApi || effectiveType(current) !== 'chat') throw new ProviderValidationError() const hadOverride = hasParameterOverride(current) - const next = this.#models.upsert({ + const next = this.#saveModel({ ...current, acknowledgedSourceRevision: hadOverride ? current.acknowledgedSourceRevision @@ -553,7 +444,7 @@ export class ProviderModelCatalog { const current = this.#requireModelState(providerId, modelId) if (!hasParameterOverride(current)) throw new ProviderValidationError() - return this.#toBuddyModel(this.#models.upsert({ + return this.#toBuddyModel(this.#saveModel({ ...current, acknowledgedSourceRevision: current.sourceRevision, updatedAt: new Date().toISOString(), @@ -562,7 +453,7 @@ export class ProviderModelCatalog { restoreSourceParameters(providerId: string, modelId: string): BuddyModel { const current = this.#requireModelState(providerId, modelId) - return this.#toBuddyModel(this.#models.upsert({ + return this.#toBuddyModel(this.#saveModel({ ...current, acknowledgedSourceRevision: null, overrideContextWindow: null, @@ -574,7 +465,7 @@ export class ProviderModelCatalog { setEnabled(providerId: string, modelId: string, enabled: boolean): BuddyModel { this.assertCanSetEnabled(providerId, modelId, enabled) const current = this.#requireModelState(providerId, modelId) - return this.#toBuddyModel(this.#models.upsert({ + return this.#toBuddyModel(this.#saveModel({ ...current, enabled, updatedAt: new Date().toISOString(), @@ -591,7 +482,7 @@ export class ProviderModelCatalog { updatedAt, } }) - return records.map(record => this.#toBuddyModel(this.#models.upsert(record))) + return records.map(record => this.#toBuddyModel(this.#saveModel(record))) } assertCanSetEnabled(providerId: string, modelId: string, enabled: boolean): void { @@ -862,6 +753,19 @@ export class ProviderModelCatalog { }, serviceMetadata) } + #reconcileModels(providerId: string | undefined, observations: readonly ObservedProviderModel[], policy: ModelReconciliationPolicy, preserveUnchanged = false): void { + const current = this.#models.list(providerId) + const plan = planModelReconciliation(current, observations, policy, new Date().toISOString()) + for (const record of plan.upserts) + this.#upsertReconciled(record, this.#models.find(record.providerId, record.modelId), preserveUnchanged) + for (const record of plan.unavailable) + this.#saveModel(record) + } + + #saveModel(record: ProviderModelStateRecord): ProviderModelStateRecord { + return this.#models.upsert({ ...record, resolution: effectiveType(record) === 'chat' ? this.#resolveSource(record) : null }) + } + #upsertReconciled(record: ProviderModelStateRecord, current: ProviderModelStateRecord | null, preserveUnchanged = false): ProviderModelStateRecord { const next = { ...record, resolution: effectiveType(record) === 'chat' ? this.#resolveSource(record) : null } if (preserveUnchanged && current) { @@ -900,94 +804,6 @@ function effectiveParameters(model: ProviderModelStateRecord): ModelParametersOv } } -function nextSourceRevision(previous: string | undefined, candidate: string): string { - if (!previous || candidate > previous) - return candidate - return new Date(Date.parse(previous) + 1).toISOString() -} - -interface ModelSourceMetadata { - type: ProviderModelType - serviceMetadata: ServiceModelMetadata | null - api: string - catalogProviderId: string | null - catalogModelId: string | null - compat: Model['compat'] | null - cost: Model['cost'] - displayName: string - input: Array<'text' | 'image'> - reasoning: boolean - samplingParams: Record | null - sourceContextWindow: number | null - sourceFingerprint: string - sourceMaxTokens: number | null - thinkingLevelMap: Model['thinkingLevelMap'] | null -} - -function sourceMetadataFromCatalogModel(api: string, model: CatalogModel, cost: Model['cost'], current: ProviderModelStateRecord | null = null, serviceMetadata: ServiceModelMetadata | null = null): ModelSourceMetadata { - return createSourceMetadata({ - api, - catalogProviderId: model.provider, - catalogModelId: model.id, - compat: current?.api === api ? current.compat : null, - cost, - displayName: model.name, - input: [...model.input], - reasoning: model.reasoning, - samplingParams: current?.api === api ? current.samplingParams : null, - sourceContextWindow: model.contextWindow, - sourceMaxTokens: model.maxTokens, - thinkingLevelMap: model.thinkingLevelMap ?? null, - }, serviceMetadata) -} - -function emptyModelCost(): Model['cost'] { - return { cacheRead: 0, cacheWrite: 0, input: 0, output: 0 } -} - -function createSourceMetadata( - input: Omit & { type?: ProviderModelType, catalogModelId?: string | null }, - serviceMetadata: ServiceModelMetadata | null = null, -): ModelSourceMetadata { - const declared = serviceMetadata?.metadata - const reasoningOptions = declared?.reasoningOptions - const contextWindow = declared?.contextWindow ?? input.sourceContextWindow - const maxTokens = declared?.maxTokens ?? input.sourceMaxTokens - const metadata = { - ...input, - type: input.type ?? 'chat' as const, - catalogModelId: input.catalogModelId ?? null, - serviceMetadata, - ...(declared && (input.type ?? 'chat') === 'chat' - ? { - displayName: declared.name ?? input.displayName, - input: declared.input ? declared.input.filter((value): value is 'text' | 'image' => value === 'text' || value === 'image') : input.input, - reasoning: reasoningOptions !== undefined ? reasoningOptions.some(level => level !== 'off') : input.reasoning, - thinkingLevelMap: reasoningOptions !== undefined - ? Object.fromEntries(BUDDY_THINKING_LEVELS.map(level => [level, reasoningOptions.includes(level) ? level === 'off' ? 'none' : level : null])) - : input.thinkingLevelMap, - sourceContextWindow: declared.contextWindow === undefined ? Math.max(contextWindow ?? 128_000, maxTokens ?? 16_384) : contextWindow, - sourceMaxTokens: declared.maxTokens === undefined ? Math.min(maxTokens ?? 16_384, contextWindow ?? 128_000) : maxTokens, - } - : {}), - } - return { - ...metadata, - sourceFingerprint: createHash('sha256') - .update(serializeModelMetadata(metadata)) - .digest('hex'), - } -} - -function sourceParametersChanged( - current: ProviderModelStateRecord | null, - next: ModelSourceMetadata, -): boolean { - return !current - || current.sourceContextWindow !== next.sourceContextWindow - || current.sourceMaxTokens !== next.sourceMaxTokens -} - function hasServiceMetadata(model: ProviderModelStateRecord): boolean { return Boolean(model.serviceMetadata && Object.entries(model.serviceMetadata.metadata).some(([key, value]) => key !== 'name' && value !== undefined)) } diff --git a/apps/buddy/service/src/providers/__tests__/BuiltinModelDiscovery.spec.ts b/apps/buddy/service/src/providers/__tests__/BuiltinModelDiscovery.spec.ts index 687978cd4..f876a2a12 100644 --- a/apps/buddy/service/src/providers/__tests__/BuiltinModelDiscovery.spec.ts +++ b/apps/buddy/service/src/providers/__tests__/BuiltinModelDiscovery.spec.ts @@ -6,6 +6,7 @@ import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, describe, expect, it, vi } from 'vitest' +import { createRpcRuntimePlatform } from '../../platform/createRpcRuntimePlatform' import { openBuddyDatabase } from '../../storage/database' import { createProviderService } from '../createProviderService' @@ -52,7 +53,7 @@ async function fixture() { } return { credentials, - setup: () => createProviderService({ agentDirectory, database, peer }), + setup: () => createProviderService({ agentDirectory, database, platform: createRpcRuntimePlatform(peer), notify: peer.notify, notifyManagement: peer.notify }), cache: () => readFile(join(agentDirectory, 'models-store.json'), 'utf8'), } } diff --git a/apps/buddy/service/src/providers/__tests__/PluginModelRegistry.spec.ts b/apps/buddy/service/src/providers/__tests__/PluginModelRegistry.spec.ts index bd011fd0f..4e3e400b2 100644 --- a/apps/buddy/service/src/providers/__tests__/PluginModelRegistry.spec.ts +++ b/apps/buddy/service/src/providers/__tests__/PluginModelRegistry.spec.ts @@ -6,20 +6,17 @@ import { randomUUID } from 'node:crypto' import { InMemoryCredentialStore, InMemoryModelsStore } from '@earendil-works/pi-ai' import { ModelRuntime } from '@earendil-works/pi-coding-agent' import { describe, expect, it, onTestFinished } from 'vitest' -import { createExtensionSpeechBackend } from '../../../../electron/main/extensions/createExtensionSpeechBackend' import { registerExtensionImageReadingRpc } from '../../../../electron/main/extensions/registerExtensionImageReadingRpc' import { registerExtensionImageRpc } from '../../../../electron/main/extensions/registerExtensionImageRpc' import { registerExtensionModelRpc } from '../../../../electron/main/extensions/registerExtensionModelRpc' import { extensionChatRequestSchema, extensionChatRpc, pluginChatApi } from '../../../../shared/extensions/extensionChat' import { providersRpc } from '../../../../shared/providers/providerApi' import { isChatModel } from '../../../../shared/providers/providerModelType' -import { HOST_SPEECH_MODELS } from '../../../../shared/speech/speechApi' import { inferenceHost, pngBytes, readInferenceJson, rpcPair } from '../../images/__tests__/pluginImageFixtures' import { createPluginImageBackends } from '../../images/createPluginImageBackends' import { PluginImageGateway } from '../../images/PluginImageGateway' import { openBuddyDatabase } from '../../storage/database' import { createProviderRepository } from '../../storage/providerRepository' -import { createWorkspaceRepository } from '../../storage/workspaceRepository' import { AuthInteractionService } from '../AuthInteractionService' import { createProviderModelRuntime } from '../createProviderModelRuntime' import { PluginModelRegistry } from '../PluginModelRegistry' @@ -27,8 +24,7 @@ import { createProviderCredentialStatus } from '../ProviderCredentialStatus' import { ProviderModelSnapshotService } from '../ProviderModelSnapshotService' import { ProviderRequestHeaders } from '../ProviderRequestHeaders' import { ProviderService } from '../ProviderService' -import { registerProviderRpc } from '../registerProviderRpc' -import { refreshProviderSpeechModels } from '../registerProviderSpeechRpc' +import { registerProviderCatalogRpc, registerProviderRpc } from '../registerProviderRpc' const descriptor: ExtensionModel = { id: 'local-vision', @@ -85,6 +81,7 @@ async function fixture(modelSnapshot?: ProviderServiceOptions['modelSnapshot'], registerExtensionImageReadingRpc(peers.host, () => inference), registerExtensionImageRpc(peers.host, () => inference), registerExtensionModelRpc(peers.host, () => inference), + registerProviderCatalogRpc({ rpc: peers.service, service, refreshCapabilities: () => registry.refresh() }), registerProviderRpc({ rpc: peers.service, service, refreshCapabilities: () => registry.refresh() }), ] const gateway = new PluginImageGateway(peers.service) @@ -107,7 +104,6 @@ async function fixture(modelSnapshot?: ProviderServiceOptions['modelSnapshot'], inference, reading, generation, - workspace: createWorkspaceRepository(database), restart: async () => { await registry.dispose() const nextRuntime = await ModelRuntime.create({ credentials: new InMemoryCredentialStore(), modelsPath: null, modelsStore: new InMemoryModelsStore(), refreshOnCreate: false }) @@ -154,7 +150,6 @@ describe('plugin model registry', () => { expect(effective).toMatchObject({ id: model.id, provider: providerId, toolCall: tools, reasoning, input: ['text'], contextWindow, maxTokens }) expect(listed.find(item => item.id === model.id)).toMatchObject({ toolCalling: tools, capabilities: reasoning ? ['text', 'reasoning'] : ['text'], reasoningOptions: reasoning ? ['off', 'medium'] : [], contextWindow, maxTokens, sourceCapabilities: { toolCall: tools, image: false, pdf: false, audio: false, video: false } }) } - expect(await f.inference.models()).toEqual(cases.map(item => item.model)) await expect(f.service.executionModels.resolveAvailable({ ...selection, modelId: 'local', contextWindow: 262144, maxTokens: 8192 })).rejects.toMatchObject({ code: 'PROVIDER_UNAVAILABLE' }) }) @@ -175,7 +170,6 @@ describe('plugin model registry', () => { expect(await restarted.service.listModels(providerId)).toMatchObject([{ capabilityOverrides: overrides, toolCalling: false, capabilities: ['text', 'image', 'reasoning'], reasoningOptions: ['off', 'medium'], sourceCapabilities: { toolCall: false, reasoningOptions: ['off'] } }]) expect(await restarted.service.executionModels.resolveAvailable(selection)).toMatchObject({ toolCall: false, reasoning: true, thinkingLevelMap: { low: null, high: null }, contextWindow: descriptor.contextWindow, maxTokens: descriptor.maxTokens }) expect(await restarted.service.getDefaultModel()).toEqual(off) - expect(await f.inference.models()).toEqual([updated]) f.failListing() await expect(restarted.registry.refresh()).rejects.toThrow() expect(await restarted.service.listModels(providerId)).toMatchObject([{ capabilityOverrides: overrides, reasoningOptions: ['off', 'medium'] }]) @@ -276,7 +270,6 @@ describe('plugin model registry', () => { expect(await f.service.getDefaultModel()).toEqual(selected) expect(await f.service.listProviders()).toMatchObject([{ id: providerId, enabled: true, activeRunCount: 1 }]) expect(inFlight.reasoning).toBe(false) - expect((await f.inference.models())[0]).toEqual(automatic) } await expect(f.service.setModelEnabled(providerId, descriptor.id, false)).rejects.toThrow() await expect(f.service.setProviderEnabled(providerId, false)).rejects.toThrow() @@ -318,7 +311,6 @@ describe('plugin model registry', () => { expect(await f.service.executionModels.resolveAvailable(selection)).toMatchObject({ id: descriptor.id, provider: providerId, toolCall: supported, contextWindow: descriptor.contextWindow, maxTokens: descriptor.maxTokens }) expect(await f.service.getDefaultModel()).toEqual(selected) expect(f.repository.models.find(providerId, descriptor.id)).toMatchObject({ createdAt: before.createdAt, modelId: descriptor.id, providerId }) - expect((await f.inference.models())[0]).toEqual(automatic) } await f.service.setProviderEnabled(providerId, false) data = catalogData(false) @@ -450,63 +442,6 @@ describe('plugin model registry', () => { expect(await f.service.listProviders()).toMatchObject([{ id: providerId, displayName: descriptor.extensionName, plugin: { id: descriptor.extensionId, managementPage: true }, status: 'available', enabled: true, modelCount: 1, enabledModelCount: 1, setupComplete: true, authTypes: [] }]) }) - it('refreshes a service-only rename without changing plugin identity, model switches or default references', async () => { - const f = await fixture() - const disabled = { ...descriptor, id: 'disabled-model' } - f.setModels([descriptor, disabled]) - await f.registry.refresh() - await f.service.setDefaultModel(selected) - await f.service.setModelEnabled(providerId, disabled.id, false) - const previous = f.repository.models.list(providerId) - for (const providerName of ['Example Local', 'Renamed service', undefined]) { - const committed = Promise.withResolvers() - const dispose = f.service.onDidCommit(event => committed.resolve(event)) - f.setModels([{ ...descriptor, providerName }, { ...disabled, providerName }]) - f.peers.host.notify(extensionChatRpc.changed, {}) - const event = await committed.promise - dispose.dispose() - await f.registry.refresh() - expect(event).toMatchObject({ providers: [{ providerId, kind: 'changed' }], defaultModelChanged: false }) - expect(await f.service.listProviders()).toMatchObject([{ id: providerId, displayName: providerName ?? descriptor.extensionName, plugin: { id: descriptor.extensionId } }]) - expect(f.runtime.getProvider(providerId)?.name).toBe(providerName ?? descriptor.extensionName) - expect(await f.inference.models()).toMatchObject([{ extensionName: descriptor.extensionName }, { extensionName: descriptor.extensionName }]) - expect(await f.service.getDefaultModel()).toEqual(selected) - expect(await f.service.listModels(providerId)).toEqual(expect.arrayContaining([ - expect.objectContaining({ id: descriptor.id, enabled: true }), - expect.objectContaining({ id: disabled.id, enabled: false }), - ])) - expect(f.repository.models.list(providerId).map(({ providerId, modelId, createdAt }) => ({ providerId, modelId, createdAt }))).toEqual(previous.map(({ providerId, modelId, createdAt }) => ({ providerId, modelId, createdAt }))) - } - }) - - it('shares service naming across speech and image catalogs while keeping reader ownership', async () => { - const providerName = 'Example Local' - const f = await fixture() - f.setModels([]) - const identity = { extensionId: descriptor.extensionId, extensionName: descriptor.extensionName, providerName, revision: descriptor.revision, bindingRevision: descriptor.bindingRevision, generation: descriptor.generation, managementPage: true } - f.inference.imageModels = async () => [{ ...identity, id: 'image', title: 'Local image', capability: 'image', support: { operations: ['generate'], maxReferenceImages: 0, sizes: ['auto'], qualities: ['auto'], backgrounds: ['auto'] } }] - f.inference.imageReaders = async () => [{ ...identity, id: 'ocr', title: 'Local OCR', capability: 'ocr', support: { operations: ['extract'] } }] - await f.registry.refresh() - expect(await f.service.listProviders()).toMatchObject([{ id: providerId, displayName: providerName, modelCount: 1 }]) - expect(await f.generation.candidates()).toMatchObject([{ owner: providerName }]) - expect(await f.reading.candidates()).toMatchObject([{ owner: descriptor.extensionName }]) - const speech = createExtensionSpeechBackend({ - list: async () => (['speech.transcribe', 'speech.synthesize'] as const).map((kind, index) => ({ ...identity, id: `speech-${index}`, title: kind, kind, runtime: 'local', platforms: ['linux-x64'], streamingInput: false, streamingOutput: false })), - prepare: async () => {}, - invoke: async () => {}, - }, () => () => {}) - onTestFinished(f.peers.host.onRequest(HOST_SPEECH_MODELS, () => speech.list())) - const refreshSpeech = () => refreshProviderSpeechModels({ rpc: f.peers.service, providers: f.service, workspace: f.workspace }) - await refreshSpeech() - expect(await speech.list()).toMatchObject([{ source: providerName, providerId }, { source: providerName, providerId }]) - expect(await f.service.listProviders()).toMatchObject([{ id: providerId, displayName: providerName, modelCount: 3 }]) - expect((await f.service.listModels()).map(model => model.type).sort()).toEqual(['image-generation', 'speech', 'transcription']) - f.inference.imageModels = async () => [] - f.inference.imageReaders = async () => [] - await f.registry.refresh() - expect(await f.service.listProviders()).toMatchObject([{ id: providerId, displayName: providerName, status: 'available' }]) - }) - it('rechecks changed bindings received during a pending catalog request', async () => { const f = await fixture() const started = Promise.withResolvers() @@ -605,17 +540,4 @@ describe('plugin model registry', () => { expect(await f.service.listModels()).toEqual([]) expect(await f.reading.candidates()).toMatchObject([{ target: { kind: 'plugin', extensionId: descriptor.extensionId, modelId: 'ocr' }, support: { operations: ['extract'] }, available: true }]) }) - - it('keeps the last successful catalog on failure and does not create unregistered models', async () => { - const f = await fixture() - f.setModels([]) - await f.registry.refresh() - expect(await f.service.listModels()).toEqual([]) - expect(await f.service.listProviders()).toEqual([]) - f.setModels([descriptor]) - await f.registry.refresh() - f.failListing() - await expect(f.registry.refresh()).rejects.toThrow() - expect(await f.service.listModels()).toMatchObject([{ id: descriptor.id, available: true, capabilities: ['text', 'image'] }]) - }) }) diff --git a/apps/buddy/service/src/providers/__tests__/providerModelReconciliation.spec.ts b/apps/buddy/service/src/providers/__tests__/providerModelReconciliation.spec.ts new file mode 100644 index 000000000..b89061857 --- /dev/null +++ b/apps/buddy/service/src/providers/__tests__/providerModelReconciliation.spec.ts @@ -0,0 +1,47 @@ +import { describe, expect, it } from 'vitest' +import { planModelReconciliation } from '../providerModelReconciliation' +import { createSourceMetadata } from '../providerModelSource' + +const before = '2026-11-01T00:00:00.000Z' +const now = '2026-11-14T00:00:00.000Z' +const observation = { + providerId: 'fixture', + modelId: 'request-model-id', + metadata: createSourceMetadata({ + api: 'openai-completions', + catalogProviderId: null, + compat: null, + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + displayName: 'Fixture model', + input: ['text'], + reasoning: false, + samplingParams: null, + sourceContextWindow: 32_000, + sourceMaxTokens: 4_000, + thinkingLevelMap: null, + }), +} + +function currentModel() { + return planModelReconciliation([], [observation], 'synced-complete', before).upserts[0]! +} + +describe('model source reconciliation', () => { + it('only marks missing discovered models unavailable after a complete discovery', () => { + const current = currentModel() + const manual = { ...current, modelId: 'manual', source: 'manual' as const } + expect(planModelReconciliation([current, manual], [], 'synced-partial', now)).toEqual({ upserts: [], unavailable: [] }) + expect(planModelReconciliation([current, manual], [], 'synced-complete', now).unavailable).toEqual([ + { ...current, available: false, updatedAt: now }, + ]) + expect(planModelReconciliation([manual], [{ ...observation, modelId: 'manual' }], 'synced-complete', now).upserts).toEqual([]) + }) + + it('refreshes builtin metadata without restoring account availability or seeding new models', () => { + const current = { ...currentModel(), source: 'builtin' as const, available: false } + const result = planModelReconciliation([current], [observation, { ...observation, modelId: 'new-model' }], 'builtin-metadata', now) + expect(result.upserts).toHaveLength(1) + expect(result.upserts[0]).toMatchObject({ available: false, lastSeenAt: before, source: 'builtin' }) + expect(result.unavailable).toEqual([]) + }) +}) diff --git a/apps/buddy/service/src/providers/createProviderService.ts b/apps/buddy/service/src/providers/createProviderService.ts index bbdd8118e..eab481a29 100644 --- a/apps/buddy/service/src/providers/createProviderService.ts +++ b/apps/buddy/service/src/providers/createProviderService.ts @@ -1,6 +1,6 @@ import type { DatabaseSync } from 'node:sqlite' import type { ApplicationDiagnosticReporter } from '../../../shared/diagnostics/applicationDiagnostic' -import type { RuntimeRpcPeerContract } from '../../../shared/runtime/rpcPeer' +import type { RuntimePlatform } from '../platform/RuntimePlatform' import type { ProviderRepository } from '../storage/providerRepository' import { chmod, mkdir, writeFile } from 'node:fs/promises' import { join } from 'node:path' @@ -14,7 +14,6 @@ import { createWorkspaceRepository } from '../storage/workspaceRepository' import { AuthInteractionService } from './AuthInteractionService' import { createProviderLoginOptions } from './createProviderLoginOptions' import { createProviderModelRuntime } from './createProviderModelRuntime' -import { HostCredentialStore } from './HostCredentialStore' import { createProviderCredentialStatus } from './ProviderCredentialStatus' import { clearAmbientProviderCredentials } from './providerEnvironment' import { OpenAiCompatibleModelDiscovery } from './ProviderModelDiscovery' @@ -25,7 +24,9 @@ export interface CreateProviderServiceOptions { agentDirectory: string database: DatabaseSync getActiveRuns?: () => ReadonlyArray<{ model: string, provider: string }> - peer: RuntimeRpcPeerContract + platform: Pick + notify: (method: string, params: unknown) => void + notifyManagement: (method: string, params: unknown) => void providers?: ProviderRepository record?: ApplicationDiagnosticReporter } @@ -42,7 +43,7 @@ export async function createProviderService( await writeFile(modelsPath, '{}\n', { encoding: 'utf8', mode: 0o600 }) await chmod(modelsPath, 0o600) - const credentials = new HostCredentialStore(options.peer) + const credentials = options.platform.createCredentials() const modelRuntime = await ModelRuntime.create({ allowModelNetwork: false, refreshOnCreate: false, @@ -53,10 +54,8 @@ export async function createProviderService( const providers = options.providers ?? createProviderRepository(options.database) const requestHeaders = new ProviderRequestHeaders(providers.states) const providerRuntime = createProviderModelRuntime(modelRuntime, requestHeaders, options.record) - const authInteractions = new AuthInteractionService({ openExternal: async (url) => { - await options.peer.request('host.openExternal', { url }) - } }) - authInteractions.onDidChallenge(challenge => options.peer.notify(providerNotifications.authChallenge.method, challenge)) + const authInteractions = new AuthInteractionService({ openExternal: options.platform.openExternal }) + authInteractions.onDidChallenge(challenge => options.notifyManagement(providerNotifications.authChallenge.method, challenge)) authInteractions.onDidChange(change => options.record?.({ event: `provider.auth.${change.kind.replaceAll('-', '_')}`, level: 'info', operationId: change.loginId, count: change.kind === 'challenge-opened' ? 1 : 0 })) const service = new ProviderService({ authInteractions, @@ -73,14 +72,14 @@ export async function createProviderService( sessionRuntime: modelRuntime, }) service.onDidResolve(event => options.record?.({ event: `provider.resolution.${event.restored ? 'restored' : 'rebuilt'}`, level: 'info', count: 1 })) - service.onDidCommit(event => options.peer.notify(providerNotifications.changed.method, { source: 'catalog', revision: event.revision })) + service.onDidCommit(event => options.notify(providerNotifications.changed.method, { source: 'catalog', revision: event.revision })) service.onDidChangeMetadata((event) => { if (event.kind === 'refresh-completed' || event.kind === 'refresh-failed' || event.kind === 'accepted') - options.peer.notify(providerNotifications.changed.method, { source: 'metadata', revision: event.revision }) + options.notify(providerNotifications.changed.method, { source: 'metadata', revision: event.revision }) }) service.onDidChangeCredential((event) => { if (event.kind === 'observation' || event.kind === 'store-availability') - options.peer.notify(providerNotifications.changed.method, { source: 'credentials', revision: event.kind === 'observation' ? event.current.revision : event.revision }) + options.notify(providerNotifications.changed.method, { source: 'credentials', revision: event.kind === 'observation' ? event.current.revision : event.revision }) }) service.onDidOperate(event => options.record?.({ event: `provider.operation.${event.stage}`, level: event.stage === 'failed' ? 'warn' : 'info', operationId: event.operationId })) service.onDidCommit(event => options.record?.({ event: 'provider.state.committed', level: 'info', operationId: event.commitId, revision: event.revision, count: event.providers.length + event.models.length })) diff --git a/apps/buddy/service/src/providers/providerModelReconciliation.ts b/apps/buddy/service/src/providers/providerModelReconciliation.ts new file mode 100644 index 000000000..44388cbdd --- /dev/null +++ b/apps/buddy/service/src/providers/providerModelReconciliation.ts @@ -0,0 +1,86 @@ +import type { ProviderModelStateRecord } from '../storage/providerModelStateRepository' +import type { ModelSourceMetadata } from './providerModelSource' +import { nextSourceRevision, sourceParametersChanged } from './providerModelSource' + +export type ModelReconciliationPolicy = 'plugin-chat' | 'plugin-capability' | 'builtin' | 'builtin-metadata' | 'catalog-media' | 'synced-complete' | 'synced-partial' + +export interface ObservedProviderModel { + providerId: string + modelId: string + metadata: ModelSourceMetadata + available?: boolean +} + +export function planModelReconciliation( + currentModels: readonly ProviderModelStateRecord[], + observations: readonly ObservedProviderModel[], + policy: ModelReconciliationPolicy, + now: string, +): { upserts: ProviderModelStateRecord[], unavailable: ProviderModelStateRecord[] } { + const currentById = new Map(currentModels.map(model => [identity(model), model])) + const seen = new Set(observations.map(identity)) + const plugin = policy === 'plugin-chat' || policy === 'plugin-capability' + const metadataOnly = policy === 'builtin-metadata' + const synced = policy === 'synced-complete' || policy === 'synced-partial' + const upserts: ProviderModelStateRecord[] = [] + for (const observation of observations) { + const current = currentById.get(identity(observation)) ?? null + if ((!plugin && current?.source === 'manual') || (metadataOnly && !current)) + continue + const metadata = observation.metadata + const preserveRevision = policy === 'catalog-media' || policy === 'plugin-capability' + const next: ProviderModelStateRecord = { + ...metadata, + providerId: observation.providerId, + modelId: observation.modelId, + source: plugin || synced ? 'synced' : 'builtin', + typeOverride: plugin ? null : current?.typeOverride ?? null, + catalogSelection: synced ? current?.catalogSelection ?? null : null, + capabilityOverrides: current?.capabilityOverrides ?? null, + acknowledgedSourceRevision: policy === 'plugin-capability' ? null : current?.acknowledgedSourceRevision ?? null, + overrideContextWindow: plugin ? null : current?.overrideContextWindow ?? null, + overrideMaxTokens: plugin ? null : current?.overrideMaxTokens ?? null, + enabled: current?.enabled ?? plugin, + available: metadataOnly ? current!.available : observation.available ?? true, + createdAt: current?.createdAt ?? now, + lastSeenAt: metadataOnly ? current!.lastSeenAt : now, + updatedAt: now, + sourceRevision: !preserveRevision && sourceParametersChanged(current, metadata) + ? nextSourceRevision(current?.sourceRevision, now) + : current?.sourceRevision ?? now, + ...(policy === 'catalog-media' + ? { + sourceContextWindow: current?.typeOverride === 'chat' ? current.sourceContextWindow : null, + sourceMaxTokens: current?.typeOverride === 'chat' ? current.sourceMaxTokens : null, + } + : {}), + } + upserts.push(next) + currentById.set(identity(next), next) + } + const unavailable = currentModels.filter(current => !seen.has(identity(current)) && marksMissingUnavailable(current, policy)) + .map(current => ({ ...current, available: false, updatedAt: now })) + return { upserts, unavailable } +} + +function marksMissingUnavailable(current: ProviderModelStateRecord, policy: ModelReconciliationPolicy): boolean { + switch (policy) { + case 'builtin-metadata': + case 'synced-partial': + return false + case 'plugin-chat': + return (current.typeOverride ?? current.type ?? 'chat') === 'chat' + case 'plugin-capability': + return current.api === 'plugin-speech' || current.api === 'plugin-image' + case 'builtin': + return current.source !== 'manual' && (current.typeOverride ?? current.type ?? 'chat') === 'chat' && current.available + case 'catalog-media': + return current.source === 'builtin' && current.api === 'openai-audio' && current.available + case 'synced-complete': + return current.source === 'synced' + } +} + +function identity(model: { providerId: string, modelId: string }): string { + return JSON.stringify([model.providerId, model.modelId]) +} diff --git a/apps/buddy/service/src/providers/providerModelSource.ts b/apps/buddy/service/src/providers/providerModelSource.ts new file mode 100644 index 000000000..9b2cb5a60 --- /dev/null +++ b/apps/buddy/service/src/providers/providerModelSource.ts @@ -0,0 +1,96 @@ +import type { Api, Model } from '@earendil-works/pi-ai' +import type { ServiceModelMetadata } from '../../../shared/providers/providerDiscovery' +import type { ProviderModelType } from '../../../shared/providers/providerModelType' +import type { ProviderModelStateRecord } from '../storage/providerModelStateRepository' +import type { CatalogModel } from './ModelsDevCatalog' +import { createHash } from 'node:crypto' +import { BUDDY_THINKING_LEVELS } from '../../../shared/conversation/modelSelection' +import { serializeModelMetadata } from './modelMetadata' + +export function nextSourceRevision(previous: string | undefined, candidate: string): string { + if (!previous || candidate > previous) + return candidate + return new Date(Date.parse(previous) + 1).toISOString() +} + +export interface ModelSourceMetadata { + type: ProviderModelType + serviceMetadata: ServiceModelMetadata | null + api: string + catalogProviderId: string | null + catalogModelId: string | null + compat: Model['compat'] | null + cost: Model['cost'] + displayName: string + input: Array<'text' | 'image'> + reasoning: boolean + samplingParams: Record | null + sourceContextWindow: number | null + sourceFingerprint: string + sourceMaxTokens: number | null + thinkingLevelMap: Model['thinkingLevelMap'] | null +} + +export function sourceMetadataFromCatalogModel(api: string, model: CatalogModel, cost: Model['cost'], current: ProviderModelStateRecord | null = null, serviceMetadata: ServiceModelMetadata | null = null): ModelSourceMetadata { + return createSourceMetadata({ + api, + catalogProviderId: model.provider, + catalogModelId: model.id, + compat: current?.api === api ? current.compat : null, + cost, + displayName: model.name, + input: [...model.input], + reasoning: model.reasoning, + samplingParams: current?.api === api ? current.samplingParams : null, + sourceContextWindow: model.contextWindow, + sourceMaxTokens: model.maxTokens, + thinkingLevelMap: model.thinkingLevelMap ?? null, + }, serviceMetadata) +} + +export function emptyModelCost(): Model['cost'] { + return { cacheRead: 0, cacheWrite: 0, input: 0, output: 0 } +} + +export function createSourceMetadata( + input: Omit & { type?: ProviderModelType, catalogModelId?: string | null }, + serviceMetadata: ServiceModelMetadata | null = null, +): ModelSourceMetadata { + const declared = serviceMetadata?.metadata + const reasoningOptions = declared?.reasoningOptions + const contextWindow = declared?.contextWindow ?? input.sourceContextWindow + const maxTokens = declared?.maxTokens ?? input.sourceMaxTokens + const metadata = { + ...input, + type: input.type ?? 'chat' as const, + catalogModelId: input.catalogModelId ?? null, + serviceMetadata, + ...(declared && (input.type ?? 'chat') === 'chat' + ? { + displayName: declared.name ?? input.displayName, + input: declared.input ? declared.input.filter((value): value is 'text' | 'image' => value === 'text' || value === 'image') : input.input, + reasoning: reasoningOptions !== undefined ? reasoningOptions.some(level => level !== 'off') : input.reasoning, + thinkingLevelMap: reasoningOptions !== undefined + ? Object.fromEntries(BUDDY_THINKING_LEVELS.map(level => [level, reasoningOptions.includes(level) ? level === 'off' ? 'none' : level : null])) + : input.thinkingLevelMap, + sourceContextWindow: declared.contextWindow === undefined ? Math.max(contextWindow ?? 128_000, maxTokens ?? 16_384) : contextWindow, + sourceMaxTokens: declared.maxTokens === undefined ? Math.min(maxTokens ?? 16_384, contextWindow ?? 128_000) : maxTokens, + } + : {}), + } + return { + ...metadata, + sourceFingerprint: createHash('sha256') + .update(serializeModelMetadata(metadata)) + .digest('hex'), + } +} + +export function sourceParametersChanged( + current: ProviderModelStateRecord | null, + next: ModelSourceMetadata, +): boolean { + return !current + || current.sourceContextWindow !== next.sourceContextWindow + || current.sourceMaxTokens !== next.sourceMaxTokens +} diff --git a/apps/buddy/service/src/providers/registerProviderRpc.ts b/apps/buddy/service/src/providers/registerProviderRpc.ts index cde00a0b1..3ccdb0d4e 100644 --- a/apps/buddy/service/src/providers/registerProviderRpc.ts +++ b/apps/buddy/service/src/providers/registerProviderRpc.ts @@ -11,21 +11,12 @@ export interface RegisterProviderRpcOptions { refreshCapabilities?: () => Promise } -export function registerProviderRpc(options: RegisterProviderRpcOptions): () => void { +export function registerProviderCatalogRpc(options: RegisterProviderRpcOptions): () => void { const disposers: Array<() => void> = [] - disposers.push(registerRuntimeRequest(options.rpc, providersRpc.list, async () => { await options.refreshCapabilities?.() return options.service.listProviders() })) - disposers.push(registerRuntimeRequest(options.rpc, providersRpc.listBuiltinPresets, () => options.service.listBuiltinPresets())) - disposers.push(registerRuntimeRequest(options.rpc, providersRpc.rename, async (input) => { - const provider = await options.service.renameProvider(input.providerId, input.displayName, input.requestHeaders) - return provider - })) - disposers.push(registerRuntimeRequest(options.rpc, providersRpc.add, async (input) => { - return options.service.addProvider(input.providerId) - })) disposers.push(registerRuntimeRequest(options.rpc, providersRpc.listModels, async (input) => { await options.refreshCapabilities?.() const models = await options.service.listModels(input.providerId ?? undefined) @@ -37,6 +28,20 @@ export function registerProviderRpc(options: RegisterProviderRpcOptions): () => disposers.push(registerRuntimeRequest(options.rpc, providersRpc.getDefaultModel, () => { return options.service.getDefaultModel() })) + return () => disposers.splice(0).forEach(dispose => dispose()) +} + +export function registerProviderRpc(options: RegisterProviderRpcOptions): () => void { + const disposers: Array<() => void> = [] + + disposers.push(registerRuntimeRequest(options.rpc, providersRpc.listBuiltinPresets, () => options.service.listBuiltinPresets())) + disposers.push(registerRuntimeRequest(options.rpc, providersRpc.rename, async (input) => { + const provider = await options.service.renameProvider(input.providerId, input.displayName, input.requestHeaders) + return provider + })) + disposers.push(registerRuntimeRequest(options.rpc, providersRpc.add, async (input) => { + return options.service.addProvider(input.providerId) + })) disposers.push(registerRuntimeRequest(options.rpc, providersRpc.login, async (input) => { await options.service.login(input.providerId, input.authType) return ok() diff --git a/apps/buddy/service/src/questions/UserQuestionDelivery.ts b/apps/buddy/service/src/questions/UserQuestionDelivery.ts index 35e18f62f..1a916f1e1 100644 --- a/apps/buddy/service/src/questions/UserQuestionDelivery.ts +++ b/apps/buddy/service/src/questions/UserQuestionDelivery.ts @@ -24,7 +24,7 @@ export class UserQuestionDelivery { constructor(options: Options) { this.#options = options } deliver(request: UserQuestionRequest): Promise { - const key = request.conversationId + const key = JSON.stringify([request.conversationId, request.branchId]) const pending = (this.#operations.get(key) ?? Promise.resolve()).catch(() => {}).then(() => this.#deliver(request)) this.#operations.set(key, pending) void pending.finally(() => { @@ -80,8 +80,8 @@ export class UserQuestionDelivery { } if (owner.run.status !== 'completed' || this.#options.runner.isStopping) throw new Error('QUESTION_STALE') - if (this.#options.runner.hasActiveExecution(owner.run.conversationId, owner.run.buddyId) - || this.#options.database.prepare(`SELECT 1 FROM runs WHERE conversation_id = ? AND buddy_id = ? AND status IN ('queued', 'running') LIMIT 1`).get(owner.run.conversationId, owner.run.buddyId) + if (this.#options.runner.hasActiveExecution(owner.run.conversationId, owner.run.buddyId, owner.run.branchId) + || this.#options.database.prepare(`SELECT 1 FROM runs WHERE conversation_id = ? AND branch_id = ? AND buddy_id = ? AND status IN ('queued', 'running') LIMIT 1`).get(owner.run.conversationId, owner.run.branchId, owner.run.buddyId) || this.#options.database.prepare(`SELECT 1 FROM chat_queue WHERE conversation_id = ? AND branch_id = ? AND state IN ('waiting', 'paused') LIMIT 1`).get(request.conversationId, request.branchId)) { return null } diff --git a/apps/buddy/service/src/questions/UserQuestionService.ts b/apps/buddy/service/src/questions/UserQuestionService.ts index e65d1c642..2dd2a1226 100644 --- a/apps/buddy/service/src/questions/UserQuestionService.ts +++ b/apps/buddy/service/src/questions/UserQuestionService.ts @@ -2,12 +2,18 @@ import type { DatabaseSync } from 'node:sqlite' import type { ExecutionScope } from '../../../shared/executions/executionPresentationApi' import type { AskUserInput, UserQuestionRequest, UserQuestionsChanged, UserQuestionSubmit, UserQuestionTarget } from '../../../shared/questions/userQuestionApi' import type { ExecutionCoordinator } from '../agent/execution/ExecutionCoordinator' +import type { CommandAdmissionService } from '../commands/CommandAdmissionService' +import type { InputCommandIntent } from '../commands/commandIntent' +import type { CommandRejection } from '../storage/CommandCommitter' import type { ConversationRepository } from '../storage/conversationRepository' import type { ExecutionRepository } from '../storage/executionRepository' import { randomUUID } from 'node:crypto' import { Emitter } from '../../../shared/events/Emitter' import { askUserInputSchema, userQuestionRequestSchema, userQuestionRpc, validateUserQuestionResponse } from '../../../shared/questions/userQuestionApi' import { executionFence } from '../storage/executionRepository' +import { afterTransactionCommit } from '../storage/transaction' + +type QuestionCommandMethod = 'questions.answer' | 'questions.cancel' | 'questions.retry' interface Options { database: DatabaseSync @@ -20,10 +26,12 @@ interface Options { export class UserQuestionService { readonly #options: Options readonly #changes = new Emitter(() => {}) + readonly #commits = new Emitter(() => {}) readonly #waiting = new Map() - readonly #operations = new Map>() + readonly #operations = new Map>() readonly #stopping = new AbortController() readonly onDidChange = this.#changes.event + readonly onDidCommit = this.#commits.event constructor(options: Options) { this.#options = options @@ -77,10 +85,62 @@ export class UserQuestionService { resolvedAt: null, } this.#options.database.prepare('INSERT INTO user_questions (id, run_id, tool_call_id, request_json) VALUES (?, ?, ?, ?)').run(request.id, runId, toolCallId, JSON.stringify(request)) - this.#changes.fire(request) + this.#notify(request) return request } + executeCommand(method: QuestionCommandMethod, input: unknown, admission: CommandAdmissionService) { + return admission.execute(method, input, { + scope: (intent, operation) => this.#serialize(intent.requestId, async () => { + const result = await operation() + if (result.created && result.receipt.decision === 'accepted' && result.receipt.result.questionDelivery === 'pending') + await this.#deliver(this.#find(intent.requestId)) + return result + }), + prepare: async intent => intent, + validate: intent => this.#validateCommand(method, intent), + commit: (intent) => { + const current = this.#target(intent) + const response = method === 'questions.answer' + ? validateUserQuestionResponse(current.questions, { answers: (intent as InputCommandIntent<'questions.answer'>).answers }) + : current.response + const request = method === 'questions.retry' && current.delivery?.state === 'delivered' + ? current + : this.#save({ ...current, response, status: method === 'questions.answer' ? 'answered' : method === 'questions.cancel' ? 'cancelled' : current.status, resolvedAt: current.resolvedAt ?? new Date().toISOString(), delivery: { state: 'pending', targetRunId: null } }) + return { + target: { questionId: request.id, conversationId: request.conversationId, branchId: request.branchId }, + result: { questionStatus: request.status, questionDelivery: request.delivery!.state }, + } + }, + }) + } + + #validateCommand(method: QuestionCommandMethod, intent: Readonly>): CommandRejection | null { + const target = { questionId: intent.requestId, conversationId: intent.conversationId, branchId: intent.branchId } + try { + if (this.#stopping.signal.aborted) + return { errorCode: 'RESOURCE_UNAVAILABLE', target, result: {} } + const request = this.#target(intent) + if (method === 'questions.retry') { + if (!request.delivery || request.status === 'pending' || request.status === 'interrupted') + throw new Error('QUESTION_CLOSED') + } + else { + if (request.status !== 'pending') + throw new Error('QUESTION_CLOSED') + if (method === 'questions.answer') + validateUserQuestionResponse(request.questions, { answers: (intent as InputCommandIntent<'questions.answer'>).answers }) + } + return null + } + catch (error) { + const code = error instanceof Error ? error.message : '' + if (code === 'QUESTION_CLOSED' || code === 'QUESTION_STALE' || code === 'QUESTION_NOT_FOUND' || code === 'QUESTION_ANSWER_INVALID') + return { errorCode: code, target, result: {} } + throw error + } + } + async wait(runId: string, requestId: string, signal: AbortSignal, deliveredMessageIds: readonly string[] = []): Promise { this.#assertLive(runId) const request = this.#find(requestId) @@ -197,7 +257,7 @@ export class UserQuestionService { if (request.conversationId !== input.conversationId || request.branchId !== input.branchId) throw new Error('QUESTION_NOT_FOUND') const conversation = this.#options.conversations.findById(request.conversationId) - if (!conversation || conversation.deletedAt || conversation.activeBranchId !== request.branchId) + if (!conversation || conversation.deletedAt) throw new Error('QUESTION_STALE') return request } @@ -205,7 +265,7 @@ export class UserQuestionService { #assertLive(runId: string) { const record = this.#options.executions.find(runId) const root = record && this.#options.executions.find(record.run.rootRunId) - if (!record || !root || this.#options.conversations.findById(record.run.conversationId)?.activeBranchId !== record.run.branchId) + if (!record || !root || this.#options.conversations.findById(record.run.conversationId)?.deletedAt !== null) throw new Error('QUESTION_STALE') this.#options.executions.assertCurrent(executionFence(record, root)) return record.run @@ -228,15 +288,23 @@ export class UserQuestionService { #save(request: UserQuestionRequest): UserQuestionRequest { this.#options.database.prepare('UPDATE user_questions SET request_json = ? WHERE id = ?').run(JSON.stringify(request), request.id) - this.#changes.fire(request) + this.#notify(request) return request } + #notify(request: UserQuestionRequest): void { + afterTransactionCommit(this.#options.database, () => { + this.#changes.fire(request) + this.#commits.fire({ conversationId: request.conversationId, branchId: request.branchId, requestId: request.id }) + }, () => {}) + } + async #deliver(request: UserQuestionRequest): Promise { if (this.#stopping.signal.aborted) return request const pending = this.#save({ ...request, delivery: { state: 'pending', targetRunId: null } }) try { + this.#target({ ...request, requestId: request.id }) const targetRunId = await this.#options.deliver(pending) return targetRunId ? this.#save({ ...pending, delivery: { state: 'delivered', targetRunId } }) : pending } @@ -245,7 +313,7 @@ export class UserQuestionService { } } - #serialize(id: string, operation: () => Promise): Promise { + #serialize(id: string, operation: () => Promise): Promise { const result = (this.#operations.get(id) ?? Promise.resolve()).catch(() => {}).then(operation) this.#operations.set(id, result) void result.finally(() => { @@ -259,5 +327,6 @@ export class UserQuestionService { this.#stopping.abort() await Promise.allSettled([...this.#operations.values()]) this.#changes.dispose() + this.#commits.dispose() } } diff --git a/apps/buddy/service/src/questions/__tests__/ProductQuestionCommands.spec.ts b/apps/buddy/service/src/questions/__tests__/ProductQuestionCommands.spec.ts new file mode 100644 index 000000000..05384bb82 --- /dev/null +++ b/apps/buddy/service/src/questions/__tests__/ProductQuestionCommands.spec.ts @@ -0,0 +1,348 @@ +import type { TrustedProductContext } from '../../../../shared/productProtocol/identity' +import type { BuddyServiceCommands } from '../../BuddyService' +import { mkdtempSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { ProductDesktopGateway } from '../../../../electron/main/productProtocol/ProductDesktopGateway' +import { productPageSchema, productQuestionSchema } from '../../../../shared/productProtocol/queries' +import { userQuestionRpc } from '../../../../shared/questions/userQuestionApi' +import { ExecutionCoordinator } from '../../agent/execution/ExecutionCoordinator' +import { MutationCoordinator } from '../../changes/MutationCoordinator' +import { CommandAdmissionService, createLocalCommandPrincipal } from '../../commands/CommandAdmissionService' +import { SqliteRunEventLog } from '../../events/SqliteRunEventLog' +import { createProductCommandHandlers } from '../../productProtocol/createProductCommandHandlers' +import { createProductSynchronization } from '../../productProtocol/createProductSynchronization' +import { ProductConnection } from '../../productProtocol/ProductConnection' +import { ProductQueries } from '../../productProtocol/ProductQueries' +import { RunLifecycleService } from '../../runs/RunLifecycleService' +import { seedSqliteRun } from '../../storage/__tests__/sqliteRunFixture' +import { createConversationRepository } from '../../storage/conversationRepository' +import { openBuddyMigrationCandidate } from '../../storage/database' +import { createExecutionRepository } from '../../storage/executionRepository' +import { createMailboxRepository } from '../../storage/mailboxRepository' +import { createRunRepository } from '../../storage/runRepository' +import { UserQuestionDelivery } from '../UserQuestionDelivery' +import { UserQuestionService } from '../UserQuestionService' + +const cleanups: Array<() => Promise> = [] +afterEach(async () => { + for (const cleanup of cleanups.splice(0).reverse()) await cleanup() +}) +const scope = { conversationId: 'conversation', branchId: 'branch-conversation' } +const question = { questions: [{ id: 'scope', question: '选择范围', kind: 'single' as const, options: [{ id: 'small', label: '只处理当前目录', recommended: true }, { id: 'large', label: '处理整个空间' }] }] } +const answers = [{ questionId: 'scope', selectedOptionIds: ['small'], text: '保留原始说明😀' }] +function fixture() { + const directory = mkdtempSync(join(tmpdir(), 'lexora-question-command-')) + const database = openBuddyMigrationCandidate({ databasePath: join(directory, 'buddy.sqlite3') }) + seedSqliteRun(database, 'root', { conversationId: 'conversation' }) + database.exec('UPDATE runs SET status = \'running\'') + const storage = { database, conversations: createConversationRepository(database), executions: createExecutionRepository(database), runs: createRunRepository(database) } + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + database.prepare('INSERT INTO storage_actors VALUES (\'phone\', \'device\', \'active\', \'2026-11-09T00:00:00.000Z\')').run() + const events = new SqliteRunEventLog({ database }) + const mailbox = createMailboxRepository(database) + const lifecycle = new RunLifecycleService({ eventLog: events, repository: storage.runs }) + const mutations = new MutationCoordinator() + const coordinator = new ExecutionCoordinator({ executions: storage.executions, mailbox, events, lifecycle, mutations, readResult: () => '', launchChild: async () => { + throw new Error('Unexpected child') + }, stopExecution: async () => false, listIncomplete: () => storage.runs.listIncomplete() }) + const delivery = new UserQuestionDelivery({ ...storage, mailbox, coordinator, runner: { hasActiveExecution: () => false, isStopping: false }, turns: { continueQuestion: async () => { + throw new Error('Unexpected continuation') + } } }) + let deliveryAvailable = true + const options = { ...storage, coordinator, deliver: (request: Parameters[0]) => deliveryAvailable ? delivery.deliver(request) : Promise.reject(new Error('Fixture delivery unavailable')) } + const service = new UserQuestionService(options) + const admission = new CommandAdmissionService({ database, principal: createLocalCommandPrincipal(database), sourceInstanceId: 'instance' }) + const remote = new CommandAdmissionService({ database, principal: { actorId: 'phone', assertAuthenticated() {} }, sourceInstanceId: 'instance' }) + const dataGeneration = String(database.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation) + const queries = new ProductQueries(database, 'phone', 'instance') + const command = (requestId: string, commandId: string, extra: Record = {}) => ({ dataGeneration, commandId, expectedServiceInstanceId: 'instance', intent: { ...scope, requestId, ...extra } }) + const ask = (toolCallId = 'ask') => service.ask('root', toolCallId, question, new AbortController().signal) + const countDeliveries = () => Number(database.prepare('SELECT COUNT(*) AS count FROM execution_mailbox_sources WHERE id LIKE \'user-question:%\'').get()!.count) + cleanups.push(async () => { + await admission.dispose() + await remote.dispose() + await service.dispose() + await coordinator.dispose() + await lifecycle.dispose() + await events.close() + mutations.dispose() + database.close() + rmSync(directory, { recursive: true, force: true }) + }) + return { ...storage, directory, events, queries, service, options, admission, remote, command, ask, countDeliveries, setDeliveryAvailable: (value: boolean) => { + deliveryAvailable = value + } } +} + +describe('durable product question commands', () => { + it('answers two concurrently running branches into their original execution trees despite a different default selection', async () => { + const f = fixture() + f.conversations.createBranch({ id: 'branch-B', conversationId: scope.conversationId, parentBranchId: null, forkedFromMessageId: null, createdAt: '2026-11-09T00:00:00.000Z', activate: false }) + const a = f.runs.findById('root')! + const b = f.runs.create({ ...a, id: 'root-B', branchId: 'branch-B', rootRunId: 'root-B', threadId: undefined, triggeringMessageId: 'input-B' }) + const qa = f.ask('question-A') + const qb = f.service.ask(b.id, 'question-B', question, new AbortController().signal) + f.conversations.activateBranch({ conversationId: scope.conversationId, branchId: b.branchId, updatedAt: '2026-11-09T00:00:01.000Z' }) + expect(f.queries.task(scope.conversationId, true)).toMatchObject({ pendingQuestionCount: 2, activity: 'running' }) + const [ra, rb] = await Promise.all([ + f.service.executeCommand('questions.answer', f.command(qa.id, 'answer-A', { answers }), f.remote), + f.service.executeCommand('questions.answer', f.command(qb.id, 'answer-B', { branchId: b.branchId, answers: [{ ...answers[0]!, text: 'Only branch B' }] }), f.remote), + ]) + expect([ra.receipt.decision, rb.receipt.decision]).toEqual(['accepted', 'accepted']) + expect(f.service.list(scope)[0]).toMatchObject({ id: qa.id, delivery: { state: 'delivered', targetRunId: 'root' } }) + expect(f.service.list({ ...scope, branchId: b.branchId })[0]).toMatchObject({ id: qb.id, delivery: { state: 'delivered', targetRunId: 'root-B' } }) + expect(f.countDeliveries()).toBe(2) + expect(f.queries.task(scope.conversationId, true)).toMatchObject({ pendingQuestionCount: 0 }) + }) + + it('advertises real question reads, enforces reader roles and resolves through the product command handler', async () => { + const f = fixture() + const request = f.ask() + const commands = { admission: f.remote, questions: { execute: (method, input) => f.service.executeCommand(method, input, f.remote) } } as BuddyServiceCommands + const handlers = { ...f.queries.handlers(), ...createProductCommandHandlers({ commands, assertOwner: () => {}, resolveQuote: () => { + throw new Error('Unexpected quote') + }, allowedActions: new Set() }) } + const initialize = { protocol: { major: 1, minMinor: 0, maxMinor: 0 }, client: { kind: 'mobile', version: 'test' }, supportedContentKinds: [], supportedReviewKinds: [] } + const context: TrustedProductContext = { serviceId: 'service', profileId: 'profile', dataGeneration: f.command(request.id, 'x').dataGeneration, serviceInstanceId: 'instance', connectionId: 'connection', clientSessionId: 'session', deviceId: 'phone', ownerPrincipalId: 'owner', credentialId: 'credential', grantGeneration: '1', role: 'reader' } + for (const role of ['reader', 'owner'] as const) { + const frames: any[] = [] + const connection = new ProductConnection({ context: { ...context, role }, assertCurrent() {}, phase: () => 'ready', handlers, send: (frame) => { + frames.push(JSON.parse(frame)) + return true + }, close() {} }) + const send = (method: string, params: unknown) => connection.receive(JSON.stringify({ jsonrpc: '2.0', id: String(frames.length), method, params })) + await send('initialize', initialize) + expect(frames.at(-1).result.capabilities).toContain('questions.list') + expect(frames.at(-1).result.capabilities.includes('questions.answer')).toBe(role === 'owner') + await send('questions.list', scope) + expect(frames.at(-1).result.items).toMatchObject([{ id: request.id, questions: question.questions, status: 'pending' }]) + expect(frames.at(-1).result.items[0]).not.toHaveProperty('toolCallId') + expect(frames.at(-1).result.items[0]).not.toHaveProperty('rootRunId') + await send('questions.answer', f.command(request.id, 'answer', { answers })) + if (role === 'reader') { + expect(frames.at(-1).error.code).toBe('NOT_AUTHORIZED') + expect(f.countDeliveries()).toBe(0) + } + else { + expect(frames.at(-1).result.receipt).toMatchObject({ decision: 'accepted', target: { questionId: request.id }, result: { questionStatus: 'answered', questionDelivery: 'pending' } }) + expect(f.queries.views.question(request.id)).toMatchObject({ status: 'answered', response: { answers }, delivery: { state: 'delivered' } }) + expect(f.countDeliveries()).toBe(1) + await send('commands.get', { dataGeneration: context.dataGeneration, commandId: 'answer' }) + expect(frames.at(-1).result).toMatchObject({ lookup: 'decided', receipt: { decision: 'accepted' } }) + } + connection.close() + } + }) + + it('preserves Desktop question DTOs and business request IDs while answer, retry and cancel have separate operation identities', async () => { + const f = fixture() + const question = f.ask() + const commands = { admission: f.admission, questions: { execute: (method, input) => f.service.executeCommand(method, input, f.admission) } } as BuddyServiceCommands + const notifications = new Set<(value: { method: string, params: unknown }) => void>() + const connection = new ProductConnection({ + context: { serviceId: 'service', profileId: 'profile', dataGeneration: f.command(question.id, 'x').dataGeneration, serviceInstanceId: 'instance', connectionId: 'desktop-connection', clientSessionId: 'desktop-session', deviceId: 'desktop-local', ownerPrincipalId: 'owner', credentialId: 'credential', grantGeneration: '1', role: 'owner' }, + assertCurrent() {}, + phase: () => 'ready', + handlers: createProductCommandHandlers({ commands, assertOwner() {}, resolveQuote: () => { + throw new Error('Unexpected quote') + }, allowedActions: new Set() }), + send: (frame) => { + notifications.forEach(notify => notify({ method: 'product.frame', params: { frame } })) + return true + }, + close() {}, + }) + const gateway = new ProductDesktopGateway({ + state: { status: 'ready' }, + onNotification: (listener) => { + notifications.add(listener) + return () => { + notifications.delete(listener) + } + }, + onStateChange: () => () => {}, + restart: async () => {}, + request: async (method, params) => { + if (method === 'product.exchange') { + await connection.receive((params as { frame: string }).frame) + return { received: true } + } + if (method === 'questions.list') + return f.service.list(userQuestionRpc.list.input.parse(params)) + throw new Error(`Unexpected internal RPC: ${method}`) + }, + }, join(f.directory, 'desktop-outbox'), 'test') + try { + const input = { ...scope, requestId: question.id, answers } + const answered = userQuestionRpc.answer.response.parse(await gateway.request('questions.answer', input, { requestId: 'desktop-answer' })) + expect(answered).toMatchObject({ id: question.id, rootRunId: 'root', toolCallId: 'ask', status: 'answered', response: { answers }, delivery: { state: 'delivered' } }) + expect(await gateway.request('questions.answer', input, { requestId: 'desktop-answer' })).toEqual(answered) + expect(userQuestionRpc.retry.response.parse(await gateway.request('questions.retry', { ...scope, requestId: question.id }, { requestId: 'desktop-retry' }))).toEqual(answered) + const second = f.ask('decline') + const cancelled = userQuestionRpc.cancel.response.parse(await gateway.request('questions.cancel', { ...scope, requestId: second.id }, { requestId: 'desktop-cancel' })) + expect(cancelled).toMatchObject({ id: second.id, rootRunId: 'root', toolCallId: 'decline', status: 'cancelled', response: null, delivery: { state: 'delivered' } }) + const receipts = f.database.prepare('SELECT command_id, method, target_json FROM command_receipts ORDER BY rowid').all() + expect(receipts.map(receipt => [receipt.command_id, receipt.method, JSON.parse(String(receipt.target_json)).questionId])).toEqual([ + ['desktop-answer', 'questions.answer', question.id], + ['desktop-retry', 'questions.retry', question.id], + ['desktop-cancel', 'questions.cancel', second.id], + ]) + expect(f.countDeliveries()).toBe(2) + expect(f.queries.views.question(question.id)).not.toHaveProperty('rootRunId') + expect(f.queries.views.question(question.id)).not.toHaveProperty('toolCallId') + } + finally { + gateway.dispose() + connection.close() + } + }) + + it('commits the immutable answer and receipt together before publishing or delivering', async () => { + const f = fixture() + const request = f.ask() + const states: unknown[] = [] + const subscription = f.service.onDidCommit(() => { + states.push({ question: f.queries.views.question(request.id)?.status, receipt: f.remote.lookup({ dataGeneration: f.command(request.id, 'answer').dataGeneration, commandId: 'answer' }).lookup }) + }) + f.database.exec('CREATE TRIGGER fail_question_receipt BEFORE INSERT ON command_receipts BEGIN SELECT RAISE(ABORT, \'fixture receipt unavailable\'); END') + await expect(f.service.executeCommand('questions.answer', f.command(request.id, 'answer', { answers }), f.remote)).rejects.toThrow('fixture receipt unavailable') + expect(f.queries.views.question(request.id)).toMatchObject({ status: 'pending', response: null }) + expect(f.remote.lookup({ dataGeneration: f.command(request.id, 'answer').dataGeneration, commandId: 'answer' }).lookup).toBe('not_found') + expect(f.countDeliveries()).toBe(0) + expect(states).toEqual([]) + f.database.exec('DROP TRIGGER fail_question_receipt') + const result = await f.service.executeCommand('questions.answer', f.command(request.id, 'answer', { answers }), f.remote) + expect(result.receipt.decision).toBe('accepted') + expect(states.length).toBeGreaterThan(0) + expect(states.every(value => JSON.stringify(value) === JSON.stringify({ question: 'answered', receipt: 'decided' }))).toBe(true) + expect(f.countDeliveries()).toBe(1) + subscription.dispose() + }) + + it('decides two owner commands once and retains distinct durable receipts for the losing answer', async () => { + const f = fixture() + const request = f.ask() + const results = await Promise.all([ + f.service.executeCommand('questions.answer', f.command(request.id, 'local-answer', { answers }), f.admission), + f.service.executeCommand('questions.cancel', f.command(request.id, 'phone-cancel'), f.remote), + ]) + expect(results.map(result => result.receipt.decision)).toEqual(['accepted', 'rejected']) + expect(results[1]!.receipt).toMatchObject({ errorCode: 'QUESTION_CLOSED' }) + expect(f.countDeliveries()).toBe(1) + expect(f.database.prepare('SELECT COUNT(*) AS count FROM command_receipts').get()!.count).toBe(2) + await expect(f.service.executeCommand('questions.answer', f.command(request.id, 'local-answer', { answers: [{ ...answers[0]!, text: 'changed' }] }), f.admission)).rejects.toMatchObject({ code: 'COMMAND_ID_REUSED' }) + }) + + it('retains accepted answers separately from failed delivery and only an explicit retry redelivers', async () => { + const f = fixture() + const request = f.ask() + f.setDeliveryAvailable(false) + const envelope = f.command(request.id, 'answer', { answers }) + const original = await f.service.executeCommand('questions.answer', envelope, f.remote) + expect(original.receipt).toMatchObject({ decision: 'accepted', result: { questionStatus: 'answered', questionDelivery: 'pending' } }) + expect(f.queries.views.question(request.id)).toMatchObject({ status: 'answered', response: { answers }, delivery: { state: 'failed' } }) + f.setDeliveryAvailable(true) + expect(await f.service.executeCommand('questions.answer', envelope, f.remote)).toEqual({ created: false, receipt: original.receipt }) + expect(f.countDeliveries()).toBe(0) + const retry = f.command(request.id, 'retry') + const retried = await f.service.executeCommand('questions.retry', retry, f.remote) + expect(retried.receipt.decision).toBe('accepted') + expect(f.queries.views.question(request.id)).toMatchObject({ response: { answers }, delivery: { state: 'delivered' } }) + expect(f.countDeliveries()).toBe(1) + expect(await f.service.executeCommand('questions.retry', retry, f.remote)).toEqual({ created: false, receipt: retried.receipt }) + expect((await f.service.executeCommand('questions.retry', f.command(request.id, 'retry-again'), f.remote)).receipt).toMatchObject({ decision: 'accepted', result: { questionDelivery: 'delivered' } }) + expect(f.countDeliveries()).toBe(1) + }) + + it('recovers an interrupted delivery without replaying its durable receipt or duplicating the persisted mailbox source', async () => { + const f = fixture() + const request = f.ask() + const envelope = f.command(request.id, 'answer', { answers }) + const original = await f.service.executeCommand('questions.answer', envelope, f.remote) + const saved = f.service.list(scope)[0]! + f.database.prepare('UPDATE user_questions SET request_json = ? WHERE id = ?').run(JSON.stringify({ ...saved, delivery: { state: 'pending', targetRunId: null } }), request.id) + await f.service.dispose() + const recovered = new UserQuestionService(f.options) + const next = new CommandAdmissionService({ database: f.database, principal: { actorId: 'phone', assertAuthenticated() {} }, sourceInstanceId: 'next-instance' }) + cleanups.push(async () => { + await next.dispose() + await recovered.dispose() + }) + expect(recovered.list(scope)[0]).toMatchObject({ status: 'answered', response: { answers }, delivery: { state: 'failed' } }) + expect(await recovered.executeCommand('questions.answer', envelope, next)).toEqual({ created: false, receipt: original.receipt }) + expect(recovered.list(scope)[0]?.delivery?.state).toBe('failed') + await recovered.executeCommand('questions.retry', { ...f.command(request.id, 'retry'), expectedServiceInstanceId: 'next-instance' }, next) + expect(recovered.list(scope)[0]).toMatchObject({ response: { answers }, delivery: { state: 'delivered', targetRunId: 'root' } }) + expect(f.countDeliveries()).toBe(1) + }) + + it('rejects invalid, foreign, stale and closed requests without delivering and preserves stale receipts after task restoration', async () => { + const f = fixture() + const request = f.ask() + const invalid = await f.service.executeCommand('questions.answer', f.command(request.id, 'invalid', { answers: [{ ...answers[0]!, selectedOptionIds: ['missing'] }] }), f.remote) + expect(invalid.receipt).toMatchObject({ decision: 'rejected', errorCode: 'QUESTION_ANSWER_INVALID' }) + const foreign = await f.service.executeCommand('questions.cancel', f.command(request.id, 'foreign', { branchId: 'other' }), f.remote) + expect(foreign.receipt).toMatchObject({ decision: 'rejected', errorCode: 'QUESTION_NOT_FOUND' }) + f.database.prepare('UPDATE conversations SET deleted_at = ? WHERE id = ?').run('2026-11-09T00:00:00.000Z', scope.conversationId) + const staleEnvelope = f.command(request.id, 'stale', { answers }) + const stale = await f.service.executeCommand('questions.answer', staleEnvelope, f.remote) + expect(stale.receipt).toMatchObject({ decision: 'rejected', errorCode: 'QUESTION_STALE' }) + f.database.prepare('UPDATE conversations SET deleted_at = NULL WHERE id = ?').run(scope.conversationId) + expect(await f.service.executeCommand('questions.answer', staleEnvelope, f.remote)).toEqual({ created: false, receipt: stale.receipt }) + expect(f.countDeliveries()).toBe(0) + const cancelled = await f.service.executeCommand('questions.cancel', f.command(request.id, 'cancel'), f.remote) + expect(cancelled.receipt).toMatchObject({ decision: 'accepted', result: { questionStatus: 'cancelled' } }) + expect(f.queries.views.question(request.id)).toMatchObject({ status: 'cancelled', response: null, delivery: { state: 'delivered' } }) + expect((await f.service.executeCommand('questions.answer', f.command(request.id, 'late', { answers }), f.remote)).receipt).toMatchObject({ decision: 'rejected', errorCode: 'QUESTION_CLOSED' }) + expect(f.countDeliveries()).toBe(1) + }) + + it('pages public questions and publishes exact task-wide pending counts after durable decisions', async () => { + const f = fixture() + const first = f.ask('first') + const idle = { onDidCommit: () => ({ dispose() {} }) } + const sync = createProductSynchronization({ database: f.database, queries: f.queries, drafts: idle, metadata: idle, lifecycle: idle, questions: f.service, events: f.events }) + const frames: any[] = [] + const disconnect = sync.publisher.connect('phone', { send: (frame) => { + frames.push(JSON.parse(frame)) + return true + }, close() {} }) + const topic = 'questions:conversation:branch-conversation' + sync.authorizeTopic(topic) + try { + for (const name of [topic, 'taskIndex']) { + const subscription = sync.publisher.subscribe('phone', { topic: name, subscriptionId: name }) + subscription.activate() + sync.publisher.ack('phone', { subscriptionId: name, ...subscription.snapshot.cursor }) + if (subscription.snapshot.mode !== 'snapshot') + throw new Error('Initial question subscription must return a snapshot') + if (name === topic) + expect(subscription.snapshot.items).toEqual([productQuestionSchema.parse(f.queries.views.question(first.id))]) + else + expect(subscription.snapshot.items).toMatchObject([{ pendingQuestionCount: 1 }]) + } + const second = f.ask('second') + const page = productPageSchema(productQuestionSchema).parse(f.queries.read('questions.list', { ...scope, limit: 1, cursor: null })) + expect(page.items.map(item => item.id)).toEqual([first.id]) + expect(page.nextCursor).not.toBeNull() + expect(productPageSchema(productQuestionSchema).parse(f.queries.read('questions.list', { ...scope, limit: 1, cursor: page.nextCursor })).items.map(item => item.id)).toEqual([second.id]) + expect(() => f.queries.read('questions.list', { ...scope, branchId: 'foreign', limit: 1, cursor: page.nextCursor })).toThrow('CURSOR_EXPIRED') + expect(frames.some(frame => frame.params.changes?.some((change: any) => change.value?.pendingQuestionCount === 2))).toBe(true) + await f.service.executeCommand('questions.answer', f.command(second.id, 'answer', { answers }), f.remote) + expect(frames.some(frame => frame.params.changes?.some((change: any) => change.value?.id === second.id && change.value?.status === 'answered' && change.value?.delivery?.state === 'delivered'))).toBe(true) + expect(f.queries.task(scope.conversationId, true)).toMatchObject({ pendingQuestionCount: 1 }) + expect(() => f.queries.read('questions.list', { ...scope, limit: 1, cursor: page.nextCursor })).toThrow('CURSOR_EXPIRED') + f.database.prepare('UPDATE conversations SET active_branch_id = NULL WHERE id = ?').run(scope.conversationId) + expect(f.queries.task(scope.conversationId, true)).toMatchObject({ pendingQuestionCount: 1 }) + f.database.prepare('UPDATE conversations SET deleted_at = ? WHERE id = ?').run('2026-11-09T00:00:00.000Z', scope.conversationId) + expect(f.queries.read('questions.list', { ...scope, limit: 100, cursor: null })).toEqual({ items: [], nextCursor: null }) + expect(() => sync.authorizeTopic('questions:conversation:missing')).toThrow('OBJECT_DELETED') + } + finally { + disconnect() + sync.dispose() + } + }) +}) diff --git a/apps/buddy/service/src/questions/__tests__/UserQuestionService.spec.ts b/apps/buddy/service/src/questions/__tests__/UserQuestionService.spec.ts index 303ad9e47..049433a01 100644 --- a/apps/buddy/service/src/questions/__tests__/UserQuestionService.spec.ts +++ b/apps/buddy/service/src/questions/__tests__/UserQuestionService.spec.ts @@ -1,7 +1,5 @@ import type { UserQuestionRequest } from '../../../../shared/questions/userQuestionApi' import { afterEach, describe, expect, it } from 'vitest' -import { askUserInputSchema, validateUserQuestionResponse } from '../../../../shared/questions/userQuestionApi' -import { projectUserQuestionHistory } from '../../../../shared/questions/userQuestionHistory' import { createExecutionStorageFixture } from '../../storage/__tests__/executionFixture' import { createMailboxRepository } from '../../storage/mailboxRepository' import { UserQuestionDelivery } from '../UserQuestionDelivery' @@ -46,43 +44,6 @@ describe('user question lifecycle', () => { await expect(service.answer({ ...submit, answers: [{ ...response.answers[0]!, text: 'changed' }] })).rejects.toThrow('QUESTION_CLOSED') }) - it('keeps accepted answers immutable through delivery failure and retry', async () => { - let unavailable = true - const f = fixture(async () => { - if (unavailable) - throw new Error('offline') - return 'continuation' - }) - const request = f.service.ask('root', 'call', input, signal()) - const submit = { ...scope, requestId: request.id, ...response } - const waiting = f.service.wait('root', request.id, signal()) - const failed = await f.service.answer(submit) - expect(f.service.isWaiting('root')).toBe(true) - expect(failed).toMatchObject({ status: 'answered', response, delivery: { state: 'failed' } }) - const history = projectUserQuestionHistory(failed)! - expect(history.entries[0]?.answer).toBe('Large\nOriginal user text') - expect('questions' in history).toBe(false) - expect('response' in history).toBe(false) - expect(Object.isFrozen(history.entries[0])).toBe(true) - unavailable = false - const delivered = await f.service.retry({ ...scope, requestId: request.id }) - expect(delivered).toMatchObject({ response, delivery: { state: 'delivered', targetRunId: 'continuation' } }) - expect(history.delivery?.state).toBe('failed') - expect((await waiting).response).toEqual(response) - expect(f.service.isWaiting('root')).toBe(false) - }) - - it('rejects foreign scope, stale branches and invalid answers before resolving', async () => { - const f = fixture() - const request = f.service.ask('root', 'call', input, signal()) - const submit = { ...scope, requestId: request.id, ...response } - await expect(f.service.answer({ ...submit, branchId: 'foreign' })).rejects.toThrow('QUESTION_NOT_FOUND') - await expect(f.service.answer({ ...submit, answers: [{ ...response.answers[0]!, selectedOptionIds: ['foreign'] }] })).rejects.toThrow('QUESTION_ANSWER_INVALID') - expect(f.service.hasPending('root')).toBe(true) - f.database.prepare('UPDATE conversations SET active_branch_id = NULL WHERE id = ?').run(scope.conversationId) - await expect(f.service.answer(submit)).rejects.toThrow('QUESTION_STALE') - }) - it('retains unanswered async requests after a completed run and across restart', async () => { const f = fixture() const request = f.service.ask('root', 'call', input, signal()) @@ -147,14 +108,4 @@ describe('user question lifecycle', () => { expect(f.service.list(scope)[0]).toMatchObject({ response, delivery: { state: 'delivered', targetRunId: 'continued' } }) expect(f.database.prepare('SELECT * FROM composer_drafts').all()).toEqual(drafts) }) - - it('validates complete answers, custom text and multi-selection using stable IDs', () => { - expect(() => askUserInputSchema.parse({ questions: [input.questions[0], input.questions[0]] })).toThrow() - expect(() => validateUserQuestionResponse(input.questions, { answers: [] })).toThrow() - expect(() => validateUserQuestionResponse(input.questions, { answers: [{ questionId: 'scope', selectedOptionIds: ['a', 'b'], text: '' }] })).toThrow() - expect(validateUserQuestionResponse(input.questions, { answers: [{ questionId: 'scope', selectedOptionIds: [], text: ' My own answer ' }] }).answers[0]?.text).toBe(' My own answer ') - const multiple = [{ ...input.questions[0]!, kind: 'multiple' as const }] - expect(validateUserQuestionResponse(multiple, { answers: [{ questionId: 'scope', selectedOptionIds: ['b', 'a'], text: '' }] }).answers[0]?.selectedOptionIds).toEqual(['a', 'b']) - expect(() => validateUserQuestionResponse(multiple, { answers: [{ questionId: 'scope', selectedOptionIds: ['a', 'a'], text: '' }] })).toThrow() - }) }) diff --git a/apps/buddy/service/src/rpc/BuddyServiceRpcServer.ts b/apps/buddy/service/src/rpc/BuddyServiceRpcServer.ts index 3e6ac81f5..c55c4dbee 100644 --- a/apps/buddy/service/src/rpc/BuddyServiceRpcServer.ts +++ b/apps/buddy/service/src/rpc/BuddyServiceRpcServer.ts @@ -1,21 +1,18 @@ import type { ApplicationDiagnosticReporter } from '../../../shared/diagnostics/applicationDiagnostic' import type { RuntimeRequestHandler, RuntimeRpcPeerContract } from '../../../shared/runtime/rpcPeer' import type { BuddyServiceFailureCode } from '../../../shared/runtime/runtimeProtocol' -import process from 'node:process' -import { captureNodeCpuProfile } from '../../../platform/diagnostics/cpuProfile' import { RuntimeRpcPeer } from '../../../platform/ipc/runtimeRpcPeer' import { readDiagnosticErrorCode, safeDiagnosticReporter } from '../../../shared/diagnostics/applicationDiagnostic' -import { cpuProfileRequestSchema } from '../../../shared/diagnostics/performanceDiagnostic' import { isRoutineRpc } from '../../../shared/diagnostics/rpcDiagnosticPolicy' +import { Emitter } from '../../../shared/events/Emitter' import { BUDDY_SERVICE_PROTOCOL_VERSION, buddyServiceFailureNotificationSchema, - runtimeShutdownRequestSchema, } from '../../../shared/runtime/runtimeProtocol' import { diagnosticContext } from '../diagnostics/diagnosticContext' -import { BuddyServiceError } from './runtimeRequest' -const SERVICE_NAME = 'lexora-buddy-service' +import { registerServiceManagementRpc, SERVICE_NAME } from './registerServiceManagementRpc' +import { BuddyServiceError } from './runtimeRequest' export interface BuddyServiceParentPort { postMessage: (message: unknown) => void @@ -27,7 +24,7 @@ export interface CreateBuddyServiceOptions { recordDiagnostic?: ApplicationDiagnosticReporter announceReady?: boolean port: BuddyServiceParentPort - scheduleShutdown?: () => void + scheduleShutdown: () => void prepareIdleShutdown?: () => boolean onFatalError?: (error: Error) => void } @@ -35,6 +32,9 @@ export interface CreateBuddyServiceOptions { export class BuddyServiceRpcServer implements RuntimeRpcPeerContract { readonly #record: ApplicationDiagnosticReporter readonly #peer: RuntimeRpcPeer + readonly #closed = new Emitter(() => this.#record({ event: 'observer.failed', component: 'runtime.rpc', level: 'warn' })) + readonly onDidClose = this.#closed.event + #isClosed = false #ready = false #stopping = false @@ -42,7 +42,10 @@ export class BuddyServiceRpcServer implements RuntimeRpcPeerContract { this.#record = safeDiagnosticReporter(options.recordDiagnostic) const { port } = options this.#peer = new RuntimeRpcPeer({ - onFatalError: options.onFatalError, + onFatalError: (error) => { + this.close(error) + options.onFatalError?.(error) + }, transport: { postMessage: message => port.postMessage(message), subscribe(listener) { @@ -62,6 +65,10 @@ export class BuddyServiceRpcServer implements RuntimeRpcPeerContract { return this.#ready } + get closed(): boolean { + return this.#isClosed + } + markReady(): boolean { if (this.#ready) return false @@ -105,7 +112,12 @@ export class BuddyServiceRpcServer implements RuntimeRpcPeerContract { } close(reason: Error): void { + if (this.#isClosed) + return + this.#isClosed = true this.#peer.close(reason) + this.#closed.fire() + this.#closed.dispose() } } @@ -115,35 +127,7 @@ export function createBuddyService(options: CreateBuddyServiceOptions): BuddySer port: options.port, onFatalError: options.onFatalError, }) - let profiling = false - server.onRequest('diagnostics.profile', async (input, signal) => { - const request = cpuProfileRequestSchema.parse(input) - if (request.target !== 'runtime' || request.pid !== process.pid) - throw new Error('CPU_PROFILE_PROCESS_CHANGED') - if (profiling) - throw new Error('CPU_PROFILE_BUSY') - profiling = true - try { - return await captureNodeCpuProfile('runtime', signal) - } - finally { profiling = false } - }) - server.onRequest('runtime.status', () => ({ - name: SERVICE_NAME, - protocolVersion: BUDDY_SERVICE_PROTOCOL_VERSION, - ready: server.ready, - })) - server.onRequest('runtime.localState', () => ({ - status: server.ready ? 'ready' : 'starting', - })) - server.onRequest('runtime.shutdown', (params) => { - const { onlyIfIdle } = runtimeShutdownRequestSchema.parse(params) - if (onlyIfIdle && (!server.ready || !options.prepareIdleShutdown?.())) - return { accepted: false } - server.stopAcceptingRequests() - setImmediate(options.scheduleShutdown ?? (() => process.exit(0))) - return { accepted: true } - }) + registerServiceManagementRpc(server, options.scheduleShutdown, options.prepareIdleShutdown) if (options.announceReady !== false) notifyBuddyServiceReady(server) return server diff --git a/apps/buddy/service/src/rpc/ServiceClientConnections.ts b/apps/buddy/service/src/rpc/ServiceClientConnections.ts new file mode 100644 index 000000000..c31cbf3df --- /dev/null +++ b/apps/buddy/service/src/rpc/ServiceClientConnections.ts @@ -0,0 +1,100 @@ +import type { Event } from '../../../shared/events/Emitter' +import type { RuntimeRpcPeerContract } from '../../../shared/runtime/rpcPeer' +import type { RuntimeRequestRegistrar } from './runtimeRequest' + +export type ServiceClientRole = 'product' | 'management' +export interface ServiceClientPeer extends Pick { + readonly closed: boolean + readonly onDidClose: Event +} + +interface Connection { + role: ServiceClientRole + peer: ServiceClientPeer + stopRequests: () => void + dispose: () => void +} + +export class ServiceClientConnections { + readonly #connections = new Set() + readonly #assertAvailable: () => void + #accepting = true + + constructor(assertAvailable: () => void = () => {}) { + this.#assertAvailable = assertAvailable + } + + connect(role: ServiceClientRole, peer: ServiceClientPeer, register: (rpc: RuntimeRequestRegistrar, role: ServiceClientRole) => void): () => void { + this.#assertAvailable() + if (!this.#accepting || peer.closed) + throw new Error('SERVICE_CONNECTION_UNAVAILABLE') + const controller = new AbortController() + const registrations = new Set<() => void>() + let closed: ReturnType | undefined + const connection: Connection = { + role, + peer, + stopRequests() { + controller.abort(new Error('SERVICE_CONNECTION_CLOSED')) + for (const unregister of registrations) unregister() + registrations.clear() + }, + dispose: () => { + connection.stopRequests() + closed?.dispose() + this.#connections.delete(connection) + }, + } + closed = peer.onDidClose(connection.dispose) + this.#connections.add(connection) + try { + register({ onRequest: (method, handler) => { + controller.signal.throwIfAborted() + const unregister = peer.onRequest(method, async (params, signal, requestId) => { + const waiting = signal ? AbortSignal.any([signal, controller.signal]) : controller.signal + this.#assertAvailable() + waiting.throwIfAborted() + const result = await handler(params, waiting, requestId) + this.#assertAvailable() + waiting.throwIfAborted() + return result + }) + const dispose = () => { + if (registrations.delete(dispose)) + unregister() + } + registrations.add(dispose) + return dispose + } }, role) + return connection.dispose + } + catch (error) { + connection.dispose() + throw error + } + } + + notify(role: ServiceClientRole, method: string, params: unknown): void { + for (const connection of this.#connections) { + if (connection.role !== role) + continue + try { + connection.peer.notify(method, params) + } + catch { + connection.dispose() + connection.peer.close(new Error('SERVICE_NOTIFICATION_FAILED')) + } + } + } + + stopAccepting(): void { + this.#accepting = false + for (const connection of this.#connections) connection.stopRequests() + } + + dispose(): void { + this.stopAccepting() + for (const connection of this.#connections) connection.dispose() + } +} diff --git a/apps/buddy/service/src/rpc/__tests__/BuddyServiceRpcServer.spec.ts b/apps/buddy/service/src/rpc/__tests__/BuddyServiceRpcServer.spec.ts index 60e16ba72..4f6a803ca 100644 --- a/apps/buddy/service/src/rpc/__tests__/BuddyServiceRpcServer.spec.ts +++ b/apps/buddy/service/src/rpc/__tests__/BuddyServiceRpcServer.spec.ts @@ -7,8 +7,7 @@ import { diagnosticContext } from '../../diagnostics/diagnosticContext' import { HostCredentialStoreError } from '../../providers/HostCredentialStore' import { - createBuddyService, - notifyBuddyServiceFailure, + createBuddyService as createService, notifyBuddyServiceReady, } from '../BuddyServiceRpcServer' @@ -46,7 +45,6 @@ describe('runtimeRpcServer', () => { it.each([ ['CREDENTIAL_STORE_UNAVAILABLE', 'CREDENTIAL_STORE_UNAVAILABLE', 'CREDENTIAL_STORE_UNAVAILABLE'], - ['CREDENTIAL_STORE_FAILURE', 'CREDENTIAL_STORE_FAILURE', 'CREDENTIAL_STORE_FAILURE'], ['PRIVATE_DIAGNOSTIC', 'BUDDY_RUNTIME_REQUEST_FAILED', 'OPERATION_FAILED'], ])('reports a Pi-wrapped %s without exposing private error content', async (causeCode, wireCode, diagnosticCode) => { const port = new FakeParentPort() @@ -79,19 +77,6 @@ describe('runtimeRpcServer', () => { server.close(new Error('test completed')) }) - it('emits only the stable failure code before readiness', () => { - const port = new FakeParentPort() - const server = createBuddyService({ announceReady: false, port }) - - notifyBuddyServiceFailure(server, 'EVENT_LOG_CORRUPTED') - - expect(port.sent).toEqual([{ - jsonrpc: '2.0', - method: 'runtime.failed', - params: { code: 'EVENT_LOG_CORRUPTED' }, - }]) - }) - it('does not report readiness before startup recovery completes', async () => { const port = new FakeParentPort() const server = createBuddyService({ announceReady: false, port }) @@ -188,17 +173,8 @@ describe('runtimeRpcServer', () => { expect(stopped).toBe(true) server.close(new Error('test completed')) }) - - it('does not accept an idle-only shutdown without an available idle check', async () => { - const port = new FakeParentPort() - let stopped = false - const server = createBuddyService({ port, scheduleShutdown: () => { - stopped = true - } }) - port.receive({ jsonrpc: '2.0', id: 'unchecked', method: 'runtime.shutdown', params: { onlyIfIdle: true } }) - await new Promise(resolve => setImmediate(resolve)) - expect(port.sent.at(-1)).toMatchObject({ id: 'unchecked', result: { accepted: false } }) - expect(stopped).toBe(false) - server.close(new Error('test completed')) - }) }) + +function createBuddyService(options: Omit[0], 'scheduleShutdown'> & { scheduleShutdown?: () => void }) { + return createService({ scheduleShutdown() {}, ...options }) +} diff --git a/apps/buddy/service/src/rpc/__tests__/ServiceClientConnections.spec.ts b/apps/buddy/service/src/rpc/__tests__/ServiceClientConnections.spec.ts new file mode 100644 index 000000000..5a9be5202 --- /dev/null +++ b/apps/buddy/service/src/rpc/__tests__/ServiceClientConnections.spec.ts @@ -0,0 +1,84 @@ +import { describe, expect, it } from 'vitest' +import { StorageIsolation } from '../../storage/StorageIsolation' +import { ServiceClientConnections } from '../ServiceClientConnections' +import { createServiceConnection } from './serviceConnectionHarness' + +describe('serviceClientConnections', () => { + it('rejects late success and new clients after shared storage isolation', async () => { + const storage = new StorageIsolation() + const connections = new ServiceClientConnections(storage.assertAvailable) + const subscription = storage.onDidIsolate(() => connections.stopAccepting()) + const first = createServiceConnection() + const second = createServiceConnection() + const entered = Promise.withResolvers() + const completed = Promise.withResolvers() + connections.connect('product', first.server, rpc => rpc.onRequest('accept', async () => { + entered.resolve() + await completed.promise + return { accepted: true } + })) + const pending = expect(first.client.request('accept', {})).rejects.toMatchObject({ code: -32000 }) + try { + await entered.promise + storage.isolate(new Error('Commit acknowledgement lost')) + completed.resolve() + await pending + expect(first.received.some(message => 'result' in message)).toBe(false) + expect(() => connections.connect('management', second.server, () => {})).toThrow('Storage is isolated') + await expect(first.client.request('accept', {})).rejects.toMatchObject({ code: -32601 }) + } + finally { + completed.resolve() + subscription.dispose() + connections.dispose() + first.close() + second.close() + } + }) + + it('releases connection waits and subscriptions while leaving the other client usable', async () => { + const connections = new ServiceClientConnections() + const first = createServiceConnection() + const second = createServiceConnection() + const waiting = Promise.withResolvers() + let waitSignal: AbortSignal | undefined + connections.connect('product', first.server, rpc => rpc.onRequest('wait', async (_, signal) => { + waitSignal = signal + waiting.resolve() + await new Promise((_, reject) => signal!.addEventListener('abort', () => reject(signal!.reason), { once: true })) + })) + connections.connect('product', second.server, rpc => rpc.onRequest('read', () => ({ value: 'available' }))) + const pending = first.client.request('wait', {}) + const cancelled = expect(pending).rejects.toThrow('Connection closed') + await waiting.promise + first.close() + await cancelled + expect(waitSignal?.aborted).toBe(true) + connections.notify('product', 'changed', { revision: 1 }) + await expect(second.client.request('read', {})).resolves.toEqual({ value: 'available' }) + expect(first.received).toEqual([]) + expect(second.received).toContainEqual({ jsonrpc: '2.0', method: 'changed', params: { revision: 1 } }) + connections.dispose() + second.close() + }) + + it('keeps management challenges private and rolls back partial connection registration', async () => { + const connections = new ServiceClientConnections() + const product = createServiceConnection() + const management = createServiceConnection() + connections.connect('product', product.server, rpc => rpc.onRequest('read', () => ({ ok: true }))) + connections.connect('management', management.server, () => {}) + expect(() => connections.connect('management', product.server, (rpc) => { + rpc.onRequest('admin.write', () => ({ ok: true })) + rpc.onRequest('read', () => ({})) + })).toThrow('already registered') + await expect(product.client.request('admin.write', {})).rejects.toMatchObject({ code: -32601 }) + connections.notify('management', 'providers.authChallenge', { id: 'synthetic' }) + await expect(product.client.request('read', {})).resolves.toEqual({ ok: true }) + expect(product.received.some(message => 'method' in message && message.method === 'providers.authChallenge')).toBe(false) + expect(management.received).toEqual([{ jsonrpc: '2.0', method: 'providers.authChallenge', params: { id: 'synthetic' } }]) + connections.dispose() + product.close() + management.close() + }) +}) diff --git a/apps/buddy/service/src/rpc/__tests__/serviceConnectionHarness.ts b/apps/buddy/service/src/rpc/__tests__/serviceConnectionHarness.ts new file mode 100644 index 000000000..e231a190c --- /dev/null +++ b/apps/buddy/service/src/rpc/__tests__/serviceConnectionHarness.ts @@ -0,0 +1,33 @@ +import type { RuntimeWireMessage } from '../../../../shared/runtime/runtimeProtocol' +import { EventEmitter } from 'node:events' +import { RuntimeRpcPeer } from '../../../../platform/ipc/runtimeRpcPeer' +import { BuddyServiceRpcServer } from '../BuddyServiceRpcServer' + +export function createServiceConnection() { + const incoming = new EventEmitter() + const outgoing = new EventEmitter() + const received: RuntimeWireMessage[] = [] + const server = new BuddyServiceRpcServer({ port: { + postMessage(message) { + received.push(structuredClone(message) as RuntimeWireMessage) + queueMicrotask(() => outgoing.emit('message', message)) + }, + on: (event, listener) => incoming.on(event, listener), + off: (event, listener) => incoming.off(event, listener), + } }) + const client = new RuntimeRpcPeer({ transport: { + postMessage: message => queueMicrotask(() => incoming.emit('message', { data: message })), + subscribe(listener) { + outgoing.on('message', listener) + return () => outgoing.off('message', listener) + }, + } }) + server.onDidClose(() => client.close(new Error('Connection closed'))) + return { + server, + client, + received, + sendInvalid: () => incoming.emit('message', { data: { invalid: true } }), + close: () => server.close(new Error('Connection closed')), + } +} diff --git a/apps/buddy/service/src/rpc/candidateLegacyRegistrar.ts b/apps/buddy/service/src/rpc/candidateLegacyRegistrar.ts new file mode 100644 index 000000000..8385ab534 --- /dev/null +++ b/apps/buddy/service/src/rpc/candidateLegacyRegistrar.ts @@ -0,0 +1,41 @@ +import type { RuntimeRequestRegistrar } from './runtimeRequest' + +const queries = new Set([ + 'runs.get', + 'runs.list', + 'runs.listEvents', + 'runs.isBusy', + 'chat.queue.list', + 'composerDrafts.find', + 'composerDrafts.get', + 'composerDrafts.list', + 'conversations.get', + 'conversations.list', + 'conversations.listMembers', + 'conversations.getModelControls', + 'conversations.listBranches', + 'conversations.listMessages', + 'conversations.listTimeline', + 'conversations.listHistory', + 'conversations.getHistoryEntries', + 'taskMarks.list', + 'taskMarks.states', + 'approvals.list', + 'approvals.getReview', + 'questions.list', +]) + +export class ProductCommandRequiredError extends Error { + readonly code = 'METHOD_NOT_AVAILABLE' + + constructor() { + super('This operation requires the product command protocol') + this.name = 'ProductCommandRequiredError' + } +} + +export function candidateLegacyRegistrar(rpc: RuntimeRequestRegistrar): RuntimeRequestRegistrar { + return { + onRequest: (method, handler) => rpc.onRequest(method, queries.has(method) ? handler : () => { throw new ProductCommandRequiredError() }), + } +} diff --git a/apps/buddy/service/src/rpc/registerServiceManagementRpc.ts b/apps/buddy/service/src/rpc/registerServiceManagementRpc.ts new file mode 100644 index 000000000..334c08f3b --- /dev/null +++ b/apps/buddy/service/src/rpc/registerServiceManagementRpc.ts @@ -0,0 +1,39 @@ +import type { BuddyServiceRpcServer } from './BuddyServiceRpcServer' +import process from 'node:process' +import { captureNodeCpuProfile } from '../../../platform/diagnostics/cpuProfile' +import { cpuProfileRequestSchema } from '../../../shared/diagnostics/performanceDiagnostic' +import { BUDDY_SERVICE_PROTOCOL_VERSION, runtimeShutdownRequestSchema } from '../../../shared/runtime/runtimeProtocol' + +export const SERVICE_NAME = 'lexora-buddy-service' + +export function registerServiceManagementRpc(server: Pick, scheduleShutdown: () => void, prepareIdleShutdown?: () => boolean): void { + let profiling = false + server.onRequest('diagnostics.profile', async (input, signal) => { + const request = cpuProfileRequestSchema.parse(input) + if (request.target !== 'runtime' || request.pid !== process.pid) + throw new Error('CPU_PROFILE_PROCESS_CHANGED') + if (profiling) + throw new Error('CPU_PROFILE_BUSY') + profiling = true + try { + return await captureNodeCpuProfile('runtime', signal) + } + finally { profiling = false } + }) + server.onRequest('runtime.status', () => ({ + name: SERVICE_NAME, + protocolVersion: BUDDY_SERVICE_PROTOCOL_VERSION, + ready: server.ready, + })) + server.onRequest('runtime.localState', () => ({ + status: server.ready ? 'ready' : 'starting', + })) + server.onRequest('runtime.shutdown', (params) => { + const { onlyIfIdle } = runtimeShutdownRequestSchema.parse(params) + if (onlyIfIdle && (!server.ready || !prepareIdleShutdown?.())) + return { accepted: false } + server.stopAcceptingRequests() + setImmediate(scheduleShutdown) + return { accepted: true } + }) +} diff --git a/apps/buddy/service/src/runs/RunLifecycleService.ts b/apps/buddy/service/src/runs/RunLifecycleService.ts index f36b97b41..4f6f71e34 100644 --- a/apps/buddy/service/src/runs/RunLifecycleService.ts +++ b/apps/buddy/service/src/runs/RunLifecycleService.ts @@ -147,9 +147,11 @@ export class RunLifecycleService { } let run = this.#requireRun(input.runId) if (run.status !== status || run.completedAt !== input.completedAt || run.errorCode !== errorCode) { - if (!this.#repository.reconcileTerminal(input.runId, status, input.completedAt, errorCode)) + if (terminalEventPersisted || !this.#repository.reconcileTerminal(input.runId, status, input.completedAt, errorCode)) throw new BuddyAgentRunError('RUN_STATE_MISMATCH') run = this.#requireRun(input.runId) + if (run.status !== status || run.completedAt !== input.completedAt || run.errorCode !== errorCode) + throw new BuddyAgentRunError('RUN_STATE_MISMATCH') } if (!terminalEventPersisted) { this.#reconciled.fire(copyEventSnapshot({ diff --git a/apps/buddy/service/src/runs/__tests__/recoverSqliteRuns.spec.ts b/apps/buddy/service/src/runs/__tests__/recoverSqliteRuns.spec.ts new file mode 100644 index 000000000..104b9b46f --- /dev/null +++ b/apps/buddy/service/src/runs/__tests__/recoverSqliteRuns.spec.ts @@ -0,0 +1,319 @@ +import type { AssistantMessage, Usage } from '@earendil-works/pi-ai' +import type { DatabaseSync } from 'node:sqlite' +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { SessionManager } from '@earendil-works/pi-coding-agent' +import { afterEach, describe, expect, it } from 'vitest' +import { createBuddyUserContent } from '../../../../shared/conversation/buddyUserContent' +import { PreparedRunCommandService } from '../../chat/PreparedRunCommandService' +import { SqliteRunEventLog } from '../../events/SqliteRunEventLog' +import { sqliteRunFixtureTime as now, seedSqliteRun } from '../../storage/__tests__/sqliteRunFixture' +import { createApprovalRepository } from '../../storage/approvalRepository' +import { createComposerDraftRepository } from '../../storage/composerDraftRepository' +import { createConversationRepository } from '../../storage/conversationRepository' +import { openBuddyMigrationCandidate } from '../../storage/database' +import { createRunRepository } from '../../storage/runRepository' +import { createUsageRepository } from '../../storage/usageRepository' +import { recoverSqliteRuns } from '../recoverSqliteRuns' + +const databases: DatabaseSync[] = [] +const directories: string[] = [] +const logs: SqliteRunEventLog[] = [] + +afterEach(async () => { + for (const log of logs.splice(0)) + await log.close() + for (const database of databases.splice(0)) { + if (database.isOpen) + database.close() + } + for (const directory of directories.splice(0)) + rmSync(directory, { recursive: true, force: true }) +}) + +function fixture(ready = true) { + const root = mkdtempSync(join(tmpdir(), 'lexora-sqlite-recovery-')) + directories.push(root) + const databasePath = join(root, 'candidate.sqlite3') + const open = () => { + const database = openBuddyMigrationCandidate({ databasePath }) + databases.push(database) + return database + } + const database = open() + if (ready) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + const conversationsDirectory = join(root, 'conversations') + return { root, database, open, conversationsDirectory, recover: (database: DatabaseSync) => recoverSqliteRuns({ database, conversationsDirectory }) } +} + +function eventLog(database: DatabaseSync) { + const log = new SqliteRunEventLog({ database }) + logs.push(log) + return log +} + +function seedQueue(database: DatabaseSync, id: string, state: string) { + database.prepare(`INSERT INTO chat_queue (id, conversation_id, branch_id, request_id, request_fingerprint, prepared_json, state, created_at) + VALUES (?, 'task-1', 'branch-task-1', ?, 'fixture', ?, ?, ?)`).run(id, `request-${id}`, JSON.stringify({ prompt: 'Keep queued input', attachmentIds: ['retained-file'] }), state, now) +} + +function approval(id: string) { + return { + runId: 'run-1', + type: 'approval.requested', + createdAt: now, + payload: { + id, + runId: 'run-1', + toolCallId: `tool-${id}`, + kind: 'shell', + status: 'pending', + summary: 'Fixture approval', + payload: { card: 'shell', toolName: 'bash', command: 'fixture', allowForTurn: false }, + createdAt: now, + resolvedAt: null, + }, + } +} + +function accept(database: DatabaseSync) { + const drafts = createComposerDraftRepository(database) + drafts.open({ + draftId: 'draft-1', + initialContent: createBuddyUserContent('Keep input'), + initialExecutionConfig: { approvalPolicy: 'policy', executionProfile: 'workspace_write' }, + initialModelSelection: { modelId: 'fixture', providerId: 'fixture', reasoning: null, serviceTier: null }, + now, + scope: { kind: 'global' }, + }) + const owner = new PreparedRunCommandService({ database, sourceInstanceId: 'instance-1' }) + const command = { + dataGeneration: String(database.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation), + actorId: 'desktop-local', + commandId: 'command-1', + fingerprintVersion: 1, + fingerprint: 'fixture-intent', + } + const input = { + attachmentBindings: [], + branchId: 'branch-task-1', + conversationId: 'task-1', + createdAt: now, + approvalPolicy: 'policy' as const, + executionProfile: 'workspace_write' as const, + draft: { draftId: 'draft-1', expectedRevision: 0 }, + model: 'fixture', + spaceId: null, + provider: 'fixture', + runInput: { attachmentIds: [], contextItems: [], prompt: 'Keep input', reasoning: null, serviceTier: null }, + runId: 'run-1', + title: 'Fixture', + userMessageContent: { text: 'Keep input' }, + userMessageId: 'input-run-1', + } + const { receipt } = owner.start(command, input) + owner.dispose() + return { receipt, command, input } +} + +function piSession(f: ReturnType, runId: string, compact: boolean) { + const directory = join(f.conversationsDirectory, 'task-1', 'session', 'branch-task-1') + mkdirSync(directory, { recursive: true }) + const manager = SessionManager.create(f.root, directory) + const firstKeptEntryId = manager.appendMessage({ role: 'user', content: 'Recent context', timestamp: Date.now() }) + const usage: Usage = { input: 20, output: 5, cacheRead: 0, cacheWrite: 0, totalTokens: 25, cost: { input: 0.01, output: 0.02, cacheRead: 0, cacheWrite: 0, total: 0.03 } } + const assistant: AssistantMessage = { api: 'anthropic-messages', role: 'assistant', content: [{ type: 'text', text: 'Pi-only answer' }], model: 'fixture', provider: 'fixture', stopReason: 'stop', timestamp: Date.now(), usage } + manager.appendMessage(assistant) + if (compact) + manager.appendCompaction('Pi-only private summary', firstKeptEntryId, 1400, undefined, false, usage) + const file = manager.getSessionFile()! + f.database.prepare('UPDATE runs SET pi_session_file = ?, purpose = ? WHERE id = ?').run(file, compact ? 'conversation.compaction' : 'chat', runId) + return { file, bytes: readFileSync(file), firstKeptEntryId } +} + +describe('sqlite run restart recovery', () => { + it('closes interrupted runs and approvals, pauses queues, and keeps original receipts and independent files after reopen', async () => { + const f = fixture() + const accepted = accept(f.database) + seedSqliteRun(f.database, 'run-queued') + seedSqliteRun(f.database, 'run-terminal') + const events = eventLog(f.database) + await events.append({ runId: 'run-terminal', type: 'run.completed', payload: {}, createdAt: now }) + const terminal = createRunRepository(f.database).findById('run-terminal') + await events.appendBatch([ + { runId: 'run-1', type: 'run.started', payload: {}, createdAt: now }, + ...Array.from({ length: 602 }, (_, index) => ({ runId: 'run-1', type: 'message.delta', payload: { messageId: 'partial-1', delta: `${index},` }, createdAt: new Date(Date.parse(now) - index).toISOString() })), + { runId: 'run-1', type: 'tool.started', payload: { toolCallId: 'unknown-effect', toolName: 'bash' }, createdAt: now }, + { runId: 'run-1', type: 'future.fact', payload: { retained: true }, createdAt: now }, + approval('pending-1'), + approval('approved-1'), + { runId: 'run-1', type: 'approval.resolved', payload: { id: 'approved-1', status: 'approved', resolvedAt: now }, createdAt: now }, + ]) + for (const state of ['waiting', 'paused', 'sent', 'cancelled']) + seedQueue(f.database, `queue-${state}`, state) + const beforeQueues = f.database.prepare('SELECT * FROM chat_queue ORDER BY id').all() + const sourceDirectory = join(f.conversationsDirectory, 'task-1', 'events') + mkdirSync(sourceDirectory, { recursive: true }) + const legacy = join(sourceDirectory, 'run-1.jsonl') + writeFileSync(legacy, '{"type":"run.completed","runId":"run-1","sequence":999}\n{broken tail') + const legacyBytes = readFileSync(legacy) + const pi = piSession(f, 'run-1', false) + const externalResult = join(f.root, 'external-result.txt') + writeFileSync(externalResult, 'Previously performed effect; outcome unrecorded') + const originalDraft = createComposerDraftRepository(f.database).findById('draft-1') + f.database.exec(`CREATE TRIGGER reject_terminal_rewrite BEFORE UPDATE OF status ON runs + WHEN OLD.status IN ('completed', 'failed', 'cancelled') BEGIN SELECT RAISE(ABORT, 'terminal already projected'); END;`) + await events.close() + f.database.close() + + const database = f.open() + expect(await f.recover(database)).toBe(2) + const recovered = eventLog(database) + expect(createRunRepository(database).findById('run-1')).toMatchObject({ status: 'failed', errorCode: 'RUNTIME_RESTARTED' }) + expect(createRunRepository(database).findById('run-queued')).toMatchObject({ status: 'failed', errorCode: 'RUNTIME_RESTARTED' }) + expect(createRunRepository(database).findById('run-terminal')).toEqual(terminal) + expect(createConversationRepository(database).findMessageById('partial-1')?.content).toEqual({ state: 'interrupted', truncated: false, text: Array.from({ length: 602 }, (_, index) => `${index},`).join('') }) + expect(createApprovalRepository(database).findById('pending-1')?.status).toBe('cancelled') + expect(createApprovalRepository(database).findById('approved-1')?.status).toBe('approved') + expect(database.prepare('SELECT * FROM chat_queue ORDER BY id').all()).toEqual(beforeQueues.map(row => row.state === 'waiting' ? { ...row, state: 'paused', revision: Number(row.revision) + 1 } : row)) + expect(createComposerDraftRepository(database).findById('draft-1')).toEqual(originalDraft) + const history = await recovered.read('run-1') + expect(history.filter(event => event.type === 'message.delta')).toEqual([]) + expect(history.filter(event => event.type === 'tool.started')).toHaveLength(1) + expect(history.filter(event => event.type === 'tool.completed')).toEqual([]) + expect(history.find(event => event.type === 'future.fact')?.payload).toEqual({ retained: true }) + const owner = new PreparedRunCommandService({ database, sourceInstanceId: 'instance-2' }) + expect(owner.find(accepted.command)).toEqual(accepted.receipt) + expect(owner.start(accepted.command, accepted.input)).toEqual({ created: false, receipt: accepted.receipt }) + owner.dispose() + expect(await f.recover(database)).toBe(0) + expect(await recovered.read('run-1')).toEqual(history) + expect(database.prepare('SELECT count(*) AS count FROM runs').get()?.count).toBe(3) + expect(readFileSync(legacy)).toEqual(legacyBytes) + expect(readFileSync(pi.file)).toEqual(pi.bytes) + expect(readFileSync(externalResult, 'utf8')).toBe('Previously performed effect; outcome unrecorded') + expect(JSON.stringify(history)).not.toContain('Pi-only answer') + }) + + it('rolls back interrupted-message and terminal facts together on failure without SQL-only fallback', async () => { + const f = fixture() + seedSqliteRun(f.database, 'run-1') + const events = eventLog(f.database) + await events.appendBatch([ + { runId: 'run-1', type: 'run.started', payload: {}, createdAt: now }, + { runId: 'run-1', type: 'message.delta', payload: { messageId: 'partial', delta: 'Keep partial' }, createdAt: now }, + ]) + const before = await events.read('run-1') + f.database.exec(`CREATE TRIGGER reject_recovery BEFORE INSERT ON run_events + WHEN NEW.event_type = 'run.failed' BEGIN SELECT RAISE(ABORT, 'recovery unavailable'); END;`) + await expect(f.recover(f.database)).rejects.toMatchObject({ code: 'EVENT_STORAGE_FAILED', operation: 'append', cause: { errcode: 1811 } }) + expect(() => f.database.prepare('SELECT 1')).toThrow('Storage is isolated') + await events.close() + f.database.close() + const reopened = f.open() + expect(await eventLog(reopened).read('run-1')).toEqual(before) + expect(createRunRepository(reopened).findById('run-1')).toMatchObject({ status: 'running', errorCode: null }) + expect(createConversationRepository(reopened).findMessageById('partial')).toBeNull() + expect(reopened.prepare('SELECT next_event_sequence FROM runs').get()?.next_event_sequence).toBe(3) + reopened.exec('DROP TRIGGER reject_recovery') + expect(await f.recover(reopened)).toBe(1) + expect(createConversationRepository(reopened).findMessageById('partial')?.content).toMatchObject({ text: 'Keep partial', state: 'interrupted' }) + expect((await eventLog(reopened).read('run-1')).map(event => event.sequence)).toEqual([1, 3, 4]) + }) + + it('resumes partially committed approval cancellation without reviving approved history or dispatching paused queues', async () => { + const f = fixture() + seedSqliteRun(f.database, 'run-1') + const events = eventLog(f.database) + await events.appendBatch([approval('a'), approval('b')]) + seedQueue(f.database, 'queue-waiting', 'waiting') + f.database.exec(`CREATE TRIGGER reject_second_cancellation BEFORE INSERT ON run_events + WHEN NEW.event_type = 'approval.resolved' AND json_extract(NEW.payload_json, '$.id') = 'b' + BEGIN SELECT RAISE(ABORT, 'approval unavailable'); END;`) + await expect(f.recover(f.database)).rejects.toMatchObject({ code: 'EVENT_STORAGE_FAILED' }) + expect(() => f.database.prepare('SELECT 1')).toThrow('Storage is isolated') + await events.close() + f.database.close() + const database = f.open() + expect(createApprovalRepository(database).findById('a')?.status).toBe('cancelled') + expect(createApprovalRepository(database).findById('b')?.status).toBe('pending') + expect(createRunRepository(database).findById('run-1')?.status).toBe('queued') + expect(database.prepare('SELECT state FROM chat_queue').get()?.state).toBe('paused') + database.exec('DROP TRIGGER reject_second_cancellation') + expect(await f.recover(database)).toBe(1) + expect(createApprovalRepository(database).listPending()).toEqual([]) + expect((await eventLog(database).read('run-1')).filter(event => event.type === 'approval.resolved')).toHaveLength(2) + }) + + it('stops on unreadable authority without replacing it with empty or legacy history', async () => { + const f = fixture() + seedSqliteRun(f.database, 'run-1') + const events = eventLog(f.database) + await events.append({ runId: 'run-1', type: 'future.fact', payload: {}, createdAt: now }) + f.database.exec('UPDATE run_events SET format_version = 2') + const before = f.database.prepare('SELECT * FROM run_events').all() + await expect(f.recover(f.database)).rejects.toMatchObject({ code: 'EVENT_STORAGE_FAILED', operation: 'read' }) + expect(() => f.database.prepare('SELECT 1')).toThrow('Storage is isolated') + f.database.close() + const database = f.open() + expect(database.prepare('SELECT * FROM run_events').all()).toEqual(before) + expect(createRunRepository(database).findById('run-1')?.status).toBe('queued') + }) + + it('leaves a pending migration untouched before any recovery mutation', async () => { + const f = fixture(false) + seedSqliteRun(f.database, 'run-1') + seedQueue(f.database, 'queue-waiting', 'waiting') + await expect(f.recover(f.database)).rejects.toThrow('not ready') + expect(f.database.prepare('SELECT state FROM chat_queue').get()?.state).toBe('waiting') + expect(f.database.prepare('SELECT * FROM run_events').all()).toEqual([]) + expect(createRunRepository(f.database).findById('run-1')?.status).toBe('queued') + }) + + it.each([false, true])('recovers committed Pi compaction independently and deduplicates usage across an interrupted finalization (%s)', async (interrupt) => { + const f = fixture() + seedSqliteRun(f.database, 'run-1') + const events = eventLog(f.database) + await events.append({ runId: 'run-1', type: 'run.started', payload: {}, createdAt: now }) + const pi = piSession(f, 'run-1', true) + if (interrupt) { + f.database.exec(`CREATE TRIGGER interrupt_compaction BEFORE INSERT ON run_events + WHEN NEW.event_type = 'run.completed' BEGIN SELECT RAISE(ABORT, 'finalization unavailable'); END;`) + await expect(f.recover(f.database)).rejects.toMatchObject({ code: 'EVENT_STORAGE_FAILED' }) + expect(() => f.database.prepare('SELECT 1')).toThrow('Storage is isolated') + } + await events.close() + f.database.close() + const database = f.open() + if (interrupt) { + expect(createRunRepository(database).findById('run-1')?.status).toBe('running') + expect(createUsageRepository(database).listForRun('run-1')).toHaveLength(1) + database.exec('DROP TRIGGER interrupt_compaction') + } + expect(await f.recover(database)).toBe(1) + expect(createRunRepository(database).findById('run-1')).toMatchObject({ status: 'completed', errorCode: null }) + const history = await eventLog(database).read('run-1') + expect(history.map(event => event.type)).toEqual(['run.started', 'context.compaction.completed', 'usage.recorded', 'run.completed']) + expect(createUsageRepository(database).listForRun('run-1')).toMatchObject([{ sourceEntryId: `compaction:${pi.firstKeptEntryId}`, inputTokens: 20, outputTokens: 5, purpose: 'compaction' }]) + expect(JSON.stringify(history)).not.toContain('Pi-only private summary') + expect(readFileSync(pi.file)).toEqual(pi.bytes) + expect(await f.recover(database)).toBe(0) + expect(await eventLog(database).read('run-1')).toEqual(history) + }) + + it.each(['missing', 'unavailable'] as const)('does not rerun compaction when its Pi evidence is %s', async (kind) => { + const f = fixture() + seedSqliteRun(f.database, 'run-1') + const directory = join(f.conversationsDirectory, 'task-1', 'session', 'branch-task-1') + mkdirSync(directory, { recursive: true }) + const file = join(directory, 'session.jsonl') + if (kind === 'unavailable') + mkdirSync(file) + f.database.prepare('UPDATE runs SET purpose = ?, pi_session_file = ?').run('conversation.compaction', file) + expect(await f.recover(f.database)).toBe(1) + expect(createRunRepository(f.database).findById('run-1')).toMatchObject({ status: 'failed', errorCode: kind === 'missing' ? 'RUNTIME_RESTARTED' : 'SESSION_STORAGE_UNAVAILABLE' }) + expect((await eventLog(f.database).read('run-1')).map(event => event.type)).toEqual(['run.failed']) + }) +}) diff --git a/apps/buddy/service/src/runs/publicRun.ts b/apps/buddy/service/src/runs/publicRun.ts index c6e309ede..dcb6823b0 100644 --- a/apps/buddy/service/src/runs/publicRun.ts +++ b/apps/buddy/service/src/runs/publicRun.ts @@ -10,6 +10,7 @@ export function toPublicRun(run: RunRecord, reasoningLevel: string | null) { errorCode: run.errorCode, executionProfile: run.executionProfile, id: run.id, + generation: run.generation, modelId: run.model, providerId: run.provider, purpose: run.purpose, diff --git a/apps/buddy/service/src/runs/recoverSqliteRuns.ts b/apps/buddy/service/src/runs/recoverSqliteRuns.ts new file mode 100644 index 000000000..5cc11a1fd --- /dev/null +++ b/apps/buddy/service/src/runs/recoverSqliteRuns.ts @@ -0,0 +1,70 @@ +import type { DatabaseSync } from 'node:sqlite' +import { inspectCommittedPiCompaction } from '../agent/sessions/recovery/inspectCommittedPiCompaction' +import { SqliteRunEventLog } from '../events/SqliteRunEventLog' +import { createApprovalRepository } from '../storage/approvalRepository' +import { createChatQueueRepository } from '../storage/chatQueueRepository' +import { createConversationRepository } from '../storage/conversationRepository' +import { createRunRepository } from '../storage/runRepository' +import { requireSqliteAuthority } from '../storage/sqliteAuthority' +import { guardStorageDatabase } from '../storage/StorageIsolation' +import { withTransaction } from '../storage/transaction' +import { createUsageRepository } from '../storage/usageRepository' +import { UsageService } from '../usage/UsageService' +import { RunLifecycleService } from './RunLifecycleService' +import { RunRecoveryService } from './RunRecoveryService' + +export interface RecoverSqliteRunsOptions { + database: DatabaseSync + conversationsDirectory: string + captureInterruptedChanges?: (runId: string) => Promise +} + +export async function recoverSqliteRuns(options: RecoverSqliteRunsOptions): Promise { + const { database } = options + guardStorageDatabase(database) + const eventLog = new SqliteRunEventLog({ database }) + const runs = createRunRepository(database) + const approvals = createApprovalRepository(database) + const usage = new UsageService({ eventLog, repository: createUsageRepository(database) }) + const lifecycle = new RunLifecycleService({ eventLog, repository: runs }) + try { + withTransaction(database, () => { + requireSqliteAuthority(database) + createChatQueueRepository(database).pause() + }) + const recovery = new RunRecoveryService({ + conversations: createConversationRepository(database), + eventLog, + lifecycle, + repository: runs, + usage, + captureInterruptedChanges: options.captureInterruptedChanges, + cancelPendingApprovals: async () => { + const pending = approvals.listPending() + for (const approval of pending) { + await eventLog.append({ + runId: approval.runId, + type: 'approval.resolved', + payload: { id: approval.id, resolution: 'cancelled', status: 'cancelled', resolvedAt: new Date().toISOString() }, + }) + } + return pending.length + }, + inspectCommittedCompaction: run => inspectCommittedPiCompaction({ + conversationsDirectory: options.conversationsDirectory, + conversationId: run.conversationId, + branchId: run.branchId, + piSessionFile: run.piSessionFile!, + startedAt: run.startedAt, + }), + }) + const recovered = await recovery.recoverInterruptedRuns() + await eventLog.compactTerminalRuns() + return recovered + } + finally { + usage.dispose() + await lifecycle.dispose() + await eventLog.close() + } +} diff --git a/apps/buddy/service/src/sandbox/ShellSandboxClient.ts b/apps/buddy/service/src/sandbox/ShellSandboxClient.ts index 2056a1cb1..3633e40a0 100644 --- a/apps/buddy/service/src/sandbox/ShellSandboxClient.ts +++ b/apps/buddy/service/src/sandbox/ShellSandboxClient.ts @@ -31,7 +31,7 @@ interface PendingCommand { } export class ShellSandboxClient { - readonly #peer: RuntimeRpcPeerContract + readonly #peer: Omit readonly #pending = new Map() readonly #dispose: () => void readonly #changes = new Emitter(() => console.error('SANDBOX_CLIENT_OBSERVER_FAILED')) @@ -39,7 +39,7 @@ export class ShellSandboxClient { #revision = 0 #disposed = false - constructor(peer: RuntimeRpcPeerContract) { + constructor(peer: Omit) { this.#peer = peer const network = peer.onRequest('sandbox.network', async (params) => { const { requestId, ...target } = sandboxNetworkRequestSchema.parse(params) diff --git a/apps/buddy/service/src/skills/SkillService.ts b/apps/buddy/service/src/skills/SkillService.ts index dac6a66f0..47f024b45 100644 --- a/apps/buddy/service/src/skills/SkillService.ts +++ b/apps/buddy/service/src/skills/SkillService.ts @@ -1010,7 +1010,7 @@ export function formatBuddySkillPrompt(skill: BuddyMaterializedSkill): string { return [``, `References are relative to ${skill.baseDirectory}.`, '', skill.body, ''].join('\n') } -export function registerSkillServiceRpc(rpc: RuntimeRequestRegistrar, service: SkillService): () => void { +export function registerSkillCatalogRpc(rpc: RuntimeRequestRegistrar, service: SkillService): () => void { const stops = [ registerRuntimeRequest(rpc, skillsRpc.list, input => service.list(input.spaceId, input.metadataOnly, input.forceReload)), registerRuntimeRequest(rpc, skillsRpc.get, input => service.get(input.spaceId, input.id)), @@ -1018,6 +1018,12 @@ export function registerSkillServiceRpc(rpc: RuntimeRequestRegistrar, service: S registerRuntimeRequest(rpc, skillsRpc.readFile, input => service.readFile(input)), registerRuntimeRequest(rpc, skillsRpc.resolveImage, input => service.resolveImage(input)), registerRuntimeRequest(rpc, skillsRpc.locateFile, input => service.locateFile(input)), + ] + return () => stops.forEach(stop => stop()) +} + +export function registerSkillServiceRpc(rpc: RuntimeRequestRegistrar, service: SkillService): () => void { + const stops = [ registerRuntimeRequest(rpc, skillsRpc.preview, input => service.preview(input)), registerRuntimeRequest(rpc, skillsRpc.install, input => service.install(input)), registerRuntimeRequest(rpc, skillsRpc.discard, async (input) => { diff --git a/apps/buddy/service/src/spaces/__tests__/registerSpaceRpc.spec.ts b/apps/buddy/service/src/spaces/__tests__/registerSpaceRpc.spec.ts index 9d90f0cd7..e4d8ffd19 100644 --- a/apps/buddy/service/src/spaces/__tests__/registerSpaceRpc.spec.ts +++ b/apps/buddy/service/src/spaces/__tests__/registerSpaceRpc.spec.ts @@ -13,7 +13,7 @@ import { AutomationService } from '../../automations/AutomationService' import { createAutomationRepositories } from '../../storage/automationRepository' import { openBuddyDatabase } from '../../storage/database' import { createSpaceRepository } from '../../storage/spaceRepository' -import { registerSpaceRpc } from '../registerSpaceRpc' +import { registerSpaceQueries, registerSpaceRpc } from '../registerSpaceRpc' import { SpaceDependents } from '../SpaceDependents' import { SpaceService } from '../SpaceService' @@ -69,6 +69,7 @@ describe('registerSpaceRpc', () => { record: () => {}, }) registerSpaceRpc({ rpc: harness.rpc, service }) + registerSpaceQueries({ rpc: harness.rpc, service }) const created = await harness.invoke('spaces.create', { memoryScope: 'space_only', diff --git a/apps/buddy/service/src/spaces/registerSpaceRpc.ts b/apps/buddy/service/src/spaces/registerSpaceRpc.ts index 220fbd9c7..11675668d 100644 --- a/apps/buddy/service/src/spaces/registerSpaceRpc.ts +++ b/apps/buddy/service/src/spaces/registerSpaceRpc.ts @@ -19,6 +19,12 @@ export function registerSpaceRpc(options: RegisterSpaceRpcOptions): () => void { await options.service.delete(input.spaceId) return ok() })) + + return () => disposers.splice(0).forEach(dispose => dispose()) +} + +export function registerSpaceQueries(options: RegisterSpaceRpcOptions): () => void { + const disposers: Array<() => void> = [] disposers.push(registerRuntimeRequest(options.rpc, spacesRpc.list, (input) => { return options.service.list().slice(0, input.limit ?? 100) })) diff --git a/apps/buddy/service/src/startServiceRuntime.ts b/apps/buddy/service/src/startServiceRuntime.ts new file mode 100644 index 000000000..086c50254 --- /dev/null +++ b/apps/buddy/service/src/startServiceRuntime.ts @@ -0,0 +1,171 @@ +import type { BuddyServiceFailureCode } from '../../shared/runtime/runtimeProtocol' +import type { RunEventLogPort } from './events/RunEventPorts' +import type { RuntimeProxy } from './network/runtimeNetwork' +import type { RuntimePlatform } from './platform/RuntimePlatform' +import { randomUUID } from 'node:crypto' +import { dirname, join } from 'node:path' +import { readOrCreateProductIdentity } from '../../platform/storage/productIdentity' +import { APPLICATION_DIAGNOSTIC_METHOD, readDiagnosticErrorCode } from '../../shared/diagnostics/applicationDiagnostic' +import { ServiceHost } from '../../shared/lifecycle/ServiceHost' +import { SERVICE_LIFECYCLE_METHOD } from '../../shared/lifecycle/serviceLifecycle' +import { ServiceLifecycleSource } from '../../shared/lifecycle/ServiceLifecycleSource' +import { ApplicationEvents } from '../../shared/observability/ApplicationEvents' +import { observeLifecycleDiagnostics } from '../../shared/observability/lifecycleDiagnostics' +import { buddyServiceFailureCodeSchema } from '../../shared/runtime/runtimeProtocol' +import { startBuddyService } from './BuddyService' +import { DiagnosticForwarder } from './diagnostics/DiagnosticForwarder' +import { createRunEventLog } from './events/createRunEventLog' +import { RunEventLogFatalError } from './events/RunEventFailure' +import { SqliteRunEventLog } from './events/SqliteRunEventLog' +import { RuntimeNetwork } from './network/runtimeNetwork' +import { openBuddyDatabase, openBuddySqliteDatabase, resolveBuddyDatabasePath } from './storage/database' +import { guardStorageDatabase, StorageIsolatedError } from './storage/StorageIsolation' + +export interface ServiceRuntimeOptions { + events?: ApplicationEvents + buddyHome: string + serviceInstanceId?: string + builtinSkillsDirectories: readonly string[] + proxy: RuntimeProxy + forceEventRecovery?: boolean + storageAuthority?: 'legacy_jsonl' | 'sqlite' + platform: RuntimePlatform + notify: (method: string, params: unknown) => void + onFatalFailure: (error: unknown) => void +} + +export async function startServiceRuntime(options: ServiceRuntimeOptions) { + const { buddyHome } = options + const serviceInstanceId = options.serviceInstanceId ?? randomUUID() + const network = new RuntimeNetwork() + let database: ReturnType | null = null + const events = options.events ?? new ApplicationEvents() + const diagnostics = new DiagnosticForwarder(event => options.notify(APPLICATION_DIAGNOSTIC_METHOD, event)) + events.subscribe(event => diagnostics.record(event)) + const record = events.publish + const lifecycle = new ServiceLifecycleSource(() => record({ event: 'observer.failed', component: 'runtime.lifecycle', level: 'warn' })) + const host = new ServiceHost(lifecycle) + const stopLifecycleDiagnostics = observeLifecycleDiagnostics(host.lifecycle, events) + const lifecycleDelivery = host.lifecycle.onDidChange(change => options.notify(SERVICE_LIFECYCLE_METHOD, change)) + let isDatabaseClosed = false + const closeDatabase = () => { + if (isDatabaseClosed || !database) + return + isDatabaseClosed = true + database.close() + } + let serviceHandle: Awaited> | null = null + let eventLog: RunEventLogPort | null = null + let stopping: Promise | undefined + const dispose = () => stopping ??= (async () => { + record({ event: 'service.stopping', level: 'info', component: 'local_service' }) + try { + await host.stop() + record({ event: 'service.stopped', level: 'info', component: 'runtime.service' }) + } + catch (error) { + record({ event: 'service.stop_failed', level: 'error', component: 'runtime.service' }) + throw error + } + finally { + lifecycleDelivery.dispose() + stopLifecycleDiagnostics() + diagnostics.dispose() + closeDatabase() + } + })() + try { + await host.start('runtime.network', async ({ defer }) => { + defer(() => network.dispose()) + await network.replace(options.proxy) + }) + const openedDatabase = await host.start('runtime.database', ({ defer }) => { + database = (options.storageAuthority === 'sqlite' ? openBuddySqliteDatabase : openBuddyDatabase)({ databasePath: resolveBuddyDatabasePath(buddyHome) }) + defer(closeDatabase) + const isolation = guardStorageDatabase(database) + const subscription = isolation.onDidIsolate(error => options.onFatalFailure(error)) + defer(() => subscription.dispose()) + return database + }) + eventLog = await host.start('runtime.event_log', ({ defer }) => { + if (options.storageAuthority === 'sqlite') { + const log = new SqliteRunEventLog({ database: openedDatabase, onFatalFailure: options.onFatalFailure, onObserverError: () => record({ event: 'run.observer_failed', level: 'warn' }) }) + defer(() => log.close()) + return log + } + const log = createRunEventLog({ + conversationsDirectory: join(buddyHome, 'conversations'), + database: openedDatabase, + onObserverError: error => record({ event: 'run.observer_failed', level: 'warn', errorCode: readDiagnosticErrorCode(error) }), + onMaintenance: (summary) => { + if (!summary.checked && !summary.failed) + return + record({ event: 'run.maintenance.checked', level: 'info', durationMs: summary.durationMs, count: summary.checked }) + record({ event: 'run.maintenance.removed', level: 'info', count: summary.removed }) + if (summary.failed) + record({ event: 'run.maintenance.failed', level: 'warn', count: summary.failed }) + }, + onRecovery: (summary) => { + record({ event: 'run.recovery.enumerated', level: 'info', durationMs: summary.enumerationMs, count: summary.scanned }) + record({ event: 'run.recovery.inspected', level: 'info', durationMs: summary.inspectionMs, count: summary.scanned }) + record({ event: 'run.recovery.read', level: 'info', durationMs: summary.readMs, count: summary.events }) + record({ event: 'run.recovery.projected', level: 'info', durationMs: summary.projectionMs, count: summary.rebuilt }) + record({ event: 'run.recovery.skipped', level: 'info', count: summary.skipped }) + record({ event: 'run.recovery.finished', level: summary.failed ? 'error' : 'info', durationMs: summary.durationMs, count: summary.failed }) + }, + onRunQuarantined: failure => record({ event: 'run.recovery.quarantined', level: 'error', runId: failure.runId, ...(failure.conversationId ? { conversationId: failure.conversationId } : {}), errorCode: failure.code }), + onFatalFailure: (error) => { + record({ event: 'run.storage_failed', level: 'error', runId: error.runId, errorCode: error.code }) + options.onFatalFailure(error) + }, + }) + defer(() => log.close()) + return log + }, ['runtime.database']) + await host.step('runtime.event_replay', ({ reportProgress }) => eventLog!.recoverAll({ + force: options.forceEventRecovery, + onProgress: reportProgress, + })) + await host.start('runtime.services', async ({ defer }) => { + serviceHandle = await startBuddyService({ + buddyHome, + serviceInstanceId, + ...(options.storageAuthority === 'sqlite' ? { productIdentity: await readOrCreateProductIdentity(dirname(buddyHome)) } : {}), + builtinSkillsDirectories: options.builtinSkillsDirectories, + database: openedDatabase, + eventLog: eventLog!, + platform: options.platform, + events, + lifecycle, + }) + defer(() => serviceHandle!.dispose()) + }) + return { + ...serviceHandle!, + async suspendPlatform(reason?: string) { + const cleanup = serviceHandle!.suspendPlatform(reason) + await Promise.all([network.invalidate(), cleanup]) + }, + async resumePlatform(proxy: RuntimeProxy) { + await network.replace(proxy) + await serviceHandle!.resumePlatform() + }, + dispose, + } + } + catch (error) { + await dispose().catch(() => {}) + throw error + } +} + +export function readBuddyServiceFailureCode(error: unknown): BuddyServiceFailureCode { + if (error instanceof StorageIsolatedError) + return 'EVENT_STORAGE_FAILED' + if (error instanceof RunEventLogFatalError) { + const parsed = buddyServiceFailureCodeSchema.safeParse(error.code) + if (parsed.success) + return parsed.data + } + return 'RUNTIME_START_FAILED' +} diff --git a/apps/buddy/service/src/storage/ChatControlCommandRepository.ts b/apps/buddy/service/src/storage/ChatControlCommandRepository.ts new file mode 100644 index 000000000..feaa5a38b --- /dev/null +++ b/apps/buddy/service/src/storage/ChatControlCommandRepository.ts @@ -0,0 +1,132 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { LocalChatQueueTarget } from '../../../shared/conversation/chatQueueApi' +import type { CommandRejection } from './CommandCommitter' +import { CommandAdmissionError } from '../commands/CommandAdmissionError' +import { createChatQueueRepository } from './chatQueueRepository' +import { createExecutionRepository } from './executionRepository' +import { ExecutionResourceQueries } from './ExecutionResourceQueries' +import { requireSqliteAuthority } from './sqliteAuthority' +import { withTransaction } from './transaction' + +export interface QueueCommandTarget extends LocalChatQueueTarget { + expectedRevision: number +} + +export interface RunCommandTarget { + runId: string + expectedRunGeneration: number +} + +export class ChatControlConflictError extends Error { + readonly code: CommandRejection['errorCode'] + + constructor(code: CommandRejection['errorCode']) { + super(code) + this.code = code + this.name = 'ChatControlConflictError' + } +} + +export class ChatControlCommandRepository { + readonly #database: DatabaseSync + readonly #sourceInstanceId: string + readonly #queue: ReturnType + readonly #executions: ReturnType + + constructor(database: DatabaseSync, sourceInstanceId: string) { + requireSqliteAuthority(database) + this.#database = database + this.#sourceInstanceId = sourceInstanceId + this.#queue = createChatQueueRepository(database) + this.#executions = createExecutionRepository(database) + } + + assertAccepting(): void { + if (this.#database.prepare('SELECT 1 FROM service_admission_gates WHERE source_instance_id = ?').get(this.#sourceInstanceId)) + throw new CommandAdmissionError('SERVICE_STOPPING') + } + + queueTarget(target: LocalChatQueueTarget): QueueCommandTarget | null { + const row = this.#database.prepare(`SELECT q.revision FROM chat_queue q JOIN conversations c ON c.id = q.conversation_id + WHERE q.id = ? AND q.conversation_id = ? AND q.branch_id = ? AND q.state IN ('waiting', 'paused') + AND c.deleted_at IS NULL`).get(target.id, target.conversationId, target.branchId) + return row ? { ...target, expectedRevision: Number(row.revision) } : null + } + + assertQueue(target: QueueCommandTarget): void { + if (this.queueTarget(target)?.expectedRevision !== target.expectedRevision) + throw new ChatControlConflictError('QUEUE_CHANGED') + } + + withQueue(target: QueueCommandTarget, operation: () => T): T { + return withTransaction(this.#database, () => { + this.assertAccepting() + this.assertQueue(target) + const result = operation() + if (result && typeof result === 'object' && 'then' in result) + throw new TypeError('Queue commits must be synchronous') + return result as T & (T extends PromiseLike ? never : unknown) + }) + } + + cancelQueue(target: QueueCommandTarget): void { + this.withQueue(target, () => { + if (!this.#queue.cancel(target)) + throw new ChatControlConflictError('QUEUE_CHANGED') + }) + } + + assertRun(target: RunCommandTarget, executable = false) { + const current = this.#executions.find(target.runId) + if (!current || current.run.generation !== target.expectedRunGeneration) + throw new ChatControlConflictError('SOURCE_CHANGED') + if (executable && (current.cancelRequestedAt || !['working', 'waiting'].includes(current.phase) + || current.run.status !== 'running')) { + throw new ChatControlConflictError('INPUT_DELIVERY_TARGET_UNAVAILABLE') + } + return current + } + + cancelRun(target: RunCommandTarget) { + return withTransaction(this.#database, () => { + this.assertAccepting() + const current = this.assertRun(target) + const requested = current.phase !== 'closed' + if (requested) + this.#executions.requestCancel(target.runId, new Date().toISOString()) + return { + target: { runId: target.runId, runGeneration: target.expectedRunGeneration, conversationId: current.run.conversationId, branchId: current.run.branchId }, + result: { cancelRequested: requested, executionResources: new ExecutionResourceQueries(this.#database).subtree(target.runId) }, + } + }) + } + + stopService(input: { targetServiceInstanceId: string, scope: 'stop_service' | 'keep_service' }, commandId: string) { + return withTransaction(this.#database, () => { + if (input.targetServiceInstanceId !== this.#sourceInstanceId) + throw new CommandAdmissionError('SERVICE_INSTANCE_CHANGED') + const runIds: string[] = [] + if (input.scope === 'stop_service') { + const now = new Date().toISOString() + this.#database.prepare('INSERT INTO service_admission_gates (source_instance_id, stopped_at, command_id) VALUES (?, ?, ?)').run(this.#sourceInstanceId, now, commandId) + const roots = this.#database.prepare('SELECT r.id FROM runs r JOIN execution_state e ON e.run_id = r.id WHERE r.parent_run_id IS NULL AND e.phase <> \'closed\'').all() + for (const root of roots) { + this.#executions.requestCancel(String(root.id), now) + runIds.push(String(root.id)) + } + this.#queue.pause() + } + return { target: { serviceInstanceId: this.#sourceInstanceId }, result: { scope: input.scope, runIds, executionResources: new ExecutionResourceQueries(this.#database).service() } } + }) + } + + recordDelivery(queueId: string, runId: string): void { + this.#database.prepare('INSERT INTO chat_queue_deliveries (queue_id, run_id, stage, committed_at) VALUES (?, ?, \'committed\', ?)').run(queueId, runId, new Date().toISOString()) + } + + observeDelivery(queueId: string, stage: 'submitted' | 'unavailable' | 'unknown'): void { + withTransaction(this.#database, () => { + this.#database.prepare('UPDATE chat_queue_deliveries SET stage = ?, observed_at = ? WHERE queue_id = ? AND stage = \'committed\'').run(stage, new Date().toISOString(), queueId) + }) + } +} diff --git a/apps/buddy/service/src/storage/CommandCommitter.ts b/apps/buddy/service/src/storage/CommandCommitter.ts new file mode 100644 index 000000000..b1d99d13c --- /dev/null +++ b/apps/buddy/service/src/storage/CommandCommitter.ts @@ -0,0 +1,146 @@ +import type { DatabaseSync } from 'node:sqlite' +import { z } from 'zod' +import { copyEventSnapshot } from '../../../shared/events/eventSnapshot' +import { CommandAdmissionError } from '../commands/CommandAdmissionError' +import { requireSqliteAuthority } from './sqliteAuthority' +import { getStorageIsolation } from './StorageIsolation' +import { assertCommittedRead, withTransaction } from './transaction' + +const objectSchema = z.record(z.string(), z.json()) +const commandSchema = z.object({ + dataGeneration: z.string().min(1), + actorId: z.string().min(1), + commandId: z.string().min(1), + method: z.string().min(1), + fingerprintVersion: z.number().int().positive(), + fingerprint: z.string().min(1), + sourceInstanceId: z.string().min(1), +}).strict() +const receiptSchema = commandSchema.extend({ + decision: z.literal('accepted'), + target: objectSchema, + result: objectSchema, + decidedAt: z.iso.datetime(), +}).or(commandSchema.extend({ + decision: z.literal('rejected'), + target: objectSchema, + result: objectSchema, + errorCode: z.string().min(1), + decidedAt: z.iso.datetime(), +})) + +export type StorageCommand = z.infer +export type CommandReceipt = z.infer +export type CommandDecision = Pick +type ReceiptSnapshot = Receipt extends unknown + ? Readonly> & { readonly target: Readonly, readonly result: Readonly } + : never +export type CommandReceiptSnapshot = ReceiptSnapshot + +const rejectionSchema = z.object({ + errorCode: z.enum(['DRAFT_CONFLICT', 'SOURCE_CHANGED', 'QUEUE_CHANGED', 'REVIEW_CHANGED', 'APPROVAL_ALREADY_RESOLVED', 'QUESTION_CLOSED', 'QUESTION_STALE', 'QUESTION_NOT_FOUND', 'QUESTION_ANSWER_INVALID', 'RESOURCE_UNAVAILABLE', 'OBJECT_DELETED', 'DESKTOP_UNAVAILABLE', 'INPUT_DELIVERY_TARGET_UNAVAILABLE', 'INPUT_DELIVERY_INCOMPATIBLE']), + target: objectSchema, + result: objectSchema, +}).strict() + +const decisionSchema = z.object({ decision: z.literal('accepted'), target: objectSchema, result: objectSchema }).strict().or(rejectionSchema.extend({ decision: z.literal('rejected') })) + +export type CommandRejection = z.infer +export type CommandDecisionOutcome = CommandDecision & ({ decision: 'accepted' } | ({ decision: 'rejected' } & Pick)) + +export function assertSameCommand(receipt: Pick, intent: Pick): void { + if (receipt.method !== intent.method || receipt.fingerprintVersion !== intent.fingerprintVersion || receipt.fingerprint !== intent.fingerprint) + throw new CommandAdmissionError('COMMAND_ID_REUSED', 'Command identity conflicts with its recorded intent') +} + +export class CommandCommitter { + readonly #database: DatabaseSync + + constructor(database: DatabaseSync) { + requireSqliteAuthority(database) + this.#database = database + } + + accept(command: StorageCommand, operation: () => CommandDecision) { + return this.#commit(command, () => ({ ...operation(), decision: 'accepted' })) + } + + reject(command: StorageCommand, rejection: CommandRejection) { + const decision = rejectionSchema.parse(rejection) + return this.#commit(command, () => ({ ...decision, decision: 'rejected' })) + } + + decide(command: StorageCommand, operation: () => CommandDecisionOutcome) { + return this.#commit(command, operation) + } + + #commit(command: StorageCommand, operation: () => CommandDecisionOutcome) { + return withTransaction(this.#database, () => { + const intent = commandSchema.parse(command) + this.#validateIdentity(intent) + const previous = this.#find(intent) + if (previous) { + assertSameCommand(previous, intent) + return { created: false, receipt: previous } + } + const decision = decisionSchema.parse(withTransaction(this.#database, operation)) + const receipt = receiptSchema.parse({ + ...intent, + ...decision, + decidedAt: new Date().toISOString(), + }) + this.#database.prepare(` + INSERT INTO command_receipts ( + data_generation, actor_id, command_id, method, fingerprint_version, + fingerprint, source_instance_id, decision, target_json, result_json, error_code, decided_at + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + `).run(receipt.dataGeneration, receipt.actorId, receipt.commandId, receipt.method, receipt.fingerprintVersion, receipt.fingerprint, receipt.sourceInstanceId, receipt.decision, JSON.stringify(receipt.target), JSON.stringify(receipt.result), receipt.decision === 'rejected' ? receipt.errorCode : null, receipt.decidedAt) + return { created: true, receipt: copyEventSnapshot(receipt) as unknown as CommandReceiptSnapshot } + }) + } + + find(identity: Pick) { + assertCommittedRead(this.#database) + this.#validateIdentity(identity) + return this.#find(identity) + } + + #validateIdentity(identity: Pick): void { + if (requireSqliteAuthority(this.#database).dataGeneration !== identity.dataGeneration) + throw new CommandAdmissionError('DATA_GENERATION_CHANGED', 'Command data generation is not current') + if (!this.#database.prepare('SELECT 1 FROM storage_actors WHERE id = ? AND state = \'active\'').get(identity.actorId)) + throw new CommandAdmissionError('COMMAND_ACTOR_UNAVAILABLE', 'Command actor is not active') + } + + #find(identity: Pick) { + try { + return this.#readReceipt(identity) + } + catch (error) { + getStorageIsolation(this.#database).isolate(error) + throw error + } + } + + #readReceipt(identity: Pick) { + const row = this.#database.prepare(` + SELECT * FROM command_receipts WHERE data_generation = ? AND actor_id = ? AND command_id = ? + `).get(identity.dataGeneration, identity.actorId, identity.commandId) + if (!row) + return null + return copyEventSnapshot(receiptSchema.parse({ + dataGeneration: row.data_generation, + actorId: row.actor_id, + commandId: row.command_id, + method: row.method, + fingerprintVersion: row.fingerprint_version, + fingerprint: row.fingerprint, + sourceInstanceId: row.source_instance_id, + decision: row.decision, + target: JSON.parse(String(row.target_json)), + result: JSON.parse(String(row.result_json)), + ...(row.error_code === null ? {} : { errorCode: row.error_code }), + decidedAt: row.decided_at, + })) as unknown as CommandReceiptSnapshot + } +} diff --git a/apps/buddy/service/src/storage/ConversationCommandRepository.ts b/apps/buddy/service/src/storage/ConversationCommandRepository.ts new file mode 100644 index 000000000..2970e9819 --- /dev/null +++ b/apps/buddy/service/src/storage/ConversationCommandRepository.ts @@ -0,0 +1,88 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { ConversationModelSelection } from './conversationRecord' +import { canonicalCommandJson } from '../commands/commandIntent' +import { ChatControlConflictError } from './ChatControlCommandRepository' +import { createRunInputRepository } from './runInputRepository' +import { createRunRepository } from './runRepository' +import { requireSqliteAuthority } from './sqliteAuthority' +import { createTaskMarkRepository } from './taskMarkRepository' + +export class ConversationCommandRepository { + readonly #database: DatabaseSync + + constructor(database: DatabaseSync) { + requireSqliteAuthority(database) + this.#database = database + } + + conversationRevision(conversationId: string, title = false): number { + const row = this.#database.prepare('SELECT * FROM conversations WHERE id = ? AND deleted_at IS NULL').get(conversationId) + if (!row) + throw new ChatControlConflictError('OBJECT_DELETED') + if (row.archived_at) + throw new ChatControlConflictError('SOURCE_CHANGED') + return Number(title ? row.title_revision : row.metadata_revision) + } + + assertConversation(conversationId: string, expectedRevision: number, title = false): void { + if (this.conversationRevision(conversationId, title) !== expectedRevision) + throw new ChatControlConflictError('SOURCE_CHANGED') + } + + assertRunModel(input: { conversationId: string, actorId?: string, targetRunId: string, expectedRunGeneration: number, expectedRunModelSelection: ConversationModelSelection | null }): void { + const run = createRunRepository(this.#database).findById(input.targetRunId) + if (!run || run.conversationId !== input.conversationId || (input.actorId && run.buddyId !== input.actorId) + || run.generation !== input.expectedRunGeneration || !['queued', 'running'].includes(run.status) + || this.#database.prepare('SELECT cancel_requested_at FROM execution_state WHERE run_id = ?').get(run.id)?.cancel_requested_at) { + throw new ChatControlConflictError('SOURCE_CHANGED') + } + const stored = createRunInputRepository(this.#database).findByRunId(run.id) + const selection = run.modelOverride ?? run.actorSnapshot?.model ?? { providerId: run.provider, modelId: run.model, reasoning: stored?.reasoning ?? null, serviceTier: stored?.serviceTier ?? null } + if (canonicalCommandJson(selection) !== canonicalCommandJson(input.expectedRunModelSelection)) + throw new ChatControlConflictError('SOURCE_CHANGED') + } + + assertActiveRunModel(input: { conversationId: string, actorId: string, expectedActiveRun: { runId: string, generation: number, modelSelection: ConversationModelSelection } | null }): void { + const run = createRunRepository(this.#database).findActiveForConversation(input.conversationId, input.actorId) + if ((run?.id ?? null) !== (input.expectedActiveRun?.runId ?? null)) + throw new ChatControlConflictError('SOURCE_CHANGED') + if (input.expectedActiveRun) { + this.assertRunModel({ + conversationId: input.conversationId, + actorId: input.actorId, + targetRunId: input.expectedActiveRun.runId, + expectedRunGeneration: input.expectedActiveRun.generation, + expectedRunModelSelection: input.expectedActiveRun.modelSelection, + }) + } + } + + markRevision(markId: string): number { + const row = this.#database.prepare('SELECT revision FROM task_marks WHERE id = ?').get(markId) + if (!row) + throw new ChatControlConflictError('OBJECT_DELETED') + return Number(row.revision) + } + + assertMark(markId: string, expectedRevision: number): void { + if (this.markRevision(markId) !== expectedRevision) + throw new ChatControlConflictError('SOURCE_CHANGED') + } + + assignmentRevision(conversationId: string): number { + this.conversationRevision(conversationId) + return Number(this.#database.prepare('SELECT assignment_revision FROM task_attention WHERE conversation_id = ?').get(conversationId)?.assignment_revision ?? 0) + } + + assertAssignment(conversationId: string, expectedRevision: number): void { + if (this.assignmentRevision(conversationId) !== expectedRevision) + throw new ChatControlConflictError('SOURCE_CHANGED') + } + + assertRead(input: { conversationId: string, readRevision: number, resultRunId: string | null }): void { + this.conversationRevision(input.conversationId) + const current = createTaskMarkRepository(this.#database).getState(input.conversationId) + if (current.readRevision !== input.readRevision || current.resultRunId !== input.resultRunId) + throw new ChatControlConflictError('SOURCE_CHANGED') + } +} diff --git a/apps/buddy/service/src/storage/ExecutionResourceQueries.ts b/apps/buddy/service/src/storage/ExecutionResourceQueries.ts new file mode 100644 index 000000000..de28a3484 --- /dev/null +++ b/apps/buddy/service/src/storage/ExecutionResourceQueries.ts @@ -0,0 +1,58 @@ +import type { DatabaseSync } from 'node:sqlite' + +export type ExecutionResourceState = 'released' | 'occupied' | 'unknown' + +const resourceSeverity = `MAX( + CASE WHEN e.run_id IS NULL OR e.cleanup_state = 'degraded' THEN 2 + WHEN r.status IN ('queued', 'running') OR e.phase <> 'closed' OR e.cleanup_state <> 'clean' THEN 1 + ELSE 0 END, + COALESCE((SELECT MAX(CASE WHEN l.status = 'hazard' THEN 2 ELSE 1 END) + FROM run_mutation_leases l WHERE l.run_id = r.id AND l.status IN ('active', 'hazard')), 0))` + +export const conversationExecutionResourcesReleased = `NOT EXISTS ( + SELECT 1 FROM runs r LEFT JOIN execution_state e ON e.run_id = r.id + WHERE r.conversation_id = c.id AND ${resourceSeverity} > 0)` + +function resourceState(severity: unknown): ExecutionResourceState { + return Number(severity) === 2 ? 'unknown' : Number(severity) === 1 ? 'occupied' : 'released' +} + +export class ExecutionResourceQueries { + readonly #database: DatabaseSync + + constructor(database: DatabaseSync) { + this.#database = database + } + + conversations(conversationIds: readonly string[]): ReadonlyMap { + const states = new Map(conversationIds.map(id => [id, 'released'])) + if (!states.size) + return states + const rows = this.#database.prepare(`SELECT r.conversation_id, MAX(${resourceSeverity}) AS severity + FROM runs r LEFT JOIN execution_state e ON e.run_id = r.id + WHERE r.conversation_id IN (SELECT value FROM json_each(?)) GROUP BY r.conversation_id`) + .all(JSON.stringify([...states.keys()])) + for (const row of rows) + states.set(String(row.conversation_id), resourceState(row.severity)) + return states + } + + conversation(conversationId: string): ExecutionResourceState { + return this.conversations([conversationId]).get(conversationId)! + } + + subtree(runId: string): ExecutionResourceState { + const row = this.#database.prepare(`WITH RECURSIVE subtree(id) AS ( + SELECT id FROM runs WHERE id = ? + UNION SELECT r.id FROM runs r JOIN subtree s ON r.parent_run_id = s.id + ) SELECT COUNT(*) AS count, MAX(${resourceSeverity}) AS severity + FROM subtree s JOIN runs r ON r.id = s.id LEFT JOIN execution_state e ON e.run_id = r.id`).get(runId)! + return Number(row.count) ? resourceState(row.severity) : 'unknown' + } + + service(): ExecutionResourceState { + const row = this.#database.prepare(`SELECT MAX(${resourceSeverity}) AS severity + FROM runs r LEFT JOIN execution_state e ON e.run_id = r.id`).get()! + return resourceState(row.severity) + } +} diff --git a/apps/buddy/service/src/storage/StorageIsolation.ts b/apps/buddy/service/src/storage/StorageIsolation.ts new file mode 100644 index 000000000..fc7471ef8 --- /dev/null +++ b/apps/buddy/service/src/storage/StorageIsolation.ts @@ -0,0 +1,100 @@ +import type { DatabaseSync } from 'node:sqlite' +import { Emitter } from '../../../shared/events/Emitter' + +export class StorageIsolatedError extends Error { + readonly code = 'STORAGE_ISOLATED' + + constructor(cause: unknown) { + super('Storage is isolated; verify committed facts on a new connection', { cause }) + this.name = 'StorageIsolatedError' + } +} + +export class StorageIsolation { + readonly #isolated = new Emitter(() => {}) + readonly onDidIsolate = this.#isolated.event + #failure: StorageIsolatedError | null = null + + get failure(): StorageIsolatedError | null { + return this.#failure + } + + readonly assertAvailable = (): void => { + if (this.#failure) + throw this.#failure + } + + isolate(cause: unknown): void { + if (this.#failure) + return + this.#failure = new StorageIsolatedError(cause) + this.#isolated.fire(this.#failure) + } +} + +const isolations = new WeakMap() +const guarded = new WeakSet() + +export function getStorageIsolation(database: DatabaseSync): StorageIsolation { + let isolation = isolations.get(database) + if (!isolation) { + isolation = new StorageIsolation() + isolations.set(database, isolation) + } + return isolation +} + +export function guardStorageDatabase(database: DatabaseSync): StorageIsolation { + const isolation = getStorageIsolation(database) + isolation.assertAvailable() + if (guarded.has(database)) + return isolation + const execute = (operation: () => T, rollback = false): T => { + if (!rollback) + isolation.assertAvailable() + try { + return operation() + } + catch (error) { + if (!database.isOpen || isStorageFailure(error)) + isolation.isolate(error) + throw error + } + } + const prepare = database.prepare.bind(database) + database.prepare = (sql) => { + const statement = execute(() => prepare(sql)) + const all = statement.all.bind(statement) + const get = statement.get.bind(statement) + const run = statement.run.bind(statement) + const iterate = statement.iterate.bind(statement) + statement.all = (...parameters) => execute(() => Reflect.apply(all, statement, parameters) as ReturnType) + statement.get = (...parameters) => execute(() => Reflect.apply(get, statement, parameters) as ReturnType) + statement.run = (...parameters) => execute(() => Reflect.apply(run, statement, parameters) as ReturnType) + statement.iterate = function* (...parameters) { + const iterator = execute(() => Reflect.apply(iterate, statement, parameters) as ReturnType) + try { + while (true) { + const next = execute(() => iterator.next()) + if (next.done) + return undefined + yield next.value + } + } + finally { + iterator.return?.() + } + } + return statement + } + const exec = database.exec.bind(database) + database.exec = sql => execute(() => exec(sql), sql.trim().toUpperCase() === 'ROLLBACK') + guarded.add(database) + return isolation +} + +function isStorageFailure(error: unknown): boolean { + if (!error || typeof error !== 'object' || !('errcode' in error) || typeof error.errcode !== 'number') + return false + return [7, 8, 10, 11, 13, 14, 15, 26].includes(error.errcode & 0xFF) +} diff --git a/apps/buddy/service/src/storage/StorageMigration.ts b/apps/buddy/service/src/storage/StorageMigration.ts index 5e1a0f874..9e3742a2a 100644 --- a/apps/buddy/service/src/storage/StorageMigration.ts +++ b/apps/buddy/service/src/storage/StorageMigration.ts @@ -12,9 +12,10 @@ import { acquireStorageRootLease } from '../../../platform/storage/StorageRootLe import { copyStorageTree, planStorageCopy, syncStorageDirectories } from '../../../platform/storage/storageTree' import { storageIdentitySchema } from '../../../shared/storage/storageApi' import { commitBackupRestore, prepareRestoreCommit, restoreRetained, restoreStage } from './commitBackupRestore' -import { openBuddyDatabase } from './database' +import { activatePersistenceCandidate } from './persistenceMigration/activatePersistenceCandidate' import { relocateDatabaseReferences, relocateProfileJournals, relocateStructuredPaths, verifyProfileReferences } from './relocateProfileReferences' import { prepareBackupRestore } from './restoreBackup' +import { rewriteIsolatedSnapshotDatabase } from './snapshotDatabase' export interface LegacyStorageSource { source: string @@ -36,18 +37,20 @@ export async function runStorageMigration(input: StorageMigrationInput, signal: const resumingCommit = operation.phase === 'ready' const activeSignal = resumingCommit ? new AbortController().signal : signal const checkpoint = (phase: StorageOperation['phase']) => resumingCommit && phase !== 'ready' ? Promise.resolve() : store.checkpoint(operation, phase) - let releaseTarget = () => {} + let releaseSource: (() => void) | undefined + let releaseTarget: (() => void) | undefined let progress: StorageProgress = { phase: 'preparing', completedBytes: 0, totalBytes: null, files: 0, error: null } const publish = (next: Partial) => { progress = { ...progress, ...next } report(progress) } try { + releaseSource = acquireStorageRootLease(operation.source, input.leaseDirectory, input.privateDirectoriesExecutable) if (operation.source !== operation.target) releaseTarget = acquireStorageRootLease(operation.target, input.leaseDirectory, input.privateDirectoriesExecutable) await checkpoint('copying') activeSignal.throwIfAborted() - const retainedPath = operation.kind === 'restore' && operation.source === operation.target ? restoreRetained(operation) : join(dirname(operation.target), `.${basename(operation.target)}.retained-${operation.id}`) + const retainedPath = (operation.kind === 'restore' || operation.kind === 'upgrade') && operation.source === operation.target ? restoreRetained(operation) : join(dirname(operation.target), `.${basename(operation.target)}.retained-${operation.id}`) const retained = async () => await lstat(retainedPath).catch((error) => { if (!isMissing(error)) throw error @@ -60,7 +63,7 @@ export async function runStorageMigration(input: StorageMigrationInput, signal: if (operation.source !== operation.target) await writeStorageJson(join(operation.source, '.storage-retired.json'), { operationId: operation.id, target: operation.target }) await store.activate(operation, identity, await retained()) - if (operation.kind === 'restore' && operation.source === operation.target) { + if ((operation.kind === 'restore' || operation.kind === 'upgrade') && operation.source === operation.target) { await rm(restoreStage(operation), { recursive: true, force: true }) await rm(restoreCredentialStage(input.locationPath, operation.id), { recursive: true, force: true }) } @@ -72,10 +75,13 @@ export async function runStorageMigration(input: StorageMigrationInput, signal: return } await assertStorageIdentity(state) - if (operation.kind === 'restore' && operation.source === operation.target) { + if ((operation.kind === 'restore' || operation.kind === 'upgrade') && operation.source === operation.target) { if (!resumingCommit) { const stage = await prepareStorageStage(operation, input.privateDirectoriesExecutable) - await prepareBackupRestore(operation, stage, input.locationPath, activeSignal, publish, input.privateDirectoriesExecutable) + if (operation.kind === 'upgrade') + await preparePersistenceUpgrade(operation, stage, input, releaseSource, activeSignal, publish, checkpoint) + else + await prepareBackupRestore(operation, stage, input.locationPath, activeSignal, publish, input.privateDirectoriesExecutable) activeSignal.throwIfAborted() await prepareRestoreCommit(operation) await checkpoint('ready') @@ -117,7 +123,8 @@ export async function runStorageMigration(input: StorageMigrationInput, signal: throw Object.assign(storageError(code), { cause: error }) } finally { - releaseTarget() + releaseTarget?.() + releaseSource?.() } } async function consolidateStorage(operation: StorageOperation, sources: LegacyStorageSource[], signal: AbortSignal, report: StorageReporter, privateDirectoriesExecutable?: string): Promise { @@ -188,8 +195,7 @@ async function relocateStorage(operation: StorageOperation, identity: StorageIde if (exists) { if (!exists.isFile() || exists.isSymbolicLink()) throw storageError('STORAGE_DATABASE_INVALID') - const database = openBuddyDatabase({ databasePath }) - try { + await rewriteIsolatedSnapshotDatabase(databasePath, async (database) => { relocateDatabaseReferences(database, operation.source, operation.target) await relocateProfileJournals(candidate, operation.source, operation.target, signal) await checkpoint('verifying') @@ -198,10 +204,7 @@ async function relocateStorage(operation: StorageOperation, identity: StorageIde throw storageError('STORAGE_DATABASE_INVALID') await verifyProfileReferences(database, candidate, operation.target) database.exec('PRAGMA wal_checkpoint(TRUNCATE)') - } - finally { - database.close() - } + }) await syncStorageFile(databasePath) } const config = join(candidate, 'config.toml') @@ -242,3 +245,37 @@ async function relocateStorage(operation: StorageOperation, identity: StorageIde await syncStorageDirectory(dirname(operation.target)) await rm(stage, { recursive: true }) } + +async function preparePersistenceUpgrade(operation: StorageOperation, stage: string, input: StorageMigrationInput, sourceLease: () => void, signal: AbortSignal, report: StorageReporter, checkpoint: (phase: StorageOperation['phase']) => Promise): Promise { + report({ phase: 'preparing', completedBytes: 0, totalBytes: null, files: 0, error: null }) + const candidate = await activatePersistenceCandidate({ + scope: 'upgrade', + report, + sourceRoot: operation.source, + sourceLease, + candidateRoot: join(stage, 'candidate'), + signal, + leaseDirectory: input.leaseDirectory, + privateDirectoriesExecutable: input.privateDirectoriesExecutable, + checkpoint: async (phase) => { + if (phase === 'copied') { + await checkpoint('converting') + report({ phase: 'converting', completedBytes: 0, totalBytes: null, files: 0, error: null }) + } + else if (phase === 'files') { + await checkpoint('verifying') + report({ phase: 'verifying', completedBytes: 0, totalBytes: null, files: 0, error: null }) + } + }, + }) + const databasePath = join(candidate.profileRoot, 'buddy', 'buddy.sqlite3') + await rewriteIsolatedSnapshotDatabase(databasePath, async (database) => { + relocateDatabaseReferences(database, candidate.profileRoot, operation.target) + await relocateProfileJournals(candidate.profileRoot, candidate.profileRoot, operation.target, signal) + await verifyProfileReferences(database, candidate.profileRoot, operation.target) + }) + signal.throwIfAborted() + await ensurePrivateDirectories([join(stage, 'profile')], input.privateDirectoriesExecutable) + await rename(join(candidate.profileRoot, 'buddy'), join(stage, 'profile', 'buddy')) + await syncStorageDirectories(join(stage, 'profile')) +} diff --git a/apps/buddy/service/src/storage/StorageSnapshots.ts b/apps/buddy/service/src/storage/StorageSnapshots.ts index 6d02bb165..52012232e 100644 --- a/apps/buddy/service/src/storage/StorageSnapshots.ts +++ b/apps/buddy/service/src/storage/StorageSnapshots.ts @@ -14,10 +14,10 @@ import { backupDirectoryAllowed, backupScopes } from '../../../shared/storage/ba import { snapshotManifestSchema } from '../../../shared/storage/snapshot' import { selectBackupDatabase } from './backupDatabase' import { portableSettings } from './backupSettings' -import { openBuddyDatabase } from './database' import { prepareNotificationSoundBackup } from './notificationSoundBackup' import { sanitizeRestoredDatabase } from './restorePolicy' -import { BUDDY_SCHEMA_VERSION } from './schema' +import { BUDDY_MIGRATION_SCHEMA_VERSION } from './schema' +import { rewriteIsolatedSnapshotDatabase } from './snapshotDatabase' export function snapshotDirectory(root: string, id: string): string { return join(root, 'exports', 'snapshots', id) @@ -43,17 +43,14 @@ export async function createStorageSnapshot(root: string, dataId: string, signal signal.throwIfAborted() if (!(await lstat(join(stage, 'files', 'buddy', 'buddy.sqlite3'))).isFile()) throw storageError('SNAPSHOT_DATABASE_MISSING') - const database = openBuddyDatabase({ databasePath: join(stage, 'files', 'buddy', 'buddy.sqlite3') }) - try { + const databaseVersion = await rewriteIsolatedSnapshotDatabase(join(stage, 'files', 'buddy', 'buddy.sqlite3'), (database) => { sanitizeRestoredDatabase(database, !!encryptedCredentials) selectBackupDatabase(database, options.scopes, paths) if (Object.values(database.prepare('PRAGMA quick_check').get() ?? {})[0] !== 'ok' || database.prepare('PRAGMA foreign_key_check').all().length) throw storageError('SNAPSHOT_DATABASE_INVALID') database.exec('VACUUM; PRAGMA wal_checkpoint(TRUNCATE)') - } - finally { - database.close() - } + return Number(database.prepare('PRAGMA user_version').get()!.user_version) + }) await syncStorageFile(join(stage, 'files', 'buddy', 'buddy.sqlite3')) if (options.scopes.includes('settings')) { const path = join(stage, 'files', 'config.toml') @@ -79,7 +76,7 @@ export async function createStorageSnapshot(root: string, dataId: string, signal throw storageError('SNAPSHOT_INVALID_LINK') files.push({ kind: 'file', path: entry.path, executable: entry.executable, ...await packSnapshotFile(join(stage, 'files', entry.path), entry.size, entry.executable, signal) }) } - const manifest = snapshotManifestSchema.parse({ version: 4, ...(options.automatic ? { automatic: options.automatic } : {}), scopes: options.scopes, credentials: !!encryptedCredentials, id, dataId, sourceRoot: root, createdAt: new Date().toISOString(), databaseVersion: BUDDY_SCHEMA_VERSION, files, excludedLinks }) + const manifest = snapshotManifestSchema.parse({ version: 4, ...(options.automatic ? { automatic: options.automatic } : {}), scopes: options.scopes, credentials: !!encryptedCredentials, id, dataId, sourceRoot: root, createdAt: new Date().toISOString(), databaseVersion, files, excludedLinks }) await verifySnapshotFiles(stage, manifest, signal) await writeSnapshotManifest(stage, manifest) await syncStorageDirectories(stage) @@ -95,7 +92,7 @@ export async function createStorageSnapshot(root: string, dataId: string, signal } export async function verifyStorageSnapshot(directory: string, signal: AbortSignal): Promise { const manifest = await readSnapshotManifest(directory) - if (manifest.databaseVersion > BUDDY_SCHEMA_VERSION) + if (manifest.databaseVersion > BUDDY_MIGRATION_SCHEMA_VERSION) throw storageError('SNAPSHOT_NEWER_VERSION') await verifySnapshotFiles(directory, manifest, signal) return manifest diff --git a/apps/buddy/service/src/storage/__tests__/ExecutionResourceQueries.spec.ts b/apps/buddy/service/src/storage/__tests__/ExecutionResourceQueries.spec.ts new file mode 100644 index 000000000..38aaac882 --- /dev/null +++ b/apps/buddy/service/src/storage/__tests__/ExecutionResourceQueries.spec.ts @@ -0,0 +1,81 @@ +import type { DatabaseSync } from 'node:sqlite' +import { afterEach, describe, expect, it } from 'vitest' +import { ProductContentReferences } from '../../productProtocol/ProductContentReferences' +import { ProductReadViews } from '../../productProtocol/ProductReadViews' +import { ChatControlCommandRepository } from '../ChatControlCommandRepository' +import { openBuddySqliteDatabase } from '../database' +import { ExecutionResourceQueries } from '../ExecutionResourceQueries' +import { sqliteRunFixtureTime as now, seedSqliteRun } from './sqliteRunFixture' + +const databases: DatabaseSync[] = [] +afterEach(() => { + for (const database of databases.splice(0)) database.close() +}) + +function fixture() { + const database = openBuddySqliteDatabase({ databasePath: ':memory:' }) + databases.push(database) + const resources = new ExecutionResourceQueries(database) + return { database, resources } +} + +function closed(database: DatabaseSync, id: string, conversationId = 'task-1') { + seedSqliteRun(database, id, { conversationId }) + database.prepare('UPDATE runs SET status = \'completed\' WHERE id = ?').run(id) + database.prepare('UPDATE execution_state SET phase = \'closed\', cleanup_state = \'clean\' WHERE run_id = ?').run(id) +} + +describe('execution resource release evidence', () => { + it.each([ + ['completed', 'closing', 'pending', 'occupied'], + ['completed', 'closed', 'degraded', 'unknown'], + ])('projects %s / %s / %s as %s consistently', (status, phase, cleanup, expected) => { + const { database, resources } = fixture() + closed(database, 'root') + database.prepare('UPDATE runs SET status = ? WHERE id = ?').run(status!, 'root') + database.prepare('UPDATE execution_state SET phase = ?, cleanup_state = ? WHERE run_id = ?').run(phase!, cleanup!, 'root') + expect(resources.subtree('root')).toBe(expected) + expect(resources.conversation('task-1')).toBe(expected) + expect(resources.service()).toBe(expected) + }) + + it('distinguishes an empty task from missing execution evidence and scopes batch results', () => { + const { database, resources } = fixture() + expect(resources.service()).toBe('released') + expect(resources.subtree('missing')).toBe('unknown') + closed(database, 'lost') + closed(database, 'other', 'task-2') + database.prepare('DELETE FROM execution_state WHERE run_id = ?').run('lost') + expect(resources.conversations(['task-1', 'task-2', 'empty'])).toEqual(new Map([ + ['task-1', 'unknown'], + ['task-2', 'released'], + ['empty', 'released'], + ])) + expect(resources.subtree('lost')).toBe('unknown') + expect(resources.service()).toBe('unknown') + }) + + it('includes private descendants and mutation leases in public release evidence and cancellation receipts', () => { + const { database, resources } = fixture() + closed(database, 'root') + closed(database, 'child') + closed(database, 'other', 'task-2') + database.prepare('UPDATE runs SET parent_run_id = ?, root_run_id = ?, execution_kind = \'npc_child\' WHERE id = ?').run('root', 'root', 'child') + database.prepare(`INSERT INTO run_mutation_leases (invocation_id, run_id, segment_id, claims_json, status, created_at, updated_at) + VALUES ('lease', 'child', 'segment', '[]', 'active', ?, ?)`).run(now, now) + const views = new ProductReadViews(database, 'desktop-local', new ProductContentReferences(database, 'desktop-local')) + const commands = new ChatControlCommandRepository(database, 'instance') + for (const [status, expected] of [['active', 'occupied'], ['hazard', 'unknown'], ['released', 'released']]) { + database.prepare('UPDATE run_mutation_leases SET status = ? WHERE invocation_id = ?').run(status!, 'lease') + expect(resources.subtree('root')).toBe(expected) + expect(views.runExecution('root')).toMatchObject({ resources: expected }) + expect(views.runExecution('child')).toBeNull() + expect(commands.cancelRun({ runId: 'root', expectedRunGeneration: 0 }).result).toEqual({ cancelRequested: false, executionResources: expected }) + expect(commands.stopService({ targetServiceInstanceId: 'instance', scope: 'keep_service' }, 'keep').result.executionResources).toBe(expected) + expect(resources.conversation('task-2')).toBe('released') + } + database.prepare('DELETE FROM execution_state WHERE run_id = ?').run('child') + expect(views.runExecution('root')).toMatchObject({ resources: 'unknown' }) + expect(commands.stopService({ targetServiceInstanceId: 'instance', scope: 'stop_service' }, 'stop').result).toEqual({ scope: 'stop_service', runIds: [], executionResources: 'unknown' }) + }) +}) diff --git a/apps/buddy/service/src/storage/__tests__/StorageIsolation.spec.ts b/apps/buddy/service/src/storage/__tests__/StorageIsolation.spec.ts new file mode 100644 index 000000000..5044f74d0 --- /dev/null +++ b/apps/buddy/service/src/storage/__tests__/StorageIsolation.spec.ts @@ -0,0 +1,86 @@ +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { DatabaseSync } from 'node:sqlite' +import { afterEach, describe, expect, it } from 'vitest' +import { guardStorageDatabase, StorageIsolatedError } from '../StorageIsolation' +import { withTransaction } from '../transaction' + +const databases: DatabaseSync[] = [] +const directories: string[] = [] + +afterEach(() => { + for (const database of databases.splice(0)) { + if (database.isOpen) + database.close() + } + for (const directory of directories.splice(0)) + rmSync(directory, { recursive: true, force: true }) +}) + +function open(path = ':memory:') { + const database = new DatabaseSync(path) + databases.push(database) + return database +} + +describe('storage isolation', () => { + it('isolates all prepared reads and writes on real READONLY failure while allowing rollback and close', () => { + const database = open() + const isolation = guardStorageDatabase(database) + database.exec('CREATE TABLE values_table (value TEXT); INSERT INTO values_table VALUES (\'original\')') + const read = database.prepare('SELECT * FROM values_table') + const write = database.prepare('INSERT INTO values_table VALUES (?)') + const iterator = read.iterate() + expect(iterator.next().value).toEqual({ value: 'original' }) + database.exec('PRAGMA query_only = ON; BEGIN') + const failures: unknown[] = [] + isolation.onDidIsolate(error => failures.push(error)) + expect(() => write.run('lost')).toThrow(expect.objectContaining({ errcode: 8 })) + expect(database.isTransaction).toBe(true) + const failure = isolation.failure + for (const operation of [() => read.get(), () => read.all(), () => iterator.next(), () => write.run('retry'), () => database.exec('SELECT 1'), () => database.prepare('SELECT 1'), () => withTransaction(database, () => 1)]) + expect(operation).toThrow(StorageIsolatedError) + isolation.isolate(new Error('secondary failure')) + expect(isolation.failure).toBe(failure) + expect(failures).toEqual([failure]) + database.exec('ROLLBACK') + expect(database.isTransaction).toBe(false) + database.close() + expect(database.isOpen).toBe(false) + }) + + it('isolates a real FULL failure even when SQLite has already rolled the transaction back', () => { + const database = open() + const isolation = guardStorageDatabase(database) + database.exec('CREATE TABLE values_table (value BLOB)') + const pages = Number(database.prepare('PRAGMA page_count').get()!.page_count) + database.exec(`PRAGMA max_page_count = ${pages}`) + expect(() => withTransaction(database, () => database.prepare('INSERT INTO values_table VALUES (zeroblob(1048576))').run())) + .toThrow(expect.objectContaining({ errcode: 13 })) + expect(database.isTransaction).toBe(false) + expect(isolation.failure?.cause).toMatchObject({ errcode: 13 }) + expect(() => database.prepare('SELECT * FROM values_table')).toThrow(StorageIsolatedError) + }) + + it('isolates an unreadable database instead of returning empty results', () => { + const directory = mkdtempSync(join(tmpdir(), 'lexora-storage-unreadable-')) + directories.push(directory) + const path = join(directory, 'broken.sqlite3') + writeFileSync(path, 'not a SQLite database'.repeat(64)) + const database = open(path) + const isolation = guardStorageDatabase(database) + expect(() => database.prepare('SELECT * FROM sqlite_schema').all()).toThrow(expect.objectContaining({ errcode: 26 })) + expect(isolation.failure?.cause).toMatchObject({ errcode: 26 }) + }) + + it('keeps a known constraint rollback usable', () => { + const database = open() + const isolation = guardStorageDatabase(database) + database.exec('CREATE TABLE values_table (id INTEGER PRIMARY KEY); INSERT INTO values_table VALUES (1)') + expect(() => withTransaction(database, () => database.prepare('INSERT INTO values_table VALUES (1)').run())).toThrow() + expect(isolation.failure).toBeNull() + withTransaction(database, () => database.prepare('INSERT INTO values_table VALUES (2)').run()) + expect(database.prepare('SELECT id FROM values_table ORDER BY id').all()).toEqual([{ id: 1 }, { id: 2 }]) + }) +}) diff --git a/apps/buddy/service/src/storage/__tests__/StorageMigration.spec.ts b/apps/buddy/service/src/storage/__tests__/StorageMigration.spec.ts index c474aa80f..645a78eba 100644 --- a/apps/buddy/service/src/storage/__tests__/StorageMigration.spec.ts +++ b/apps/buddy/service/src/storage/__tests__/StorageMigration.spec.ts @@ -1,6 +1,6 @@ import type { StorageProgress } from '../../../../shared/storage/storageApi' -import { randomUUID } from 'node:crypto' -import { chmod, mkdir, mkdtemp, readdir, readFile, rm, writeFile } from 'node:fs/promises' +import { createHash, randomUUID } from 'node:crypto' +import { chmod, lstat, mkdir, mkdtemp, readdir, readFile, rename, rm, writeFile } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import process from 'node:process' @@ -12,8 +12,11 @@ import { StorageLocationStore } from '../../../../platform/storage/StorageLocati import { acquireStorageRootLease } from '../../../../platform/storage/StorageRootLease' import { readDiagnosticError } from '../../../../shared/diagnostics/applicationDiagnostic' import { SkillService } from '../../skills/SkillService' +import { createAttachmentPersistenceRepository } from '../attachmentPersistenceRepository' import { BuddyDataPaths } from '../BuddyDataPaths' -import { openBuddyDatabase } from '../database' +import { CommandCommitter } from '../CommandCommitter' +import { restoreRetained, restoreStage } from '../commitBackupRestore' +import { openBuddyDatabase, openBuddySqliteDatabase } from '../database' import { createSkillRepository } from '../skillRepository' import { createSpaceRepository } from '../spaceRepository' import { runStorageMigration } from '../StorageMigration' @@ -24,7 +27,7 @@ afterEach(async () => { vi.restoreAllMocks() await Promise.all(directories.splice(0).map(path => rm(path, { recursive: true, force: true }))) }) -async function fixture() { +async function fixture(sqlite = false) { const directory = await mkdtemp(join(tmpdir(), 'lexora-storage-')) directories.push(directory) const root = join(directory, 'a') @@ -33,7 +36,9 @@ async function fixture() { await mkdir(join(root, 'buddy', 'empty'), { recursive: true }) const identity = { version: 1 as const, layoutVersion: 1 as const, dataId: randomUUID(), generation: randomUUID() } await writeStorageJson(join(root, 'storage.json'), identity) - const database = openBuddyDatabase({ databasePath: join(root, 'buddy', 'buddy.sqlite3') }) + const database = (sqlite ? openBuddySqliteDatabase : openBuddyDatabase)({ databasePath: join(root, 'buddy', 'buddy.sqlite3') }) + if (sqlite) + createAttachmentPersistenceRepository(database).adopt({ path: join(root, 'buddy', 'drafts', 'draft', 'hello.txt'), sizeBytes: 5, sha256: createHash('sha256').update('hello').digest('hex') }) database.prepare('INSERT INTO attachments (id, draft_id, stored_path, name, mime_type, size_bytes, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)').run('file', 'draft', join(root, 'buddy', 'drafts', 'draft', 'hello.txt'), 'hello.txt', 'text/plain', 5, '2026-01-01') database.close() const store = new StorageLocationStore(join(directory, 'bootstrap', 'location.json'), root) @@ -92,15 +97,6 @@ it('never activates a cancelled copy or overwrites an unrelated target', async ( expect((await store.read()).activeRoot).toBe(root) expect(await readFile(join(target, 'unrelated.txt'), 'utf8')).toBe('keep') }) -it('excludes concurrent users of one root and releases the lease without stale PID recovery', async () => { - const { root, directory } = await fixture() - const locks = join(directory, 'locks') - const release = acquireStorageRootLease(root, locks, privateDirectoriesExecutable) - expect(() => acquireStorageRootLease(root, locks, privateDirectoriesExecutable)).toThrow('STORAGE_ROOT_IN_USE') - release() - acquireStorageRootLease(root, locks, privateDirectoriesExecutable)() -}) - it('consolidates legacy state once and resumes an interrupted import without changing the source', async () => { const { directory, root, store } = await fixture() await rm(join(root, 'storage.json')) @@ -246,3 +242,150 @@ it('resumes an interrupted commit without reopening cancellation or recopying th expect(await store.read()).toMatchObject({ activeRoot: target, pending: null }) expect(await readFile(join(target, 'buddy', 'drafts', 'draft', 'hello.txt'), 'utf8')).toBe('hello') }) + +it('relocates SQLite authority without changing receipts, file identities or source bytes', async () => { + const { root, directory, store, run } = await fixture(true) + const databasePath = join(root, 'buddy/buddy.sqlite3') + const source = openBuddySqliteDatabase({ databasePath }) + const identity = source.prepare('SELECT * FROM storage_profile').get()! + const file = source.prepare('SELECT * FROM managed_files').get()! + const references = source.prepare('SELECT * FROM file_references').all() + const original = new CommandCommitter(source).accept({ dataGeneration: String(identity.data_generation), actorId: 'desktop-local', commandId: 'preserved-receipt', method: 'composerDrafts.save', fingerprint: 'fingerprint', fingerprintVersion: 1, sourceInstanceId: 'original-instance' }, () => ({ target: { draftId: 'draft' }, result: { revision: 1 } })).receipt + source.close() + const before = await readFile(databasePath) + const target = join(directory, 'moved') + await store.request('relocate', target) + await run() + expect(await readFile(databasePath)).toEqual(before) + expect((await store.read()).activeRoot).toBe(target) + const moved = openBuddySqliteDatabase({ databasePath: join(target, 'buddy/buddy.sqlite3') }) + try { + expect(moved.prepare('SELECT * FROM storage_profile').get()).toEqual(identity) + expect(new CommandCommitter(moved).find(original)).toEqual(original) + expect(moved.prepare('SELECT * FROM file_references').all()).toEqual(references) + expect(moved.prepare('SELECT * FROM managed_files').get()).toEqual({ ...file, path: join(target, 'buddy/drafts/draft/hello.txt') }) + expect(moved.prepare('SELECT stored_path FROM attachments').get()?.stored_path).toBe(join(target, 'buddy/drafts/draft/hello.txt')) + expect(await readFile(join(target, 'buddy/drafts/draft/hello.txt'), 'utf8')).toBe('hello') + expect(moved.prepare('PRAGMA foreign_key_check').all()).toEqual([]) + expect(() => moved.exec('DELETE FROM command_receipts')).toThrow('IMMUTABLE') + expect(() => moved.exec('DELETE FROM managed_files')).toThrow('IMMUTABLE') + } + finally { moved.close() } +}) + +it('upgrades in place, retains original bytes and preserves references and device settings', async () => { + const { root, store, run } = await fixture() + const original = await readFile(join(root, 'buddy', 'buddy.sqlite3')) + await writeFile(join(root, 'config.toml'), '[desktop]\nexit_behavior = "stop_service"\n') + const untouched = ['buddy/unknown-data/file.bin', 'buddy/extensions/inference/model.bin', 'buddy/backups/snapshot.bin', 'buddy/themes/custom/theme.json', 'device/chromium/Cache/page', 'recovery/previous/buddy.sqlite3'] + for (const path of untouched) { + await mkdir(join(root, path, '..'), { recursive: true }) + await writeFile(join(root, path), 'preserve in place') + } + const identities = await Promise.all(untouched.map(path => lstat(join(root, path)))) + const requested = await store.request('upgrade', root) + await run() + const state = await store.read() + expect(state).toMatchObject({ activeRoot: root, pending: null, identity: { generation: requested.pending!.id } }) + const retained = state.history.at(-1)!.retainedTarget! + for (const [index, path] of untouched.entries()) { + expect(await lstat(join(root, path))).toMatchObject({ ino: identities[index]!.ino, mtimeMs: identities[index]!.mtimeMs }) + expect(await readFile(join(root, path), 'utf8')).toBe('preserve in place') + await expect(lstat(join(retained, path))).rejects.toMatchObject({ code: 'ENOENT' }) + } + expect(await readFile(join(retained, 'buddy', 'buddy.sqlite3'))).toEqual(original) + expect(await readFile(join(retained, 'buddy', 'drafts', 'draft', 'hello.txt'), 'utf8')).toBe('hello') + expect(await readFile(join(root, 'config.toml'), 'utf8')).toBe('[desktop]\nexit_behavior = "stop_service"\n') + const database = openBuddySqliteDatabase({ databasePath: join(root, 'buddy', 'buddy.sqlite3') }) + try { + expect(database.prepare('SELECT authority, migration_state FROM storage_profile').get()).toMatchObject({ authority: 'sqlite', migration_state: 'ready' }) + expect(database.prepare('SELECT stored_path FROM attachments').get()?.stored_path).toBe(join(root, 'buddy', 'drafts', 'draft', 'hello.txt')) + expect(database.prepare('PRAGMA foreign_key_check').all()).toEqual([]) + } + finally { database.close() } +}) + +it('refuses to migrate an actively used source and can retry after the owner exits', async () => { + const { root, directory, store, run } = await fixture() + await store.request('upgrade', root) + const release = acquireStorageRootLease(root, join(directory, 'locks'), privateDirectoriesExecutable) + try { + await expect(run()).rejects.toThrow('STORAGE_ROOT_IN_USE') + expect((await store.read()).pending?.phase).toBe('failed') + } + finally { release() } + await run() + expect((await store.read()).pending).toBeNull() +}) + +it.each(['retained', 'installed', 'legacy-retained', 'legacy-installed'])('recovers an upgrade interrupted after the original data is %s', async (point) => { + const { root, directory, store, run } = await fixture() + const original = await readFile(join(root, 'buddy', 'buddy.sqlite3')) + const operation = (await store.request('upgrade', root)).pending! + await expect(runStorageMigration({ locationPath: store.path, defaultRoot: root, leaseDirectory: join(directory, 'locks'), privateDirectoriesExecutable, legacySources: [] }, new AbortController().signal, (progress) => { + if (progress.phase === 'activating') + throw new Error('STORAGE_TEST_INTERRUPTED') + })).rejects.toThrow('STORAGE_TEST_INTERRUPTED') + expect((await store.read()).pending?.phase).toBe('ready') + const path = point.startsWith('legacy-') ? 'buddy' : 'buddy/buddy.sqlite3' + if (point.startsWith('legacy-')) + await writeStorageJson(join(restoreStage(operation), 'commit.json'), { id: operation.id, entries: [{ path, original: true }] }) + await mkdir(join(restoreRetained(operation), path, '..'), { recursive: true }) + await rename(join(root, path), join(restoreRetained(operation), path)) + if (point.endsWith('installed')) + await rename(join(restoreStage(operation), 'profile', path), join(root, path)) + await expect(store.cancel()).rejects.toThrow('STORAGE_COMMITTING') + await run(AbortSignal.abort()) + expect(await store.read()).toMatchObject({ activeRoot: root, pending: null, identity: { generation: operation.id } }) + expect(await readFile(join(restoreRetained(operation), 'buddy', 'buddy.sqlite3'))).toEqual(original) + const database = openBuddySqliteDatabase({ databasePath: join(root, 'buddy', 'buddy.sqlite3') }) + database.close() +}) + +it('reports real upgrade progress and retries an interrupted copy without reusing its partial candidate', async () => { + const { root, directory, store } = await fixture() + const operation = (await store.request('upgrade', root)).pending! + const input = { locationPath: store.path, defaultRoot: root, leaseDirectory: join(directory, 'locks'), privateDirectoriesExecutable, legacySources: [] } + const original = await readFile(join(root, 'buddy/buddy.sqlite3')) + const controller = new AbortController() + const updates: StorageProgress[] = [] + await expect(runStorageMigration(input, controller.signal, (progress) => { + updates.push(progress) + if (progress.phase === 'copying' && progress.completedBytes > 0) + controller.abort() + })).rejects.toThrow('STORAGE_CANCELLED') + expect(updates.some(update => update.phase === 'preparing' && update.files > 0)).toBe(true) + expect(updates.some(update => update.phase === 'copying' && update.totalBytes! > 0)).toBe(true) + expect(await readFile(join(root, 'buddy/buddy.sqlite3'))).toEqual(original) + const stale = join(restoreStage(operation), 'candidate/source/buddy/extensions') + await mkdir(stale, { recursive: true }) + await writeFile(join(stale, 'partial.bin'), 'old interrupted copy') + await runStorageMigration(input, new AbortController().signal, progress => updates.push(progress)) + expect((await store.read()).pending).toBeNull() + expect(updates.some(update => update.phase === 'copying' && update.files > 0 && update.completedBytes === update.totalBytes)).toBe(true) + expect(updates.some(update => update.phase === 'verifying' && update.files > 0)).toBe(true) + await expect(lstat(restoreStage(operation))).rejects.toMatchObject({ code: 'ENOENT' }) + await expect(lstat(join(root, 'buddy/extensions'))).rejects.toMatchObject({ code: 'ENOENT' }) +}) + +it('imports uncheckpointed WAL data and retires old sidecars when replacing the database', async () => { + const { root, store, run } = await fixture() + const path = join(root, 'buddy/buddy.sqlite3') + const database = openBuddyDatabase({ databasePath: path }) + database.prepare('INSERT INTO conversations (id, created_at, updated_at) VALUES (?, ?, ?)').run('wal-task', '2026-01-01', '2026-01-01') + const snapshot = await Promise.all(['', '-wal', '-shm'].map(suffix => readFile(`${path}${suffix}`))) + database.close() + await Promise.all(['', '-wal', '-shm'].map((suffix, index) => writeFile(`${path}${suffix}`, snapshot[index]!))) + await store.request('upgrade', root) + await run() + const retained = (await store.read()).history.at(-1)!.retainedTarget! + for (const [index, suffix] of ['', '-wal', '-shm'].entries()) + expect(await readFile(join(retained, `buddy/buddy.sqlite3${suffix}`))).toEqual(snapshot[index]) + for (const suffix of ['-wal', '-shm']) + await expect(lstat(`${path}${suffix}`)).rejects.toMatchObject({ code: 'ENOENT' }) + const upgraded = openBuddySqliteDatabase({ databasePath: path }) + try { + expect(upgraded.prepare('SELECT id FROM conversations').get()?.id).toBe('wal-task') + } + finally { upgraded.close() } +}) diff --git a/apps/buddy/service/src/storage/__tests__/archiveBackup.spec.ts b/apps/buddy/service/src/storage/__tests__/archiveBackup.spec.ts deleted file mode 100644 index 6cdd4305e..000000000 --- a/apps/buddy/service/src/storage/__tests__/archiveBackup.spec.ts +++ /dev/null @@ -1,74 +0,0 @@ -import type { DatabaseSync } from 'node:sqlite' -import { mkdtempSync, rmSync } from 'node:fs' -import { tmpdir } from 'node:os' -import { join } from 'node:path' -import { afterEach, expect, it } from 'vitest' -import { mergeBackupDatabase, selectBackupDatabase } from '../backupDatabase' -import { openBuddyDatabase } from '../database' - -const roots: string[] = [] -const databases: DatabaseSync[] = [] -const now = '2026-11-09T00:00:00.000Z' -afterEach(() => { - for (const database of databases.splice(0)) { - if (database.isOpen) - database.close() - } - for (const root of roots.splice(0)) - rmSync(root, { recursive: true, force: true }) -}) - -function fixture() { - const root = mkdtempSync(join(tmpdir(), 'lexora-archive-backup-')) - roots.push(root) - const databasePath = join(root, 'buddy.sqlite3') - const database = openBuddyDatabase({ databasePath }) - databases.push(database) - return { database, databasePath } -} - -function seed(database: DatabaseSync) { - database.exec(` - INSERT INTO conversations (id, title, created_at, updated_at) VALUES ('task', 'Archived history', '${now}', '${now}'); - INSERT INTO conversation_branches (id, conversation_id, created_at) VALUES ('branch', 'task', '${now}'); - INSERT INTO messages (id, conversation_id, branch_id, role, content_json, created_at) - VALUES ('input', 'task', 'branch', 'user', '{"text":"Keep history"}', '${now}'); - INSERT INTO runs (id, conversation_id, branch_id, triggering_message_id, provider, model, purpose, status, started_at) - VALUES ('run', 'task', 'branch', 'input', 'fixture', 'fixture', 'chat', 'completed', '${now}'); - UPDATE conversations SET archived_at = '${now}' WHERE id = 'task'; - UPDATE conversation_archive_policy SET days = 7; - INSERT INTO conversation_deletion_jobs VALUES ('removed', 'failed', 'legacy_removed', 'RETRY_REQUIRED', '{"files":[]}', '${now}', '${now}'); - `) -} - -it('keeps lineage in non-task snapshots without leaking archive policy or jobs', () => { - const { database, databasePath } = fixture() - seed(database) - selectBackupDatabase(database, ['providers']) - expect(database.prepare('SELECT * FROM conversation_archive_policy').all()).toEqual([]) - expect(database.prepare('SELECT * FROM conversation_deletion_jobs').all()).toEqual([]) - database.close() - const reopened = openBuddyDatabase({ databasePath }) - databases.push(reopened) - expect(reopened.prepare('SELECT lineage FROM buddy_schema_lineage').get()?.lineage).toBe('archive-v42') - expect(reopened.prepare('PRAGMA user_version').get()?.user_version).toBe(42) - expect(reopened.prepare('PRAGMA foreign_key_check').all()).toEqual([]) -}) - -it('restores archived history, policy and durable jobs then restores active-admission protection', () => { - const source = fixture() - seed(source.database) - selectBackupDatabase(source.database, ['tasks']) - const target = fixture() - mergeBackupDatabase(target.database, source.database, ['tasks']) - expect(target.database.prepare('SELECT archived_at FROM conversations WHERE id = \'task\'').get()?.archived_at).toBe(now) - expect(target.database.prepare('SELECT days FROM conversation_archive_policy').get()?.days).toBe(7) - expect(target.database.prepare('SELECT conversation_id, origin, status FROM conversation_deletion_jobs').all()).toEqual([{ conversation_id: 'removed', origin: 'legacy_removed', status: 'failed' }]) - expect(target.database.prepare('SELECT status FROM runs WHERE id = \'run\'').get()?.status).toBe('completed') - expect(() => target.database.exec('UPDATE runs SET status = \'queued\' WHERE id = \'run\'')).toThrow('CONVERSATION_NOT_ACTIVE') - expect(target.database.prepare('PRAGMA foreign_key_check').all()).toEqual([]) - target.database.close() - const reopened = openBuddyDatabase({ databasePath: target.databasePath }) - databases.push(reopened) - expect(reopened.prepare('SELECT count(*) AS count FROM conversation_deletion_jobs').get()?.count).toBe(1) -}) diff --git a/apps/buddy/service/src/storage/__tests__/archiveSchemaMigration.spec.ts b/apps/buddy/service/src/storage/__tests__/archiveSchemaMigration.spec.ts index e0cbc8a3f..e40710d30 100644 --- a/apps/buddy/service/src/storage/__tests__/archiveSchemaMigration.spec.ts +++ b/apps/buddy/service/src/storage/__tests__/archiveSchemaMigration.spec.ts @@ -4,9 +4,9 @@ import { tmpdir } from 'node:os' import { join } from 'node:path' import { DatabaseSync } from 'node:sqlite' import { afterEach, describe, expect, it } from 'vitest' -import { openBuddyDatabase } from '../database' -import { BUDDY_SCHEMA_MIGRATIONS } from '../schema' -import { EXPERIMENTAL_PERSISTENCE_SCHEMA_SQL } from './experimentalPersistenceSchema' +import { openBuddyDatabase, openBuddyMigrationCandidate, openBuddySqliteDatabase } from '../database' +import { BUDDY_PERSISTENCE_SCHEMA_LINEAGE } from '../migrations/v43Persistence' +import { BUDDY_MIGRATION_SCHEMA_VERSION, BUDDY_SCHEMA_MIGRATIONS } from '../schema' import { MIGRATION_TEST_TIMEOUT, openMigrationFixtureDatabase } from './migrationFixture' const roots: string[] = [] @@ -109,35 +109,45 @@ describe('archive schema lineage', { timeout: MIGRATION_TEST_TIMEOUT }, () => { expect(readdirSync(external)).toEqual(['session.jsonl']) }) - it('persists archive CAS, admission guards, deletion jobs and detached usage on the legacy schema', () => { + it.each([42, BUDDY_MIGRATION_SCHEMA_VERSION])('upgrades or reopens v%s preserving identity, receipts and revisions', (version) => { const { databasePath, database: previous } = fixture() seed(previous, '/synthetic/session.jsonl') + apply(previous, BUDDY_SCHEMA_MIGRATIONS.filter(migration => migration.version >= 42 && migration.version <= version)) + previous.exec(`UPDATE conversations SET archived_at = '${now}' WHERE id = 'task'`) + const identity = version >= 43 ? previous.prepare('SELECT * FROM storage_profile').get() : undefined + if (identity) { + previous.exec(`INSERT INTO command_receipts + (data_generation, actor_id, command_id, method, fingerprint_version, fingerprint, source_instance_id, decision, target_json, result_json, decided_at) + SELECT data_generation, local_actor_id, 'preserved-command', 'fixture.command', 1, 'fingerprint', 'instance', 'accepted', '{}', '{"kept":true}', '${now}' FROM storage_profile`) + } + const receipts = identity ? previous.prepare('SELECT * FROM command_receipts').all() : [] + if (identity) + previous.exec('UPDATE product_query_revisions SET revision = 9007199254740993') + const revisions = identity + ? previous.prepare('SELECT table_name, scope, CAST(revision AS TEXT) AS revision FROM product_query_revisions').all() + : [] previous.close() - const database = open(databasePath) - database.exec(`UPDATE conversations SET archived_at = '${now}' WHERE id = 'task'`) - expect(database.prepare('SELECT metadata_revision FROM conversations WHERE id = \'task\'').get()?.metadata_revision).toBe(1) - database.exec(`UPDATE conversation_members SET removed_at = '${now}' WHERE conversation_id = 'task'`) - expect(database.prepare('SELECT metadata_revision FROM conversations WHERE id = \'task\'').get()?.metadata_revision).toBe(2) - expect(database.prepare('UPDATE conversations SET archived_at = NULL WHERE id = ? AND metadata_revision = ?').run('task', 0).changes).toBe(0) - expect(() => database.exec('UPDATE runs SET status = \'queued\' WHERE id = \'run\'')).toThrow('CONVERSATION_NOT_ACTIVE') - expect(() => database.exec(`INSERT INTO runs (id, conversation_id, branch_id, triggering_message_id, provider, model, purpose, status, started_at) - VALUES ('blocked', 'task', 'branch', 'input', 'fixture', 'fixture', 'chat', 'queued', '${now}')`)).toThrow('CONVERSATION_NOT_ACTIVE') - database.exec(`INSERT INTO conversation_deletion_jobs VALUES ('task', 'failed', 'conversation', 'RETRY_REQUIRED', '{"files":[]}', '${now}', '${now}')`) - database.exec('DELETE FROM runs; DELETE FROM extension_invocations') - const retainedUsage = database.prepare('SELECT id, run_id, invocation_id, detached, total_tokens, total_cost FROM usage_records ORDER BY id').all() - expect(retainedUsage).toEqual(['usage-action', 'usage-run'].map(id => ({ id, run_id: null, invocation_id: null, detached: 1, total_tokens: 10, total_cost: 10 }))) - database.close() - const reopened = open(databasePath) - expect(reopened.prepare('SELECT status, origin, error_code, manifest_json FROM conversation_deletion_jobs').get()).toEqual({ status: 'failed', origin: 'conversation', error_code: 'RETRY_REQUIRED', manifest_json: '{"files":[]}' }) - expect(reopened.prepare('SELECT id, run_id, invocation_id, detached, total_tokens, total_cost FROM usage_records ORDER BY id').all()).toEqual(retainedUsage) - expect(reopened.prepare('PRAGMA foreign_key_check').all()).toEqual([]) + const candidate = openBuddyMigrationCandidate({ databasePath }) + databases.push(candidate) + expect(candidate.prepare('PRAGMA user_version').get()?.user_version).toBe(BUDDY_MIGRATION_SCHEMA_VERSION) + expect(candidate.prepare('SELECT lineage FROM buddy_schema_lineage').get()?.lineage).toBe(BUDDY_PERSISTENCE_SCHEMA_LINEAGE) + expect(candidate.prepare('SELECT archived_at, metadata_revision FROM conversations WHERE id = \'task\'').get()).toEqual({ archived_at: now, metadata_revision: 1 }) + expect(candidate.prepare('SELECT authority FROM storage_profile').get()?.authority).toBe('legacy_jsonl') + if (identity) + expect(candidate.prepare('SELECT * FROM storage_profile').get()).toEqual(identity) + expect(candidate.prepare('SELECT * FROM command_receipts').all()).toEqual(receipts) + for (const row of revisions) + expect(candidate.prepare('SELECT CAST(revision AS TEXT) AS revision FROM product_query_revisions WHERE table_name = ? AND scope = ?').get(row.table_name!, row.scope!)).toEqual({ revision: row.revision }) + expect(candidate.prepare('SELECT name FROM sqlite_master WHERE name IN (\'managed_files\', \'product_content_reference_owners\')').all()).toHaveLength(2) + expect(candidate.prepare('SELECT * FROM conversation_deletion_jobs').all()).toEqual([]) + expect(candidate.prepare('PRAGMA foreign_key_check').all()).toEqual([]) }) - it.each([false, true])('rejects experimental v42 before changing database or WAL bytes (live WAL=%s)', (wal) => { + it.each([false, true])('rejects a mismatched lineage (live WAL=%s) before changing database bytes or version', (wal) => { const { databasePath, database } = fixture() seed(database, '/synthetic/session.jsonl') - database.exec(EXPERIMENTAL_PERSISTENCE_SCHEMA_SQL) - database.exec('PRAGMA user_version = 42') + apply(database, BUDDY_SCHEMA_MIGRATIONS.filter(migration => migration.version === 42)) + database.exec(`PRAGMA user_version = ${BUDDY_MIGRATION_SCHEMA_VERSION}`) if (wal) { database.exec('PRAGMA journal_mode = WAL; PRAGMA wal_autocheckpoint = 0') database.exec('UPDATE conversations SET title = \'Keep committed WAL data\' WHERE id = \'task\'') @@ -147,20 +157,13 @@ describe('archive schema lineage', { timeout: MIGRATION_TEST_TIMEOUT }, () => { } const paths = wal ? [databasePath, `${databasePath}-wal`] : [databasePath] const original = paths.map(path => readFileSync(path)) - expect(() => openBuddyDatabase({ databasePath })).toThrowError(expect.objectContaining({ name: 'BuddyDatabaseVersionError', reason: 'incompatible schema lineage' })) - paths.forEach((path, index) => expect(readFileSync(path).equals(original[index]!)).toBe(true)) + for (const opener of [openBuddyDatabase, openBuddyMigrationCandidate, openBuddySqliteDatabase]) { + expect(() => opener({ databasePath })).toThrowError(expect.objectContaining({ name: 'BuddyDatabaseVersionError', reason: 'incompatible schema lineage' })) + paths.forEach((path, index) => expect(readFileSync(path).equals(original[index]!)).toBe(true)) + } const check = new DatabaseSync(databasePath, { readOnly: true }) databases.push(check) - expect(check.prepare('PRAGMA user_version').get()?.user_version).toBe(42) + expect(check.prepare('PRAGMA user_version').get()?.user_version).toBe(BUDDY_MIGRATION_SCHEMA_VERSION) expect(check.prepare('SELECT title FROM conversations WHERE id = \'task\'').get()?.title).toBe(wal ? 'Keep committed WAL data' : 'Keep task') }) - - it.each([43, 44, 45, 46, 47, 48, 49])('rejects future schema v%s without changing its bytes', (version) => { - const { databasePath, database } = fixture() - database.exec(`PRAGMA user_version = ${version}`) - database.close() - const original = readFileSync(databasePath) - expect(() => openBuddyDatabase({ databasePath })).toThrow('newer schema version') - expect(readFileSync(databasePath).equals(original)).toBe(true) - }) }) diff --git a/apps/buddy/service/src/storage/__tests__/branchParallelExecution.spec.ts b/apps/buddy/service/src/storage/__tests__/branchParallelExecution.spec.ts new file mode 100644 index 000000000..6a2bb04a6 --- /dev/null +++ b/apps/buddy/service/src/storage/__tests__/branchParallelExecution.spec.ts @@ -0,0 +1,145 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { PrepareTurnRequestInput } from '../turnRequestRepository' +import { mkdtempSync, readFileSync, rmSync, writeFileSync } 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 { productBranchSchema, productPageSchema } from '../../../../shared/productProtocol/queries' +import { ProductQueries } from '../../productProtocol/ProductQueries' +import { ChatControlCommandRepository } from '../ChatControlCommandRepository' +import { createChatQueueRepository } from '../chatQueueRepository' +import { createConversationRepository } from '../conversationRepository' +import { openBuddyDatabase, openBuddyMigrationCandidate } from '../database' +import { createExecutionRepository, executionFence } from '../executionRepository' +import { createRunRepository } from '../runRepository' +import { actorThreadId, createThreadBindingRepository } from '../threadBindingRepository' +import { createTurnRequestRepository, TurnRequestConflictError } from '../turnRequestRepository' + +const cleanups: Array<() => void> = [] +afterEach(() => { + for (const clean of cleanups.splice(0).reverse()) clean() +}) +const now = '2026-11-09T00:00:00.000Z' +function input(id: string, branchId: string): PrepareTurnRequestInput { + return { conversationId: 'conversation', branchId, spaceId: null, createdAt: now, draft: null, requestId: `request-${id}`, requestFingerprint: id, runId: id, userMessageId: `input-${id}`, title: null, userMessageContent: { userContent: createBuddyUserContent(id), resourceSnapshots: [] }, approvalPolicy: 'policy', executionProfile: 'workspace_write', attachmentBindings: [], provider: 'fixture', model: 'fixture', runInput: { attachmentIds: [], contextItems: [], prompt: id, reasoning: null, serviceTier: null } } +} +function fixture() { + const database = openBuddyMigrationCandidate({ databasePath: ':memory:' }) + cleanups.push(() => database.close()) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + const conversations = createConversationRepository(database) + conversations.create({ id: 'conversation', branchId: 'A', spaceId: null, title: null, approvalPolicy: 'policy', executionProfile: 'workspace_write', createdAt: now }) + conversations.createBranch({ id: 'B', conversationId: 'conversation', parentBranchId: null, forkedFromMessageId: null, createdAt: now, activate: false }) + return { database, conversations, turns: createTurnRequestRepository(database), runs: createRunRepository(database), queue: createChatQueueRepository(database), executions: createExecutionRepository(database) } +} +function rows(database: DatabaseSync) { + return { + runs: database.prepare('SELECT id, branch_id, thread_id, pi_session_file FROM runs ORDER BY id').all(), + threads: database.prepare('SELECT * FROM execution_threads ORDER BY id').all(), + bindings: database.prepare('SELECT * FROM execution_thread_bindings ORDER BY thread_id').all(), + } +} + +describe('independent conversation branches', () => { + it('admits two root branches with distinct threads while preserving the same-branch lane fence', () => { + const f = fixture() + f.turns.prepare({ ...input('run-A', 'A'), expectedActorModelSelection: null }) + f.turns.prepare({ ...input('run-B', 'B'), expectedActorModelSelection: null }) + const a = f.runs.findById('run-A')! + const b = f.runs.findById('run-B')! + expect(a.threadId).not.toBe(b.threadId) + expect(() => f.turns.prepare(input('duplicate-A', 'A'))).toThrow(TurnRequestConflictError) + expect(() => f.turns.prepare(input('foreign', 'missing'))).toThrow(TurnRequestConflictError) + f.conversations.activateBranch({ conversationId: 'conversation', branchId: 'B', updatedAt: now }) + for (const runId of ['run-A', 'run-B']) { + const execution = f.executions.find(runId)! + expect(() => f.executions.assertCurrent(executionFence(execution, execution))).not.toThrow() + } + f.runs.reconcileTerminal('run-A', 'cancelled', now, null) + const live = f.executions.find('run-B')! + expect(() => f.executions.assertCurrent(executionFence(live, live))).not.toThrow() + expect(f.runs.findById('run-B')?.status).toBe('queued') + }) + + it('keeps queue lookup, cancellation and recovery on the explicit branch and reports public branch counts', () => { + const f = fixture() + f.turns.prepare(input('run-A', 'A')) + f.turns.prepare(input('run-B', 'B')) + f.queue.enqueue(input('queued-A', 'A')) + f.queue.enqueue(input('queued-B', 'B')) + const a = { conversationId: 'conversation', branchId: 'A' } + const b = { conversationId: 'conversation', branchId: 'B' } + expect(f.queue.activeRun(a, 'default')?.id).toBe('run-A') + expect(f.queue.latestRun(b, 'default')?.id).toBe('run-B') + expect(f.queue.continuationScopes()).toEqual([a, b]) + expect(f.queue.continuationScope(b)).toEqual(b) + f.queue.pause(a.conversationId, a.branchId) + expect(f.queue.list(a)[0]?.state).toBe('paused') + expect(f.queue.list(b)[0]?.state).toBe('waiting') + const queries = new ProductQueries(f.database, 'desktop-local', 'instance') + const branches = productPageSchema(productBranchSchema).parse(queries.read('conversations.listBranches', { conversationId: 'conversation', limit: 100, cursor: null })) + expect(branches.items.map(branch => ({ id: branch.id, runningCount: branch.runningCount, queuedCount: branch.queuedCount }))).toEqual([{ id: 'A', runningCount: 1, queuedCount: 1 }, { id: 'B', runningCount: 1, queuedCount: 1 }]) + }) + + it('forks edit and regenerate from explicit immutable sources without moving the default or blocking another running branch', () => { + const f = fixture() + f.turns.prepare(input('source', 'A')) + f.runs.reconcileTerminal('source', 'completed', now, null) + f.turns.prepare(input('run-B', 'B')) + const fork = f.turns.edit({ ...input('edit', 'edit-branch'), parentBranchId: 'A', sourceUserMessageId: 'input-source', forkedFromMessageId: null }) + expect(fork.branchId).toBe('edit-branch') + expect(f.runs.findById('run-B')?.status).toBe('queued') + const model = f.conversations.findById('conversation')!.modelSelection + const retry = f.turns.regenerate({ conversationId: 'conversation', parentBranchId: 'A', branchId: 'retry-branch', sourceRunId: 'source', forkedFromMessageId: 'input-source', requestId: 'retry', requestFingerprint: 'retry', runId: 'retry', createdAt: now, approvalPolicy: 'policy', executionProfile: 'workspace_write', provider: 'fixture', model: 'fixture', reasoning: null, serviceTier: null, expectedModelSelection: model }) + expect(retry.branchId).toBe('retry-branch') + expect(f.conversations.findById('conversation')?.activeBranchId).toBe('A') + expect(new Set(f.runs.listForConversation('conversation').map(run => run.threadId)).size).toBe(4) + expect(() => f.turns.edit({ ...input('wrong-edit', 'wrong-branch'), parentBranchId: 'B', sourceUserMessageId: 'input-source', forkedFromMessageId: null })).toThrow(TurnRequestConflictError) + }) + + it('exposes public actor execution controls while hiding NPC ids, events and states', () => { + const f = fixture() + f.turns.prepare(input('root', 'A')) + const root = f.runs.findById('root')! + f.runs.create({ ...root, id: 'actor', rootRunId: root.id, parentRunId: root.id, executionKind: 'actor_child', threadId: createThreadBindingRepository(f.database).createIsolated('conversation', 'default', 'actor', now).id }) + f.runs.create({ ...root, id: 'npc', rootRunId: root.id, parentRunId: root.id, executionKind: 'npc_child', threadId: createThreadBindingRepository(f.database).createIsolated('conversation', 'default', 'npc', now).id }) + const queries = new ProductQueries(f.database, 'desktop-local', 'instance') + expect(queries.read('runs.get', { runId: 'npc' })).toBeNull() + expect(queries.read('runs.getExecutionState', { runId: 'npc' })).toBeNull() + expect(queries.read('runs.listEvents', { runId: 'npc', limit: 100, cursor: null })).toEqual({ items: [], nextCursor: null }) + expect(queries.read('runs.get', { runId: 'actor' })).toMatchObject({ id: 'actor', executionKind: 'actor_child' }) + expect(queries.read('runs.list', { conversationId: 'conversation', branchId: 'A', limit: 100, cursor: null })).toMatchObject({ items: [{ id: 'root' }, { id: 'actor' }] }) + const branches = productPageSchema(productBranchSchema).parse(queries.read('conversations.listBranches', { conversationId: 'conversation', limit: 100, cursor: null })) + expect(branches.items.find(branch => branch.id === 'A')?.runningCount).toBe(2) + const control = new ChatControlCommandRepository(f.database, 'instance') + expect(control.cancelRun({ runId: 'actor', expectedRunGeneration: 0 }).result.cancelRequested).toBe(true) + expect(f.executions.find('actor')?.cancelRequestedAt).not.toBeNull() + expect(f.executions.find('root')?.cancelRequestedAt).toBeNull() + expect(f.executions.find('npc')?.cancelRequestedAt).toBeNull() + }) + + it('upgrades existing bindings without rewriting runs, threads or session bytes and writes new work to branch threads', () => { + const directory = mkdtempSync(join(tmpdir(), 'lexora-branch-upgrade-')) + cleanups.push(() => rmSync(directory, { recursive: true, force: true })) + const path = join(directory, 'buddy.sqlite3') + const sessionFile = join(directory, 'legacy.jsonl') + writeFileSync(sessionFile, '{"legacy":"keep exactly"}\n') + let database = openBuddyDatabase({ databasePath: path }) + const conversations = createConversationRepository(database) + conversations.create({ id: 'conversation', branchId: 'A', spaceId: null, title: null, approvalPolicy: 'policy', executionProfile: 'workspace_write', createdAt: now }) + const run = createRunRepository(database).create({ id: 'legacy', conversationId: 'conversation', branchId: 'A', triggeringMessageId: 'old-input', provider: 'fixture', model: 'fixture', purpose: 'chat', status: 'completed', piSessionFile: sessionFile, startedAt: now, approvalPolicy: 'policy', executionProfile: 'workspace_write' }) + createThreadBindingRepository(database).bind(run.threadId, sessionFile, 'legacy-root') + const before = rows(database) + const bytes = readFileSync(sessionFile) + database.close() + database = openBuddyMigrationCandidate({ databasePath: path }) + cleanups.push(() => database.close()) + expect(rows(database)).toEqual(before) + createTurnRequestRepository(database).prepare(input('new-A', 'A')) + expect(createRunRepository(database).findById('new-A')?.threadId).toBe(actorThreadId('conversation', 'default', 'A')) + expect(createRunRepository(database).findById('new-A')?.piSessionFile).toBeNull() + expect(createRunRepository(database).findById('legacy')?.threadId).toBe(run.threadId) + expect(readFileSync(sessionFile)).toEqual(bytes) + }) +}) diff --git a/apps/buddy/service/src/storage/__tests__/buddyIdentityRepository.spec.ts b/apps/buddy/service/src/storage/__tests__/buddyIdentityRepository.spec.ts index ca9084659..237139953 100644 --- a/apps/buddy/service/src/storage/__tests__/buddyIdentityRepository.spec.ts +++ b/apps/buddy/service/src/storage/__tests__/buddyIdentityRepository.spec.ts @@ -27,7 +27,7 @@ function setup() { } } -function input(id: string, conversationId = 'conversation'): PrepareTurnRequestInput { +function input(id: string, conversationId = 'conversation'): PrepareTurnRequestInput & { draft: NonNullable } { const conversation = createConversationRepository(database).findById(conversationId) const draft = createComposerDraftRepository(database).open({ draftId: `draft-${id}`, @@ -113,7 +113,6 @@ it('retries the complete accepted definition after configuration, member model a const retry = f.runs.findById('retry-captured')! expect(retry.actorSnapshot).toEqual(actorSnapshot) expect(retry).toMatchObject({ buddyId: actor.id, provider: 'fixture', model: 'accepted-model', modelOverride: effectiveModel }) - expect(database.prepare('SELECT actor_snapshot_json FROM runs WHERE id = ?').get('retry-captured')).toEqual(database.prepare('SELECT actor_snapshot_json FROM runs WHERE id = ?').get(original.runId)) expect(database.prepare('SELECT count(*) AS count FROM messages').get()).toEqual({ count: 1 }) expect(f.conversations.findById('conversation')?.buddyId).toBe('default') }) diff --git a/apps/buddy/service/src/storage/__tests__/chatQueueRepository.spec.ts b/apps/buddy/service/src/storage/__tests__/chatQueueRepository.spec.ts index e40dfac8e..5eab47ba0 100644 --- a/apps/buddy/service/src/storage/__tests__/chatQueueRepository.spec.ts +++ b/apps/buddy/service/src/storage/__tests__/chatQueueRepository.spec.ts @@ -21,7 +21,7 @@ function fixture() { databases.push(database) const drafts = createComposerDraftRepository(database) drafts.open({ draftId: 'draft', scope: { kind: 'global' }, initialContent: createBuddyUserContent('initial'), initialModelSelection: null, initialExecutionConfig: { approvalPolicy: 'policy', executionProfile: 'workspace_write' }, now: '2026-09-10T00:00:00.000Z' }) - const input = (id: string): PrepareTurnRequestInput => ({ + const input = (id: string): PrepareTurnRequestInput & { draft: NonNullable } => ({ approvalPolicy: 'policy', executionProfile: 'workspace_write', attachmentBindings: [], @@ -238,15 +238,6 @@ describe('chat queue ownership and delivery', () => { expect(() => f.queue.replay('request-A', 'changed')).toThrow() }) - it('rejects an attachment handoff that does not match the queued message', () => { - const f = fixture() - const input = f.input('invalid') - input.runInput.attachmentIds = ['missing'] - expect(() => f.queue.enqueue(input)).toThrow() - expect(f.queue.list(f.scope)).toEqual([]) - expect(f.drafts.findById('draft')?.revision).toBe(input.draft.expectedRevision) - }) - it('rejects cross-conversation actions and does not dispatch entries from a deleted conversation', () => { const f = fixture() f.queue.enqueue(f.input('A')) diff --git a/apps/buddy/service/src/storage/__tests__/checkpointSqliteStorage.spec.ts b/apps/buddy/service/src/storage/__tests__/checkpointSqliteStorage.spec.ts new file mode 100644 index 000000000..9aaae1165 --- /dev/null +++ b/apps/buddy/service/src/storage/__tests__/checkpointSqliteStorage.spec.ts @@ -0,0 +1,102 @@ +import { mkdtempSync, rmSync, statSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { DatabaseSync } from 'node:sqlite' +import { afterEach, describe, expect, it } from 'vitest' +import { SqliteRunEventLog } from '../../events/SqliteRunEventLog' +import { SqliteRunEventWriter } from '../../events/SqliteRunEventWriter' +import { checkpointSqliteStorage } from '../checkpointSqliteStorage' +import { openBuddyMigrationCandidate } from '../database' +import { getStorageIsolation, guardStorageDatabase } from '../StorageIsolation' +import { seedSqliteRun } from './sqliteRunFixture' + +const databases: DatabaseSync[] = [] +const directories: string[] = [] + +afterEach(() => { + for (const database of databases.splice(0)) { + if (database.isOpen) + database.close() + } + for (const directory of directories.splice(0)) + rmSync(directory, { recursive: true, force: true }) +}) + +function fixture() { + const directory = mkdtempSync(join(tmpdir(), 'lexora-sqlite-checkpoint-')) + directories.push(directory) + const databasePath = join(directory, 'candidate.sqlite3') + const database = openBuddyMigrationCandidate({ databasePath }) + databases.push(database) + guardStorageDatabase(database) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + seedSqliteRun(database, 'run-1') + const writer = new SqliteRunEventWriter(database) + const append = () => writer.append({ runId: 'run-1', type: 'message.delta', payload: { messageId: 'answer', delta: 'x'.repeat(8192) } }) + return { databasePath, database, append, walSize: () => statSync(`${databasePath}-wal`).size } +} + +describe('candidate SQLite durability', () => { + it('returns busy for a real long reader, keeps accepting writes and reclaims after release', () => { + const f = fixture() + f.append() + const reader = new DatabaseSync(f.databasePath, { readOnly: true }) + databases.push(reader) + reader.exec('BEGIN') + expect(reader.prepare('SELECT count(*) AS count FROM run_events').get()).toEqual({ count: 1 }) + f.append() + const started = performance.now() + const result = checkpointSqliteStorage(f.database) + expect(performance.now() - started).toBeLessThan(1000) + expect(result.busy).toBe(1) + expect(result.log).toBeGreaterThan(result.checkpointed) + expect(f.walSize()).toBeGreaterThan(0) + expect(getStorageIsolation(f.database).failure).toBeNull() + expect(f.database.prepare('PRAGMA busy_timeout').get()).toEqual({ timeout: 5000 }) + expect(f.append().sequence).toBe(3) + expect(reader.prepare('SELECT count(*) AS count FROM run_events').get()).toEqual({ count: 1 }) + reader.exec('ROLLBACK') + expect(checkpointSqliteStorage(f.database).busy).toBe(0) + expect(f.walSize()).toBe(0) + expect(f.database.prepare('SELECT count(*) AS count FROM run_events').get()).toEqual({ count: 3 }) + }) + + it('refuses checkpoint inside a business transaction without poisoning the connection', () => { + const f = fixture() + f.database.exec('BEGIN') + expect(() => checkpointSqliteStorage(f.database)).toThrow('transaction') + f.database.exec('ROLLBACK') + expect(getStorageIsolation(f.database).failure).toBeNull() + expect(checkpointSqliteStorage(f.database).busy).toBe(0) + }) + + it('reclaims the WAL when the healthy SQLite event log closes', async () => { + const f = fixture() + const log = new SqliteRunEventLog({ database: f.database }) + await log.append({ runId: 'run-1', type: 'run.started', payload: null }) + expect(f.walSize()).toBeGreaterThan(0) + await log.close() + await log.close() + expect(log.state).toBe('closed') + expect(f.walSize()).toBe(0) + expect(f.database.prepare('SELECT status FROM runs').get()).toEqual({ status: 'running' }) + }) + + it('isolates a real read-only checkpoint failure and preserves its original error', () => { + const f = fixture() + f.append() + const reader = new DatabaseSync(f.databasePath, { readOnly: true }) + databases.push(reader) + const isolation = guardStorageDatabase(reader) + let error: unknown + try { + checkpointSqliteStorage(reader) + } + catch (cause) { + error = cause + } + expect(error).toMatchObject({ code: 'ERR_SQLITE_ERROR', errcode: expect.any(Number) }) + expect(isolation.failure?.cause).toBe(error) + expect(f.database.prepare('SELECT count(*) AS count FROM run_events').get()).toEqual({ count: 1 }) + }) +}) diff --git a/apps/buddy/service/src/storage/__tests__/contentReferenceOwnership.spec.ts b/apps/buddy/service/src/storage/__tests__/contentReferenceOwnership.spec.ts new file mode 100644 index 000000000..24331bf4d --- /dev/null +++ b/apps/buddy/service/src/storage/__tests__/contentReferenceOwnership.spec.ts @@ -0,0 +1,208 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { BuddyUserContentV1 } from '../../../../shared/conversation/buddyUserContent' +import { mkdtempSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { DatabaseSync as Sqlite } from 'node:sqlite' +import { afterEach, describe, expect, it } from 'vitest' +import { createBuddyUserContent } from '../../../../shared/conversation/buddyUserContent' +import { ConversationPurgeService } from '../../conversations/ConversationPurgeService' +import { ProductContentReferences } from '../../productProtocol/ProductContentReferences' +import { ProductQueries } from '../../productProtocol/ProductQueries' +import { mergeBackupDatabase, selectBackupDatabase } from '../backupDatabase' +import { createComposerDraftCommitter } from '../commitComposerDraft' +import { createComposerDraftRepository } from '../composerDraftRepository' +import { createConversationDeletionRepository } from '../conversationDeletionRepository' +import { createConversationRepository } from '../conversationRepository' +import { openBuddyMigrationCandidate, openBuddySqliteDatabase } from '../database' +import { BUDDY_MIGRATION_SCHEMA_VERSION, BUDDY_SCHEMA_MIGRATIONS } from '../schema' +import { rewriteIsolatedSnapshotDatabase } from '../snapshotDatabase' + +const now = '2026-11-01T00:00:00.000Z' +const executionConfig = { approvalPolicy: 'policy', executionProfile: 'workspace_write' } as const +const quoted: BuddyUserContentV1 = { + ...createBuddyUserContent('A file quotation'), + resourceQuotes: [{ id: 'file-quote', text: 'Private historical excerpt', source: { kind: 'file', title: 'fixture.txt', file: { spaceId: 'space', directoryId: 'directory', revision: 1, path: 'fixture.txt' }, format: 'source' } }], +} +const databases = new Set() +const directories: string[] = [] +afterEach(() => { + for (const database of databases) database.close() + databases.clear() + for (const directory of directories.splice(0)) rmSync(directory, { force: true, recursive: true }) +}) +function fixture() { + const home = mkdtempSync(join(tmpdir(), 'lexora-reference-ownership-')) + directories.push(home) + const path = join(home, 'buddy.sqlite3') + const database = openBuddySqliteDatabase({ databasePath: path }) + databases.add(database) + const conversations = createConversationRepository(database) + const drafts = createComposerDraftRepository(database) + const references = new ProductContentReferences(database, 'desktop-local') + const queries = new ProductQueries(database, 'desktop-local', 'instance', references) + const task = (id: string) => conversations.create({ id, branchId: `branch-${id}`, spaceId: null, title: id, createdAt: now, ...executionConfig }) + const draft = (id: string, conversationId: string | null, content = quoted) => drafts.open({ draftId: id, scope: conversationId ? { kind: 'conversation_branch', conversationId, branchId: `branch-${conversationId}` } : { kind: 'task', draftId: id, spaceId: null }, initialContent: content, initialExecutionConfig: executionConfig, initialModelSelection: null, now }) + const clear = (id: string) => drafts.save({ draftId: id, expectedRevision: drafts.findById(id)!.revision, content: createBuddyUserContent(), modelSelection: null, executionConfig, now }) + const project = (id: string) => { + const content = queries.draft(id)!.content + if (content.kind !== 'editable') + throw new Error('Expected editable draft') + return content.content.resourceQuotes![0]!.resourceId + } + const purge = async (id: string) => { + createConversationDeletionRepository(database).commit(id, now) + await new ConversationPurgeService({ database, home, storageAuthority: 'sqlite' }).purge(id) + } + return { database, home, path, references, queries, drafts, task, draft, clear, project, purge } +} + +describe('durable content reference ownership', () => { + it('forgets an interned file quotation after draft removal and task purge', async () => { + const f = fixture() + f.task('task') + f.draft('draft', 'task') + const id = f.project('draft') + f.clear('draft') + expect(f.references.resolve(id)).toEqual(quoted.resourceQuotes![0]) + expect(() => f.database.exec('DELETE FROM product_content_reference_owners')).toThrow('CONTENT_REFERENCE_OWNER_IMMUTABLE') + await f.purge('task') + expect(f.database.prepare('SELECT quote_json FROM product_content_references WHERE id = ?').get(id)).toBeUndefined() + expect(() => f.references.resolve(id)).toThrow('RESOURCE_UNAVAILABLE') + expect(f.database.prepare('PRAGMA foreign_key_check').all()).toEqual([]) + }) + + it('retains historical shared ownership even when both drafts no longer contain the quote', async () => { + const f = fixture() + f.task('first') + f.task('second') + f.draft('draft-first', 'first') + f.draft('draft-second', 'second') + const id = f.project('draft-first') + expect(f.database.prepare('SELECT owner_id FROM product_content_reference_owners WHERE reference_id = ? ORDER BY owner_id').all(id)) + .toEqual([{ owner_id: 'first' }, { owner_id: 'second' }]) + f.clear('draft-first') + f.clear('draft-second') + await f.purge('first') + expect(f.references.resolve(id)).toEqual(quoted.resourceQuotes![0]) + expect(f.database.prepare('SELECT owner_id FROM product_content_reference_owners WHERE reference_id = ?').all(id)).toEqual([{ owner_id: 'second' }]) + await f.purge('second') + expect(() => f.references.resolve(id)).toThrow('RESOURCE_UNAVAILABLE') + }) + + it('attaches a preprojected Desktop quote when it is first persisted in a task draft', async () => { + const f = fixture() + const id = f.references.project(quoted).resourceQuotes![0]!.resourceId + f.task('task') + f.draft('draft', 'task') + f.clear('draft') + await f.purge('task') + expect(() => f.references.resolve(id)).toThrow('RESOURCE_UNAVAILABLE') + }) + + it('transfers historical unbound draft ownership when the draft becomes a task', async () => { + const f = fixture() + f.draft('draft', null) + const id = f.project('draft') + const cleared = f.clear('draft') + f.task('task') + createComposerDraftCommitter(f.database)({ draftId: 'draft', expectedRevision: cleared.revision, conversationId: 'task', branchId: 'branch-task', spaceId: null, updatedAt: now, ...executionConfig }) + expect(f.database.prepare('SELECT owner_kind, owner_id FROM product_content_reference_owners WHERE reference_id = ?').all(id)) + .toEqual([{ owner_kind: 'conversation', owner_id: 'task' }]) + await f.purge('task') + expect(() => f.references.resolve(id)).toThrow('RESOURCE_UNAVAILABLE') + }) + + it('migrates legacy quotes and collects exact source owners on first modern projection', async () => { + const home = mkdtempSync(join(tmpdir(), 'lexora-reference-migration-')) + directories.push(home) + const path = join(home, 'buddy.sqlite3') + const previous = new Sqlite(path) + databases.add(previous) + for (const migration of BUDDY_SCHEMA_MIGRATIONS.filter(migration => migration.version <= 42)) { + previous.exec(migration.sql) + previous.exec(`PRAGMA user_version = ${migration.version}`) + } + const conversations = createConversationRepository(previous) + for (const id of ['draft-owner', 'message-owner', 'queue-owner', 'different-quote']) + conversations.create({ id, branchId: `branch-${id}`, spaceId: null, title: id, createdAt: now, ...executionConfig }) + const drafts = createComposerDraftRepository(previous) + const different = { ...quoted, resourceQuotes: [{ ...quoted.resourceQuotes![0]!, text: 'Different excerpt with the same local quote ID' }] } + for (const [id, owner, content] of [['draft', 'draft-owner', quoted], ['different', 'different-quote', different]] as const) { + drafts.open({ draftId: id, scope: { kind: 'conversation_branch', conversationId: owner, branchId: `branch-${owner}` }, initialContent: content, initialExecutionConfig: executionConfig, initialModelSelection: null, now }) + } + previous.prepare(`INSERT INTO messages(id,conversation_id,branch_id,role,content_json,created_at) + VALUES ('message','message-owner','branch-message-owner','user',?,?)`).run(JSON.stringify({ userContent: quoted }), now) + previous.prepare(`INSERT INTO chat_queue(id,conversation_id,branch_id,request_id,request_fingerprint,prepared_json,state,created_at) + VALUES ('queue','queue-owner','branch-queue-owner','request','fingerprint',?,'cancelled',?)`).run(JSON.stringify({ userMessageContent: { userContent: quoted } }), now) + expect(previous.prepare(`SELECT name FROM sqlite_master WHERE name IN ('product_content_references','product_content_reference_owners')`).all()).toEqual([]) + previous.exec(BUDDY_SCHEMA_MIGRATIONS.find(migration => migration.version === BUDDY_MIGRATION_SCHEMA_VERSION)!.sql) + previous.exec(`PRAGMA user_version = ${BUDDY_MIGRATION_SCHEMA_VERSION}`) + expect(previous.prepare('SELECT * FROM product_content_references').all()).toEqual([]) + expect(previous.prepare('SELECT * FROM product_content_reference_owners').all()).toEqual([]) + expect(previous.prepare('SELECT content_json FROM composer_drafts WHERE id = \'draft\'').get()?.content_json).toBe(JSON.stringify(quoted)) + previous.close() + databases.delete(previous) + const candidate = openBuddyMigrationCandidate({ databasePath: path }) + expect(candidate.prepare('PRAGMA user_version').get()?.user_version).toBe(BUDDY_MIGRATION_SCHEMA_VERSION) + candidate.exec(`UPDATE storage_profile SET authority = 'sqlite', migration_state = 'ready'`) + candidate.close() + const database = openBuddySqliteDatabase({ databasePath: path }) + databases.add(database) + const references = new ProductContentReferences(database, 'desktop-local') + const id = references.project(quoted).resourceQuotes![0]!.resourceId + expect(database.prepare('SELECT owner_kind,owner_id FROM product_content_reference_owners WHERE reference_id = ? ORDER BY owner_id').all(id)) + .toEqual(['draft-owner', 'message-owner', 'queue-owner'].map(owner_id => ({ owner_kind: 'conversation', owner_id }))) + database.prepare(`UPDATE composer_drafts SET content_json = ? WHERE id = 'draft'`).run(JSON.stringify(createBuddyUserContent())) + for (const owner of ['draft-owner', 'message-owner', 'queue-owner']) { + createConversationDeletionRepository(database).commit(owner, now) + await new ConversationPurgeService({ database, home, storageAuthority: 'sqlite' }).purge(owner) + if (owner !== 'queue-owner') + expect(references.resolve(id)).toEqual(quoted.resourceQuotes![0]) + } + expect(() => references.resolve(id)).toThrow('RESOURCE_UNAVAILABLE') + expect(database.prepare('SELECT content_json FROM composer_drafts WHERE id = \'different\'').get()?.content_json).toBe(JSON.stringify(different)) + expect(database.prepare('PRAGMA foreign_key_check').all()).toEqual([]) + }) + + it('merges saved ownership without deleting unrelated target references', async () => { + const source = fixture() + source.task('source-task') + source.draft('source-draft', 'source-task') + const importedId = source.project('source-draft') + source.clear('source-draft') + const target = fixture() + target.task('target-task') + target.draft('target-draft', 'target-task') + const retainedId = target.project('target-draft') + target.clear('target-draft') + target.database.close() + databases.delete(target.database) + await rewriteIsolatedSnapshotDatabase(target.path, database => mergeBackupDatabase(database, source.database, ['tasks'])) + const database = openBuddySqliteDatabase({ databasePath: target.path }) + databases.add(database) + createConversationDeletionRepository(database).commit('source-task', now) + await new ConversationPurgeService({ database, home: target.home, storageAuthority: 'sqlite' }).purge('source-task') + expect(database.prepare('SELECT 1 FROM product_content_references WHERE id = ?').get(importedId)).toBeUndefined() + expect(new ProductContentReferences(database, 'desktop-local').resolve(retainedId)).toEqual(quoted.resourceQuotes![0]) + expect(database.prepare('PRAGMA foreign_key_check').all()).toEqual([]) + }) + + it('preserves ownership through selective task backup and reopening before purge', async () => { + const f = fixture() + f.task('task') + f.draft('draft', 'task') + const id = f.project('draft') + f.clear('draft') + f.database.close() + databases.delete(f.database) + await rewriteIsolatedSnapshotDatabase(f.path, database => selectBackupDatabase(database, ['tasks'])) + const database = openBuddySqliteDatabase({ databasePath: f.path }) + databases.add(database) + expect(database.prepare('SELECT owner_kind, owner_id FROM product_content_reference_owners WHERE reference_id = ?').all(id)) + .toEqual([{ owner_kind: 'conversation', owner_id: 'task' }]) + createConversationDeletionRepository(database).commit('task', now) + await new ConversationPurgeService({ database, home: f.home, storageAuthority: 'sqlite' }).purge('task') + expect(() => new ProductContentReferences(database, 'desktop-local').resolve(id)).toThrow('RESOURCE_UNAVAILABLE') + }) +}) diff --git a/apps/buddy/service/src/storage/__tests__/conversationArchivePurge.spec.ts b/apps/buddy/service/src/storage/__tests__/conversationArchivePurge.spec.ts new file mode 100644 index 000000000..3a29563f1 --- /dev/null +++ b/apps/buddy/service/src/storage/__tests__/conversationArchivePurge.spec.ts @@ -0,0 +1,343 @@ +import type { DatabaseSync } from 'node:sqlite' +import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { DatabaseSync as Sqlite } from 'node:sqlite' +import { afterEach, describe, expect, it } from 'vitest' +import { ConversationPurgeService } from '../../conversations/ConversationPurgeService' +import { createAttachmentPersistenceRepository } from '../attachmentPersistenceRepository' +import { selectBackupDatabase } from '../backupDatabase' +import { createConversationArchiveRepository } from '../conversationArchiveRepository' +import { createConversationDeletionRepository } from '../conversationDeletionRepository' +import { createConversationRepository } from '../conversationRepository' +import { openBuddyDatabase, openBuddySqliteDatabase } from '../database' +import { createExecutionRepository, executionFence } from '../executionRepository' +import { BUDDY_SCHEMA_MIGRATIONS } from '../schema' +import { rewriteIsolatedSnapshotDatabase } from '../snapshotDatabase' +import { sqliteRunFixtureTime as now, seedSqliteRun } from './sqliteRunFixture' + +const databases: DatabaseSync[] = [] +const directories: string[] = [] +afterEach(() => { + for (const db of databases.splice(0)) + db.close() + for (const directory of directories.splice(0)) + rmSync(directory, { recursive: true, force: true }) +}) +function fixture() { + const home = mkdtempSync(join(tmpdir(), 'lexora-purge-')) + directories.push(home) + const database = openBuddySqliteDatabase({ databasePath: ':memory:' }) + databases.push(database) + const deletion = createConversationDeletionRepository(database) + const archive = createConversationArchiveRepository(database) + const purge = new ConversationPurgeService({ database, home, storageAuthority: 'sqlite' }) + return { home, database, deletion, archive, purge } +} +function seed(database: DatabaseSync, id = 'task-1') { + createConversationRepository(database).create({ id, branchId: `branch-${id}`, spaceId: null, title: 'Fixture task', createdAt: now, approvalPolicy: 'policy', executionProfile: 'workspace_write' }) + seedSqliteRun(database, `run-${id}`, { conversationId: id }) + database.prepare(`UPDATE runs SET status = 'completed' WHERE conversation_id = ?`).run(id) + database.prepare(`UPDATE execution_state SET phase = 'closed', cleanup_state = 'clean' WHERE run_id = ?`).run(`run-${id}`) + database.prepare('INSERT INTO task_attention(conversation_id, seen_run_id) VALUES (?, ?)').run(id, `run-${id}`) +} +function content(home: string, id = 'task-1') { + const directory = join(home, 'conversations', id) + mkdirSync(directory, { recursive: true }) + const path = join(directory, 'session.jsonl') + writeFileSync(path, 'private content') + return { directory, path } +} +function managed(database: DatabaseSync, path: string) { + database.prepare(`INSERT INTO managed_files (id,path,size_bytes,sha256,state,durability,created_at,updated_at) + VALUES ('file-1',?,15,?,'ready','file-sync',?,?)`).run(path, 'a'.repeat(64), now, now) + database.prepare(`INSERT INTO attachments (id,message_id,draft_id,name,mime_type,size_bytes,stored_path,created_at) + VALUES ('attachment-1','input-run-task-1',NULL,'fixture','text/plain',15,?,?)`).run(path, now) +} + +describe('conversation archive and physical purge', () => { + it('blocks expiry, legacy deletion and physical purge when terminal execution evidence is missing', async () => { + const { database, home, deletion, archive, purge } = fixture() + seed(database) + const { path } = content(home) + archive.archive('task-1', now) + archive.setPolicy(3) + const due = '2026-11-01T00:00:00.000Z' + expect(archive.isDue('task-1', due)).toBe(true) + database.prepare('DELETE FROM execution_state WHERE run_id = ?').run('run-task-1') + expect(archive.due(due)).toEqual([]) + expect(archive.isDue('task-1', due)).toBe(false) + database.prepare('UPDATE conversations SET archived_at = NULL, deleted_at = ? WHERE id = ?').run(now, 'task-1') + expect(deletion.commitLegacyRemoved('task-1', now)).toBeNull() + deletion.commit('task-1', now) + await expect(purge.purge('task-1')).rejects.toMatchObject({ code: 'CONVERSATION_RESOURCES_BUSY' }) + expect(readFileSync(path, 'utf8')).toBe('private content') + expect(database.prepare('SELECT id FROM runs WHERE id = ?').get('run-task-1')).toEqual({ id: 'run-task-1' }) + }) + + it('upgrades v41 tombstones to v42 without scheduling purge and retains usage after explicit deletion', async () => { + const home = mkdtempSync(join(tmpdir(), 'lexora-archive-migration-')) + directories.push(home) + const path = join(home, 'fixture.sqlite3') + const previous = new Sqlite(path) + for (const migration of BUDDY_SCHEMA_MIGRATIONS.filter(item => item.version <= 41)) { + previous.exec(migration.sql) + previous.exec(`PRAGMA user_version = ${migration.version}`) + } + previous.exec(` + INSERT INTO conversations(id,created_at,updated_at,deleted_at) VALUES ('task-1','${now}','${now}','${now}'); + INSERT INTO conversation_branches(id,conversation_id,created_at) VALUES ('branch-task-1','task-1','${now}'); + INSERT INTO conversation_members(conversation_id,actor_id,joined_at) SELECT 'task-1',default_buddy_id,'${now}' FROM buddy_catalog; + INSERT INTO messages(id,conversation_id,branch_id,role,content_json,created_at) VALUES ('input','task-1','branch-task-1','user','{}','${now}'); + INSERT INTO runs(id,conversation_id,branch_id,triggering_message_id,provider,model,purpose,status,started_at) + VALUES ('run','task-1','branch-task-1','input','fixture','fixture','chat','completed','${now}'); + UPDATE execution_state SET phase = 'closed', cleanup_state = 'clean'; + INSERT INTO task_attention(conversation_id,seen_run_id) VALUES ('task-1','run'); + INSERT INTO usage_records(id,run_id,source_entry_id,provider,model,purpose,input_tokens,output_tokens, + cache_read_tokens,cache_write_tokens,total_tokens,input_cost,output_cost,cache_read_cost,cache_write_cost,total_cost,created_at) + VALUES ('usage','run','entry','fixture','fixture','chat',10,20,0,0,30,1,2,0,0,3,'${now}'); + `) + const { directory } = content(home) + previous.close() + const database = openBuddyDatabase({ databasePath: path }) + databases.push(database) + const deletion = createConversationDeletionRepository(database) + const archive = createConversationArchiveRepository(database) + expect(deletion.pending()).toEqual([]) + archive.setPolicy(3) + expect(archive.due('2027-01-01T00:00:00.000Z')).toEqual([]) + expect(archive.list({ kind: 'removed' }).items).toMatchObject([{ id: 'task-1', deletedAt: now, archivedAt: null }]) + expect(archive.restore('task-1', now)).toBe(true) + expect(database.prepare('PRAGMA user_version').get()?.user_version).toBe(42) + expect(existsSync(directory)).toBe(true) + deletion.commit('task-1', now) + await new ConversationPurgeService({ database, home, storageAuthority: 'legacy_jsonl' }).purge('task-1') + expect(existsSync(directory)).toBe(false) + expect(database.prepare('SELECT run_id, detached, total_tokens, total_cost FROM usage_records').get()) + .toEqual({ run_id: null, detached: 1, total_tokens: 30, total_cost: 3 }) + expect(database.prepare('PRAGMA foreign_key_check').all()).toEqual([]) + }) + + it('removes task, execution, attachment and session data but preserves detached usage and external workspaces', async () => { + const { database, home, deletion, purge } = fixture() + seed(database) + const { path, directory } = content(home) + managed(database, path) + const external = join(home, 'external-workspace') + mkdirSync(external) + writeFileSync(join(external, 'keep.txt'), 'user document') + database.prepare('UPDATE runs SET pi_session_file = ?').run(join(external, 'keep.txt')) + database.exec(`INSERT INTO usage_records (id,run_id,source_entry_id,provider,model,purpose,input_tokens,output_tokens, + cache_read_tokens,cache_write_tokens,total_tokens,input_cost,output_cost,cache_read_cost,cache_write_cost,total_cost,created_at) + VALUES ('usage','run-task-1','entry','fixture','fixture','chat',10,20,0,0,30,1,2,0,0,3,'${now}')`) + deletion.commit('task-1', now) + await purge.purge('task-1') + expect(existsSync(directory)).toBe(false) + expect(readFileSync(join(external, 'keep.txt'), 'utf8')).toBe('user document') + for (const table of ['conversations', 'messages', 'runs', 'execution_state', 'execution_threads', 'attachments', 'managed_files', 'file_references', 'conversation_deletion_jobs', 'conversation_members', 'task_attention']) + expect(database.prepare(`SELECT count(*) AS count FROM ${table}`).get()?.count, table).toBe(0) + expect(database.prepare('SELECT run_id, invocation_id, total_tokens, total_cost FROM usage_records').get()).toMatchObject({ run_id: null, invocation_id: null, total_tokens: 30, total_cost: 3 }) + expect(database.prepare('PRAGMA foreign_key_check').all()).toEqual([]) + await purge.purge('task-1') + }) + + it('refuses outside references before touching files, and retries after they are released', async () => { + const { database, home, deletion, purge, archive } = fixture() + seed(database) + const { path } = content(home) + managed(database, path) + database.exec('INSERT INTO file_references VALUES (\'file-1\',\'export\',\'other-task\',\'export-1\')') + deletion.commit('task-1', now) + await expect(purge.purge('task-1')).rejects.toMatchObject({ code: 'CONVERSATION_FILES_REFERENCED' }) + expect(readFileSync(path, 'utf8')).toBe('private content') + expect(deletion.pending()).toEqual([]) + expect(archive.list({ kind: 'deleting' }).items[0]).toMatchObject({ deletionStatus: 'failed', errorCode: 'CONVERSATION_FILES_REFERENCED' }) + database.exec('DELETE FROM file_references WHERE kind = \'export\'') + deletion.retry('task-1', now) + await purge.purge('task-1') + expect(existsSync(path)).toBe(false) + }) + + it('rejects symlinks including ancestors and does not touch their targets', async () => { + const { database, home, deletion, purge } = fixture() + seed(database) + const outside = join(home, 'outside') + mkdirSync(outside) + writeFileSync(join(outside, 'keep.txt'), 'keep') + mkdirSync(join(home, 'conversations')) + symlinkSync(outside, join(home, 'conversations', 'task-1'), 'dir') + deletion.commit('task-1', now) + await expect(purge.purge('task-1')).rejects.toMatchObject({ code: 'CONVERSATION_FILE_BOUNDARY' }) + expect(readFileSync(join(outside, 'keep.txt'), 'utf8')).toBe('keep') + expect(database.prepare('SELECT 1 FROM conversations').get()).toBeTruthy() + rmSync(join(home, 'conversations', 'task-1')) + deletion.retry('task-1', now) + await purge.purge('task-1') + expect(readFileSync(join(outside, 'keep.txt'), 'utf8')).toBe('keep') + }) + + it('persists cleanup truth across a partial filesystem failure and a fresh service retry', async () => { + const { database, home, deletion, purge } = fixture() + seed(database) + const { directory } = content(home) + const blocked = join(directory, 'blocked') + mkdirSync(blocked) + writeFileSync(join(blocked, 'private.txt'), 'private') + chmodSync(blocked, 0o500) + deletion.commit('task-1', now) + try { + await expect(purge.purge('task-1')).rejects.toMatchObject({ code: 'CONVERSATION_PURGE_FAILED' }) + expect(database.prepare('SELECT manifest_json FROM conversation_deletion_jobs').get()?.manifest_json).toBeTruthy() + expect(database.prepare('SELECT 1 FROM messages').get()).toBeTruthy() + } + finally { chmodSync(blocked, 0o700) } + deletion.retry('task-1', now) + await new ConversationPurgeService({ database, home, storageAuthority: 'sqlite' }).purge('task-1') + expect(existsSync(directory)).toBe(false) + }) + + it('does not purge historical tombstones from old run completion and scopes legacy jobs strictly', async () => { + const { database, home, deletion, purge, archive } = fixture() + seed(database) + const { path } = content(home) + database.prepare('UPDATE conversations SET deleted_at = ?').run(now) + deletion.recoverLegacy('task-1', now) + await purge.purge('task-1') + expect(existsSync(path)).toBe(true) + expect(deletion.pending()).toEqual([]) + expect(deletion.commitLegacyRemoved('task-1', now)).not.toBeNull() + deletion.failed('task-1', 'CONVERSATION_PURGE_FAILED', now) + expect(archive.list({ kind: 'removed' }).items[0]?.deletionStatus).toBe('failed') + expect(archive.restore('task-1', now)).toBe(false) + seed(database, 'active') + expect(deletion.commitLegacyRemoved('active', now)).toBeNull() + archive.archive('active', now) + expect(deletion.commitLegacyRemoved('active', now)).toBeNull() + }) + + it.each([ + ['archived', 'queued'], + ['deleted', 'running'], + ] as const)('finishes cancellation of a %s task with a %s run without reopening admission', (state, status) => { + const { database, archive, deletion } = fixture() + seed(database) + database.prepare('UPDATE runs SET status = ?').run(status) + database.prepare('UPDATE execution_state SET phase = ?, cleanup_state = \'pending\'').run(status === 'queued' ? 'queued' : 'working') + if (state === 'archived') + archive.archive('task-1', now) + else + deletion.commit('task-1', now) + expect(() => database.exec('UPDATE runs SET generation = generation + 2')).toThrow('CONVERSATION_NOT_ACTIVE') + expect(() => database.prepare('UPDATE runs SET status = ?').run(status === 'queued' ? 'running' : 'queued')).toThrow('CONVERSATION_NOT_ACTIVE') + expect(() => seedSqliteRun(database, 'late', { conversationId: 'task-1' })).toThrow('CONVERSATION_NOT_ACTIVE') + const executions = createExecutionRepository(database) + const cancelled = executions.requestCancel('run-task-1', now)[0]! + expect(cancelled).toMatchObject({ cancelRequestedAt: now, run: { generation: 1, status } }) + const closing = executions.beginClosing(executionFence(cancelled, cancelled), 'cancel') + expect(closing).toMatchObject({ phase: 'closing', run: { generation: 2, status } }) + expect(executions.finishClosing(executionFence(closing, closing), 'cancelled', now, 'RUN_CANCELLED').run) + .toMatchObject({ status: 'cancelled', errorCode: 'RUN_CANCELLED' }) + expect(() => database.exec('UPDATE runs SET status = \'queued\', generation = generation + 1')).toThrow('CONVERSATION_NOT_ACTIVE') + }) + + it('allows failure closure of an inactive task but rejects generation changes on a closed execution', () => { + const { database, deletion } = fixture() + seed(database) + database.exec('UPDATE runs SET status = \'running\'') + deletion.commit('task-1', now) + expect(() => database.exec('UPDATE runs SET generation = generation + 1')).toThrow('CONVERSATION_NOT_ACTIVE') + database.exec('UPDATE execution_state SET phase = \'working\', cleanup_state = \'pending\'') + const executions = createExecutionRepository(database) + const record = executions.find('run-task-1')! + const closing = executions.beginClosing(executionFence(record, record), 'failure') + expect(executions.finishClosing(executionFence(closing, closing), 'failed', now, 'RUNTIME_RESTARTED').run) + .toMatchObject({ status: 'failed', errorCode: 'RUNTIME_RESTARTED' }) + expect(() => database.exec('UPDATE runs SET status = \'running\'')).toThrow('CONVERSATION_NOT_ACTIVE') + }) + + it('keeps in-flight file preparations and task content intact', async () => { + const { database, home, deletion, purge } = fixture() + seed(database) + const { path } = content(home) + managed(database, path) + database.exec(`INSERT INTO file_preparations(id,operation_id,owner_instance,actor_id,data_generation,kind,target_owner_id,state,reserved_bytes,created_at,updated_at) + SELECT 'file-1','operation','instance',local_actor_id,data_generation,'message','input-run-task-1','published',15,'${now}','${now}' FROM storage_profile`) + deletion.commit('task-1', now) + await expect(purge.purge('task-1')).rejects.toMatchObject({ code: 'CONVERSATION_RESOURCES_BUSY' }) + expect(database.prepare('SELECT state FROM file_preparations').get()?.state).toBe('published') + expect(readFileSync(path, 'utf8')).toBe('private content') + database.exec('UPDATE file_preparations SET state = \'settled\', reserved_bytes = 0') + deletion.retry('task-1', now) + await purge.purge('task-1') + expect(database.prepare('SELECT * FROM file_preparations').all()).toEqual([]) + }) + + it('purges branches, child execution trees, stored events and immutable quote projections', async () => { + const { database, home, deletion, purge } = fixture() + seed(database) + database.prepare('INSERT INTO conversation_branches(id,conversation_id,parent_branch_id,created_at) VALUES (?,?,?,?)').run('fork', 'task-1', 'branch-task-1', now) + seedSqliteRun(database, 'child', { conversationId: 'task-1' }) + database.exec('UPDATE runs SET parent_run_id = \'run-task-1\', root_run_id = \'run-task-1\', execution_kind = \'npc_child\', branch_id = \'fork\' WHERE id = \'child\'; UPDATE runs SET status = \'completed\'; UPDATE execution_state SET phase = \'closed\', cleanup_state = \'clean\'') + database.prepare('INSERT INTO run_events(run_id,sequence,event_type,payload_json,created_at) VALUES (?,?,?,?,?)').run('child', 1, 'fixture', '{"text":"secret result"}', now) + database.exec(`INSERT INTO product_content_references(id,owner_actor_id,data_generation,quote_json,created_at) + SELECT 'quote',local_actor_id,data_generation,'{"id":"quote","text":"secret quotation","source":{"conversationId":"task-1"}}','${now}' FROM storage_profile`) + database.exec('INSERT INTO product_content_reference_owners VALUES (\'quote\', \'conversation\', \'task-1\')') + expect(() => database.exec('DELETE FROM product_content_references')).toThrow('IMMUTABLE') + const { directory } = content(home) + mkdirSync(join(directory, 'session', 'fork'), { recursive: true }) + writeFileSync(join(directory, 'session', 'fork', 'child.jsonl'), 'secret session') + deletion.commit('task-1', now) + await purge.purge('task-1') + for (const table of ['conversation_branches', 'runs', 'execution_work_attempts', 'run_events', 'product_content_references']) + expect(database.prepare(`SELECT * FROM ${table}`).all(), table).toEqual([]) + expect(existsSync(directory)).toBe(false) + }) + + it('backs up detached usage without retaining runs, invocations or their bodies', async () => { + const home = mkdtempSync(join(tmpdir(), 'lexora-purge-backup-')) + directories.push(home) + const path = join(home, 'buddy.sqlite3') + const database = openBuddySqliteDatabase({ databasePath: path }) + try { + seed(database) + database.exec(`INSERT INTO extension_invocations(id,extension_id,action_id,conversation_id,trigger,status,started_at,result_message) + VALUES ('invocation','extension','action','task-1','manual','completed','${now}','private action result'); + INSERT INTO usage_records (id,invocation_id,source_entry_id,provider,model,purpose,input_tokens,output_tokens, + cache_read_tokens,cache_write_tokens,total_tokens,input_cost,output_cost,cache_read_cost,cache_write_cost,total_cost,created_at) + VALUES ('usage','invocation','entry','fixture','fixture','extension.action',1,2,0,0,3,1,2,0,0,3,'${now}')`) + createConversationDeletionRepository(database).commit('task-1', now) + await new ConversationPurgeService({ database, home, storageAuthority: 'sqlite' }).purge('task-1') + } + finally { database.close() } + await rewriteIsolatedSnapshotDatabase(path, candidate => selectBackupDatabase(candidate, ['tasks'])) + const backup = openBuddySqliteDatabase({ databasePath: path }) + databases.push(backup) + expect(backup.prepare('SELECT run_id,invocation_id,detached,total_tokens,total_cost FROM usage_records').get()).toMatchObject({ run_id: null, invocation_id: null, detached: 1, total_tokens: 3, total_cost: 3 }) + expect(backup.prepare('SELECT * FROM extension_invocations').all()).toEqual([]) + expect(backup.prepare('PRAGMA foreign_key_check').all()).toEqual([]) + }) + + it('purges a genuinely prepared and settled attachment with its immutable historical staging path', async () => { + const { database, home, deletion, purge } = fixture() + seed(database) + const { path } = content(home) + const persistence = createAttachmentPersistenceRepository(database) + const file = persistence.prepare({ operationId: 'import', ownerInstance: 'fixture', availableBytes: 1000, stagingDirectory: join(home, 'managed-files', 'staging'), kind: 'message', targetOwnerId: 'input-run-task-1', path, sizeBytes: 15 }) + persistence.publish(path, { sizeBytes: 15, sha256: 'a'.repeat(64), durability: 'file-and-directory-sync' }) + database.prepare(`INSERT INTO attachments (id,message_id,draft_id,name,mime_type,size_bytes,stored_path,created_at) + VALUES ('real-attachment','input-run-task-1',NULL,'fixture','text/plain',15,?,?)`).run(path, now) + persistence.settle([path], 'commit') + expect(persistence.get(path)?.temporaryPath).toBe(file.temporaryPath) + mkdirSync(join(home, 'managed-files', 'staging'), { recursive: true }) + writeFileSync(file.temporaryPath!, 'leftover owned staging') + const unrelated = join(home, 'managed-files', 'staging', 'unrelated.part') + writeFileSync(unrelated, 'keep') + deletion.commit('task-1', now) + await purge.purge('task-1') + expect(existsSync(path)).toBe(false) + expect(existsSync(file.temporaryPath!)).toBe(false) + expect(readFileSync(unrelated, 'utf8')).toBe('keep') + expect(database.prepare('SELECT * FROM managed_files').all()).toEqual([]) + expect(database.prepare('SELECT * FROM file_preparations').all()).toEqual([]) + }) +}) diff --git a/apps/buddy/service/src/storage/__tests__/conversationArchiveRepository.spec.ts b/apps/buddy/service/src/storage/__tests__/conversationArchiveRepository.spec.ts deleted file mode 100644 index 3c62775af..000000000 --- a/apps/buddy/service/src/storage/__tests__/conversationArchiveRepository.spec.ts +++ /dev/null @@ -1,228 +0,0 @@ -import type { DatabaseSync } from 'node:sqlite' -import { afterEach, describe, expect, it } from 'vitest' -import { conversationArchiveQuerySchema } from '../../../../shared/conversation/conversationArchiveApi' -import { createConversationArchiveRepository } from '../conversationArchiveRepository' -import { createConversationDeletionRepository } from '../conversationDeletionRepository' -import { createConversationRepository } from '../conversationRepository' -import { openBuddyDatabase } from '../database' -import { createSpaceRepository } from '../spaceRepository' - -const now = '2026-11-01T00:00:00.000Z' -const databases: DatabaseSync[] = [] -afterEach(() => { - for (const database of databases.splice(0)) database.close() -}) -function fixture() { - const database = openBuddyDatabase({ databasePath: ':memory:' }) - databases.push(database) - const conversations = createConversationRepository(database) - const archive = createConversationArchiveRepository(database) - const deletion = createConversationDeletionRepository(database) - function seed(id: string, spaceId: string | null = null) { - return conversations.create({ id, branchId: `branch-${id}`, spaceId, title: id, createdAt: now, approvalPolicy: 'policy', executionProfile: 'workspace_write' }) - } - return { database, conversations, archive, deletion, seed } -} - -describe('v42 conversation archive repository', () => { - it('hides archives from all normal indexes but preserves direct reading and revisioned restoration', () => { - const f = fixture() - const task = f.seed('target') - f.seed('visible') - expect(task).toMatchObject({ archivedAt: null, metadataRevision: expect.any(Number) }) - expect(f.archive.archive('target', now)).toBe(true) - expect(f.conversations.findById('target')).toMatchObject({ archivedAt: now, metadataRevision: task.metadataRevision + 1 }) - expect(f.conversations.listRecent().map(task => task.id)).toEqual(['visible']) - expect(f.conversations.listHistory({ kind: 'search', query: 'target' }).items).toEqual([]) - expect(f.conversations.getHistoryEntries(['target'])).toEqual([]) - expect(f.archive.restore('target', '2026-11-02T00:00:00.000Z')).toBe(true) - expect(f.conversations.findById('target')).toMatchObject({ archivedAt: null, metadataRevision: task.metadataRevision + 2 }) - expect(f.conversations.getHistoryEntries(['target']).map(item => item.conversation.id)).toEqual(['target']) - }) - - it('binds archive cursors to their kind and search and orders tied timestamps by task ID', () => { - const f = fixture() - for (const id of ['target-a', 'target-b', 'other']) { - f.seed(id) - f.archive.archive(id, now) - } - const first = f.archive.list({ kind: 'archived', query: 'target', limit: 1 }) - expect(first.items.map(item => item.id)).toEqual(['target-b']) - expect(first.nextCursor).toBeTypeOf('string') - const second = f.archive.list({ kind: 'archived', query: 'target', limit: 1, cursor: first.nextCursor! }) - expect(second.items.map(item => item.id)).toEqual(['target-a']) - expect(second.nextCursor).toBeNull() - expect(() => f.archive.list({ kind: 'removed', query: 'target', cursor: first.nextCursor! })).toThrow(expect.objectContaining({ code: 'VALIDATION_FAILED' })) - expect(() => f.archive.list({ kind: 'archived', query: 'other', cursor: first.nextCursor! })).toThrow(expect.objectContaining({ code: 'VALIDATION_FAILED' })) - }) - - it('manages archives and ordinary deletion jobs without exposing legacy cleanup records', () => { - const f = fixture() - f.seed('active') - f.seed('archived') - f.archive.archive('archived', now) - f.seed('old-tombstone') - f.conversations.markDeleted('old-tombstone', now) - for (const status of ['pending', 'failed'] as const) { - for (const source of ['active', 'archived', 'legacy'] as const) { - const id = `${source}-${status}` - f.seed(id) - if (source === 'archived') - f.archive.archive(id, now) - if (source === 'legacy') { - f.conversations.markDeleted(id, now) - expect(f.deletion.commitLegacyRemoved(id, now)).not.toBeNull() - } - else { - expect(f.deletion.commit(id, now)).not.toBeNull() - } - if (status === 'failed') - f.deletion.failed(id, 'CONVERSATION_FILES_REFERENCED', now) - } - } - - const managed = f.archive.list(conversationArchiveQuerySchema.parse({ kind: 'managed' })) - expect(managed.items.map(item => item.id)).toEqual(['archived-pending', 'archived-failed', 'archived', 'active-pending', 'active-failed']) - expect(managed.nextCursor).toBeNull() - expect(managed.items).toMatchObject([ - { archivedAt: now, deletedAt: now, deletionStatus: 'pending', errorCode: null }, - { archivedAt: now, deletedAt: now, deletionStatus: 'failed', errorCode: 'CONVERSATION_FILES_REFERENCED' }, - { archivedAt: now, deletedAt: null, deletionStatus: null, errorCode: null }, - { archivedAt: null, deletedAt: now, deletionStatus: 'pending', errorCode: null }, - { archivedAt: null, deletedAt: now, deletionStatus: 'failed', errorCode: 'CONVERSATION_FILES_REFERENCED' }, - ]) - expect(f.archive.list({ kind: 'archived' }).items.map(item => item.id)).toEqual(['archived']) - expect(f.archive.list({ kind: 'removed' }).items.map(item => item.id)).toEqual(['old-tombstone', 'legacy-pending', 'legacy-failed']) - expect(f.archive.list({ kind: 'deleting' }).items.map(item => item.id)).toEqual([ - 'legacy-pending', - 'legacy-failed', - 'archived-pending', - 'archived-failed', - 'active-pending', - 'active-failed', - ]) - }) - - it('searches and paginates mixed managed states with kind-bound and query-bound cursors', () => { - const f = fixture() - for (const id of ['target-a', 'target-b', 'target-c', 'target-d', 'other', 'target-legacy', 'target-tombstone', 'target-active']) - f.seed(id) - for (const id of ['target-a', 'target-c', 'other']) - f.archive.archive(id, now) - for (const id of ['target-b', 'target-d']) - f.deletion.commit(id, now) - f.deletion.failed('target-b', 'CONVERSATION_FILES_REFERENCED', now) - for (const id of ['target-legacy', 'target-tombstone']) - f.conversations.markDeleted(id, now) - f.deletion.commitLegacyRemoved('target-legacy', now) - f.database.prepare('UPDATE conversations SET updated_at = ? WHERE id = ?').run('2026-11-02T00:00:00.000Z', 'target-a') - - const first = f.archive.list({ kind: 'managed', query: ' TARGET ', limit: 2 }) - expect(first.items.map(item => item.id)).toEqual(['target-a', 'target-d']) - expect(first.nextCursor).toBeTypeOf('string') - const second = f.archive.list({ kind: 'managed', query: 'TARGET', limit: 2, cursor: first.nextCursor! }) - expect(second.items.map(item => item.id)).toEqual(['target-c', 'target-b']) - expect(second.nextCursor).toBeNull() - const boundary = f.archive.list({ kind: 'managed', query: 'TARGET', limit: 1, cursor: first.nextCursor! }) - expect(boundary.items.map(item => item.id)).toEqual(['target-c']) - expect(boundary.nextCursor).toBeTypeOf('string') - expect(f.archive.list({ kind: 'managed', query: 'TARGET', limit: 1, cursor: boundary.nextCursor! })).toMatchObject({ - items: [{ id: 'target-b' }], - nextCursor: null, - }) - expect(f.archive.list({ kind: 'managed', query: 'target', limit: 4 }).nextCursor).toBeNull() - expect(f.archive.list({ kind: 'managed', query: 'absent' })).toEqual({ items: [], nextCursor: null }) - for (const kind of ['archived', 'removed', 'deleting'] as const) - expect(() => f.archive.list({ kind, query: 'TARGET', cursor: first.nextCursor! })).toThrow(expect.objectContaining({ code: 'VALIDATION_FAILED' })) - expect(() => f.archive.list({ kind: 'managed', query: 'other', cursor: first.nextCursor! })).toThrow(expect.objectContaining({ code: 'VALIDATION_FAILED' })) - const archived = f.archive.list({ kind: 'archived', query: 'TARGET', limit: 1 }) - expect(archived.nextCursor).toBeTypeOf('string') - expect(() => f.archive.list({ kind: 'managed', query: 'TARGET', cursor: archived.nextCursor! })).toThrow(expect.objectContaining({ code: 'VALIDATION_FAILED' })) - }) - - it.each(['managed', 'archived', 'removed', 'deleting'] as const)('filters %s by Space before pagination and rejects cursors from another scope', (kind) => { - const f = fixture() - const spaces = createSpaceRepository(f.database) - for (const id of ['space-a', 'space-b']) { - spaces.create({ id, name: id, createdAt: now, memoryScope: 'space_only', primaryDirectory: null, additionalDirectories: [] }) - } - const scopes = [null, 'space-a', 'space-b'] as const - for (const spaceId of scopes) { - for (const suffix of ['target-a', 'target-b', 'other']) { - const id = `${spaceId ?? 'unbound'}-${suffix}` - f.seed(id, spaceId) - if (kind === 'removed') { - f.conversations.markDeleted(id, now) - } - else if (kind === 'deleting' || (kind === 'managed' && suffix === 'target-b')) { - f.deletion.commit(id, now) - f.deletion.failed(id, 'CONVERSATION_FILES_REFERENCED', now) - } - else { - f.archive.archive(id, now) - } - } - } - - const all = f.archive.list({ kind, query: 'target' }) - expect(all.items.map(item => item.id)).toEqual([ - 'unbound-target-b', - 'unbound-target-a', - 'space-b-target-b', - 'space-b-target-a', - 'space-a-target-b', - 'space-a-target-a', - ]) - const allFirst = f.archive.list({ kind, query: 'target', limit: 1 }) - expect(allFirst.nextCursor).toBeTypeOf('string') - for (const spaceId of scopes) { - const input = conversationArchiveQuerySchema.parse({ kind, spaceId, query: 'target', limit: 1 }) - const first = f.archive.list(input) - expect(first.items).toMatchObject([{ id: `${spaceId ?? 'unbound'}-target-b`, spaceId }]) - expect(first.items).toHaveLength(1) - expect(first.nextCursor).toBeTypeOf('string') - const second = f.archive.list({ ...input, cursor: first.nextCursor! }) - expect(second.items).toMatchObject([{ id: `${spaceId ?? 'unbound'}-target-a`, spaceId }]) - expect(second.items).toHaveLength(1) - expect(second.nextCursor).toBeNull() - expect(() => f.archive.list({ ...input, cursor: allFirst.nextCursor! })).toThrow(expect.objectContaining({ code: 'VALIDATION_FAILED' })) - for (const otherSpaceId of [undefined, ...scopes]) { - if (otherSpaceId !== spaceId) - expect(() => f.archive.list({ ...input, spaceId: otherSpaceId, cursor: first.nextCursor! })).toThrow(expect.objectContaining({ code: 'VALIDATION_FAILED' })) - } - } - expect(f.archive.list({ kind, spaceId: 'missing', query: 'target' })).toEqual({ items: [], nextCursor: null }) - }) - - it.each([3, 7, 30] as const)('expires only explicit archives at the exact %s-day boundary', (days) => { - const f = fixture() - f.seed('archived') - f.seed('old-tombstone') - f.archive.archive('archived', now) - f.conversations.markDeleted('old-tombstone', now) - expect(f.archive.getPolicy()).toEqual({ days: 0 }) - expect(f.archive.due('2027-01-01T00:00:00.000Z')).toEqual([]) - f.archive.setPolicy(days) - const dueAt = Date.parse(now) + days * 24 * 60 * 60 * 1000 - expect(f.archive.due(new Date(dueAt - 1).toISOString())).toEqual([]) - expect(f.archive.due(new Date(dueAt).toISOString())).toEqual(['archived']) - expect(f.archive.isDue('old-tombstone', new Date(dueAt).toISOString())).toBe(false) - expect(f.deletion.pending()).toEqual([]) - }) - - it('keeps failed deletion visible and blocks restore or repeat expiry while its job exists', () => { - const f = fixture() - f.seed('target') - f.archive.archive('target', now) - f.archive.setPolicy(3) - f.deletion.commit('target', now) - f.deletion.failed('target', 'CONVERSATION_FILES_REFERENCED', now) - expect(f.archive.list({ kind: 'deleting' }).items).toMatchObject([{ id: 'target', deletionStatus: 'failed', errorCode: 'CONVERSATION_FILES_REFERENCED' }]) - expect(f.archive.restore('target', now)).toBe(false) - expect(f.archive.archive('target', now)).toBe(false) - expect(f.archive.due('2027-01-01T00:00:00.000Z')).toEqual([]) - expect(f.deletion.pending()).toEqual([]) - f.deletion.retry('target', now) - expect(f.deletion.pending()).toEqual(['target']) - }) -}) diff --git a/apps/buddy/service/src/storage/__tests__/conversationHistoryRepositories.spec.ts b/apps/buddy/service/src/storage/__tests__/conversationHistoryRepositories.spec.ts index 5170a40cf..c14c33d19 100644 --- a/apps/buddy/service/src/storage/__tests__/conversationHistoryRepositories.spec.ts +++ b/apps/buddy/service/src/storage/__tests__/conversationHistoryRepositories.spec.ts @@ -137,71 +137,6 @@ describe('conversation history repositories', () => { expect(conversations.findById('conversation-rolled-back')).toBeNull() }) - it('projects pending approval ahead of running and returns terminal runs to idle', () => { - const database = openBuddyDatabase({ databasePath: ':memory:' }) - databases.push(database) - const conversations = createConversationRepository(database) - const runs = createRunRepository(database) - conversations.create({ - branchId: 'branch-activity', - createdAt: '2026-08-20T00:00:00.000Z', - approvalPolicy: 'policy' as const, - executionProfile: 'workspace_write', - id: 'conversation-activity', - spaceId: null, - title: 'Activity', - }) - - expect(conversations.listRecent()).toMatchObject([ - { activity: 'idle', id: 'conversation-activity' }, - ]) - - runs.create({ - branchId: 'branch-activity', - conversationId: 'conversation-activity', - approvalPolicy: 'policy' as const, - executionProfile: 'workspace_write', - id: 'run-activity', - model: 'model-1', - piSessionFile: null, - provider: 'provider-1', - purpose: 'conversation.compaction', - startedAt: '2026-08-20T00:01:00.000Z', - status: 'running', - triggeringMessageId: 'message-activity', - }) - expect(conversations.listRecent()[0]?.activity).toBe('running') - - database.prepare(` - INSERT INTO approvals ( - id, run_id, tool_call_id, kind, status, summary, - payload_json, created_at, resolved_at - ) VALUES (?, ?, ?, ?, 'pending', ?, ?, ?, NULL) - `).run( - 'approval-activity', - 'run-activity', - 'tool-activity', - 'shell', - 'Run a command', - '{}', - '2026-08-20T00:02:00.000Z', - ) - expect(conversations.listRecent()[0]?.activity).toBe('awaiting_approval') - - database.prepare(` - UPDATE approvals SET status = 'approved', resolved_at = ? WHERE id = ? - `).run('2026-08-20T00:03:00.000Z', 'approval-activity') - expect(conversations.listRecent()[0]?.activity).toBe('running') - - runs.reconcileTerminal( - 'run-activity', - 'failed', - '2026-08-20T00:04:00.000Z', - 'MODEL_REQUEST_FAILED', - ) - expect(conversations.listRecent()[0]?.activity).toBe('idle') - }) - it('paginates the visible branch lineage without duplicating the page boundary', () => { const database = openBuddyDatabase({ databasePath: ':memory:' }) databases.push(database) @@ -657,58 +592,4 @@ describe('conversation history repositories', () => { updatedAt: '2026-08-15T00:00:01.000Z', })).toThrow(/branch is not activatable/i) }) - - it('does not switch branches while a conversation run is incomplete', () => { - const database = openBuddyDatabase({ databasePath: ':memory:' }) - databases.push(database) - const conversations = createConversationRepository(database) - const runs = createRunRepository(database) - conversations.create({ - branchId: 'branch-root', - createdAt: '2026-08-14T00:00:00.000Z', - approvalPolicy: 'policy' as const, - executionProfile: 'workspace_write', - id: 'conversation-1', - spaceId: null, - title: null, - }) - conversations.createBranch({ - activate: false, - conversationId: 'conversation-1', - createdAt: '2026-08-14T00:00:01.000Z', - forkedFromMessageId: null, - id: 'branch-second', - parentBranchId: null, - }) - conversations.createMessage({ - branchId: 'branch-root', - content: { text: 'hello' }, - conversationId: 'conversation-1', - createdAt: '2026-08-14T00:00:02.000Z', - id: 'message-1', - role: 'user', - runId: null, - }) - runs.create({ - branchId: 'branch-root', - conversationId: 'conversation-1', - approvalPolicy: 'policy' as const, - executionProfile: 'workspace_write', - id: 'run-1', - model: 'model-1', - piSessionFile: null, - provider: 'provider-1', - purpose: 'chat', - startedAt: '2026-08-14T00:00:03.000Z', - status: 'running', - triggeringMessageId: 'message-1', - }) - - expect(() => conversations.activateBranch({ - branchId: 'branch-second', - conversationId: 'conversation-1', - updatedAt: '2026-08-15T00:00:00.000Z', - })).toThrow(/branch is not activatable/i) - expect(conversations.findById('conversation-1')?.activeBranchId).toBe('branch-root') - }) }) diff --git a/apps/buddy/service/src/storage/__tests__/databaseTransaction.spec.ts b/apps/buddy/service/src/storage/__tests__/databaseTransaction.spec.ts deleted file mode 100644 index 6fea2083e..000000000 --- a/apps/buddy/service/src/storage/__tests__/databaseTransaction.spec.ts +++ /dev/null @@ -1,180 +0,0 @@ -import { mkdtempSync, rmSync } from 'node:fs' -import { tmpdir } from 'node:os' -import { join } from 'node:path' -import { DatabaseSync } from 'node:sqlite' -import { afterEach, expect, it, vi } from 'vitest' -import { withTransaction } from '../database' - -const databases: DatabaseSync[] = [] -const roots: string[] = [] -afterEach(() => { - vi.restoreAllMocks() - for (const database of databases.splice(0)) { - if (database.isOpen) - database.close() - } - for (const root of roots.splice(0)) - rmSync(root, { recursive: true, force: true }) -}) - -function fixture() { - const root = mkdtempSync(join(tmpdir(), 'lexora-transaction-')) - roots.push(root) - const path = join(root, 'database.sqlite3') - const database = new DatabaseSync(path) - databases.push(database) - database.exec('CREATE TABLE ledger (value TEXT UNIQUE)') - const insert = (value: string) => database.prepare('INSERT INTO ledger VALUES (?)').run(value) - const persisted = () => { - const reader = new DatabaseSync(path, { readOnly: true }) - try { - return reader.prepare('SELECT value FROM ledger ORDER BY value').all() - } - finally { reader.close() } - } - return { database, insert, persisted } -} - -it('commits nested writes exactly once and returns the synchronous result', () => { - const { database, insert, persisted } = fixture() - expect(withTransaction(database, () => { - insert('outer') - expect(withTransaction(database, () => { - insert('inner') - return 'nested-result' - })).toBe('nested-result') - expect(persisted()).toEqual([]) - return 42 - })).toBe(42) - expect(persisted()).toEqual([{ value: 'inner' }, { value: 'outer' }]) - expect(database.isTransaction).toBe(false) -}) - -it.each([new Error('inner failure'), undefined])('rolls back the outer transaction when a caught inner operation fails (%s)', (failure) => { - const { database, insert, persisted } = fixture() - let thrown = false - try { - withTransaction(database, () => { - insert('before') - try { - withTransaction(database, () => { - insert('inner') - throw failure - }) - } - catch {} - insert('after-catch') - }) - } - catch (error) { - thrown = true - expect(error).toBe(failure) - } - expect(thrown).toBe(true) - expect(persisted()).toEqual([]) - withTransaction(database, () => insert('recovered')) - expect(persisted()).toEqual([{ value: 'recovered' }]) -}) - -it('keeps SAVEPOINT rollback and deferred foreign-key commits inside the outer transaction', () => { - const { database } = fixture() - database.exec('PRAGMA foreign_keys = ON; CREATE TABLE parent (id INTEGER PRIMARY KEY); CREATE TABLE child (parent_id INTEGER REFERENCES parent(id))') - withTransaction(database, () => { - database.exec('PRAGMA defer_foreign_keys = ON; SAVEPOINT probe') - database.exec('INSERT INTO child VALUES (1)') - database.exec('ROLLBACK TO probe; RELEASE probe') - withTransaction(database, () => database.exec('INSERT INTO child VALUES (2)')) - database.exec('INSERT INTO parent VALUES (2)') - }) - expect(database.prepare('SELECT * FROM child').all()).toEqual([{ parent_id: 2 }]) - expect(database.prepare('PRAGMA foreign_key_check').all()).toEqual([]) -}) - -it('rolls back a failed deferred constraint commit and leaves a confirmed-clean connection writable', () => { - const { database, insert, persisted } = fixture() - database.exec('PRAGMA foreign_keys = ON; CREATE TABLE parent (id INTEGER PRIMARY KEY); CREATE TABLE child (parent_id INTEGER REFERENCES parent(id) DEFERRABLE INITIALLY DEFERRED)') - expect(() => withTransaction(database, () => { - insert('must-rollback') - database.exec('INSERT INTO child VALUES (1)') - })).toThrow('FOREIGN KEY constraint failed') - expect(persisted()).toEqual([]) - withTransaction(database, () => insert('recovered')) - expect(persisted()).toEqual([{ value: 'recovered' }]) -}) - -it('refuses an async callback before it can write', () => { - const { database, insert, persisted } = fixture() - const operation = (async () => { - insert('forbidden') - }) as unknown as () => void - expect(() => withTransaction(database, operation)).toThrow('synchronous operations') - expect(persisted()).toEqual([]) - withTransaction(database, () => insert('recovered')) - expect(persisted()).toEqual([{ value: 'recovered' }]) -}) - -it('rolls back a promise result and prevents its continuation from writing after rejection', async () => { - const { database, insert, persisted } = fixture() - let continuation: Promise | undefined - let lateError: unknown - const operation = (() => { - insert('before-await') - continuation = Promise.resolve().then(() => insert('late')).catch((error: unknown) => { - lateError = error - }) - return continuation - }) as unknown as () => void - expect(() => withTransaction(database, operation)).toThrow('cannot return a promise') - await continuation - expect(lateError).toBeInstanceOf(Error) - expect(database.isOpen).toBe(false) - expect(() => insert('direct')).toThrow() - expect(persisted()).toEqual([]) -}) - -it('blocks direct writes after a nested SQLite automatic rollback even if its exception is caught', () => { - const { database, insert, persisted } = fixture() - expect(() => withTransaction(database, () => { - insert('duplicate') - try { - withTransaction(database, () => database.exec('INSERT OR ROLLBACK INTO ledger VALUES (\'duplicate\')')) - } - catch {} - expect(() => insert('after-auto-rollback')).toThrow() - })).toThrowError(expect.objectContaining({ code: 'STORAGE_TRANSACTION_UNCERTAIN' })) - expect(database.isOpen).toBe(false) - expect(persisted()).toEqual([]) -}) - -it('closes an uncertain connection after a commit that succeeded before its acknowledgement failed', () => { - const { database, insert, persisted } = fixture() - const execute = database.exec.bind(database) - vi.spyOn(database, 'exec').mockImplementation((sql) => { - execute(sql) - if (sql === 'COMMIT') - throw new Error('commit acknowledgement lost') - }) - expect(() => withTransaction(database, () => insert('committed'))).toThrowError(expect.objectContaining({ code: 'STORAGE_TRANSACTION_UNCERTAIN' })) - expect(database.isOpen).toBe(false) - expect(() => insert('direct')).toThrow() - expect(() => withTransaction(database, () => insert('wrapped'))).toThrowError(expect.objectContaining({ code: 'STORAGE_TRANSACTION_UNCERTAIN' })) - expect(persisted()).toEqual([{ value: 'committed' }]) -}) - -it('closes a connection when rollback fails and prevents subsequent writes', () => { - const { database, insert, persisted } = fixture() - const execute = database.exec.bind(database) - vi.spyOn(database, 'exec').mockImplementation((sql) => { - if (sql === 'ROLLBACK') - throw new Error('rollback failed') - execute(sql) - }) - expect(() => withTransaction(database, () => { - insert('uncommitted') - throw new Error('operation failed') - })).toThrowError(expect.objectContaining({ code: 'STORAGE_TRANSACTION_UNCERTAIN' })) - expect(database.isOpen).toBe(false) - expect(() => insert('direct')).toThrow() - expect(() => withTransaction(database, () => insert('wrapped'))).toThrowError(expect.objectContaining({ code: 'STORAGE_TRANSACTION_UNCERTAIN' })) - expect(persisted()).toEqual([]) -}) diff --git a/apps/buddy/service/src/storage/__tests__/draftOwnershipRepository.spec.ts b/apps/buddy/service/src/storage/__tests__/draftOwnershipRepository.spec.ts new file mode 100644 index 000000000..9881d7ad5 --- /dev/null +++ b/apps/buddy/service/src/storage/__tests__/draftOwnershipRepository.spec.ts @@ -0,0 +1,57 @@ +import type { DatabaseSync } from 'node:sqlite' +import { afterEach, describe, expect, it } from 'vitest' +import { createBuddyUserContent } from '../../../../shared/conversation/buddyUserContent' +import { createComposerDraftRepository } from '../composerDraftRepository' +import { openBuddyMigrationCandidate } from '../database' +import { createDraftOwnershipRepository } from '../draftOwnershipRepository' + +const databases: DatabaseSync[] = [] +const now = '2026-11-07T00:00:00.000Z' +afterEach(() => { + for (const db of databases.splice(0)) + db.close() +}) +function fixture() { + const db = openBuddyMigrationCandidate({ databasePath: ':memory:' }) + databases.push(db) + db.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + db.prepare('INSERT INTO storage_actors VALUES (\'device-1\', \'device\', \'active\', ?)').run(now) + return { db, local: createComposerDraftRepository(db), device: createComposerDraftRepository(db, 'device-1'), slots: createDraftOwnershipRepository(db, 'desktop-local') } +} +const openInput = (draftId: string) => ({ draftId, scope: { kind: 'global' as const }, initialContent: createBuddyUserContent(draftId), initialModelSelection: null, initialExecutionConfig: { approvalPolicy: 'policy' as const, executionProfile: 'workspace_write' as const }, now }) + +describe('owned drafts and independent current slots', () => { + it('opens one current draft per trusted actor and prevents other actors from mutating it', () => { + const f = fixture() + const own = f.local.open(openInput('desktop-draft')) + const remote = f.device.open(openInput('device-draft')) + expect(f.local.findByScope({ kind: 'global' })).toEqual(own) + expect(f.device.findByScope({ kind: 'global' })).toEqual(remote) + expect(f.local.findById(remote.draftId)).toBeNull() + expect(f.device.findById(own.draftId)).toBeNull() + expect(f.local.list().map(d => d.draftId)).toEqual(['desktop-draft']) + expect(() => f.device.discard({ draftId: own.draftId, expectedRevision: 0 })).toThrow() + expect(() => f.device.save({ draftId: own.draftId, expectedRevision: 0, content: createBuddyUserContent('replacement'), modelSelection: null, executionConfig: own.executionConfig, now })).toThrow() + expect(f.local.findById(own.draftId)).toEqual(own) + expect(() => f.db.exec('UPDATE composer_drafts SET owner_actor_id = \'device-1\' WHERE id = \'desktop-draft\'')).toThrow('IMMUTABLE') + f.db.exec('UPDATE storage_actors SET state = \'revoked\' WHERE id = \'device-1\'') + expect(() => f.device.open(openInput('another-device-draft'))).toThrow('OWNER_UNAVAILABLE') + }) + + it('retains detached drafts and uses slot revision rather than draft revision to select the current draft', () => { + const f = fixture() + const first = f.local.open(openInput('first')) + const initial = f.slots.findSlot(first.scope)! + const detached = f.slots.select(first.scope, null, initial.slotRevision) + expect(f.local.findByScope(first.scope)).toBeNull() + expect(f.local.findById(first.draftId)).toEqual({ ...first, slotRevision: detached.slotRevision }) + const second = f.local.open(openInput('second')) + expect(f.slots.findSlot(first.scope)?.slotRevision).toBe(detached.slotRevision + 1) + expect(() => f.slots.select(first.scope, first.draftId, detached.slotRevision)).toThrow('REVISION_CONFLICT') + const slot = f.slots.findSlot(first.scope)! + const selected = f.slots.select(first.scope, first.draftId, slot.slotRevision) + expect(f.local.findByScope(first.scope)).toEqual({ ...first, slotRevision: selected.slotRevision }) + expect(f.local.findById(second.draftId)).toEqual({ ...second, slotRevision: selected.slotRevision }) + expect(f.local.list()).toHaveLength(2) + }) +}) diff --git a/apps/buddy/service/src/storage/__tests__/executionRepository.spec.ts b/apps/buddy/service/src/storage/__tests__/executionRepository.spec.ts index 5d7add71f..ceab51137 100644 --- a/apps/buddy/service/src/storage/__tests__/executionRepository.spec.ts +++ b/apps/buddy/service/src/storage/__tests__/executionRepository.spec.ts @@ -18,7 +18,7 @@ function rehash[0]>(input } describe('execution admission and fences', () => { - it.each(['disabled', 'removed', 'unavailable', 'dependency', 'user'] as const)('rejects %s managed targets in the admission transaction and preserves accepted snapshots', (reason) => { + it.each(['removed', 'dependency', 'user'] as const)('rejects %s managed targets in the admission transaction and preserves accepted snapshots', (reason) => { const f = fixture() const managed = managedBuddyFixture(f.database) const npc = f.buddies.upsertManagedNpc(managed.input) @@ -66,12 +66,12 @@ describe('execution admission and fences', () => { expect(f.conversations.findMember('conversation', 'default')?.modelSelection).toBeNull() }) - it.each(['completed', 'failed', 'cancelled'] as const)('resumes the same actor journal after a %s delegation', (status) => { + it('resumes the same actor journal after a completed delegation', () => { const f = fixture() const first = f.executions.admitChild(f.child('first', f.actor.id)).execution expect(first.run.piSessionFile).toBeNull() f.runs.bindSession('first', '/fixture/reviewer.jsonl') - f.runs.reconcileTerminal('first', status, executionNow, status === 'failed' ? 'REQUEST_FAILED' : null) + f.runs.reconcileTerminal('first', 'completed', executionNow, null) f.database.prepare('UPDATE execution_state SET phase = \'closed\' WHERE run_id = ?').run('first') const second = f.executions.admitChild(f.child('second', f.actor.id)).execution expect(second.run.threadId).toBe(first.run.threadId) @@ -124,7 +124,7 @@ describe('execution admission and fences', () => { expect(() => f.executions.admitChild(rehash({ ...request, initialInput: { ...request.initialInput, prompt: 'Conflicting goal' } }))).toThrow(expect.objectContaining({ code: 'conflict' })) }) - it.each(['cancel', 'close', 'branch', 'definition'] as const)('rejects stale %s admission without creating a child or thread', (change) => { + it.each(['cancel', 'close', 'delete', 'definition'] as const)('rejects stale %s admission without creating a child or thread', (change) => { const f = fixture() const request = f.child('rejected') const before = f.database.prepare('SELECT COUNT(*) AS count FROM execution_threads').get()?.count @@ -132,8 +132,8 @@ describe('execution admission and fences', () => { f.executions.requestCancel('root', executionNow) if (change === 'close') f.executions.beginClosing(f.fence('root'), 'success') - if (change === 'branch') - f.database.prepare('UPDATE conversations SET active_branch_id = ? WHERE id = ?').run('other', 'conversation') + if (change === 'delete') + f.conversations.markDeleted('conversation', executionNow) if (change === 'definition') f.buddies.update({ buddyId: f.npc.id, expectedRevision: f.npc.revision, enabled: false }) expect(() => f.executions.admitChild(request)).toThrow() @@ -141,6 +141,15 @@ describe('execution admission and fences', () => { expect(f.database.prepare('SELECT COUNT(*) AS count FROM execution_threads').get()?.count).toBe(before) }) + it('keeps child admission bound to the original branch after the default changes', () => { + const f = fixture() + const request = f.child('original-child') + f.conversations.createBranch({ id: 'other', conversationId: 'conversation', parentBranchId: null, forkedFromMessageId: null, createdAt: executionNow, activate: true }) + expect(f.executions.admitChild(request).execution.run).toMatchObject({ branchId: 'branch', parentRunId: 'root', rootRunId: 'root' }) + expect(() => f.executions.admitChild(rehash({ ...f.child('foreign-child'), run: { ...f.child('foreign-child').run, branchId: 'other' } }))).toThrow() + expect(f.runs.findById('foreign-child')).toBeNull() + }) + it('keeps ordinary active/timeline/unread queries on roots while recovery sees every child', () => { const f = fixture() f.executions.admitChild(f.child('private-child')) diff --git a/apps/buddy/service/src/storage/__tests__/executionRestart.spec.ts b/apps/buddy/service/src/storage/__tests__/executionRestart.spec.ts index 137a7bc37..d94388c73 100644 --- a/apps/buddy/service/src/storage/__tests__/executionRestart.spec.ts +++ b/apps/buddy/service/src/storage/__tests__/executionRestart.spec.ts @@ -73,7 +73,7 @@ it('reopens an active root with a completed child and interrupts unknown work wi } }) -it.each(['branch', 'delete'] as const)('retains a late child result as undelivered after %s without publishing or injecting it', async (boundary) => { +it.each(['branch', 'delete'] as const)('preserves late child result ownership after %s without public leakage', async (boundary) => { const directory = await mkdtemp(join(tmpdir(), 'lexora-late-execution-')) const f = createExecutionStorageFixture() const events = createRunEventLog({ database: f.database, conversationsDirectory: directory }) @@ -91,14 +91,24 @@ it.each(['branch', 'delete'] as const)('retains a late child result as undeliver { runId: 'late-child', type: 'execution.mailbox.sent', payload: { messageId: 'late-result', kind: 'result', targetRunId: 'root', targetGeneration: 0, text: 'PRIVATE_LATE_RESULT' } }, { runId: 'late-child', type: 'run.completed', payload: {} }, ]) - expect(mailbox.find('late-result')).toMatchObject({ state: 'undelivered', receipt: null }) - expect(mailbox.listUnconsumed('root')).toEqual([]) const message = mailbox.find('late-result')!.message - await expect(events.append({ runId: 'root', type: 'execution.mailbox.accepted', payload: { messageId: message.messageId, sourceRunId: message.sourceRunId, payloadHash: message.payloadHash, contextEntryId: 'wrong-new-context' } })).rejects.toThrow('cannot accept') + if (boundary === 'branch') { + const original = f.runs.findById('root')! + f.runs.create({ ...original, id: 'other-root', rootRunId: 'other-root', branchId: 'new-branch' }) + expect(mailbox.find('late-result')).toMatchObject({ state: 'pending', receipt: null, message: { targetRunId: 'root' } }) + expect(mailbox.listUnconsumed('other-root')).toEqual([]) + await expect(events.append({ runId: 'other-root', type: 'execution.mailbox.accepted', payload: { messageId: message.messageId, sourceRunId: message.sourceRunId, payloadHash: message.payloadHash, contextEntryId: 'wrong-new-context' } })).rejects.toThrow() + await events.append({ runId: 'root', type: 'execution.mailbox.accepted', payload: { messageId: message.messageId, sourceRunId: message.sourceRunId, payloadHash: message.payloadHash, contextEntryId: 'original-context' } }) + } + else { + expect(mailbox.find('late-result')).toMatchObject({ state: 'undelivered', receipt: null }) + expect(mailbox.listUnconsumed('root')).toEqual([]) + await expect(events.append({ runId: 'root', type: 'execution.mailbox.accepted', payload: { messageId: message.messageId, sourceRunId: message.sourceRunId, payloadHash: message.payloadHash, contextEntryId: 'wrong-new-context' } })).rejects.toThrow('cannot accept') + } await events.replay('late-child') - expect(mailbox.find('late-result')?.state).toBe('undelivered') + expect(mailbox.find('late-result')?.state).toBe(boundary === 'branch' ? 'accepted' : 'undelivered') expect(f.database.prepare('SELECT count(*) AS count FROM messages').get()).toEqual({ count: 0 }) - expect(f.database.prepare('SELECT count(*) AS count FROM execution_mailbox_receipts').get()).toEqual({ count: 0 }) + expect(f.database.prepare('SELECT count(*) AS count FROM execution_mailbox_receipts').get()).toEqual({ count: boundary === 'branch' ? 1 : 0 }) expect(f.runs.findById('root')).toMatchObject({ branchId: 'branch', status: 'running' }) expect(f.runs.listTree('root')).toHaveLength(2) } diff --git a/apps/buddy/service/src/storage/__tests__/legacyConversationPurge.spec.ts b/apps/buddy/service/src/storage/__tests__/legacyConversationPurge.spec.ts index 3f22806e0..7ade45eef 100644 --- a/apps/buddy/service/src/storage/__tests__/legacyConversationPurge.spec.ts +++ b/apps/buddy/service/src/storage/__tests__/legacyConversationPurge.spec.ts @@ -25,7 +25,7 @@ function fixture() { const conversations = createConversationRepository(database) const deletion = createConversationDeletionRepository(database) const archive = createConversationArchiveRepository(database) - const purge = new ConversationPurgeService({ database, home }) + const purge = new ConversationPurgeService({ database, home, storageAuthority: 'legacy_jsonl' }) function file(path: string) { mkdirSync(dirname(path), { recursive: true }) writeFileSync(path, 'private fixture') @@ -133,13 +133,11 @@ describe('v42 JSONL conversation purge', () => { expect(f.archive.list({ kind: 'deleting' }).items[0]).toMatchObject({ deletionStatus: 'failed', errorCode: 'CONVERSATION_FILES_REFERENCED' }) }) - it.each(['queued', 'running', 'cleanup', 'active', 'hazard'] as const)('blocks automatic expiry and physical deletion during %s resources', async (state) => { + it.each(['running', 'hazard'] as const)('blocks automatic expiry and physical deletion during %s resources', async (state) => { const f = fixture() const path = f.seed('target') - if (state === 'queued' || state === 'running') + if (state === 'running') f.database.prepare('UPDATE runs SET status = ?').run(state) - else if (state === 'cleanup') - f.database.exec(`UPDATE execution_state SET cleanup_state = 'pending'`) else f.lease(state) f.archive.archive('target', now) f.archive.setPolicy(3) @@ -213,7 +211,7 @@ describe('v42 JSONL conversation purge', () => { databases.push(database) expect(createConversationDeletionRepository(database).pending()).toEqual([]) createConversationDeletionRepository(database).retry('target', now) - await new ConversationPurgeService({ database, home: f.home }).purge('target') + await new ConversationPurgeService({ database, home: f.home, storageAuthority: 'legacy_jsonl' }).purge('target') expect(existsSync(join(f.home, 'conversations', 'target'))).toBe(false) expect(readFileSync(outside, 'utf8')).toBe('private fixture') }) diff --git a/apps/buddy/service/src/storage/__tests__/persistenceBackup.spec.ts b/apps/buddy/service/src/storage/__tests__/persistenceBackup.spec.ts new file mode 100644 index 000000000..54c96ecd5 --- /dev/null +++ b/apps/buddy/service/src/storage/__tests__/persistenceBackup.spec.ts @@ -0,0 +1,197 @@ +import type { BackupScope } from '../../../../shared/storage/backupScope' +import { createHash, randomUUID } from 'node:crypto' +import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { copySnapshot } from '../../../../platform/storage/copySnapshot' +import { extractSnapshotFiles } from '../../../../platform/storage/extractSnapshotFiles' +import { createBuddyUserContent } from '../../../../shared/conversation/buddyUserContent' +import { deviceBearer } from '../../../../shared/privateNetwork/pairing' +import { DeviceAuthorizationService } from '../../privateNetwork/DeviceAuthorizationService' +import { createAttachmentPersistenceRepository } from '../attachmentPersistenceRepository' +import { createAttachmentRepository } from '../attachmentRepository' +import { CommandCommitter } from '../CommandCommitter' +import { createComposerDraftRepository } from '../composerDraftRepository' +import { openBuddyDatabase, openBuddyMigrationCandidate } from '../database' +import { prepareBackupRestore } from '../restoreBackup' +import { BUDDY_MIGRATION_SCHEMA_VERSION } from '../schema' +import { openSnapshotDatabase, rewriteIsolatedSnapshotDatabase } from '../snapshotDatabase' +import { createStorageSnapshot, snapshotDirectory } from '../StorageSnapshots' + +const directories: string[] = [] +const signal = new AbortController().signal +const now = '2026-11-07T00:00:00.000Z' +function report() {} +afterEach(async () => { + await Promise.all(directories.splice(0).map(path => rm(path, { recursive: true, force: true }))) +}) +async function fixture(legacy = false) { + const directory = await mkdtemp(join(tmpdir(), 'lexora-persistence-backup-')) + directories.push(directory) + const root = join(directory, 'profile') + await mkdir(join(root, 'buddy'), { recursive: true }) + const path = join(root, 'buddy/buddy.sqlite3') + const database = legacy ? openBuddyDatabase({ databasePath: path }) : openBuddyMigrationCandidate({ databasePath: path }) + if (!legacy) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + database.close() + return { root, path, directory, open: () => openSnapshotDatabase(path), snapshot: (scopes: BackupScope[]) => createStorageSnapshot(root, randomUUID(), signal, report, undefined, { scopes, credentials: false }) } +} +function seedDraft(database: ReturnType, id: string) { + return createComposerDraftRepository(database).open({ draftId: id, scope: { kind: 'task', draftId: id, spaceId: null }, initialContent: createBuddyUserContent('Keep this draft'), initialExecutionConfig: { approvalPolicy: 'policy', executionProfile: 'workspace_write' }, initialModelSelection: null, now }) +} +function receipt(database: ReturnType) { + const generation = String(database.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation) + return new CommandCommitter(database).accept({ dataGeneration: generation, actorId: 'desktop-local', commandId: 'receipt', method: 'composerDrafts.save', fingerprint: 'fingerprint', fingerprintVersion: 1, sourceInstanceId: 'old-instance' }, () => ({ target: { draftId: 'saved-draft' }, result: { revision: 1 } })).receipt +} + +describe('sQLite authority snapshot compatibility', () => { + it('preserves autoincrement high water marks above JavaScript safe integers after deleted history', async () => { + const f = await fixture() + const source = f.open() + source.exec('CREATE TABLE retained_sequence (id INTEGER PRIMARY KEY AUTOINCREMENT, value TEXT); INSERT INTO retained_sequence (id, value) VALUES (9007199254740993, \'deleted history\'); DELETE FROM retained_sequence') + source.close() + await rewriteIsolatedSnapshotDatabase(f.path, () => {}) + const rewritten = f.open() + try { + rewritten.exec('INSERT INTO retained_sequence (value) VALUES (\'new record\')') + const statement = rewritten.prepare('SELECT id FROM retained_sequence') + statement.setReadBigInts(true) + expect(statement.get()?.id).toBe(9007199254740994n) + } + finally { rewritten.close() } + }) + + it('exports a selective snapshot with permanent receipts and valid identity while preserving source bytes and constraints', async () => { + const f = await fixture() + const source = f.open() + const original = receipt(source) + seedDraft(source, 'draft') + source.close() + const before = await readFile(f.path) + const manifest = await f.snapshot(['providers']) + expect(manifest.databaseVersion).toBe(BUDDY_MIGRATION_SCHEMA_VERSION) + expect(await readFile(f.path)).toEqual(before) + const extracted = join(f.directory, 'extracted') + await extractSnapshotFiles(snapshotDirectory(f.root, manifest.id), extracted, manifest, { signal, report }) + const saved = openSnapshotDatabase(join(extracted, 'buddy/buddy.sqlite3')) + try { + expect(saved.prepare('SELECT * FROM composer_drafts').all()).toEqual([]) + expect(saved.prepare('SELECT * FROM managed_files').all()).toEqual([]) + expect(new CommandCommitter(saved).find(original)).toEqual(original) + expect(saved.prepare('PRAGMA foreign_key_check').all()).toEqual([]) + expect(() => saved.exec('DELETE FROM command_receipts')).toThrow('IMMUTABLE') + } + finally { saved.close() } + }) + + it('restores files, ownership and receipts into an isolated new generation without reactivating devices', async () => { + const from = await fixture() + const source = from.open() + const original = receipt(source) + seedDraft(source, 'saved-draft') + const file = join(from.root, 'buddy/drafts/saved-draft/file.txt') + await mkdir(join(from.root, 'buddy/drafts/saved-draft'), { recursive: true }) + await writeFile(file, 'fixture') + createAttachmentPersistenceRepository(source).adopt({ path: file, sizeBytes: 7, sha256: createHash('sha256').update('fixture').digest('hex') }) + createAttachmentRepository(source).create({ id: 'attachment', draftId: 'saved-draft', messageId: null, conversationId: null, name: 'file.txt', mimeType: 'text/plain', sizeBytes: 7, storedPath: file, createdAt: now }) + source.close() + const snapshot = await from.snapshot(['tasks']) + const to = await fixture() + const current = to.open() + seedDraft(current, 'unrelated-draft') + current.exec('INSERT INTO storage_actors VALUES (\'device\', \'device\', \'active\', \'2026-11-07T00:00:00.000Z\')') + const devices = new DeviceAuthorizationService({ database: current, serviceId: 'service', profileId: 'profile', serviceInstanceId: 'before' }) + const qr = devices.issue('https://127.0.0.1:9443', `sha256/${'A'.repeat(43)}=`) + const { serviceId, profileId, issuerInstanceId, ticketId, ticketSecret } = qr + const context = { serviceId, profileId, issuerInstanceId, ticketId, ticketSecret } + const attempt = devices.prepare({ ...context, deviceName: 'Restore fixture' }) + const credential = devices.redeem({ ...context, attemptId: attempt.attemptId, recoverySecret: attempt.recoverySecret }) + devices.confirm(deviceBearer(credential)) + current.prepare('UPDATE private_network_state SET tls_pin = ? WHERE id = 1').run(qr.spkiPin) + + const oldGeneration = current.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation + current.close() + await copySnapshot(snapshotDirectory(from.root, snapshot.id), snapshotDirectory(to.root, snapshot.id), { signal, report }) + const stage = join(to.directory, 'restore') + await prepareBackupRestore({ id: randomUUID(), kind: 'restore', source: to.root, target: to.root, phase: 'pending', createdAt: now, error: null, snapshotId: snapshot.id, scopes: ['tasks'], credentials: false }, stage, join(to.directory, 'location.json'), signal, report) + const restored = openSnapshotDatabase(join(stage, 'profile/buddy/buddy.sqlite3')) + try { + const newGeneration = restored.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation + expect(newGeneration).not.toBe(oldGeneration) + expect(newGeneration).not.toBe(original.dataGeneration) + expect(restored.prepare('SELECT state FROM storage_actors WHERE id = \'device\'').get()).toEqual({ state: 'revoked' }) + expect(restored.prepare('SELECT state, grant_generation FROM remote_devices WHERE id = ?').get(credential.deviceId)).toEqual({ state: 'revoked', grant_generation: 2 }) + expect(restored.prepare('SELECT tls_pin FROM private_network_state WHERE id = 1').get()!.tls_pin).toBe(qr.spkiPin) + const restoredDevices = new DeviceAuthorizationService({ database: restored, serviceId: 'service', profileId: 'profile', serviceInstanceId: 'after' }) + expect(() => restoredDevices.authenticate(deviceBearer(credential))).toThrow('NOT_AUTHORIZED') + + expect(restored.prepare('SELECT id, owner_actor_id FROM composer_drafts ORDER BY id').all()).toEqual([{ id: 'saved-draft', owner_actor_id: 'desktop-local' }, { id: 'unrelated-draft', owner_actor_id: 'desktop-local' }]) + expect(restored.prepare('SELECT data_generation, command_id FROM command_receipts').get()).toEqual({ data_generation: original.dataGeneration, command_id: 'receipt' }) + const stored = String(restored.prepare('SELECT path FROM managed_files').get()!.path) + expect(stored).toBe(join(to.root, 'buddy/drafts/saved-draft/file.txt')) + expect(await readFile(join(stage, 'profile/buddy/drafts/saved-draft/file.txt'), 'utf8')).toBe('fixture') + expect(() => restored.exec('DELETE FROM managed_files')).toThrow('IMMUTABLE') + expect(restored.prepare('PRAGMA foreign_key_check').all()).toEqual([]) + } + finally { restored.close() } + const untouched = to.open() + expect(untouched.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation).toBe(oldGeneration) + untouched.close() + }) + + it('converts a legacy selective snapshot through full source verification before merging into SQLite authority', async () => { + const legacy = await fixture(true) + const source = legacy.open() + source.prepare('INSERT INTO provider_states (provider_id, enabled, created_at, updated_at) VALUES (?, 1, ?, ?)').run('fixture-provider', now, now) + source.close() + const snapshot = await legacy.snapshot(['providers']) + const to = await fixture() + await copySnapshot(snapshotDirectory(legacy.root, snapshot.id), snapshotDirectory(to.root, snapshot.id), { signal, report }) + const stage = join(to.directory, 'restore') + await prepareBackupRestore({ id: randomUUID(), kind: 'restore', source: to.root, target: to.root, phase: 'pending', createdAt: now, error: null, snapshotId: snapshot.id, scopes: ['providers'], credentials: false }, stage, join(to.directory, 'location.json'), signal, report) + const restored = openSnapshotDatabase(join(stage, 'profile/buddy/buddy.sqlite3')) + expect(restored.prepare('SELECT enabled FROM provider_states WHERE provider_id = ?').get('fixture-provider')).toEqual({ enabled: 1 }) + expect(restored.prepare('SELECT authority, migration_state FROM storage_profile').get()).toEqual({ authority: 'sqlite', migration_state: 'ready' }) + restored.close() + const original = legacy.open() + expect(original.prepare('PRAGMA user_version').get()).toEqual({ user_version: 42 }) + original.close() + }) + it('upgrades an isolated legacy target when restoring a SQLite snapshot and leaves the original target unchanged', async () => { + const from = await fixture() + const source = from.open() + receipt(source) + source.prepare('INSERT INTO provider_states (provider_id, enabled, created_at, updated_at) VALUES (?, 1, ?, ?)').run('candidate-provider', now, now) + source.close() + const snapshot = await from.snapshot(['providers']) + const to = await fixture(true) + const before = await readFile(to.path) + await copySnapshot(snapshotDirectory(from.root, snapshot.id), snapshotDirectory(to.root, snapshot.id), { signal, report }) + const stage = join(to.directory, 'restore') + await prepareBackupRestore({ id: randomUUID(), kind: 'restore', source: to.root, target: to.root, phase: 'pending', createdAt: now, error: null, snapshotId: snapshot.id, scopes: ['providers'], credentials: false }, stage, join(to.directory, 'location.json'), signal, report) + const restored = openSnapshotDatabase(join(stage, 'profile/buddy/buddy.sqlite3')) + expect(restored.prepare('PRAGMA user_version').get()).toEqual({ user_version: BUDDY_MIGRATION_SCHEMA_VERSION }) + expect(restored.prepare('SELECT command_id FROM command_receipts').all()).toEqual([{ command_id: 'receipt' }]) + restored.close() + expect(await readFile(to.path)).toEqual(before) + }) + + it('rejects conflicting immutable receipt identities without altering the current profile', async () => { + const from = await fixture() + const source = from.open() + const original = receipt(source) + source.close() + const snapshot = await from.snapshot(['providers']) + const to = await fixture() + const current = to.open() + current.prepare('UPDATE storage_profile SET data_generation = ?').run(original.dataGeneration) + new CommandCommitter(current).accept({ dataGeneration: original.dataGeneration, actorId: original.actorId, commandId: original.commandId, method: original.method, fingerprint: original.fingerprint, fingerprintVersion: original.fingerprintVersion, sourceInstanceId: original.sourceInstanceId }, () => ({ target: { draftId: 'different-target' }, result: { revision: 1 } })) + current.close() + const before = await readFile(to.path) + await copySnapshot(snapshotDirectory(from.root, snapshot.id), snapshotDirectory(to.root, snapshot.id), { signal, report }) + await expect(prepareBackupRestore({ id: randomUUID(), kind: 'restore', source: to.root, target: to.root, phase: 'pending', createdAt: now, error: null, snapshotId: snapshot.id, scopes: ['providers'], credentials: false }, join(to.directory, 'restore'), join(to.directory, 'location.json'), signal, report)).rejects.toThrow('SNAPSHOT_DEPENDENCY_CONFLICT') + expect(await readFile(to.path)).toEqual(before) + }) +}) diff --git a/apps/buddy/service/src/storage/__tests__/persistenceMigration.spec.ts b/apps/buddy/service/src/storage/__tests__/persistenceMigration.spec.ts new file mode 100644 index 000000000..721c82cff --- /dev/null +++ b/apps/buddy/service/src/storage/__tests__/persistenceMigration.spec.ts @@ -0,0 +1,256 @@ +import type { SQLInputValue } from 'node:sqlite' +import { spawnSync } from 'node:child_process' +import { createHash } from 'node:crypto' +import { copyFileSync, mkdtempSync, readFileSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import process from 'node:process' +import { DatabaseSync } from 'node:sqlite' +import { afterEach, describe, expect, it } from 'vitest' +import { openBuddyDatabase, openBuddyMigrationCandidate } from '../database' +import { BUDDY_V43_PERSISTENCE_SCHEMA_SQL } from '../migrations/v43Persistence' +import { BUDDY_MIGRATION_SCHEMA_VERSION, BUDDY_SCHEMA_VERSION } from '../schema' +import { withTransaction } from '../transaction' +import { MIGRATION_TEST_TIMEOUT } from './migrationFixture' + +const databases: DatabaseSync[] = [] +const directories: string[] = [] +const now = '2026-09-29T00:00:00.000Z' + +afterEach(() => { + for (const database of databases.splice(0)) { + if (database.isOpen) + database.close() + } + for (const directory of directories.splice(0)) + rmSync(directory, { recursive: true, force: true }) +}) + +function candidate(databasePath = ':memory:'): DatabaseSync { + const database = openBuddyMigrationCandidate({ databasePath }) + databases.push(database) + return database +} + +function fixture() { + const directory = mkdtempSync(join(tmpdir(), 'lexora-persistence-migration-')) + directories.push(directory) + const sourcePath = join(directory, 'source.sqlite3') + const databasePath = join(directory, 'candidate.sqlite3') + const source = openBuddyDatabase({ databasePath: sourcePath }) + databases.push(source) + seed(source) + const before = snapshot(source) + source.close() + const sourceHash = hash(sourcePath) + copyFileSync(sourcePath, databasePath) + return { sourcePath, sourceHash, databasePath, before } +} + +function hash(path: string): string { + return createHash('sha256').update(readFileSync(path)).digest('hex') +} + +function snapshot(database: DatabaseSync, previous?: ReturnType) { + const columns = previous ?? tableColumns(database) + return { + columns, + tables: Object.fromEntries(Object.entries(columns).map(([table, names]) => [ + table, + database.prepare(`SELECT ${names.map(name => `"${name}"`).join(', ')} FROM "${table}"`).all().map(row => JSON.stringify(row)).sort(), + ])), + } +} + +function tableColumns(database: DatabaseSync): Record { + const tables = database.prepare('SELECT name FROM sqlite_master WHERE type = \'table\' AND name NOT LIKE \'sqlite_%\' AND name <> \'buddy_schema_lineage\' ORDER BY name').all() as Array<{ name: string }> + return Object.fromEntries(tables.map(({ name }) => [name, (database.prepare(`PRAGMA table_info("${name}")`).all() as Array<{ name: string }>).map(column => column.name)])) +} + +function seed(database: DatabaseSync): void { + withTransaction(database, () => database.exec(` + INSERT INTO conversations (id, title, created_at, updated_at) VALUES ('task', 'Keep task', '${now}', '${now}'); + INSERT INTO conversation_branches (id, conversation_id, created_at) VALUES ('branch', 'task', '${now}'); + UPDATE conversations SET active_branch_id = 'branch'; + INSERT INTO messages (id, conversation_id, branch_id, role, content_json, created_at) + VALUES ('input', 'task', 'branch', 'user', '{"text":"Keep input"}', '${now}'); + INSERT INTO runs (id, conversation_id, branch_id, triggering_message_id, provider, model, purpose, status, started_at) + VALUES ('run', 'task', 'branch', 'input', 'fixture', 'fixture', 'chat', 'completed', '${now}'); + INSERT INTO run_inputs (run_id, prompt, attachment_ids_json, context_items_json, created_at) + VALUES ('run', 'Keep original input', '[]', '[]', '${now}'); + INSERT INTO run_events (run_id, sequence, event_type, payload_json, created_at) + VALUES ('run', 1, 'run.started', '{}', '${now}'), + ('run', 4, 'future.unknown', '{"keep":true}', '${now}'), + ('run', 5, 'run.completed', '{}', '${now}'); + INSERT INTO composer_drafts (id, scope_kind, revision, content_json, approval_policy, execution_profile, created_at, updated_at) + VALUES ('draft-one', 'task', 7, '{"uninterpreted":"Keep draft one"}', 'policy', 'workspace_write', '${now}', '${now}'), + ('draft-two', 'task', 9, '{"uninterpreted":"Keep draft two"}', 'policy', 'read_only', '${now}', '${now}'); + INSERT INTO turn_requests (request_id, request_fingerprint, conversation_id, branch_id, run_id, created_at) + VALUES ('same-legacy-id', 'turn-intent', 'task', 'branch', 'run', '${now}'); + INSERT INTO command_requests (request_id, request_fingerprint, conversation_id, branch_id, run_id, command, arguments, created_at) + VALUES ('same-legacy-id', 'compact-intent', 'task', 'branch', 'run', 'compact', '', '${now}'); + INSERT INTO chat_queue (id, conversation_id, branch_id, request_id, request_fingerprint, prepared_json, state, created_at) + VALUES ('queue', 'task', 'branch', 'same-legacy-id', 'queue-intent', '{}', 'paused', '${now}'); + `)) +} + +function addReceipt(database: DatabaseSync, overrides: Record = {}, insert = 'INSERT'): void { + const values = { + data_generation: (database.prepare('SELECT data_generation FROM storage_profile').get() as { data_generation: string }).data_generation, + actor_id: 'desktop-local', + command_id: 'command', + method: 'chat.startTurn', + fingerprint_version: 1, + fingerprint: 'normalized-intent', + source_instance_id: 'instance-before-restart', + decision: 'accepted', + target_json: '{"runId":"run"}', + result_json: '{"draftId":"draft-one","sourceRevision":"7","committedRevision":"8"}', + error_code: null, + decided_at: now, + ...overrides, + } + database.prepare(`${insert} INTO command_receipts (${Object.keys(values).join(', ')}) VALUES (${Object.keys(values).map(() => '?').join(', ')})`).run(...Object.values(values)) +} + +describe('persistence schema migration', { timeout: MIGRATION_TEST_TIMEOUT }, () => { + it('keeps runtime databases on v42 and creates only an explicit pending persistence candidate', () => { + const runtime = openBuddyDatabase({ databasePath: ':memory:' }) + databases.push(runtime) + expect(runtime.prepare('PRAGMA user_version').get()).toEqual({ user_version: BUDDY_SCHEMA_VERSION }) + expect(runtime.prepare('SELECT name FROM sqlite_master WHERE name = \'storage_profile\'').get()).toBeUndefined() + const database = candidate() + expect(database.prepare('PRAGMA user_version').get()).toEqual({ user_version: BUDDY_MIGRATION_SCHEMA_VERSION }) + expect(database.prepare('SELECT * FROM storage_profile').get()).toEqual({ + id: 1, + data_generation: expect.stringMatching(/^[0-9a-f]{32}$/), + local_actor_id: 'desktop-local', + min_writer_version: 1, + authority: 'legacy_jsonl', + migration_state: 'pending', + }) + expect(database.prepare('SELECT id, kind, state FROM storage_actors').all()).toEqual([{ id: 'desktop-local', kind: 'desktop', state: 'active' }]) + expect(() => database.exec('UPDATE storage_profile SET authority = \'sqlite\'')).toThrow() + expect(() => database.exec('UPDATE storage_profile SET migration_state = \'ready\'')).toThrow() + }) + + it('preserves every existing row without inventing receipts or verified event watermarks', () => { + const { sourcePath, sourceHash, databasePath, before } = fixture() + let database = candidate(databasePath) + expect(snapshot(database, before.columns)).toEqual(before) + expect(database.prepare('SELECT * FROM command_receipts').all()).toEqual([]) + expect(database.prepare('SELECT next_event_sequence FROM runs').get()).toEqual({ next_event_sequence: null }) + expect(database.prepare('SELECT sequence, format_version FROM run_events ORDER BY sequence').all()).toEqual([ + { sequence: 1, format_version: 1 }, + { sequence: 4, format_version: 1 }, + { sequence: 5, format_version: 1 }, + ]) + const profile = database.prepare('SELECT * FROM storage_profile').get() + addReceipt(database) + const receipt = database.prepare('SELECT * FROM command_receipts').get() + database.close() + database = candidate(databasePath) + expect(database.prepare('SELECT * FROM storage_profile').get()).toEqual(profile) + expect(database.prepare('SELECT * FROM command_receipts').get()).toEqual(receipt) + expect(database.prepare('PRAGMA foreign_key_check').all()).toEqual([]) + expect(database.prepare('PRAGMA integrity_check').get()).toEqual({ integrity_check: 'ok' }) + expect(hash(sourcePath)).toBe(sourceHash) + }) + + it('scopes immutable receipts by generation and actor and retains them when an actor is revoked', () => { + const database = candidate() + database.prepare('INSERT INTO storage_actors VALUES (?, \'device\', \'active\', ?)').run('device-fixture', now) + addReceipt(database) + addReceipt(database, { actor_id: 'device-fixture' }) + addReceipt(database, { data_generation: 'previous-generation' }) + const original = database.prepare('SELECT * FROM command_receipts ORDER BY data_generation, actor_id').all() + expect(() => addReceipt(database)).toThrow('ALREADY_DECIDED') + expect(() => addReceipt(database, { method: 'chat.cancel' })).toThrow('ALREADY_DECIDED') + expect(() => addReceipt(database, { target_json: '{"runId":"different"}' }, 'INSERT OR REPLACE')).toThrow('ALREADY_DECIDED') + expect(() => database.exec('UPDATE command_receipts SET target_json = \'{"runId":"different"}\'')).toThrow('IMMUTABLE') + expect(() => database.exec('DELETE FROM command_receipts')).toThrow('IMMUTABLE') + database.exec('UPDATE storage_actors SET state = \'revoked\' WHERE id = \'device-fixture\'') + expect(() => database.exec('DELETE FROM storage_actors WHERE id = \'device-fixture\'')).toThrow() + expect(database.prepare('SELECT * FROM command_receipts ORDER BY data_generation, actor_id').all()).toEqual(original) + }) + + it('stores a monotonic integer watermark independently of retained events', () => { + const { databasePath } = fixture() + const database = candidate(databasePath) + database.exec('UPDATE runs SET next_event_sequence = 8; DELETE FROM run_events') + expect(database.prepare('SELECT next_event_sequence FROM runs').get()).toEqual({ next_event_sequence: 8 }) + for (const invalid of [7, null]) + expect(() => database.prepare('UPDATE runs SET next_event_sequence = ?').run(invalid)).toThrow() + database.prepare('UPDATE runs SET next_event_sequence = ?').run(9223372036854775807n) + expect(database.prepare('SELECT CAST(next_event_sequence AS TEXT) AS sequence FROM runs').get()).toEqual({ sequence: '9223372036854775807' }) + expect(() => database.exec('UPDATE runs SET next_event_sequence = next_event_sequence + 1')).toThrow() + }) + + it('rolls the entire schema and identity back on a late DDL conflict', () => { + const table = 'product_query_revisions' + const { databasePath, before } = fixture() + const conflict = new DatabaseSync(databasePath) + conflict.exec(`CREATE TABLE ${table} (preserve TEXT); INSERT INTO ${table} VALUES ('unrecognized data')`) + conflict.close() + expect(() => candidate(databasePath)).toThrow('already exists') + const rejected = new DatabaseSync(databasePath) + databases.push(rejected) + expect(rejected.prepare('PRAGMA user_version').get()).toEqual({ user_version: BUDDY_SCHEMA_VERSION }) + expect(rejected.prepare('SELECT name FROM sqlite_master WHERE name IN (\'storage_profile\', \'storage_actors\')').all()).toEqual([]) + expect(rejected.prepare(`SELECT * FROM ${table}`).all()).toEqual([{ preserve: 'unrecognized data' }]) + expect(snapshot(rejected, before.columns)).toEqual(before) + }) + + it.skipIf(process.platform === 'win32').each(['partial', 'before-commit', 'after-commit'])('recovers a process killed at %s without claiming SQLite authority', (checkpoint) => { + const { databasePath, sourcePath, sourceHash, before } = fixture() + const sql = checkpoint === 'partial' + ? BUDDY_V43_PERSISTENCE_SCHEMA_SQL.slice(0, BUDDY_V43_PERSISTENCE_SCHEMA_SQL.indexOf('ALTER TABLE runs')) + : BUDDY_V43_PERSISTENCE_SCHEMA_SQL + const interrupted = spawnSync(process.execPath, ['--input-type=module', '-e', ` + import { DatabaseSync } from 'node:sqlite'; + import { readFileSync, writeSync } from 'node:fs'; + const db = new DatabaseSync(process.argv[1]); + db.exec('PRAGMA synchronous=FULL; BEGIN IMMEDIATE'); + db.exec(readFileSync(0, 'utf8')); + if (process.argv[2] !== 'partial') db.exec('PRAGMA user_version=${BUDDY_MIGRATION_SCHEMA_VERSION}'); + if (process.argv[2] === 'after-commit') db.exec('COMMIT'); + writeSync(1, JSON.stringify(db.prepare('SELECT data_generation FROM storage_profile').get())); + process.kill(process.pid, 'SIGKILL'); + `, databasePath, checkpoint], { encoding: 'utf8', input: sql, timeout: 10000 }) + expect(interrupted.error, 'Migration test subprocess must start').toBeUndefined() + expect(interrupted.signal, interrupted.stderr).toBe('SIGKILL') + const raw = new DatabaseSync(databasePath) + expect(raw.prepare('PRAGMA user_version').get()).toEqual({ user_version: checkpoint === 'after-commit' ? BUDDY_MIGRATION_SCHEMA_VERSION : BUDDY_SCHEMA_VERSION }) + if (checkpoint !== 'after-commit') + expect(raw.prepare('SELECT name FROM sqlite_master WHERE name = \'storage_profile\'').get()).toBeUndefined() + raw.close() + const database = candidate(databasePath) + expect(snapshot(database, before.columns)).toEqual(before) + expect(database.prepare('SELECT authority, migration_state FROM storage_profile').get()).toEqual({ authority: 'legacy_jsonl', migration_state: 'pending' }) + if (checkpoint === 'after-commit') + expect(database.prepare('SELECT data_generation FROM storage_profile').get()).toEqual(JSON.parse(interrupted.stdout)) + expect(database.prepare('PRAGMA foreign_key_check').all()).toEqual([]) + expect(hash(sourcePath)).toBe(sourceHash) + }) + + it.each(['runtime', 'newer-writer', 'missing-identity', 'missing-constraint', 'missing-owner-constraint'])('rejects %s before changing the candidate journal or repairing its identity', (reason) => { + const { databasePath } = fixture() + const database = candidate(databasePath) + if (reason === 'newer-writer') + database.exec('UPDATE storage_profile SET min_writer_version = 2') + if (reason === 'missing-identity') + database.exec('DELETE FROM storage_profile') + if (reason === 'missing-constraint') + database.exec('DROP TRIGGER command_receipts_no_update') + if (reason === 'missing-owner-constraint') + database.exec('DROP INDEX storage_actors_desktop') + database.exec('PRAGMA journal_mode = DELETE') + database.close() + const beforeHash = hash(databasePath) + expect(() => reason === 'runtime' ? openBuddyDatabase({ databasePath }) : candidate(databasePath)).toThrow(/schema version|writer version|storage identity|storage constraints/) + expect(hash(databasePath)).toBe(beforeHash) + const rejected = new DatabaseSync(databasePath) + expect(rejected.prepare('PRAGMA journal_mode').get()).toEqual({ journal_mode: 'delete' }) + rejected.close() + }) +}) diff --git a/apps/buddy/service/src/storage/__tests__/schema.spec.ts b/apps/buddy/service/src/storage/__tests__/schema.spec.ts index 09eabb852..b16031d3c 100644 --- a/apps/buddy/service/src/storage/__tests__/schema.spec.ts +++ b/apps/buddy/service/src/storage/__tests__/schema.spec.ts @@ -8,10 +8,10 @@ import { afterEach, describe, expect, it } from 'vitest' import { createChangeSetRepository } from '../../changes/changeSetRepository' import { createComposerResourceRepository } from '../composerResourceRepository' -import { openBuddyDatabase } from '../database' +import { openBuddyDatabase, openBuddyMigrationCandidate } from '../database' import { createExtensionInvocationRepository } from '../extensionInvocationRepository' import { BUDDY_V15_CAPABILITY_OVERRIDES_SCHEMA_SQL, BUDDY_V15_CATALOG_MODEL_ID_SCHEMA_SQL, BUDDY_V15_CATALOG_SELECTION_SCHEMA_SQL, BUDDY_V15_MODEL_SERVICES_SCHEMA_SQL, BUDDY_V15_PROVIDER_INSTANCES_SCHEMA_SQL, BUDDY_V15_REQUEST_HEADERS_SCHEMA_SQL } from '../migrations/v15ModelServices' -import { BUDDY_SCHEMA_MIGRATIONS, BUDDY_SCHEMA_VERSION } from '../schema' +import { BUDDY_MIGRATION_SCHEMA_VERSION, BUDDY_SCHEMA_MIGRATIONS, BUDDY_SCHEMA_VERSION } from '../schema' import { createUsageAnalyticsRepository } from '../usageAnalyticsRepository' import { createUsageRepository } from '../usageRepository' import { MIGRATION_TEST_TIMEOUT, openMigrationFixtureDatabase } from './migrationFixture' @@ -26,12 +26,6 @@ afterEach(() => { rmSync(directory, { force: true, recursive: true }) }) -function createDatabase(): DatabaseSync { - const database = openBuddyDatabase({ databasePath: ':memory:' }) - databases.push(database) - return database -} - function seedRun( database: DatabaseSync, memoryScope: 'personal_and_project' | 'personal_and_space' = 'personal_and_space', @@ -577,13 +571,6 @@ describe('buddy schema', { timeout: MIGRATION_TEST_TIMEOUT }, () => { expect(reopened.prepare('PRAGMA user_version').get()).toEqual({ user_version: BUDDY_SCHEMA_VERSION }) }) - it('requires an explicit database path before opening storage', () => { - for (const invalid of [undefined, ':memory:', null, [], {}, { buddyHome: '/unused' }, { databasePath: '' }, { databasePath: 'buddy.sqlite3' }]) { - expect(() => openBuddyDatabase(invalid as unknown as Parameters[0])) - .toThrow(TypeError) - } - }) - it('recovers an interrupted v20 migration and preserves legacy draft identities and resources', () => { const directory = mkdtempSync(join(tmpdir(), 'lexora-buddy-task-drafts-')) directories.push(directory) @@ -657,46 +644,12 @@ describe('buddy schema', { timeout: MIGRATION_TEST_TIMEOUT }, () => { expect(migrated.prepare('PRAGMA foreign_keys').get()).toEqual({ foreign_keys: 1 }) }) - it('rejects partial or inverted model parameter pairs at the database boundary', () => { - const database = createDatabase() - database.exec(` - INSERT INTO provider_model_states ( - provider_id, model_id, display_name, api, input_json, reasoning, cost_json, - context_window, max_tokens, override_context_window, override_max_tokens, - source_revision, acknowledged_source_revision, source, enabled, available, - last_seen_at, created_at, updated_at - ) VALUES ( - 'anthropic', 'claude', 'Claude', 'anthropic-messages', '["text"]', 0, - '{"input":0,"output":0,"cacheRead":0,"cacheWrite":0}', - 200000, 32000, NULL, NULL, '2026-08-20T00:00:00.000Z', NULL, - 'builtin', 1, 1, '2026-08-20T00:00:00.000Z', - '2026-08-20T00:00:00.000Z', '2026-08-20T00:00:00.000Z' - ); - `) - - expect(() => database.prepare(` - UPDATE provider_model_states - SET override_context_window = ?, override_max_tokens = ? - WHERE provider_id = 'anthropic' AND model_id = 'claude' - `).run(100_000, null)).toThrow(/valid pair/) - expect(() => database.prepare(` - UPDATE provider_model_states - SET override_context_window = ?, override_max_tokens = ? - WHERE provider_id = 'anthropic' AND model_id = 'claude' - `).run(16_000, 32_000)).toThrow(/valid pair/) - - seedRun(database) - expect(() => database.prepare(` - UPDATE runs SET context_window = ?, max_tokens = ? WHERE id = 'run-1' - `).run(200_000, null)).toThrow(/valid pair/) - }) - it('rejects databases created by a newer Buddy version', () => { const directory = mkdtempSync(join(tmpdir(), 'lexora-buddy-schema-')) directories.push(directory) const databasePath = join(directory, 'buddy.sqlite3') - const database = openMigrationFixtureDatabase(databasePath) - database.exec(`PRAGMA user_version = ${BUDDY_SCHEMA_VERSION + 1}`) + const database = openBuddyMigrationCandidate({ databasePath }) + database.exec(`PRAGMA user_version = ${BUDDY_MIGRATION_SCHEMA_VERSION + 1}`) database.close() expect(() => openBuddyDatabase({ databasePath })).toThrow(/newer schema version/i) @@ -888,58 +841,4 @@ describe('buddy schema', { timeout: MIGRATION_TEST_TIMEOUT }, () => { expect(() => openBuddyDatabase({ databasePath })).toThrow(/unversioned schema/i) }) - - it('enforces Space foreign keys', () => { - const database = createDatabase() - const statement = database.prepare(` - INSERT INTO conversations ( - id, space_id, title, active_branch_id, created_at, updated_at - ) VALUES (?, ?, ?, ?, ?, ?) - `) - - expect(() => statement.run( - 'conversation-1', - 'missing-space', - null, - null, - '2026-08-14T00:00:00.000Z', - '2026-08-14T00:00:00.000Z', - )).toThrow(/FOREIGN KEY/) - }) - - it('deduplicates usage by run, source entry, and purpose', () => { - const database = createDatabase() - seedRun(database) - const insert = database.prepare(` - INSERT INTO usage_records ( - id, run_id, source_entry_id, provider, model, purpose, - input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, - reasoning_tokens, total_tokens, input_cost, output_cost, - cache_read_cost, cache_write_cost, total_cost, created_at - ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `) - const values = [ - 'usage-1', - 'run-1', - 'pi-entry-1', - 'anthropic', - 'claude-sonnet-4-5', - 'turn', - 10, - 5, - 2, - 1, - null, - 18, - 0.1, - 0.2, - 0.01, - 0.02, - 0.33, - '2026-08-14T00:00:01.000Z', - ] as const - - insert.run(...values) - expect(() => insert.run('usage-2', ...values.slice(1))).toThrow(/UNIQUE/) - }) }) diff --git a/apps/buddy/service/src/storage/__tests__/sqliteRunFixture.ts b/apps/buddy/service/src/storage/__tests__/sqliteRunFixture.ts new file mode 100644 index 000000000..bd543cbc7 --- /dev/null +++ b/apps/buddy/service/src/storage/__tests__/sqliteRunFixture.ts @@ -0,0 +1,15 @@ +import type { DatabaseSync } from 'node:sqlite' + +export const sqliteRunFixtureTime = '2026-09-29T00:00:00.000Z' + +export function seedSqliteRun(database: DatabaseSync, runId: string, options: { conversationId?: string, nextSequence?: number | null } = {}) { + const conversationId = options.conversationId ?? 'task-1' + const branchId = `branch-${conversationId}` + database.prepare('INSERT OR IGNORE INTO conversations (id, created_at, updated_at) VALUES (?, ?, ?)').run(conversationId, sqliteRunFixtureTime, sqliteRunFixtureTime) + database.prepare('INSERT OR IGNORE INTO conversation_branches (id, conversation_id, created_at) VALUES (?, ?, ?)').run(branchId, conversationId, sqliteRunFixtureTime) + database.prepare('UPDATE conversations SET active_branch_id = ? WHERE id = ?').run(branchId, conversationId) + database.prepare(`INSERT INTO messages (id, conversation_id, branch_id, role, content_json, created_at) + VALUES (?, ?, ?, 'user', ?, ?)`).run(`input-${runId}`, conversationId, branchId, '{"text":"Fixture input"}', sqliteRunFixtureTime) + database.prepare(`INSERT INTO runs (id, conversation_id, branch_id, triggering_message_id, provider, model, purpose, status, started_at, next_event_sequence) + VALUES (?, ?, ?, ?, 'fixture', 'fixture', 'chat', 'queued', ?, ?)`).run(runId, conversationId, branchId, `input-${runId}`, sqliteRunFixtureTime, options.nextSequence === undefined ? 1 : options.nextSequence) +} diff --git a/apps/buddy/service/src/storage/__tests__/sqliteStartup.spec.ts b/apps/buddy/service/src/storage/__tests__/sqliteStartup.spec.ts new file mode 100644 index 000000000..40495ae0d --- /dev/null +++ b/apps/buddy/service/src/storage/__tests__/sqliteStartup.spec.ts @@ -0,0 +1,84 @@ +import { mkdtempSync, readFileSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { DatabaseSync } from 'node:sqlite' +import { afterEach, expect, it } from 'vitest' +import { openBuddyDatabase, openBuddyMigrationCandidate, openBuddySqliteDatabase } from '../database' +import { BUDDY_MIGRATION_SCHEMA_VERSION, BUDDY_SCHEMA_VERSION } from '../schema' + +const roots: string[] = [] +afterEach(() => { + for (const root of roots.splice(0)) + rmSync(root, { recursive: true, force: true }) +}) +function path() { + const root = mkdtempSync(join(tmpdir(), 'lexora-sqlite-startup-')) + roots.push(root) + return join(root, 'buddy.sqlite3') +} + +it('initializes a new database with ready authority and FULL durability, then reopens the same generation', () => { + const databasePath = path() + const database = openBuddySqliteDatabase({ databasePath }) + const identity = database.prepare('SELECT * FROM storage_profile').get() + expect(identity).toMatchObject({ authority: 'sqlite', migration_state: 'ready' }) + expect(database.prepare('PRAGMA user_version').get()?.user_version).toBe(BUDDY_MIGRATION_SCHEMA_VERSION) + expect(database.prepare('PRAGMA synchronous').get()?.synchronous).toBe(2) + expect(database.prepare('PRAGMA foreign_keys').get()?.foreign_keys).toBe(1) + expect(database.prepare('PRAGMA foreign_key_check').all()).toEqual([]) + database.close() + const reopened = openBuddySqliteDatabase({ databasePath }) + try { + expect(reopened.prepare('SELECT * FROM storage_profile').get()).toEqual(identity) + } + finally { reopened.close() } +}) + +it('refuses a legacy profile before changing its schema or bytes', () => { + const databasePath = path() + const legacy = openBuddyDatabase({ databasePath }) + legacy.close() + const original = readFileSync(databasePath) + expect(() => openBuddySqliteDatabase({ databasePath })).toThrow('explicit persistence migration required') + expect(readFileSync(databasePath)).toEqual(original) + const reopened = openBuddyDatabase({ databasePath }) + try { + expect(reopened.prepare('PRAGMA user_version').get()?.user_version).toBe(BUDDY_SCHEMA_VERSION) + } + finally { reopened.close() } +}) + +it('does not activate an unfinished candidate even when it contains no tasks', () => { + const databasePath = path() + const pending = openBuddyMigrationCandidate({ databasePath }) + pending.close() + const original = readFileSync(databasePath) + expect(() => openBuddySqliteDatabase({ databasePath })).toThrow('authority is not ready') + expect(readFileSync(databasePath)).toEqual(original) +}) + +it('does not initialize an unversioned file containing existing tables', () => { + const databasePath = path() + const unknown = new DatabaseSync(databasePath) + unknown.exec('CREATE TABLE preserved (value TEXT); INSERT INTO preserved VALUES (\'existing user data\')') + unknown.close() + const original = readFileSync(databasePath) + expect(() => openBuddySqliteDatabase({ databasePath })).toThrow('unversioned schema') + expect(readFileSync(databasePath)).toEqual(original) +}) + +it('rejects a live legacy WAL without checkpointing or changing its committed bytes', () => { + const databasePath = path() + const legacy = openBuddyDatabase({ databasePath }) + try { + legacy.exec('INSERT INTO conversations (id, title, created_at, updated_at) VALUES (\'preserved\', \'Keep WAL data\', \'2026-11-07\', \'2026-11-07\')') + const databaseBytes = readFileSync(databasePath) + const journalBytes = readFileSync(`${databasePath}-wal`) + expect(journalBytes.length).toBeGreaterThan(0) + expect(() => openBuddySqliteDatabase({ databasePath })).toThrow('explicit persistence migration required') + expect(readFileSync(databasePath)).toEqual(databaseBytes) + expect(readFileSync(`${databasePath}-wal`)).toEqual(journalBytes) + expect(legacy.prepare('SELECT title FROM conversations WHERE id = ?').get('preserved')?.title).toBe('Keep WAL data') + } + finally { legacy.close() } +}) diff --git a/apps/buddy/service/src/storage/__tests__/taskMarkRepository.spec.ts b/apps/buddy/service/src/storage/__tests__/taskMarkRepository.spec.ts index 084346f86..a2df31bd2 100644 --- a/apps/buddy/service/src/storage/__tests__/taskMarkRepository.spec.ts +++ b/apps/buddy/service/src/storage/__tests__/taskMarkRepository.spec.ts @@ -3,7 +3,6 @@ import { mkdtempSync, rmSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, describe, expect, it } from 'vitest' -import { SYSTEM_UNREAD_MARK_ID } from '../../../../shared/conversation/taskMarkApi' import { createConversationRepository } from '../conversationRepository' import { openBuddyDatabase } from '../database' import { createRunRepository } from '../runRepository' @@ -65,18 +64,6 @@ describe('task marks and reading state', () => { expect(f.marks.getState('a')).toMatchObject({ markId: null, unread: true, resultRunId: 'next-result' }) }) - it.each(['automatic', 'manual'] as const)('clears %s unread without a custom mark', (kind) => { - const f = fixture() - if (kind === 'automatic') - f.run('result') - else f.marks.setRead({ ...f.marks.getState('a'), read: false }) - const before = f.marks.getState('a') - expect(before).toMatchObject({ markId: null, unread: true }) - const cleared = f.marks.clear(before) - expect(cleared).toMatchObject({ markId: null, unread: false, readRevision: before.readRevision + 1 }) - expect(f.marks.clear(before)).toEqual(cleared) - }) - it('does not clear later results or later manual unread with a stale clear request', () => { const f = fixture() const custom = f.marks.create(mark) @@ -127,20 +114,6 @@ describe('task marks and reading state', () => { expect(f.marks.getState('a')).toMatchObject({ markId: custom.id, unread: true }) }) - it('observes terminal results, not queued work, compaction or cancellation', () => { - const f = fixture() - f.run('active', 'running') - expect(f.marks.getState('a').unread).toBe(false) - f.runs.reconcileTerminal('active', 'completed', now, null) - expect(f.marks.getState('a')).toMatchObject({ resultRunId: 'active', unread: true }) - f.marks.setRead({ ...f.marks.getState('a'), read: true }) - f.run('compaction', 'completed', 'conversation.compaction') - f.run('cancelled', 'cancelled') - expect(f.marks.getState('a').unread).toBe(false) - f.run('failure', 'failed') - expect(f.marks.getState('a')).toMatchObject({ resultRunId: 'failure', unread: true }) - }) - it('updates shared definitions by ID and deletes only associations, excluding deleted tasks', () => { const f = fixture() const first = f.marks.create(mark) @@ -166,15 +139,6 @@ describe('task marks and reading state', () => { expect(() => f.marks.setRead({ conversationId: 'a', resultRunId: 'result', readRevision: 0, read: true })).toThrow() }) - it('protects the system identity even when a custom mark has the same name', () => { - const f = fixture() - const custom = f.marks.create({ ...mark, name: '未读' }) - expect(custom.id).not.toBe(SYSTEM_UNREAD_MARK_ID) - for (const action of [() => f.marks.update(SYSTEM_UNREAD_MARK_ID, mark), () => f.marks.delete(SYSTEM_UNREAD_MARK_ID), () => f.marks.assign('a', SYSTEM_UNREAD_MARK_ID)]) - expect(action).toThrow() - expect(f.marks.list()).toHaveLength(1) - }) - it('migrates existing results as read, preserves history and persists new unread and assignments across restarts', () => { const directory = mkdtempSync(join(tmpdir(), 'buddy-task-marks-')) directories.push(directory) diff --git a/apps/buddy/service/src/storage/__tests__/threadBindingRepository.spec.ts b/apps/buddy/service/src/storage/__tests__/threadBindingRepository.spec.ts index 9a9fe529b..c0965d1c5 100644 --- a/apps/buddy/service/src/storage/__tests__/threadBindingRepository.spec.ts +++ b/apps/buddy/service/src/storage/__tests__/threadBindingRepository.spec.ts @@ -23,7 +23,7 @@ it('keeps stable member threads and separates journals and latest contexts by ac f.conversations.addMember({ id: 'conversation', buddyId: f.actor.id, updatedAt: executionNow }) f.conversations.setBuddy({ id: 'conversation', buddyId: f.actor.id, updatedAt: executionNow }) expect(f.threads.ensureActor('conversation', 'default', executionNow).id).toBe(original.id) - expect(f.threads.ensureActor('conversation', f.actor.id, executionNow).id).toBe(actor.threadId) + expect(f.threads.ensureActor('conversation', f.actor.id, executionNow, 'branch').id).toBe(actor.threadId) expect(f.runs.findLatestForThread(original.id, 'branch')?.id).toBe('root') expect(f.runs.findLatestForThread(actor.threadId, 'branch')?.id).toBe('actor-child') expect(f.threads.findBinding(actor.threadId)).toMatchObject({ sessionFile: '/fixture/b.jsonl', rootEntryId: 'root-b' }) diff --git a/apps/buddy/service/src/storage/__tests__/transaction.spec.ts b/apps/buddy/service/src/storage/__tests__/transaction.spec.ts new file mode 100644 index 000000000..1152f357e --- /dev/null +++ b/apps/buddy/service/src/storage/__tests__/transaction.spec.ts @@ -0,0 +1,246 @@ +import type { DatabaseSync as SqliteDatabase } from 'node:sqlite' +import { mkdtempSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { constants, DatabaseSync } from 'node:sqlite' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { afterTransactionCommit, BuddyTransactionUncertainError, readTransactionFailure, withTransaction } from '../transaction' + +const databases: SqliteDatabase[] = [] +const directories: string[] = [] + +afterEach(() => { + vi.restoreAllMocks() + for (const database of databases.splice(0)) { + if (database.isOpen) + database.close() + } + for (const directory of directories.splice(0)) + rmSync(directory, { recursive: true, force: true }) +}) + +function open(databasePath = ':memory:'): SqliteDatabase { + const database = new DatabaseSync(databasePath) + database.exec('PRAGMA busy_timeout = 0; CREATE TABLE IF NOT EXISTS entries (id INTEGER PRIMARY KEY, value BLOB)') + databases.push(database) + return database +} + +function temporaryPath(): string { + const directory = mkdtempSync(join(tmpdir(), 'lexora-transaction-')) + directories.push(directory) + return join(directory, 'candidate.sqlite3') +} + +function capture(operation: () => unknown): unknown { + try { + operation() + } + catch (error) { + return error + } + throw new Error('Expected operation to fail') +} + +describe('storage transactions', () => { + it('joins the caller transaction and commits all repository writes once', () => { + const database = open() + const value = withTransaction(database, () => { + database.exec('INSERT INTO entries VALUES (1, NULL)') + return withTransaction(database, () => { + database.exec('INSERT INTO entries VALUES (2, NULL)') + return 'accepted' + }) + }) + expect(value).toBe('accepted') + expect(database.isTransaction).toBe(false) + expect(database.prepare('SELECT id FROM entries ORDER BY id').all()).toEqual([{ id: 1 }, { id: 2 }]) + }) + + it('rolls back all writes even when the caller catches a participant failure', () => { + const database = open() + const original = new Error('intent conflicts') + const failure = capture(() => withTransaction(database, () => { + database.exec('INSERT INTO entries VALUES (1, NULL)') + expect(capture(() => withTransaction(database, () => { + database.exec('INSERT INTO entries VALUES (2, NULL)') + throw original + }))).toBe(original) + database.exec('INSERT INTO entries VALUES (3, NULL)') + })) + expect(failure).toBe(original) + expect(readTransactionFailure(failure)).toEqual({ phase: 'operation', outcome: 'rolled_back' }) + expect(database.prepare('SELECT * FROM entries').all()).toEqual([]) + withTransaction(database, () => database.exec('INSERT INTO entries VALUES (4, NULL)')) + expect(database.prepare('SELECT id FROM entries').all()).toEqual([{ id: 4 }]) + }) + + it('does not adopt or roll back an unmanaged transaction', () => { + const database = open() + database.exec('BEGIN; INSERT INTO entries VALUES (1, NULL)') + expect(() => withTransaction(database, () => database.exec('INSERT INTO entries VALUES (2, NULL)'))).toThrow('owned by withTransaction') + expect(database.isTransaction).toBe(true) + database.exec('COMMIT') + expect(database.prepare('SELECT id FROM entries').all()).toEqual([{ id: 1 }]) + }) + + it('retains SQLITE_FULL after SQLite automatically rolls back the transaction', () => { + const database = open(temporaryPath()) + database.exec('PRAGMA journal_mode = WAL; PRAGMA synchronous = FULL') + const pages = database.prepare('PRAGMA page_count').get() as { page_count: number } + database.exec(`PRAGMA max_page_count = ${pages.page_count + 1}`) + const failure = capture(() => withTransaction(database, () => { + database.exec('INSERT INTO entries VALUES (1, NULL)') + database.exec('INSERT INTO entries VALUES (2, zeroblob(1048576))') + })) + expect(failure).toMatchObject({ code: 'ERR_SQLITE_ERROR', errcode: 13 }) + expect(readTransactionFailure(failure)).toEqual({ phase: 'operation', outcome: 'rolled_back' }) + expect(database.isTransaction).toBe(false) + expect(database.prepare('SELECT * FROM entries').all()).toEqual([]) + withTransaction(database, () => database.exec('INSERT INTO entries VALUES (1, NULL)')) + }) + + it('retains BEGIN BUSY without rolling back another connection', () => { + const databasePath = temporaryPath() + const database = open(databasePath) + const writer = open(databasePath) + writer.exec('BEGIN IMMEDIATE; INSERT INTO entries VALUES (1, NULL)') + const failure = capture(() => withTransaction(database, () => database.exec('INSERT INTO entries VALUES (2, NULL)'))) + expect(failure).toMatchObject({ errcode: 5 }) + expect(readTransactionFailure(failure)).toEqual({ phase: 'begin', outcome: 'not_started' }) + expect(writer.isTransaction).toBe(true) + writer.exec('COMMIT') + expect(database.prepare('SELECT id FROM entries').all()).toEqual([{ id: 1 }]) + }) + + it('closes an auto-rolled-back participant connection before a caught failure can leak autocommit writes', () => { + const databasePath = temporaryPath() + const database = open(databasePath) + const pages = database.prepare('PRAGMA page_count').get() as { page_count: number } + database.exec(`PRAGMA max_page_count = ${pages.page_count + 1}`) + const failure = capture(() => withTransaction(database, () => { + expect(capture(() => withTransaction(database, () => { + database.exec('INSERT INTO entries VALUES (1, zeroblob(1048576))') + }))).toMatchObject({ errcode: 13 }) + database.exec('INSERT INTO entries VALUES (2, NULL)') + })) + expect(failure).toMatchObject({ errcode: 13 }) + expect(readTransactionFailure(failure)).toEqual({ phase: 'operation', outcome: 'rolled_back' }) + expect(database.isOpen).toBe(false) + expect(open(databasePath).prepare('SELECT * FROM entries').all()).toEqual([]) + }) + + it('rolls back COMMIT BUSY and never publishes an acceptance', () => { + const databasePath = temporaryPath() + const database = open(databasePath) + const reader = open(databasePath) + reader.exec('BEGIN') + reader.prepare('SELECT * FROM entries').all() + const notifications: string[] = [] + const failure = capture(() => withTransaction(database, () => { + database.exec('INSERT INTO entries VALUES (1, NULL)') + afterTransactionCommit(database, () => notifications.push('accepted'), () => {}) + })) + expect(failure).toMatchObject({ errcode: 5 }) + expect(readTransactionFailure(failure)).toEqual({ phase: 'commit', outcome: 'rolled_back' }) + expect(database.isTransaction).toBe(false) + reader.exec('COMMIT') + expect(database.prepare('SELECT * FROM entries').all()).toEqual([]) + expect(notifications).toEqual([]) + withTransaction(database, () => database.exec('INSERT INTO entries VALUES (1, NULL)')) + }) + + it('retains the original error and blocks subsequent transactions if rollback is denied', () => { + const database = open() + database.setAuthorizer((action, operation) => action === constants.SQLITE_TRANSACTION && operation === 'ROLLBACK' ? constants.SQLITE_DENY : constants.SQLITE_OK) + const original = new Error('write failed') + const failure = capture(() => withTransaction(database, () => { + database.exec('INSERT INTO entries VALUES (1, NULL)') + throw original + })) + expect(failure).toBeInstanceOf(BuddyTransactionUncertainError) + expect(failure).toMatchObject({ cause: original }) + expect(readTransactionFailure(failure)).toMatchObject({ phase: 'operation', outcome: 'unknown', cleanupError: { errcode: 23 } }) + expect(capture(() => withTransaction(database, () => database.exec('INSERT INTO entries VALUES (2, NULL)')))).toBe(failure) + expect(database.prepare('SELECT id FROM entries').all()).toEqual([{ id: 1 }]) + database.setAuthorizer(null) + database.exec('ROLLBACK') + expect(capture(() => withTransaction(database, () => {}))).toBe(failure) + }) + + it('requires a new connection to verify a commit that succeeded before an injected error', () => { + const databasePath = temporaryPath() + const database = open(databasePath) + const exec = database.exec.bind(database) + const original = new Error('injected failure after COMMIT') + vi.spyOn(database, 'exec').mockImplementation((sql) => { + exec(sql) + if (sql === 'COMMIT') + throw original + }) + const notifications: string[] = [] + const failure = capture(() => withTransaction(database, () => { + database.exec('INSERT INTO entries VALUES (1, NULL)') + afterTransactionCommit(database, () => notifications.push('accepted'), () => {}) + })) + expect(failure).toBeInstanceOf(BuddyTransactionUncertainError) + expect(failure).toMatchObject({ cause: original }) + expect(readTransactionFailure(failure)).toEqual({ phase: 'commit', outcome: 'unknown' }) + expect(capture(() => withTransaction(database, () => database.exec('INSERT INTO entries VALUES (2, NULL)')))).toBe(failure) + expect(notifications).toEqual([]) + database.close() + expect(open(databasePath).prepare('SELECT id FROM entries').all()).toEqual([{ id: 1 }]) + }) + + it('rejects async callbacks before their body runs and rolls back returned promises', async () => { + const database = open() + const asyncOperation = async () => { + database.exec('INSERT INTO entries VALUES (1, NULL)') + } + // @ts-expect-error Transaction callbacks cannot be asynchronous. + expect(() => withTransaction(database, asyncOperation)).toThrow('synchronous') + const returnsPromise = () => { + database.exec('INSERT INTO entries VALUES (2, NULL)') + return Promise.reject(new Error('late failure')) + } + // @ts-expect-error Thenable results cannot escape the transaction. + expect(() => withTransaction(database, returnsPromise)).toThrow('promise') + await Promise.resolve() + expect(database.prepare('SELECT * FROM entries').all()).toEqual([]) + expect(database.isTransaction).toBe(false) + }) + + it('publishes after the outer commit in FIFO order even when an observer starts another transaction', async () => { + const database = open() + const notifications: number[] = [] + const errors: unknown[] = [] + const original = new Error('observer failed') + const notify = (id: number) => afterTransactionCommit(database, () => { + expect(database.isTransaction).toBe(false) + expect(database.prepare('SELECT id FROM entries WHERE id = ?').get(id)).toEqual({ id }) + notifications.push(id) + if (id === 1) { + withTransaction(database, () => { + database.exec('INSERT INTO entries VALUES (3, NULL)') + notify(3) + }) + throw original + } + if (id === 2) + return Promise.reject(original) + }, error => errors.push(error)) + withTransaction(database, () => { + database.exec('INSERT INTO entries VALUES (1, NULL)') + notify(1) + withTransaction(database, () => { + database.exec('INSERT INTO entries VALUES (2, NULL)') + notify(2) + }) + expect(notifications).toEqual([]) + }) + await Promise.resolve() + expect(notifications).toEqual([1, 2, 3]) + expect(errors).toEqual([original, original]) + expect(database.prepare('SELECT id FROM entries ORDER BY id').all()).toEqual([{ id: 1 }, { id: 2 }, { id: 3 }]) + }) +}) diff --git a/apps/buddy/service/src/storage/__tests__/turnRequestRepository.spec.ts b/apps/buddy/service/src/storage/__tests__/turnRequestRepository.spec.ts index f6d894e76..b5d50c853 100644 --- a/apps/buddy/service/src/storage/__tests__/turnRequestRepository.spec.ts +++ b/apps/buddy/service/src/storage/__tests__/turnRequestRepository.spec.ts @@ -12,7 +12,7 @@ import { createAttachmentRepository } from '../attachmentRepository' import { ComposerDraftCommitConflictError } from '../commitComposerDraft' import { createComposerDraftRepository } from '../composerDraftRepository' import { createConversationRepository } from '../conversationRepository' -import { openBuddyDatabase } from '../database' +import { openBuddyDatabase, withTransaction } from '../database' import { createRunRepository } from '../runRepository' import { createThreadBindingRepository } from '../threadBindingRepository' import { @@ -29,6 +29,29 @@ afterEach(() => { }) describe('turnRequestRepository', () => { + it('publishes accepted facts only after the outer owner transaction commits', () => { + const database = createDatabase() + const service = new TurnRequestService(database) + const events: TurnRequestCommit[] = [] + service.onDidCommit(event => events.push(event)) + const original = createComposerDraftRepository(database).findById('draft-1') + expect(() => withTransaction(database, () => { + service.prepare(createInput()) + expect(events).toEqual([]) + throw new Error('owner rejected') + })).toThrow('owner rejected') + expect(database.prepare('SELECT id FROM runs').all()).toEqual([]) + expect(createComposerDraftRepository(database).findById('draft-1')).toEqual(original) + expect(events).toEqual([]) + withTransaction(database, () => { + service.prepare(createInput()) + expect(events).toEqual([]) + }) + expect(events).toHaveLength(1) + expect(database.prepare('SELECT id FROM runs').all()).toEqual([{ id: 'run-1' }]) + service.dispose() + }) + it('continues a persisted question answer without reading or consuming a composer draft', () => { const database = createDatabase() const repository = createTurnRequestRepository(database) @@ -180,7 +203,7 @@ describe('turnRequestRepository', () => { const database = createDatabase() const repository = createTurnRequestRepository(database) const failure = new RunEventCorruptionError('historical-run') - const service = new TurnRequestService(repository, undefined, { + const service = new TurnRequestService(database, undefined, { assertConversationWritable: () => { throw failure }, }) const original = createComposerDraftRepository(database).findById('draft-1') @@ -202,7 +225,7 @@ describe('turnRequestRepository', () => { attachments.create({ id: 'owned-attachment', conversationId: null, draftId: 'draft-1', messageId: null, name: 'private-name.txt', mimeType: 'text/plain', sizeBytes: 1, storedPath: '/private/source.txt', createdAt: '2026-08-14T00:00:00.000Z' }) const input = createInput() const prepared = { ...input, runInput: { ...input.runInput, attachmentIds: ['owned-attachment'] }, attachmentBindings: [{ id: 'owned-attachment', sourceAttachmentId: 'owned-attachment', sourceDraftId: 'draft-1', messageId: input.userMessageId, storedPath: '/private/destination.txt', createdAt: input.createdAt }] } - const service = new TurnRequestService(createTurnRequestRepository(database)) + const service = new TurnRequestService(database) const events: TurnRequestCommit[] = [] service.onDidCommit(event => events.push(event)) expect(() => service.prepare({ ...prepared, draft: { draftId: 'draft-1', expectedRevision: 99 } })).toThrow(ComposerDraftCommitConflictError) @@ -217,7 +240,7 @@ describe('turnRequestRepository', () => { it('publishes one frozen transaction batch and no facts for rollback or request replay', () => { const database = createDatabase() - const service = new TurnRequestService(createTurnRequestRepository(database)) + const service = new TurnRequestService(database) const events: TurnRequestCommit[] = [] service.onDidCommit(event => events.push(event)) const input = createInput() @@ -434,8 +457,7 @@ describe('turnRequestRepository', () => { it('atomically replaces an interrupted run without duplicating the user message', () => { const database = createDatabase() - const repository = createTurnRequestRepository(database) - const service = new TurnRequestService(repository) + const service = new TurnRequestService(database) const events: TurnRequestCommit[] = [] service.onDidCommit(event => events.push(event)) service.prepare(createInput()) diff --git a/apps/buddy/service/src/storage/__tests__/usageAnalyticsRepository.spec.ts b/apps/buddy/service/src/storage/__tests__/usageAnalyticsRepository.spec.ts index 941205a64..8a2f4848f 100644 --- a/apps/buddy/service/src/storage/__tests__/usageAnalyticsRepository.spec.ts +++ b/apps/buddy/service/src/storage/__tests__/usageAnalyticsRepository.spec.ts @@ -1,28 +1,21 @@ import type { DatabaseSync } from 'node:sqlite' -import { mkdtempSync, rmSync } from 'node:fs' -import { tmpdir } from 'node:os' -import { join } from 'node:path' import { afterEach, describe, expect, it } from 'vitest' -import { usageAnalyticsSchema, usagePeriodSchema, usageTopTasksRequestSchema, usageTrendRequestSchema, usageTrendSchema } from '../../../../shared/usage/usageAnalyticsApi' +import { usageAnalyticsSchema, usageTrendSchema } from '../../../../shared/usage/usageAnalyticsApi' import { openBuddyDatabase } from '../database' -import { BUDDY_SCHEMA_MIGRATIONS } from '../schema' import { createUsageAnalyticsRepository } from '../usageAnalyticsRepository' -import { openMigrationFixtureDatabase } from './migrationFixture' const databases: DatabaseSync[] = [] afterEach(() => databases.splice(0).forEach(database => database.close())) -function fixture(databasePath = ':memory:') { - const database = openBuddyDatabase({ databasePath }) +function fixture() { + const database = openBuddyDatabase({ databasePath: ':memory:' }) databases.push(database) let sequence = 0 const instant = '2026-01-01T00:00:00.000Z' - function run(id: string, deleted = false) { + function run(id: string) { database.prepare('INSERT INTO conversations (id, title, created_at, updated_at) VALUES (?, ?, ?, ?)').run(id, `Task ${id}`, instant, instant) database.prepare('INSERT INTO conversation_branches (id, conversation_id, created_at) VALUES (?, ?, ?)').run(id, id, instant) database.prepare(`INSERT INTO runs (id, conversation_id, branch_id, triggering_message_id, provider, model, purpose, status, started_at) VALUES (?, ?, ?, 'message', 'provider', 'model', 'chat', 'completed', ?)`).run(id, id, id, instant) - if (deleted) - database.prepare('UPDATE conversations SET deleted_at = ? WHERE id = ?').run(instant, id) } function usage(runId: string, createdAt = instant, provider = 'provider', model = 'model', tokens = 22, purpose = 'turn') { const id = `usage-${++sequence}` @@ -64,20 +57,10 @@ describe('usage analytics', () => { expect(trend.slice(1).every(bucket => bucket.totalTokens === null && bucket.recordCount === 0)).toBe(true) }) - it('uses local dates with inclusive start and exclusive next-day boundaries', () => { - const f = fixture() - f.run('task') - for (const date of ['2025-12-31T15:59:59.999Z', '2025-12-31T16:00:00.000Z', '2026-01-01T15:59:59.999Z', '2026-01-01T16:00:00.000Z']) - f.usage('task', date) - const range = { startDate: '2026-01-01', endDate: '2026-01-01', timeZone: 'Asia/Shanghai' } - expect(f.repository.analytics(range).days).toMatchObject([{ date: '2026-01-01', recordCount: 2, totalTokens: 44 }]) - expect(f.repository.topTasks({ ...range, model: null })).toMatchObject([{ recordCount: 2, totalTokens: 44 }]) - }) - it.each([ - ['2026-03-08', '2026-03-08T08:00:00.000Z', '2026-03-09T06:59:59.999Z', '2026-03-09T07:00:00.000Z'], - ['2026-11-01', '2026-11-01T07:00:00.000Z', '2026-11-02T07:59:59.999Z', '2026-11-02T08:00:00.000Z'], - ])('groups the DST day %s without assuming 24 hours', (date, start, last, end) => { + ['2026-03-08', 23, '2026-03-08T08:00:00.000Z', '2026-03-09T06:59:59.999Z', '2026-03-09T07:00:00.000Z'], + ['2026-11-01', 25, '2026-11-01T07:00:00.000Z', '2026-11-02T07:59:59.999Z', '2026-11-02T08:00:00.000Z'], + ])('groups the DST day %s across %i distinct contiguous hours', (date, hours, start, last, end) => { const f = fixture() f.run('task') for (const instant of [start, last, end]) @@ -85,6 +68,12 @@ describe('usage analytics', () => { const range = { startDate: date, endDate: date, timeZone: 'America/Los_Angeles' } expect(f.repository.analytics(range).days).toMatchObject([{ date, recordCount: 2 }]) expect(f.repository.topTasks({ ...range, model: null })).toMatchObject([{ recordCount: 2 }]) + const trend = f.repository.trend({ ...range, granularity: 'hour' }) + expect(trend).toHaveLength(hours) + expect(trend[0]!.startAt).toBe(start) + expect(trend.at(-1)!.endAt).toBe(end) + expect(new Set(trend.map(bucket => bucket.startAt)).size).toBe(hours) + expect(trend.slice(1).every((bucket, index) => bucket.startAt === trend[index]!.endAt)).toBe(true) }) it('keeps missing days distinct from zero-token reports and preserves provider/model identity', () => { @@ -102,8 +91,9 @@ describe('usage analytics', () => { it('retains deleted consumption in totals but does not return deleted task identities', () => { const f = fixture() - f.run('deleted', true) + f.run('deleted') f.usage('deleted', '2026-01-01T00:00:00.000Z', 'provider', 'model', 99999) + f.database.prepare('UPDATE conversations SET deleted_at = ? WHERE id = ?').run('2026-01-01T00:00:00.000Z', 'deleted') for (let index = 1; index <= 12; index++) { f.run(`task-${index}`) f.usage(`task-${index}`, '2026-01-01T00:00:00.000Z', 'provider', 'model', index) @@ -113,38 +103,6 @@ describe('usage analytics', () => { expect(tasks.map(task => task.conversationId)).toEqual(Array.from({ length: 5 }, (_, index) => `task-${12 - index}`)) expect(JSON.stringify(tasks)).not.toContain('deleted') }) - - it('upgrades an existing usage ledger without losing its records', () => { - const directory = mkdtempSync(join(tmpdir(), 'buddy-usage-upgrade-')) - try { - const path = join(directory, 'buddy.sqlite3') - const legacy = openMigrationFixtureDatabase(path) - for (const migration of BUDDY_SCHEMA_MIGRATIONS.filter(value => value.version <= 13)) - legacy.exec(migration.sql) - legacy.exec(` - PRAGMA user_version = 13; - INSERT INTO conversations (id,title,created_at,updated_at) VALUES ('retained','Retained','2026-01-01T00:00:00.000Z','2026-01-01T00:00:00.000Z'); - INSERT INTO conversation_branches (id,conversation_id,created_at) VALUES ('retained','retained','2026-01-01T00:00:00.000Z'); - INSERT INTO runs (id,conversation_id,branch_id,triggering_message_id,provider,model,purpose,status,started_at) VALUES ('retained','retained','retained','message','provider','model','chat','completed','2026-01-01T00:00:00.000Z'); - INSERT INTO usage_records (id,run_id,source_entry_id,provider,model,purpose,input_tokens,output_tokens,cache_read_tokens,cache_write_tokens,reasoning_tokens,total_tokens,input_cost,output_cost,cache_read_cost,cache_write_cost,total_cost,created_at) - VALUES ('usage','retained','usage','provider','model','turn',10,5,3,4,2,22,0,0,0,0,0,'2026-01-01T00:00:00.000Z'); - `) - const records = legacy.prepare('SELECT * FROM usage_records').all() - legacy.close() - const upgraded = openBuddyDatabase({ databasePath: path }) - try { - expect(upgraded.prepare('SELECT * FROM usage_records').all()).toEqual(records.map(record => ({ ...record, invocation_id: null, detached: 0 }))) - expect(createUsageAnalyticsRepository(upgraded).analytics(period).days).toMatchObject([{ recordCount: 1, totalTokens: 22 }]) - expect(upgraded.prepare('PRAGMA foreign_key_check').all()).toEqual([]) - } - finally { - upgraded.close() - } - } - finally { - rmSync(directory, { recursive: true, force: true }) - } - }) }) describe('usage trend queries', () => { @@ -164,19 +122,6 @@ describe('usage trend queries', () => { expect(f.repository.trend({ ...range, granularity: 'day' })).toMatchObject([{ totalTokens: 44, recordCount: 3 }]) }) - it.each([ - ['2026-03-08', 23, '2026-03-08T08:00:00.000Z', '2026-03-09T07:00:00.000Z'], - ['2026-11-01', 25, '2026-11-01T07:00:00.000Z', '2026-11-02T08:00:00.000Z'], - ])('covers the real %s day with %i hours', (date, hours, startAt, endAt) => { - const f = fixture() - const trend = f.repository.trend({ startDate: date, endDate: date, timeZone: 'America/Los_Angeles', granularity: 'hour' }) - expect(trend).toHaveLength(hours) - expect(trend[0]!.startAt).toBe(startAt) - expect(trend.at(-1)!.endAt).toBe(endAt) - expect(new Set(trend.map(bucket => bucket.startAt)).size).toBe(hours) - expect(trend.slice(1).every((bucket, index) => bucket.startAt === trend[index]!.endAt)).toBe(true) - }) - it('does not merge the repeated local hour on DST fallback', () => { const f = fixture() f.run('task') @@ -188,32 +133,4 @@ describe('usage trend queries', () => { { startAt: '2026-11-01T09:00:00.000Z', endAt: '2026-11-01T10:00:00.000Z', totalTokens: 20, recordCount: 1 }, ]) }) - - it('clips a fractional DST hour at the next local midnight', () => { - const f = fixture() - const trend = f.repository.trend({ startDate: '2026-04-05', endDate: '2026-04-05', timeZone: 'Australia/Lord_Howe', granularity: 'hour' }) - const last = trend.at(-1)! - expect(trend).toHaveLength(25) - expect(Date.parse(last.endAt) - Date.parse(last.startAt)).toBe(30 * 60_000) - expect(last.endAt).toBe('2026-04-05T13:30:00.000Z') - }) -}) - -describe('usage request boundaries', () => { - it('accepts a complete leap year and rejects invalid or unbounded requests', () => { - expect(usagePeriodSchema.safeParse({ ...period, startDate: '2024-01-01', endDate: '2024-12-31' }).success).toBe(true) - for (const patch of [ - { startDate: '2025-12-30' }, - { startDate: '2027-01-01' }, - { startDate: '2026-02-30' }, - { timeZone: 'Not/AZone' }, - { path: '/private' }, - ]) - expect(usagePeriodSchema.safeParse({ ...period, ...patch }).success).toBe(false) - expect(usageTopTasksRequestSchema.safeParse({ ...period, model: { modelId: 'same' } }).success).toBe(false) - expect(usageTrendRequestSchema.safeParse({ ...period, granularity: 'day' }).success).toBe(true) - expect(usageTrendRequestSchema.safeParse({ ...period, granularity: 'hour' }).success).toBe(false) - expect(usageTrendRequestSchema.safeParse({ ...period, endDate: period.startDate, granularity: 'hour' }).success).toBe(true) - expect(usageTrendRequestSchema.safeParse({ ...period, granularity: 'month' }).success).toBe(false) - }) }) diff --git a/apps/buddy/service/src/storage/__tests__/verifyCommandReceipt.spec.ts b/apps/buddy/service/src/storage/__tests__/verifyCommandReceipt.spec.ts new file mode 100644 index 000000000..954055775 --- /dev/null +++ b/apps/buddy/service/src/storage/__tests__/verifyCommandReceipt.spec.ts @@ -0,0 +1,95 @@ +import type { StorageCommand } from '../CommandCommitter' +import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { CommandCommitter } from '../CommandCommitter' +import { openBuddyMigrationCandidate } from '../database' +import { guardStorageDatabase } from '../StorageIsolation' +import { verifyCommandReceipt } from '../verifyCommandReceipt' + +const directories: string[] = [] + +afterEach(() => { + for (const directory of directories.splice(0)) + rmSync(directory, { recursive: true, force: true }) +}) + +function fixture() { + const root = mkdtempSync(join(tmpdir(), 'lexora-receipt-verification-')) + directories.push(root) + const databasePath = join(root, 'candidate.sqlite3') + const database = openBuddyMigrationCandidate({ databasePath }) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\'') + const identity: StorageCommand = { + dataGeneration: String(database.prepare('SELECT data_generation FROM storage_profile').get()!.data_generation), + actorId: 'desktop-local', + commandId: 'command-1', + method: 'fixture', + fingerprint: 'intent-1', + fingerprintVersion: 1, + sourceInstanceId: 'instance-1', + } + return { root, databasePath, database, identity } +} + +describe('read-only command receipt verification', () => { + it('returns only the original receipt without changing the database or independent files', () => { + const f = fixture() + const receipt = new CommandCommitter(f.database).accept(f.identity, () => ({ target: { runId: 'original-run' }, result: {} })).receipt + f.database.close() + const independent = join(f.root, 'session.jsonl') + writeFileSync(independent, 'independent session contents\n') + const before = readFileSync(f.databasePath) + expect(verifyCommandReceipt(f)).toEqual(receipt) + expect(verifyCommandReceipt({ ...f, identity: { ...f.identity, commandId: 'absent' } })).toBeNull() + expect(readFileSync(f.databasePath)).toEqual(before) + expect(readFileSync(independent, 'utf8')).toBe('independent session contents\n') + }) + + it.each(['generation', 'actor', 'pending'] as const)('rejects %s mismatch instead of reporting a missing command', (kind) => { + const f = fixture() + if (kind === 'pending') + f.database.exec('UPDATE storage_profile SET authority = \'legacy_jsonl\', migration_state = \'pending\'') + f.database.close() + const before = readFileSync(f.databasePath) + const identity = { ...f.identity } + if (kind === 'generation') + identity.dataGeneration = 'another-generation' + if (kind === 'actor') + identity.actorId = 'another-actor' + expect(() => verifyCommandReceipt({ databasePath: f.databasePath, identity })).toThrow() + expect(readFileSync(f.databasePath)).toEqual(before) + }) + + it('does not create or replace a missing or unreadable source', () => { + const f = fixture() + f.database.close() + const missing = join(f.root, 'absent', 'candidate.sqlite3') + expect(() => verifyCommandReceipt({ ...f, databasePath: missing })).toThrow() + expect(existsSync(join(f.root, 'absent'))).toBe(false) + writeFileSync(f.databasePath, 'unreadable database'.repeat(64)) + const before = readFileSync(f.databasePath) + expect(() => verifyCommandReceipt(f)).toThrow() + expect(readFileSync(f.databasePath)).toEqual(before) + expect(() => verifyCommandReceipt({ ...f, databasePath: ':memory:' })).toThrow('existing absolute') + }) + + it('isolates corrupt receipts on both lookup and repeated acceptance', () => { + for (const operation of ['find', 'accept'] as const) { + const f = fixture() + try { + const committer = new CommandCommitter(f.database) + committer.accept(f.identity, () => ({ target: {}, result: {} })) + f.database.exec('DROP TRIGGER command_receipts_no_update; UPDATE command_receipts SET decided_at = \'invalid\'') + const isolation = guardStorageDatabase(f.database) + expect(() => operation === 'find' ? committer.find(f.identity) : committer.accept(f.identity, () => ({ target: {}, result: {} }))).toThrow() + expect(isolation.failure).not.toBeNull() + expect(() => f.database.prepare('SELECT 1')).toThrow('Storage is isolated') + } + finally { + f.database.close() + } + } + }) +}) diff --git a/apps/buddy/service/src/storage/approvalRepository.ts b/apps/buddy/service/src/storage/approvalRepository.ts index 03198a497..503c34419 100644 --- a/apps/buddy/service/src/storage/approvalRepository.ts +++ b/apps/buddy/service/src/storage/approvalRepository.ts @@ -1,4 +1,6 @@ import type { DatabaseSync } from 'node:sqlite' +import type { ApprovalReview } from '../approvals/approvalReview' +import { approvalReviewSchema } from '../approvals/approvalReview' export type ApprovalStatus = 'pending' | 'approved' | 'denied' | 'cancelled' @@ -12,6 +14,10 @@ export interface ApprovalRecord { payload: unknown createdAt: string resolvedAt: string | null + review?: ApprovalReview + resolution?: string | null + decisionActorId?: string | null + cancellationReason?: string | null } export interface ApprovalRepository { @@ -34,6 +40,19 @@ interface ApprovalRow { } export function createApprovalRepository(database: DatabaseSync): ApprovalRepository { + const hasReviews = Boolean(database.prepare('SELECT 1 FROM sqlite_master WHERE type = \'table\' AND name = \'approval_reviews\'').get()) + const withReview = (row: ApprovalRow): ApprovalRecord => { + const approval = toApproval(row) + if (!hasReviews) + return approval + const review = database.prepare('SELECT review_json FROM approval_reviews WHERE approval_id = ?').get(row.id) + const decision = database.prepare('SELECT * FROM approval_decisions WHERE approval_id = ?').get(row.id) + return { + ...approval, + ...(review ? { review: approvalReviewSchema.parse(JSON.parse(String(review.review_json))) } : {}), + ...(decision ? { resolution: decision.resolution as string | null, decisionActorId: decision.actor_id as string | null, cancellationReason: decision.cancellation_reason as string | null } : {}), + } + } const pending = database.prepare('SELECT 1 FROM approvals WHERE run_id = ? AND status = \'pending\' LIMIT 1') const find = database.prepare('SELECT * FROM approvals WHERE id = ?') const listAllPending = database.prepare(` @@ -64,11 +83,11 @@ export function createApprovalRepository(database: DatabaseSync): ApprovalReposi hasPending: runId => pending.get(runId) !== undefined, findById(id) { const row = find.get(id) as ApprovalRow | undefined - return row ? toApproval(row) : null + return row ? withReview(row) : null }, listPending(runId) { const rows = runId ? listRunPending.all(runId) : listAllPending.all() - return (rows as unknown as ApprovalRow[]).map(toApproval) + return (rows as unknown as ApprovalRow[]).map(withReview) }, list(options = {}) { const runId = options.runId ?? null @@ -82,7 +101,7 @@ export function createApprovalRepository(database: DatabaseSync): ApprovalReposi options.conversationId ?? null, options.branchId ?? null, options.limit ?? 100, - ) as unknown as ApprovalRow[]).map(toApproval) + ) as unknown as ApprovalRow[]).map(withReview) }, } } diff --git a/apps/buddy/service/src/storage/attachmentPersistenceRepository.ts b/apps/buddy/service/src/storage/attachmentPersistenceRepository.ts new file mode 100644 index 000000000..dc594d1f7 --- /dev/null +++ b/apps/buddy/service/src/storage/attachmentPersistenceRepository.ts @@ -0,0 +1,191 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { FileStorage } from '../../../platform/filesystem/fileStorage' +import type { AttachmentRecord } from './attachmentRepository' +import { randomUUID } from 'node:crypto' +import { isAbsolute, join } from 'node:path' +import { getStorageIsolation } from './StorageIsolation' +import { withTransaction } from './transaction' + +export interface ManagedFileRecord { + id: string + path: string + temporaryPath: string | null + sizeBytes: number + sha256: string | null + state: 'preparing' | 'ready' | 'unavailable' | 'deleting' | 'deleted' + durability: FileStorage['durability'] | 'legacy-verified' | null +} +interface FileRow { id: string, path: string, temporary_path: string | null, size_bytes: number, sha256: string | null, state: ManagedFileRecord['state'], durability: ManagedFileRecord['durability'] } +export interface FilePreparationInput { + operationId: string + actorId?: string + availableBytes: number + stagingDirectory: string + ownerInstance: string + kind: 'import' | 'message' | 'copy' | 'export' + targetOwnerId: string + path: string + sizeBytes: number + source?: AttachmentRecord +} +export type AttachmentPersistenceRepository = ReturnType + +export function createAttachmentPersistenceRepository(database: DatabaseSync) { + const isolation = getStorageIsolation(database) + const find = database.prepare('SELECT * FROM managed_files WHERE path = ?') + const protection = `NOT EXISTS (SELECT 1 FROM file_references WHERE file_id = managed_files.id) + AND NOT EXISTS (SELECT 1 FROM file_preparations WHERE state IN ('preparing', 'published') + AND (id = managed_files.id OR source_file_id = managed_files.id))` + const get = (path: string): ManagedFileRecord | null => { + isolation.assertAvailable() + const row = find.get(path) as FileRow | undefined + return row ? { id: row.id, path: row.path, temporaryPath: row.temporary_path, sizeBytes: row.size_bytes, sha256: row.sha256, state: row.state, durability: row.durability } : null + } + return { + get, + prepare(input: FilePreparationInput): ManagedFileRecord { + if (!isAbsolute(input.path) || !isAbsolute(input.stagingDirectory) || !Number.isSafeInteger(input.sizeBytes) || input.sizeBytes < 0) + throw new Error('FILE_PREPARATION_INVALID') + return withTransaction(database, () => { + const existing = get(input.path) + if (existing) { + const preparation = database.prepare('SELECT * FROM file_preparations WHERE id = ?').get(existing.id) + if (preparation?.operation_id === input.operationId && preparation.owner_instance === input.ownerInstance + && preparation.kind === input.kind && preparation.target_owner_id === input.targetOwnerId + && preparation.source_attachment_id === (input.source?.id ?? null) + && preparation.source_draft_id === (input.source?.draftId ?? null) + && preparation.source_message_id === (input.source?.messageId ?? null) + && (preparation.state === 'preparing' || preparation.state === 'published') && existing.sizeBytes === input.sizeBytes) { + return existing + } + throw new Error('FILE_PREPARATION_CONFLICT') + } + const reserved = database.prepare('SELECT COALESCE(SUM(reserved_bytes), 0) AS bytes FROM file_preparations WHERE state IN (\'preparing\', \'published\')').get() as { bytes: number } + if (!Number.isSafeInteger(input.availableBytes) || input.availableBytes < input.sizeBytes + reserved.bytes) + throw new Error('FILE_STORAGE_CAPACITY') + const profile = database.prepare('SELECT data_generation, local_actor_id FROM storage_profile WHERE id = 1').get() as { data_generation: string, local_actor_id: string } + const source = input.source ? get(input.source.storedPath) : null + if (input.source && (!source || source.state !== 'ready' || source.sizeBytes !== input.source.sizeBytes)) + throw new Error('FILE_SOURCE_UNAVAILABLE') + const id = randomUUID() + const now = new Date().toISOString() + database.prepare(`INSERT INTO managed_files (id, path, temporary_path, size_bytes, state, created_at, updated_at) + VALUES (?, ?, ?, ?, 'preparing', ?, ?)`).run(id, input.path, join(input.stagingDirectory, `${id}.part`), input.sizeBytes, now, now) + database.prepare(`INSERT INTO file_preparations + (id, operation_id, owner_instance, actor_id, data_generation, kind, target_owner_id, source_file_id, + source_attachment_id, source_draft_id, source_message_id, state, reserved_bytes, created_at, updated_at) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'preparing', ?, ?, ?)`).run( + id, + input.operationId, + input.ownerInstance, + input.actorId ?? profile.local_actor_id, + profile.data_generation, + input.kind, + input.targetOwnerId, + source?.id ?? null, + input.source?.id ?? null, + input.source?.draftId ?? null, + input.source?.messageId ?? null, + input.sizeBytes, + now, + now, + ) + return get(input.path)! + }) + }, + publish(path: string, proof: { sizeBytes: number, sha256: string, durability: FileStorage['durability'] }): void { + withTransaction(database, () => { + const file = get(path) + if (!file || file.state !== 'preparing' || file.sizeBytes !== proof.sizeBytes || !/^[a-f0-9]{64}$/.test(proof.sha256)) + throw new Error('FILE_PUBLICATION_INVALID') + const now = new Date().toISOString() + database.prepare('UPDATE managed_files SET state = \'ready\', sha256 = ?, durability = ?, updated_at = ? WHERE id = ?').run(proof.sha256, proof.durability, now, file.id) + database.prepare('UPDATE file_preparations SET state = \'published\', updated_at = ? WHERE id = ? AND state = \'preparing\'').run(now, file.id) + }) + }, + settle(paths: readonly string[], outcome: 'commit' | 'rollback'): void { + withTransaction(database, () => { + for (const path of paths) { + const file = get(path) + if (!file) + throw new Error('FILE_PREPARATION_MISSING') + const referenced = Boolean(database.prepare('SELECT 1 FROM file_references WHERE file_id = ?').get(file.id)) + if (outcome === 'commit' && !referenced) + throw new Error('ATTACHMENT_OWNERSHIP_NOT_COMMITTED') + database.prepare('UPDATE file_preparations SET state = ?, reserved_bytes = 0, updated_at = ? WHERE id = ? AND state IN (\'preparing\', \'published\')') + .run(referenced ? 'settled' : 'aborted', new Date().toISOString(), file.id) + } + }) + }, + recover(ownerInstance: string): void { + withTransaction(database, () => { + const remote = database.prepare('SELECT 1 FROM sqlite_master WHERE type = \'table\' AND name = \'remote_uploads\'').get() + ? ' AND NOT EXISTS (SELECT 1 FROM remote_uploads u WHERE u.file_id = file_preparations.id AND u.state IN (\'receiving\', \'ready\'))' + : '' + database.prepare(`UPDATE file_preparations SET state = CASE + WHEN EXISTS (SELECT 1 FROM file_references WHERE file_id = file_preparations.id) THEN 'settled' ELSE 'aborted' END, + reserved_bytes = 0, updated_at = ? + WHERE owner_instance <> ? AND state IN ('preparing', 'published')${remote}`).run(new Date().toISOString(), ownerInstance) + }) + }, + hasProtection(path: string): boolean { + isolation.assertAvailable() + return Boolean(database.prepare(`SELECT 1 FROM managed_files WHERE path = ? AND EXISTS + (SELECT 1 FROM file_preparations WHERE state IN ('preparing', 'published') AND (id = managed_files.id OR source_file_id = managed_files.id))`).get(path)) + }, + claim(path: string): ManagedFileRecord | null { + return withTransaction(database, () => { + database.prepare(`UPDATE managed_files SET state = 'deleting', updated_at = ? + WHERE path = ? AND state IN ('preparing', 'ready', 'unavailable') AND ${protection}`).run(new Date().toISOString(), path) + const file = get(path) + return file?.state === 'deleting' ? file : null + }) + }, + candidates(): ManagedFileRecord[] { + isolation.assertAvailable() + return (database.prepare(`SELECT path FROM managed_files WHERE state = 'deleting' + OR (state IN ('preparing', 'ready', 'unavailable') AND ${protection}) ORDER BY created_at, id LIMIT 100`).all() as { path: string }[]).map(row => get(row.path)!) + }, + deleted(id: string): void { + withTransaction(database, () => { + if (Number(database.prepare('UPDATE managed_files SET state = \'deleted\', updated_at = ? WHERE id = ? AND state = \'deleting\'').run(new Date().toISOString(), id).changes) !== 1) + throw new Error('FILE_DELETION_NOT_CLAIMED') + }) + }, + unavailable(path: string): void { + withTransaction(database, () => { + database.prepare('UPDATE managed_files SET state = \'unavailable\', updated_at = ? WHERE path = ? AND state = \'ready\'').run(new Date().toISOString(), path) + }) + }, + adopt(input: { path: string, sizeBytes: number, sha256: string | null }): ManagedFileRecord { + return withTransaction(database, () => { + const existing = get(input.path) + if (existing) { + if (existing.sizeBytes !== input.sizeBytes || existing.sha256 !== input.sha256) + throw new Error('FILE_IMPORT_CONFLICT') + return existing + } + if (!isAbsolute(input.path) || (input.sha256 !== null && !/^[a-f0-9]{64}$/.test(input.sha256))) + throw new Error('FILE_IMPORT_INVALID') + const now = new Date().toISOString() + database.prepare(`INSERT INTO managed_files (id, path, size_bytes, sha256, state, durability, created_at, updated_at) + VALUES (?, ?, ?, ?, ?, 'legacy-verified', ?, ?)`).run(randomUUID(), input.path, input.sizeBytes, input.sha256, input.sha256 ? 'ready' : 'unavailable', now, now) + database.prepare('UPDATE attachments SET stored_path = stored_path WHERE stored_path = ?').run(input.path) + return get(input.path)! + }) + }, + reference(fileId: string, input: { kind: 'export', ownerId: string, bindingId: string }): void { + withTransaction(database, () => { + const existing = database.prepare('SELECT file_id FROM file_references WHERE kind = ? AND owner_id = ? AND binding_id = ?').get(input.kind, input.ownerId, input.bindingId) + if (existing?.file_id === fileId) + return + database.prepare('INSERT INTO file_references (file_id, kind, owner_id, binding_id) VALUES (?, ?, ?, ?)').run(fileId, input.kind, input.ownerId, input.bindingId) + }) + }, + releaseReference(input: { kind: 'export', ownerId: string, bindingId: string }): void { + withTransaction(database, () => { + database.prepare('DELETE FROM file_references WHERE kind = ? AND owner_id = ? AND binding_id = ?').run(input.kind, input.ownerId, input.bindingId) + }) + }, + } +} diff --git a/apps/buddy/service/src/storage/attachmentRepository.ts b/apps/buddy/service/src/storage/attachmentRepository.ts index 209d2bfd5..1eceab029 100644 --- a/apps/buddy/service/src/storage/attachmentRepository.ts +++ b/apps/buddy/service/src/storage/attachmentRepository.ts @@ -1,4 +1,7 @@ import type { DatabaseSync } from 'node:sqlite' +import type { AttachmentPersistenceRepository } from './attachmentPersistenceRepository' +import { createAttachmentPersistenceRepository } from './attachmentPersistenceRepository' +import { withTransaction } from './transaction' export interface AttachmentRecord { nameSource?: 'file' | 'clipboard' @@ -15,6 +18,7 @@ export interface AttachmentRecord { } export interface AttachmentRepository { + persistence?: AttachmentPersistenceRepository create: (record: AttachmentRecord) => AttachmentRecord findById: (id: string) => AttachmentRecord | null findVisibleById: (id: string) => AttachmentRecord | null @@ -75,20 +79,23 @@ export function createAttachmentRepository(database: DatabaseSync): AttachmentRe `) return { + persistence: database.prepare('SELECT 1 FROM sqlite_master WHERE type = \'table\' AND name = \'managed_files\'').get() ? createAttachmentPersistenceRepository(database) : undefined, create(record) { - insert.run( - record.id, - record.draftId, - record.messageId, - record.storedPath, - record.name, - record.mimeType, - record.sizeBytes, - record.createdAt, - record.nameSource ?? 'file', - record.sourcePath ?? null, - ) - return requireAttachment(find.get(record.id), record.id) + return withTransaction(database, () => { + insert.run( + record.id, + record.draftId, + record.messageId, + record.storedPath, + record.name, + record.mimeType, + record.sizeBytes, + record.createdAt, + record.nameSource ?? 'file', + record.sourcePath ?? null, + ) + return requireAttachment(find.get(record.id), record.id) + }) }, findById(id) { const row = find.get(id) as AttachmentRow | undefined @@ -110,10 +117,10 @@ export function createAttachmentRepository(database: DatabaseSync): AttachmentRe .map(toAttachment) }, removeDraft(id) { - return Number(removeDraft.run(id).changes) === 1 + return withTransaction(database, () => Number(removeDraft.run(id).changes) === 1) }, removeDrafts(ids) { - return ids.reduce((count, id) => count + Number(removeDraft.run(id).changes), 0) + return withTransaction(database, () => ids.reduce((count, id) => count + Number(removeDraft.run(id).changes), 0)) }, } } diff --git a/apps/buddy/service/src/storage/backupDatabase.ts b/apps/buddy/service/src/storage/backupDatabase.ts index 3123dd40c..15652110a 100644 --- a/apps/buddy/service/src/storage/backupDatabase.ts +++ b/apps/buddy/service/src/storage/backupDatabase.ts @@ -1,7 +1,7 @@ import type { DatabaseSync, SQLInputValue } from 'node:sqlite' import type { BackupScope } from '../../../shared/storage/backupScope' import { storageError } from '../../../platform/storage/storageFiles' -import { backupCapabilityOwners, backupIdentityTables, backupMergeColumns, backupTables, backupTableScopes, databaseRowScope } from './backupDatabasePolicy' +import { backupCapabilityOwners, backupMergeColumns, backupTables, backupTableScopes, databaseRowScope, persistenceIdentityTables } from './backupDatabasePolicy' import { reconcileRestoredTaskState } from './restoreTaskState' const quote = (name: string) => `"${name.replaceAll('"', '""')}"` @@ -9,6 +9,7 @@ function columns(database: DatabaseSync, table: string) { return database.prepare(`PRAGMA table_info(${quote(table)})`).all() as { name: string, pk: number }[] } export function selectBackupDatabase(database: DatabaseSync, scopes: readonly BackupScope[], paths: readonly string[] = []): void { + assertIsolatedPersistenceConstruction(database) const tables = database.prepare('SELECT name FROM sqlite_master WHERE type = \'table\' AND name NOT LIKE \'sqlite_%\'').all().map(row => String(row.name)) database.exec('PRAGMA foreign_keys = OFF; BEGIN IMMEDIATE') try { @@ -22,7 +23,7 @@ export function selectBackupDatabase(database: DatabaseSync, scopes: readonly Ba insert.run(row.__id!) } } - else if (backupIdentityTables.has(table) || scopes.includes(backupTableScopes.get(table)!)) { + else if (persistenceIdentityTables.has(table) || scopes.includes(backupTableScopes.get(table)!)) { database.exec(`INSERT INTO temp.${quote(`keep_${table}`)} SELECT rowid FROM ${quote(table)}`) } } @@ -105,13 +106,30 @@ function restoredCapabilityIds(target: DatabaseSync, source: DatabaseSync, scope export function mergeBackupDatabase(target: DatabaseSync, source: DatabaseSync, scopes: readonly BackupScope[]): { added: number, updated: number, dependencies: number } { const counts = { added: 0, updated: 0, dependencies: 0 } const restoredProviders = new Set() + assertIsolatedPersistenceConstruction(target) target.exec('PRAGMA foreign_keys = OFF; BEGIN IMMEDIATE') try { const capabilities = restoredCapabilityIds(target, source, scopes) - const triggers = target.prepare('SELECT name, sql FROM sqlite_master WHERE type = \'trigger\' AND (sql LIKE \'%AFTER %\' OR name IN (\'runs_require_active_conversation\', \'runs_retry_requires_active_conversation\'))').all() + const persistence = Boolean(target.prepare('SELECT 1 FROM sqlite_master WHERE name = \'storage_profile\'').get()) + const newerRuns = new Set() + if (persistence) { + mergePersistenceIdentity(target, source) + for (const run of source.prepare('SELECT id, next_event_sequence FROM runs').iterate()) { + const current = target.prepare('SELECT next_event_sequence FROM runs WHERE id = ?').get(run.id!) + if (current && Number(current.next_event_sequence) > Number(run.next_event_sequence)) + newerRuns.add(String(run.id)) + } + } + const triggers = target.prepare('SELECT name, sql FROM sqlite_master WHERE type = \'trigger\' AND sql LIKE \'%AFTER %\'').all() for (const trigger of triggers) target.exec(`DROP TRIGGER ${quote(String(trigger.name))}`) + const targetTables = new Set(target.prepare('SELECT name FROM sqlite_master WHERE type = \'table\'').all().map(row => String(row.name))) + const sourceTables = new Set(source.prepare('SELECT name FROM sqlite_master WHERE type = \'table\'').all().map(row => String(row.name))) for (const table of backupTables) { + if (persistence && ['persistence_migrations', 'persistence_migration_entries'].includes(table)) + continue + if (!targetTables.has(table) || !sourceTables.has(table)) + continue const merge = backupMergeColumns(table, columns(target, table)) if (!merge) continue @@ -131,6 +149,19 @@ export function mergeBackupDatabase(target: DatabaseSync, source: DatabaseSync, const selected = scopes.includes(scope) const key = keys.map(name => row[name] as SQLInputValue) const existing = find.get(...key) + if (existing && persistence && ((table === 'runs' && newerRuns.has(String(row.id))) || (typeof row.run_id === 'string' && newerRuns.has(row.run_id) && ['messages', 'approvals', 'usage_records', 'execution_state'].includes(table)))) + continue + if (existing && persistence && ['run_events', 'approval_reviews', 'product_content_references', 'approval_decisions', 'legacy_request_aliases', 'execution_mailbox_sources', 'execution_mailbox_receipts'].includes(table)) { + const current = target.prepare(`SELECT * FROM ${quote(table)} WHERE ${where}`).get(...key)! + if (names.some(name => current[name] !== row[name])) + throw storageError('SNAPSHOT_DEPENDENCY_CONFLICT') + continue + } + if (existing && persistence && table === 'managed_files') { + const current = target.prepare(`SELECT * FROM managed_files WHERE ${where}`).get(...key)! + if (['id', 'path', 'size_bytes', 'sha256'].some(name => current[name] !== row[name])) + throw storageError('SNAPSHOT_DEPENDENCY_CONFLICT') + } if (existing && !selected && table !== 'extension_capabilities') continue if (scope === 'providers') { @@ -158,6 +189,9 @@ export function mergeBackupDatabase(target: DatabaseSync, source: DatabaseSync, for (const providerId of restoredProviders) invalidateProvider.run(providerId) reconcileRestoredTaskState(target, source) + if (persistence) { + target.exec(`UPDATE runs SET next_event_sequence = MAX(COALESCE(next_event_sequence, 1), COALESCE((SELECT MAX(sequence) + 1 FROM run_events WHERE run_id = runs.id), 1))`) + } if (target.prepare('PRAGMA foreign_key_check').all().length) throw storageError('SNAPSHOT_DEPENDENCY_CONFLICT') target.exec('COMMIT') @@ -169,3 +203,28 @@ export function mergeBackupDatabase(target: DatabaseSync, source: DatabaseSync, } finally { target.exec('PRAGMA foreign_keys = ON') } } + +function assertIsolatedPersistenceConstruction(database: DatabaseSync): void { + if (database.prepare('SELECT 1 FROM sqlite_master WHERE type = \'trigger\' AND name = \'command_receipts_no_delete\'').get()) + throw storageError('SNAPSHOT_ISOLATED_CONSTRUCTION_REQUIRED') +} + +function mergePersistenceIdentity(target: DatabaseSync, source: DatabaseSync): void { + for (const actor of source.prepare('SELECT * FROM storage_actors').iterate()) { + if (!target.prepare('SELECT 1 FROM storage_actors WHERE id = ?').get(actor.id!)) { + target.prepare('INSERT INTO storage_actors (id, kind, state, created_at) VALUES (?, \'device\', \'revoked\', ?)').run(actor.id!, actor.created_at!) + } + } + const names = columns(target, 'command_receipts').map(column => column.name) + const insert = target.prepare(`INSERT INTO command_receipts (${names.map(quote).join(',')}) VALUES (${names.map(() => '?').join(',')})`) + for (const row of source.prepare('SELECT * FROM command_receipts').iterate()) { + const existing = target.prepare('SELECT * FROM command_receipts WHERE data_generation = ? AND actor_id = ? AND command_id = ?').get(row.data_generation!, row.actor_id!, row.command_id!) + if (existing) { + if (names.some(name => existing[name] !== row[name])) + throw storageError('SNAPSHOT_DEPENDENCY_CONFLICT') + } + else { + insert.run(...names.map(name => row[name] as SQLInputValue)) + } + } +} diff --git a/apps/buddy/service/src/storage/backupDatabasePolicy.ts b/apps/buddy/service/src/storage/backupDatabasePolicy.ts index acdbe1937..58f5251f2 100644 --- a/apps/buddy/service/src/storage/backupDatabasePolicy.ts +++ b/apps/buddy/service/src/storage/backupDatabasePolicy.ts @@ -8,10 +8,10 @@ const domains: Partial> = { connections: ['mcp_servers'], skills: ['skill_installations', 'skill_space_exclusions'], automations: ['automations'], - tasks: ['conversation_archive_policy', 'conversation_deletion_jobs', 'user_questions', 'conversation_branches', 'messages', 'run_events', 'turn_requests', 'run_inputs', 'command_requests', 'approvals', 'attachments', 'space_events', 'automation_occurrences', 'automation_mutation_requests', 'run_change_sets', 'space_directory_bindings', 'spaces', 'conversations', 'runs', 'conversation_directory_grants', 'artifacts', 'run_file_change_captures', 'composer_resources', 'conversation_pi_trees', 'run_tree_sources', 'task_marks', 'task_attention', 'composer_drafts', 'extension_invocations', 'usage_records', 'conversation_members', 'execution_threads', 'execution_thread_bindings', 'execution_state', 'execution_work_attempts', 'execution_mailbox_sources', 'execution_mailbox_receipts', 'run_change_invocations', 'conversation_public_heads', 'conversation_publications', 'run_public_snapshots', 'run_session_reference_scopes', 'session_reference_exclusions', 'actor_public_cursors'], + tasks: ['conversation_archive_policy', 'conversation_deletion_jobs', 'product_content_references', 'product_content_reference_owners', 'approval_reviews', 'approval_decisions', 'composer_draft_slots', 'chat_queue', 'chat_queue_deliveries', 'managed_files', 'file_preparations', 'file_references', 'legacy_request_aliases', 'persistence_migrations', 'persistence_migration_entries', 'user_questions', 'conversation_branches', 'messages', 'run_events', 'turn_requests', 'run_inputs', 'command_requests', 'approvals', 'attachments', 'space_events', 'automation_occurrences', 'automation_mutation_requests', 'run_change_sets', 'space_directory_bindings', 'spaces', 'conversations', 'runs', 'conversation_directory_grants', 'artifacts', 'run_file_change_captures', 'composer_resources', 'conversation_pi_trees', 'run_tree_sources', 'task_marks', 'task_attention', 'composer_drafts', 'extension_invocations', 'usage_records', 'conversation_members', 'execution_threads', 'execution_thread_bindings', 'execution_state', 'execution_work_attempts', 'execution_mailbox_sources', 'execution_mailbox_receipts', 'run_change_invocations', 'conversation_public_heads', 'conversation_publications', 'run_public_snapshots', 'run_session_reference_scopes', 'session_reference_exclusions', 'actor_public_cursors'], } export const backupTableScopes = new Map(Object.entries(domains).flatMap(([scope, tables]) => tables.map(table => [table, scope as BackupScope] as const))) -export const backupIdentityTables = new Set(['buddy_schema_lineage']) +export const persistenceIdentityTables = new Set(['product_query_revisions', 'buddy_schema_lineage', 'storage_profile', 'storage_actors', 'command_receipts', 'private_network_state', 'pairing_tickets', 'pairing_attempts', 'remote_devices', 'remote_credentials', 'remote_uploads', 'remote_upload_chunks', 'remote_exports']) export const backupTables = [...backupTableScopes.keys(), 'workspace_settings', 'extension_capabilities'] export const backupCapabilityOwners = [['subagent', 'buddies'], ['mcp', 'mcp_servers']] as const diff --git a/apps/buddy/service/src/storage/chatQueueRepository.ts b/apps/buddy/service/src/storage/chatQueueRepository.ts index d531f50d1..fcc27a840 100644 --- a/apps/buddy/service/src/storage/chatQueueRepository.ts +++ b/apps/buddy/service/src/storage/chatQueueRepository.ts @@ -64,10 +64,10 @@ export function createChatQueueRepository(database: DatabaseSync) { } const receipt = (row: QueueRow): LocalChatQueueReceipt => { const input = decode(row) - return { id: row.id, conversationId: row.conversation_id, branchId: row.branch_id, draftReceipt: { draftId: input.draft.draftId, sourceRevision: input.draft.expectedRevision, committedRevision: input.draft.expectedRevision + 1 } } + return { id: row.id, conversationId: row.conversation_id, branchId: row.branch_id, draftReceipt: input.draft ? { draftId: input.draft.draftId, sourceRevision: input.draft.expectedRevision, committedRevision: input.draft.expectedRevision + 1 } : null } } const assertScope = (scope: LocalChatQueueScope) => { - if (!database.prepare('SELECT id FROM conversations WHERE id = ? AND active_branch_id = ? AND deleted_at IS NULL').get(scope.conversationId, scope.branchId)) + if (!database.prepare('SELECT c.id 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(scope.conversationId, scope.branchId)) throw new BuddyServiceError('VALIDATION_FAILED') } return { @@ -92,9 +92,9 @@ export function createChatQueueRepository(database: DatabaseSync) { throw new BuddyServiceError('VALIDATION_FAILED') for (const binding of input.attachmentBindings) { const source = attachments.findById(binding.sourceAttachmentId) - if (!source || (binding.sourceDraftId ? source.draftId !== input.draft.draftId : source.conversationId !== input.conversationId)) + if (!source || (binding.sourceDraftId ? source.draftId !== binding.sourceDraftId : source.conversationId !== input.conversationId)) throw new TurnRequestAttachmentError() - if (binding.sourceDraftId) { + if (binding.sourceDraftId && binding.id === source.id) { database.prepare('UPDATE attachments SET draft_id = ?, stored_path = ? WHERE id = ?').run(id, binding.storedPath, source.id) } else { @@ -109,7 +109,8 @@ export function createChatQueueRepository(database: DatabaseSync) { queuedMessageId: id, attachmentBindings: input.attachmentBindings.map(binding => ({ ...binding, sourceDraftId: id, sourceAttachmentId: binding.id })), } - commitDraft({ ...input, draftId: input.draft.draftId, expectedRevision: input.draft.expectedRevision, updatedAt: input.createdAt }) + if (input.draft) + commitDraft({ ...input, draftId: input.draft.draftId, expectedRevision: input.draft.expectedRevision, updatedAt: input.createdAt }) database.prepare(`INSERT INTO chat_queue (id, conversation_id, branch_id, request_id, request_fingerprint, prepared_json, state, created_at) VALUES (?, ?, ?, ?, ?, ?, 'waiting', ?)`).run(id, input.conversationId, input.branchId, input.requestId, input.requestFingerprint, JSON.stringify(prepared), input.createdAt) return receipt(find(id)!) @@ -133,23 +134,21 @@ export function createChatQueueRepository(database: DatabaseSync) { }, hasWaiting(): boolean { return Boolean(database.prepare(`SELECT 1 FROM chat_queue q JOIN conversations c ON c.id = q.conversation_id - WHERE q.state = 'waiting' AND c.deleted_at IS NULL AND c.active_branch_id = q.branch_id LIMIT 1`).get()) + WHERE q.state = 'waiting' AND c.deleted_at IS NULL LIMIT 1`).get()) }, continuationScopes(): LocalChatQueueScope[] { return database.prepare(`SELECT DISTINCT q.conversation_id AS conversationId, q.branch_id AS branchId FROM chat_queue q JOIN conversations c ON c.id = q.conversation_id - WHERE q.state IN ('waiting', 'paused') AND c.deleted_at IS NULL AND c.active_branch_id = q.branch_id`).all() as unknown as LocalChatQueueScope[] + WHERE q.state IN ('waiting', 'paused') AND c.deleted_at IS NULL`).all() as unknown as LocalChatQueueScope[] }, - continuationScope(conversationId: string): LocalChatQueueScope | null { - return database.prepare(`SELECT id AS conversationId, active_branch_id AS branchId FROM conversations c - WHERE id = ? AND deleted_at IS NULL AND EXISTS ( - SELECT 1 FROM chat_queue q WHERE q.conversation_id = c.id AND q.branch_id = c.active_branch_id AND q.state IN ('waiting', 'paused') - )`).get(conversationId) as LocalChatQueueScope | undefined ?? null + continuationScope(scope: LocalChatQueueScope): LocalChatQueueScope | null { + return database.prepare(`SELECT q.conversation_id AS conversationId, q.branch_id AS branchId FROM chat_queue q JOIN conversations c ON c.id = q.conversation_id + WHERE q.conversation_id = ? AND q.branch_id = ? AND c.deleted_at IS NULL AND q.state IN ('waiting', 'paused') LIMIT 1`).get(scope.conversationId, scope.branchId) as LocalChatQueueScope | undefined ?? null }, latestRun(scope: LocalChatQueueScope, actorId?: string) { - return database.prepare(`SELECT id, status FROM runs WHERE conversation_id = ? + return database.prepare(`SELECT id, status FROM runs WHERE conversation_id = ? AND branch_id = ? AND ((? IS NOT NULL AND buddy_id = ?) OR (? IS NULL AND scheduling_lane = 'conversation' AND execution_kind IN ('root', 'maintenance'))) - ORDER BY rowid DESC LIMIT 1`).get(scope.conversationId, actorId ?? null, actorId ?? null, actorId ?? null) as { id: string, status: string } | undefined + ORDER BY rowid DESC LIMIT 1`).get(scope.conversationId, scope.branchId, actorId ?? null, actorId ?? null, actorId ?? null) as { id: string, status: string } | undefined }, pending(target: LocalChatQueueTarget) { assertScope(target) @@ -175,9 +174,9 @@ export function createChatQueueRepository(database: DatabaseSync) { return Number(database.prepare('UPDATE chat_queue SET state = \'cancelled\' WHERE id = ? AND state IN (\'waiting\', \'paused\')').run(target.id).changes) === 1 }, activeRun(scope: LocalChatQueueScope, actorId?: string) { - return database.prepare(`SELECT id, purpose, model, provider FROM runs WHERE conversation_id = ? + return database.prepare(`SELECT id, purpose, model, provider FROM runs WHERE conversation_id = ? AND branch_id = ? AND ((? IS NOT NULL AND buddy_id = ?) OR (? IS NULL AND scheduling_lane = 'conversation' AND execution_kind IN ('root', 'maintenance'))) - AND status IN ('queued', 'running') ORDER BY started_at DESC LIMIT 1`).get(scope.conversationId, actorId ?? null, actorId ?? null, actorId ?? null) as { id: string, purpose: string, model: string, provider: string } | undefined + AND status IN ('queued', 'running') ORDER BY started_at DESC LIMIT 1`).get(scope.conversationId, scope.branchId, actorId ?? null, actorId ?? null, actorId ?? null) as { id: string, purpose: string, model: string, provider: string } | undefined }, pauseActor(scope: LocalChatQueueScope, actorId: string) { return withTransaction(database, () => { @@ -189,7 +188,9 @@ export function createChatQueueRepository(database: DatabaseSync) { return changed }) }, - pause(conversationId?: string) { + pause(conversationId?: string, branchId?: string) { + if (conversationId && branchId) + return Number(database.prepare('UPDATE chat_queue SET state = \'paused\' WHERE conversation_id = ? AND branch_id = ? AND state = \'waiting\'').run(conversationId, branchId).changes) if (conversationId) return Number(database.prepare('UPDATE chat_queue SET state = \'paused\' WHERE conversation_id = ? AND state = \'waiting\'').run(conversationId).changes) return Number(database.prepare('UPDATE chat_queue SET state = \'paused\' WHERE state = \'waiting\'').run().changes) diff --git a/apps/buddy/service/src/storage/checkpointSqliteStorage.ts b/apps/buddy/service/src/storage/checkpointSqliteStorage.ts new file mode 100644 index 000000000..655370944 --- /dev/null +++ b/apps/buddy/service/src/storage/checkpointSqliteStorage.ts @@ -0,0 +1,27 @@ +import type { DatabaseSync } from 'node:sqlite' +import { requireSqliteAuthority } from './sqliteAuthority' +import { getStorageIsolation } from './StorageIsolation' +import { assertCommittedRead } from './transaction' + +export interface SqliteCheckpointResult { + busy: number + log: number + checkpointed: number +} + +export function checkpointSqliteStorage(database: DatabaseSync): SqliteCheckpointResult { + assertCommittedRead(database) + requireSqliteAuthority(database) + const isolation = getStorageIsolation(database) + const timeout = database.prepare('PRAGMA busy_timeout').get()!.timeout as number + try { + database.exec('PRAGMA busy_timeout = 0') + const result = database.prepare('PRAGMA main.wal_checkpoint(TRUNCATE)').get() as unknown as SqliteCheckpointResult + database.exec(`PRAGMA busy_timeout = ${timeout}`) + return result + } + catch (cause) { + isolation.isolate(cause) + throw cause + } +} diff --git a/apps/buddy/service/src/storage/commandRequestRepository.ts b/apps/buddy/service/src/storage/commandRequestRepository.ts index bc5b4d72c..b7d6d2e57 100644 --- a/apps/buddy/service/src/storage/commandRequestRepository.ts +++ b/apps/buddy/service/src/storage/commandRequestRepository.ts @@ -4,6 +4,7 @@ import type { BuddyExecutionProfile } from '../../../shared/permissions/executio import type { ComposerDraftCommitReceipt } from './commitComposerDraft' import { createComposerDraftCommitter } from './commitComposerDraft' import { withTransaction } from './database' +import { createThreadBindingRepository } from './threadBindingRepository' export type BuddyActionCommandName = 'compact' @@ -17,7 +18,7 @@ export interface PrepareCommandRequestInput { draft: { draftId: string expectedRevision: number - } + } | null executionProfile: BuddyExecutionProfile requestFingerprint: string requestId: string @@ -30,7 +31,7 @@ export interface CommandRequestRecord { command: BuddyActionCommandName conversationId: string created: boolean - draftReceipt: ComposerDraftCommitReceipt + draftReceipt: ComposerDraftCommitReceipt | null requestFingerprint: string requestId: string runId: string @@ -139,7 +140,7 @@ export function createCommandRequestRepository(database: DatabaseSync): CommandR const conversation = findConversation.get(input.conversationId) as ConversationRow | undefined if ( !conversation - || conversation.active_branch_id !== input.branchId + || !database.prepare('SELECT 1 FROM conversation_branches WHERE conversation_id = ? AND id = ?').get(input.conversationId, input.branchId) || conversation.approval_policy !== input.approvalPolicy || conversation.execution_profile !== input.executionProfile || conversation.deleted_at !== null @@ -162,6 +163,8 @@ export function createCommandRequestRepository(database: DatabaseSync): CommandR input.approvalPolicy, input.executionProfile, ) + const thread = createThreadBindingRepository(database).ensureActor(input.conversationId, source.buddy_id, input.createdAt, input.branchId) + database.prepare('UPDATE runs SET thread_id = ? WHERE id = ?').run(thread.id, input.runId) insertRequest.run( input.requestId, input.requestFingerprint, @@ -171,20 +174,22 @@ export function createCommandRequestRepository(database: DatabaseSync): CommandR input.command, input.arguments, input.createdAt, - input.draft.draftId, - input.draft.expectedRevision, - input.draft.expectedRevision + 1, + input.draft?.draftId ?? null, + input.draft?.expectedRevision ?? null, + input.draft ? input.draft.expectedRevision + 1 : null, ) - const draftReceipt = commitDraft({ - approvalPolicy: input.approvalPolicy, - branchId: input.branchId, - conversationId: input.conversationId, - draftId: input.draft.draftId, - executionProfile: input.executionProfile, - expectedRevision: input.draft.expectedRevision, - spaceId: null, - updatedAt: input.createdAt, - }) + const draftReceipt = input.draft + ? commitDraft({ + approvalPolicy: input.approvalPolicy, + branchId: input.branchId, + conversationId: input.conversationId, + draftId: input.draft.draftId, + executionProfile: input.executionProfile, + expectedRevision: input.draft.expectedRevision, + spaceId: null, + updatedAt: input.createdAt, + }) + : null return { ...input, created: true, @@ -232,24 +237,19 @@ function insertCompactionRun( } function toRecord(row: CommandRequestRow, created: boolean): CommandRequestRecord { - if ( - row.draft_id === null - || row.draft_revision === null - || row.committed_draft_revision === null - ) { - throw new CommandRequestConflictError() - } return { arguments: row.arguments, branchId: row.branch_id, command: row.command, conversationId: row.conversation_id, created, - draftReceipt: { - committedRevision: row.committed_draft_revision, - draftId: row.draft_id, - sourceRevision: row.draft_revision, - }, + draftReceipt: row.draft_id !== null && row.draft_revision !== null && row.committed_draft_revision !== null + ? { + committedRevision: row.committed_draft_revision, + draftId: row.draft_id, + sourceRevision: row.draft_revision, + } + : null, requestFingerprint: row.request_fingerprint, requestId: row.request_id, runId: row.run_id, diff --git a/apps/buddy/service/src/storage/commitBackupRestore.ts b/apps/buddy/service/src/storage/commitBackupRestore.ts index bac83c81a..0cd496bf3 100644 --- a/apps/buddy/service/src/storage/commitBackupRestore.ts +++ b/apps/buddy/service/src/storage/commitBackupRestore.ts @@ -4,11 +4,13 @@ import { dirname, join } from 'node:path' import { z } from 'zod' import { isMissing, readStorageJson, storageError, syncStorageDirectory, writeStorageJson } from '../../../platform/storage/storageFiles' import { storageOperationStage } from '../../../platform/storage/storageOperationFiles' +import { persistenceUpgradePaths } from './persistenceMigration/persistenceSources' const paths = ['buddy', 'config.toml', 'device/sync.json'] as const -const journalSchema = z.object({ id: z.uuid(), entries: z.array(z.object({ path: z.enum(paths), original: z.boolean() }).strict()).min(1).max(3) }).strict() +const entryPath = z.enum([...paths, ...persistenceUpgradePaths]) +const journalSchema = z.object({ id: z.uuid(), entries: z.array(z.object({ path: entryPath, original: z.boolean(), replacement: z.boolean().default(true) }).strict()).min(1) }).strict() export const restoreStage = storageOperationStage -export const restoreRetained = (operation: StorageOperation) => join(operation.target, 'recovery', `restore-${operation.id}`) +export const restoreRetained = (operation: StorageOperation) => join(operation.target, 'recovery', `${operation.kind}-${operation.id}`) function exists(path: string) { return lstat(path).catch((error) => { if (isMissing(error)) @@ -19,22 +21,44 @@ function exists(path: string) { export async function prepareRestoreCommit(operation: StorageOperation): Promise { const stage = restoreStage(operation) const entries = [] - for (const path of paths) { - if (await exists(join(stage, 'profile', path))) - entries.push({ path, original: !!await exists(join(operation.target, path)) }) + const selected = operation.kind === 'upgrade' ? persistenceUpgradePaths : paths + for (const path of selected) { + const replacement = !!await exists(join(stage, 'profile', path)) + const original = !!await exists(join(operation.target, path)) + if (replacement || (operation.kind === 'upgrade' && original && /^buddy\/buddy\.sqlite3-(?:wal|shm|journal)$/.test(path))) + entries.push({ path, original, replacement }) } await writeStorageJson(join(stage, 'commit.json'), { id: operation.id, entries }) } export async function commitBackupRestore(operation: StorageOperation): Promise { const stage = restoreStage(operation) const journal = journalSchema.parse(await readStorageJson(join(stage, 'commit.json'))) - if (journal.id !== operation.id || new Set(journal.entries.map(item => item.path)).size !== journal.entries.length) + if (journal.id !== operation.id || new Set(journal.entries.map(item => item.path)).size !== journal.entries.length + || (journal.entries.some(item => item.path === 'buddy') && journal.entries.some(item => item.path.startsWith('buddy/')))) { throw storageError('STORAGE_STAGE_NOT_OWNED') + } + for (const entry of journal.entries) { + if ((entry.path.startsWith('buddy/') && operation.kind !== 'upgrade') + || (!entry.replacement && (operation.kind !== 'upgrade' || !/^buddy\/buddy\.sqlite3-(?:wal|shm|journal)$/.test(entry.path)))) { + throw storageError('STORAGE_STAGE_NOT_OWNED') + } + } for (const entry of journal.entries) { const candidate = join(stage, 'profile', entry.path) const current = join(operation.target, entry.path) const retained = join(restoreRetained(operation), entry.path) const [next, previous, saved] = await Promise.all([exists(candidate), exists(current), exists(retained)]) + if (!entry.replacement) { + if (next || !entry.original || (previous && saved) || (!previous && !saved)) + throw storageError('STORAGE_RESTORE_CONFLICT') + if (previous) { + await mkdir(dirname(retained), { recursive: true, mode: 0o700 }) + await rename(current, retained) + await syncStorageDirectory(dirname(retained)) + await syncStorageDirectory(dirname(current)) + } + continue + } if (next) { if (previous) { if (saved || !entry.original) diff --git a/apps/buddy/service/src/storage/composerDraftRepository.ts b/apps/buddy/service/src/storage/composerDraftRepository.ts index a14c1e9da..9ee14cabc 100644 --- a/apps/buddy/service/src/storage/composerDraftRepository.ts +++ b/apps/buddy/service/src/storage/composerDraftRepository.ts @@ -8,6 +8,7 @@ import type { } from '../../../shared/conversation/composerDraft' import { buddyComposerDraftSchema } from '../../../shared/conversation/composerDraft' import { withTransaction } from './database' +import { createDraftOwnershipRepository } from './draftOwnershipRepository' interface ComposerDraftRow { approval_policy: BuddyComposerDraft['executionConfig']['approvalPolicy'] @@ -43,8 +44,11 @@ export class ComposerDraftConflictError extends Error { } } -export function createComposerDraftRepository(database: DatabaseSync): ComposerDraftRepository { - const findByIdStatement = database.prepare('SELECT * FROM composer_drafts WHERE id = ?') +export function createComposerDraftRepository(database: DatabaseSync, trustedActorId?: string): ComposerDraftRepository { + const owned = Boolean(database.prepare('SELECT 1 FROM sqlite_master WHERE type = \'table\' AND name = \'composer_draft_slots\'').get()) + const actorId = owned ? trustedActorId ?? String(database.prepare('SELECT local_actor_id FROM storage_profile WHERE id = 1').get()!.local_actor_id) : null + const ownership = actorId ? createDraftOwnershipRepository(database, actorId) : null + const findByIdStatement = database.prepare(`SELECT * FROM composer_drafts WHERE id = ?${ownership ? ' AND owner_actor_id = ?' : ''}`) const findGlobal = database.prepare('SELECT * FROM composer_drafts WHERE scope_kind = \'global\'') const findSpace = database.prepare('SELECT * FROM composer_drafts WHERE scope_kind = \'space\' AND space_id = ?') const findBranch = database.prepare(` @@ -60,8 +64,8 @@ export function createComposerDraftRepository(database: DatabaseSync): ComposerD INSERT INTO composer_drafts ( id, scope_kind, space_id, conversation_id, branch_id, source_message_id, revision, content_json, model_selection_json, participants_json, approval_policy, execution_profile, - created_at, updated_at - ) VALUES (?, ?, ?, ?, ?, ?, 0, ?, ?, ?, ?, ?, ?, ?) + created_at, updated_at${ownership ? ', owner_actor_id' : ''} + ) VALUES (?, ?, ?, ?, ?, ?, 0, ?, ?, ?, ?, ?, ?, ?${ownership ? ', ?' : ''}) `) const save = database.prepare(` UPDATE composer_drafts @@ -71,12 +75,22 @@ export function createComposerDraftRepository(database: DatabaseSync): ComposerD WHERE id = ? AND revision = ? `) + const project = (row: ComposerDraftRow): BuddyComposerDraft => { + const draft = toDraft(row) + return ownership ? { ...draft, slotRevision: ownership.findSlot(draft.scope)?.slotRevision ?? 0 } : draft + } + const findById = (draftId: string): BuddyComposerDraft | null => { - const row = findByIdStatement.get(draftId) as unknown as ComposerDraftRow | undefined - return row ? toDraft(row) : null + ownership?.assertActor() + const row = (ownership ? findByIdStatement.get(draftId, actorId) : findByIdStatement.get(draftId)) as unknown as ComposerDraftRow | undefined + return row ? project(row) : null } const findByScope = (scope: BuddyComposerDraftScope): BuddyComposerDraft | null => { + if (ownership) { + const slot = ownership.findSlot(scope) + return slot?.draftId ? findById(slot.draftId) : null + } if (scope.kind === 'task') { const draft = findById(scope.draftId) if (draft && draft.scope.kind !== 'task') @@ -97,8 +111,11 @@ export function createComposerDraftRepository(database: DatabaseSync): ComposerD discard(input) { return withTransaction(database, () => { const draft = findById(input.draftId) - if (!draft) + if (!draft) { + if (ownership && database.prepare('SELECT 1 FROM composer_drafts WHERE id = ?').get(input.draftId)) + throw new ComposerDraftConflictError() return true + } if (draft.scope.kind !== 'task' || draft.revision !== input.expectedRevision) return false return Number(database.prepare(` @@ -108,10 +125,16 @@ export function createComposerDraftRepository(database: DatabaseSync): ComposerD `).run(input.draftId, input.expectedRevision, input.draftId, input.draftId).changes) === 1 }) }, - list: () => (database.prepare('SELECT * FROM composer_drafts WHERE scope_kind IN (\'task\', \'global\', \'space\') ORDER BY updated_at DESC').all() as unknown as ComposerDraftRow[]).map(toDraft), + list: () => { + ownership?.assertActor() + const query = database.prepare(`SELECT * FROM composer_drafts WHERE scope_kind IN ('task', 'global', 'space')${ownership ? ' AND owner_actor_id = ?' : ''} ORDER BY updated_at DESC`) + return ((ownership ? query.all(actorId) : query.all()) as unknown as ComposerDraftRow[]).map(project) + }, findById, findByScope, open(input) { + if (/^input-[a-f\d]{64}$/.test(input.draftId)) + throw new ComposerDraftConflictError() return withTransaction(database, () => { if (input.scope.kind === 'task' && input.scope.draftId !== input.draftId) throw new ComposerDraftConflictError() @@ -135,7 +158,10 @@ export function createComposerDraftRepository(database: DatabaseSync): ComposerD input.initialExecutionConfig.executionProfile, input.now, input.now, + ...(ownership ? [actorId] : []), ) + if (ownership) + ownership.select(input.scope, input.draftId, ownership.findSlot(input.scope)?.slotRevision ?? 0) return requireDraft(findById(input.draftId)) }) }, @@ -235,3 +261,8 @@ function requireValue(value: string | null): string { throw new ComposerDraftConflictError() return value } + +export function findPersonalComposerDraft(database: DatabaseSync, draftId: string): BuddyComposerDraft | null { + const owner = database.prepare('SELECT owner_actor_id FROM composer_drafts WHERE id = ?').get(draftId) + return owner ? createComposerDraftRepository(database, String(owner.owner_actor_id)).findById(draftId) : null +} diff --git a/apps/buddy/service/src/storage/composerResourceRepository.ts b/apps/buddy/service/src/storage/composerResourceRepository.ts index 9e5fd6de1..1419e112d 100644 --- a/apps/buddy/service/src/storage/composerResourceRepository.ts +++ b/apps/buddy/service/src/storage/composerResourceRepository.ts @@ -136,16 +136,16 @@ export function createComposerResourceRepository(database: DatabaseSync) { )) }, finish(input: { attachmentId: string, contentHash: string, draftId: string, now: string, resourceId: string }) { - return Number(finish.run(input.attachmentId, input.contentHash, input.now, input.resourceId, input.draftId).changes) === 1 + return withTransaction(database, () => Number(finish.run(input.attachmentId, input.contentHash, input.now, input.resourceId, input.draftId).changes) === 1) }, fail(draftId: string, resourceId: string, errorCode: 'IMPORT_FAILED' | 'IMPORT_INTERRUPTED', now: string) { - return Number(fail.run(errorCode, now, resourceId, draftId).changes) === 1 + return withTransaction(database, () => Number(fail.run(errorCode, now, resourceId, draftId).changes) === 1) }, retry(draftId: string, resourceId: string, now: string) { - return Number(retry.run(now, resourceId, draftId).changes) === 1 + return withTransaction(database, () => Number(retry.run(now, resourceId, draftId).changes) === 1) }, interruptImports(now: string) { - return Number(interrupt.run(now).changes) + return withTransaction(database, () => Number(interrupt.run(now).changes)) }, failUnavailableAttachments(attachmentIds: readonly string[], now: string) { return withTransaction(database, () => attachmentIds.reduce( diff --git a/apps/buddy/service/src/storage/conversationArchiveRepository.ts b/apps/buddy/service/src/storage/conversationArchiveRepository.ts index 28880238a..76d02cec0 100644 --- a/apps/buddy/service/src/storage/conversationArchiveRepository.ts +++ b/apps/buddy/service/src/storage/conversationArchiveRepository.ts @@ -1,6 +1,7 @@ import type { DatabaseSync, SQLInputValue } from 'node:sqlite' import { Buffer } from 'node:buffer' import { BuddyServiceError } from '../rpc/runtimeRequest' +import { conversationExecutionResourcesReleased } from './ExecutionResourceQueries' export type ConversationArchiveDays = 0 | 3 | 7 | 30 export interface ConversationArchiveQuery { @@ -26,11 +27,7 @@ export type ConversationArchiveRepository = ReturnType 0 AND c.archived_at IS NOT NULL AND c.deleted_at IS NULL AND julianday(c.archived_at) + p.days <= julianday(?) AND NOT EXISTS (SELECT 1 FROM conversation_deletion_jobs j WHERE j.conversation_id = c.id) - AND NOT EXISTS (SELECT 1 FROM runs r WHERE r.conversation_id = c.id AND r.status IN ('queued', 'running')) - AND NOT EXISTS (SELECT 1 FROM execution_state s JOIN runs r ON r.id = s.run_id - WHERE r.conversation_id = c.id AND (s.phase <> 'closed' OR s.cleanup_state <> 'clean')) - AND NOT EXISTS (SELECT 1 FROM run_mutation_leases l JOIN runs r ON r.id = l.run_id - WHERE r.conversation_id = c.id AND l.status <> 'released') + AND ${conversationExecutionResourcesReleased} AND NOT EXISTS (SELECT 1 FROM chat_queue q WHERE q.conversation_id = c.id AND q.state = 'waiting') AND NOT EXISTS (SELECT 1 FROM approvals a JOIN runs r ON r.id = a.run_id WHERE r.conversation_id = c.id AND a.status = 'pending')` diff --git a/apps/buddy/service/src/storage/conversationDeletionRepository.ts b/apps/buddy/service/src/storage/conversationDeletionRepository.ts index a5678f788..9dfd23736 100644 --- a/apps/buddy/service/src/storage/conversationDeletionRepository.ts +++ b/apps/buddy/service/src/storage/conversationDeletionRepository.ts @@ -1,5 +1,6 @@ import type { DatabaseSync } from 'node:sqlite' import { withTransaction } from './database' +import { ExecutionResourceQueries } from './ExecutionResourceQueries' export interface ConversationDeletionCommit { conversationId: string @@ -11,21 +12,6 @@ export interface ConversationDeletionCommit { export type ConversationDeletionRepository = ReturnType export function createConversationDeletionRepository(database: DatabaseSync) { - const commitRemoval = (conversationId: string, now: string, permanent: boolean): ConversationDeletionCommit | null => withTransaction(database, () => { - const conversation = database.prepare('SELECT deleted_at, archived_at FROM conversations WHERE id = ?').get(conversationId) as { deleted_at: string | null, archived_at: string | null } | undefined - if (!conversation || (!permanent && conversation.archived_at !== null)) - return null - const deletedAt = conversation.deleted_at ?? now - const grants = database.prepare('SELECT id FROM conversation_directory_grants WHERE conversation_id = ? AND revoked_at IS NULL').all(conversationId) as { id: string }[] - if (conversation.deleted_at === null) - database.prepare('UPDATE conversations SET deleted_at = ?, updated_at = ? WHERE id = ? AND deleted_at IS NULL').run(deletedAt, deletedAt, conversationId) - database.prepare('UPDATE conversation_directory_grants SET revoked_at = ? WHERE conversation_id = ? AND revoked_at IS NULL').run(deletedAt, conversationId) - if (permanent) { - database.prepare(`INSERT OR IGNORE INTO conversation_deletion_jobs (conversation_id, status, created_at, updated_at) - VALUES (?, 'pending', ?, ?)`).run(conversationId, now, now) - } - return { conversationId, deletedAt, tombstoned: conversation.deleted_at === null, revokedGrantIds: grants.map(grant => grant.id) } - }) return { recoverLegacy(conversationId: string, now: string): ConversationDeletionCommit | null { return withTransaction(database, () => { @@ -46,10 +32,7 @@ export function createConversationDeletionRepository(database: DatabaseSync) { return withTransaction(database, () => { const conversation = database.prepare(`SELECT deleted_at FROM conversations WHERE id = ? AND deleted_at IS NOT NULL AND archived_at IS NULL AND NOT EXISTS (SELECT 1 FROM conversation_deletion_jobs WHERE conversation_id = conversations.id AND origin <> 'legacy_removed')`).get(conversationId) - if (!conversation || database.prepare(`SELECT 1 FROM runs r LEFT JOIN execution_state s ON s.run_id = r.id - WHERE r.conversation_id = ? AND (r.status IN ('queued', 'running') OR s.phase <> 'closed' OR s.cleanup_state <> 'clean')`).get(conversationId) - || database.prepare(`SELECT 1 FROM run_mutation_leases l JOIN runs r ON r.id = l.run_id - WHERE r.conversation_id = ? AND l.status <> 'released'`).get(conversationId)) { + if (!conversation || new ExecutionResourceQueries(database).conversation(conversationId) !== 'released') { return null } const grants = database.prepare('SELECT id FROM conversation_directory_grants WHERE conversation_id = ? AND revoked_at IS NULL').all(conversationId) @@ -60,10 +43,19 @@ export function createConversationDeletionRepository(database: DatabaseSync) { }) }, commit(conversationId: string, now: string): ConversationDeletionCommit | null { - return commitRemoval(conversationId, now, true) - }, - commitSoftDelete(conversationId: string, now: string): ConversationDeletionCommit | null { - return commitRemoval(conversationId, now, false) + return withTransaction(database, () => { + const conversation = database.prepare('SELECT deleted_at FROM conversations WHERE id = ?').get(conversationId) as { deleted_at: string | null } | undefined + if (!conversation) + return null + const deletedAt = conversation.deleted_at ?? now + const grants = database.prepare('SELECT id FROM conversation_directory_grants WHERE conversation_id = ? AND revoked_at IS NULL').all(conversationId) as { id: string }[] + if (conversation.deleted_at === null) + database.prepare('UPDATE conversations SET deleted_at = ?, updated_at = ? WHERE id = ? AND deleted_at IS NULL').run(deletedAt, deletedAt, conversationId) + database.prepare('UPDATE conversation_directory_grants SET revoked_at = ? WHERE conversation_id = ? AND revoked_at IS NULL').run(deletedAt, conversationId) + database.prepare(`INSERT OR IGNORE INTO conversation_deletion_jobs (conversation_id, status, created_at, updated_at) + VALUES (?, 'pending', ?, ?)`).run(conversationId, now, now) + return { conversationId, deletedAt, tombstoned: conversation.deleted_at === null, revokedGrantIds: grants.map(grant => grant.id) } + }) }, has(conversationId: string): boolean { return !!database.prepare('SELECT 1 FROM conversation_deletion_jobs WHERE conversation_id = ?').get(conversationId) diff --git a/apps/buddy/service/src/storage/conversationHistoryRepository.ts b/apps/buddy/service/src/storage/conversationHistoryRepository.ts index 7c86c90e1..39b65462f 100644 --- a/apps/buddy/service/src/storage/conversationHistoryRepository.ts +++ b/apps/buddy/service/src/storage/conversationHistoryRepository.ts @@ -192,11 +192,6 @@ export function createConversationHistoryStore(database: DatabaseSync): Conversa WHERE conversation_id = ? ORDER BY created_at, id `) - const findIncompleteRun = database.prepare(` - SELECT 1 FROM runs - WHERE conversation_id = ? AND execution_kind IN ('root', 'maintenance') AND status IN ('queued', 'running') - LIMIT 1 - `) const collectBranchMessages = ( conversationId: string, branchId: string, @@ -281,8 +276,6 @@ export function createConversationHistoryStore(database: DatabaseSync): Conversa } if (conversation.active_branch_id === input.branchId) return - if (findIncompleteRun.get(input.conversationId)) - throw new ConversationHistoryError('branch is not activatable') activateBranch.run(input.branchId, input.updatedAt, input.conversationId) }, insertRootBranch(input) { diff --git a/apps/buddy/service/src/storage/conversationIndexRepository.ts b/apps/buddy/service/src/storage/conversationIndexRepository.ts index 28eae0f1c..c4360a82c 100644 --- a/apps/buddy/service/src/storage/conversationIndexRepository.ts +++ b/apps/buddy/service/src/storage/conversationIndexRepository.ts @@ -108,10 +108,13 @@ function parseHistoryCursor(cursor: string, identity: string): HistoryCursor { export function createConversationIndexRepository( database: DatabaseSync, ): ConversationIndexRepository { + const visiblePredicate = database.prepare('PRAGMA table_info(conversations)').all().some(column => column.name === 'archived_at') + ? 'conversations.deleted_at IS NULL AND conversations.archived_at IS NULL' + : 'conversations.deleted_at IS NULL' const marks = createTaskMarkRepository(database) const listRecent = database.prepare(` ${SUMMARY_SELECT} - WHERE conversations.deleted_at IS NULL AND conversations.archived_at IS NULL + WHERE ${visiblePredicate} AND (? = 0 OR conversations.space_id IS ?) AND instr(lower(COALESCE(conversations.title, '')), lower(?)) > 0 AND (? IS NULL OR conversations.id != ?) @@ -132,7 +135,7 @@ export function createConversationIndexRepository( const query = parse(conversationHistoryQuerySchema, input) const identity = queryIdentity(query) const cursor = query.cursor ? parseHistoryCursor(query.cursor, identity) : null - const conditions = ['conversations.deleted_at IS NULL AND conversations.archived_at IS NULL'] + const conditions = [visiblePredicate] const parameters: SQLInputValue[] = [] if (query.spaceId !== undefined) { conditions.push('conversations.space_id IS ?') @@ -179,7 +182,7 @@ export function createConversationIndexRepository( return [] const rows = database.prepare(` ${SUMMARY_SELECT} - WHERE conversations.deleted_at IS NULL AND conversations.archived_at IS NULL AND conversations.id IN (${unique.map(() => '?').join(', ')}) + WHERE ${visiblePredicate} AND conversations.id IN (${unique.map(() => '?').join(', ')}) `).all(...unique) as unknown as ConversationSummaryRow[] const entries = new Map(withMarks(rows).map(entry => [entry.conversation.id, entry])) return unique.flatMap(id => entries.has(id) ? [entries.get(id)!] : []) diff --git a/apps/buddy/service/src/storage/conversationPurgeRepository.ts b/apps/buddy/service/src/storage/conversationPurgeRepository.ts index 899b36660..3a5ab5ef0 100644 --- a/apps/buddy/service/src/storage/conversationPurgeRepository.ts +++ b/apps/buddy/service/src/storage/conversationPurgeRepository.ts @@ -2,9 +2,14 @@ import type { DatabaseSync } from 'node:sqlite' import type { BuddyDataPaths } from './BuddyDataPaths' import { isAbsolute, relative, resolve, sep } from 'node:path' import { withTransaction } from './database' +import { ExecutionResourceQueries } from './ExecutionResourceQueries' + +export type ConversationStorageAuthority = 'legacy_jsonl' | 'sqlite' export interface ConversationPurgeManifest { draftIds: string[] + fileIds: string[] + contentReferenceIds: string[] } export class ConversationPurgeError extends Error { @@ -27,16 +32,31 @@ export function isPurgeDescendant(directory: string, path: string): boolean { export class ConversationPurgeRepository { private readonly database: DatabaseSync private readonly paths: BuddyDataPaths + private readonly storageAuthority: ConversationStorageAuthority - constructor(database: DatabaseSync, paths: BuddyDataPaths) { + constructor(database: DatabaseSync, paths: BuddyDataPaths, storageAuthority: ConversationStorageAuthority) { this.database = database this.paths = paths + this.storageAuthority = storageAuthority } directories(id: string, manifest: ConversationPurgeManifest): string[] { return [this.paths.conversationDirectory(id), ...manifest.draftIds.map(draftId => resolve(this.paths.draftAttachments(draftId), '..'))] } + stagingFiles(manifest: ConversationPurgeManifest): string[] { + if (this.storageAuthority === 'legacy_jsonl') + return [] + return this.database.prepare('SELECT id, temporary_path FROM managed_files WHERE id IN (SELECT value FROM json_each(?))') + .all(JSON.stringify(manifest.fileIds)) + .flatMap((row) => { + if (!/^[A-Z0-9][\w-]{0,127}$/i.test(String(row.id))) + throw new ConversationPurgeError('CONVERSATION_FILE_BOUNDARY') + const expected = resolve(this.paths.root, 'managed-files', 'staging', `${row.id}.part`) + return row.temporary_path === expected ? [expected] : [] + }) + } + prepare(id: string): ConversationPurgeManifest | null { const db = this.database return withTransaction(db, () => { @@ -47,10 +67,7 @@ export class ConversationPurgeRepository { throw new ConversationPurgeError('CONVERSATION_DELETION_FAILED') if (!db.prepare('SELECT 1 FROM conversations WHERE id = ? AND deleted_at IS NOT NULL').get(id)) throw new ConversationPurgeError('CONVERSATION_DELETION_STATE_CHANGED') - if (db.prepare(`SELECT 1 FROM runs WHERE conversation_id = ? AND status IN ('running', 'queued')`).get(id) - || db.prepare(`SELECT 1 FROM execution_state s JOIN runs r ON r.id = s.run_id - WHERE r.conversation_id = ? AND (s.phase <> 'closed' OR s.cleanup_state <> 'clean')`).get(id) - || db.prepare(`SELECT 1 FROM run_mutation_leases l JOIN runs r ON r.id = l.run_id WHERE r.conversation_id = ? AND l.status <> 'released'`).get(id)) { + if (new ExecutionResourceQueries(db).conversation(id) !== 'released') { throw new ConversationPurgeError('CONVERSATION_RESOURCES_BUSY') } const manifest: ConversationPurgeManifest = job.manifest_json @@ -58,7 +75,19 @@ export class ConversationPurgeRepository { : { draftIds: db.prepare(`SELECT id FROM composer_drafts WHERE conversation_id = ? UNION SELECT id FROM chat_queue WHERE conversation_id = ?`).all(id, id).map(row => String(row.id)), + fileIds: [], + contentReferenceIds: [], } + if (this.storageAuthority === 'sqlite') { + const directories = this.directories(id, manifest) + const files = db.prepare('SELECT id, path FROM managed_files').all().filter(row => directories.some(directory => isPurgeDescendant(directory, String(row.path)))) + manifest.fileIds = [...new Set([...manifest.fileIds, ...files.map(row => String(row.id))])] + manifest.contentReferenceIds = db.prepare(`SELECT DISTINCT reference_id FROM product_content_reference_owners + WHERE (owner_kind = 'conversation' AND owner_id = ?) + OR (owner_kind = 'draft' AND owner_id IN (SELECT value FROM json_each(?)))`) + .all(id, JSON.stringify(manifest.draftIds)) + .map(row => String(row.reference_id)) + } this.assertReferences(id, manifest) db.prepare('UPDATE conversation_deletion_jobs SET manifest_json = ? WHERE conversation_id = ?').run(JSON.stringify(manifest), id) db.exec('SAVEPOINT purge_preflight') @@ -70,6 +99,11 @@ export class ConversationPurgeRepository { finally { db.exec('ROLLBACK TO purge_preflight; RELEASE purge_preflight') } + if (this.storageAuthority === 'sqlite') { + this.releaseOwnedFileReferences(id, manifest) + db.prepare(`UPDATE managed_files SET state = 'deleting', updated_at = ? + WHERE id IN (SELECT value FROM json_each(?)) AND state <> 'deleted'`).run(new Date().toISOString(), JSON.stringify(manifest.fileIds)) + } return manifest }) } @@ -101,6 +135,8 @@ export class ConversationPurgeRepository { WHERE b.conversation_id = ? AND q.conversation_id <> ?`).get(id, id, id, id, id, id, id, id, id, id, id, id)) { throw new ConversationPurgeError('CONVERSATION_REFERENCED') } + if (this.storageAuthority === 'sqlite') + this.assertManagedFileReferences(id, manifest) if (db.prepare(`SELECT 1 FROM composer_drafts WHERE id IN (SELECT value FROM json_each(?)) AND (conversation_id IS NULL OR conversation_id <> ?) UNION ALL SELECT 1 FROM chat_queue WHERE id IN (SELECT value FROM json_each(?)) AND conversation_id <> ?`).get(draftIds, id, draftIds, id)) { @@ -128,17 +164,65 @@ export class ConversationPurgeRepository { } } + private assertManagedFileReferences(id: string, manifest: ConversationPurgeManifest): void { + const db = this.database + const draftIds = JSON.stringify(manifest.draftIds) + const fileIds = JSON.stringify(manifest.fileIds) + if (db.prepare(`SELECT 1 FROM file_preparations WHERE state IN ('preparing', 'published') + AND (id IN (SELECT value FROM json_each(?)) OR source_file_id IN (SELECT value FROM json_each(?)) + OR target_owner_id IN (SELECT value FROM json_each(?)) + OR target_owner_id IN (SELECT id FROM messages WHERE conversation_id = ?))`).get(fileIds, fileIds, draftIds, id)) { + throw new ConversationPurgeError('CONVERSATION_RESOURCES_BUSY') + } + if (db.prepare(`SELECT 1 FROM file_references f WHERE f.file_id IN (SELECT value FROM json_each(?)) AND NOT ( + (f.kind = 'message' AND f.owner_id IN (SELECT id FROM messages WHERE conversation_id = ?)) + OR (f.kind IN ('draft', 'queue') AND f.owner_id IN (SELECT value FROM json_each(?))))`).get(fileIds, id, draftIds) + || db.prepare(`SELECT 1 FROM file_preparations WHERE source_file_id IN (SELECT value FROM json_each(?)) + AND id NOT IN (SELECT value FROM json_each(?))`).get(fileIds, fileIds)) { + throw new ConversationPurgeError('CONVERSATION_FILES_REFERENCED') + } + } + + private releaseOwnedFileReferences(id: string, manifest: ConversationPurgeManifest): void { + this.database.prepare(`DELETE FROM file_references WHERE + (kind = 'message' AND owner_id IN (SELECT id FROM messages WHERE conversation_id = ?)) + OR (kind IN ('draft', 'queue') AND owner_id IN (SELECT value FROM json_each(?)))`).run(id, JSON.stringify(manifest.draftIds)) + } + private erase(id: string, manifest: ConversationPurgeManifest): void { const db = this.database const draftIds = JSON.stringify(manifest.draftIds) db.exec('PRAGMA defer_foreign_keys = ON') + if (this.storageAuthority === 'sqlite') + this.releaseOwnedFileReferences(id, manifest) db.prepare('DELETE FROM composer_resources WHERE draft_id IN (SELECT value FROM json_each(?))').run(draftIds) db.prepare(`DELETE FROM attachments WHERE message_id IN (SELECT id FROM messages WHERE conversation_id = ?) OR draft_id IN (SELECT value FROM json_each(?))`).run(id, draftIds) + if (this.storageAuthority === 'sqlite') + this.eraseManagedContent(id, manifest) db.prepare('DELETE FROM run_mutation_leases WHERE run_id IN (SELECT id FROM runs WHERE conversation_id = ?)').run(id) db.prepare('DELETE FROM automation_occurrences WHERE conversation_id = ?').run(id) db.prepare('DELETE FROM task_attention WHERE conversation_id = ?').run(id) db.prepare('DELETE FROM conversation_members WHERE conversation_id = ?').run(id) db.prepare('DELETE FROM conversations WHERE id = ?').run(id) } + + private eraseManagedContent(id: string, manifest: ConversationPurgeManifest): void { + const db = this.database + const draftIds = JSON.stringify(manifest.draftIds) + const fileIds = JSON.stringify(manifest.fileIds) + db.prepare(`DELETE FROM remote_upload_chunks WHERE upload_id IN (SELECT id FROM remote_uploads WHERE file_id IN (SELECT value FROM json_each(?)))`).run(fileIds) + db.prepare('DELETE FROM remote_uploads WHERE file_id IN (SELECT value FROM json_each(?))').run(fileIds) + db.prepare('DELETE FROM remote_exports WHERE file_id IN (SELECT value FROM json_each(?))').run(fileIds) + db.prepare('DELETE FROM file_preparations WHERE id IN (SELECT value FROM json_each(?))').run(fileIds) + db.prepare(`UPDATE managed_files SET state = 'deleted' WHERE id IN (SELECT value FROM json_each(?))`).run(fileIds) + db.prepare('DELETE FROM managed_files WHERE id IN (SELECT value FROM json_each(?))').run(fileIds) + db.prepare(`DELETE FROM product_content_reference_owners WHERE + (owner_kind = 'conversation' AND owner_id = ?) + OR (owner_kind = 'draft' AND owner_id IN (SELECT value FROM json_each(?)))`).run(id, draftIds) + db.prepare(`DELETE FROM product_content_references WHERE id IN (SELECT value FROM json_each(?)) + AND NOT EXISTS (SELECT 1 FROM product_content_reference_owners WHERE reference_id = product_content_references.id)`) + .run(JSON.stringify(manifest.contentReferenceIds)) + db.prepare(`DELETE FROM composer_draft_slots WHERE draft_id IN (SELECT value FROM json_each(?)) OR json_extract(scope_key, '$[2]') = ?`).run(draftIds, id) + } } diff --git a/apps/buddy/service/src/storage/conversationRecord.ts b/apps/buddy/service/src/storage/conversationRecord.ts index 7e7b65214..5ef16f7e6 100644 --- a/apps/buddy/service/src/storage/conversationRecord.ts +++ b/apps/buddy/service/src/storage/conversationRecord.ts @@ -10,6 +10,8 @@ export interface ConversationModelSelection { } export interface ConversationRecord { + metadataRevision?: number + titleRevision?: number buddyId: string approvalPolicy: BuddyApprovalPolicy id: string @@ -20,13 +22,14 @@ export interface ConversationRecord { executionProfile: BuddyExecutionProfile modelSelection: ConversationModelSelection | null origin: 'automation' | 'interactive' - archivedAt: string | null - metadataRevision: number + archivedAt?: string | null deletedAt: string | null updatedAt: string } export interface ConversationRow { + metadata_revision?: number + title_revision?: number buddy_id: string approval_policy: BuddyApprovalPolicy id: string @@ -37,8 +40,7 @@ export interface ConversationRow { execution_profile: BuddyExecutionProfile actor_model_selection_json?: string | null origin: ConversationRecord['origin'] - archived_at: string | null - metadata_revision: number + archived_at?: string | null deleted_at: string | null updated_at: string } @@ -54,6 +56,7 @@ export function requireConversationRecord(value: unknown, id: string): Conversat export function toConversationRecord(row: ConversationRow): ConversationRecord { return { + ...(row.metadata_revision !== undefined ? { metadataRevision: row.metadata_revision, titleRevision: row.title_revision } : {}), buddyId: row.buddy_id, approvalPolicy: row.approval_policy, id: row.id, @@ -66,8 +69,7 @@ export function toConversationRecord(row: ConversationRow): ConversationRecord { ? JSON.parse(row.actor_model_selection_json) as ConversationModelSelection : null, origin: row.origin, - archivedAt: row.archived_at, - metadataRevision: row.metadata_revision, + archivedAt: row.archived_at ?? null, deletedAt: row.deleted_at, updatedAt: row.updated_at, } diff --git a/apps/buddy/service/src/storage/database.ts b/apps/buddy/service/src/storage/database.ts index a3fb6bd80..3b72276a3 100644 --- a/apps/buddy/service/src/storage/database.ts +++ b/apps/buddy/service/src/storage/database.ts @@ -1,19 +1,31 @@ import type { DatabaseSync } from 'node:sqlite' import type { BuddySchemaMigration } from './schema' -import { existsSync, mkdirSync } from 'node:fs' +import { copyFileSync, existsSync, mkdirSync, mkdtempSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' import { dirname, isAbsolute, join } from 'node:path' import { DatabaseSync as NodeDatabaseSync } from 'node:sqlite' -import { isAsyncFunction } from 'node:util/types' +import { CONTENT_REFERENCE_OWNERSHIP_TRIGGER_NAMES } from './migrations/contentReferenceOwnershipSchema' +import { MANAGED_FILES_INDEX_NAMES, MANAGED_FILES_TRIGGER_NAMES } from './migrations/managedFilesSchema' +import { PRIVATE_NETWORK_TABLE_COLUMNS, PRIVATE_NETWORK_TRIGGER_NAMES } from './migrations/privateNetworkSchema' +import { PRODUCT_CONTENT_REFERENCES_TRIGGER_NAMES } from './migrations/productContentReferencesSchema' +import { SCOPED_QUERY_REVISION_SOURCES, SCOPED_QUERY_REVISION_TRIGGER_NAMES } from './migrations/productQueryRevisionsSchema' +import { REMOTE_FILES_INDEX_NAMES, REMOTE_FILES_TABLE_COLUMNS, REMOTE_FILES_TRIGGER_NAMES } from './migrations/remoteFilesSchema' +import { RESOURCE_EXPORT_INDEX_NAMES, RESOURCE_EXPORT_TABLE_COLUMNS, RESOURCE_EXPORT_TRIGGER_NAMES } from './migrations/resourceExportsSchema' import { BUDDY_V15_CAPABILITY_OVERRIDES_SCHEMA_SQL, BUDDY_V15_CATALOG_MODEL_ID_SCHEMA_SQL, BUDDY_V15_CATALOG_SELECTION_SCHEMA_SQL, BUDDY_V15_PROVIDER_INSTANCES_SCHEMA_SQL, BUDDY_V15_REQUEST_HEADERS_SCHEMA_SQL } from './migrations/v15ModelServices' import { BUDDY_RUN_EVENT_CHECKPOINT_TRIGGER_NAMES } from './migrations/v24RunEventCheckpoints' import { BUDDY_PROVIDER_RESOLUTION_TRIGGER_NAMES } from './migrations/v39ProviderResolution' import { BUDDY_EVENT_MAINTENANCE_TRIGGER_NAMES } from './migrations/v40EventMaintenance' import { BUDDY_ARCHIVE_TRIGGER_NAMES, BUDDY_SCHEMA_LINEAGE } from './migrations/v42ConversationArchive' +import { BUDDY_PERSISTENCE_SCHEMA_LINEAGE } from './migrations/v43Persistence' import { + BUDDY_MIGRATION_SCHEMA_VERSION, BUDDY_SCHEMA_MIGRATIONS, BUDDY_SCHEMA_VERSION, + BUDDY_STORAGE_WRITER_VERSION, } from './schema' +import { requireSqliteAuthority } from './sqliteAuthority' +import { withTransaction } from './transaction' export const BUDDY_DATABASE_FILE_NAME = 'buddy.sqlite3' @@ -54,7 +66,7 @@ const BUDDY_CURRENT_SCHEMA_COLUMNS = { provider_resolution_checkpoints: ['provider_id', 'input_fingerprint'], extension_invocations: ['id', 'extension_id', 'action_id', 'conversation_id', 'trigger', 'status', 'branch_id', 'source_message_id', 'extension_name', 'action_title', 'result_message'], usage_records: ['run_id', 'invocation_id', 'detached'], - conversations: ['buddy_id', 'title_source', 'title_revision', 'archived_at', 'metadata_revision'], + conversations: ['buddy_id', 'title_source', 'title_revision', 'metadata_revision', 'archived_at'], conversation_archive_policy: ['id', 'days'], conversation_deletion_jobs: ['conversation_id', 'status', 'origin', 'error_code', 'manifest_json', 'created_at', 'updated_at'], connector_tool_catalogs: ['connector_id', 'tools_json', 'updated_at'], @@ -112,15 +124,37 @@ export function resolveBuddyDatabasePath(buddyHome: string): string { } export function openBuddyDatabase(options: OpenBuddyDatabaseOptions): DatabaseSync { + return openDatabase(options, BUDDY_SCHEMA_VERSION, 'NORMAL') +} + +export function openBuddyMigrationCandidate(options: OpenBuddyDatabaseOptions): DatabaseSync { + return openDatabase(options, BUDDY_MIGRATION_SCHEMA_VERSION, 'FULL') +} + +export function openBuddySqliteDatabase(options: OpenBuddyDatabaseOptions): DatabaseSync { + return openDatabase(options, BUDDY_MIGRATION_SCHEMA_VERSION, 'FULL', true) +} + +function validateDatabaseOptions(options: OpenBuddyDatabaseOptions): string { if (!options || typeof options !== 'object' || Array.isArray(options)) throw new TypeError('Buddy database options must be an object') const { databasePath } = options if (typeof databasePath !== 'string' || (databasePath !== ':memory:' && !isAbsolute(databasePath))) throw new TypeError('Buddy database requires an explicit absolute path or :memory:') + return databasePath +} + +function openDatabase(options: OpenBuddyDatabaseOptions, targetVersion: number, synchronous: 'NORMAL' | 'FULL', sqliteAuthority = false): DatabaseSync { + const databasePath = validateDatabaseOptions(options) if (databasePath !== ':memory:' && existsSync(databasePath)) { const existing = new NodeDatabaseSync(databasePath, { readOnly: true }) try { - validateDatabaseVersion(existing) + validateDatabaseVersion(existing, targetVersion, sqliteAuthority) + } + catch (error) { + if (!(error instanceof Error) || !('errcode' in error) || error.errcode !== 776) + throw error + validateRecoverableDatabase(databasePath, targetVersion, sqliteAuthority) } finally { existing.close() } } @@ -129,14 +163,29 @@ export function openBuddyDatabase(options: OpenBuddyDatabaseOptions): DatabaseSy const database = new NodeDatabaseSync(databasePath) try { - const currentVersion = validateDatabaseVersion(database) + const currentVersion = validateDatabaseVersion(database, targetVersion, sqliteAuthority) database.exec(` PRAGMA foreign_keys = ON; PRAGMA journal_mode = WAL; - PRAGMA synchronous = NORMAL; + PRAGMA synchronous = ${synchronous}; PRAGMA busy_timeout = 5000; `) - migrateBuddyDatabase(database, currentVersion) + if (synchronous === 'FULL') + database.exec('PRAGMA wal_autocheckpoint = 1000') + if (sqliteAuthority && currentVersion === 0) { + database.exec('PRAGMA foreign_keys = OFF') + try { + withTransaction(database, () => { + migrateBuddyDatabase(database, currentVersion, targetVersion) + database.exec('UPDATE storage_profile SET authority = \'sqlite\', migration_state = \'ready\' WHERE id = 1') + requireSqliteAuthority(database) + }) + } + finally { database.exec('PRAGMA foreign_keys = ON') } + } + else { + migrateBuddyDatabase(database, currentVersion, targetVersion) + } return database } catch (error) { @@ -146,26 +195,133 @@ export function openBuddyDatabase(options: OpenBuddyDatabaseOptions): DatabaseSy } } -function validateDatabaseVersion(database: DatabaseSync): number { +function validateRecoverableDatabase(databasePath: string, targetVersion: number, sqliteAuthority: boolean): void { + const directory = mkdtempSync(join(tmpdir(), 'lexora-sqlite-preflight-')) + try { + const copy = join(directory, BUDDY_DATABASE_FILE_NAME) + copyFileSync(databasePath, copy) + copyFileSync(`${databasePath}-journal`, `${copy}-journal`) + if (existsSync(`${databasePath}-wal`)) + copyFileSync(`${databasePath}-wal`, `${copy}-wal`) + const recovered = new NodeDatabaseSync(copy) + try { + validateDatabaseVersion(recovered, targetVersion, sqliteAuthority) + } + finally { recovered.close() } + } + finally { rmSync(directory, { recursive: true, force: true }) } +} + +function validateDatabaseVersion(database: DatabaseSync, targetVersion: number, sqliteAuthority: boolean): number { const currentVersion = readSchemaVersion(database) - if (currentVersion > BUDDY_SCHEMA_VERSION) + if (currentVersion >= 42 && currentVersion <= BUDDY_MIGRATION_SCHEMA_VERSION) + assertSchemaLineage(database, currentVersion === 42 ? BUDDY_SCHEMA_LINEAGE : BUDDY_PERSISTENCE_SCHEMA_LINEAGE) + if (currentVersion > targetVersion) throw new BuddyDatabaseVersionError('newer schema version') if (currentVersion === 0 && hasApplicationTables(database)) throw new BuddyDatabaseVersionError('unversioned schema') - if (currentVersion === 42) + if (currentVersion >= 42) assertCurrentSchema(database) + if (currentVersion >= 43) + assertMigrationSchema(database) + if (sqliteAuthority && currentVersion !== 0) { + if (currentVersion < 43) + throw new BuddyDatabaseVersionError('explicit persistence migration required') + requireSqliteAuthority(database) + } return currentVersion } -function migrateBuddyDatabase(database: DatabaseSync, currentVersion: number): void { +function migrateBuddyDatabase(database: DatabaseSync, currentVersion: number, targetVersion: number): void { if (currentVersion === 15) completeModelServicesMigration(database) for (const migration of BUDDY_SCHEMA_MIGRATIONS) { - if (migration.version <= currentVersion) + if (migration.version <= currentVersion || migration.version > targetVersion) continue applyMigration(database, migration) } + assertSchemaLineage(database, targetVersion === 42 ? BUDDY_SCHEMA_LINEAGE : BUDDY_PERSISTENCE_SCHEMA_LINEAGE) assertCurrentSchema(database) + if (targetVersion >= BUDDY_MIGRATION_SCHEMA_VERSION) { + assertMigrationSchema(database) + assertForeignKeysIntact(database) + } +} + +function assertMigrationSchema(database: DatabaseSync): void { + const tables: Record = { + storage_profile: ['id', 'data_generation', 'local_actor_id', 'min_writer_version', 'authority', 'migration_state'], + storage_actors: ['id', 'kind', 'state', 'created_at'], + command_receipts: ['data_generation', 'actor_id', 'command_id', 'method', 'fingerprint_version', 'fingerprint', 'source_instance_id', 'decision', 'target_json', 'result_json', 'error_code', 'decided_at'], + runs: ['next_event_sequence'], + run_events: ['format_version'], + product_content_references: ['id', 'owner_actor_id', 'data_generation', 'quote_json', 'created_at'], + product_content_reference_owners: ['reference_id', 'owner_kind', 'owner_id'], + approval_reviews: ['approval_id', 'review_json'], + approval_decisions: ['approval_id', 'resolution', 'actor_id', 'cancellation_reason', 'decided_at'], + conversations: ['metadata_revision'], + task_attention: ['assignment_revision'], + task_marks: ['revision'], + composer_drafts: ['owner_actor_id'], + composer_draft_slots: ['owner_actor_id', 'scope_key', 'draft_id', 'slot_revision'], + chat_queue: ['revision'], + chat_queue_deliveries: ['queue_id', 'run_id', 'stage', 'committed_at', 'observed_at'], + service_admission_gates: ['source_instance_id', 'stopped_at', 'command_id'], + persistence_migrations: ['id', 'source_fingerprint', 'source_schema', 'state', 'created_at'], + persistence_migration_entries: ['migration_id', 'kind', 'source_id', 'source_fingerprint', 'evidence_json'], + legacy_request_aliases: ['namespace', 'request_id', 'target_json', 'source_fingerprint'], + managed_files: ['id', 'path', 'temporary_path', 'size_bytes', 'sha256', 'state', 'durability'], + file_preparations: ['id', 'data_generation', 'actor_id', 'state'], + file_references: ['file_id', 'kind', 'owner_id', 'binding_id'], + ...PRIVATE_NETWORK_TABLE_COLUMNS, + ...REMOTE_FILES_TABLE_COLUMNS, + ...RESOURCE_EXPORT_TABLE_COLUMNS, + product_query_revisions: ['table_name', 'scope', 'revision'], + } + const protections = [ + 'command_receipts_no_replace', + 'command_receipts_no_update', + 'command_receipts_no_delete', + 'runs_event_sequence_monotonic', + ...MANAGED_FILES_TRIGGER_NAMES, + ...PRODUCT_CONTENT_REFERENCES_TRIGGER_NAMES, + 'task_attention_assignment_revision', + 'task_attention_assignment_insert', + 'task_marks_revision', + 'approval_reviews_immutable', + 'approval_decisions_immutable', + 'chat_queue_revision', + 'runs_new_event_sequence', + 'composer_drafts_default_owner', + 'composer_drafts_owner_immutable', + 'composer_drafts_release_slot', + 'composer_draft_slots_owner_insert', + 'composer_draft_slots_owner_update', + 'composer_draft_slots_revision', + ...CONTENT_REFERENCE_OWNERSHIP_TRIGGER_NAMES, + ...PRIVATE_NETWORK_TRIGGER_NAMES, + ...REMOTE_FILES_TRIGGER_NAMES, + ...RESOURCE_EXPORT_TRIGGER_NAMES, + ...SCOPED_QUERY_REVISION_TRIGGER_NAMES, + ] + const indexes = ['storage_actors_desktop', 'composer_drafts_owner', ...MANAGED_FILES_INDEX_NAMES, ...REMOTE_FILES_INDEX_NAMES, ...RESOURCE_EXPORT_INDEX_NAMES] + for (const [table, required] of Object.entries(tables)) { + const columns = new Set((database.prepare(`PRAGMA table_info(${table})`).all() as Array<{ name: string }>).map(column => column.name)) + if (required.some(column => !columns.has(column))) + throw new BuddyDatabaseVersionError('incomplete schema version') + } + const findRevision = database.prepare('SELECT 1 FROM product_query_revisions WHERE table_name = ? AND scope = \'\'') + if (SCOPED_QUERY_REVISION_SOURCES.some(table => !findRevision.get(table))) + throw new BuddyDatabaseVersionError('incomplete query revisions') + const profile = database.prepare('SELECT min_writer_version, local_actor_id FROM storage_profile WHERE id = 1').get() as { min_writer_version: number, local_actor_id: string } | undefined + if (!profile || !database.prepare('SELECT 1 FROM storage_actors WHERE id = ? AND kind = \'desktop\'').get(profile.local_actor_id)) + throw new BuddyDatabaseVersionError('incomplete storage identity') + if (profile.min_writer_version > BUDDY_STORAGE_WRITER_VERSION) + throw new BuddyDatabaseVersionError('newer storage writer version') + const findTrigger = database.prepare('SELECT 1 FROM sqlite_master WHERE type = \'trigger\' AND name = ?') + const findIndex = database.prepare('SELECT 1 FROM sqlite_master WHERE type = \'index\' AND name = ?') + if (indexes.some(name => !findIndex.get(name)) || protections.some(name => !findTrigger.get(name))) + throw new BuddyDatabaseVersionError('incomplete storage constraints') } function completeModelServicesMigration(database: DatabaseSync): void { @@ -192,13 +348,16 @@ function completeModelServicesMigration(database: DatabaseSync): void { }) } -function assertCurrentSchema(database: DatabaseSync): void { - const lineageColumns = database.prepare('PRAGMA table_info(buddy_schema_lineage)').all() - if (!['id', 'lineage'].every(name => lineageColumns.some(column => column.name === name))) +function assertSchemaLineage(database: DatabaseSync, expectedLineage: string): void { + const columns = database.prepare('PRAGMA table_info(buddy_schema_lineage)').all() as Array<{ name: string }> + if (!['id', 'lineage'].every(name => columns.some(column => column.name === name))) throw new BuddyDatabaseVersionError('incompatible schema lineage') - const lineage = database.prepare('SELECT id, lineage FROM buddy_schema_lineage').all() - if (lineage.length !== 1 || lineage[0]!.id !== 1 || lineage[0]!.lineage !== BUDDY_SCHEMA_LINEAGE) + const rows = database.prepare('SELECT id, lineage FROM buddy_schema_lineage').all() + if (rows.length !== 1 || rows[0]!.id !== 1 || rows[0]!.lineage !== expectedLineage) throw new BuddyDatabaseVersionError('incompatible schema lineage') +} + +function assertCurrentSchema(database: DatabaseSync): void { for (const [table, requiredColumns] of Object.entries(BUDDY_CURRENT_SCHEMA_COLUMNS)) { const columns = new Set((database.prepare(`PRAGMA table_info(${table})`).all() as Array<{ name: string @@ -218,6 +377,12 @@ function applyMigration(database: DatabaseSync, migration: BuddySchemaMigration) try { withTransaction(database, () => { database.exec(migration.sql) + if (migration.version === 43) { + assertSchemaLineage(database, BUDDY_PERSISTENCE_SCHEMA_LINEAGE) + assertCurrentSchema(database) + assertMigrationSchema(database) + assertForeignKeysIntact(database) + } if (suspendForeignKeys) assertForeignKeysIntact(database) database.exec(`PRAGMA user_version = ${migration.version}`) @@ -259,105 +424,4 @@ export class BuddyDatabaseVersionError extends Error { } } -interface TransactionContext { - failure: { error: unknown } | null - asynchronousResult: boolean -} - -const transactions = new WeakMap() -const blockedConnections = new WeakMap() - -export class BuddyTransactionUncertainError extends Error { - readonly code = 'STORAGE_TRANSACTION_UNCERTAIN' - - constructor(cause: unknown) { - super('Storage transaction outcome requires verification on a new connection', { cause }) - this.name = 'BuddyTransactionUncertainError' - } -} - -export function withTransaction(database: DatabaseSync, operation: () => T): T { - const blocked = blockedConnections.get(database) - if (blocked) - throw blocked - const active = transactions.get(database) - if (active) - return participateInTransaction(database, active, operation) - if (database.isTransaction) - throw new Error('Storage transaction must be owned by withTransaction') - - const context: TransactionContext = { failure: null, asynchronousResult: false } - let phase: 'begin' | 'operation' | 'commit' = 'begin' - try { - database.exec('BEGIN IMMEDIATE') - transactions.set(database, context) - phase = 'operation' - const result = participateInTransaction(database, context, operation) - if (context.failure) - throw context.failure.error - phase = 'commit' - database.exec('COMMIT') - if (database.isTransaction) - throw new Error('Storage transaction remains active after commit') - return result - } - catch (error) { - const blocked = blockedConnections.get(database) - if (blocked) - throw blocked - const original = context.failure ? context.failure.error : error - try { - if (database.isTransaction) { - database.exec('ROLLBACK') - if (database.isTransaction) - throw new Error('Storage transaction remains active after rollback') - } - else if (phase !== 'begin') { - throw original - } - } - catch (cleanupError) { - throw blockConnection(database, new BuddyTransactionUncertainError(new AggregateError([original, cleanupError], 'Storage transaction could not be rolled back reliably'))) - } - if (context.asynchronousResult) - blockConnection(database, new TypeError('Storage transactions cannot return a promise')) - throw original - } - finally { - transactions.delete(database) - } -} - -function participateInTransaction(database: DatabaseSync, context: TransactionContext, operation: () => T): T { - if (context.failure) - throw context.failure.error - try { - if (isAsyncFunction(operation)) - throw new TypeError('Storage transactions require synchronous operations') - const result = operation() - if (result !== null && (typeof result === 'object' || typeof result === 'function') && 'then' in result && typeof result.then === 'function') { - context.asynchronousResult = true - void Promise.resolve(result).catch(() => {}) - throw new TypeError('Storage transactions cannot return a promise') - } - if (!database.isTransaction) - throw new Error('Storage transaction ended outside withTransaction') - return result - } - catch (error) { - context.failure ??= { error } - if (!database.isOpen || !database.isTransaction) - throw blockConnection(database, new BuddyTransactionUncertainError(error)) - throw error - } -} - -function blockConnection(database: DatabaseSync, error: Error): Error { - blockedConnections.set(database, error) - try { - if (database.isOpen) - database.close() - } - catch {} - return error -} +export { withTransaction } from './transaction' diff --git a/apps/buddy/service/src/storage/draftOwnershipRepository.ts b/apps/buddy/service/src/storage/draftOwnershipRepository.ts new file mode 100644 index 000000000..2394450e7 --- /dev/null +++ b/apps/buddy/service/src/storage/draftOwnershipRepository.ts @@ -0,0 +1,68 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { BuddyComposerDraftScope } from '../../../shared/conversation/composerDraft' +import { withTransaction } from './transaction' + +export interface ComposerDraftSlot { + ownerActorId: string + scopeKey: string + draftId: string | null + slotRevision: number +} + +export function composerDraftScopeKey(scope: BuddyComposerDraftScope): string { + return JSON.stringify([ + scope.kind, + scope.kind === 'space' ? scope.spaceId : null, + 'conversationId' in scope ? scope.conversationId : null, + 'branchId' in scope ? scope.branchId : null, + scope.kind === 'message_edit' ? scope.userMessageId : scope.kind === 'message_followup' ? scope.assistantMessageId : null, + scope.kind === 'task' ? scope.draftId : null, + ]) +} + +export function createDraftOwnershipRepository(database: DatabaseSync, actorId: string) { + const assertActor = () => { + if (!database.prepare('SELECT 1 FROM storage_actors WHERE id = ? AND state = \'active\'').get(actorId)) + throw new Error('DRAFT_OWNER_UNAVAILABLE') + } + const findSlot = (scope: BuddyComposerDraftScope): ComposerDraftSlot | null => { + assertActor() + const scopeKey = composerDraftScopeKey(scope) + const row = database.prepare('SELECT draft_id, slot_revision FROM composer_draft_slots WHERE owner_actor_id = ? AND scope_key = ?').get(actorId, scopeKey) + return row ? { ownerActorId: actorId, scopeKey, draftId: row.draft_id as string | null, slotRevision: Number(row.slot_revision) } : null + } + return { + actorId, + assertActor, + findSlot, + owns(draftId: string): boolean { + assertActor() + return Boolean(database.prepare('SELECT 1 FROM composer_drafts WHERE id = ? AND owner_actor_id = ?').get(draftId, actorId)) + }, + select(scope: BuddyComposerDraftScope, draftId: string | null, expectedSlotRevision: number): ComposerDraftSlot { + return withTransaction(database, () => { + assertActor() + if (!Number.isSafeInteger(expectedSlotRevision) || expectedSlotRevision < 0) + throw new Error('DRAFT_SLOT_REVISION_CONFLICT') + const scopeKey = composerDraftScopeKey(scope) + if (draftId && !database.prepare(`SELECT 1 FROM composer_drafts WHERE id = ? AND owner_actor_id = ? + AND json_array(scope_kind, CASE WHEN scope_kind = 'space' THEN space_id END, + conversation_id, branch_id, source_message_id, CASE WHEN scope_kind = 'task' THEN id END) = ?`).get(draftId, actorId, scopeKey)) { + throw new Error('DRAFT_OWNER_OR_SCOPE_MISMATCH') + } + const existing = findSlot(scope) + if ((existing?.slotRevision ?? 0) !== expectedSlotRevision) + throw new Error('DRAFT_SLOT_REVISION_CONFLICT') + if (existing?.draftId === draftId) + return existing + if (expectedSlotRevision >= Number.MAX_SAFE_INTEGER) + throw new Error('DRAFT_SLOT_REVISION_EXHAUSTED') + if (!existing) + database.prepare('INSERT INTO composer_draft_slots (owner_actor_id, scope_key, draft_id, slot_revision) VALUES (?, ?, ?, 1)').run(actorId, scopeKey, draftId) + else + database.prepare('UPDATE composer_draft_slots SET draft_id = ?, slot_revision = slot_revision + 1 WHERE owner_actor_id = ? AND scope_key = ? AND slot_revision = ?').run(draftId, actorId, scopeKey, expectedSlotRevision) + return findSlot(scope)! + }) + }, + } +} diff --git a/apps/buddy/service/src/storage/executionRepository.ts b/apps/buddy/service/src/storage/executionRepository.ts index 8f73afeda..ce1ca4564 100644 --- a/apps/buddy/service/src/storage/executionRepository.ts +++ b/apps/buddy/service/src/storage/executionRepository.ts @@ -109,6 +109,7 @@ export function createExecutionRepository(database: DatabaseSync) { return null const initial = inputs.findByRunId(runId) return { + work: work.find(runId)!, run, phase: row.phase as ExecutionPhase, @@ -156,8 +157,8 @@ export function createExecutionRepository(database: DatabaseSync) { function assertLive(record: ExecutionStateRecord): ExecutionStateRecord[] { const chain = ancestors(record) - const conversation = database.prepare('SELECT active_branch_id, deleted_at FROM conversations WHERE id = ?').get(record.run.conversationId) - if (!conversation || conversation.deleted_at !== null || conversation.active_branch_id !== record.run.branchId || chain.some(item => !isOpen(item))) + const conversation = database.prepare('SELECT deleted_at FROM conversations WHERE id = ?').get(record.run.conversationId) + if (!conversation || conversation.deleted_at !== null || chain.some(item => !isOpen(item))) throw new ExecutionAdmissionError('stale') for (let index = 0; index < chain.length - 1; index++) { if (chain[index]!.admittedParentGeneration !== chain[index + 1]!.run.generation || chain[index]!.admittedRootGeneration !== chain.at(-1)!.run.generation) @@ -249,6 +250,15 @@ export function createExecutionRepository(database: DatabaseSync) { } return { + waitingRunIds(conversationId: string): string[] { + return database.prepare(`SELECT r.id FROM runs r + JOIN execution_state e ON e.run_id = r.id JOIN runs root ON root.id = r.root_run_id + JOIN execution_state root_state ON root_state.run_id = root.id + JOIN conversations c ON c.id = r.conversation_id + WHERE r.conversation_id = ? AND r.status = 'running' AND e.phase = 'waiting' AND e.cancel_requested_at IS NULL + AND root.status IN ('queued', 'running') AND root_state.phase NOT IN ('closing', 'closed') AND root_state.cancel_requested_at IS NULL + AND c.deleted_at IS NULL`).all(conversationId).map(row => String(row.id)) + }, find, listAttachments, assertNoForwardFallback, @@ -384,8 +394,8 @@ export function createExecutionRepository(database: DatabaseSync) { } if (snapshot.kind === 'actor' && database.prepare(`SELECT 1 FROM runs JOIN execution_state ON execution_state.run_id = runs.id JOIN execution_threads ON execution_threads.id = runs.thread_id - WHERE execution_threads.conversation_id = ? AND execution_threads.actor_id = ? AND execution_threads.kind = 'actor' - AND execution_state.phase <> 'closed' LIMIT 1`).get(caller.run.conversationId, snapshot.id)) { + WHERE execution_threads.conversation_id = ? AND runs.branch_id = ? AND execution_threads.actor_id = ? AND execution_threads.kind = 'actor' + AND execution_state.phase <> 'closed' LIMIT 1`).get(caller.run.conversationId, caller.run.branchId, snapshot.id)) { throw new ExecutionAdmissionError('busy') } if (database.prepare('SELECT 1 FROM execution_state WHERE result_mailbox_id = ?').get(input.resultMailboxId)) @@ -408,7 +418,7 @@ export function createExecutionRepository(database: DatabaseSync) { if (snapshot.kind === 'actor') freezeConversationMemberModel(database, caller.run.conversationId, snapshot.id, snapshot.model) const threadId = previous?.run.threadId ?? (snapshot.kind === 'actor' && context.contextMode === 'conversation' - ? threads.ensureActor(caller.run.conversationId, snapshot.id, input.run.startedAt).id + ? threads.ensureActor(caller.run.conversationId, snapshot.id, input.run.startedAt, caller.run.branchId).id : threads.createIsolated(caller.run.conversationId, snapshot.id, snapshot.kind, input.run.startedAt).id) const piSessionFile = threads.findPreviousSession(threadId, input.run.branchId) const run = runs.create({ ...input.run, actorSnapshot: snapshot, status: 'queued', piSessionFile, rootRunId: root.run.id, parentRunId: caller.run.id, executionKind: snapshot.kind === 'npc' ? 'npc_child' : 'actor_child', threadId, generation: 0 }) diff --git a/apps/buddy/service/src/storage/extensionReferenceRepository.ts b/apps/buddy/service/src/storage/extensionReferenceRepository.ts index c4fcfd513..94b1be859 100644 --- a/apps/buddy/service/src/storage/extensionReferenceRepository.ts +++ b/apps/buddy/service/src/storage/extensionReferenceRepository.ts @@ -103,7 +103,7 @@ export function createExtensionReferenceRepository(database: DatabaseSync, linea JOIN run_session_reference_scopes scopes ON scopes.run_id = runs.id JOIN conversations ON conversations.id = runs.conversation_id WHERE runs.id = ? AND runs.conversation_id = ? AND runs.status = 'running' - AND conversations.deleted_at IS NULL AND conversations.active_branch_id = runs.branch_id`) + AND conversations.deleted_at IS NULL`) .get(runId, conversationId) as SessionReferenceScope | undefined ?? null }, snapshots(scope: SessionReferenceScope, input: ReferenceSnapshotQuery) { diff --git a/apps/buddy/service/src/storage/mailboxRepository.ts b/apps/buddy/service/src/storage/mailboxRepository.ts index e4463adfd..94bb07fe1 100644 --- a/apps/buddy/service/src/storage/mailboxRepository.ts +++ b/apps/buddy/service/src/storage/mailboxRepository.ts @@ -66,7 +66,7 @@ export function createMailboxRepository(database: DatabaseSync) { WHERE target.id = ? AND target.generation = ? AND target.status IN ('queued', 'running') AND state.phase NOT IN ('closing', 'closed') AND state.cancel_requested_at IS NULL AND root.status IN ('queued', 'running') AND root_state.phase NOT IN ('closing', 'closed') AND root_state.cancel_requested_at IS NULL - AND conversations.deleted_at IS NULL AND conversations.active_branch_id = target.branch_id + AND conversations.deleted_at IS NULL AND NOT EXISTS (SELECT 1 FROM chain JOIN runs ancestor ON ancestor.id = chain.id JOIN execution_state ancestor_state ON ancestor_state.run_id = ancestor.id LEFT JOIN runs parent ON parent.id = ancestor.parent_run_id WHERE ancestor_state.phase IN ('closing', 'closed') OR ancestor_state.cancel_requested_at IS NOT NULL diff --git a/apps/buddy/service/src/storage/migrations/approvalReviewsSchema.ts b/apps/buddy/service/src/storage/migrations/approvalReviewsSchema.ts new file mode 100644 index 000000000..463145c3e --- /dev/null +++ b/apps/buddy/service/src/storage/migrations/approvalReviewsSchema.ts @@ -0,0 +1,17 @@ +export const APPROVAL_REVIEWS_SCHEMA_SQL = ` +CREATE TABLE approval_reviews ( + approval_id TEXT PRIMARY KEY REFERENCES approvals(id) ON DELETE CASCADE, + review_json TEXT NOT NULL CHECK (json_valid(review_json) AND json_type(review_json) = 'object') +) STRICT; +CREATE TRIGGER approval_reviews_immutable BEFORE UPDATE ON approval_reviews +BEGIN SELECT RAISE(ABORT, 'APPROVAL_REVIEW_IMMUTABLE'); END; +CREATE TABLE approval_decisions ( + approval_id TEXT PRIMARY KEY REFERENCES approvals(id) ON DELETE CASCADE, + resolution TEXT, + actor_id TEXT REFERENCES storage_actors(id), + cancellation_reason TEXT CHECK (cancellation_reason IN ('cancelled', 'expired', 'recovery', 'shutdown')), + decided_at TEXT NOT NULL +) STRICT; +CREATE TRIGGER approval_decisions_immutable BEFORE UPDATE ON approval_decisions +BEGIN SELECT RAISE(ABORT, 'APPROVAL_DECISION_IMMUTABLE'); END; +` diff --git a/apps/buddy/service/src/storage/migrations/branchExecutionSchema.ts b/apps/buddy/service/src/storage/migrations/branchExecutionSchema.ts new file mode 100644 index 000000000..e00a656db --- /dev/null +++ b/apps/buddy/service/src/storage/migrations/branchExecutionSchema.ts @@ -0,0 +1,15 @@ +export const BRANCH_EXECUTION_SCHEMA_SQL = ` +DROP TRIGGER initialize_execution; +CREATE TRIGGER initialize_execution AFTER INSERT ON runs +BEGIN + INSERT OR IGNORE INTO execution_threads (id, conversation_id, actor_id, kind, created_at) + SELECT 'actor:' || json_array(NEW.conversation_id, NEW.buddy_id, NEW.branch_id), NEW.conversation_id, NEW.buddy_id, 'actor', NEW.started_at + WHERE NEW.thread_id IS NULL; + UPDATE runs SET root_run_id = COALESCE(NEW.root_run_id, NEW.id), + thread_id = COALESCE(NEW.thread_id, 'actor:' || json_array(NEW.conversation_id, NEW.buddy_id, NEW.branch_id)), + execution_kind = CASE WHEN NEW.purpose = 'conversation.compaction' AND NEW.execution_kind = 'root' THEN 'maintenance' ELSE NEW.execution_kind END + WHERE id = NEW.id; + INSERT INTO execution_state (run_id, phase) VALUES (NEW.id, + CASE NEW.status WHEN 'queued' THEN 'queued' WHEN 'running' THEN 'working' ELSE 'closed' END); +END; +` diff --git a/apps/buddy/service/src/storage/migrations/contentReferenceOwnershipSchema.ts b/apps/buddy/service/src/storage/migrations/contentReferenceOwnershipSchema.ts new file mode 100644 index 000000000..359b40b65 --- /dev/null +++ b/apps/buddy/service/src/storage/migrations/contentReferenceOwnershipSchema.ts @@ -0,0 +1,91 @@ +const sources = [ + { table: 'composer_drafts', column: 'content_json', path: '$.resourceQuotes', draft: true }, + { table: 'messages', column: 'content_json', path: '$.userContent.resourceQuotes', draft: false }, + { table: 'chat_queue', column: 'prepared_json', path: '$.userMessageContent.userContent.resourceQuotes', draft: false }, +] as const + +const exactQuote = `NOT EXISTS ( + SELECT fullkey, type, atom FROM json_tree(reference.quote_json) + EXCEPT SELECT fullkey, type, atom FROM json_tree(quote.value) +) AND NOT EXISTS ( + SELECT fullkey, type, atom FROM json_tree(quote.value) + EXCEPT SELECT fullkey, type, atom FROM json_tree(reference.quote_json) +)` + +function ownerSelection(source: typeof sources[number], row: string): string { + return source.draft + ? `CASE WHEN ${row}.conversation_id IS NULL THEN 'draft' ELSE 'conversation' END, COALESCE(${row}.conversation_id, ${row}.id)` + : `'conversation', ${row}.conversation_id` +} + +function collectExistingSources(referenceId?: string): string { + return sources.map(source => ` +INSERT OR IGNORE INTO product_content_reference_owners (reference_id, owner_kind, owner_id) +SELECT reference.id, ${ownerSelection(source, 'owner')} +FROM ${source.table} owner JOIN json_each(owner.${source.column}, '${source.path}') quote +JOIN product_content_references reference ON json_extract(reference.quote_json, '$.id') = json_extract(quote.value, '$.id') +WHERE ${referenceId ? `reference.id = ${referenceId} AND ` : ''}${exactQuote}; +`).join('\n') +} + +export const CONTENT_REFERENCE_OWNERSHIP_SCHEMA_SQL = ` +CREATE TABLE product_content_reference_owners ( + reference_id TEXT NOT NULL REFERENCES product_content_references(id) ON DELETE CASCADE, + owner_kind TEXT NOT NULL CHECK (owner_kind IN ('conversation', 'draft', 'legacy')), + owner_id TEXT NOT NULL, + PRIMARY KEY (reference_id, owner_kind, owner_id) +) STRICT; +CREATE INDEX product_content_reference_owner ON product_content_reference_owners(owner_kind, owner_id); +CREATE INDEX product_content_reference_quote_id ON product_content_references(json_extract(quote_json, '$.id')); + +${collectExistingSources()} +INSERT OR IGNORE INTO product_content_reference_owners (reference_id, owner_kind, owner_id) +SELECT reference.id, 'legacy', reference.id FROM product_content_references reference +WHERE NOT EXISTS (SELECT 1 FROM product_content_reference_owners WHERE reference_id = reference.id); + +CREATE TRIGGER content_reference_sources_insert AFTER INSERT ON product_content_references +BEGIN + ${collectExistingSources('NEW.id')} +END; +${sources.flatMap(source => ['insert', 'update'].map(operation => ` +CREATE TRIGGER content_reference_${source.table}_${operation} AFTER ${operation === 'insert' ? 'INSERT' : `UPDATE OF ${source.column}, conversation_id`} ON ${source.table} +BEGIN + INSERT OR IGNORE INTO product_content_reference_owners (reference_id, owner_kind, owner_id) + SELECT reference.id, ${ownerSelection(source, 'NEW')} + FROM json_each(NEW.${source.column}, '${source.path}') quote + JOIN product_content_references reference ON json_extract(reference.quote_json, '$.id') = json_extract(quote.value, '$.id') + WHERE ${exactQuote}; + ${source.draft + ? ` + INSERT OR IGNORE INTO product_content_reference_owners (reference_id, owner_kind, owner_id) + SELECT reference_id, 'conversation', NEW.conversation_id FROM product_content_reference_owners + WHERE owner_kind = 'draft' AND owner_id = NEW.id AND NEW.conversation_id IS NOT NULL; + DELETE FROM product_content_reference_owners WHERE owner_kind = 'draft' AND owner_id = NEW.id AND NEW.conversation_id IS NOT NULL; + ` + : ''} +END; +`)).join('\n')} +CREATE TRIGGER content_reference_owner_immutable BEFORE UPDATE ON product_content_reference_owners +BEGIN SELECT RAISE(ABORT, 'CONTENT_REFERENCE_OWNER_IMMUTABLE'); END; +CREATE TRIGGER content_reference_owner_delete BEFORE DELETE ON product_content_reference_owners +WHEN NOT EXISTS ( + SELECT 1 FROM conversation_deletion_jobs job WHERE job.status = 'pending' AND ( + (OLD.owner_kind = 'conversation' AND OLD.owner_id = job.conversation_id) + OR (OLD.owner_kind = 'draft' AND OLD.owner_id IN (SELECT value FROM json_each(job.manifest_json, '$.draftIds'))) + ) +) AND NOT ( + OLD.owner_kind = 'draft' AND EXISTS ( + SELECT 1 FROM composer_drafts draft JOIN product_content_reference_owners owner + ON owner.reference_id = OLD.reference_id AND owner.owner_kind = 'conversation' AND owner.owner_id = draft.conversation_id + WHERE draft.id = OLD.owner_id + ) +) +BEGIN SELECT RAISE(ABORT, 'CONTENT_REFERENCE_OWNER_IMMUTABLE'); END; +` + +export const CONTENT_REFERENCE_OWNERSHIP_TRIGGER_NAMES = [ + ...sources.flatMap(source => ['insert', 'update'].map(operation => `content_reference_${source.table}_${operation}`)), + 'content_reference_sources_insert', + 'content_reference_owner_immutable', + 'content_reference_owner_delete', +] diff --git a/apps/buddy/service/src/storage/migrations/conversationArchiveManagedFilesSchema.ts b/apps/buddy/service/src/storage/migrations/conversationArchiveManagedFilesSchema.ts new file mode 100644 index 000000000..a759885e0 --- /dev/null +++ b/apps/buddy/service/src/storage/migrations/conversationArchiveManagedFilesSchema.ts @@ -0,0 +1,19 @@ +import { CONTENT_REFERENCE_OWNERSHIP_SCHEMA_SQL } from './contentReferenceOwnershipSchema' + +export const CONVERSATION_ARCHIVE_MANAGED_FILES_SCHEMA_SQL = ` +DROP TRIGGER managed_files_no_delete; +CREATE TRIGGER managed_files_no_delete BEFORE DELETE ON managed_files +WHEN OLD.state <> 'deleted' OR NOT EXISTS ( + SELECT 1 FROM conversation_deletion_jobs j, json_each(j.manifest_json, '$.fileIds') f + WHERE j.status = 'pending' AND f.value = OLD.id +) +BEGIN SELECT RAISE(ABORT, 'FILE_IDENTITY_IMMUTABLE'); END; +${CONTENT_REFERENCE_OWNERSHIP_SCHEMA_SQL} +DROP TRIGGER product_content_references_no_delete; +CREATE TRIGGER product_content_references_no_delete BEFORE DELETE ON product_content_references +WHEN EXISTS (SELECT 1 FROM product_content_reference_owners WHERE reference_id = OLD.id) OR NOT EXISTS ( + SELECT 1 FROM conversation_deletion_jobs j, json_each(j.manifest_json, '$.contentReferenceIds') r + WHERE j.status = 'pending' AND r.value = OLD.id +) +BEGIN SELECT RAISE(ABORT, 'CONTENT_REFERENCE_IMMUTABLE'); END; +` diff --git a/apps/buddy/service/src/storage/migrations/draftOwnershipSchema.ts b/apps/buddy/service/src/storage/migrations/draftOwnershipSchema.ts new file mode 100644 index 000000000..2f701e8f2 --- /dev/null +++ b/apps/buddy/service/src/storage/migrations/draftOwnershipSchema.ts @@ -0,0 +1,43 @@ +export const DRAFT_OWNERSHIP_SCHEMA_SQL = ` +ALTER TABLE composer_drafts ADD COLUMN owner_actor_id TEXT REFERENCES storage_actors(id); +UPDATE composer_drafts SET owner_actor_id = (SELECT local_actor_id FROM storage_profile WHERE id = 1); +DROP INDEX composer_drafts_global_scope; +DROP INDEX composer_drafts_space_scope; +DROP INDEX composer_drafts_branch_scope; +DROP INDEX composer_drafts_message_edit_scope; +DROP INDEX composer_drafts_followup_scope; +CREATE INDEX composer_drafts_owner ON composer_drafts(owner_actor_id, updated_at DESC); + +CREATE TABLE composer_draft_slots ( + owner_actor_id TEXT NOT NULL REFERENCES storage_actors(id), + scope_key TEXT NOT NULL CHECK (json_valid(scope_key) AND json_type(scope_key) = 'array'), + draft_id TEXT UNIQUE REFERENCES composer_drafts(id) ON DELETE SET NULL, + slot_revision INTEGER NOT NULL DEFAULT 0 CHECK (slot_revision >= 0), + PRIMARY KEY (owner_actor_id, scope_key) +) STRICT; + +INSERT INTO composer_draft_slots (owner_actor_id, scope_key, draft_id, slot_revision) +SELECT owner_actor_id, + json_array(scope_kind, CASE WHEN scope_kind = 'space' THEN space_id END, + conversation_id, branch_id, source_message_id, CASE WHEN scope_kind = 'task' THEN id END), + id, 0 FROM composer_drafts; + +CREATE TRIGGER composer_drafts_default_owner AFTER INSERT ON composer_drafts +WHEN NEW.owner_actor_id IS NULL +BEGIN UPDATE composer_drafts SET owner_actor_id = (SELECT local_actor_id FROM storage_profile WHERE id = 1) WHERE id = NEW.id; END; +CREATE TRIGGER composer_drafts_owner_immutable BEFORE UPDATE OF owner_actor_id ON composer_drafts +WHEN OLD.owner_actor_id IS NOT NULL AND NEW.owner_actor_id IS NOT OLD.owner_actor_id +BEGIN SELECT RAISE(ABORT, 'DRAFT_OWNER_IMMUTABLE'); END; +CREATE TRIGGER composer_drafts_release_slot BEFORE DELETE ON composer_drafts +BEGIN UPDATE composer_draft_slots SET draft_id = NULL, slot_revision = slot_revision + 1 WHERE draft_id = OLD.id; END; +CREATE TRIGGER composer_draft_slots_owner_insert BEFORE INSERT ON composer_draft_slots +WHEN NEW.draft_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM composer_drafts WHERE id = NEW.draft_id AND owner_actor_id = NEW.owner_actor_id) +BEGIN SELECT RAISE(ABORT, 'DRAFT_OWNER_MISMATCH'); END; +CREATE TRIGGER composer_draft_slots_owner_update BEFORE UPDATE OF owner_actor_id, draft_id ON composer_draft_slots +WHEN NEW.owner_actor_id <> OLD.owner_actor_id OR (NEW.draft_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM composer_drafts WHERE id = NEW.draft_id AND owner_actor_id = NEW.owner_actor_id)) +BEGIN SELECT RAISE(ABORT, 'DRAFT_OWNER_MISMATCH'); END; +CREATE TRIGGER composer_draft_slots_revision BEFORE UPDATE ON composer_draft_slots +WHEN NEW.scope_key <> OLD.scope_key OR NEW.slot_revision < OLD.slot_revision + OR (NEW.draft_id IS NOT OLD.draft_id AND NEW.slot_revision <> OLD.slot_revision + 1) +BEGIN SELECT RAISE(ABORT, 'DRAFT_SLOT_REVISION_CONFLICT'); END; +` diff --git a/apps/buddy/service/src/storage/migrations/managedFilesSchema.ts b/apps/buddy/service/src/storage/migrations/managedFilesSchema.ts new file mode 100644 index 000000000..9ebbf5ad2 --- /dev/null +++ b/apps/buddy/service/src/storage/migrations/managedFilesSchema.ts @@ -0,0 +1,133 @@ +export const MANAGED_FILES_SCHEMA_SQL = ` +CREATE TABLE managed_files ( + id TEXT PRIMARY KEY NOT NULL, + path TEXT NOT NULL UNIQUE, + temporary_path TEXT UNIQUE, + size_bytes INTEGER NOT NULL CHECK (size_bytes >= 0), + sha256 TEXT CHECK (sha256 IS NULL OR length(sha256) = 64), + state TEXT NOT NULL CHECK (state IN ('preparing', 'ready', 'unavailable', 'deleting', 'deleted')), + durability TEXT CHECK (durability IN ('file-and-directory-sync', 'file-sync', 'legacy-verified')), + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + CHECK (state <> 'ready' OR (sha256 IS NOT NULL AND durability IS NOT NULL)) +) STRICT; +CREATE TABLE file_preparations ( + id TEXT PRIMARY KEY NOT NULL REFERENCES managed_files(id), + operation_id TEXT NOT NULL, + owner_instance TEXT NOT NULL, + actor_id TEXT NOT NULL REFERENCES storage_actors(id), + data_generation TEXT NOT NULL, + kind TEXT NOT NULL CHECK (kind IN ('import', 'message', 'copy', 'export')), + target_owner_id TEXT NOT NULL, + source_file_id TEXT REFERENCES managed_files(id), + source_attachment_id TEXT, + source_draft_id TEXT, + source_message_id TEXT, + state TEXT NOT NULL CHECK (state IN ('preparing', 'published', 'settled', 'aborted')), + reserved_bytes INTEGER NOT NULL CHECK (reserved_bytes >= 0), + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL +) STRICT; +CREATE INDEX file_preparations_sources ON file_preparations(source_file_id, state); +CREATE INDEX file_preparations_active ON file_preparations(state, reserved_bytes) WHERE state IN ('preparing', 'published'); +CREATE TABLE file_references ( + file_id TEXT NOT NULL REFERENCES managed_files(id), + kind TEXT NOT NULL CHECK (kind IN ('draft', 'message', 'queue', 'export')), + owner_id TEXT NOT NULL, + binding_id TEXT NOT NULL, + PRIMARY KEY (kind, owner_id, binding_id) +) STRICT; +CREATE INDEX file_references_files ON file_references(file_id); +CREATE TRIGGER managed_files_identity_immutable BEFORE UPDATE OF id, path, temporary_path, size_bytes, sha256 ON managed_files +WHEN NEW.id <> OLD.id OR NEW.path <> OLD.path OR NEW.temporary_path IS NOT OLD.temporary_path OR NEW.size_bytes <> OLD.size_bytes + OR (OLD.sha256 IS NOT NULL AND NEW.sha256 IS NOT OLD.sha256) +BEGIN SELECT RAISE(ABORT, 'FILE_IDENTITY_IMMUTABLE'); END; +CREATE TRIGGER managed_files_no_delete BEFORE DELETE ON managed_files +BEGIN SELECT RAISE(ABORT, 'FILE_IDENTITY_IMMUTABLE'); END; +CREATE TRIGGER managed_files_claim_unreferenced BEFORE UPDATE OF state ON managed_files +WHEN NEW.state = 'deleting' AND ( + EXISTS (SELECT 1 FROM file_references WHERE file_id = OLD.id) + OR EXISTS (SELECT 1 FROM file_preparations WHERE state IN ('preparing', 'published') AND (id = OLD.id OR source_file_id = OLD.id)) +) +BEGIN SELECT RAISE(ABORT, 'FILE_REFERENCED'); END; +CREATE TRIGGER file_preparations_identity_immutable BEFORE UPDATE OF id, operation_id, owner_instance, actor_id, data_generation, kind, target_owner_id, source_file_id, source_attachment_id, source_draft_id, source_message_id ON file_preparations +BEGIN SELECT RAISE(ABORT, 'FILE_PREPARATION_IMMUTABLE'); END; +CREATE TRIGGER file_preparations_current BEFORE INSERT ON file_preparations +WHEN NOT EXISTS (SELECT 1 FROM storage_profile p JOIN storage_actors a ON a.id = NEW.actor_id + WHERE p.id = 1 AND p.data_generation = NEW.data_generation AND a.state = 'active') +BEGIN SELECT RAISE(ABORT, 'FILE_PREPARATION_IDENTITY_CHANGED'); END; +CREATE TRIGGER managed_files_no_resurrection BEFORE UPDATE OF state ON managed_files +WHEN (OLD.state = 'deleted' AND NEW.state <> 'deleted') + OR (OLD.state = 'deleting' AND NEW.state NOT IN ('deleting', 'deleted')) +BEGIN SELECT RAISE(ABORT, 'FILE_DELETION_CLAIMED'); END; +CREATE TRIGGER file_references_ready BEFORE INSERT ON file_references +WHEN EXISTS (SELECT 1 FROM managed_files WHERE id = NEW.file_id AND state IN ('preparing', 'deleting', 'deleted')) + OR EXISTS (SELECT 1 FROM file_preparations WHERE id = NEW.file_id AND state IN ('preparing', 'aborted')) + OR ((SELECT authority FROM storage_profile WHERE id = 1) = 'sqlite' + AND NOT EXISTS (SELECT 1 FROM managed_files WHERE id = NEW.file_id AND state = 'ready')) +BEGIN SELECT RAISE(ABORT, 'FILE_NOT_READY'); END; +CREATE TRIGGER file_references_no_rebind BEFORE UPDATE ON file_references +BEGIN SELECT RAISE(ABORT, 'FILE_REFERENCE_IMMUTABLE'); END; +CREATE TRIGGER file_preparations_source_ready BEFORE INSERT ON file_preparations +WHEN NEW.source_file_id IS NOT NULL + AND NOT EXISTS (SELECT 1 FROM managed_files WHERE id = NEW.source_file_id AND state = 'ready') +BEGIN SELECT RAISE(ABORT, 'FILE_SOURCE_UNAVAILABLE'); END; +CREATE TRIGGER attachments_file_ready_insert BEFORE INSERT ON attachments +WHEN (SELECT authority FROM storage_profile WHERE id = 1) = 'sqlite' + AND NOT EXISTS (SELECT 1 FROM managed_files WHERE path = NEW.stored_path AND state = 'ready' AND size_bytes = NEW.size_bytes) +BEGIN SELECT RAISE(ABORT, 'FILE_NOT_READY'); END; +CREATE TRIGGER attachments_file_ready_update BEFORE UPDATE OF stored_path, draft_id, message_id ON attachments +WHEN (SELECT authority FROM storage_profile WHERE id = 1) = 'sqlite' + AND NOT EXISTS (SELECT 1 FROM managed_files WHERE path = NEW.stored_path AND state = 'ready' AND size_bytes = NEW.size_bytes) +BEGIN SELECT RAISE(ABORT, 'FILE_NOT_READY'); END; +CREATE TRIGGER attachments_source_current_insert BEFORE INSERT ON attachments +WHEN EXISTS ( + SELECT 1 FROM file_preparations p JOIN managed_files f ON f.id = p.id + WHERE f.path = NEW.stored_path AND p.source_attachment_id IS NOT NULL + AND NOT EXISTS (SELECT 1 FROM attachments a JOIN managed_files s ON s.path = a.stored_path + WHERE a.id = p.source_attachment_id AND s.id = p.source_file_id + AND a.draft_id IS p.source_draft_id AND a.message_id IS p.source_message_id AND s.state = 'ready') +) +BEGIN SELECT RAISE(ABORT, 'FILE_SOURCE_CHANGED'); END; +CREATE TRIGGER attachments_source_current_update BEFORE UPDATE OF stored_path ON attachments +WHEN NEW.stored_path <> OLD.stored_path AND EXISTS ( + SELECT 1 FROM file_preparations p JOIN managed_files f ON f.id = p.id + WHERE f.path = NEW.stored_path AND p.source_attachment_id IS NOT NULL + AND NOT EXISTS (SELECT 1 FROM attachments a JOIN managed_files s ON s.path = a.stored_path + WHERE a.id = p.source_attachment_id AND s.id = p.source_file_id + AND a.draft_id IS p.source_draft_id AND a.message_id IS p.source_message_id AND s.state = 'ready') +) +BEGIN SELECT RAISE(ABORT, 'FILE_SOURCE_CHANGED'); END; +CREATE TRIGGER attachments_preparation_current_insert BEFORE INSERT ON attachments +WHEN EXISTS (SELECT 1 FROM file_preparations p JOIN managed_files f ON f.id = p.id + WHERE f.path = NEW.stored_path AND NOT EXISTS ( + SELECT 1 FROM storage_profile s JOIN storage_actors a ON a.id = p.actor_id + WHERE s.id = 1 AND s.data_generation = p.data_generation AND a.state = 'active')) +BEGIN SELECT RAISE(ABORT, 'FILE_PREPARATION_IDENTITY_CHANGED'); END; +CREATE TRIGGER attachments_preparation_current_update BEFORE UPDATE OF stored_path ON attachments +WHEN NEW.stored_path <> OLD.stored_path AND EXISTS (SELECT 1 FROM file_preparations p JOIN managed_files f ON f.id = p.id + WHERE f.path = NEW.stored_path AND NOT EXISTS ( + SELECT 1 FROM storage_profile s JOIN storage_actors a ON a.id = p.actor_id + WHERE s.id = 1 AND s.data_generation = p.data_generation AND a.state = 'active')) +BEGIN SELECT RAISE(ABORT, 'FILE_PREPARATION_IDENTITY_CHANGED'); END; +CREATE TRIGGER attachments_file_reference_insert AFTER INSERT ON attachments +BEGIN + INSERT INTO file_references (file_id, kind, owner_id, binding_id) + SELECT id, CASE WHEN NEW.message_id IS NOT NULL THEN 'message' + WHEN EXISTS (SELECT 1 FROM chat_queue WHERE id = NEW.draft_id) THEN 'queue' ELSE 'draft' END, + COALESCE(NEW.message_id, NEW.draft_id), NEW.id FROM managed_files WHERE path = NEW.stored_path; +END; +CREATE TRIGGER attachments_file_reference_update AFTER UPDATE OF stored_path, draft_id, message_id ON attachments +BEGIN + DELETE FROM file_references WHERE binding_id = OLD.id AND kind IN ('draft', 'message', 'queue'); + INSERT INTO file_references (file_id, kind, owner_id, binding_id) + SELECT id, CASE WHEN NEW.message_id IS NOT NULL THEN 'message' + WHEN EXISTS (SELECT 1 FROM chat_queue WHERE id = NEW.draft_id) THEN 'queue' ELSE 'draft' END, + COALESCE(NEW.message_id, NEW.draft_id), NEW.id FROM managed_files WHERE path = NEW.stored_path; +END; +CREATE TRIGGER attachments_file_reference_delete AFTER DELETE ON attachments +BEGIN DELETE FROM file_references WHERE binding_id = OLD.id AND kind IN ('draft', 'message', 'queue'); END; +` + +export const MANAGED_FILES_TRIGGER_NAMES = [...MANAGED_FILES_SCHEMA_SQL.matchAll(/CREATE TRIGGER (\w+)/g)].map(match => match[1]!) +export const MANAGED_FILES_INDEX_NAMES = ['file_preparations_sources', 'file_preparations_active', 'file_references_files'] as const diff --git a/apps/buddy/service/src/storage/migrations/metadataCommandsSchema.ts b/apps/buddy/service/src/storage/migrations/metadataCommandsSchema.ts new file mode 100644 index 000000000..41e5b8136 --- /dev/null +++ b/apps/buddy/service/src/storage/migrations/metadataCommandsSchema.ts @@ -0,0 +1,12 @@ +export const METADATA_COMMANDS_SCHEMA_SQL = ` +ALTER TABLE task_attention ADD COLUMN assignment_revision INTEGER NOT NULL DEFAULT 0 CHECK (assignment_revision >= 0); +CREATE TRIGGER task_attention_assignment_revision AFTER UPDATE OF mark_id ON task_attention +WHEN NEW.assignment_revision = OLD.assignment_revision AND NEW.mark_id IS NOT OLD.mark_id +BEGIN UPDATE task_attention SET assignment_revision = assignment_revision + 1 WHERE conversation_id = NEW.conversation_id; END; +CREATE TRIGGER task_attention_assignment_insert AFTER INSERT ON task_attention WHEN NEW.mark_id IS NOT NULL AND NEW.assignment_revision = 0 +BEGIN UPDATE task_attention SET assignment_revision = 1 WHERE conversation_id = NEW.conversation_id; END; +ALTER TABLE task_marks ADD COLUMN revision INTEGER NOT NULL DEFAULT 0 CHECK (revision >= 0); +CREATE TRIGGER task_marks_revision AFTER UPDATE OF name, description, color ON task_marks +WHEN NEW.revision = OLD.revision AND (NEW.name IS NOT OLD.name OR NEW.description IS NOT OLD.description OR NEW.color IS NOT OLD.color) +BEGIN UPDATE task_marks SET revision = revision + 1 WHERE id = NEW.id; END; +` diff --git a/apps/buddy/service/src/storage/migrations/persistenceCommandsSchema.ts b/apps/buddy/service/src/storage/migrations/persistenceCommandsSchema.ts new file mode 100644 index 000000000..1fc4e5235 --- /dev/null +++ b/apps/buddy/service/src/storage/migrations/persistenceCommandsSchema.ts @@ -0,0 +1,60 @@ +import { APPROVAL_REVIEWS_SCHEMA_SQL } from './approvalReviewsSchema' +import { CONVERSATION_ARCHIVE_MANAGED_FILES_SCHEMA_SQL } from './conversationArchiveManagedFilesSchema' +import { DRAFT_OWNERSHIP_SCHEMA_SQL } from './draftOwnershipSchema' +import { MANAGED_FILES_SCHEMA_SQL } from './managedFilesSchema' +import { METADATA_COMMANDS_SCHEMA_SQL } from './metadataCommandsSchema' +import { PRODUCT_CONTENT_REFERENCES_SCHEMA_SQL } from './productContentReferencesSchema' + +export const PERSISTENCE_COMMANDS_SCHEMA_SQL = ` +CREATE TABLE persistence_migrations ( + id TEXT PRIMARY KEY CHECK (id = 'legacy-jsonl-v1'), + source_fingerprint TEXT NOT NULL, + source_schema INTEGER NOT NULL, + state TEXT NOT NULL CHECK (state IN ('importing', 'verified', 'activated')), + created_at TEXT NOT NULL +) STRICT; + +CREATE TABLE persistence_migration_entries ( + migration_id TEXT NOT NULL REFERENCES persistence_migrations(id), + kind TEXT NOT NULL CHECK (kind IN ('run', 'file', 'pi', 'draft', 'alias')), + source_id TEXT NOT NULL, + source_fingerprint TEXT NOT NULL, + evidence_json TEXT NOT NULL CHECK (json_valid(evidence_json)), + PRIMARY KEY (migration_id, kind, source_id) +) STRICT; + +CREATE TABLE legacy_request_aliases ( + namespace TEXT NOT NULL, + request_id TEXT NOT NULL, + target_json TEXT NOT NULL CHECK (json_valid(target_json)), + source_fingerprint TEXT NOT NULL, + PRIMARY KEY (namespace, request_id) +) STRICT; + +CREATE TRIGGER runs_new_event_sequence AFTER INSERT ON runs +WHEN NEW.next_event_sequence IS NULL AND (SELECT authority FROM storage_profile WHERE id = 1) = 'sqlite' +BEGIN UPDATE runs SET next_event_sequence = 1 WHERE id = NEW.id; END; +ALTER TABLE chat_queue ADD COLUMN revision INTEGER NOT NULL DEFAULT 0 CHECK (revision >= 0); +CREATE TRIGGER chat_queue_revision AFTER UPDATE OF state, prepared_json, run_id ON chat_queue +WHEN NEW.revision = OLD.revision AND (NEW.state IS NOT OLD.state OR NEW.prepared_json IS NOT OLD.prepared_json OR NEW.run_id IS NOT OLD.run_id) +BEGIN UPDATE chat_queue SET revision = revision + 1 WHERE id = NEW.id; END; +CREATE TABLE chat_queue_deliveries ( + queue_id TEXT PRIMARY KEY REFERENCES chat_queue(id) ON DELETE CASCADE, + run_id TEXT NOT NULL REFERENCES runs(id), + stage TEXT NOT NULL CHECK (stage IN ('committed', 'submitted', 'unavailable', 'unknown')), + committed_at TEXT NOT NULL, + observed_at TEXT +) STRICT; +CREATE TABLE service_admission_gates ( + source_instance_id TEXT PRIMARY KEY NOT NULL, + stopped_at TEXT NOT NULL, + command_id TEXT NOT NULL +) STRICT; + +${METADATA_COMMANDS_SCHEMA_SQL} +${APPROVAL_REVIEWS_SCHEMA_SQL} +${DRAFT_OWNERSHIP_SCHEMA_SQL} +${MANAGED_FILES_SCHEMA_SQL} +${PRODUCT_CONTENT_REFERENCES_SCHEMA_SQL} +${CONVERSATION_ARCHIVE_MANAGED_FILES_SCHEMA_SQL} +` diff --git a/apps/buddy/service/src/storage/__tests__/experimentalPersistenceSchema.ts b/apps/buddy/service/src/storage/migrations/persistenceIdentitySchema.ts similarity index 98% rename from apps/buddy/service/src/storage/__tests__/experimentalPersistenceSchema.ts rename to apps/buddy/service/src/storage/migrations/persistenceIdentitySchema.ts index e2c861442..f6998e60c 100644 --- a/apps/buddy/service/src/storage/__tests__/experimentalPersistenceSchema.ts +++ b/apps/buddy/service/src/storage/migrations/persistenceIdentitySchema.ts @@ -1,4 +1,4 @@ -export const EXPERIMENTAL_PERSISTENCE_SCHEMA_SQL = ` +export const PERSISTENCE_IDENTITY_SCHEMA_SQL = ` CREATE TABLE storage_actors ( id TEXT PRIMARY KEY NOT NULL CHECK (length(id) > 0), kind TEXT NOT NULL CHECK (kind IN ('desktop', 'device')), diff --git a/apps/buddy/service/src/storage/migrations/privateNetworkSchema.ts b/apps/buddy/service/src/storage/migrations/privateNetworkSchema.ts new file mode 100644 index 000000000..cd290e71b --- /dev/null +++ b/apps/buddy/service/src/storage/migrations/privateNetworkSchema.ts @@ -0,0 +1,71 @@ +export const PRIVATE_NETWORK_TABLE_COLUMNS = { + private_network_state: ['id', 'enabled', 'bind_address', 'port', 'tls_pin', 'last_observed_ms'], + pairing_tickets: ['id', 'secret_hash', 'issuer_instance_id', 'state', 'expires_ms', 'attempt_id'], + pairing_attempts: ['id', 'ticket_id', 'recovery_hash', 'device_name', 'expires_ms', 'device_id', 'closed_ms', 'credential_count'], + remote_devices: ['id', 'owner_actor_id', 'data_generation', 'state', 'grant_generation', 'created_ms', 'confirmed_ms', 'last_seen_ms', 'pending_expires_ms', 'idle_expires_ms', 'absolute_expires_ms'], + remote_credentials: ['id', 'device_id', 'secret_hash', 'grant_generation', 'created_ms'], +} as const +export const PRIVATE_NETWORK_TRIGGER_NAMES = ['remote_devices_terminal', 'remote_devices_generation', 'remote_credentials_immutable', 'pairing_ticket_terminal'] as const +export const PRIVATE_NETWORK_SCHEMA_SQL = ` +CREATE TABLE private_network_state ( + id INTEGER PRIMARY KEY CHECK (id = 1), + enabled INTEGER NOT NULL DEFAULT 0 CHECK (enabled IN (0,1)), + bind_address TEXT, + port INTEGER CHECK (port BETWEEN 1 AND 65535), + tls_pin TEXT, + last_observed_ms INTEGER NOT NULL DEFAULT 0 CHECK (last_observed_ms >= 0) +) STRICT; +INSERT INTO private_network_state(id) VALUES (1); +CREATE TABLE pairing_tickets ( + id TEXT PRIMARY KEY NOT NULL, + secret_hash TEXT NOT NULL UNIQUE, + issuer_instance_id TEXT NOT NULL, + state TEXT NOT NULL CHECK (state IN ('pending','consumed','cancelled','expired')), + expires_ms INTEGER NOT NULL, + attempt_id TEXT +) STRICT; +CREATE UNIQUE INDEX pairing_tickets_pending ON pairing_tickets(state) WHERE state = 'pending'; +CREATE TABLE pairing_attempts ( + id TEXT PRIMARY KEY NOT NULL, + ticket_id TEXT NOT NULL REFERENCES pairing_tickets(id), + recovery_hash TEXT NOT NULL UNIQUE, + device_name TEXT NOT NULL, + expires_ms INTEGER NOT NULL, + device_id TEXT, + closed_ms INTEGER, + credential_count INTEGER NOT NULL DEFAULT 0 CHECK (credential_count BETWEEN 0 AND 5) +) STRICT; +CREATE TABLE remote_devices ( + id TEXT PRIMARY KEY NOT NULL REFERENCES storage_actors(id), + owner_actor_id TEXT NOT NULL REFERENCES storage_actors(id), + data_generation TEXT NOT NULL, + state TEXT NOT NULL CHECK (state IN ('pending','active','revoked','expired')), + name TEXT NOT NULL, + grant_generation INTEGER NOT NULL DEFAULT 1 CHECK (grant_generation >= 1), + created_ms INTEGER NOT NULL, + confirmed_ms INTEGER, + last_seen_ms INTEGER, + pending_expires_ms INTEGER NOT NULL, + idle_expires_ms INTEGER NOT NULL, + absolute_expires_ms INTEGER NOT NULL +) STRICT; +CREATE TABLE remote_credentials ( + id TEXT PRIMARY KEY NOT NULL, + device_id TEXT NOT NULL REFERENCES remote_devices(id), + secret_hash TEXT NOT NULL UNIQUE, + grant_generation INTEGER NOT NULL, + created_ms INTEGER NOT NULL +) STRICT; +CREATE INDEX remote_credentials_device ON remote_credentials(device_id); +CREATE TRIGGER remote_devices_terminal BEFORE UPDATE OF state ON remote_devices +WHEN (OLD.state IN ('revoked','expired') AND NEW.state != OLD.state) OR (OLD.state = 'active' AND NEW.state = 'pending') +BEGIN SELECT RAISE(ABORT, 'DEVICE_STATE_TERMINAL'); END; +CREATE TRIGGER remote_devices_generation BEFORE UPDATE OF grant_generation ON remote_devices +WHEN NEW.grant_generation < OLD.grant_generation +BEGIN SELECT RAISE(ABORT, 'DEVICE_GENERATION_REGRESSION'); END; +CREATE TRIGGER remote_credentials_immutable BEFORE UPDATE ON remote_credentials +BEGIN SELECT RAISE(ABORT, 'CREDENTIAL_IMMUTABLE'); END; +CREATE TRIGGER pairing_ticket_terminal BEFORE UPDATE OF state ON pairing_tickets +WHEN OLD.state != 'pending' AND NEW.state != OLD.state +BEGIN SELECT RAISE(ABORT, 'PAIRING_TICKET_TERMINAL'); END; +` diff --git a/apps/buddy/service/src/storage/migrations/productContentReferencesSchema.ts b/apps/buddy/service/src/storage/migrations/productContentReferencesSchema.ts new file mode 100644 index 000000000..1c4c74294 --- /dev/null +++ b/apps/buddy/service/src/storage/migrations/productContentReferencesSchema.ts @@ -0,0 +1,26 @@ +export const PRODUCT_CONTENT_REFERENCES_SCHEMA_SQL = ` +CREATE TABLE product_content_references ( + id TEXT PRIMARY KEY NOT NULL, + owner_actor_id TEXT NOT NULL REFERENCES storage_actors(id), + data_generation TEXT NOT NULL, + quote_json TEXT NOT NULL CHECK (json_valid(quote_json) AND json_type(quote_json) = 'object'), + created_at TEXT NOT NULL +) STRICT; +CREATE TRIGGER product_content_references_no_replace BEFORE INSERT ON product_content_references +WHEN EXISTS (SELECT 1 FROM product_content_references WHERE id = NEW.id) +BEGIN SELECT RAISE(ABORT, 'CONTENT_REFERENCE_IMMUTABLE'); END; +CREATE TRIGGER product_content_references_no_update BEFORE UPDATE ON product_content_references +BEGIN SELECT RAISE(ABORT, 'CONTENT_REFERENCE_IMMUTABLE'); END; +CREATE TRIGGER product_content_references_no_delete BEFORE DELETE ON product_content_references +BEGIN SELECT RAISE(ABORT, 'CONTENT_REFERENCE_IMMUTABLE'); END; +CREATE TRIGGER product_content_references_identity BEFORE INSERT ON product_content_references +WHEN NOT EXISTS (SELECT 1 FROM storage_actors WHERE id = NEW.owner_actor_id AND state = 'active') + OR NOT EXISTS (SELECT 1 FROM storage_profile WHERE id = 1 AND data_generation = NEW.data_generation AND authority = 'sqlite' AND migration_state = 'ready') +BEGIN SELECT RAISE(ABORT, 'CONTENT_REFERENCE_OWNER_UNAVAILABLE'); END; +` +export const PRODUCT_CONTENT_REFERENCES_TRIGGER_NAMES = [ + 'product_content_references_no_replace', + 'product_content_references_no_update', + 'product_content_references_no_delete', + 'product_content_references_identity', +] as const diff --git a/apps/buddy/service/src/storage/migrations/productQueryRevisionsSchema.ts b/apps/buddy/service/src/storage/migrations/productQueryRevisionsSchema.ts new file mode 100644 index 000000000..189c05b32 --- /dev/null +++ b/apps/buddy/service/src/storage/migrations/productQueryRevisionsSchema.ts @@ -0,0 +1,132 @@ +export const PRODUCT_QUERY_REVISION_TABLES = [ + 'approvals', + 'approval_reviews', + 'approval_decisions', + 'artifacts', + 'attachments', + 'automation_occurrences', + 'chat_queue', + 'composer_drafts', + 'composer_resources', + 'conversation_branches', + 'conversation_publications', + 'conversations', + 'managed_files', + 'messages', + 'run_events', + 'run_inputs', + 'runs', + 'spaces', + 'task_attention', + 'task_marks', + 'user_questions', +] as const + +type Row = 'OLD' | 'NEW' +interface RevisionSource { + name: string + table: string + owners: (row: Row) => string + columns?: readonly string[] + visible?: (row: Row) => string + history?: (row: Row) => string +} +const task = (id: string) => `SELECT ${id} AS conversation_id, NULL AS branch_id, NULL AS run_id` +const branch = (row: string) => `SELECT ${row}.conversation_id, ${row}.branch_id, NULL AS run_id` +const run = (id: string) => `SELECT conversation_id, branch_id, id AS run_id FROM runs WHERE id = ${id}` +const owners: Record string> = { + approvals: row => run(`${row}.run_id`), + approval_reviews: row => `SELECT r.conversation_id, r.branch_id, r.id AS run_id FROM approvals a JOIN runs r ON r.id = a.run_id WHERE a.id = ${row}.approval_id`, + approval_decisions: row => `SELECT r.conversation_id, r.branch_id, r.id AS run_id FROM approvals a JOIN runs r ON r.id = a.run_id WHERE a.id = ${row}.approval_id`, + artifacts: row => task(`${row}.conversation_id`), + attachments: row => `SELECT conversation_id, branch_id, NULL AS run_id FROM messages WHERE id = ${row}.message_id`, + automation_occurrences: row => task(`${row}.conversation_id`), + chat_queue: branch, + composer_drafts: branch, + composer_resources: row => `SELECT conversation_id, branch_id, NULL AS run_id FROM composer_drafts WHERE id = ${row}.draft_id`, + conversation_branches: row => `SELECT ${row}.conversation_id, ${row}.id AS branch_id, NULL AS run_id`, + conversation_publications: branch, + conversations: row => task(`${row}.id`), + managed_files: row => `SELECT m.conversation_id, m.branch_id, NULL AS run_id FROM attachments a JOIN messages m ON m.id = a.message_id WHERE a.stored_path = ${row}.path`, + messages: branch, + run_events: row => run(`${row}.run_id`), + run_inputs: row => run(`${row}.run_id`), + runs: row => `SELECT ${row}.conversation_id, ${row}.branch_id, ${row}.id AS run_id`, + spaces: () => 'SELECT NULL AS conversation_id, NULL AS branch_id, NULL AS run_id WHERE 0', + task_attention: row => task(`${row}.conversation_id`), + task_marks: () => 'SELECT NULL AS conversation_id, NULL AS branch_id, NULL AS run_id WHERE 0', + user_questions: row => run(`${row}.run_id`), +} + +const sources: RevisionSource[] = [ + ...PRODUCT_QUERY_REVISION_TABLES.map(table => ({ name: table, table, owners: owners[table] })), + { name: 'conversation_visibility', table: 'conversations', owners: owners.conversations, columns: ['id', 'deleted_at'] }, + { name: 'run_summaries', table: 'runs', owners: owners.runs, columns: ['id', 'conversation_id', 'branch_id', 'status', 'execution_kind', 'purpose', 'started_at', 'completed_at', 'generation'] }, + { name: 'run_history', table: 'runs', owners: owners.runs, columns: ['id', 'conversation_id', 'branch_id', 'execution_kind', 'actor_snapshot_json'] }, + { name: 'history_tools', table: 'run_events', owners: owners.run_events, visible: row => `${row}.event_type = 'tool.completed'` }, + { name: 'message_summaries', table: 'messages', owners: owners.messages, visible: row => `${row}.role IN ('user', 'assistant') AND (${row}.run_id IS NULL OR EXISTS (SELECT 1 FROM runs WHERE id = ${row}.run_id AND execution_kind IN ('root', 'actor_child')))` }, + { name: 'publication_summaries', table: 'conversation_publications', owners: owners.conversation_publications, visible: row => `${row}.kind = 'message'` }, +] +function attachmentReference(content: string, id: string) { + const json = `CASE WHEN json_valid(${content}) THEN ${content} ELSE '{}' END` + return `EXISTS (SELECT 1 FROM json_each(${json}, '$.resourceSnapshots') snapshot + WHERE CASE WHEN snapshot.type = 'object' THEN json_extract(snapshot.value, '$.attachmentId') END = ${id}) + OR EXISTS (SELECT 1 FROM json_each(${json}, '$.attachmentIds') reference WHERE reference.value = ${id})` +} +const history: Record string> = { + messages: row => `SELECT ${row}.id AS id`, + conversation_publications: row => `SELECT ${row}.message_id AS id WHERE ${row}.kind = 'message'`, + run_history: row => `SELECT id FROM messages WHERE run_id = ${row}.id`, + history_tools: row => `SELECT id FROM messages WHERE run_id = ${row}.run_id AND role = 'tool'`, + attachments: row => `SELECT id FROM messages WHERE id = ${row}.message_id OR ${attachmentReference('messages.content_json', `${row}.id`)}`, + managed_files: row => `SELECT DISTINCT m.id FROM attachments a JOIN messages m ON m.id = a.message_id + OR ${attachmentReference('m.content_json', 'a.id')} + WHERE a.stored_path = ${row}.path`, +} +for (const source of sources) source.history = history[source.name] +const operations = ['insert', 'update', 'delete'] as const +export const SCOPED_QUERY_REVISION_SOURCES = [...sources.map(source => source.name), 'history_messages', 'history_branches'] +export const SCOPED_QUERY_REVISION_TRIGGER_NAMES = sources.flatMap(source => operations.map(operation => `product_query_revision_${source.name}_${operation}`)) + +function scopes(source: RevisionSource, row: Row) { + const select = source.owners(row) + return `SELECT '' AS scope + UNION SELECT json_array('task', conversation_id) FROM (${select}) WHERE conversation_id IS NOT NULL + UNION SELECT json_array('branch', conversation_id, branch_id) FROM (${select}) WHERE conversation_id IS NOT NULL AND branch_id IS NOT NULL + UNION SELECT json_array('run', run_id) FROM (${select}) WHERE run_id IS NOT NULL` +} + +export const PRODUCT_QUERY_REVISIONS_SCHEMA_SQL = ` +CREATE TABLE product_query_revisions ( + table_name TEXT NOT NULL, + scope TEXT NOT NULL DEFAULT '', + revision INTEGER NOT NULL DEFAULT 0 CHECK (revision >= 0), + PRIMARY KEY (table_name, scope) +); +${SCOPED_QUERY_REVISION_SOURCES.map(name => `INSERT OR IGNORE INTO product_query_revisions (table_name) VALUES ('${name}');`).join('\n')} +${sources.flatMap(source => operations.map((operation) => { + const rows: Row[] = operation === 'insert' ? ['NEW'] : operation === 'delete' ? ['OLD'] : ['OLD', 'NEW'] + const changed = operation === 'update' && source.columns ? `(${source.columns.map(column => `NEW.${column} IS NOT OLD.${column}`).join(' OR ')})` : '1' + const visible = source.visible ? `(${rows.map(row => `(${source.visible!(row)})`).join(' OR ')})` : '1' + return `CREATE TRIGGER product_query_revision_${source.name}_${operation} AFTER ${operation.toUpperCase()} ON ${source.table} + WHEN ${changed} AND ${visible} + BEGIN + INSERT INTO product_query_revisions (table_name, scope, revision) + SELECT '${source.name}', scope, 1 FROM (${rows.map(row => scopes(source, row)).join(' UNION ')}) WHERE 1 + ON CONFLICT (table_name, scope) DO UPDATE SET revision = revision + 1; + ${source.history + ? `INSERT INTO product_query_revisions (table_name, scope, revision) + SELECT 'history_messages', json_array('message', id), 1 FROM (${rows.map(row => source.history!(row)).join(' UNION ')}) WHERE id IS NOT NULL + ON CONFLICT (table_name, scope) DO UPDATE SET revision = revision + 1; + INSERT INTO product_query_revisions (table_name, scope, revision) + ${source.name === 'messages' + ? `SELECT 'history_branches', scope, 1 + FROM (${rows.map(row => `SELECT json_array('branch', ${row}.conversation_id, ${row}.branch_id) AS scope`).join(' UNION ')}) WHERE 1` + : `SELECT DISTINCT 'history_branches', json_array('branch', m.conversation_id, m.branch_id), 1 + FROM messages m WHERE m.id IN (${rows.map(row => source.history!(row)).join(' UNION ')}) + `} + ON CONFLICT (table_name, scope) DO UPDATE SET revision = revision + 1;` + : ''} + END;` +})).join('\n')} +` diff --git a/apps/buddy/service/src/storage/migrations/remoteFilesSchema.ts b/apps/buddy/service/src/storage/migrations/remoteFilesSchema.ts new file mode 100644 index 000000000..8f490d38f --- /dev/null +++ b/apps/buddy/service/src/storage/migrations/remoteFilesSchema.ts @@ -0,0 +1,46 @@ +export const REMOTE_FILES_SCHEMA_SQL = ` +CREATE TABLE remote_uploads ( + id TEXT PRIMARY KEY NOT NULL, + actor_id TEXT NOT NULL REFERENCES storage_actors(id), + data_generation TEXT NOT NULL, + grant_generation INTEGER NOT NULL, + draft_id TEXT NOT NULL, + expected_revision INTEGER NOT NULL, + file_id TEXT NOT NULL UNIQUE REFERENCES managed_files(id), + name TEXT NOT NULL, + mime_type TEXT NOT NULL, + size_bytes INTEGER NOT NULL CHECK (size_bytes >= 0), + sha256 TEXT NOT NULL CHECK (length(sha256) = 64), + state TEXT NOT NULL CHECK (state IN ('receiving', 'ready', 'adopted', 'cancelled', 'expired', 'failed')), + confirmed_bytes INTEGER NOT NULL DEFAULT 0 CHECK (confirmed_bytes >= 0 AND confirmed_bytes <= size_bytes), + resource_id TEXT NOT NULL UNIQUE, + attachment_id TEXT NOT NULL UNIQUE, + created_ms INTEGER NOT NULL, + expires_ms INTEGER NOT NULL, + input_id TEXT +) STRICT; +CREATE INDEX remote_uploads_owner ON remote_uploads(actor_id, state); +CREATE TABLE remote_upload_chunks ( + upload_id TEXT NOT NULL REFERENCES remote_uploads(id), + start INTEGER NOT NULL, + length INTEGER NOT NULL CHECK (length > 0), + sha256 TEXT NOT NULL CHECK (length(sha256) = 64), + PRIMARY KEY (upload_id, start) +) STRICT; +CREATE TRIGGER remote_upload_identity BEFORE UPDATE OF id, actor_id, data_generation, grant_generation, draft_id, expected_revision, file_id, name, mime_type, size_bytes, sha256, resource_id, attachment_id, created_ms, expires_ms ON remote_uploads +BEGIN SELECT RAISE(ABORT, 'UPLOAD_IDENTITY_IMMUTABLE'); END; +CREATE TRIGGER remote_upload_terminal BEFORE UPDATE OF state ON remote_uploads +WHEN (OLD.state IN ('adopted', 'cancelled', 'expired', 'failed') AND NEW.state <> OLD.state) + OR (OLD.state = 'ready' AND NEW.state = 'receiving') +BEGIN SELECT RAISE(ABORT, 'UPLOAD_STATE_TERMINAL'); END; +CREATE TRIGGER remote_upload_prefix BEFORE UPDATE OF confirmed_bytes ON remote_uploads +WHEN NEW.confirmed_bytes < OLD.confirmed_bytes +BEGIN SELECT RAISE(ABORT, 'UPLOAD_PREFIX_REGRESSION'); END; +CREATE TRIGGER remote_upload_chunk_immutable BEFORE UPDATE ON remote_upload_chunks +BEGIN SELECT RAISE(ABORT, 'UPLOAD_CHUNK_IMMUTABLE'); END; +CREATE TRIGGER remote_upload_input_immutable BEFORE UPDATE OF input_id ON remote_uploads +BEGIN SELECT RAISE(ABORT, 'UPLOAD_IDENTITY_IMMUTABLE'); END; +` +export const REMOTE_FILES_TABLE_COLUMNS = { remote_uploads: ['id', 'actor_id', 'data_generation', 'grant_generation', 'file_id', 'state', 'confirmed_bytes', 'sha256', 'input_id'], remote_upload_chunks: ['upload_id', 'start', 'length', 'sha256'] } +export const REMOTE_FILES_TRIGGER_NAMES = ['remote_upload_identity', 'remote_upload_terminal', 'remote_upload_prefix', 'remote_upload_chunk_immutable', 'remote_upload_input_immutable'] +export const REMOTE_FILES_INDEX_NAMES = ['remote_uploads_owner'] diff --git a/apps/buddy/service/src/storage/migrations/resourceExportsSchema.ts b/apps/buddy/service/src/storage/migrations/resourceExportsSchema.ts new file mode 100644 index 000000000..ebba1cd83 --- /dev/null +++ b/apps/buddy/service/src/storage/migrations/resourceExportsSchema.ts @@ -0,0 +1,25 @@ +export const RESOURCE_EXPORTS_SCHEMA_SQL = ` +CREATE TABLE remote_exports ( + id TEXT PRIMARY KEY NOT NULL, + actor_id TEXT NOT NULL REFERENCES storage_actors(id), + data_generation TEXT NOT NULL, + grant_generation INTEGER NOT NULL, + source_id TEXT NOT NULL, + source_version TEXT NOT NULL, + file_id TEXT NOT NULL REFERENCES managed_files(id), + name TEXT NOT NULL, + mime_type TEXT NOT NULL, + size_bytes INTEGER NOT NULL CHECK (size_bytes >= 0), + state TEXT NOT NULL CHECK (state IN ('preparing','ready','expired','failed')), + expires_ms INTEGER NOT NULL +) STRICT; +CREATE INDEX remote_exports_owner ON remote_exports(actor_id, state, expires_ms); +CREATE TRIGGER remote_export_identity BEFORE UPDATE OF id,actor_id,data_generation,grant_generation,source_id,source_version,file_id,name,mime_type,size_bytes,expires_ms ON remote_exports +BEGIN SELECT RAISE(ABORT, 'RESOURCE_IDENTITY_IMMUTABLE'); END; +CREATE TRIGGER remote_export_terminal BEFORE UPDATE OF state ON remote_exports +WHEN (OLD.state IN ('expired','failed') AND NEW.state <> OLD.state) OR (OLD.state = 'ready' AND NEW.state = 'preparing') +BEGIN SELECT RAISE(ABORT, 'RESOURCE_STATE_TERMINAL'); END; +` +export const RESOURCE_EXPORT_TABLE_COLUMNS = { remote_exports: ['id', 'actor_id', 'source_id', 'source_version', 'file_id', 'state', 'expires_ms'] } +export const RESOURCE_EXPORT_TRIGGER_NAMES = ['remote_export_identity', 'remote_export_terminal'] +export const RESOURCE_EXPORT_INDEX_NAMES = ['remote_exports_owner'] diff --git a/apps/buddy/service/src/storage/migrations/v43Persistence.ts b/apps/buddy/service/src/storage/migrations/v43Persistence.ts new file mode 100644 index 000000000..46cf9939b --- /dev/null +++ b/apps/buddy/service/src/storage/migrations/v43Persistence.ts @@ -0,0 +1,28 @@ +import { BRANCH_EXECUTION_SCHEMA_SQL } from './branchExecutionSchema' +import { PERSISTENCE_COMMANDS_SCHEMA_SQL } from './persistenceCommandsSchema' +import { PERSISTENCE_IDENTITY_SCHEMA_SQL } from './persistenceIdentitySchema' +import { PRIVATE_NETWORK_SCHEMA_SQL } from './privateNetworkSchema' +import { PRODUCT_QUERY_REVISIONS_SCHEMA_SQL } from './productQueryRevisionsSchema' +import { REMOTE_FILES_SCHEMA_SQL } from './remoteFilesSchema' +import { RESOURCE_EXPORTS_SCHEMA_SQL } from './resourceExportsSchema' + +export const BUDDY_PERSISTENCE_SCHEMA_LINEAGE = 'persistence-v43' as const +const PERSISTENCE_LINEAGE_SCHEMA_SQL = ` +DROP TABLE buddy_schema_lineage; +CREATE TABLE buddy_schema_lineage ( + id INTEGER PRIMARY KEY CHECK (id = 1), + lineage TEXT NOT NULL CHECK (lineage = '${BUDDY_PERSISTENCE_SCHEMA_LINEAGE}') +) STRICT; +INSERT INTO buddy_schema_lineage VALUES (1, '${BUDDY_PERSISTENCE_SCHEMA_LINEAGE}'); +` + +export const BUDDY_V43_PERSISTENCE_SCHEMA_SQL = [ + PERSISTENCE_IDENTITY_SCHEMA_SQL, + PERSISTENCE_COMMANDS_SCHEMA_SQL, + PRIVATE_NETWORK_SCHEMA_SQL, + REMOTE_FILES_SCHEMA_SQL, + RESOURCE_EXPORTS_SCHEMA_SQL, + BRANCH_EXECUTION_SCHEMA_SQL, + PRODUCT_QUERY_REVISIONS_SCHEMA_SQL, + PERSISTENCE_LINEAGE_SCHEMA_SQL, +].join('\n') diff --git a/apps/buddy/service/src/storage/persistenceMigration/__tests__/persistenceCandidate.spec.ts b/apps/buddy/service/src/storage/persistenceMigration/__tests__/persistenceCandidate.spec.ts new file mode 100644 index 000000000..64241cc42 --- /dev/null +++ b/apps/buddy/service/src/storage/persistenceMigration/__tests__/persistenceCandidate.spec.ts @@ -0,0 +1,196 @@ +import type { BuddyRunEvent } from '../../../events/BuddyRunEvent' +import { mkdir, mkdtemp, readFile, rm, unlink, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { dirname, join } from 'node:path' +import { afterEach, describe, expect, it } from 'vitest' +import { createRunEventLog } from '../../../events/createRunEventLog' +import { RunEventQueries } from '../../../events/RunEventQueries' +import { SqliteRunEventLog } from '../../../events/SqliteRunEventLog' +import { openBuddyDatabase, openBuddyMigrationCandidate } from '../../database' +import { activatePersistenceCandidate } from '../activatePersistenceCandidate' +import { importPersistenceCandidate } from '../importPersistenceCandidate' +import { parseLegacyRunHistory, reconcileLegacyRunHistory } from '../legacyRunHistory' + +const roots: string[] = [] +const now = '2026-11-06T00:00:00.000Z' +afterEach(async () => Promise.all(roots.splice(0).map(root => rm(root, { recursive: true, force: true })))) + +async function sourceFixture(options: { compact?: boolean, attachment?: boolean, pi?: boolean } = {}) { + const directory = await mkdtemp(join(tmpdir(), 'lexora-persistence-candidate-')) + roots.push(directory) + const sourceRoot = join(directory, 'original') + const candidateRoot = join(directory, 'candidate') + const databasePath = join(sourceRoot, 'buddy/buddy.sqlite3') + const database = openBuddyDatabase({ databasePath }) + database.exec(` + INSERT INTO conversations (id, created_at, updated_at) VALUES ('task', '${now}', '${now}'); + INSERT INTO conversation_branches (id, conversation_id, created_at) VALUES ('branch', 'task', '${now}'); + UPDATE conversations SET active_branch_id = 'branch' WHERE id = 'task'; + INSERT INTO messages (id, conversation_id, branch_id, role, content_json, created_at) + VALUES ('input', 'task', 'branch', 'user', '{"text":"fixture"}', '${now}'); + INSERT INTO runs (id, conversation_id, branch_id, triggering_message_id, provider, model, purpose, status, started_at) + VALUES ('run', 'task', 'branch', 'input', 'fixture', 'fixture', 'chat', 'queued', '${now}'); + INSERT INTO turn_requests (request_id, request_fingerprint, conversation_id, branch_id, run_id, created_at) + VALUES ('same-id', 'old-turn', 'task', 'branch', 'run', '${now}'); + INSERT INTO command_requests (request_id, request_fingerprint, conversation_id, branch_id, run_id, command, arguments, created_at) + VALUES ('same-id', 'old-command', 'task', 'branch', 'run', 'compact', '', '${now}'); + `) + const eventFile = join(sourceRoot, 'buddy/conversations/task/events/run.jsonl') + const log = createRunEventLog({ database, conversationsDirectory: join(sourceRoot, 'buddy/conversations') }) + await log.appendBatch([ + { runId: 'run', type: 'run.queued', payload: {}, createdAt: now }, + { runId: 'run', type: 'message.delta', payload: { messageId: 'answer', delta: 'hello' }, createdAt: now }, + { runId: 'run', type: 'future.event', payload: { untouched: true }, createdAt: now }, + { runId: 'run', type: 'message.completed', payload: { messageId: 'answer', role: 'assistant', content: { text: 'hello' }, stopReason: 'completed' }, createdAt: now }, + { runId: 'run', type: 'run.completed', payload: {}, createdAt: now }, + ]) + if (options.compact) + await log.compactTerminalRun('run') + if (options.attachment) { + const path = join(sourceRoot, 'buddy/conversations/task/inputs/input/attachment.txt') + await mkdir(dirname(path), { recursive: true }) + await writeFile(path, 'attachment') + database.prepare(`INSERT INTO attachments (id, message_id, draft_id, name, mime_type, size_bytes, stored_path, source_path, created_at) + VALUES ('attachment', 'input', NULL, 'attachment.txt', 'text/plain', 10, ?, NULL, ?)`) + .run(path, now) + } + if (options.pi) { + const path = join(sourceRoot, 'buddy/conversations/task/session/branch/session.jsonl') + await mkdir(dirname(path), { recursive: true }) + await writeFile(path, `${JSON.stringify({ type: 'session', id: 'session', version: 3, cwd: join(sourceRoot, 'buddy/conversations/task/workspace') })}\n${JSON.stringify({ type: 'message', id: 'entry', parentId: null, message: { role: 'user', content: 'fixture' } })}\n`) + database.prepare('UPDATE runs SET pi_session_file = ? WHERE id = ?').run(path, 'run') + } + await writeFile(join(sourceRoot, 'buddy/unknown-preserved.bin'), 'unknown bytes') + await log.close() + database.close() + return { sourceRoot, candidateRoot, databasePath, eventFile, leaseDirectory: join(directory, 'locks'), signal: new AbortController().signal } +} + +describe('persistence candidate migration', () => { + it('limits in-place upgrades to owned data while leaving independent stores outside the candidate', async () => { + const fixture = await sourceFixture({ attachment: true, pi: true }) + const excluded = ['buddy/unknown-preserved.bin', 'buddy/extensions/inference/model.bin', 'buddy/backups/old.bin', 'buddy/themes/custom.json', 'device/chromium/Cache/page', 'recovery/old/data', 'config.toml'] + for (const path of excluded) { + await mkdir(dirname(join(fixture.sourceRoot, path)), { recursive: true }) + await writeFile(join(fixture.sourceRoot, path), 'untouched') + } + const result = await activatePersistenceCandidate({ ...fixture, scope: 'upgrade' }) + const manifest = JSON.parse(await readFile(join(fixture.candidateRoot, '.persistence-migration.json'), 'utf8')) + expect(manifest.scope).toBe('upgrade') + for (const path of excluded) { + expect(manifest.entries.some((entry: { path: string }) => entry.path === path)).toBe(false) + await expect(readFile(join(result.profileRoot, path))).rejects.toMatchObject({ code: 'ENOENT' }) + expect(await readFile(join(fixture.sourceRoot, path), 'utf8')).toBe('untouched') + } + expect(result).toMatchObject({ runs: 1, files: 1 }) + await expect(activatePersistenceCandidate(fixture)).rejects.toThrow('MIGRATION_SOURCE_CHANGED') + }) + + it('preserves unknown facts, aliases, bytes and Pi continuity and can resume verification', async () => { + const fixture = await sourceFixture({ compact: true, attachment: true, pi: true }) + const original = await readFile(fixture.databasePath) + const events = await readFile(fixture.eventFile) + const result = await importPersistenceCandidate(fixture) + expect(result).toMatchObject({ runs: 1, files: 1 }) + expect(await importPersistenceCandidate(fixture)).toEqual(result) + expect(await readFile(fixture.databasePath)).toEqual(original) + expect(await readFile(fixture.eventFile)).toEqual(events) + expect(await readFile(join(result.profileRoot, 'buddy/unknown-preserved.bin'), 'utf8')).toBe('unknown bytes') + const database = openBuddyMigrationCandidate({ databasePath: join(result.profileRoot, 'buddy/buddy.sqlite3') }) + try { + expect(new RunEventQueries(database).read('run').map(event => event.sequence)).toEqual([1, 3, 4, 5]) + expect(database.prepare('SELECT next_event_sequence, status FROM runs').get()).toMatchObject({ next_event_sequence: 6, status: 'completed' }) + expect(database.prepare('SELECT namespace, request_id FROM legacy_request_aliases ORDER BY namespace').all()).toEqual([ + { namespace: 'command_requests', request_id: 'same-id' }, + { namespace: 'turn_requests', request_id: 'same-id' }, + ]) + expect(database.prepare('SELECT COUNT(*) AS count FROM command_receipts').get()?.count).toBe(0) + expect(database.prepare('SELECT authority, migration_state FROM storage_profile').get()).toMatchObject({ authority: 'legacy_jsonl', migration_state: 'pending' }) + expect(database.prepare('SELECT state FROM persistence_migrations').get()?.state).toBe('verified') + expect(database.prepare('SELECT state, size_bytes FROM managed_files').get()).toMatchObject({ state: 'ready', size_bytes: 10 }) + expect(database.prepare('SELECT COUNT(*) AS count FROM file_references').get()?.count).toBeGreaterThan(0) + } + finally { database.close() } + }) + + it('activates only the verified candidate and keeps the source compatible with its old reader', async () => { + const fixture = await sourceFixture({ attachment: true }) + const result = await activatePersistenceCandidate(fixture) + expect(await activatePersistenceCandidate(fixture)).toEqual(result) + const database = openBuddyMigrationCandidate({ databasePath: join(result.profileRoot, 'buddy/buddy.sqlite3') }) + const log = new SqliteRunEventLog({ database }) + try { + expect(await log.read('run')).toHaveLength(5) + expect(database.prepare('SELECT authority, migration_state FROM storage_profile').get()).toMatchObject({ authority: 'sqlite', migration_state: 'ready' }) + expect(() => openBuddyDatabase({ databasePath: join(result.profileRoot, 'buddy/buddy.sqlite3') })).toThrow('newer schema version') + } + finally { + await log.close() + database.close() + } + const original = openBuddyDatabase({ databasePath: fixture.databasePath }) + original.close() + }) + + it('refuses altered candidate files instead of declaring the source copy verified', async () => { + const fixture = await sourceFixture() + const result = await importPersistenceCandidate(fixture) + await writeFile(join(result.profileRoot, 'buddy/unknown-preserved.bin'), 'changed candidate') + await expect(activatePersistenceCandidate(fixture)).rejects.toThrow('MIGRATION_CANDIDATE_CHANGED') + expect(await readFile(join(fixture.sourceRoot, 'buddy/unknown-preserved.bin'), 'utf8')).toBe('unknown bytes') + }) + + it('resumes a committed per-run checkpoint without duplicating facts', async () => { + const fixture = await sourceFixture() + await expect(importPersistenceCandidate({ ...fixture, checkpoint: async (phase) => { + if (phase === 'run') + throw new Error('interrupted') + } })).rejects.toThrow('interrupted') + const result = await importPersistenceCandidate(fixture) + const database = openBuddyMigrationCandidate({ databasePath: join(result.profileRoot, 'buddy/buddy.sqlite3') }) + try { + expect(new RunEventQueries(database).read('run')).toHaveLength(5) + expect(database.prepare(`SELECT COUNT(*) AS count FROM persistence_migration_entries WHERE kind = 'run'`).get()?.count).toBe(1) + } + finally { database.close() } + }) + + it('blocks changed source data on resume and retains both copies', async () => { + const fixture = await sourceFixture() + await importPersistenceCandidate(fixture) + await writeFile(join(fixture.sourceRoot, 'buddy/unknown-preserved.bin'), 'changed') + await expect(importPersistenceCandidate(fixture)).rejects.toThrow('MIGRATION_SOURCE_CHANGED') + expect(await readFile(join(fixture.candidateRoot, 'source/buddy/unknown-preserved.bin'), 'utf8')).toBe('unknown bytes') + }) + + it('blocks missing logs without deleting projected replies', async () => { + const fixture = await sourceFixture() + await unlink(fixture.eventFile) + await expect(importPersistenceCandidate(fixture)).rejects.toThrow('MIGRATION_EVENT_SOURCE_MISSING') + const database = openBuddyMigrationCandidate({ databasePath: join(fixture.candidateRoot, 'profile/buddy/buddy.sqlite3') }) + try { + expect(database.prepare('SELECT id FROM messages WHERE id = ?').get('answer')?.id).toBe('answer') + } + finally { database.close() } + }) + + it('blocks missing attachments before any authority switch', async () => { + const fixture = await sourceFixture({ attachment: true }) + await unlink(join(fixture.sourceRoot, 'buddy/conversations/task/inputs/input/attachment.txt')) + await expect(importPersistenceCandidate(fixture)).rejects.toThrow('MIGRATION_REFERENCE_MISSING') + }) +}) + +const event = (sequence: number, type = 'future.event'): BuddyRunEvent => ({ runId: 'run', sequence, type, payload: {}, createdAt: now }) + +describe('legacy event evidence', () => { + it('accepts a complete last line without newline and rejects incomplete JSON', () => { + expect(parseLegacyRunHistory(JSON.stringify(event(1)), 'run')).toMatchObject({ repairedNewline: true, events: [event(1)] }) + expect(() => parseLegacyRunHistory(`${JSON.stringify(event(1))}\n{"runId":`, 'run')).toThrow('MIGRATION_EVENT_CORRUPTED') + }) + it('does not discard conflicting SQL facts or silently accept sequence holes', () => { + expect(() => reconcileLegacyRunHistory({ events: [event(1, 'run.queued')], projected: [event(1)], certifiedThrough: null, hasProductFacts: true })).toThrow('MIGRATION_EVENT_CONFLICT') + expect(() => reconcileLegacyRunHistory({ events: [event(1), event(3)], projected: [event(1), event(3)], certifiedThrough: null, hasProductFacts: true })).toThrow('MIGRATION_EVENT_GAP_UNVERIFIED') + expect(() => reconcileLegacyRunHistory({ events: [event(1)], projected: [event(1), event(2)], certifiedThrough: 2, hasProductFacts: true })).toThrow('MIGRATION_EVENT_SOURCE_BEHIND') + }) +}) diff --git a/apps/buddy/service/src/storage/persistenceMigration/activatePersistenceCandidate.ts b/apps/buddy/service/src/storage/persistenceMigration/activatePersistenceCandidate.ts new file mode 100644 index 000000000..e947de81e --- /dev/null +++ b/apps/buddy/service/src/storage/persistenceMigration/activatePersistenceCandidate.ts @@ -0,0 +1,79 @@ +import type { PersistenceCandidateInput } from './importPersistenceCandidate' +import { access } from 'node:fs/promises' +import { join, resolve } from 'node:path' +import { DatabaseSync } from 'node:sqlite' +import { isMissing } from '../../../../platform/storage/storageFiles' +import { acquireStorageRootLease, assertStorageRootLease } from '../../../../platform/storage/StorageRootLease' +import { openBuddyMigrationCandidate } from '../database' +import { BUDDY_STORAGE_WRITER_VERSION } from '../schema' +import { withTransaction } from '../transaction' +import { importPersistenceCandidate } from './importPersistenceCandidate' +import { PERSISTENCE_MIGRATION_ID, PersistenceMigrationError } from './legacyRunHistory' +import { readPersistenceSourceManifest } from './persistenceSources' + +export async function activatePersistenceCandidate(input: PersistenceCandidateInput) { + if (input.sourceLease) + assertStorageRootLease(input.sourceLease, input.sourceRoot) + const existing = await readActivatedCandidate(input) + if (existing) + return existing + const candidate = await importPersistenceCandidate(input) + const release = acquireStorageRootLease(candidate.profileRoot, input.leaseDirectory, input.privateDirectoriesExecutable) + try { + const database = openBuddyMigrationCandidate({ databasePath: join(candidate.profileRoot, 'buddy/buddy.sqlite3') }) + try { + input.signal.throwIfAborted() + withTransaction(database, () => { + const migration = database.prepare('SELECT source_fingerprint, state FROM persistence_migrations WHERE id = ?').get(PERSISTENCE_MIGRATION_ID) + if (migration?.state !== 'verified' || migration.source_fingerprint !== candidate.sourceFingerprint) + throw new PersistenceMigrationError('MIGRATION_NOT_VERIFIED') + const result = database.prepare(`UPDATE storage_profile SET authority = 'sqlite', migration_state = 'ready', min_writer_version = ? + WHERE id = 1 AND authority = 'legacy_jsonl' AND migration_state = 'pending'`).run(BUDDY_STORAGE_WRITER_VERSION) + if (result.changes !== 1) + throw new PersistenceMigrationError('MIGRATION_NOT_PENDING') + database.prepare(`UPDATE persistence_migrations SET state = 'activated' WHERE id = ?`).run(PERSISTENCE_MIGRATION_ID) + }) + } + finally { database.close() } + return candidate + } + finally { release() } +} + +async function readActivatedCandidate(input: PersistenceCandidateInput) { + const manifest = await readPersistenceSourceManifest(input.candidateRoot) + if (!manifest || !manifest.workingCopyReady) + return null + if (manifest.scope !== (input.scope ?? 'profile') || manifest.sourceRoot !== resolve(input.sourceRoot)) + throw new PersistenceMigrationError('MIGRATION_SOURCE_CHANGED') + const profileRoot = join(input.candidateRoot, 'profile') + const path = join(profileRoot, 'buddy/buddy.sqlite3') + const exists = await access(path).then(() => true, (error) => { + if (!isMissing(error)) + throw error + return false + }) + if (!exists) + return null + const release = acquireStorageRootLease(profileRoot, input.leaseDirectory, input.privateDirectoriesExecutable) + let database: DatabaseSync | undefined + try { + database = new DatabaseSync(path, { readOnly: true }) + if (!database.prepare('SELECT 1 FROM sqlite_master WHERE name = \'persistence_migrations\'').get()) + return null + const migration = database.prepare('SELECT state, source_fingerprint FROM persistence_migrations WHERE id = ?').get(PERSISTENCE_MIGRATION_ID) + if (migration?.state !== 'activated') + return null + const profile = database.prepare('SELECT authority, migration_state, min_writer_version FROM storage_profile WHERE id = 1').get() + if (migration.source_fingerprint !== manifest.fingerprint || profile?.authority !== 'sqlite' || profile.migration_state !== 'ready' + || profile.min_writer_version !== BUDDY_STORAGE_WRITER_VERSION) { + throw new PersistenceMigrationError('MIGRATION_CANDIDATE_CHANGED') + } + const count = (kind: string) => Number(database!.prepare('SELECT COUNT(*) AS count FROM persistence_migration_entries WHERE migration_id = ? AND kind = ?').get(PERSISTENCE_MIGRATION_ID, kind)!.count) + return { profileRoot, sourceFingerprint: manifest.fingerprint, runs: count('run'), files: count('file') } + } + finally { + database?.close() + release() + } +} diff --git a/apps/buddy/service/src/storage/persistenceMigration/importPersistenceCandidate.ts b/apps/buddy/service/src/storage/persistenceMigration/importPersistenceCandidate.ts new file mode 100644 index 000000000..28a9f70c3 --- /dev/null +++ b/apps/buddy/service/src/storage/persistenceMigration/importPersistenceCandidate.ts @@ -0,0 +1,291 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { StorageReporter } from '../../../../platform/storage/storageTree' +import type { PersistenceSourceManifest } from './persistenceSources' +import { createHash } from 'node:crypto' +import { lstat, readFile, readlink, realpath } from 'node:fs/promises' +import { join, relative } from 'node:path' +import { DatabaseSync as ReadonlyDatabase } from 'node:sqlite' +import { containsStoragePath } from '../../../../platform/storage/storageFiles' +import { acquireStorageRootLease, assertStorageRootLease } from '../../../../platform/storage/StorageRootLease' +import { hashStorageFile } from '../../../../platform/storage/storageTree' +import { RUN_EVENT_PROJECTION_VERSION } from '../../events/RunEventRecovery' +import { createAttachmentPersistenceRepository } from '../attachmentPersistenceRepository' +import { openBuddyMigrationCandidate } from '../database' +import { relocateDatabaseReferences, relocateProfileJournals, relocateStructuredPaths } from '../relocateProfileReferences' +import { withTransaction } from '../transaction' +import { importLegacyRunHistory, PERSISTENCE_MIGRATION_ID, PersistenceMigrationError } from './legacyRunHistory' +import { preparePersistenceSources, preparePersistenceWorkingCopy, readPersistenceSource, verifyPersistenceSources } from './persistenceSources' + +export interface PersistenceCandidateInput { + sourceRoot: string + sourceLease?: () => void + candidateRoot: string + signal: AbortSignal + scope?: PersistenceSourceManifest['scope'] + report?: StorageReporter + leaseDirectory?: string + privateDirectoriesExecutable?: string + checkpoint?: (phase: 'copied' | 'run' | 'files' | 'verified') => Promise +} + +export async function importPersistenceCandidate(input: PersistenceCandidateInput): Promise<{ profileRoot: string, sourceFingerprint: string, runs: number, files: number }> { + if (input.sourceLease) + assertStorageRootLease(input.sourceLease, input.sourceRoot) + const release = input.sourceLease ? undefined : acquireStorageRootLease(input.sourceRoot, input.leaseDirectory, input.privateDirectoriesExecutable) + try { + const releaseCandidate = acquireStorageRootLease(join(input.candidateRoot, 'profile'), input.leaseDirectory, input.privateDirectoriesExecutable) + try { + return await importCandidate(input) + } + finally { releaseCandidate() } + } + finally { release?.() } +} + +async function importCandidate(input: PersistenceCandidateInput): Promise<{ profileRoot: string, sourceFingerprint: string, runs: number, files: number }> { + const manifest = await preparePersistenceSources(input) + const profileRoot = await preparePersistenceWorkingCopy(manifest, input.signal, input.report) + await input.checkpoint?.('copied') + const original = new ReadonlyDatabase(join(manifest.candidateRoot, 'source/buddy/buddy.sqlite3'), { readOnly: true }) + let database: DatabaseSync | undefined + try { + const sourceSchema = Number(original.prepare('PRAGMA user_version').get()!.user_version) + database = openBuddyMigrationCandidate({ databasePath: join(profileRoot, 'buddy/buddy.sqlite3') }) + const previous = database.prepare('SELECT * FROM persistence_migrations WHERE id = ?').get(PERSISTENCE_MIGRATION_ID) + if (previous && previous.source_fingerprint !== manifest.fingerprint) + throw new PersistenceMigrationError('MIGRATION_SOURCE_CHANGED') + if (previous?.state === 'activated') + throw new PersistenceMigrationError('MIGRATION_ALREADY_ACTIVATED') + if (!previous) { + withTransaction(database, () => { + database!.prepare(`INSERT INTO persistence_migrations (id, source_fingerprint, source_schema, state, created_at) + VALUES (?, ?, ?, 'importing', ?)`).run(PERSISTENCE_MIGRATION_ID, manifest.fingerprint, sourceSchema, new Date().toISOString()) + }) + } + relocateDatabaseReferences(database, manifest.sourceRoot, profileRoot) + const runs = database.prepare('SELECT id, conversation_id FROM runs ORDER BY rowid').all() as { id: string, conversation_id: string }[] + const hasCheckpoints = !!original.prepare(`SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'run_event_checkpoints'`).get() + for (const run of runs) { + input.signal.throwIfAborted() + const path = `buddy/conversations/${run.conversation_id}/events/${run.id}.jsonl` + const entry = manifest.entries.find(item => item.path === path) + const checkpoint = hasCheckpoints ? original.prepare('SELECT * FROM run_event_checkpoints WHERE run_id = ?').get(run.id) : undefined + const content = await readPersistenceSource(manifest, path) + const mapped = content === null ? null : mapEventContent(content, manifest.sourceRoot, profileRoot) + const projectedLast = original.prepare('SELECT COALESCE(MAX(sequence), 0) AS value FROM run_events WHERE run_id = ?').get(run.id)?.value + importLegacyRunHistory(database, { + runId: run.id, + content: mapped, + sourceFingerprint: entry?.digest ?? 'missing', + certifiedThrough: entry && checkpoint?.file_fingerprint === entry.identity + && checkpoint.projection_version === RUN_EVENT_PROJECTION_VERSION && checkpoint.last_sequence === projectedLast + ? Number(checkpoint.last_sequence) + : null, + }) + await input.checkpoint?.('run') + } + importAliases(database) + const files = await importFiles(database, profileRoot, manifest, input.signal) + await verifyPiReferences(database, profileRoot, manifest, input.signal) + await relocateProfileJournals(profileRoot, manifest.sourceRoot, profileRoot, input.signal) + await verifyCandidateFiles(manifest, profileRoot, input.signal) + verifyDrafts(database, original, manifest.sourceRoot, profileRoot) + await input.checkpoint?.('files') + await verifyPersistenceSources(manifest, input.signal, input.report) + if (database.prepare('PRAGMA foreign_key_check').all().length + || database.prepare('SELECT 1 FROM runs WHERE next_event_sequence IS NULL').get()) { + throw new PersistenceMigrationError('MIGRATION_VERIFICATION_FAILED') + } + withTransaction(database, () => { + database!.prepare(`UPDATE persistence_migrations SET state = 'verified' WHERE id = ?`).run(PERSISTENCE_MIGRATION_ID) + }) + await input.checkpoint?.('verified') + return { profileRoot, sourceFingerprint: manifest.fingerprint, runs: runs.length, files } + } + finally { + database?.close() + original.close() + } +} + +function mapEventContent(content: string, source: string, target: string): string { + try { + return content.split('\n').map(line => line.trim() ? JSON.stringify(relocateStructuredPaths(JSON.parse(line), source, target)) : line).join('\n') + } + catch (cause) { + throw new PersistenceMigrationError('MIGRATION_EVENT_CORRUPTED', { cause }) + } +} + +function importAliases(database: DatabaseSync): void { + const namespaces = ['turn_requests', 'command_requests', 'chat_queue', 'automation_mutation_requests', 'automation_occurrences'] as const + withTransaction(database, () => { + for (const namespace of namespaces) { + for (const row of database.prepare(`SELECT * FROM ${namespace} WHERE request_id IS NOT NULL`).iterate()) { + const target = Object.fromEntries(Object.entries(row).filter(([key]) => ['id', 'run_id', 'conversation_id', 'branch_id', 'automation_id', 'draft_id', 'draft_revision', 'committed_draft_revision', 'state', 'status', 'kind'].includes(key))) + const fingerprint = digest(row) + const existing = database.prepare('SELECT source_fingerprint FROM legacy_request_aliases WHERE namespace = ? AND request_id = ?').get(namespace, row.request_id!) + if (existing && existing.source_fingerprint !== fingerprint) + throw new PersistenceMigrationError('MIGRATION_LEGACY_ALIAS_CHANGED') + if (!existing) { + database.prepare('INSERT INTO legacy_request_aliases (namespace, request_id, target_json, source_fingerprint) VALUES (?, ?, ?, ?)') + .run(namespace, row.request_id!, JSON.stringify({ observed: target, originalTargetKnown: false }), fingerprint) + } + recordEvidence(database, 'alias', `${namespace}:${row.request_id}`, fingerprint, { namespace }) + } + } + for (const draft of database.prepare('SELECT * FROM composer_drafts ORDER BY id').iterate()) + recordEvidence(database, 'draft', String(draft.id), digest(draft), { revision: draft.revision, scope: draft.scope_kind }) + }) +} + +async function importFiles(database: DatabaseSync, root: string, manifest: PersistenceSourceManifest, signal: AbortSignal): Promise { + const catalog = createAttachmentPersistenceRepository(database) + const records = database.prepare('SELECT id, stored_path, size_bytes FROM attachments ORDER BY id').all() as { id: string, stored_path: string, size_bytes: number }[] + for (const record of records) { + signal.throwIfAborted() + const path = await requireOwnedFile(root, record.stored_path) + const source = manifest.entries.find(entry => entry.path === relative(root, path).replaceAll('\\', '/')) + if (!source?.digest || source.size !== record.size_bytes || await hashStorageFile(path, signal) !== source.digest) + throw new PersistenceMigrationError('MIGRATION_ATTACHMENT_MISMATCH') + withTransaction(database, () => { + catalog.adopt({ path, sizeBytes: record.size_bytes, sha256: source.digest }) + recordEvidence(database, 'file', record.id, source.digest!, { sizeBytes: record.size_bytes }) + }) + } + return records.length +} + +async function verifyPiReferences(database: DatabaseSync, root: string, manifest: PersistenceSourceManifest, signal: AbortSignal): Promise { + const references = database.prepare(`SELECT pi_session_file AS path FROM runs WHERE pi_session_file IS NOT NULL + UNION SELECT session_file FROM execution_thread_bindings + UNION SELECT session_file FROM conversation_pi_trees`).all() as { path: string }[] + for (const reference of references) { + signal.throwIfAborted() + const path = await requireOwnedFile(root, reference.path) + const sourcePath = relative(root, path).replaceAll('\\', '/') + const source = manifest.entries.find(entry => entry.path === sourcePath) + if (!source?.digest) + throw new PersistenceMigrationError('MIGRATION_PI_SOURCE_MISSING') + const content = await readPersistenceSource(manifest, sourcePath) + if (content === null) + throw new PersistenceMigrationError('MIGRATION_PI_SOURCE_MISSING') + const ids = new Set() + try { + const entries = content.split('\n').filter(line => line.trim()).map(line => JSON.parse(line) as { id?: unknown, parentId?: unknown, type?: unknown }) + if (entries[0]?.type !== 'session') + throw new Error('Missing Pi session header') + for (const entry of entries) { + if (typeof entry.id !== 'string' || ids.has(entry.id) + || (entry.parentId !== undefined && entry.parentId !== null && (typeof entry.parentId !== 'string' || !ids.has(entry.parentId)))) { + throw new Error('Invalid Pi tree') + } + ids.add(entry.id) + } + } + catch (cause) { + throw new PersistenceMigrationError('MIGRATION_PI_CORRUPTED', { cause }) + } + const current = await readFile(path, 'utf8') + const expected = mapEventContent(content, manifest.sourceRoot, root) + if (current !== content && current !== `${expected.trimEnd()}\n` && current !== expected) + throw new PersistenceMigrationError('MIGRATION_PI_CHANGED') + const bindings = database.prepare(`SELECT root_entry_id AS id FROM execution_thread_bindings WHERE session_file = ? + UNION SELECT root_entry_id FROM conversation_pi_trees WHERE session_file = ?`).all(path, path) + if (bindings.some(binding => binding.id !== null && !ids.has(String(binding.id)))) + throw new PersistenceMigrationError('MIGRATION_PI_BINDING_MISSING') + withTransaction(database, () => recordEvidence(database, 'pi', sourcePath, source.digest!, { entries: ids.size })) + } +} + +async function verifyCandidateFiles(manifest: PersistenceSourceManifest, root: string, signal: AbortSignal): Promise { + for (const entry of manifest.entries) { + signal.throwIfAborted() + if (/^buddy\/buddy\.sqlite3(?:-(?:wal|shm|journal))?$/.test(entry.path)) + continue + const path = join(root, entry.path) + const metadata = await lstat(path) + if (entry.directory) { + if (!metadata.isDirectory() || metadata.isSymbolicLink()) + throw new PersistenceMigrationError('MIGRATION_CANDIDATE_CHANGED') + continue + } + if (entry.link !== null) { + const expected = containsStoragePath(manifest.sourceRoot, entry.link) + ? join(root, relative(manifest.sourceRoot, entry.link)) + : entry.link + if (!metadata.isSymbolicLink() || await readlink(path) !== expected) + throw new PersistenceMigrationError('MIGRATION_CANDIDATE_CHANGED') + continue + } + if (!metadata.isFile() || metadata.isSymbolicLink()) + throw new PersistenceMigrationError('MIGRATION_CANDIDATE_CHANGED') + if (await hashStorageFile(path, signal) === entry.digest) + continue + const isJournal = /^buddy\/conversations\/[^/]+\/(?:events|session)\/.+\.jsonl$/.test(entry.path) + const isWorkbench = /^buddy\/workbench(?:\.previous)?\.json$/.test(entry.path) + if (!isJournal && !isWorkbench) + throw new PersistenceMigrationError('MIGRATION_CANDIDATE_CHANGED') + const original = await readPersistenceSource(manifest, entry.path) + if (original === null) + throw new PersistenceMigrationError('MIGRATION_SOURCE_CHANGED') + const expected = isJournal + ? `${mapEventContent(original, manifest.sourceRoot, root).trimEnd()}\n` + : JSON.stringify(relocateStructuredPaths(JSON.parse(original), manifest.sourceRoot, root)) + if (await readFile(path, 'utf8') !== expected) + throw new PersistenceMigrationError('MIGRATION_CANDIDATE_CHANGED') + } +} + +function verifyDrafts(database: DatabaseSync, original: DatabaseSync, source: string, target: string): void { + if (!original.prepare('SELECT 1 FROM sqlite_master WHERE name = \'composer_drafts\' AND type = \'table\'').get()) + return + const actor = database.prepare('SELECT local_actor_id FROM storage_profile WHERE id = 1').get()?.local_actor_id + for (const row of original.prepare('SELECT * FROM composer_drafts').iterate()) { + const current = database.prepare('SELECT * FROM composer_drafts WHERE id = ?').get(row.id!) + if (!current || current.owner_actor_id !== actor) + throw new PersistenceMigrationError('MIGRATION_DRAFT_OWNER_MISMATCH') + for (const key of ['revision', 'content_json', 'model_selection_json', 'approval_policy', 'execution_profile', 'participants_json']) { + if (!(key in row)) + continue + const value = row[key] + const expected = key.endsWith('_json') && typeof value === 'string' + ? JSON.stringify(relocateStructuredPaths(JSON.parse(value), source, target)) + : value + const actual = key.endsWith('_json') && typeof current[key] === 'string' ? JSON.stringify(JSON.parse(current[key])) : current[key] + if (expected !== actual) + throw new PersistenceMigrationError('MIGRATION_DRAFT_CONTENT_CHANGED') + } + if (!database.prepare('SELECT 1 FROM composer_draft_slots WHERE owner_actor_id = ? AND draft_id = ?').get(actor!, row.id!)) + throw new PersistenceMigrationError('MIGRATION_DRAFT_SLOT_MISSING') + } +} + +async function requireOwnedFile(root: string, path: string): Promise { + if (!containsStoragePath(root, path)) + throw new PersistenceMigrationError('MIGRATION_REFERENCE_OUTSIDE') + try { + const metadata = await lstat(path) + if (!metadata.isFile() || await realpath(path) !== path) + throw new Error('Reference is not an owned regular file') + } + catch (cause) { + throw new PersistenceMigrationError('MIGRATION_REFERENCE_MISSING', { cause }) + } + return path +} + +function recordEvidence(database: DatabaseSync, kind: 'alias' | 'draft' | 'file' | 'pi', id: string, fingerprint: string, evidence: object): void { + const existing = database.prepare('SELECT source_fingerprint FROM persistence_migration_entries WHERE migration_id = ? AND kind = ? AND source_id = ?') + .get(PERSISTENCE_MIGRATION_ID, kind, id) + if (existing && existing.source_fingerprint !== fingerprint) + throw new PersistenceMigrationError('MIGRATION_CANDIDATE_CHANGED') + if (!existing) { + database.prepare('INSERT INTO persistence_migration_entries (migration_id, kind, source_id, source_fingerprint, evidence_json) VALUES (?, ?, ?, ?, ?)') + .run(PERSISTENCE_MIGRATION_ID, kind, id, fingerprint, JSON.stringify(evidence)) + } +} + +function digest(value: unknown): string { + return createHash('sha256').update(JSON.stringify(value)).digest('hex') +} diff --git a/apps/buddy/service/src/storage/persistenceMigration/legacyRunHistory.ts b/apps/buddy/service/src/storage/persistenceMigration/legacyRunHistory.ts new file mode 100644 index 000000000..529658a55 --- /dev/null +++ b/apps/buddy/service/src/storage/persistenceMigration/legacyRunHistory.ts @@ -0,0 +1,150 @@ +import type { DatabaseSync } from 'node:sqlite' +import type { BuddyRunEvent } from '../../events/BuddyRunEvent' +import { createHash } from 'node:crypto' +import { isDeepStrictEqual } from 'node:util' +import { buddyRunEventSchema, terminalRunStatus } from '../../events/BuddyRunEvent' +import { createRunEventCompactionPlan } from '../../events/RunEventCompaction' +import { RunEventProjector } from '../../events/RunEventProjector' +import { RunEventQueries } from '../../events/RunEventQueries' +import { withTransaction } from '../transaction' + +export const PERSISTENCE_MIGRATION_ID = 'legacy-jsonl-v1' + +export class PersistenceMigrationError extends Error { + readonly code: string + + constructor(code: string, options?: ErrorOptions) { + super(code, options) + this.name = 'PersistenceMigrationError' + this.code = code + } +} + +export function parseLegacyRunHistory(content: string, runId: string): { events: BuddyRunEvent[], repairedNewline: boolean } { + const events: BuddyRunEvent[] = [] + try { + for (const line of content.split('\n')) { + if (!line.trim()) + continue + const event = buddyRunEventSchema.parse(JSON.parse(line)) + if (event.runId !== runId || !Number.isSafeInteger(event.sequence) || event.sequence >= Number.MAX_SAFE_INTEGER + || event.sequence <= (events.at(-1)?.sequence ?? 0)) { + throw new Error('Invalid event identity or sequence') + } + events.push(event) + } + } + catch (cause) { + throw new PersistenceMigrationError('MIGRATION_EVENT_CORRUPTED', { cause }) + } + return { events, repairedNewline: content.length > 0 && !content.endsWith('\n') } +} + +export function reconcileLegacyRunHistory(input: { + events: readonly BuddyRunEvent[] + projected: readonly BuddyRunEvent[] + certifiedThrough: number | null + hasProductFacts: boolean +}): { events: BuddyRunEvent[], nextSequence: number, compacted: boolean } { + const { events, projected } = input + if (!events.length && (projected.length || input.hasProductFacts)) + throw new PersistenceMigrationError('MIGRATION_EVENT_SOURCE_MISSING') + const bySequence = new Map(events.map(event => [event.sequence, event])) + const merged = new Map(projected.map(event => [event.sequence, event])) + for (const event of events) { + const existing = merged.get(event.sequence) + if (existing && !isDeepStrictEqual(existing, event)) + throw new PersistenceMigrationError('MIGRATION_EVENT_CONFLICT') + merged.set(event.sequence, event) + } + const all = [...merged.values()].sort((a, b) => a.sequence - b.sequence) + const last = all.at(-1)?.sequence ?? 0 + const terminal = all.some(event => terminalRunStatus(event.type) !== null) + const removable = new Set(terminal ? createRunEventCompactionPlan(all).removed.map(event => event.sequence) : []) + const missing = projected.filter(event => !bySequence.has(event.sequence)) + if (missing.some(event => !removable.has(event.sequence))) + throw new PersistenceMigrationError('MIGRATION_EVENT_SOURCE_BEHIND') + const gaps = all.filter((event, index) => event.sequence !== (all[index - 1]?.sequence ?? 0) + 1) + const hasGaps = gaps.length > 0 + if (gaps.some(event => input.certifiedThrough === null || event.sequence > input.certifiedThrough)) + throw new PersistenceMigrationError('MIGRATION_EVENT_GAP_UNVERIFIED') + return { events: [...events], nextSequence: last + 1, compacted: hasGaps || missing.length > 0 } +} + +export function importLegacyRunHistory(database: DatabaseSync, input: { + runId: string + content: string | null + sourceFingerprint: string + certifiedThrough: number | null +}): { events: number, nextSequence: number } { + const { runId, sourceFingerprint } = input + const parsed = parseLegacyRunHistory(input.content ?? '', runId) + const existing = database.prepare(`SELECT source_fingerprint, evidence_json FROM persistence_migration_entries + WHERE migration_id = ? AND kind = 'run' AND source_id = ?`).get(PERSISTENCE_MIGRATION_ID, runId) + if (existing) { + if (existing.source_fingerprint !== sourceFingerprint) + throw new PersistenceMigrationError('MIGRATION_SOURCE_CHANGED') + const evidence = JSON.parse(String(existing.evidence_json)) as { events: number, nextSequence: number, importedFingerprint: string } + const current = new RunEventQueries(database).read(runId) + const run = database.prepare('SELECT next_event_sequence FROM runs WHERE id = ?').get(runId) + if (digest(current) !== evidence.importedFingerprint || run?.next_event_sequence !== evidence.nextSequence) + throw new PersistenceMigrationError('MIGRATION_CANDIDATE_CHANGED') + return { events: evidence.events, nextSequence: evidence.nextSequence } + } + const profile = database.prepare('SELECT authority, migration_state FROM storage_profile WHERE id = 1').get() + if (profile?.authority !== 'legacy_jsonl' || profile.migration_state !== 'pending') + throw new PersistenceMigrationError('MIGRATION_NOT_PENDING') + const queries = new RunEventQueries(database) + const reconciled = reconcileLegacyRunHistory({ + events: parsed.events, + projected: queries.read(runId), + hasProductFacts: queries.hasProjectedEvents(runId), + certifiedThrough: input.certifiedThrough, + }) + return withTransaction(database, () => { + assertLegacyMessageProjection(database, runId, reconciled.events) + const projected = queries.read(runId) + const existingSequences = new Set(projected.map(event => event.sequence)) + new RunEventProjector(database).project(reconciled.events.filter(event => !existingSequences.has(event.sequence))) + const retained = new Set(reconciled.events.map(event => event.sequence)) + const remove = database.prepare('DELETE FROM run_events WHERE run_id = ? AND sequence = ?') + for (const event of projected) { + if (!retained.has(event.sequence)) + remove.run(runId, event.sequence) + } + const updated = database.prepare('UPDATE runs SET next_event_sequence = ? WHERE id = ?').run(reconciled.nextSequence, runId) + if (updated.changes !== 1) + throw new PersistenceMigrationError('MIGRATION_RUN_MISSING') + const evidence = { + events: reconciled.events.length, + nextSequence: reconciled.nextSequence, + compacted: reconciled.compacted, + repairedNewline: parsed.repairedNewline, + importedFingerprint: digest(queries.read(runId)), + } + database.prepare(`INSERT INTO persistence_migration_entries + (migration_id, kind, source_id, source_fingerprint, evidence_json) VALUES (?, 'run', ?, ?, ?)`) + .run(PERSISTENCE_MIGRATION_ID, runId, sourceFingerprint, JSON.stringify(evidence)) + return { events: evidence.events, nextSequence: evidence.nextSequence } + }) +} + +function assertLegacyMessageProjection(database: DatabaseSync, runId: string, events: readonly BuddyRunEvent[]): void { + const messages = new Map(events.flatMap((event) => { + if (!['message.completed', 'message.interrupted', 'message.tool_result'].includes(event.type) + || !event.payload || typeof event.payload !== 'object') { + return [] + } + const value = event.payload as Record + return typeof value.messageId === 'string' ? [[value.messageId, value] as const] : [] + })) + for (const row of database.prepare(`SELECT id, role, content_json FROM messages WHERE run_id = ? AND role IN ('assistant', 'tool')`).iterate(runId)) { + const source = messages.get(String(row.id)) + if (!source || source.role !== row.role || !isDeepStrictEqual(source.content, JSON.parse(String(row.content_json)))) + throw new PersistenceMigrationError('MIGRATION_PROJECTION_CONFLICT') + } +} + +function digest(value: unknown): string { + return createHash('sha256').update(JSON.stringify(value)).digest('hex') +} diff --git a/apps/buddy/service/src/storage/persistenceMigration/persistenceSources.ts b/apps/buddy/service/src/storage/persistenceMigration/persistenceSources.ts new file mode 100644 index 000000000..657a95431 --- /dev/null +++ b/apps/buddy/service/src/storage/persistenceMigration/persistenceSources.ts @@ -0,0 +1,190 @@ +import type { BigIntStats } from 'node:fs' +import type { StorageCopyPlan, StorageEntry, StorageReporter } from '../../../../platform/storage/storageTree' +import { createHash } from 'node:crypto' +import { lstat, mkdir, readFile, realpath } from 'node:fs/promises' +import { dirname, isAbsolute, join, resolve } from 'node:path' +import { z } from 'zod' +import { containsStoragePath, isMissing, readStorageJson, writeStorageJson } from '../../../../platform/storage/storageFiles' +import { copyStorageTree, hashStorageFile, planStorageCopy } from '../../../../platform/storage/storageTree' +import { PersistenceMigrationError } from './legacyRunHistory' + +const sourceEntrySchema = z.object({ + path: z.string().min(1).refine(value => !isAbsolute(value) && !value.split(/[\\/]/).some(segment => segment === '..' || segment === '.')), + size: z.number().int().nonnegative(), + link: z.string().nullable(), + mtimeMs: z.number(), + directory: z.boolean(), + executable: z.boolean(), + digest: z.string().regex(/^[a-f0-9]{64}$/).nullable(), + identity: z.string(), +}).strict() +const sourceManifestSchema = z.object({ + version: z.literal(1), + scope: z.enum(['profile', 'upgrade']).default('profile'), + sourceRoot: z.string(), + candidateRoot: z.string(), + fingerprint: z.string().regex(/^[a-f0-9]{64}$/), + copied: z.boolean(), + workingCopyReady: z.boolean(), + entries: z.array(sourceEntrySchema), +}).strict() +export type PersistenceSourceManifest = z.infer +export type PersistenceSourceEntry = PersistenceSourceManifest['entries'][number] +const MANIFEST_NAME = '.persistence-migration.json' + +export const persistenceUpgradePaths = [ + 'buddy/buddy.sqlite3', + 'buddy/buddy.sqlite3-wal', + 'buddy/buddy.sqlite3-shm', + 'buddy/buddy.sqlite3-journal', + 'buddy/conversations', + 'buddy/drafts', + 'buddy/spaces', + 'buddy/agent', + 'buddy/skills', +] as const + +export function persistenceUpgradeIncludes(path: string): boolean { + return path === 'buddy' || persistenceUpgradePaths.some(root => path === root || path.startsWith(`${root}/`)) +} + +function sourceExcluded(path: string, scope: PersistenceSourceManifest['scope']): boolean { + return path === '.runtime' || path === 'buddy/buddy.sqlite3-shm' + || (scope === 'upgrade' && !persistenceUpgradeIncludes(path)) +} + +export async function readPersistenceSourceManifest(candidateRoot: string): Promise { + const value = await readStorageJson(join(candidateRoot, MANIFEST_NAME)) + if (!value) + return null + const manifest = sourceManifestSchema.parse(value) + if (manifest.candidateRoot !== resolve(candidateRoot) || sourceDigest(manifest.entries) !== manifest.fingerprint) + throw new PersistenceMigrationError('MIGRATION_CANDIDATE_CHANGED') + return manifest +} + +export async function preparePersistenceSources(input: { + sourceRoot: string + candidateRoot: string + signal: AbortSignal + scope?: PersistenceSourceManifest['scope'] + report?: StorageReporter +}): Promise { + const { signal } = input + const scope = input.scope ?? 'profile' + const report = input.report ?? (() => {}) + const sourceRoot = await realpath(input.sourceRoot) + const candidateRoot = resolve(input.candidateRoot) + if (!isAbsolute(input.sourceRoot) || !isAbsolute(input.candidateRoot) + || containsStoragePath(sourceRoot, candidateRoot) || containsStoragePath(candidateRoot, sourceRoot) + || await realpath(dirname(candidateRoot)) !== dirname(candidateRoot)) { + throw new PersistenceMigrationError('MIGRATION_CANDIDATE_PATH_INVALID') + } + const manifestPath = join(candidateRoot, MANIFEST_NAME) + const target = await lstat(candidateRoot).catch((error) => { + if (!isMissing(error)) + throw error + return null + }) + if (target?.isSymbolicLink() || (target && !target.isDirectory())) + throw new PersistenceMigrationError('MIGRATION_CANDIDATE_PATH_INVALID') + const previous = target ? await readStorageJson(manifestPath) : null + if (target && !previous) + throw new PersistenceMigrationError('MIGRATION_TARGET_NOT_EMPTY') + const plan = await planStorageCopy(sourceRoot, { + signal, + report, + exclude: path => sourceExcluded(path, scope), + }) + if (plan.entries.some(entry => entry.path === MANIFEST_NAME)) + throw new PersistenceMigrationError('MIGRATION_SOURCE_ALREADY_CANDIDATE') + const entries = await fingerprintEntries(sourceRoot, plan, signal, report, 'preparing') + if (!entries.some(entry => entry.path === 'buddy/buddy.sqlite3' && entry.digest)) + throw new PersistenceMigrationError('MIGRATION_DATABASE_MISSING') + const fingerprint = sourceDigest(entries) + let manifest: PersistenceSourceManifest = { version: 1, scope, sourceRoot, candidateRoot, fingerprint, copied: false, workingCopyReady: false, entries } + if (previous) { + manifest = sourceManifestSchema.parse(previous) + if (manifest.scope !== scope || manifest.sourceRoot !== sourceRoot || manifest.candidateRoot !== candidateRoot + || manifest.fingerprint !== fingerprint || sourceDigest(manifest.entries) !== fingerprint) { + throw new PersistenceMigrationError('MIGRATION_SOURCE_CHANGED') + } + if (manifest.copied) + return manifest + } + else { + await mkdir(candidateRoot, { mode: 0o700 }) + await writeStorageJson(manifestPath, manifest) + } + await copyStorageTree(sourceRoot, join(candidateRoot, 'source'), { signal, report, plan, overwrite: !!previous }) + await verifyPersistenceSources(manifest, signal, report) + manifest = { ...manifest, copied: true } + await writeStorageJson(manifestPath, manifest) + return manifest +} + +export async function preparePersistenceWorkingCopy(manifest: PersistenceSourceManifest, signal: AbortSignal, report: StorageReporter = () => {}): Promise { + const profile = join(manifest.candidateRoot, 'profile') + const source = join(manifest.candidateRoot, 'source') + const plan = await planStorageCopy(source, { signal, report, exclude: path => path === 'buddy/buddy.sqlite3-shm' }) + const snapshot = await fingerprintEntries(source, plan, signal, report, 'verifying') + const digests = new Map(snapshot.map(entry => [entry.path, entry.digest])) + for (const entry of manifest.entries) { + if (entry.digest && digests.get(entry.path) !== entry.digest) + throw new PersistenceMigrationError('MIGRATION_SNAPSHOT_CHANGED') + } + if (manifest.workingCopyReady) + return profile + await copyStorageTree(source, profile, { signal, report, plan, overwrite: true }) + manifest.workingCopyReady = true + await writeStorageJson(join(manifest.candidateRoot, MANIFEST_NAME), manifest) + return profile +} + +export async function verifyPersistenceSources(manifest: PersistenceSourceManifest, signal: AbortSignal, report: StorageReporter = () => {}): Promise { + const plan = await planStorageCopy(manifest.sourceRoot, { signal, report, exclude: path => sourceExcluded(path, manifest.scope) }) + if (sourceDigest(await fingerprintEntries(manifest.sourceRoot, plan, signal, report, 'verifying')) !== manifest.fingerprint) + throw new PersistenceMigrationError('MIGRATION_SOURCE_CHANGED') +} + +export async function readPersistenceSource(manifest: PersistenceSourceManifest, path: string): Promise { + const entry = manifest.entries.find(item => item.path === path) + if (!entry) + return null + if (entry.link || entry.directory || !entry.digest) + throw new PersistenceMigrationError('MIGRATION_SOURCE_NOT_REGULAR') + const content = await readFile(join(manifest.candidateRoot, 'source', path)) + if (createHash('sha256').update(content).digest('hex') !== entry.digest) + throw new PersistenceMigrationError('MIGRATION_SOURCE_CHANGED') + return content.toString('utf8') +} + +async function fingerprintEntries(root: string, plan: StorageCopyPlan, signal: AbortSignal, report: StorageReporter, phase: 'preparing' | 'verifying'): Promise { + const entries: PersistenceSourceEntry[] = [] + let completedBytes = 0 + let lastReport = 0 + for (const entry of plan.entries) { + signal.throwIfAborted() + const before = await lstat(join(root, entry.path), { bigint: true }) + const digest = entry.directory || entry.link !== null ? null : await hashStorageFile(join(root, entry.path), signal) + const after = await lstat(join(root, entry.path), { bigint: true }) + if (identity(before) !== identity(after)) + throw new PersistenceMigrationError('MIGRATION_SOURCE_CHANGED') + entries.push({ ...entry, digest, identity: identity(after) }) + completedBytes += entry.size + if (Date.now() - lastReport >= 100) { + report({ phase, completedBytes, totalBytes: plan.totalBytes, files: entries.length, error: null }) + lastReport = Date.now() + } + } + report({ phase, completedBytes, totalBytes: plan.totalBytes, files: entries.length, error: null }) + return entries +} + +function identity(metadata: BigIntStats): string { + return [metadata.dev, metadata.ino, metadata.size, metadata.mtimeNs, metadata.ctimeNs, metadata.birthtimeNs].join(':') +} + +function sourceDigest(entries: readonly (StorageEntry & { digest: string | null, identity: string })[]): string { + return createHash('sha256').update(JSON.stringify(entries)).digest('hex') +} diff --git a/apps/buddy/service/src/storage/providerRepository.ts b/apps/buddy/service/src/storage/providerRepository.ts index e7d1db353..62158093f 100644 --- a/apps/buddy/service/src/storage/providerRepository.ts +++ b/apps/buddy/service/src/storage/providerRepository.ts @@ -4,6 +4,7 @@ import type { DefaultModelRepository } from './defaultModelRepository' import type { ProviderConfigRepository } from './providerConfigRepository' import type { ProviderModelStateRepository } from './providerModelStateRepository' import type { ProviderStateRepository } from './providerStateRepository' +import type { SynchronousOperation } from './transaction' import { createBuiltinProviderConfigRepository } from './builtinProviderConfigRepository' import { withTransaction } from './database' import { createDefaultModelRepository } from './defaultModelRepository' @@ -17,7 +18,7 @@ export interface ProviderRepository { readonly defaultModel: DefaultModelRepository readonly models: ProviderModelStateRepository readonly states: ProviderStateRepository - transaction: (operation: () => T) => T + transaction: (operation: SynchronousOperation) => T } export function createProviderRepository(database: DatabaseSync): ProviderRepository { diff --git a/apps/buddy/service/src/storage/relocateProfileReferences.ts b/apps/buddy/service/src/storage/relocateProfileReferences.ts index 7da964523..35ff84e0d 100644 --- a/apps/buddy/service/src/storage/relocateProfileReferences.ts +++ b/apps/buddy/service/src/storage/relocateProfileReferences.ts @@ -1,12 +1,13 @@ import type { DatabaseSync } from 'node:sqlite' import { createReadStream } from 'node:fs' -import { lstat, open, readFile, rename, writeFile } from 'node:fs/promises' +import { lstat, open, readFile, realpath, rename, writeFile } from 'node:fs/promises' import { join, posix, relative, win32 } from 'node:path' import { createInterface } from 'node:readline' import { containsStoragePath, storageError } from '../../../platform/storage/storageFiles' -import { walkStorage } from '../../../platform/storage/storageTree' +import { hashStorageFile, walkStorage } from '../../../platform/storage/storageTree' const pathColumns: Record = { + managed_files: ['path', 'temporary_path'], attachments: ['stored_path', 'source_path'], composer_resources: ['source_path'], artifacts: ['current_path', 'directory_root'], @@ -47,6 +48,8 @@ export function relocateDatabaseReferences(database: DatabaseSync, source: strin database.exec('BEGIN IMMEDIATE') try { for (const { name: table } of tables) { + if (['command_receipts', 'approval_reviews', 'product_content_references', 'persistence_migration_entries', 'legacy_request_aliases'].includes(table)) + continue if (!/^\w+$/.test(table)) throw storageError('STORAGE_UNKNOWN_SCHEMA') const columns = database.prepare(`PRAGMA table_info("${table}")`).all() as { @@ -148,4 +151,15 @@ export async function verifyProfileReferences(database: DatabaseSync, candidate: } } } + if (database.prepare('SELECT 1 FROM sqlite_master WHERE type = \'table\' AND name = \'managed_files\'').get()) { + const root = await realpath(candidate) + for (const file of database.prepare('SELECT path, size_bytes, sha256 FROM managed_files WHERE state = \'ready\' AND EXISTS (SELECT 1 FROM file_references WHERE file_id = managed_files.id)').iterate()) { + if (typeof file.path !== 'string' || !containsStoragePath(target, file.path)) + throw storageError('STORAGE_REFERENCE_OUTSIDE') + const path = join(candidate, relative(target, file.path)) + const info = await lstat(path).catch(() => null) + if (!info?.isFile() || !containsStoragePath(root, await realpath(path)) || info.size !== Number(file.size_bytes) || await hashStorageFile(path) !== file.sha256) + throw storageError('STORAGE_REFERENCE_INVALID') + } + } } diff --git a/apps/buddy/service/src/storage/restoreBackup.ts b/apps/buddy/service/src/storage/restoreBackup.ts index 927a6d8e7..63a147d1a 100644 --- a/apps/buddy/service/src/storage/restoreBackup.ts +++ b/apps/buddy/service/src/storage/restoreBackup.ts @@ -1,7 +1,7 @@ import type { StorageReporter } from '../../../platform/storage/storageTree' import type { StorageOperation } from '../../../shared/storage/storageApi' -import { createHash } from 'node:crypto' -import { lstat, readFile, rm, writeFile } from 'node:fs/promises' +import { createHash, randomUUID } from 'node:crypto' +import { lstat, readFile, rename, rm, writeFile } from 'node:fs/promises' import { join } from 'node:path' import { restoreExtensionBackup } from '../../../platform/extensions/restoreExtensionBackup' import { extractSnapshotFiles } from '../../../platform/storage/extractSnapshotFiles' @@ -10,13 +10,17 @@ import { restoreCredentialStage } from '../../../platform/storage/storageOperati import { copyStorageTree, syncStorageDirectories } from '../../../platform/storage/storageTree' import { mergeRestoredSyncConnections } from '../../../platform/sync/syncConnectionStore' import { backupDirectoryAllowed } from '../../../shared/storage/backupScope' +import { revokeRestoredDeviceGrants } from '../privateNetwork/revokeRestoredDeviceGrants' import { mergeBackupDatabase, selectBackupDatabase } from './backupDatabase' import { mergeSettings } from './backupSettings' -import { openBuddyDatabase } from './database' import { restoreNotificationSoundBackup } from './notificationSoundBackup' +import { activatePersistenceCandidate } from './persistenceMigration/activatePersistenceCandidate' import { relocateDatabaseReferences, relocateProfileJournals, verifyProfileReferences } from './relocateProfileReferences' import { sanitizeRestoredDatabase } from './restorePolicy' +import { BUDDY_SCHEMA_VERSION } from './schema' +import { openSnapshotDatabase, rewriteIsolatedSnapshotDatabase, snapshotDatabaseVersion } from './snapshotDatabase' import { snapshotDirectory, verifyStorageSnapshot } from './StorageSnapshots' +import { withTransaction } from './transaction' export async function prepareBackupRestore(operation: StorageOperation, stage: string, locationPath: string, signal: AbortSignal, report: StorageReporter, privateDirectoriesExecutable?: string): Promise { if (!operation.snapshotId) @@ -31,48 +35,65 @@ export async function prepareBackupRestore(operation: StorageOperation, stage: s const options = { signal, report, privateDirectoriesExecutable } await copyStorageTree(join(operation.source, 'buddy'), join(candidate, 'buddy'), { ...options, destinationRoot: join(operation.target, 'buddy') }) await extractSnapshotFiles(directory, incoming, manifest, { ...options, exclude: path => !backupDirectoryAllowed(path, scopes) }) + let incomingReferenceRoot = manifest.sourceRoot + let candidateReferenceRoot = operation.source + const incomingVersion = snapshotDatabaseVersion(join(incoming, 'buddy/buddy.sqlite3')) + const candidateVersion = snapshotDatabaseVersion(join(candidate, 'buddy/buddy.sqlite3')) + if (incomingVersion > BUDDY_SCHEMA_VERSION || candidateVersion > BUDDY_SCHEMA_VERSION) { + if (incomingVersion <= BUDDY_SCHEMA_VERSION) + incomingReferenceRoot = await upgradeSnapshotProfile(incoming, incomingReferenceRoot, join(stage, 'convert-incoming'), signal, privateDirectoriesExecutable) + if (candidateVersion <= BUDDY_SCHEMA_VERSION) + candidateReferenceRoot = await upgradeSnapshotProfile(candidate, candidateReferenceRoot, join(stage, 'convert-current'), signal, privateDirectoriesExecutable) + } const sourcePath = join(incoming, 'buddy', 'buddy.sqlite3') const targetPath = join(candidate, 'buddy', 'buddy.sqlite3') - const source = openBuddyDatabase({ databasePath: sourcePath }) - const target = openBuddyDatabase({ databasePath: targetPath }) const credentialStage = restoreCredentialStage(locationPath, operation.id) - try { + await rewriteIsolatedSnapshotDatabase(sourcePath, async (source) => { sanitizeRestoredDatabase(source, operation.credentials) selectBackupDatabase(source, scopes, manifest.files.map(entry => entry.path)) - relocateDatabaseReferences(source, manifest.sourceRoot, operation.target) - for (const [scope, table, namespace] of [['providers', 'provider_states', 'providers'], ['connections', 'mcp_servers', 'connectors']] as const) { - if (!scopes.includes(scope)) - continue - for (const row of source.prepare(`SELECT * FROM ${table}`).iterate()) { - const id = String(row.provider_id ?? row.id) - const refs = namespace === 'connectors' ? [row.credential_ref, target.prepare('SELECT credential_ref FROM mcp_servers WHERE id = ?').get(id)?.credential_ref] : [id] - for (const ref of refs) { - if (typeof ref === 'string') - await rm(join(candidate, 'buddy', 'secrets', namespace, `${createHash('sha256').update(ref).digest('hex')}.credential`), { force: true }) + relocateDatabaseReferences(source, incomingReferenceRoot, operation.target) + await rewriteIsolatedSnapshotDatabase(targetPath, async (target) => { + if (candidateReferenceRoot !== operation.target) + relocateDatabaseReferences(target, candidateReferenceRoot, operation.target) + for (const [scope, table, namespace] of [['providers', 'provider_states', 'providers'], ['connections', 'mcp_servers', 'connectors']] as const) { + if (!scopes.includes(scope)) + continue + for (const row of source.prepare(`SELECT * FROM ${table}`).iterate()) { + const id = String(row.provider_id ?? row.id) + const refs = namespace === 'connectors' ? [row.credential_ref, target.prepare('SELECT credential_ref FROM mcp_servers WHERE id = ?').get(id)?.credential_ref] : [id] + for (const ref of refs) { + if (typeof ref === 'string') + await rm(join(candidate, 'buddy', 'secrets', namespace, `${createHash('sha256').update(ref).digest('hex')}.credential`), { force: true }) + } } } - } - for (const table of ['provider_configs', 'mcp_servers']) { - for (const row of source.prepare(`SELECT id, credential_ref FROM ${table} WHERE credential_ref IS NOT NULL`).iterate()) { - const namespace = table === 'provider_configs' ? 'providers' : 'connectors' - const path = join(credentialStage, 'secrets', namespace, `${createHash('sha256').update(String(row.credential_ref)).digest('hex')}.credential`) - if (!await lstat(path).catch((error) => { - if (isMissing(error)) - return null - throw error - })) { - source.prepare(`UPDATE ${table} SET credential_ref = NULL WHERE id = ?`).run(row.id!) + for (const table of ['provider_configs', 'mcp_servers']) { + for (const row of source.prepare(`SELECT id, credential_ref FROM ${table} WHERE credential_ref IS NOT NULL`).iterate()) { + const namespace = table === 'provider_configs' ? 'providers' : 'connectors' + const path = join(credentialStage, 'secrets', namespace, `${createHash('sha256').update(String(row.credential_ref)).digest('hex')}.credential`) + if (!await lstat(path).catch((error) => { + if (isMissing(error)) + return null + throw error + })) { + source.prepare(`UPDATE ${table} SET credential_ref = NULL WHERE id = ?`).run(row.id!) + } } } - } - mergeBackupDatabase(target, source, scopes) - target.exec('PRAGMA wal_checkpoint(TRUNCATE)') - } - finally { - source.close() - target.close() - } - await relocateProfileJournals(incoming, manifest.sourceRoot, operation.target, signal) + mergeBackupDatabase(target, source, scopes) + if (source.prepare('SELECT 1 FROM sqlite_master WHERE name = \'storage_profile\'').get()) { + withTransaction(target, () => { + target.prepare('UPDATE storage_profile SET data_generation = ? WHERE id = 1').run(randomUUID()) + target.exec('UPDATE storage_actors SET state = \'revoked\' WHERE kind = \'device\'; DELETE FROM service_admission_gates; UPDATE file_preparations SET state = CASE WHEN EXISTS (SELECT 1 FROM file_references WHERE file_id = file_preparations.id) THEN \'settled\' ELSE \'aborted\' END, reserved_bytes = 0') + revokeRestoredDeviceGrants(target) + }) + } + target.exec('PRAGMA wal_checkpoint(TRUNCATE)') + }) + }) + if (candidateReferenceRoot !== operation.target) + await relocateProfileJournals(candidate, candidateReferenceRoot, operation.target, signal) + await relocateProfileJournals(incoming, incomingReferenceRoot, operation.target, signal) await copyStorageTree(incoming, candidate, { ...options, overwrite: true, exclude: path => path === 'credentials.enc' || /^buddy\/notification-sounds(?:\/|$)/.test(path) || /^buddy\/buddy\.sqlite3(?:-|$)/.test(path) || path === 'config.toml' || path === 'device/sync.json' || /^buddy\/extensions\/(?:installed|schedules)\.json$/.test(path) }) if (scopes.includes('settings')) { const text = await readFile(join(incoming, 'config.toml'), 'utf8').catch((error) => { @@ -106,12 +127,12 @@ export async function prepareBackupRestore(operation: StorageOperation, stage: s })) { await copyStorageTree(join(credentialStage, 'secrets'), join(candidate, 'buddy', 'secrets'), { ...options, overwrite: true }) } - const references = openBuddyDatabase({ databasePath: sourcePath }) + const references = openSnapshotDatabase(sourcePath) try { await verifyProfileReferences(references, candidate, operation.target) } finally { references.close() } - const database = openBuddyDatabase({ databasePath: targetPath }) + const database = openSnapshotDatabase(targetPath) try { if (database.prepare('PRAGMA foreign_key_check').all().length || Object.values(database.prepare('PRAGMA quick_check').get() ?? {})[0] !== 'ok') throw storageError('STORAGE_DATABASE_INVALID') @@ -120,3 +141,12 @@ export async function prepareBackupRestore(operation: StorageOperation, stage: s await syncStorageFile(targetPath) await syncStorageDirectories(candidate) } + +async function upgradeSnapshotProfile(profile: string, referenceRoot: string, conversionRoot: string, signal: AbortSignal, privateDirectoriesExecutable?: string): Promise { + await rewriteIsolatedSnapshotDatabase(join(profile, 'buddy/buddy.sqlite3'), database => relocateDatabaseReferences(database, referenceRoot, profile)) + await relocateProfileJournals(profile, referenceRoot, profile, signal) + const converted = await activatePersistenceCandidate({ sourceRoot: profile, candidateRoot: conversionRoot, signal, privateDirectoriesExecutable }) + await rm(profile, { recursive: true }) + await rename(converted.profileRoot, profile) + return converted.profileRoot +} diff --git a/apps/buddy/service/src/storage/restorePolicy.ts b/apps/buddy/service/src/storage/restorePolicy.ts index 73462ab69..87fea7cd1 100644 --- a/apps/buddy/service/src/storage/restorePolicy.ts +++ b/apps/buddy/service/src/storage/restorePolicy.ts @@ -1,6 +1,7 @@ import type { DatabaseSync } from 'node:sqlite' export function sanitizeRestoredDatabase(database: DatabaseSync, preserveCredentials = false): void { + const persistence = Boolean(database.prepare('SELECT 1 FROM sqlite_master WHERE type = \'table\' AND name = \'storage_profile\'').get()) database.exec('PRAGMA secure_delete = ON; BEGIN IMMEDIATE') try { database.exec(` @@ -19,7 +20,7 @@ export function sanitizeRestoredDatabase(database: DatabaseSync, preserveCredent ${preserveCredentials ? '' : 'UPDATE mcp_servers SET credential_ref = NULL; UPDATE provider_configs SET credential_ref = NULL;'} UPDATE provider_states SET request_headers_json = '[]'; UPDATE skill_installations SET enabled = 0; - DELETE FROM chat_queue; + ${persistence ? 'UPDATE chat_queue SET state = \'paused\' WHERE state = \'waiting\'; UPDATE file_preparations SET state = CASE WHEN EXISTS (SELECT 1 FROM file_references WHERE file_id = file_preparations.id) THEN \'settled\' ELSE \'aborted\' END, reserved_bytes = 0; DELETE FROM file_references WHERE kind = \'export\'; DELETE FROM service_admission_gates;' : 'DELETE FROM chat_queue;'} COMMIT; `) } diff --git a/apps/buddy/service/src/storage/schema.ts b/apps/buddy/service/src/storage/schema.ts index 2bde91bc6..453b7351d 100644 --- a/apps/buddy/service/src/storage/schema.ts +++ b/apps/buddy/service/src/storage/schema.ts @@ -43,6 +43,7 @@ import { BUDDY_V39_PROVIDER_RESOLUTION_SCHEMA_SQL } from './migrations/v39Provid import { BUDDY_V40_EVENT_MAINTENANCE_SCHEMA_SQL } from './migrations/v40EventMaintenance' import { BUDDY_V41_USER_QUESTIONS_SCHEMA_SQL } from './migrations/v41UserQuestions' import { BUDDY_V42_CONVERSATION_ARCHIVE_SCHEMA_SQL } from './migrations/v42ConversationArchive' +import { BUDDY_V43_PERSISTENCE_SCHEMA_SQL } from './migrations/v43Persistence' export interface BuddySchemaMigration { foreignKeys?: 'off' @@ -51,6 +52,8 @@ export interface BuddySchemaMigration { } export const BUDDY_SCHEMA_VERSION = 42 as const +export const BUDDY_MIGRATION_SCHEMA_VERSION = 43 as const +export const BUDDY_STORAGE_WRITER_VERSION = 1 as const export const BUDDY_SCHEMA_MIGRATIONS: readonly BuddySchemaMigration[] = [ { sql: BUDDY_V1_INITIAL_SCHEMA_SQL, version: 1 }, @@ -95,4 +98,5 @@ export const BUDDY_SCHEMA_MIGRATIONS: readonly BuddySchemaMigration[] = [ { sql: BUDDY_V40_EVENT_MAINTENANCE_SCHEMA_SQL, version: 40 }, { sql: BUDDY_V41_USER_QUESTIONS_SCHEMA_SQL, version: 41 }, { sql: BUDDY_V42_CONVERSATION_ARCHIVE_SCHEMA_SQL, version: 42 }, + { sql: BUDDY_V43_PERSISTENCE_SCHEMA_SQL, version: 43 }, ] diff --git a/apps/buddy/service/src/storage/snapshotDatabase.ts b/apps/buddy/service/src/storage/snapshotDatabase.ts new file mode 100644 index 000000000..89aacf585 --- /dev/null +++ b/apps/buddy/service/src/storage/snapshotDatabase.ts @@ -0,0 +1,91 @@ +import type { SQLInputValue } from 'node:sqlite' +import { randomUUID } from 'node:crypto' +import { closeSync, openSync } from 'node:fs' +import { rename, rm } from 'node:fs/promises' +import { dirname, join } from 'node:path' +import { DatabaseSync } from 'node:sqlite' +import { storageError, syncStorageDirectory, syncStorageFile } from '../../../platform/storage/storageFiles' +import { openBuddyDatabase, openBuddySqliteDatabase } from './database' +import { BUDDY_MIGRATION_SCHEMA_VERSION, BUDDY_SCHEMA_VERSION } from './schema' + +export function snapshotDatabaseVersion(databasePath: string): number { + const database = new DatabaseSync(databasePath, { readOnly: true }) + try { + return Number(database.prepare('PRAGMA user_version').get()!.user_version) + } + finally { database.close() } +} + +export function openSnapshotDatabase(databasePath: string): DatabaseSync { + const version = snapshotDatabaseVersion(databasePath) + if (version <= BUDDY_SCHEMA_VERSION) + return openBuddyDatabase({ databasePath }) + return openBuddySqliteDatabase({ databasePath }) +} + +export async function rewriteIsolatedSnapshotDatabase(databasePath: string, operation: (database: DatabaseSync) => T | Promise): Promise { + const source = openSnapshotDatabase(databasePath) + if (Number(source.prepare('PRAGMA user_version').get()!.user_version) <= BUDDY_SCHEMA_VERSION) { + try { + return await operation(source) + } + finally { source.close() } + } + const temporary = join(dirname(databasePath), `.snapshot-${randomUUID()}.sqlite3`) + let database: DatabaseSync | undefined + try { + closeSync(openSync(temporary, 'wx', 0o600)) + database = new DatabaseSync(temporary) + database.exec('PRAGMA foreign_keys = OFF; PRAGMA synchronous = FULL; BEGIN IMMEDIATE') + const schema = source.prepare('SELECT type, name, sql FROM sqlite_master WHERE sql IS NOT NULL AND name NOT LIKE \'sqlite_%\' ORDER BY type, name').all() as Array<{ type: string, name: string, sql: string }> + for (const definition of schema.filter(item => item.type === 'table')) + database.exec(definition.sql) + for (const definition of schema.filter(item => item.type === 'index')) + database.exec(definition.sql) + for (const { name } of schema.filter(item => item.type === 'table')) { + const names = (source.prepare(`PRAGMA table_info(${quote(name)})`).all() as { name: string }[]).map(column => column.name) + const insert = database.prepare(`INSERT INTO ${quote(name)} (${names.map(quote).join(',')}) VALUES (${names.map(() => '?').join(',')})`) + const rows = source.prepare(`SELECT * FROM ${quote(name)}`) + rows.setReadBigInts(true) + for (const row of rows.iterate()) + insert.run(...names.map(column => row[column] as SQLInputValue)) + } + if (source.prepare('SELECT 1 FROM sqlite_master WHERE name = \'sqlite_sequence\'').get()) { + const sequences = source.prepare('SELECT name, seq FROM sqlite_sequence') + sequences.setReadBigInts(true) + database.exec('DELETE FROM sqlite_sequence') + const insertSequence = database.prepare('INSERT INTO sqlite_sequence (name, seq) VALUES (?, ?)') + for (const row of sequences.iterate()) + insertSequence.run(row.name!, row.seq!) + } + database.exec(`PRAGMA user_version = ${BUDDY_MIGRATION_SCHEMA_VERSION}; COMMIT; PRAGMA foreign_keys = ON`) + const result = await operation(database) + database.exec('BEGIN IMMEDIATE') + for (const definition of schema.filter(item => item.type === 'trigger' || item.type === 'view')) + database.exec(definition.sql) + if (database.prepare('PRAGMA foreign_key_check').all().length || database.prepare('PRAGMA integrity_check').get()?.integrity_check !== 'ok') + throw storageError('SNAPSHOT_DATABASE_INVALID') + database.exec('COMMIT; PRAGMA wal_checkpoint(TRUNCATE)') + database.close() + database = undefined + source.close() + const verified = openBuddySqliteDatabase({ databasePath: temporary }) + verified.close() + await syncStorageFile(temporary) + await Promise.all([rm(`${databasePath}-wal`, { force: true }), rm(`${databasePath}-shm`, { force: true })]) + await rename(temporary, databasePath) + await syncStorageDirectory(dirname(databasePath)) + return result + } + finally { + if (source.isOpen) + source.close() + if (database?.isOpen) + database.close() + await Promise.all([temporary, `${temporary}-wal`, `${temporary}-shm`].map(path => rm(path, { force: true }))) + } +} + +function quote(value: string): string { + return `"${value.replaceAll('"', '""')}"` +} diff --git a/apps/buddy/service/src/storage/sqliteAuthority.ts b/apps/buddy/service/src/storage/sqliteAuthority.ts new file mode 100644 index 000000000..4fc6e2308 --- /dev/null +++ b/apps/buddy/service/src/storage/sqliteAuthority.ts @@ -0,0 +1,16 @@ +import type { DatabaseSync } from 'node:sqlite' +import { BUDDY_STORAGE_WRITER_VERSION } from './schema' + +export function requireSqliteAuthority(database: DatabaseSync): { dataGeneration: string } { + const profile = database.prepare(` + SELECT data_generation, authority, migration_state, min_writer_version + FROM storage_profile WHERE id = 1 + `).get() + if (!profile || profile.authority !== 'sqlite' || profile.migration_state !== 'ready' + || typeof profile.data_generation !== 'string' + || typeof profile.min_writer_version !== 'number' + || profile.min_writer_version > BUDDY_STORAGE_WRITER_VERSION) { + throw new Error('SQLite storage authority is not ready for this writer') + } + return { dataGeneration: profile.data_generation } +} diff --git a/apps/buddy/service/src/storage/taskMarkRepository.ts b/apps/buddy/service/src/storage/taskMarkRepository.ts index d5e832418..7ae5d56df 100644 --- a/apps/buddy/service/src/storage/taskMarkRepository.ts +++ b/apps/buddy/service/src/storage/taskMarkRepository.ts @@ -6,6 +6,7 @@ import { BuddyServiceError } from '../rpc/runtimeRequest' import { withTransaction } from './database' interface MarkRow { + revision?: number id: string name: string description: string @@ -16,6 +17,7 @@ interface MarkRow { } interface StateRow { + assignment_revision?: number conversation_id: string mark_id: string | null result_run_id: string | null @@ -38,6 +40,8 @@ const STATE_QUERY = ` ` export function createTaskMarkRepository(database: DatabaseSync) { + const hasRevisions = database.prepare('PRAGMA table_info(task_attention)').all().some(column => column.name === 'assignment_revision') + const stateQuery = hasRevisions ? STATE_QUERY.replace('attention.mark_id,', 'attention.mark_id, COALESCE(attention.assignment_revision, 0) AS assignment_revision,') : STATE_QUERY const listMarks = database.prepare(` SELECT task_marks.*, ( SELECT COUNT(*) FROM task_attention @@ -46,7 +50,7 @@ export function createTaskMarkRepository(database: DatabaseSync) { ) AS task_count FROM task_marks ORDER BY created_at, id `) - const readState = database.prepare(`${STATE_QUERY} AND conversations.id = ?`) + const readState = database.prepare(`${stateQuery} AND conversations.id = ?`) const insert = database.prepare('INSERT INTO task_marks (id, name, description, color, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)') const update = database.prepare('UPDATE task_marks SET name = ?, description = ?, color = ?, updated_at = ? WHERE id = ?') const remove = database.prepare('DELETE FROM task_marks WHERE id = ?') @@ -63,6 +67,7 @@ export function createTaskMarkRepository(database: DatabaseSync) { function list(): LocalTaskMark[] { return (listMarks.all() as unknown as MarkRow[]).map(row => ({ + ...(row.revision !== undefined ? { revision: row.revision } : {}), id: row.id, name: row.name, description: row.description, @@ -102,12 +107,12 @@ export function createTaskMarkRepository(database: DatabaseSync) { list, getState, allStates(): LocalTaskMarkState[] { - return (database.prepare(STATE_QUERY).all() as unknown as StateRow[]).map(toState) + return (database.prepare(stateQuery).all() as unknown as StateRow[]).map(toState) }, states(conversationIds: readonly string[]): LocalTaskMarkState[] { if (conversationIds.length === 0) return [] - return (database.prepare(`${STATE_QUERY} AND conversations.id IN (${conversationIds.map(() => '?').join(', ')})`) + return (database.prepare(`${stateQuery} AND conversations.id IN (${conversationIds.map(() => '?').join(', ')})`) .all(...conversationIds) as unknown as StateRow[]).map(toState) }, create(input: TaskMarkInput): LocalTaskMark { @@ -157,6 +162,7 @@ export function createTaskMarkRepository(database: DatabaseSync) { function toState(row: StateRow): LocalTaskMarkState { return { + ...(row.assignment_revision !== undefined ? { assignmentRevision: row.assignment_revision } : {}), conversationId: row.conversation_id, markId: row.mark_id, resultRunId: row.result_run_id, diff --git a/apps/buddy/service/src/storage/threadBindingRepository.ts b/apps/buddy/service/src/storage/threadBindingRepository.ts index 5ba007bb6..a1258d00d 100644 --- a/apps/buddy/service/src/storage/threadBindingRepository.ts +++ b/apps/buddy/service/src/storage/threadBindingRepository.ts @@ -38,10 +38,10 @@ export function createThreadBindingRepository(database: DatabaseSync) { const previous = previousSession.get(threadId, branchId) as { pi_session_file: string } | undefined return previous?.pi_session_file ?? null }, - ensureActor(conversationId: string, actorId: string, createdAt: string): ExecutionThreadRecord { + ensureActor(conversationId: string, actorId: string, createdAt: string, branchId?: string): ExecutionThreadRecord { if (!database.prepare('SELECT 1 FROM conversation_members WHERE conversation_id = ? AND actor_id = ?').get(conversationId, actorId)) throw new Error('Actor thread requires conversation membership') - const id = actorThreadId(conversationId, actorId) + const id = actorThreadId(conversationId, actorId, branchId) database.prepare('INSERT OR IGNORE INTO execution_threads (id, conversation_id, actor_id, kind, created_at) VALUES (?, ?, ?, \'actor\', ?)') .run(id, conversationId, actorId, createdAt) return find(id)! diff --git a/apps/buddy/service/src/storage/transaction.ts b/apps/buddy/service/src/storage/transaction.ts new file mode 100644 index 000000000..62ad4b381 --- /dev/null +++ b/apps/buddy/service/src/storage/transaction.ts @@ -0,0 +1,185 @@ +import type { DatabaseSync } from 'node:sqlite' +import { isAsyncFunction } from 'node:util/types' +import { Emitter } from '../../../shared/events/Emitter' +import { getStorageIsolation } from './StorageIsolation' + +type TransactionPhase = 'begin' | 'operation' | 'commit' +export type SynchronousOperation = () => T & (T extends PromiseLike ? never : unknown) + +export interface TransactionFailure { + phase: TransactionPhase + outcome: 'not_started' | 'rolled_back' | 'unknown' + cleanupError?: unknown +} + +interface TransactionContext { + failure: { error: unknown } | null + ended: TransactionFailure | null + notifications: Array<() => unknown> +} + +interface ConnectionTransactions { + active: TransactionContext | null + uncertain: BuddyTransactionUncertainError | null + notifications: Emitter<() => unknown> +} + +const connections = new WeakMap() +const failures = new WeakMap() + +export class BuddyTransactionUncertainError extends Error { + readonly code = 'STORAGE_TRANSACTION_UNCERTAIN' + readonly failure: TransactionFailure + + constructor(cause: unknown, failure: TransactionFailure) { + super('Storage transaction outcome requires verification on a new connection', { cause }) + this.name = 'BuddyTransactionUncertainError' + this.failure = failure + } +} + +export function readTransactionFailure(error: unknown): TransactionFailure | null { + return error instanceof BuddyTransactionUncertainError + ? error.failure + : isObject(error) ? failures.get(error) ?? null : null +} + +export function assertCommittedRead(database: DatabaseSync): void { + const connection = getConnection(database) + if (connection.uncertain) + throw connection.uncertain + getStorageIsolation(database).assertAvailable() + if (connection.active || database.isTransaction) + throw new Error('Committed state cannot be queried from an active transaction') +} + +export function withTransaction(database: DatabaseSync, operation: SynchronousOperation): T { + const connection = getConnection(database) + if (connection.uncertain) + throw connection.uncertain + getStorageIsolation(database).assertAvailable() + if (connection.active) { + try { + return participate(connection.active, operation) + } + catch (error) { + closeAutoRolledBackConnection(database, connection.active) + throw error + } + } + if (database.isTransaction) + throw new Error('Storage transaction must be owned by withTransaction') + + const context: TransactionContext = { failure: null, ended: null, notifications: [] } + let phase: TransactionPhase = 'begin' + let result: T + try { + database.exec('BEGIN IMMEDIATE') + connection.active = context + phase = 'operation' + result = participate(context, operation) + if (context.failure) + throw context.failure.error + phase = 'commit' + database.exec('COMMIT') + } + catch (cause) { + const original = context.failure ? context.failure.error : cause + const error = isObject(original) ? original : new Error('Storage transaction failed', { cause: original }) + const failure = context.ended ?? rollback(database, phase) + failures.set(error, failure) + if (failure.outcome === 'unknown') { + connection.uncertain = new BuddyTransactionUncertainError(error, failure) + getStorageIsolation(database).isolate(connection.uncertain) + throw connection.uncertain + } + throw error + } + finally { + connection.active = null + } + connection.notifications.fireBatch(context.notifications) + return result +} + +export function afterTransactionCommit(database: DatabaseSync, notify: () => unknown, onError: (error: unknown) => void): void { + const connection = getConnection(database) + if (connection.uncertain) + throw connection.uncertain + const notification = new Emitter(onError) + notification.event(notify, { once: true }) + const publish = () => notification.fire() + if (connection.active) { + if (connection.active.failure) + throw connection.active.failure.error + connection.active.notifications.push(publish) + } + else { + if (database.isTransaction) + throw new Error('Storage transaction must be owned by withTransaction') + connection.notifications.fire(publish) + } +} + +function getConnection(database: DatabaseSync): ConnectionTransactions { + let connection = connections.get(database) + if (!connection) { + const notifications = new Emitter<() => unknown>(() => {}) + notifications.event(notify => notify()) + connection = { active: null, uncertain: null, notifications } + connections.set(database, connection) + } + return connection +} + +function participate(context: TransactionContext, operation: () => T): T { + if (context.failure) + throw context.failure.error + try { + if (isAsyncFunction(operation)) + throw new TypeError('Storage transactions require synchronous operations') + const result = operation() + if (isObject(result) && 'then' in result && typeof result.then === 'function') { + void Promise.resolve(result).catch(() => {}) + throw new TypeError('Storage transactions cannot return a promise') + } + return result + } + catch (error) { + context.failure ??= { error } + throw error + } +} + +function rollback(database: DatabaseSync, phase: TransactionPhase): TransactionFailure { + try { + if (database.isTransaction) { + database.exec('ROLLBACK') + if (database.isTransaction) + throw new Error('Storage transaction remains active after rollback') + return { phase, outcome: 'rolled_back' } + } + return { phase, outcome: phase === 'begin' ? 'not_started' : phase === 'operation' ? 'rolled_back' : 'unknown' } + } + catch (cleanupError) { + return { phase, outcome: 'unknown', cleanupError } + } +} + +function closeAutoRolledBackConnection(database: DatabaseSync, context: TransactionContext): void { + if (context.ended) + return + try { + if (!database.isTransaction) { + context.ended = { phase: 'operation', outcome: 'rolled_back' } + database.close() + } + } + catch (cleanupError) { + context.ended = { phase: 'operation', outcome: 'unknown', cleanupError } + } +} + +function isObject(value: unknown): value is object { + return value !== null && (typeof value === 'object' || typeof value === 'function') +} diff --git a/apps/buddy/service/src/storage/turnRequestRepository.ts b/apps/buddy/service/src/storage/turnRequestRepository.ts index f8373c05b..6c1625e27 100644 --- a/apps/buddy/service/src/storage/turnRequestRepository.ts +++ b/apps/buddy/service/src/storage/turnRequestRepository.ts @@ -11,6 +11,7 @@ import { buddyDraftParticipantsSchema } from '../../../shared/conversation/compo import { isExecutionProfileWithin } from '../../../shared/permissions/executionProfile' import { BuddyServiceError } from '../rpc/runtimeRequest' import { createComposerDraftCommitter } from './commitComposerDraft' +import { createConversationHistoryStore } from './conversationHistoryRepository' import { freezeConversationMemberModel } from './conversationMemberRepository' import { CONVERSATION_CURRENT_MODEL_SELECTION_SQL } from './conversationRecord' import { withTransaction } from './database' @@ -51,7 +52,7 @@ export function assertTurnInputDelivery(database: DatabaseSync, input: PrepareTu if (boundary === 'dispatch' && target && (target.phase !== 'closed' || target.cleanupState !== 'clean')) throw new BuddyServiceError('INPUT_DELIVERY_TARGET_UNAVAILABLE') if (!target && database.prepare(`SELECT 1 FROM runs r JOIN execution_state e ON e.run_id = r.id - WHERE r.conversation_id = ? AND r.buddy_id = ? AND (r.status IN ('queued', 'running') OR e.phase <> 'closed' OR e.cleanup_state <> 'clean') LIMIT 1`).get(input.conversationId, delivery.actorId)) { + WHERE r.conversation_id = ? AND r.branch_id = ? AND r.buddy_id = ? AND (r.status IN ('queued', 'running') OR e.phase <> 'closed' OR e.cleanup_state <> 'clean') LIMIT 1`).get(input.conversationId, input.branchId, delivery.actorId)) { throw new BuddyServiceError('INPUT_DELIVERY_TARGET_UNAVAILABLE') } if (target && delivery.mode === 'current') { @@ -79,6 +80,7 @@ export function resolveTurnExecutionProfile(input: { executionProfile: BuddyExec } export interface PrepareTurnRequestInput { + participants?: { leaderId: string, actorIds: readonly string[] } | null delivery?: BuddyInputDelivery leaderId?: string expectedActorModelSelection?: ConversationModelSelection | null @@ -96,7 +98,7 @@ export interface PrepareTurnRequestInput { draft: { draftId: string expectedRevision: number - } + } | null executionProfile: BuddyExecutionProfile runExecutionProfile?: BuddyExecutionProfile model: string @@ -260,6 +262,14 @@ export interface TurnRequestRepository { export function createTurnRequestRepository(database: DatabaseSync): TurnRequestRepository { const executions = createExecutionRepository(database) const threads = createThreadBindingRepository(database) + const history = createConversationHistoryStore(database).repository + const hasMaintenance = (conversationId: string) => !!database.prepare('SELECT 1 FROM runs WHERE conversation_id = ? AND execution_kind = \'maintenance\' AND status IN (\'queued\', \'running\') LIMIT 1').get(conversationId) + const hasBranch = (conversationId: string, branchId: string) => !!database.prepare('SELECT 1 FROM conversation_branches WHERE conversation_id = ? AND id = ?').get(conversationId, branchId) + const bindBranchThread = (runId: string, conversationId: string, branchId: string, actorId: string, createdAt: string) => { + const thread = threads.ensureActor(conversationId, actorId, createdAt, branchId) + database.prepare('UPDATE runs SET thread_id = ? WHERE id = ?').run(thread.id, runId) + return thread.id + } const insertTreeSource = database.prepare('INSERT INTO run_tree_sources (run_id, source_run_id, position) VALUES (?, ?, ?)') const findFollowupSource = database.prepare(` SELECT messages.id FROM messages INNER JOIN runs ON runs.id = messages.run_id @@ -321,8 +331,10 @@ export function createTurnRequestRepository(database: DatabaseSync): TurnRequest } if (input.expectedActorModelSelection !== undefined) { const member = database.prepare('SELECT model_selection_json FROM conversation_members WHERE conversation_id = ? AND actor_id = ?').get(input.conversationId, actorId) as { model_selection_json: string | null } | undefined - if (!sameModelSelection(member?.model_selection_json ?? null, input.expectedActorModelSelection ? JSON.stringify(input.expectedActorModelSelection) : null)) + if (!sameModelSelection(member?.model_selection_json ?? null, input.expectedActorModelSelection ? JSON.stringify(input.expectedActorModelSelection) : null) + && !sameModelSelection(member?.model_selection_json ?? null, JSON.stringify(input.modelOverride ?? selection))) { throw new BuddyServiceError('BUDDY_CONFLICT') + } } const member = database.prepare('SELECT model_selection_json FROM conversation_members WHERE conversation_id = ? AND actor_id = ? AND removed_at IS NULL').get(input.conversationId, actorId) as { model_selection_json: string | null } | undefined if (!member) @@ -362,8 +374,8 @@ export function createTurnRequestRepository(database: DatabaseSync): TurnRequest SELECT ?, NULL, ?, ?, attachments.name, attachments.mime_type, attachments.size_bytes, ?, attachments.name_source, attachments.source_path FROM attachments - INNER JOIN messages ON messages.id = attachments.message_id - WHERE attachments.id = ? AND messages.conversation_id = ? + LEFT JOIN messages ON messages.id = attachments.message_id + WHERE attachments.id = ? AND (messages.conversation_id = ? OR (attachments.draft_id = ? AND attachments.message_id IS NULL)) `) const insertMessage = database.prepare(` INSERT INTO messages ( @@ -399,11 +411,6 @@ export function createTurnRequestRepository(database: DatabaseSync): TurnRequest const findMessage = database.prepare(` SELECT conversation_id, role FROM messages WHERE id = ? `) - const findIncompleteRun = database.prepare(` - SELECT 1 FROM runs - WHERE conversation_id = ? AND execution_kind IN ('root', 'maintenance') AND status IN ('queued', 'running') - LIMIT 1 - `) const findPreviousCompletedSession = database.prepare(` SELECT pi_session_file FROM runs WHERE thread_id = ? AND branch_id = ? @@ -458,15 +465,17 @@ export function createTurnRequestRepository(database: DatabaseSync): TurnRequest if ( !conversation || conversation.space_id !== input.spaceId - || conversation.active_branch_id !== input.parentBranchId + || !hasBranch(input.conversationId, input.parentBranchId) + || hasMaintenance(input.conversationId) || conversation.approval_policy !== input.approvalPolicy || conversation.execution_profile !== input.executionProfile || conversation.deleted_at !== null - || findIncompleteRun.get(input.conversationId) + || !history.listBranchMessages(input.conversationId, input.parentBranchId).some(message => message.id === input.sourceUserMessageId) || !sourceMessage || sourceMessage.conversation_id !== input.conversationId || sourceMessage.role !== 'user' - || (input.forkedFromMessageId && forkMessage?.conversation_id !== input.conversationId) + || (input.forkedFromMessageId && (forkMessage?.conversation_id !== input.conversationId + || !history.listBranchMessages(input.conversationId, input.parentBranchId).some(message => message.id === input.forkedFromMessageId))) ) { throw new TurnRequestConflictError() } @@ -477,8 +486,6 @@ export function createTurnRequestRepository(database: DatabaseSync): TurnRequest input.forkedFromMessageId, input.createdAt, ) - activateBranch.run(input.branchId, input.createdAt, input.conversationId) - insertMessage.run( input.userMessageId, input.conversationId, @@ -507,6 +514,7 @@ export function createTurnRequestRepository(database: DatabaseSync): TurnRequest input.buddyId ?? conversation!.buddy_id, input.actorSnapshot ? JSON.stringify(input.actorSnapshot) : null, ) + bindBranchThread(input.runId, input.conversationId, input.branchId, input.buddyId ?? conversation!.buddy_id, input.createdAt) insertRunInput.run( input.runId, input.runInput.prompt, @@ -517,18 +525,20 @@ export function createTurnRequestRepository(database: DatabaseSync): TurnRequest input.createdAt, ) const modelChanged = persistActor(input, { providerId: input.provider, modelId: input.model, reasoning: input.runInput.reasoning, serviceTier: input.runInput.serviceTier }) - const draftReceipt = commitDraft({ - approvalPolicy: input.approvalPolicy, - branchId: input.branchId, - conversationId: input.conversationId, - draftId: input.draft.draftId, - executionProfile: input.executionProfile, - expectedBranchId: input.parentBranchId, - expectedRevision: input.draft.expectedRevision, - expectedSourceMessageId: input.sourceUserMessageId, - spaceId: input.spaceId, - updatedAt: input.createdAt, - }) + const draftReceipt = input.draft + ? commitDraft({ + approvalPolicy: input.approvalPolicy, + branchId: input.branchId, + conversationId: input.conversationId, + draftId: input.draft.draftId, + executionProfile: input.executionProfile, + expectedBranchId: input.parentBranchId, + expectedRevision: input.draft.expectedRevision, + expectedSourceMessageId: input.sourceUserMessageId, + spaceId: input.spaceId, + updatedAt: input.createdAt, + }) + : null insertDraftRequest.run( input.requestId, input.requestFingerprint, @@ -536,9 +546,9 @@ export function createTurnRequestRepository(database: DatabaseSync): TurnRequest input.branchId, input.runId, input.createdAt, - input.draft.draftId, - input.draft.expectedRevision, - draftReceipt.committedRevision, + input.draft?.draftId ?? null, + input.draft?.expectedRevision ?? null, + draftReceipt?.committedRevision ?? null, ) return { branchId: input.branchId, @@ -568,7 +578,7 @@ export function createTurnRequestRepository(database: DatabaseSync): TurnRequest } let questionOwner: ReturnType = null let questionFileAccessCeiling = false - if (!input.draft) { + if ('questionReply' in input) { const question = database.prepare('SELECT request_json FROM user_questions WHERE id = ? AND run_id = ?').get(input.questionReply.requestId, input.questionReply.sourceRunId) const request = question && JSON.parse(String(question.request_json)) if (!request || request.status !== 'answered' || request.conversationId !== input.conversationId || request.branchId !== input.branchId @@ -606,22 +616,21 @@ export function createTurnRequestRepository(database: DatabaseSync): TurnRequest if (conversation) { if ( conversation.space_id !== input.spaceId - || (!input.followup && conversation.active_branch_id !== input.branchId) + || !hasBranch(input.conversationId, input.followup?.parentBranchId ?? input.branchId) + || hasMaintenance(input.conversationId) || conversation.approval_policy !== input.approvalPolicy || conversation.execution_profile !== input.executionProfile || conversation.deleted_at !== null - || (input.followup - ? findIncompleteRun.get(input.conversationId) - : database.prepare(`SELECT 1 FROM runs + || (!input.followup && database.prepare(`SELECT 1 FROM runs WHERE conversation_id = ? AND status IN ('queued', 'running') - AND (buddy_id = ? OR execution_kind = 'maintenance' OR (? = 0 AND execution_kind = 'root' AND scheduling_lane = 'conversation')) LIMIT 1`) - .get(input.conversationId, buddyId, actorLane ? 1 : 0)) + AND (execution_kind = 'maintenance' OR (branch_id = ? AND (buddy_id = ? OR (? = 0 AND execution_kind = 'root' AND scheduling_lane = 'conversation')))) LIMIT 1`) + .get(input.conversationId, input.branchId, buddyId, actorLane ? 1 : 0)) ) { throw new TurnRequestConflictError() } } else { - if (!input.draft) + if ('questionReply' in input) throw new TurnRequestConflictError() insertConversation.run( input.conversationId, @@ -633,8 +642,8 @@ export function createTurnRequestRepository(database: DatabaseSync): TurnRequest input.executionProfile, input.leaderId ?? buddyId, ) - const draft = database.prepare('SELECT participants_json FROM composer_drafts WHERE id = ? AND revision = ?').get(input.draft.draftId, input.draft.expectedRevision) - const participants = draft?.participants_json ? buddyDraftParticipantsSchema.parse(JSON.parse(String(draft.participants_json))) : null + const draft = input.draft ? database.prepare('SELECT participants_json FROM composer_drafts WHERE id = ? AND revision = ?').get(input.draft.draftId, input.draft.expectedRevision) : null + const participants = draft?.participants_json ? buddyDraftParticipantsSchema.parse(JSON.parse(String(draft.participants_json))) : input.participants ?? null if (participants && participants.leaderId !== (input.leaderId ?? buddyId)) throw new BuddyServiceError('DRAFT_CONFLICT') for (const id of participants?.actorIds ?? [input.leaderId ?? buddyId]) { @@ -652,13 +661,15 @@ export function createTurnRequestRepository(database: DatabaseSync): TurnRequest throw new TurnRequestConflictError() } insertForkBranch.run(input.branchId, input.conversationId, input.followup.parentBranchId, input.followup.sourceMessageId, input.createdAt) - activateBranch.run(input.branchId, input.createdAt, input.conversationId) } - const actorThread = (input.actorIds?.length === 1 || input.delivery?.mode === 'next') && !input.followup + const previousActorThread = (input.actorIds?.length === 1 || input.delivery?.mode === 'next') && !input.followup ? database.prepare(`SELECT thread_id FROM runs WHERE conversation_id = ? AND branch_id = ? AND buddy_id = ? AND status IN ('completed', 'failed', 'cancelled') AND pi_session_file IS NOT NULL ORDER BY rowid DESC LIMIT 1`).get(input.conversationId, input.branchId, buddyId)?.thread_id as string | undefined : undefined - const threadId = questionOwner?.run.threadId ?? actorThread ?? actorThreadId(input.conversationId, buddyId) + const preferredThread = questionOwner?.run.threadId ?? previousActorThread + const reusableThread = preferredThread && preferredThread !== actorThreadId(input.conversationId, buddyId) + && !database.prepare('SELECT 1 FROM runs WHERE thread_id = ? AND branch_id <> ? LIMIT 1').get(preferredThread, input.branchId) + const threadId = reusableThread ? preferredThread : threads.ensureActor(input.conversationId, buddyId, input.createdAt, input.branchId).id const previous = threads.findPreviousSession(threadId, input.branchId) insertMessage.run( input.userMessageId, @@ -770,7 +781,8 @@ export function createTurnRequestRepository(database: DatabaseSync): TurnRequest || conversation.approval_policy !== input.approvalPolicy || conversation.execution_profile !== input.executionProfile || conversation.deleted_at !== null - || findIncompleteRun.get(input.conversationId) + || !hasBranch(input.conversationId, input.parentBranchId) + || hasMaintenance(input.conversationId) || !sourceRun || sourceRun.conversation_id !== input.conversationId || sourceRun.triggering_message_id !== input.forkedFromMessageId @@ -786,7 +798,6 @@ export function createTurnRequestRepository(database: DatabaseSync): TurnRequest input.forkedFromMessageId, input.createdAt, ) - activateBranch.run(input.branchId, input.createdAt, input.conversationId) insertRun.run( input.runId, input.conversationId, @@ -803,6 +814,7 @@ export function createTurnRequestRepository(database: DatabaseSync): TurnRequest input.buddyId ?? conversation!.buddy_id, input.actorSnapshot ? JSON.stringify(input.actorSnapshot) : null, ) + bindBranchThread(input.runId, input.conversationId, input.branchId, input.buddyId ?? conversation!.buddy_id, input.createdAt) if (Number(cloneRunInput.run(input.runId, input.createdAt, input.sourceRunId).changes) !== 1) throw new TurnRequestConflictError() updateRunSelection.run(input.reasoning, input.serviceTier, input.runId) @@ -846,11 +858,11 @@ export function createTurnRequestRepository(database: DatabaseSync): TurnRequest throw new TurnRequestConflictError() if (run.status !== 'failed' || run.error_code !== 'RUNTIME_RESTARTED') return toRecord(request, false) - if (findIncompleteRun.get(run.conversation_id)) + if (database.prepare('SELECT 1 FROM runs WHERE conversation_id = ? AND branch_id = ? AND status IN (\'queued\', \'running\') LIMIT 1').get(run.conversation_id, run.branch_id)) throw new TurnRequestConflictError() const previous = findPreviousCompletedSession.get( - actorThreadId(run.conversation_id, run.buddy_id), + actorThreadId(run.conversation_id, run.buddy_id, run.branch_id), run.branch_id, ) as { pi_session_file: string } | undefined insertRun.run( @@ -869,6 +881,7 @@ export function createTurnRequestRepository(database: DatabaseSync): TurnRequest run.buddy_id, run.actor_snapshot_json ?? null, ) + bindBranchThread(input.runId, run.conversation_id, run.branch_id, run.buddy_id, input.createdAt) database.prepare('UPDATE runs SET model_override_json = ? WHERE id = ?').run(run.model_override_json ?? null, input.runId) if (Number(cloneRunInput.run(input.runId, input.createdAt, request.run_id).changes) !== 1) throw new TurnRequestConflictError() @@ -934,7 +947,8 @@ function sameModelSelection(left: string | null, right: string | null): boolean function createTurnFacts(input: { taskCreated?: boolean, branchCreated?: boolean, parentBranchId?: string | null, messageId?: string, attachmentIds?: readonly string[], draftReceipt?: ComposerDraftCommitReceipt | null, queueId?: string, modelChanged?: boolean }): TurnCommitFact[] { return [ ...(input.taskCreated ? [{ kind: 'task.created' as const }] : []), - ...(input.branchCreated ? [{ kind: 'branch.created' as const, parentBranchId: input.parentBranchId ?? null }, { kind: 'task.branch_activated' as const }] : []), + ...(input.branchCreated ? [{ kind: 'branch.created' as const, parentBranchId: input.parentBranchId ?? null }] : []), + ...(input.taskCreated ? [{ kind: 'task.branch_activated' as const }] : []), ...(input.modelChanged ? [{ kind: 'task.model_changed' as const }] : []), ...(input.messageId ? [{ kind: 'message.created' as const, messageId: input.messageId }] : []), ...(input.messageId && input.attachmentIds?.length ? [{ kind: 'attachments.bound' as const, messageId: input.messageId, attachmentIds: input.attachmentIds }] : []), @@ -951,15 +965,13 @@ function bindAttachments( ): void { assertTurnAttachmentBindings(input) for (const binding of input.attachmentBindings) { - const changes = binding.sourceDraftId !== null - ? binding.id === binding.sourceAttachmentId - ? Number(bindDraftAttachment.run( - binding.messageId, - binding.storedPath, - binding.sourceAttachmentId, - binding.sourceDraftId, - ).changes) - : 0 + const changes = binding.sourceDraftId !== null && binding.id === binding.sourceAttachmentId + ? Number(bindDraftAttachment.run( + binding.messageId, + binding.storedPath, + binding.sourceAttachmentId, + binding.sourceDraftId, + ).changes) : Number(cloneMessageAttachment.run( binding.id, binding.messageId, @@ -967,6 +979,7 @@ function bindAttachments( binding.createdAt, binding.sourceAttachmentId, input.conversationId, + binding.sourceDraftId, ).changes) if (changes !== 1) throw new TurnRequestAttachmentError() diff --git a/apps/buddy/service/src/storage/verifyCommandReceipt.ts b/apps/buddy/service/src/storage/verifyCommandReceipt.ts new file mode 100644 index 000000000..918bd6b3f --- /dev/null +++ b/apps/buddy/service/src/storage/verifyCommandReceipt.ts @@ -0,0 +1,24 @@ +import type { StorageCommand } from './CommandCommitter' +import { isAbsolute } from 'node:path' +import { DatabaseSync } from 'node:sqlite' +import { CommandCommitter } from './CommandCommitter' +import { BUDDY_MIGRATION_SCHEMA_VERSION } from './schema' +import { requireSqliteAuthority } from './sqliteAuthority' + +export function verifyCommandReceipt(options: { + databasePath: string + identity: Pick +}) { + if (!isAbsolute(options.databasePath)) + throw new TypeError('Receipt verification requires an existing absolute database path') + const database = new DatabaseSync(options.databasePath, { readOnly: true }) + try { + if (database.prepare('PRAGMA user_version').get()!.user_version !== BUDDY_MIGRATION_SCHEMA_VERSION) + throw new Error('Receipt verification requires the supported storage schema') + requireSqliteAuthority(database) + return new CommandCommitter(database).find(options.identity) + } + finally { + database.close() + } +} diff --git a/apps/buddy/service/src/validateServiceRelease.ts b/apps/buddy/service/src/validateServiceRelease.ts new file mode 100644 index 000000000..0c3cdbfcb --- /dev/null +++ b/apps/buddy/service/src/validateServiceRelease.ts @@ -0,0 +1,15 @@ +import type { ServiceLaunch } from '../../shared/service/localServiceProtocol' +import { realpath } from 'node:fs/promises' +import { dirname, join } from 'node:path' +import process from 'node:process' +import { readReleaseSet } from '../../platform/service/releaseSet' +import { readServiceSession } from '../../platform/service/SessionServiceRegistration' + +export async function validateServiceRelease(config: ServiceLaunch): Promise { + const session = await readServiceSession() + if (session.sessionId !== config.sessionId || session.userId !== config.userId) + throw new Error('SERVICE_SESSION_CHANGED') + const release = await readReleaseSet(config.releaseSetPath) + if (release.manifest.buildId !== config.buildId || await realpath(config.coreEntry) !== release.coreEntry || await realpath(config.nodeExecutable) !== release.nodeExecutable || await realpath(process.execPath) !== release.nodeExecutable || process.versions.node !== release.manifest.node.version || await realpath(config.resourcesRoot) !== await realpath(join(dirname(release.coreEntry), 'resources'))) + throw new Error('SERVICE_RELEASE_IDENTITY_CHANGED') +} diff --git a/apps/buddy/service/src/web/WebSettingsService.ts b/apps/buddy/service/src/web/WebSettingsService.ts index 158459678..14b5efc2d 100644 --- a/apps/buddy/service/src/web/WebSettingsService.ts +++ b/apps/buddy/service/src/web/WebSettingsService.ts @@ -1,14 +1,13 @@ import type { EventSnapshot } from '../../../shared/events/eventTypes' import type { WebSettings, WebSettingsSnapshot } from '../../../shared/network/webProtocol' import type { RuntimeRpcPeerContract } from '../../../shared/runtime/rpcPeer' +import type { WebSecretStore } from '../platform/RuntimePlatform' import type { WorkspaceRepository } from '../storage/workspaceRepository' import { randomUUID } from 'node:crypto' import { Emitter } from '../../../shared/events/Emitter' import { copyEventSnapshot } from '../../../shared/events/eventSnapshot' import { webRpc } from '../../../shared/network/webApi' import { DEFAULT_WEB_SETTINGS, webCredentialInputSchema, webSearchSourceSchema, webSettingsSchema } from '../../../shared/network/webProtocol' -import { credentialMutationResultSchema, credentialReadResultSchema } from '../../../shared/runtime/credentialProtocol' -import { HostCredentialStoreError } from '../providers/HostCredentialStore' import { BuddyServiceError, parse, registerRuntimeRequest } from '../rpc/runtimeRequest' const SETTINGS_KEY = 'buddy.web' @@ -23,15 +22,15 @@ export class WebSettingsService { #configured: boolean | null = null #disposing: Promise | undefined readonly repository: Pick - readonly peer: Pick + readonly secrets: WebSecretStore #mutation: Promise = Promise.resolve() constructor( repository: Pick, - peer: Pick, + secrets: WebSecretStore, ) { this.repository = repository - this.peer = peer + this.secrets = secrets } get(): WebSettings { @@ -49,13 +48,7 @@ export class WebSettingsService { async getTavilyKey(): Promise { const revision = this.#revision - const result = credentialReadResultSchema.parse(await this.peer.request('host.secrets.read', { - namespace: 'web', - id: 'tavily', - })) - if (!result.ok) - throw new HostCredentialStoreError(result.error.code) - const key = typeof result.value === 'string' && result.value ? result.value : null + const key = await this.secrets.read() if (!this.#disposing && revision === this.#revision && this.#configured !== Boolean(key)) { this.#configured = Boolean(key) this.#publish('credential-observed', randomUUID()) @@ -86,12 +79,7 @@ export class WebSettingsService { this.#commitSettings(settings, operationId) } try { - const result = credentialMutationResultSchema.parse(await this.peer.request( - key === null ? 'host.secrets.delete' : 'host.secrets.write', - { namespace: 'web', id: 'tavily', ...(key === null ? {} : { value: key }) }, - )) - if (!result.ok) - throw new HostCredentialStoreError(result.error.code) + await this.secrets.write(key) this.#configured = key !== null this.#publish('credential-committed', operationId) } diff --git a/apps/buddy/service/src/web/__tests__/WebSettingsService.spec.ts b/apps/buddy/service/src/web/__tests__/WebSettingsService.spec.ts index f72fe5b4e..f0a7a1611 100644 --- a/apps/buddy/service/src/web/__tests__/WebSettingsService.spec.ts +++ b/apps/buddy/service/src/web/__tests__/WebSettingsService.spec.ts @@ -11,13 +11,11 @@ function fixture() { let key: string | null = null let failing = false const service = new WebSettingsService(createWorkspaceRepository(database), { - request: async (method, input) => { - if (method === 'host.secrets.read') - return { ok: true, value: key } + read: async () => key, + write: async (value) => { if (failing) throw new Error('fixture-private-credential-failure') - key = method === 'host.secrets.delete' ? null : (input as { value: string }).value - return { ok: true } + key = value }, }) return { service, database, fail: () => { @@ -46,15 +44,15 @@ describe('web settings ownership', () => { }) it('fences a late credential read and suppresses unchanged read observations', async () => { const { service, database } = fixture() - const delayed = Promise.withResolvers() - const original = service.peer.request + const delayed = Promise.withResolvers() + const original = service.secrets.read let delay = true - service.peer.request = (method, input) => { - if (method === 'host.secrets.read' && delay) { + service.secrets.read = () => { + if (delay) { delay = false return delayed.promise } - return original(method, input) + return original() } try { const changes: unknown[] = [] @@ -62,7 +60,7 @@ describe('web settings ownership', () => { const reading = service.getTavilyKey() await service.saveCredential({ key: 'fixture-only-key' }) const revision = service.state.revision - delayed.resolve({ ok: true, value: null }) + delayed.resolve(null) await reading expect(service.state).toMatchObject({ revision, tavilyKeyConfigured: true }) await service.getTavilyKey() diff --git a/apps/buddy/service/src/web/observeWebDiagnostics.ts b/apps/buddy/service/src/web/observeWebDiagnostics.ts index 5e8400c98..fd7bee6a8 100644 --- a/apps/buddy/service/src/web/observeWebDiagnostics.ts +++ b/apps/buddy/service/src/web/observeWebDiagnostics.ts @@ -17,7 +17,7 @@ export function observeWebDiagnostics(service: WebCapabilityService, report: App })) } -export function observeWebHostDiagnostics(client: WebHostClient, report: ApplicationDiagnosticReporter) { +export function observeWebHostDiagnostics(client: Pick, report: ApplicationDiagnosticReporter) { return client.onDidChange(change => report({ event: `web.transport.${change.kind}.${change.phase.replaceAll('-', '_')}${change.outcome ? `_${change.outcome}` : ''}${change.cancelled ? '_after_cancel' : ''}`, component: 'runtime.web_host', diff --git a/apps/buddy/service/src/web/webContent.ts b/apps/buddy/service/src/web/webContent.ts index 03359e7b9..08c5d4f5f 100644 --- a/apps/buddy/service/src/web/webContent.ts +++ b/apps/buddy/service/src/web/webContent.ts @@ -53,7 +53,7 @@ export function extractHtml(html: string, url: string): WebDocument { catch { element.removeAttribute(attribute) } } } - const article = new Readability(document as unknown as Document, { charThreshold: 120 }).parse() + const article = new Readability(document as unknown as ConstructorParameters[0], { charThreshold: 120 }).parse() const source = article?.content || document.querySelector('main, article')?.innerHTML || document.body?.innerHTML || '' const markdown = new TurndownService({ headingStyle: 'atx', codeBlockStyle: 'fenced' }) markdown.addRule('table', { diff --git a/apps/buddy/shared/browser/browserDesktopApi.ts b/apps/buddy/shared/browser/browserDesktopApi.ts index 4cca19c37..841a642c1 100644 --- a/apps/buddy/shared/browser/browserDesktopApi.ts +++ b/apps/buddy/shared/browser/browserDesktopApi.ts @@ -68,12 +68,22 @@ export interface DesktopBrowserState { } export interface DesktopBrowserGuestDescriptor { + pageId?: string owner?: BrowserPageOwner conversationId?: string partition: string sessionId: string } +export interface DesktopBrowserPresentationInput { + presentationId: string + sessionId: string + pageId: string + visible: boolean + bounds: { x: number, y: number, width: number, height: number } + clip: { x: number, y: number, width: number, height: number } +} + export interface DesktopBrowserAttachGuestInput { sessionId: string webContentsId: number @@ -109,6 +119,8 @@ export interface DesktopBrowserSetSurfaceInput { } export interface DesktopBrowserApi { + acquirePresentation: () => Promise + present: (input: DesktopBrowserPresentationInput) => Promise pickElement: (sessionId: string, requestId: string) => Promise cancelElementPick: (sessionId: string, requestId: string) => Promise locateElement: (input: BrowserLocateElementInput) => Promise diff --git a/apps/buddy/shared/browser/browserDesktopSchemas.ts b/apps/buddy/shared/browser/browserDesktopSchemas.ts index 28359e1fd..b6915fc5a 100644 --- a/apps/buddy/shared/browser/browserDesktopSchemas.ts +++ b/apps/buddy/shared/browser/browserDesktopSchemas.ts @@ -66,6 +66,7 @@ export const browserAttachGuestInputSchema: z.ZodType = z.object({ + pageId: z.uuid().optional(), owner: browserPageOwnerSchema.optional(), conversationId: browserConversationIdSchema.optional(), partition: z.string().trim().min(1).max(256), @@ -131,3 +132,19 @@ export const desktopBrowserStateSchema: z.ZodType = z.objec url: z.union([z.literal('about:blank'), browserPageUrlSchema]), visible: z.boolean(), }).strict() + +const browserPresentationRectSchema = z.object({ + x: z.number().finite().min(-100_000).max(100_000), + y: z.number().finite().min(-100_000).max(100_000), + width: z.number().finite().nonnegative().max(100_000), + height: z.number().finite().nonnegative().max(100_000), +}).strict() + +export const browserPresentationInputSchema = z.object({ + presentationId: z.uuid(), + sessionId: browserSessionIdSchema, + pageId: z.uuid(), + visible: z.boolean(), + bounds: browserPresentationRectSchema, + clip: browserPresentationRectSchema, +}).strict() diff --git a/apps/buddy/shared/conversation/__tests__/contentBehaviorVectors.spec.ts b/apps/buddy/shared/conversation/__tests__/contentBehaviorVectors.spec.ts new file mode 100644 index 000000000..c1385d929 --- /dev/null +++ b/apps/buddy/shared/conversation/__tests__/contentBehaviorVectors.spec.ts @@ -0,0 +1,17 @@ +import { describe, expect, it } from 'vitest' +import vectors from '../../productProtocol/content-behavior-vectors.json' +import { buddyUserContentToText, buddyUserContentV1Schema, createBuddyUserContent } from '../buddyUserContent' + +describe('cross-client content behavior', () => { + it.each(vectors.textCases)('$name retains line boundaries and UTF-16 content', (entry) => { + const content = createBuddyUserContent(entry.text) + expect(buddyUserContentToText(content)).toBe(entry.text) + expect(content.body).toHaveLength(entry.desktopParagraphs) + }) + it.each(vectors.contents)('$name preserves source metadata', (entry) => { + const result = buddyUserContentV1Schema.safeParse(entry.value) + expect(result.success).toBe(entry.valid) + if (result.success) + expect(buddyUserContentToText(result.data)).toBe(entry.desktopText) + }) +}) diff --git a/apps/buddy/shared/conversation/actorThread.ts b/apps/buddy/shared/conversation/actorThread.ts index 72e640f8e..4d115ed6e 100644 --- a/apps/buddy/shared/conversation/actorThread.ts +++ b/apps/buddy/shared/conversation/actorThread.ts @@ -1,3 +1,3 @@ -export function actorThreadId(conversationId: string, actorId: string): string { - return `actor:${JSON.stringify([conversationId, actorId])}` +export function actorThreadId(conversationId: string, actorId: string, branchId?: string): string { + return `actor:${JSON.stringify(branchId ? [conversationId, actorId, branchId] : [conversationId, actorId])}` } diff --git a/apps/buddy/shared/conversation/chatApi.ts b/apps/buddy/shared/conversation/chatApi.ts index 2d1b47b4e..00461a328 100644 --- a/apps/buddy/shared/conversation/chatApi.ts +++ b/apps/buddy/shared/conversation/chatApi.ts @@ -70,5 +70,5 @@ export const chatRpc = { editUserMessage: { method: 'chat.editUserMessage', input: chatRequestSchemas.editUserMessage, response: chatResponseSchemas.turnStart }, executeCommand: { method: 'chat.executeCommand', input: chatRequestSchemas.chatCommand, response: chatResponseSchemas.turnStart }, regenerateAssistant: { method: 'chat.regenerateAssistant', input: chatRequestSchemas.regenerateAssistant, response: chatResponseSchemas.turnStart }, - cancel: { method: 'chat.cancel', input: runsRequestSchemas.runId, response: runsResponseSchemas.run }, + cancel: { method: 'chat.cancel', input: runsRequestSchemas.cancel, response: runsResponseSchemas.run }, } as const satisfies Record diff --git a/apps/buddy/shared/conversation/chatQueueApi.ts b/apps/buddy/shared/conversation/chatQueueApi.ts index 1f3fcc472..8537db63b 100644 --- a/apps/buddy/shared/conversation/chatQueueApi.ts +++ b/apps/buddy/shared/conversation/chatQueueApi.ts @@ -16,16 +16,20 @@ export const chatQueueItemSchema = z.object({ content: buddyUserMessageContentV1Schema, attachments: z.array(z.object({ id: idSchema, name: z.string(), mimeType: z.string(), sizeBytes: z.number() }).strict()), createdAt: z.string(), + revision: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER).optional(), + steerTarget: z.object({ runId: idSchema, generation: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER) }).strict().nullable().optional(), }).strict() const scope = z.object({ conversationId: idSchema, branchId: idSchema }).strict() -const target = scope.extend({ id: idSchema }).strict() +const target = scope.extend({ id: idSchema, expectedRevision: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER).optional() }).strict() +const steerTarget = target.extend({ targetRunId: idSchema.nullable().optional(), expectedRunGeneration: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER).optional() }).strict() export const queueReceiptSchema = scope.extend({ id: idSchema, - draftReceipt: z.object({ draftId: z.string(), sourceRevision: z.number(), committedRevision: z.number() }).strict(), + draftReceipt: z.object({ draftId: z.string(), sourceRevision: z.number(), committedRevision: z.number() }).strict().nullable(), }).strict() export type LocalChatQueueItem = DeepReadonly> export type LocalChatQueueScope = z.infer export type LocalChatQueueTarget = z.infer +export type LocalChatQueueSteerTarget = z.infer export type LocalChatQueueReceipt = z.infer const queueSteerResultSchema = z.enum(['accepted', 'incompatible', 'unavailable']) export type LocalChatQueueSteerResult = z.infer @@ -33,5 +37,5 @@ export const chatQueueRpc = { enqueue: { method: 'chat.queue.enqueue', input: buddyComposerDraftSendSchema.unwrap().extend({ delivery: buddyInputDeliverySchema.optional() }).strict(), response: queueReceiptSchema }, list: { method: 'chat.queue.list', input: scope, response: z.array(chatQueueItemSchema) }, cancel: { method: 'chat.queue.cancel', input: target, response: z.boolean() }, - steer: { method: 'chat.queue.steer', input: target, response: queueSteerResultSchema }, + steer: { method: 'chat.queue.steer', input: steerTarget, response: queueSteerResultSchema }, } as const satisfies Record diff --git a/apps/buddy/shared/conversation/composerDraft.ts b/apps/buddy/shared/conversation/composerDraft.ts index c72bfb639..e5d06b195 100644 --- a/apps/buddy/shared/conversation/composerDraft.ts +++ b/apps/buddy/shared/conversation/composerDraft.ts @@ -51,6 +51,7 @@ export const buddyDraftParticipantsSchema = z.object({ }).strict().refine(value => value.actorIds.includes(value.leaderId) && new Set(value.actorIds).size === value.actorIds.length).readonly() export const buddyComposerDraftSchema = z.object({ + slotRevision: z.number().int().nonnegative().optional(), content: buddyUserContentV1Schema, participants: buddyDraftParticipantsSchema.nullable().optional(), draftId: draftIdentitySchema, diff --git a/apps/buddy/shared/conversation/conversationApi.ts b/apps/buddy/shared/conversation/conversationApi.ts index e3eab8013..c6bd3e5c0 100644 --- a/apps/buddy/shared/conversation/conversationApi.ts +++ b/apps/buddy/shared/conversation/conversationApi.ts @@ -23,9 +23,26 @@ export const modelSelectionSchema = z.object({ serviceTier: z.enum(BUDDY_SERVICE_TIERS).nullable(), }).strict() +export const conversationActiveRunModelSchema = z.object({ + runId: idSchema, + generation: z.number().int().nonnegative(), + modelSelection: modelSelectionSchema, +}).strict() + +export interface ConversationModelMutationTarget { + actorId?: string + runId?: string + expectedRevision?: number + expectedRunGeneration?: number + expectedRunModelSelection?: z.infer | null + expectedActiveRun?: z.infer | null +} + export const conversationModelControlsSchema = z.object({ + metadataRevision: z.number().int().nonnegative().optional(), current: z.object({ runId: idSchema, + generation: z.number().int().nonnegative().optional(), actorId: idSchema, modelSelection: modelSelectionSchema, }).strict().nullable(), @@ -38,17 +55,18 @@ export const conversationModelControlsSchema = z.object({ export type ConversationModelControls = DeepReadonly> -export const conversationMemberSchema = z.object({ actorId: idSchema, joinedAt: timestampSchema }).strict() +export const conversationMemberSchema = z.object({ actorId: idSchema, joinedAt: timestampSchema, metadataRevision: z.number().int().nonnegative().optional() }).strict() export type LocalConversationMember = DeepReadonly> export const conversationSchema = z.object({ + metadataRevision: z.number().int().nonnegative().optional(), + titleRevision: z.number().int().nonnegative().optional(), buddyId: idSchema, activeBranchId: z.string().nullable(), approvalPolicy: approvalPolicySchema, createdAt: timestampSchema, deletedAt: nullableTimestampSchema, archivedAt: nullableTimestampSchema.optional(), - metadataRevision: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER).optional(), executionProfile: executionProfileSchema, id: idSchema, modelSelection: modelSelectionSchema.nullable(), @@ -128,7 +146,9 @@ export type ConversationListFilter = z.infer> export const conversationArchiveRpc = { - deletePermanently: { method: 'conversations.deletePermanently', input: conversationArchiveTargetSchema, response: z.boolean() }, archive: { method: 'conversations.archive', input: conversationArchiveTargetSchema, response: z.boolean() }, restoreArchive: { method: 'conversations.restoreArchive', input: conversationArchiveTargetSchema, response: z.boolean() }, deleteArchived: { method: 'conversations.deleteArchived', input: conversationArchiveTargetSchema, response: z.boolean() }, diff --git a/apps/buddy/shared/conversation/draftRecovery.ts b/apps/buddy/shared/conversation/draftRecovery.ts new file mode 100644 index 000000000..a0a87957b --- /dev/null +++ b/apps/buddy/shared/conversation/draftRecovery.ts @@ -0,0 +1,42 @@ +import { z } from 'zod' +import { productIdentitySchema } from '../productProtocol/identity' +import { buddyUserContentV1Schema } from './buddyUserContent' +import { buddyComposerDraftExecutionConfigSchema, buddyComposerDraftModelSelectionSchema, buddyDraftParticipantsSchema } from './composerDraft' + +export const draftRecoveryOwnerSchema = productIdentitySchema.pick({ serviceId: true, profileId: true, dataGeneration: true, deviceId: true }) +export const draftRecoverySnapshotSchema = z.object({ + targetKey: z.string().min(1).max(1024), + draftId: z.string().min(1).max(128), + editorSessionId: z.uuid(), + editVersion: z.number().int().nonnegative(), + revision: z.number().int().nonnegative().nullable(), + slotRevision: z.number().int().nonnegative().optional(), + confirmedSnapshot: z.string().max(4 * 1024 * 1024).nullable(), + content: buddyUserContentV1Schema, + approvalPolicy: buddyComposerDraftExecutionConfigSchema.unwrap().shape.approvalPolicy, + executionProfile: buddyComposerDraftExecutionConfigSchema.unwrap().shape.executionProfile, + modelSelection: buddyComposerDraftModelSelectionSchema.nullable(), + participants: buddyDraftParticipantsSchema.nullable().optional(), +}).strict() +export const draftRecoveryRecordSchema = z.object({ + owner: draftRecoveryOwnerSchema, + sequence: z.number().int().nonnegative(), + snapshot: draftRecoverySnapshotSchema, +}).strict() +export const draftRecoveryRemovalSchema = z.object({ + owner: draftRecoveryOwnerSchema, + editorSessionId: z.uuid(), + sequence: z.number().int().nonnegative(), +}).strict() +export const draftRecoveryLoadSchema = z.object({ owner: draftRecoveryOwnerSchema.nullable(), records: z.array(draftRecoveryRecordSchema).max(1000) }).strict() +export type DraftRecoveryOwner = z.infer +export type DraftRecoverySnapshot = z.infer +export type DraftRecoveryRecord = z.infer +export interface DraftRecoveryApi { + load: () => Promise> + write: (record: DraftRecoveryRecord) => Promise + remove: (input: z.infer) => Promise +} +export function sameDraftRecoveryOwner(left: DraftRecoveryOwner, right: DraftRecoveryOwner): boolean { + return left.serviceId === right.serviceId && left.profileId === right.profileId && left.dataGeneration === right.dataGeneration && left.deviceId === right.deviceId +} diff --git a/apps/buddy/shared/conversation/submittedInput.ts b/apps/buddy/shared/conversation/submittedInput.ts new file mode 100644 index 000000000..a11ee61c2 --- /dev/null +++ b/apps/buddy/shared/conversation/submittedInput.ts @@ -0,0 +1,30 @@ +import type { ZodType } from 'zod' +import type { buddyUserContentV1Schema } from './buddyUserContent' +import { z } from 'zod' +import { buddyInputDeliverySchema } from './chatApi' +import { buddyComposerDraftExecutionConfigSchema, buddyComposerDraftModelSelectionSchema, buddyComposerDraftScopeSchema, buddyDraftParticipantsSchema } from './composerDraft' + +const id = z.string().regex(/^[A-Z0-9][\w-]{0,127}$/i) + +export function submittedInputSchema(content: T) { + return z.object({ + inputId: id, + scope: buddyComposerDraftScopeSchema, + content, + executionConfig: buddyComposerDraftExecutionConfigSchema, + modelSelection: buddyComposerDraftModelSelectionSchema.nullable(), + participants: buddyDraftParticipantsSchema.nullable().optional(), + legacyResourceDraftId: id.optional(), + }).strict() +} + +export function submitInputIntentSchema(content: T) { + return z.object({ + operation: z.enum(['start', 'edit', 'enqueue', 'command']), + input: submittedInputSchema(content), + buddyId: id.optional(), + delivery: buddyInputDeliverySchema.optional(), + }).strict().refine(value => value.operation === 'start' || (value.buddyId === undefined && (value.operation === 'enqueue' || value.delivery === undefined))) +} + +export type SubmittedInput = z.output>> diff --git a/apps/buddy/shared/conversation/taskMarkApi.ts b/apps/buddy/shared/conversation/taskMarkApi.ts index 1e70854ed..69819fbdc 100644 --- a/apps/buddy/shared/conversation/taskMarkApi.ts +++ b/apps/buddy/shared/conversation/taskMarkApi.ts @@ -22,12 +22,14 @@ export const taskMarkSchema = taskMarkInputSchema.extend({ id: idSchema, createdAt: timestampSchema, updatedAt: timestampSchema, + revision: z.number().int().nonnegative().optional(), taskCount: z.number().int().nonnegative(), }).strict() export const taskMarkStateSchema = z.object({ conversationId: idSchema, markId: idSchema.nullable(), + assignmentRevision: z.number().int().nonnegative().optional(), resultRunId: idSchema.nullable(), readRevision: z.number().int().nonnegative(), unread: z.boolean(), @@ -40,7 +42,7 @@ export const taskMarkReadSchema = z.object({ read: z.boolean(), }).strict() -export const taskMarkClearSchema = taskMarkReadSchema.omit({ read: true }) +export const taskMarkClearSchema = taskMarkReadSchema.omit({ read: true }).extend({ expectedAssignmentRevision: z.number().int().nonnegative().optional() }) export type LocalTaskMark = DeepReadonly> export type LocalTaskMarkState = DeepReadonly> @@ -51,10 +53,10 @@ export type TaskMarkClearInput = z.infer export const taskMarksRpc = { list: { method: 'taskMarks.list', input: validationRequestSchemas.empty, response: z.array(taskMarkSchema) }, create: { method: 'taskMarks.create', input: taskMarkInputSchema, response: taskMarkSchema }, - update: { method: 'taskMarks.update', input: taskMarkInputSchema.extend({ id: idSchema }).strict(), response: taskMarkSchema }, - delete: { method: 'taskMarks.delete', input: z.object({ id: idSchema }).strict(), response: validationResponseSchemas.mutation }, + update: { method: 'taskMarks.update', input: taskMarkInputSchema.extend({ id: idSchema, expectedRevision: z.number().int().nonnegative().optional() }).strict(), response: taskMarkSchema }, + delete: { method: 'taskMarks.delete', input: z.object({ id: idSchema, expectedRevision: z.number().int().nonnegative().optional() }).strict(), response: validationResponseSchemas.mutation }, states: { method: 'taskMarks.states', input: z.object({ conversationIds: z.array(idSchema).max(500) }).strict(), response: z.array(taskMarkStateSchema) }, - assign: { method: 'taskMarks.assign', input: z.object({ conversationId: idSchema, markId: idSchema.nullable() }).strict(), response: taskMarkStateSchema }, + assign: { method: 'taskMarks.assign', input: z.object({ conversationId: idSchema, markId: idSchema.nullable(), expectedAssignmentRevision: z.number().int().nonnegative().optional() }).strict(), response: taskMarkStateSchema }, setRead: { method: 'taskMarks.setRead', input: taskMarkReadSchema, response: taskMarkStateSchema }, clear: { method: 'taskMarks.clear', input: taskMarkClearSchema, response: taskMarkStateSchema }, } as const satisfies Record diff --git a/apps/buddy/shared/diagnostics/credentialVaultFailure.ts b/apps/buddy/shared/diagnostics/credentialVaultFailure.ts index 90df81a8f..362bd554f 100644 --- a/apps/buddy/shared/diagnostics/credentialVaultFailure.ts +++ b/apps/buddy/shared/diagnostics/credentialVaultFailure.ts @@ -23,7 +23,7 @@ export const credentialVaultSystemCodeSchema = z.enum([ export const credentialVaultFailureSchema = z.object({ kind: z.literal('credential_vault'), operation: z.enum(['read', 'write', 'delete', 'list']), - namespace: z.enum(['providers', 'connectors', 'web', 'sync', 'backup', 'extensions', 'images']), + namespace: z.enum(['providers', 'connectors', 'web', 'sync', 'backup', 'extensions', 'images', 'private-network']), phase: z.enum(['availability', 'list_directory', 'read_file', 'decrypt', 'parse', 'validate', 'prepare_directory', 'serialize', 'encrypt', 'write_file', 'delete_file', 'unknown']), reason: z.enum(['operation_failed', 'unavailable', 'invalid_envelope', 'unsupported_version', 'identifier_mismatch', 'invalid_credential_type']), systemCode: credentialVaultSystemCodeSchema.optional(), diff --git a/apps/buddy/shared/executions/executionPresentationApi.ts b/apps/buddy/shared/executions/executionPresentationApi.ts index b6a58a772..01d279322 100644 --- a/apps/buddy/shared/executions/executionPresentationApi.ts +++ b/apps/buddy/shared/executions/executionPresentationApi.ts @@ -91,7 +91,13 @@ export type LocalExecutionEventsPage = DeepReadonly> export type LocalExecutionDetail = DeepReadonly> +export const executionActivityRequestSchema = z.object({ conversationId: idSchema }).strict() +export const executionActivitySchema = z.object({ hasBlockingAnswer: z.boolean() }).strict() +export type ExecutionActivityRequest = z.input +export type LocalExecutionActivity = DeepReadonly> + export const executionPresentationRpc = { + activity: { method: 'executions.activity', input: executionActivityRequestSchema, response: executionActivitySchema }, list: { method: 'executions.list', input: executionScopeSchema, response: z.array(executionPresentationSchema) }, detail: { method: 'executions.detail', input: executionTargetSchema, response: executionDetailSchema }, inputs: { method: 'executions.inputs', input: executionInputsRequestSchema, response: executionInputsPageSchema }, diff --git a/apps/buddy/shared/extensions/__tests__/extensionCompatibility.spec.ts b/apps/buddy/shared/extensions/__tests__/extensionCompatibility.spec.ts deleted file mode 100644 index 7f0117a1c..000000000 --- a/apps/buddy/shared/extensions/__tests__/extensionCompatibility.spec.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { expect, it } from 'vitest' -import { extensionCompatibilityIssue, extensionUpgradeSuggested } from '../extensionCompatibility' -import { extensionMetadataSchema } from '../extensionManifest' - -it.each([ - ['>=0.11.0 <1.0.0', 3, 1, 'app-too-old', true], - ['<0.10.0', 3, 1, 'app-too-new', false], - ['<0.10.0 || >=0.11.0', 3, 1, 'app-version-unsupported', false], - ['*', 5, 1, 'api-unsupported', true], - ['*', 4, 1, null, false], - ['*', 3, 2, 'format-unsupported', true], - ['*', 3, 1, null, false], -] as const)('classifies range %s, API %i and format %i without suggesting an upgrade for other causes', (range, apiVersion, schemaVersion, reason, upgrade) => { - const metadata = extensionMetadataSchema.parse({ schemaVersion, id: 'tests.compatibility', name: 'Compatibility', version: '1.0.0', apiVersion, engines: { lexora: range } }) - const issue = extensionCompatibilityIssue(metadata, '0.10.1') - expect(issue?.reason ?? null).toBe(reason) - expect(issue ? extensionUpgradeSuggested(issue) : false).toBe(upgrade) -}) diff --git a/apps/buddy/shared/extensions/__tests__/extensionRequestLifecycle.spec.ts b/apps/buddy/shared/extensions/__tests__/extensionRequestLifecycle.spec.ts deleted file mode 100644 index ec2d047fb..000000000 --- a/apps/buddy/shared/extensions/__tests__/extensionRequestLifecycle.spec.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { expect, it } from 'vitest' -import { waitsForExtensionIo } from '../extensionRequestLifecycle' - -it.each(['resources.readBytes', 'resources.scanDirectory'])('does not impose a short view RPC deadline on %s', (method) => { - expect(waitsForExtensionIo(method)).toBe(true) -}) - -it('keeps ordinary view RPCs outside the long-I/O policy', () => { - expect(waitsForExtensionIo('storage.get')).toBe(false) - expect(waitsForExtensionIo('unknown.method')).toBe(false) -}) diff --git a/apps/buddy/shared/extensions/extensionAgent.ts b/apps/buddy/shared/extensions/extensionAgent.ts index 7f822b20f..ad254b31a 100644 --- a/apps/buddy/shared/extensions/extensionAgent.ts +++ b/apps/buddy/shared/extensions/extensionAgent.ts @@ -12,6 +12,7 @@ export const extensionAgentRpc = { request: 'extensions.agent.request', changed: 'extensions.agent.changed', resource: 'extensions.agent.resource', + releaseRun: 'extensions.agent.releaseRun', } as const export const extensionAgentInvocationLimits = { concurrent: 32, diff --git a/apps/buddy/shared/extensions/extensionApi.ts b/apps/buddy/shared/extensions/extensionApi.ts index 115d7b175..8f4e2320f 100644 --- a/apps/buddy/shared/extensions/extensionApi.ts +++ b/apps/buddy/shared/extensions/extensionApi.ts @@ -134,6 +134,7 @@ export interface ExtensionReview { sha256: string development: boolean currentVersion: string | null + previousExecutionScope?: 'desktop' | 'service' | null addedPermissions: string[] } export type ExtensionReviewResult = ExtensionReview | ExtensionIncompatibleReview diff --git a/apps/buddy/shared/extensions/extensionCompatibility.ts b/apps/buddy/shared/extensions/extensionCompatibility.ts index b52783910..524d14968 100644 --- a/apps/buddy/shared/extensions/extensionCompatibility.ts +++ b/apps/buddy/shared/extensions/extensionCompatibility.ts @@ -26,6 +26,8 @@ export function extensionCompatibilityIssue(manifest: ExtensionMetadata, version return issue('format-unsupported') if (manifest.apiVersion > EXTENSION_API_VERSION) return issue('api-unsupported') + if (manifest.executionScope === 'service' && manifest.apiVersion < 5) + return issue('manifest-invalid') return satisfies(version, range, options) ? null : issue('app-version-unsupported') } diff --git a/apps/buddy/shared/extensions/extensionInstallation.ts b/apps/buddy/shared/extensions/extensionInstallation.ts index 56c92a603..f39451942 100644 --- a/apps/buddy/shared/extensions/extensionInstallation.ts +++ b/apps/buddy/shared/extensions/extensionInstallation.ts @@ -5,6 +5,7 @@ export const extensionInstallationSchema = z.object({ name: z.string().max(120), extensionId: z.string().max(120).optional(), version: z.string().max(80).optional(), + executionScope: z.enum(['desktop', 'service']).optional(), startedAt: z.string(), status: z.enum(['running', 'review', 'completed', 'failed', 'cancelled']), stage: z.enum(['download', 'validate', 'review', 'compile', 'install', 'completed']), diff --git a/apps/buddy/shared/extensions/extensionManifest.ts b/apps/buddy/shared/extensions/extensionManifest.ts index ca7f7a59e..fea7e07fa 100644 --- a/apps/buddy/shared/extensions/extensionManifest.ts +++ b/apps/buddy/shared/extensions/extensionManifest.ts @@ -18,7 +18,9 @@ import { extensionShellContributionSchema } from './extensionShell.ts' import { extensionSkillContributionSchema, extensionSkillRoots } from './extensionSkills.ts' import { extensionSubagentContributionSchema, subagentConnectorReferences } from './extensionSubagents.ts' -export const EXTENSION_API_VERSION = 4 +export const EXTENSION_API_VERSION = 5 +export const extensionExecutionScopeSchema = z.enum(['desktop', 'service']) +export type ExtensionExecutionScope = z.infer export const EXTENSION_PROTOCOL = 'lexora-extension' export const EXTENSION_ICON_LIMIT = 64 * 1024 export const extensionIconUrlSchema = z.string().max(Math.ceil(EXTENSION_ICON_LIMIT / 3) * 4 + 40).regex(/^data:image\/(?:svg\+xml|png|jpeg|webp);base64,[A-Za-z0-9+/]+={0,2}$/) @@ -122,6 +124,7 @@ export const extensionMetadataSchema = z.object({ tags: z.array(z.string().min(1).max(40)).max(16).default([]), version: extensionVersionSchema, apiVersion: z.number().int().positive(), + executionScope: extensionExecutionScopeSchema.optional(), engines: z.object({ lexora: z.string().max(100).refine(value => validRange(value) !== null) }).strict(), }) export type ExtensionMetadata = z.infer @@ -129,7 +132,8 @@ export type ExtensionMetadata = z.infer export const extensionManifestSchema = extensionMetadataSchema.extend({ schemaVersion: z.literal(1), format: z.enum(['compiled', 'source']).default('compiled'), - apiVersion: z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(EXTENSION_API_VERSION)]), + apiVersion: z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(EXTENSION_API_VERSION)]), + executionScope: extensionExecutionScopeSchema.default('desktop'), entry: extensionPathSchema.optional(), styles: z.object({ uno: z.literal(true), @@ -137,7 +141,7 @@ export const extensionManifestSchema = extensionMetadataSchema.extend({ }).strict().optional(), dataVersion: z.number().int().min(1).max(10000).default(1), dependencies: z.record(extensionIdSchema, z.string().max(100).refine(value => validRange(value) !== null)).default({}), - permissions: extensionPermissionsSchema.prefault({}), + permissions: extensionPermissionsSchema.catchall(z.unknown()).prefault({}), contributes: z.object({ skills: z.array(extensionSkillContributionSchema).max(32).default([]), mcpConnectors: z.array(extensionMcpConnectorContributionSchema).max(16).default([]), @@ -168,6 +172,10 @@ export const extensionManifestSchema = extensionMetadataSchema.extend({ }))], })).refine(contributes => contributes.fileHandlers.length <= 32, 'Too many file handlers'), }).superRefine((manifest, context) => { + if (manifest.apiVersion >= 5 && Object.keys(manifest.permissions).some(key => !Object.hasOwn(extensionPermissionsSchema.shape, key))) + context.addIssue({ code: 'custom', message: 'Unknown API 5 permission' }) + if (manifest.executionScope === 'service' && (manifest.apiVersion < 5 || !manifest.entry || manifest.permissions.schedules)) + context.addIssue({ code: 'custom', message: 'Service execution requires API 5, an entry and no schedules permission' }) const ids = new Set() const settings = manifest.contributes.settings const agent = manifest.contributes.agent @@ -205,8 +213,8 @@ export const extensionManifestSchema = extensionMetadataSchema.extend({ issue('Theme contributions and management require API 3') if (manifest.apiVersion < 3 && (manifest.contributes.conditions.length || agent || settings.modules.length || settings.groups.length || settings.items.length || manifest.permissions.agent || manifest.permissions.models || manifest.permissions.taskMessages || manifest.permissions.tasks !== 'none')) issue('Agent capabilities and settings require API 3') - if (manifest.permissions.legacyTaskCleanup && (manifest.apiVersion < 4 || (!manifest.contributes.views.length && !manifest.contributes.menus.some(menu => menu.target === 'extension.actions')))) - context.addIssue({ code: 'custom', message: 'Legacy task cleanup requires API 4 and a desktop view or plugin action' }) + if (manifest.permissions.legacyTaskCleanup && (manifest.apiVersion < 5 || manifest.executionScope !== 'desktop' || (!manifest.contributes.views.length && !manifest.contributes.menus.some(menu => menu.target === 'extension.actions')))) + context.addIssue({ code: 'custom', message: 'Legacy task cleanup requires API 5 and a desktop view or plugin action' }) if (manifest.apiVersion < 4 && (manifest.permissions.references || manifest.permissions.fileEditing)) issue('References and file editing require API 4') if (manifest.apiVersion < 4 && (manifest.contributes.fileHandlers.length || manifest.contributes.launchers.length || manifest.contributes.views.some(view => view.resource === 'optional-file'))) @@ -333,7 +341,7 @@ export const extensionManifestSchema = extensionMetadataSchema.extend({ } if (manifest.permissions.schedules && !manifest.entry) context.addIssue({ code: 'custom', message: 'Schedules require an extension entry' }) -}) +}).transform(manifest => ({ ...manifest, permissions: extensionPermissionsSchema.parse(manifest.permissions) })) export type ExtensionManifest = z.infer export type ExtensionPermissions = z.infer diff --git a/apps/buddy/shared/extensions/extensionResources.ts b/apps/buddy/shared/extensions/extensionResources.ts index 309ca40fd..aaa8cacb9 100644 --- a/apps/buddy/shared/extensions/extensionResources.ts +++ b/apps/buddy/shared/extensions/extensionResources.ts @@ -74,3 +74,5 @@ const mimeTypes: Readonly> = { export function extensionResourceMimeType(extension: string): string { return mimeTypes[extension.toLowerCase()] ?? 'application/octet-stream' } + +export const extensionResourceWriteRequestSchema = z.object({ id: z.string().uuid(), offset: z.number().int().min(0).optional(), base64: z.string().max(128 * 1024).regex(/^(?:[A-Z0-9+/]{4})*(?:[A-Z0-9+/]{2}==|[A-Z0-9+/]{3}=)?$/i).optional() }).strict() diff --git a/apps/buddy/shared/lifecycle/AsyncOperationScope.ts b/apps/buddy/shared/lifecycle/AsyncOperationScope.ts new file mode 100644 index 000000000..886405bbb --- /dev/null +++ b/apps/buddy/shared/lifecycle/AsyncOperationScope.ts @@ -0,0 +1,52 @@ +export class AsyncOperationScope { + readonly #pending = new Set>() + readonly #unavailable: () => unknown + #accepting = true + + constructor(unavailable: () => unknown) { + this.#unavailable = unavailable + } + + get isAccepting(): boolean { + return this.#accepting + } + + get hasPending(): boolean { + return this.#pending.size > 0 + } + + assertAccepting(): void { + if (!this.#accepting) + throw this.#unavailable() + } + + track(operation: () => T | PromiseLike): Promise { + if (!this.#accepting) + return Promise.reject(this.#unavailable()) + const pending = Promise.withResolvers() + this.#pending.add(pending.promise) + const complete = (settle: () => void) => { + this.#pending.delete(pending.promise) + settle() + } + try { + void Promise.resolve(operation()).then( + value => complete(() => pending.resolve(value)), + error => complete(() => pending.reject(error)), + ) + } + catch (error) { + complete(() => pending.reject(error)) + } + return pending.promise + } + + stopAccepting(): void { + this.#accepting = false + } + + async drain(): Promise { + while (this.#pending.size) + await Promise.allSettled([...this.#pending]) + } +} diff --git a/apps/buddy/shared/lifecycle/__tests__/AsyncOperationScope.spec.ts b/apps/buddy/shared/lifecycle/__tests__/AsyncOperationScope.spec.ts new file mode 100644 index 000000000..2ccbea2f4 --- /dev/null +++ b/apps/buddy/shared/lifecycle/__tests__/AsyncOperationScope.spec.ts @@ -0,0 +1,34 @@ +import { describe, expect, it } from 'vitest' +import { AsyncOperationScope } from '../AsyncOperationScope' + +describe('asyncOperationScope', () => { + it('registers work before synchronous execution and drains accepted work after closing', async () => { + const unavailable = new Error('unavailable') + const scope = new AsyncOperationScope(() => unavailable) + const gate = Promise.withResolvers() + let drained = false + let stopping!: Promise + const result = scope.track(() => { + expect(scope.hasPending).toBe(true) + scope.stopAccepting() + stopping = scope.drain().then(() => { + drained = true + }) + return gate.promise + }) + let accepted = false + await expect(scope.track(() => { + accepted = true + })).rejects.toBe(unavailable) + expect(accepted).toBe(false) + expect(drained).toBe(false) + expect(() => scope.assertAccepting()).toThrow(unavailable) + gate.resolve(42) + await expect(result).resolves.toBe(42) + await stopping + expect(scope.hasPending).toBe(false) + scope.stopAccepting() + await scope.drain() + expect(scope.isAccepting).toBe(false) + }) +}) diff --git a/apps/buddy/shared/lifecycle/exitBehavior.ts b/apps/buddy/shared/lifecycle/exitBehavior.ts new file mode 100644 index 000000000..1f28bfa50 --- /dev/null +++ b/apps/buddy/shared/lifecycle/exitBehavior.ts @@ -0,0 +1,5 @@ +import { z } from 'zod' + +export const desktopExitBehaviorSchema = z.enum(['keep_service', 'stop_service']) +export type DesktopExitBehavior = z.infer +export const DEFAULT_DESKTOP_EXIT_BEHAVIOR: DesktopExitBehavior = 'stop_service' diff --git a/apps/buddy/shared/permissions/approvalApi.ts b/apps/buddy/shared/permissions/approvalApi.ts index a6d5fbafe..acd5cbb64 100644 --- a/apps/buddy/shared/permissions/approvalApi.ts +++ b/apps/buddy/shared/permissions/approvalApi.ts @@ -3,12 +3,14 @@ import type { DeepReadonly } from '../runtime/apiValidation' import { z } from 'zod' import { idSchema, nullableTimestampSchema, optionalLimitSchema, timestampSchema } from '../runtime/apiValidation' import { APPROVAL_GRANT_SCOPES, APPROVAL_REVIEW_KINDS, approvalReviewPayloadMatchesKind, approvalReviewPayloadSchema } from './approvalReviewPayload' +import { approvalReviewSchema } from './conditionalApproval' export const approvalSchema = z.object({ createdAt: timestampSchema, id: idSchema, kind: z.enum(APPROVAL_REVIEW_KINDS), payload: approvalReviewPayloadSchema, + review: approvalReviewSchema.nullable().optional(), resolvedAt: nullableTimestampSchema, runId: idSchema, status: z.enum(['pending', 'approved', 'denied', 'cancelled']), @@ -21,9 +23,11 @@ export const approvalSchema = z.object({ export type LocalApproval = DeepReadonly> +export const approvalDecisionConditionsSchema = z.object({ runId: idSchema, reviewVersion: z.string().regex(/^[a-f0-9]{64}$/) }).strict() + export const approvalsRequestSchemas = { - approvalId: z.object({ approvalId: idSchema }).strict(), - approvalGrant: z.object({ approvalId: idSchema, scope: z.enum(APPROVAL_GRANT_SCOPES) }).strict(), + approvalId: z.object({ approvalId: idSchema, conditions: approvalDecisionConditionsSchema.optional() }).strict(), + approvalGrant: z.object({ approvalId: idSchema, scope: z.enum(APPROVAL_GRANT_SCOPES), conditions: approvalDecisionConditionsSchema.optional() }).strict(), listApprovals: z.object({ conversationId: idSchema.optional(), branchId: idSchema.optional(), diff --git a/apps/buddy/shared/permissions/approvalReviewPayload.ts b/apps/buddy/shared/permissions/approvalReviewPayload.ts index a0d827e63..7b47fdd4e 100644 --- a/apps/buddy/shared/permissions/approvalReviewPayload.ts +++ b/apps/buddy/shared/permissions/approvalReviewPayload.ts @@ -477,7 +477,7 @@ export function redactShellCommand(command: string): string { '$1$2=[redacted]', ) .replace(/\bBearer\s+[^\s"']+/gi, 'Bearer [redacted]') - .replace(/([a-z][a-z0-9+.-]*:\/\/)[^\s/@]+@/gi, '$1[redacted]@') + .replace(/([a-z][a-z0-9+.-]{0,63}:\/\/)[^\s/@]+@/gi, '$1[redacted]@') .slice(0, MAX_COMMAND_REVIEW_LENGTH) } @@ -494,7 +494,7 @@ export function redactSensitiveText(value: string): string { /\b((?:api[-_]?key|secret|token|password|credential)\s*[:=]\s*)([^\s,;]+)/gi, '$1[redacted]', ) - .replace(/([a-z][a-z0-9+.-]*:\/\/)[^\s/@]+@/gi, '$1[redacted]@') + .replace(/([a-z][a-z0-9+.-]{0,63}:\/\/)[^\s/@]+@/gi, '$1[redacted]@') } function isSensitiveName(name: string): boolean { diff --git a/apps/buddy/shared/permissions/conditionalApproval.ts b/apps/buddy/shared/permissions/conditionalApproval.ts new file mode 100644 index 000000000..ff473ca62 --- /dev/null +++ b/apps/buddy/shared/permissions/conditionalApproval.ts @@ -0,0 +1,14 @@ +import { z } from 'zod' + +export const approvalReviewSchema = z.object({ + reviewVersion: z.string().regex(/^[a-f0-9]{64}$/), + contentVersion: z.literal(1), + operationDigest: z.string().regex(/^[a-f0-9]{64}$/), + body: z.json(), + complete: z.boolean(), + expiresAt: z.iso.datetime(), + lifetimeId: z.string().min(1), + allowedDecisions: z.array(z.enum(['approved', 'denied', 'approved_persistently', 'approved_for_operation', 'approved_for_source', 'approved_for_turn'])), + persistentEffect: z.boolean(), +}).strict() +export type ApprovalReview = z.infer diff --git a/apps/buddy/shared/privateNetwork/limits.ts b/apps/buddy/shared/privateNetwork/limits.ts new file mode 100644 index 000000000..bcd62e46f --- /dev/null +++ b/apps/buddy/shared/privateNetwork/limits.ts @@ -0,0 +1,24 @@ +export const PRIVATE_NETWORK_LIMITS = Object.freeze({ + ticketLifetimeMs: 120_000, + recoveryLifetimeMs: 300_000, + credentialIdleMs: 30 * 24 * 60 * 60 * 1000, + credentialAbsoluteMs: 90 * 24 * 60 * 60 * 1000, + siblingCredentials: 5, + pendingAttempts: 64, + devices: 64, + connections: 32, + connectionsPerDevice: 4, + httpRequests: 32, + requestBytes: 16_384, + requestsPerMinute: 120, + pairingRequestsPerMinute: 20, + requestTimeoutMs: 15_000, + tlsCertificateDays: 90, + uploadBytes: 268_435_456, + uploadReservedBytes: 1_073_741_824, + uploadsPerDevice: 4, + uploadChunkBytes: 1_048_576, + uploadLifetimeMs: 24 * 60 * 60 * 1000, + resourceStreams: 8, + resourceLifetimeMs: 30 * 60 * 1000, +}) diff --git a/apps/buddy/shared/privateNetwork/management.ts b/apps/buddy/shared/privateNetwork/management.ts new file mode 100644 index 000000000..25ab4e3b0 --- /dev/null +++ b/apps/buddy/shared/privateNetwork/management.ts @@ -0,0 +1,23 @@ +import { z } from 'zod' +import { productIdSchema } from '../productProtocol/scalars' +import { deviceStateSchema, pairingQrSchema, privateNetworkPinSchema } from './pairing' + +export const privateNetworkStatusSchema = z.object({ enabled: z.boolean(), state: z.enum(['disabled', 'listening', 'unavailable']), bindAddress: z.string().max(256).nullable(), port: z.number().int().min(0).max(65535).nullable(), endpoint: z.string().nullable(), spkiPin: privateNetworkPinSchema.nullable(), errorCode: z.enum(['CREDENTIAL_STORE_UNAVAILABLE', 'TLS_IDENTITY_UNAVAILABLE', 'ADDRESS_INVALID', 'ADDRESS_IN_USE', 'ADDRESS_UNAVAILABLE', 'LISTEN_FAILED']).nullable() }).strict() +const empty = z.object({}).strict() +const result = z.object({ ok: z.literal(true) }).strict() +export const privateNetworkManagement = { + status: { method: 'privateNetwork.status', input: empty, response: privateNetworkStatusSchema }, + configure: { method: 'privateNetwork.configure', input: z.object({ enabled: z.boolean(), bindAddress: z.string().max(256).optional(), port: z.number().int().min(1).max(65535).optional() }).strict(), response: privateNetworkStatusSchema }, + issuePairing: { method: 'privateNetwork.issuePairing', input: empty, response: pairingQrSchema }, + cancelPairing: { method: 'privateNetwork.cancelPairing', input: z.object({ ticketId: productIdSchema.optional() }).strict(), response: result }, + devices: { method: 'privateNetwork.devices', input: empty, response: z.array(deviceStateSchema).max(256) }, + revoke: { method: 'privateNetwork.revoke', input: z.object({ deviceId: productIdSchema }).strict(), response: result }, + resetIdentity: { method: 'privateNetwork.resetIdentity', input: empty, response: privateNetworkStatusSchema }, +} as const + +export type PrivateNetworkManagementApi = { + [K in keyof typeof privateNetworkManagement]: (input: z.input<(typeof privateNetworkManagement)[K]['input']>) => Promise> +} +export type PrivateNetworkStatus = z.infer +export type PairedDevice = z.infer +export type PairingCode = z.infer diff --git a/apps/buddy/shared/privateNetwork/pairing.ts b/apps/buddy/shared/privateNetwork/pairing.ts new file mode 100644 index 000000000..62cd2ee0e --- /dev/null +++ b/apps/buddy/shared/privateNetwork/pairing.ts @@ -0,0 +1,40 @@ +import { z } from 'zod' +import { productIdSchema as id, productRevisionSchema as revision } from '../productProtocol/scalars' + +export const privateNetworkSecretSchema = z.string().regex(/^[\w-]{43}$/) +export const privateNetworkPinSchema = z.string().regex(/^sha256\/[A-Za-z\d+/]{43}=$/) +export const privateNetworkEndpointSchema = z.string().max(2048).refine((value) => { + try { + const url = new URL(value) + return url.protocol === 'https:' && !url.username && !url.password && !url.search && !url.hash && url.pathname === '/' && url.origin === value + } + catch { return false } +}) +export const pairingContextSchema = z.object({ serviceId: id, profileId: id, issuerInstanceId: id, ticketId: id, ticketSecret: privateNetworkSecretSchema }).strict() +export const pairingQrSchema = pairingContextSchema.extend({ format: z.literal('lexora-pairing'), version: z.literal(1), endpoint: privateNetworkEndpointSchema, spkiPin: privateNetworkPinSchema, expiresAt: z.iso.datetime() }).strict() +export const pairingPrepareSchema = pairingContextSchema.extend({ deviceName: z.string().trim().min(1).max(80) }).strict() +export const pairingPrepareResultSchema = z.object({ attemptId: id, recoverySecret: privateNetworkSecretSchema, expiresAt: z.iso.datetime() }).strict() +export const pairingRecoverySchema = z.object({ serviceId: id, profileId: id, attemptId: id, recoverySecret: privateNetworkSecretSchema }).strict() +export const pairingRedeemSchema = pairingContextSchema.extend({ attemptId: id, recoverySecret: privateNetworkSecretSchema }).strict() +export const deviceCredentialSchema = z.object({ deviceId: id, credentialId: id, credentialSecret: privateNetworkSecretSchema, grantGeneration: revision, expiresAt: z.iso.datetime(), absoluteExpiresAt: z.iso.datetime() }).strict() +export const deviceStateSchema = z.object({ deviceId: id, name: z.string().max(80), state: z.enum(['pending', 'active', 'revoked', 'expired']), grantGeneration: revision, createdAt: z.iso.datetime(), confirmedAt: z.iso.datetime().nullable(), lastSeenAt: z.iso.datetime().nullable(), expiresAt: z.iso.datetime(), absoluteExpiresAt: z.iso.datetime() }).strict() +export const pairingConfirmSchema = z.object({}).strict() +export const PRIVATE_NETWORK_ROUTES = { + prepare: { path: '/v1/pairing/prepare', input: pairingPrepareSchema, output: pairingPrepareResultSchema }, + redeem: { path: '/v1/pairing/redeem', input: pairingRedeemSchema, output: deviceCredentialSchema }, + recover: { path: '/v1/pairing/recover', input: pairingRecoverySchema, output: deviceCredentialSchema }, + confirm: { path: '/v1/pairing/confirm', input: pairingConfirmSchema, output: deviceStateSchema }, +} as const +export const privateNetworkErrorSchema = z.object({ error: z.enum(['INVALID_REQUEST', 'NOT_AUTHORIZED', 'PAIRING_UNAVAILABLE', 'PAIRING_USED', 'RECOVERY_REQUIRED', 'RECOVERY_CLOSED', 'LIMIT_EXCEEDED', 'STORAGE_UNAVAILABLE', 'TLS_IDENTITY_UNAVAILABLE', 'RESOURCE_UNAVAILABLE', 'RANGE_INVALID', 'VERSION_CHANGED', 'UPLOAD_CONFLICT']) }).strict() +export type PrivateNetworkErrorCode = z.infer['error'] +export class PrivateNetworkError extends Error { + readonly code: PrivateNetworkErrorCode + + constructor(code: PrivateNetworkErrorCode) { + super(code) + this.code = code + } +} +export function deviceBearer(credential: z.infer): string { + return `${credential.credentialId}.${credential.credentialSecret}` +} diff --git a/apps/buddy/shared/privateNetwork/resources.ts b/apps/buddy/shared/privateNetwork/resources.ts new file mode 100644 index 000000000..4121a97d4 --- /dev/null +++ b/apps/buddy/shared/privateNetwork/resources.ts @@ -0,0 +1,14 @@ +import { z } from 'zod' +import { productIdSchema as id, productRevisionSchema } from '../productProtocol/scalars' +import { uploadDigestSchema } from './uploads' + +export const resourcePrepareSchema = z.object({ resourceId: id, expectedVersion: z.string().min(1).max(256) }).strict() +export const resourceDownloadSchema = z.object({ + downloadId: id, + resourceId: id, + contentVersion: uploadDigestSchema, + sizeBytes: productRevisionSchema, + mimeType: z.string().max(256), + name: z.string().max(512), + expiresAt: z.iso.datetime(), +}).strict() diff --git a/apps/buddy/shared/privateNetwork/uploads.ts b/apps/buddy/shared/privateNetwork/uploads.ts new file mode 100644 index 000000000..49215d879 --- /dev/null +++ b/apps/buddy/shared/privateNetwork/uploads.ts @@ -0,0 +1,53 @@ +import { z } from 'zod' +import { BUDDY_DOCUMENT_MIME_TYPES, BUDDY_MEDIA_FILE_BYTES_LIMIT } from '../conversation/attachmentFormats' +import { BUDDY_ATTACHMENT_TOTAL_BYTES_LIMIT, BUDDY_TEXT_ATTACHMENT_BYTES_LIMIT, BUDDY_TEXT_ATTACHMENT_MIME_TYPES, isTextAttachmentMimeType } from '../conversation/attachmentPolicy' +import { productIdSchema as id, productRevisionSchema as revision } from '../productProtocol/scalars' + +export const REMOTE_UPLOAD_POLICY = { + maxBytes: BUDDY_ATTACHMENT_TOTAL_BYTES_LIMIT, + textBytes: BUDDY_TEXT_ATTACHMENT_BYTES_LIMIT, + mediaBytes: BUDDY_MEDIA_FILE_BYTES_LIMIT, + textMimeTypes: BUDDY_TEXT_ATTACHMENT_MIME_TYPES, + imageMimeTypes: ['image/png', 'image/jpeg', 'image/gif', 'image/webp'], + documentMimeTypes: BUDDY_DOCUMENT_MIME_TYPES, +} as const + +export const uploadDigestSchema = z.string().regex(/^[a-f0-9]{64}$/) +export const uploadMetadataSchema = z.object({ + name: z.string().trim().min(1).max(255).refine(value => !/[\\/]/.test(value) && [...value].every(char => char.charCodeAt(0) >= 32)), + mimeType: z.string().min(1).max(255), + sizeBytes: z.number().int().nonnegative().max(BUDDY_ATTACHMENT_TOTAL_BYTES_LIMIT), + sha256: uploadDigestSchema, +}).strict().refine(({ mimeType, sizeBytes }) => { + if (isTextAttachmentMimeType(mimeType)) + return sizeBytes <= BUDDY_TEXT_ATTACHMENT_BYTES_LIMIT + if ((REMOTE_UPLOAD_POLICY.imageMimeTypes as readonly string[]).includes(mimeType)) + return sizeBytes <= BUDDY_MEDIA_FILE_BYTES_LIMIT + if (mimeType === 'application/pdf') + return true + return (BUDDY_DOCUMENT_MIME_TYPES as readonly string[]).includes(mimeType) && sizeBytes <= BUDDY_MEDIA_FILE_BYTES_LIMIT +}) +export const uploadCommandIntents = { + 'uploads.stage': z.object({ inputId: id, file: uploadMetadataSchema }).strict(), + 'uploads.create': z.object({ draftId: id, expectedRevision: revision, file: uploadMetadataSchema }).strict(), + 'uploads.complete': z.object({ uploadId: id }).strict(), + 'uploads.cancel': z.object({ uploadId: id }).strict(), + 'uploads.adopt': z.object({ uploadId: id, draftId: id, expectedRevision: revision }).strict(), +} as const +export type UploadCommandMethod = keyof typeof uploadCommandIntents +export const uploadStateSchema = z.object({ + uploadId: id, + draftId: id, + expectedRevision: revision, + inputId: id.optional(), + state: z.enum(['receiving', 'ready', 'adopted', 'cancelled', 'expired', 'failed']), + confirmedBytes: z.number().int().nonnegative(), + sizeBytes: z.number().int().nonnegative(), + sha256: uploadDigestSchema, + expiresAt: z.iso.datetime(), + resourceId: id.nullable(), + contentVersion: uploadDigestSchema.nullable(), + attachmentId: id.nullable(), +}).strict() +export const uploadGetSchema = z.object({ uploadId: id }).strict() +export const uploadChunkResultSchema = z.object({ uploadId: id, confirmedBytes: z.number().int().nonnegative() }).strict() diff --git a/apps/buddy/shared/productProtocol/__tests__/protocol.spec.ts b/apps/buddy/shared/productProtocol/__tests__/protocol.spec.ts new file mode 100644 index 000000000..4cd400c20 --- /dev/null +++ b/apps/buddy/shared/productProtocol/__tests__/protocol.spec.ts @@ -0,0 +1,52 @@ +import { describe, expect, it } from 'vitest' +import { assertProductContentActions, productContentSchema, productEditableContentSchema, projectProductContent, restoreProductContent } from '../content' +import { productInitializeSchema } from '../identity' +import { getProductMethod } from '../methods' +import { decodeProductCursor, decodeSafeProductRevision, encodeProductCursor, encodeProductRevision, productRevisionSchema } from '../scalars' +import vectors from '../vectors.json' + +describe('portable product contract', () => { + it('consumes the cross-language request and scalar vectors', () => { + for (const vector of vectors.requests) { + const schema = vector.method === 'initialize' ? productInitializeSchema : getProductMethod(vector.method)?.input + expect(schema?.safeParse(vector.params).success ?? false, vector.name).toBe(vector.valid) + } + for (const vector of vectors.responses) + expect(getProductMethod(vector.method)?.output.safeParse(vector.result).success ?? false, vector.name).toBe(vector.valid) + for (const vector of vectors.contents) + expect(productContentSchema.safeParse(vector.value).success, vector.name).toBe(vector.valid) + for (const vector of vectors.revisions) + expect(productRevisionSchema.safeParse(vector.value).success, vector.name).toBe(vector.valid) + }) + + it('round trips signed SQLite upper bound without passing through a number', () => { + expect(encodeProductRevision(9_223_372_036_854_775_807n)).toBe('9223372036854775807') + expect(() => encodeProductRevision(9_007_199_254_740_992)).toThrow() + expect(() => decodeSafeProductRevision('9007199254740992')).toThrow() + const expected = { scope: 'messages', branchId: 'branch-1', revision: '9007199254740992' } + const cursor = encodeProductCursor({ version: 1, ...expected, upperBound: '9223372036854775807', position: '9007199254740993' }) + expect(decodeProductCursor(cursor, expected).position).toBe('9007199254740993') + expect(() => decodeProductCursor(cursor, { ...expected, revision: '9007199254740993' })).toThrow('CURSOR_EXPIRED') + expect(() => decodeProductCursor(cursor, { ...expected, branchId: 'branch-2' })).toThrow('CURSOR_EXPIRED') + }) + + it('preserves Chinese, surrogate pairs, UTF-16 offsets and controlled original resource semantics', () => { + const source = { + version: 1 as const, + body: [{ type: 'paragraph' as const, content: [{ type: 'text' as const, text: '你好😀世界' }, { type: 'quote_ref' as const, quoteKind: 'resource' as const, quoteId: 'q1' }] }], + panelResourceIds: [], + resourceQuotes: [{ id: 'q1', text: '世界', textOffset: 4, source: { kind: 'artifact' as const, title: '报告', artifactId: 'a1', conversationId: 'c1', runId: 'r1', path: '/private/example/report.md', updatedAt: '2026-01-01T00:00:00.000Z', format: 'markdown' as const } }], + } + const portable = projectProductContent(source, () => ({ resourceId: 'material-1', title: '报告' })) + expect(JSON.stringify(portable)).not.toContain('/private') + expect(portable.resourceQuotes?.[0]?.textOffset).toBe(4) + expect(restoreProductContent(portable, () => source.resourceQuotes[0]!)).toEqual(source) + expect(() => restoreProductContent(portable, () => ({ ...source.resourceQuotes[0]!, text: 'changed' }))).toThrow('SOURCE_CHANGED') + expect(productEditableContentSchema.safeParse({ ...portable, version: 2 }).success).toBe(false) + }) + + it('never permits embedded action directives based on client kind', () => { + const content = productEditableContentSchema.parse({ version: 1, body: [{ type: 'paragraph', content: [{ type: 'prompt_directive', directive: 'slash_command', commandMode: 'action', commandId: 'plugins.install', value: '/install' }] }], panelResourceIds: [] }) + expect(() => assertProductContentActions(content, new Set(['compact']))).toThrow('ACTION_NOT_ALLOWED') + }) +}) diff --git a/apps/buddy/shared/productProtocol/avatars.ts b/apps/buddy/shared/productProtocol/avatars.ts new file mode 100644 index 000000000..f7167cff9 --- /dev/null +++ b/apps/buddy/shared/productProtocol/avatars.ts @@ -0,0 +1,10 @@ +import { z } from 'zod' +import { BUDDY_AVATAR_MAX_BYTES, BUDDY_AVATAR_PRESET_IDS } from '../buddies/buddyApi' +import { productIdSchema } from './scalars' + +export const productAvatarSchema = z.discriminatedUnion('kind', [ + z.object({ kind: z.literal('default') }).strict(), + z.object({ kind: z.literal('preset'), preset: z.enum(BUDDY_AVATAR_PRESET_IDS) }).strict(), + z.object({ kind: z.literal('image'), sha256: z.string().regex(/^[a-f0-9]{64}$/), mimeType: z.enum(['image/png', 'image/jpeg', 'image/webp']), sizeBytes: z.number().int().min(1).max(BUDDY_AVATAR_MAX_BYTES) }).strict(), +]) +export const productBuddyAvatarSchema = z.object({ buddyId: productIdSchema, avatar: productAvatarSchema }).strict() diff --git a/apps/buddy/shared/productProtocol/content-behavior-vectors.json b/apps/buddy/shared/productProtocol/content-behavior-vectors.json new file mode 100644 index 000000000..251d1d56b --- /dev/null +++ b/apps/buddy/shared/productProtocol/content-behavior-vectors.json @@ -0,0 +1,187 @@ +{ + "version": 1, + "textCases": [ + { + "name": "text-0", + "text": "", + "utf16Length": 0, + "androidParagraphs": 1, + "desktopParagraphs": 1 + }, + { + "name": "text-1", + "text": "first\n\nlast\n", + "utf16Length": 12, + "androidParagraphs": 1, + "desktopParagraphs": 4 + }, + { + "name": "text-2", + "text": "你好😀é\n第二行", + "utf16Length": 10, + "androidParagraphs": 1, + "desktopParagraphs": 2 + }, + { + "name": "text-3", + "text": "a\r\nb", + "utf16Length": 4, + "androidParagraphs": 1, + "desktopParagraphs": 2 + } + ], + "contents": [ + { + "name": "hard-break-utf16", + "value": { + "version": 1, + "body": [ + { + "type": "paragraph", + "content": [ + { + "type": "text", + "text": "A😀" + }, + { + "type": "hard_break" + }, + { + "type": "text", + "text": "B" + } + ] + } + ], + "panelResourceIds": [] + }, + "valid": true, + "editable": true, + "desktopText": "A😀\nB", + "preview": "A😀\nB", + "message": "A😀\nB" + }, + { + "name": "legacy-multiline-single-node", + "value": { + "version": 1, + "body": [ + { + "type": "paragraph", + "content": [ + { + "type": "text", + "text": "first\n\nlast\n" + } + ] + } + ], + "panelResourceIds": [] + }, + "valid": true, + "editable": true, + "desktopText": "first\n\nlast\n", + "preview": "first\n\nlast\n", + "message": "first\n\nlast\n" + }, + { + "name": "reference-rendering-is-host-specific", + "value": { + "version": 1, + "body": [ + { + "type": "paragraph", + "content": [ + { + "type": "text", + "text": "你好😀" + }, + { + "type": "hard_break" + }, + { + "type": "actor_ref", + "actorId": "buddy", + "name": "伙伴" + }, + { + "type": "resource_ref", + "resourceId": "bound" + }, + { + "type": "session_ref", + "sessionId": "task" + }, + { + "type": "quote_ref", + "quoteKind": "message", + "quoteId": "quote" + }, + { + "type": "prompt_directive", + "directive": "skill", + "value": "paper" + } + ] + } + ], + "panelResourceIds": [], + "sessionReferences": [ + { + "id": "task", + "title": "原任务" + } + ], + "quotes": [ + { + "id": "quote", + "text": "引用😀\n第二行", + "textOffset": 2, + "source": { + "conversationId": "task", + "branchId": "branch", + "messageId": "source", + "role": "assistant", + "runId": null + } + } + ] + }, + "valid": true, + "editable": false, + "desktopText": "你好😀\n@伙伴@file$paper", + "preview": "你好😀\n@伙伴[附件][相关任务:原任务][引用] 引用😀\n第二行paper", + "message": "你好😀\n@伙伴[附件.txt][相关任务:原任务]\n> 引用😀\n> 第二行\npaper", + "resources": [ + { + "bindingId": "bound", + "name": "附件.txt" + } + ] + }, + { + "name": "unknown-node-is-readonly-and-preserved", + "value": { + "version": 1, + "body": [ + { + "type": "paragraph", + "content": [ + { + "type": "future_node", + "payload": { + "preserve": "😀" + } + } + ] + } + ], + "panelResourceIds": [] + }, + "valid": false, + "editable": false, + "preview": "[请在桌面端查看此内容]", + "message": "[未识别的内容]" + } + ] +} diff --git a/apps/buddy/shared/productProtocol/content.ts b/apps/buddy/shared/productProtocol/content.ts new file mode 100644 index 000000000..8fd787de5 --- /dev/null +++ b/apps/buddy/shared/productProtocol/content.ts @@ -0,0 +1,63 @@ +import type { BuddyResourceQuote, BuddyUserContentV1 } from '../conversation/buddyUserContent' +import { z } from 'zod' +import { buddyUserContentV1Schema } from '../conversation/buddyUserContent' +import { ProductProtocolError } from './errors' +import { productIdSchema } from './scalars' + +const resourceQuoteSchema = z.object({ + id: productIdSchema, + resourceId: productIdSchema, + text: z.string().min(1).max(262_144), + textOffset: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER).optional(), + title: z.string().max(512), +}).strict() +const contentFields = buddyUserContentV1Schema.unwrap().shape +export const productEditableContentSchema = z.object({ + version: contentFields.version, + body: contentFields.body, + panelResourceIds: contentFields.panelResourceIds, + quotes: contentFields.quotes, + resourceQuotes: z.array(resourceQuoteSchema).max(16).optional(), + sessionReferences: contentFields.sessionReferences, +}).strict().refine(content => content.body.every(paragraph => paragraph.content.every(node => + (node.type !== 'session_ref' || content.sessionReferences?.some(reference => reference.id === node.sessionId)) + && (node.type !== 'quote_ref' || (node.quoteKind === 'message' ? content.quotes : content.resourceQuotes)?.some(quote => quote.id === node.quoteId)), +)), 'Missing material metadata').refine((content) => { + const quotes = [...content.quotes ?? [], ...content.resourceQuotes ?? []] + return quotes.length <= 16 && new Set(quotes.map(quote => quote.id)).size === quotes.length && quotes.reduce((sum, quote) => sum + quote.text.length, 0) <= 1_048_576 +}) +export type ProductEditableContent = z.infer +export const productContentSchema = z.discriminatedUnion('kind', [ + z.object({ kind: z.literal('editable'), content: productEditableContentSchema }).strict(), + z.object({ kind: z.literal('unsupported'), contentReference: productIdSchema, version: z.number().int().nonnegative(), summary: z.string().max(4096), readOnly: z.literal(true) }).strict(), +]) + +export function assertProductContentActions(content: ProductEditableContent, allowedActions: ReadonlySet): void { + for (const paragraph of content.body) { + for (const node of paragraph.content) { + if (node.type === 'prompt_directive' && node.directive === 'slash_command' && node.commandMode === 'action' && (!node.commandId || !allowedActions.has(node.commandId))) + throw new ProductProtocolError('ACTION_NOT_ALLOWED') + } + } +} + +export function projectProductContent(content: BuddyUserContentV1, publishQuote: (quote: BuddyResourceQuote) => { resourceId: string, title: string }): ProductEditableContent { + return productEditableContentSchema.parse({ + ...content, + ...(content.resourceQuotes ? { resourceQuotes: content.resourceQuotes.map(quote => ({ id: quote.id, text: quote.text, ...('textOffset' in quote && quote.textOffset !== undefined ? { textOffset: quote.textOffset } : {}), ...publishQuote(quote) })) } : {}), + }) +} + +export function restoreProductContent(content: ProductEditableContent, resolveQuote: (resourceId: string) => BuddyResourceQuote): BuddyUserContentV1 { + return buddyUserContentV1Schema.parse({ + ...content, + ...(content.resourceQuotes + ? { resourceQuotes: content.resourceQuotes.map((quote) => { + const source = resolveQuote(quote.resourceId) + if (source.id !== quote.id || source.text !== quote.text || ('textOffset' in source ? source.textOffset : undefined) !== quote.textOffset) + throw new ProductProtocolError('SOURCE_CHANGED') + return source + }) } + : {}), + }) +} diff --git a/apps/buddy/shared/productProtocol/errors.ts b/apps/buddy/shared/productProtocol/errors.ts new file mode 100644 index 000000000..4b91a3240 --- /dev/null +++ b/apps/buddy/shared/productProtocol/errors.ts @@ -0,0 +1,74 @@ +import { z } from 'zod' + +export const PRODUCT_ERROR_CODES = [ + 'INVALID_REQUEST', + 'INVALID_PARAMS', + 'METHOD_NOT_AVAILABLE', + 'NOT_INITIALIZED', + 'ALREADY_INITIALIZED', + 'PROTOCOL_INCOMPATIBLE', + 'NOT_AUTHORIZED', + 'SERVICE_NOT_READY', + 'SERVICE_STOPPING', + 'DATA_GENERATION_CHANGED', + 'SERVICE_INSTANCE_CHANGED', + 'COMMAND_ACTOR_UNAVAILABLE', + 'COMMAND_ID_REUSED', + 'COMMAND_FINGERPRINT_UNSUPPORTED', + 'COMMAND_RESULT_UNKNOWN', + 'FRAME_TOO_LARGE', + 'PAGE_TOO_LARGE', + 'CURSOR_INVALID', + 'CURSOR_EXPIRED', + 'CONTENT_UNSUPPORTED', + 'ACTION_NOT_ALLOWED', + 'DRAFT_CONFLICT', + 'SOURCE_CHANGED', + 'QUEUE_CHANGED', + 'REVIEW_CHANGED', + 'APPROVAL_ALREADY_RESOLVED', + 'QUESTION_CLOSED', + 'QUESTION_STALE', + 'QUESTION_NOT_FOUND', + 'QUESTION_ANSWER_INVALID', + 'RESOURCE_UNAVAILABLE', + 'OBJECT_DELETED', + 'DESKTOP_UNAVAILABLE', + 'INPUT_DELIVERY_TARGET_UNAVAILABLE', + 'INPUT_DELIVERY_INCOMPATIBLE', + 'SYNC_INVALID', + 'RESYNC_REQUIRED', + 'INTERNAL_ERROR', +] as const +export const productErrorCodeSchema = z.enum(PRODUCT_ERROR_CODES) +export type ProductErrorCode = z.infer +export const productErrorSchema = z.object({ + code: productErrorCodeSchema, + message: z.string().max(128), + acceptance: z.enum(['not_accepted', 'unknown']), + recovery: z.enum(['none', 'correct_request', 'initialize', 'refresh', 'query_command', 'reauthenticate']), +}).strict() +export type ProductError = z.infer + +export class ProductProtocolError extends Error { + readonly code: ProductErrorCode + readonly acceptance: ProductError['acceptance'] + + constructor(code: ProductErrorCode, acceptance: ProductError['acceptance'] = 'not_accepted') { + super(code) + this.code = code + this.acceptance = acceptance + } +} + +export function projectProductError(error: unknown, commandDispatched = false, outcomeMayBeCommitted = false): ProductError { + const code = productErrorCodeSchema.safeParse(error instanceof ProductProtocolError ? error.code : error && typeof error === 'object' && 'code' in error ? error.code : null) + const safeCode = code.success ? code.data : 'INTERNAL_ERROR' + const unknown = outcomeMayBeCommitted || safeCode === 'COMMAND_RESULT_UNKNOWN' || (error instanceof ProductProtocolError ? error.acceptance === 'unknown' : commandDispatched) + return { + code: safeCode, + message: safeCode, + acceptance: unknown ? 'unknown' : 'not_accepted', + recovery: unknown ? 'query_command' : safeCode === 'NOT_INITIALIZED' ? 'initialize' : safeCode === 'NOT_AUTHORIZED' || safeCode === 'DATA_GENERATION_CHANGED' ? 'reauthenticate' : safeCode === 'INVALID_PARAMS' || safeCode === 'INVALID_REQUEST' ? 'correct_request' : 'refresh', + } +} diff --git a/apps/buddy/shared/productProtocol/identity.ts b/apps/buddy/shared/productProtocol/identity.ts new file mode 100644 index 000000000..57e9a3735 --- /dev/null +++ b/apps/buddy/shared/productProtocol/identity.ts @@ -0,0 +1,42 @@ +import { z } from 'zod' +import { PRODUCT_LIMITS } from './limits' +import { productIdSchema, productRevisionSchema } from './scalars' + +export const PRODUCT_PROTOCOL_VERSION = Object.freeze({ major: 1, minor: 0 }) +export const productInitializeSchema = z.object({ + protocol: z.object({ major: z.number().int().nonnegative(), minMinor: z.number().int().nonnegative(), maxMinor: z.number().int().nonnegative() }).strict().refine(value => value.minMinor <= value.maxMinor), + client: z.object({ kind: z.enum(['desktop', 'mobile']), version: z.string().min(1).max(80) }).strict(), + supportedContentKinds: z.array(z.string().min(1).max(80)).max(64), + supportedReviewKinds: z.array(z.string().min(1).max(80)).max(64), +}).strict() +export const productIdentitySchema = z.object({ + serviceId: productIdSchema, + profileId: productIdSchema, + dataGeneration: productIdSchema, + serviceInstanceId: productIdSchema, + connectionId: productIdSchema, + clientSessionId: productIdSchema, + deviceId: productIdSchema, +}).strict() +export type ProductIdentity = z.infer +export interface TrustedProductContext extends ProductIdentity { + readonly ownerPrincipalId: string + readonly credentialId: string + readonly grantGeneration: string + readonly role: 'owner' | 'reader' +} +export const productPhaseSchema = z.enum(['starting', 'recovering', 'ready', 'stopping']) +export const productInitializeResultSchema = productIdentitySchema.extend({ + protocol: z.object({ major: z.literal(1), minor: z.literal(0) }).strict(), + serverTime: z.iso.datetime(), + phase: productPhaseSchema, + capabilities: z.array(z.string()), + limits: z.object(Object.fromEntries(Object.entries(PRODUCT_LIMITS).map(([key, value]) => [key, z.literal(value)])) as { [K in keyof typeof PRODUCT_LIMITS]: z.ZodLiteral<(typeof PRODUCT_LIMITS)[K]> }).strict(), +}).strict() +export const productGrantGenerationSchema = productRevisionSchema + +export function classifyProductIdentityChange(previous: ProductIdentity, current: ProductIdentity): 'connection' | 'service' | 'trust' { + if (previous.profileId !== current.profileId || previous.dataGeneration !== current.dataGeneration || previous.serviceId !== current.serviceId || previous.deviceId !== current.deviceId) + return 'trust' + return previous.serviceInstanceId !== current.serviceInstanceId ? 'service' : 'connection' +} diff --git a/apps/buddy/shared/productProtocol/limits.ts b/apps/buddy/shared/productProtocol/limits.ts new file mode 100644 index 000000000..cae08d7f3 --- /dev/null +++ b/apps/buddy/shared/productProtocol/limits.ts @@ -0,0 +1,24 @@ +export const PRODUCT_LIMITS = Object.freeze({ + snapshotLifetimeMs: 30_000, + dynamicTopics: 128, + subscriptionsPerConnection: 64, + inFlightRequests: 64, + frameBytes: 1_048_576, + pageItems: 100, + pageBytes: 524_288, + topicItems: 1024, + topicBytes: 2_097_152, + subscriptionItems: 256, + subscriptionBytes: 1_048_576, + connectionBytes: 4_194_304, +}) +const encoder = new TextEncoder() +export function productByteLength(text: string): number { + return encoder.encode(text).byteLength +} +export function encodeProductFrame(value: unknown, limit: number = PRODUCT_LIMITS.frameBytes): string { + const text = JSON.stringify(value) + if (typeof text !== 'string' || productByteLength(text) > limit) + throw new RangeError('FRAME_TOO_LARGE') + return text +} diff --git a/apps/buddy/shared/productProtocol/methods.ts b/apps/buddy/shared/productProtocol/methods.ts new file mode 100644 index 000000000..5470d1ac8 --- /dev/null +++ b/apps/buddy/shared/productProtocol/methods.ts @@ -0,0 +1,87 @@ +import type { ZodType } from 'zod' +import { z } from 'zod' +import { buddyInputDeliverySchema } from '../conversation/chatApi' +import { buddyComposerDraftOpenSchema, buddyComposerDraftSaveSchema, buddyComposerDraftScopeSchema } from '../conversation/composerDraft' +import { conversationRequestSchemas } from '../conversation/conversationApi' +import { conversationArchivePolicySchema, conversationArchiveTargetSchema } from '../conversation/conversationArchiveApi' +import { submitInputIntentSchema } from '../conversation/submittedInput' +import { taskMarkClearSchema, taskMarkInputSchema, taskMarkReadSchema } from '../conversation/taskMarkApi' +import { resourceDownloadSchema, resourcePrepareSchema } from '../privateNetwork/resources' +import { uploadCommandIntents, uploadGetSchema, uploadStateSchema } from '../privateNetwork/uploads' +import { userQuestionRpc } from '../questions/userQuestionApi' +import { desktopOpenIntentSchema } from '../service/localServiceProtocol' +import { productEditableContentSchema } from './content' +import { PRODUCT_QUERY_METHODS } from './queries' +import { productCommandLookupSchema, productCommandResultSchema } from './receipts' +import { productIdSchema as id, productRevisionSchema as revision } from './scalars' + +const draft = z.object({ draftId: id, expectedRevision: revision }).strict() +const recoveredInput = z.object({ + content: productEditableContentSchema, + executionConfig: buddyComposerDraftSaveSchema.unwrap().shape.executionConfig, + modelSelection: buddyComposerDraftSaveSchema.unwrap().shape.modelSelection, + participants: buddyComposerDraftSaveSchema.unwrap().shape.participants, +}).strict() +const queue = z.object({ conversationId: id, branchId: id, id, expectedRevision: revision }).strict() +const copyScope = z.union([ + ...buddyComposerDraftScopeSchema.options.slice(1), + z.object({ kind: z.literal('task'), spaceId: id.nullable() }).strict(), +]) +const envelope = (intent: ZodType) => z.object({ dataGeneration: id, commandId: id, expectedServiceInstanceId: id, intent }).strict() +const command = (intent: ZodType) => ({ input: envelope(intent), output: productCommandResultSchema, role: 'owner', command: true, ready: true } as const) +export const productCommandIntents = { + ...uploadCommandIntents, + 'questions.answer': userQuestionRpc.answer.input, + 'questions.cancel': userQuestionRpc.cancel.input, + 'questions.retry': userQuestionRpc.retry.input, + 'desktop.open': desktopOpenIntentSchema, + 'chat.submitInput': submitInputIntentSchema(productEditableContentSchema), + 'chat.startTurn': draft.extend({ buddyId: id.optional(), delivery: buddyInputDeliverySchema.optional() }).strict(), + 'chat.editUserMessage': draft.extend({ conversationId: id, userMessageId: id }).strict(), + 'chat.regenerateAssistant': z.object({ conversationId: id, sourceRunId: id }).strict(), + 'chat.executeCommand': draft, + 'chat.queue.enqueue': draft.extend({ delivery: buddyInputDeliverySchema.optional() }).strict(), + 'chat.queue.cancel': queue, + 'chat.queue.continue': queue, + 'chat.queue.steer': queue.extend({ targetRunId: id, expectedRunGeneration: revision }).strict(), + 'chat.cancel': z.object({ runId: id, expectedRunGeneration: revision }).strict(), + 'composerDrafts.open': z.object({ ...buddyComposerDraftOpenSchema.unwrap().shape, initialContent: productEditableContentSchema, expectedSlotRevision: revision }).strict(), + 'composerDrafts.save': z.object({ ...buddyComposerDraftSaveSchema.unwrap().shape, content: productEditableContentSchema, expectedRevision: revision }).strict(), + 'composerDrafts.discard': draft, + 'composerDrafts.copy': z.object({ sourceDraftId: id, expectedSourceRevision: revision, targetScope: copyScope, expectedSlotRevision: revision, recoveredInput: recoveredInput.optional() }).strict(), + 'approvals.resolve': z.object({ approvalId: id, runId: id, reviewVersion: z.string().regex(/^[a-f0-9]{64}$/), decision: z.enum(['approved', 'denied', 'approved_persistently', 'approved_for_operation', 'approved_for_source', 'approved_for_turn']) }).strict(), + 'conversations.rename': conversationRequestSchemas.conversationRename.extend({ expectedTitleRevision: revision }).strict(), + 'conversations.delete': conversationRequestSchemas.conversationId.extend({ expectedRevision: revision }).strict(), + 'conversations.archive': conversationArchiveTargetSchema.extend({ expectedRevision: revision }).strict(), + 'conversations.restoreArchive': conversationArchiveTargetSchema.extend({ expectedRevision: revision }).strict(), + 'conversations.deleteArchived': conversationArchiveTargetSchema.extend({ expectedRevision: revision }).strict(), + 'conversations.deleteLegacyRemoved': conversationArchiveTargetSchema.extend({ expectedRevision: revision }).strict(), + 'conversations.setArchivePolicy': conversationArchivePolicySchema, + 'conversations.activateBranch': conversationRequestSchemas.conversationBranchActivation.extend({ expectedRevision: revision }).strict(), + 'conversations.setPermissionSettings': conversationRequestSchemas.conversationPermissionSettings.extend({ expectedRevision: revision }).strict(), + 'conversations.setBuddy': conversationRequestSchemas.conversationBuddy.extend({ expectedRevision: revision }).strict(), + 'conversations.addMember': conversationRequestSchemas.conversationBuddy.extend({ expectedRevision: revision }).strict(), + 'conversations.removeMember': conversationRequestSchemas.conversationBuddy.extend({ expectedRevision: revision }).strict(), + 'conversations.moveToSpace': conversationRequestSchemas.conversationMoveToSpace.extend({ expectedRevision: revision }).strict(), + 'conversations.setModelSelection': conversationRequestSchemas.conversationModelSelection.extend({ expectedRevision: revision, expectedRunGeneration: revision.optional(), expectedRunModelSelection: conversationRequestSchemas.conversationModelSelection.shape.modelSelection.optional(), expectedActiveRun: z.object({ runId: id, generation: revision, modelSelection: conversationRequestSchemas.conversationModelSelection.shape.modelSelection }).strict().nullable().optional() }).strict().refine(input => input.targetRunId ? input.expectedRunGeneration !== undefined && input.expectedRunModelSelection !== undefined : input.expectedActiveRun !== undefined), + 'taskMarks.create': taskMarkInputSchema, + 'taskMarks.update': taskMarkInputSchema.extend({ id, expectedRevision: revision }).strict(), + 'taskMarks.delete': z.object({ id, expectedRevision: revision }).strict(), + 'taskMarks.assign': z.object({ conversationId: id, markId: id.nullable(), expectedAssignmentRevision: revision }).strict(), + 'taskMarks.setRead': taskMarkReadSchema.extend({ readRevision: revision }).strict(), + 'taskMarks.clear': taskMarkClearSchema.extend({ readRevision: revision, expectedAssignmentRevision: revision }).strict(), +} as const +export type ProductCommandMethod = keyof typeof productCommandIntents +export const PRODUCT_METHODS = { + ...PRODUCT_QUERY_METHODS, + 'resources.prepare': { input: resourcePrepareSchema, output: resourceDownloadSchema, role: 'reader', command: false, ready: true }, + 'uploads.get': { input: uploadGetSchema, output: uploadStateSchema, role: 'owner', command: false, ready: true }, + ...Object.fromEntries(Object.entries(productCommandIntents).map(([method, intent]) => [method, command(intent)])) as { [M in ProductCommandMethod]: ReturnType }, + 'commands.get': { input: z.object({ dataGeneration: id, commandId: id }).strict(), output: productCommandLookupSchema, role: 'owner', command: false, ready: false }, +} as const +export type ProductMethod = keyof typeof PRODUCT_METHODS +export interface ProductMethodContract { input: ZodType, output: ZodType, role: 'owner' | 'reader', command: boolean, ready: boolean } + +export function getProductMethod(method: string): ProductMethodContract | undefined { + return Object.hasOwn(PRODUCT_METHODS, method) ? PRODUCT_METHODS[method as ProductMethod] : undefined +} diff --git a/apps/buddy/shared/productProtocol/queries.ts b/apps/buddy/shared/productProtocol/queries.ts new file mode 100644 index 000000000..385336e7a --- /dev/null +++ b/apps/buddy/shared/productProtocol/queries.ts @@ -0,0 +1,66 @@ +import type { ZodType } from 'zod' +import { z } from 'zod' +import { 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' +import { executionPhaseSchema } from '../executions/executionApi' +import { userQuestionRequestSchema } from '../questions/userQuestionApi' +import { publicRunEventSchema } from '../runs/publicRunEvent' +import { runSchema, runStatusSchema } from '../runs/runApi' +import { productBuddyAvatarSchema } from './avatars' +import { productContentSchema } from './content' +import { productErrorCodeSchema } from './errors' +import { PRODUCT_LIMITS } from './limits' +import { productIdSchema as id, productRevisionSchema as revision } from './scalars' +import { productApprovalDecisionSchema, productApprovalSchema, productMessageSchema, productModelSchema, productModelSnapshotSchema, productProviderSchema, productQueueItemSchema, productResourceSchema, productRunEventSchema } from './views' + +export const productPageInputSchema = z.object({ limit: z.number().int().min(1).max(PRODUCT_LIMITS.pageItems).default(PRODUCT_LIMITS.pageItems), cursor: z.string().max(4096).nullable().default(null) }).strict() +export function productPageSchema(item: T) { + return z.object({ items: z.array(item).max(PRODUCT_LIMITS.pageItems), nextCursor: z.string().max(4096).nullable() }).strict() +} +export const productTaskSchema = conversationSchema.omit({ metadataRevision: true, titleRevision: true, archivedAt: true }).extend({ revision, titleRevision: revision }).strict() +export const productTaskSummarySchema = conversationSummarySchema.omit({ metadataRevision: true, titleRevision: true, archivedAt: true }).extend({ revision, titleRevision: revision, unread: z.boolean().optional(), previewText: z.string().max(240).optional(), previewBranchId: id.optional(), resultBranchId: id.optional(), resultRunId: id.optional(), lastRunStatus: runStatusSchema.nullable().optional(), pendingQuestionCount: z.number().int().nonnegative().optional() }).strict() +export const productBranchSchema = conversationBranchSchema.extend({ pendingQuestionCount: z.number().int().nonnegative().optional(), pendingApprovalCount: z.number().int().nonnegative().optional(), runningCount: z.number().int().nonnegative().optional(), queuedCount: z.number().int().nonnegative().optional() }).strict() +export const productQuestionSchema = userQuestionRequestSchema.omit({ toolCallId: true, rootRunId: true }).strict() +export const productDraftSchema = z.object({ ...buddyComposerDraftSchema.unwrap().shape, content: productContentSchema, revision, ownerDeviceId: id, writable: z.boolean(), resources: z.array(productResourceSchema).max(16) }).strict() +export const productRunSchema = runSchema.extend({ executionKind: z.enum(['root', 'actor_child', 'maintenance']).optional(), generation: revision, lastEventSequence: revision, errorCode: productErrorCodeSchema.nullable() }).strict() +export const productRunExecutionSchema = z.object({ runId: id, generation: revision, phase: executionPhaseSchema, cancelRequested: z.boolean(), resources: z.enum(['occupied', 'released', 'unknown']) }).strict() +export const productTaskMarkSchema = taskMarkSchema.extend({ revision }).strict() +export const productTaskMarkStateSchema = taskMarkStateSchema.extend({ assignmentRevision: revision, readRevision: revision }).strict() +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 PRODUCT_QUERY_METHODS = { + '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()), + 'conversations.listArchived': { ...query(conversationArchiveQuerySchema.extend({ limit: productPageInputSchema.shape.limit, cursor: productPageInputSchema.shape.cursor }), productConversationArchivePageSchema), role: 'owner' }, + 'conversations.getArchivePolicy': { ...query(z.object({}).strict(), conversationArchivePolicySchema), role: 'owner' }, + 'questions.list': query(productPageInputSchema.extend({ conversationId: id, branchId: id }), productPageSchema(productQuestionSchema)), + 'spaces.list': query(productPageInputSchema, productPageSchema(z.object({ id, name: z.string().min(1), memoryScope: z.enum(['personal_and_space', 'space_only']) }).strict())), + 'conversations.listMessages': query(productPageInputSchema.extend({ conversationId: id, branchId: id }), productPageSchema(productMessageSchema)), + 'runs.listTranscriptEvents': query(productPageInputSchema.extend({ runId: id, afterSequence: revision, throughSequence: revision }), productPageSchema(publicRunEventSchema)), + 'runs.listEvents': query(productPageInputSchema.extend({ runId: id, throughSequence: revision.optional() }), productPageSchema(productRunEventSchema)), + 'chat.queue.list': query(productPageInputSchema.extend({ conversationId: id, branchId: id }), productPageSchema(productQueueItemSchema)), + 'approvals.get': query(z.object({ approvalId: id }).strict(), productApprovalSchema.nullable()), + 'approvals.getDecision': query(z.object({ approvalId: id }).strict(), productApprovalDecisionSchema.nullable()), + 'approvals.list': query(productPageInputSchema.extend({ conversationId: id.optional(), branchId: id.optional(), runId: id.optional(), status: z.enum(['pending', 'approved', 'denied', 'cancelled']).optional() }), productPageSchema(productApprovalSchema)), + 'resources.get': query(z.object({ resourceId: id }).strict(), productResourceSchema.nullable()), + 'resources.list': query(productPageInputSchema.extend({ conversationId: id }), productPageSchema(productResourceSchema)), + 'providers.getInputSelection': query(z.object({ conversationId: id.optional() }).strict(), z.object({ modelSelection: modelSelectionSchema.nullable() }).strict()), + 'providers.list': query(productPageInputSchema, productPageSchema(productProviderSchema)), + 'providers.listModels': query(productPageInputSchema.extend({ providerId: id.optional() }), productPageSchema(productModelSchema)), + 'providers.getModelSnapshot': query(z.object({}).strict(), productModelSnapshotSchema), + 'taskMarks.list': query(productPageInputSchema, productPageSchema(productTaskMarkSchema)), + 'taskMarks.states': query(z.object({ conversationIds: z.array(id).max(PRODUCT_LIMITS.pageItems) }).strict(), z.array(productTaskMarkStateSchema).max(PRODUCT_LIMITS.pageItems)), + 'conversations.list': query(productPageInputSchema.extend({ spaceId: id.nullable().optional() }), productPageSchema(productTaskSummarySchema)), + 'conversations.get': query(z.object({ conversationId: id }).strict(), productTaskSchema.nullable()), + 'conversations.listBranches': query(productPageInputSchema.extend({ conversationId: id }), productPageSchema(productBranchSchema)), + 'composerDrafts.get': query(z.object({ draftId: id }).strict(), productDraftSchema.nullable()), + 'composerDrafts.list': query(productPageInputSchema, productPageSchema(productDraftSchema)), + 'composerDrafts.find': query(z.object({ scope: buddyComposerDraftScopeSchema }).strict(), z.object({ draft: productDraftSchema.nullable(), slotRevision: revision }).strict()), + 'runs.get': query(z.object({ runId: id }).strict(), productRunSchema.nullable()), + 'runs.getExecutionState': query(z.object({ runId: id }).strict(), productRunExecutionSchema.nullable()), + 'runs.list': query(productPageInputSchema.extend({ conversationId: id, branchId: id }), productPageSchema(productRunSchema)), +} as const diff --git a/apps/buddy/shared/productProtocol/receipts.ts b/apps/buddy/shared/productProtocol/receipts.ts new file mode 100644 index 000000000..c17dde459 --- /dev/null +++ b/apps/buddy/shared/productProtocol/receipts.ts @@ -0,0 +1,61 @@ +import { z } from 'zod' +import { conversationArchivePolicySchema } from '../conversation/conversationArchiveApi' +import { desktopOpenResultSchema } from '../service/localServiceProtocol' +import { productErrorCodeSchema } from './errors' +import { productIdSchema as id, productRevisionSchema as revision } from './scalars' + +export const productReceiptTargetSchema = z.object({ + uploadId: id.optional(), + actorId: id.optional(), + markId: id.optional(), + conversationId: id.optional(), + branchId: id.optional(), + runId: id.optional(), + messageId: id.optional(), + queueId: id.optional(), + draftId: id.optional(), + sourceDraftId: id.optional(), + approvalId: id.optional(), + questionId: id.optional(), +}).strict() +export const productReceiptResultSchema = z.object({ + archived: z.boolean().optional(), + restored: z.boolean().optional(), + deleted: z.boolean().optional(), + archivePolicy: conversationArchivePolicySchema.optional(), + questionStatus: z.enum(['pending', 'answered', 'cancelled', 'interrupted']).optional(), + questionDelivery: z.enum(['pending', 'delivered', 'failed']).optional(), + desktop: desktopOpenResultSchema.optional(), + uploadId: id.optional(), + resourceId: id.optional(), + attachmentId: id.optional(), + contentVersion: z.string().regex(/^[a-f0-9]{64}$/).optional(), + draftId: id.optional(), + revision: revision.optional(), + slotRevision: revision.optional(), + sourceRevision: revision.optional(), + discarded: z.boolean().optional(), + cancelled: z.boolean().optional(), + deliveryStage: z.enum(['committed']).optional(), + status: z.enum(['pending', 'approved', 'denied', 'cancelled']).nullable().optional(), + resolution: z.enum(['approved', 'denied', 'approved_persistently', 'approved_for_operation', 'approved_for_source', 'approved_for_turn', 'cancelled']).nullable().optional(), + reviewVersion: z.string().regex(/^[a-f0-9]{64}$/).optional(), + resolvedAt: z.iso.datetime().optional(), + reason: z.enum(['changed', 'expired', 'already_resolved', 'unsupported']).optional(), + draftReceipt: z.object({ draftId: id, committedRevision: revision }).strict().optional(), +}).strict() +export const productCommandReceiptSchema = z.object({ + dataGeneration: id, + commandId: id, + method: id, + decision: z.enum(['accepted', 'rejected']), + decidedAt: z.iso.datetime(), + target: productReceiptTargetSchema, + result: productReceiptResultSchema, + errorCode: productErrorCodeSchema.optional(), +}).strict().refine(receipt => (receipt.decision === 'rejected') === (receipt.errorCode !== undefined)) +export const productCommandResultSchema = z.object({ receipt: productCommandReceiptSchema }).strict() +export const productCommandLookupSchema = z.discriminatedUnion('lookup', [ + z.object({ lookup: z.literal('decided'), receipt: productCommandReceiptSchema }).strict(), + z.object({ lookup: z.enum(['pending', 'not_found']) }).strict(), +]) diff --git a/apps/buddy/shared/productProtocol/scalars.ts b/apps/buddy/shared/productProtocol/scalars.ts new file mode 100644 index 000000000..fd031e9d5 --- /dev/null +++ b/apps/buddy/shared/productProtocol/scalars.ts @@ -0,0 +1,41 @@ +import { z } from 'zod' + +export const PRODUCT_INTEGER_MAX = 9_223_372_036_854_775_807n +export const productIdSchema = z.string().min(1).max(256) +export const productRevisionSchema = z.string().regex(/^(?:0|[1-9]\d{0,18})$/).refine(value => /^(?:0|[1-9]\d{0,18})$/.test(value) && BigInt(value) <= PRODUCT_INTEGER_MAX) +export type ProductRevision = z.infer + +export function encodeProductRevision(value: bigint | number): ProductRevision { + if (typeof value === 'number' && !Number.isSafeInteger(value)) + throw new RangeError('Revision must be exactly representable') + return productRevisionSchema.parse(String(value)) +} + +export function decodeSafeProductRevision(value: string): number { + const revision = BigInt(productRevisionSchema.parse(value)) + if (revision > BigInt(Number.MAX_SAFE_INTEGER)) + throw new RangeError('Revision exceeds the current storage representation') + return Number(revision) +} + +const cursorSchema = z.object({ + version: z.literal(1), + scope: productIdSchema, + branchId: productIdSchema.nullable(), + revision: productRevisionSchema, + upperBound: productRevisionSchema, + position: productRevisionSchema, + anchorId: productIdSchema.optional(), +}).strict().refine(cursor => productRevisionSchema.safeParse(cursor.position).success && productRevisionSchema.safeParse(cursor.upperBound).success && BigInt(cursor.position) <= BigInt(cursor.upperBound)) +export type ProductPageCursor = z.infer +export function encodeProductCursor(cursor: ProductPageCursor): string { + return `v1.${encodeURIComponent(JSON.stringify(cursorSchema.parse(cursor)))}` +} +export function decodeProductCursor(value: string, expected: Omit): ProductPageCursor { + if (!value.startsWith('v1.') || value.length > 4096) + throw new Error('CURSOR_INVALID') + const cursor = cursorSchema.parse(JSON.parse(decodeURIComponent(value.slice(3)))) + if (cursor.scope !== expected.scope || cursor.branchId !== expected.branchId || cursor.revision !== expected.revision) + throw new Error('CURSOR_EXPIRED') + return cursor +} diff --git a/apps/buddy/shared/productProtocol/sync.ts b/apps/buddy/shared/productProtocol/sync.ts new file mode 100644 index 000000000..764e58c6a --- /dev/null +++ b/apps/buddy/shared/productProtocol/sync.ts @@ -0,0 +1,35 @@ +import { z } from 'zod' +import { PRODUCT_LIMITS } from './limits' +import { productIdSchema, productRevisionSchema } from './scalars' + +export const productSyncCursorSchema = z.object({ syncEpoch: productIdSchema, seq: productRevisionSchema }).strict() +export type ProductSyncCursor = z.infer +export const productSubscribeSchema = z.object({ subscriptionId: productIdSchema, topic: z.string().min(1).max(2048), base: productSyncCursorSchema.optional() }).strict() +export const productSnapshotPageSchema = z.object({ subscriptionId: productIdSchema, cursor: z.string().min(1).max(4096) }).strict() +export const productAckSchema = productSyncCursorSchema.extend({ subscriptionId: productIdSchema }).strict() +export const productUnsubscribeSchema = z.object({ subscriptionId: productIdSchema }).strict() +export interface ProductSyncDelta { subscriptionId: string, syncEpoch: string, fromSeq: string, toSeq: string, changes: readonly T[] } +export type ProductSyncSnapshot = { subscriptionId: string, mode: 'snapshot', cursor: ProductSyncCursor, items: readonly T[], complete: boolean, nextCursor: string | null } | { subscriptionId: string, mode: 'resume', cursor: ProductSyncCursor } + +export function classifyProductDelta(applied: ProductSyncCursor, delta: Pick): 'apply' | 'duplicate' | 'resync' { + const from = productRevisionSchema.safeParse(delta.fromSeq) + const to = productRevisionSchema.safeParse(delta.toSeq) + if (!from.success || !to.success || !productSyncCursorSchema.safeParse(applied).success || delta.syncEpoch !== applied.syncEpoch || BigInt(to.data) <= BigInt(from.data)) + return 'resync' + if (BigInt(to.data) <= BigInt(applied.seq)) + return 'duplicate' + return from.data === applied.seq ? 'apply' : 'resync' +} + +export const productSyncSnapshotSchema = z.discriminatedUnion('mode', [ + z.object({ subscriptionId: productIdSchema, mode: z.literal('snapshot'), cursor: productSyncCursorSchema, items: z.array(z.unknown()).max(PRODUCT_LIMITS.pageItems), complete: z.boolean(), nextCursor: z.string().max(4096).nullable() }).strict().refine(value => value.complete === (value.nextCursor === null)), + z.object({ subscriptionId: productIdSchema, mode: z.literal('resume'), cursor: productSyncCursorSchema }).strict(), +]) +export const productSyncDeltaSchema = z.object({ + subscriptionId: productIdSchema, + syncEpoch: productIdSchema, + fromSeq: productRevisionSchema, + toSeq: productRevisionSchema, + changes: z.array(z.unknown()).min(1).max(PRODUCT_LIMITS.subscriptionItems), +}).strict().refine(value => productRevisionSchema.safeParse(value.fromSeq).success && productRevisionSchema.safeParse(value.toSeq).success && BigInt(value.fromSeq) < BigInt(value.toSeq)) +export const productResyncRequiredSchema = z.object({ subscriptionId: productIdSchema, reason: z.literal('RESYNC_REQUIRED') }).strict() diff --git a/apps/buddy/shared/productProtocol/topics.ts b/apps/buddy/shared/productProtocol/topics.ts new file mode 100644 index 000000000..48648ef3a --- /dev/null +++ b/apps/buddy/shared/productProtocol/topics.ts @@ -0,0 +1,56 @@ +import { z } from 'zod' +import { productPhaseSchema } from './identity' +import { productDraftSchema, productQuestionSchema, productRunSchema, productTaskSchema, productTaskSummarySchema } from './queries' +import { productIdSchema } from './scalars' +import { productApprovalSchema, productMessageSchema, productQueueItemSchema, productRunEventSchema } from './views' + +const remove = z.object({ kind: z.literal('remove'), id: productIdSchema }).strict() +export const productTaskIndexChangeSchema = z.union([remove, z.object({ kind: z.literal('upsert'), value: productTaskSummarySchema }).strict()]) +export const productDraftIndexChangeSchema = z.union([remove, z.object({ kind: z.literal('upsert'), value: productDraftSchema }).strict()]) +export const productApprovalInboxChangeSchema = z.union([remove, z.object({ kind: z.literal('upsert'), value: productApprovalSchema }).strict()]) +export const productQuestionChangeSchema = z.union([remove, z.object({ kind: z.literal('upsert'), value: productQuestionSchema }).strict()]) +export const productConversationItemSchema = z.discriminatedUnion('entity', [ + z.object({ entity: z.literal('task'), value: productTaskSchema }).strict(), + z.object({ entity: z.literal('message'), value: productMessageSchema }).strict(), + z.object({ entity: z.literal('run'), value: productRunSchema }).strict(), + z.object({ entity: z.literal('queue'), value: productQueueItemSchema }).strict(), + z.object({ entity: z.literal('approval'), value: productApprovalSchema }).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(), + z.object({ kind: z.literal('event'), event: productRunEventSchema }).strict(), +]) +export const productServiceStateSchema = z.object({ phase: productPhaseSchema }).strict() +export const PRODUCT_TOPIC_SCHEMAS = { + taskIndex: { item: productTaskSummarySchema, change: productTaskIndexChangeSchema }, + draftIndex: { item: productDraftSchema, change: productDraftIndexChangeSchema }, + draft: { item: productDraftSchema, change: productDraftIndexChangeSchema }, + conversation: { item: productConversationItemSchema, 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 function formatProductTopic(topic: ProductTopic): string { + if (topic.kind === 'draft') + return `draft:${encodeURIComponent(productIdSchema.parse(topic.draftId))}` + if (topic.kind === 'conversation' || topic.kind === 'questions') + return `${topic.kind}:${encodeURIComponent(productIdSchema.parse(topic.conversationId))}:${encodeURIComponent(productIdSchema.parse(topic.branchId))}` + return topic.kind +} +export function parseProductTopic(value: string): ProductTopic { + const parts = value.split(':') + let topic: ProductTopic + if (parts.length === 1 && ['taskIndex', 'draftIndex', 'approvalInbox', 'serviceState'].includes(value)) + 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')) + topic = { kind: parts[0], conversationId: productIdSchema.parse(decodeURIComponent(parts[1]!)), branchId: productIdSchema.parse(decodeURIComponent(parts[2]!)) } + else + throw new Error('Unknown product topic') + if (formatProductTopic(topic) !== value) + throw new Error('Noncanonical product topic') + return topic +} diff --git a/apps/buddy/shared/productProtocol/transport.ts b/apps/buddy/shared/productProtocol/transport.ts new file mode 100644 index 000000000..5914e634f --- /dev/null +++ b/apps/buddy/shared/productProtocol/transport.ts @@ -0,0 +1,11 @@ +import { z } from 'zod' +import { buddyUserContentV1Schema } from '../conversation/buddyUserContent' +import { productEditableContentSchema } from './content' +import { PRODUCT_LIMITS } from './limits' + +export const productExchangeSchema = z.object({ frame: z.string().max(PRODUCT_LIMITS.frameBytes) }).strict() +export const productExchangeResultSchema = z.object({ received: z.literal(true) }).strict() +export const productFrameNotification = { method: 'product.frame', params: productExchangeSchema } as const +export const productExchange = { method: 'product.exchange', input: productExchangeSchema, response: productExchangeResultSchema } as const + +export const desktopProductContentProject = { method: 'product.content.project', input: z.object({ content: buddyUserContentV1Schema }).strict(), response: z.object({ content: productEditableContentSchema }).strict() } as const diff --git a/apps/buddy/shared/productProtocol/vectors.json b/apps/buddy/shared/productProtocol/vectors.json new file mode 100644 index 000000000..0ae4e2a12 --- /dev/null +++ b/apps/buddy/shared/productProtocol/vectors.json @@ -0,0 +1,264 @@ +{ + "version": 1, + "requests": [ + { + "name": "initialize", + "method": "initialize", + "params": { + "protocol": { + "major": 1, + "minMinor": 0, + "maxMinor": 0 + }, + "client": { + "kind": "desktop", + "version": "1.0" + }, + "supportedContentKinds": [ + "buddy-user-content-v1" + ], + "supportedReviewKinds": [] + }, + "valid": true + }, + { + "name": "no-client-role", + "method": "initialize", + "params": { + "protocol": { + "major": 1, + "minMinor": 0, + "maxMinor": 0 + }, + "client": { + "kind": "desktop", + "version": "1.0" + }, + "supportedContentKinds": [ + "buddy-user-content-v1" + ], + "supportedReviewKinds": [], + "role": "owner" + }, + "valid": false + }, + { + "name": "start-draft", + "method": "chat.startTurn", + "params": { + "dataGeneration": "generation-1", + "commandId": "command-1", + "expectedServiceInstanceId": "instance-1", + "intent": { + "draftId": "draft-1", + "expectedRevision": "0" + } + }, + "valid": true + }, + { + "name": "reject-auth-actor", + "method": "chat.startTurn", + "params": { + "dataGeneration": "generation-1", + "commandId": "command-1", + "expectedServiceInstanceId": "instance-1", + "intent": { + "draftId": "draft-1", + "expectedRevision": "0" + }, + "actorId": "actor-1" + }, + "valid": false + }, + { + "name": "numeric-revision", + "method": "chat.startTurn", + "params": { + "dataGeneration": "generation-1", + "commandId": "command-1", + "expectedServiceInstanceId": "instance-1", + "intent": { + "draftId": "draft-1", + "expectedRevision": 0 + } + }, + "valid": false + }, + { + "name": "host-method-unavailable", + "method": "runtime.stop", + "params": {}, + "valid": false + }, + { + "name": "default-page", + "method": "conversations.list", + "params": {}, + "valid": true + }, + { + "name": "null-page", + "method": "conversations.list", + "params": { + "cursor": null + }, + "valid": true + }, + { + "name": "unknown-page-field", + "method": "conversations.list", + "params": { + "path": "/fixture/private" + }, + "valid": false + } + ], + "revisions": [ + { + "name": "0", + "value": "0", + "valid": true + }, + { + "name": "9223372036854775807", + "value": "9223372036854775807", + "valid": true + }, + { + "name": "9223372036854775808", + "value": "9223372036854775808", + "valid": false + }, + { + "name": "01", + "value": "01", + "valid": false + }, + { + "name": "-1", + "value": "-1", + "valid": false + }, + { + "name": "1.0", + "value": "1.0", + "valid": false + }, + { + "name": "invalid", + "value": "invalid", + "valid": false + }, + { + "name": "9007199254740992", + "value": 9007199254740992, + "valid": false + } + ], + "responses": [ + { + "name": "receipt-over-js-safe-integer", + "method": "composerDrafts.save", + "result": { + "receipt": { + "dataGeneration": "generation-1", + "commandId": "command-1", + "method": "composerDrafts.save", + "decision": "accepted", + "decidedAt": "2026-01-01T00:00:00.000Z", + "target": { + "draftId": "draft-1" + }, + "result": { + "revision": "9007199254740992" + } + } + }, + "valid": true + }, + { + "name": "receipt-reject-internal-field", + "method": "composerDrafts.save", + "result": { + "receipt": { + "dataGeneration": "generation-1", + "commandId": "command-1", + "method": "composerDrafts.save", + "decision": "accepted", + "decidedAt": "2026-01-01T00:00:00.000Z", + "target": { + "draftId": "draft-1" + }, + "result": { + "revision": "9007199254740992" + } + }, + "fingerprint": "fixture-internal" + }, + "valid": false + }, + { + "name": "lookup-pending", + "method": "commands.get", + "result": { + "lookup": "pending" + }, + "valid": true + }, + { + "name": "lookup-unknown-is-not-notfound", + "method": "commands.get", + "result": { + "lookup": "unknown" + }, + "valid": false + } + ], + "contents": [ + { + "name": "chinese-emoji-utf16", + "value": { + "kind": "editable", + "content": { + "version": 1, + "body": [ + { + "type": "paragraph", + "content": [ + { + "type": "text", + "text": "你好😀世界" + } + ] + } + ], + "panelResourceIds": [] + } + }, + "valid": true + }, + { + "name": "unknown-preserved-readonly", + "value": { + "kind": "unsupported", + "contentReference": "content-1", + "version": 2, + "summary": "不支持的内容", + "readOnly": true + }, + "valid": true + }, + { + "name": "unknown-must-not-be-editable", + "value": { + "kind": "unsupported", + "contentReference": "content-1", + "version": 2, + "summary": "不支持的内容", + "readOnly": false + }, + "valid": false + } + ] +} diff --git a/apps/buddy/shared/productProtocol/views.ts b/apps/buddy/shared/productProtocol/views.ts new file mode 100644 index 000000000..43871848f --- /dev/null +++ b/apps/buddy/shared/productProtocol/views.ts @@ -0,0 +1,99 @@ +import { z } from 'zod' +import { approvalReviewSchema } from '../permissions/conditionalApproval' +import { modelSchema, modelSnapshotSchema, providerSchema } from '../providers/providerApi' +import { productContentSchema } from './content' +import { productErrorCodeSchema } from './errors' +import { productIdSchema as id, productRevisionSchema as revision } from './scalars' + +export const productResourceSchema = z.object({ + resourceId: id, + bindingId: id.optional(), + kind: z.enum(['attachment', 'artifact', 'message', 'review', 'reference']), + name: z.string().max(512), + mimeType: z.string().max(256), + sizeBytes: revision, + revision: z.string().min(1).max(256), + availability: z.enum(['ready', 'unavailable', 'export_required']), +}).strict() +export const productMessageSchema = z.object({ + id, + conversationId: id, + branchId: id, + runId: id.nullable(), + role: z.enum(['user', 'assistant', 'tool']), + createdAt: z.iso.datetime(), + body: z.discriminatedUnion('kind', [ + z.object({ kind: z.literal('user'), content: productContentSchema }).strict(), + z.object({ kind: z.literal('text'), text: z.string().max(131_072), interrupted: z.boolean(), truncated: z.boolean() }).strict(), + z.object({ kind: z.literal('resource'), resourceId: id, previewText: z.string().max(4096), readOnly: z.literal(true), interrupted: z.boolean().optional(), truncated: z.boolean().optional() }).strict(), + ]), + resources: z.array(productResourceSchema).max(16), + tool: z.object({ name: z.string().min(1).max(256), isError: z.boolean() }).strict().optional(), + author: z.object({ id, name: z.string().trim().min(1).max(80) }).strict().optional(), +}).strict() +export const productApprovalSchema = z.object({ + id, + runId: id, + conversationId: id.optional(), + branchId: id.optional(), + kind: z.string().min(1).max(80), + summary: z.string().max(4096), + status: z.enum(['pending', 'approved', 'denied', 'cancelled']), + createdAt: z.iso.datetime(), + resolvedAt: z.iso.datetime().nullable(), + resolution: z.string().max(80).nullable(), + review: z.object({ + ...approvalReviewSchema.omit({ body: true, lifetimeId: true }).shape, + document: z.object({ format: z.literal('json'), text: z.string().max(131_072) }).strict().nullable(), + resourceId: id.nullable(), + localManagementRequired: z.boolean(), + }).strict().nullable(), +}).strict() +export const productApprovalDecisionSchema = z.object({ + approvalId: id, + runId: id, + reviewVersion: z.string().regex(/^[a-f0-9]{64}$/).nullable(), + status: z.enum(['approved', 'denied', 'cancelled']), + resolution: z.string().max(80).nullable(), + resolvedAt: z.iso.datetime().nullable(), + actor: z.object({ id, kind: z.enum(['desktop', 'device']), current: z.boolean() }).strict().nullable(), + cancellationReason: z.enum(['cancelled', 'expired', 'recovery', 'shutdown']).nullable(), +}).strict() +export const productQueueItemSchema = z.object({ + id, + conversationId: id, + branchId: id, + revision, + state: z.enum(['waiting', 'paused', 'sent', 'cancelled']), + createdAt: z.iso.datetime(), + sourceRunId: id.nullable(), + content: productContentSchema, + steerTarget: z.object({ runId: id, generation: revision }).strict().nullable(), +}).strict() +export const productRunEventSchema = z.object({ + runId: id, + sequence: revision, + createdAt: z.iso.datetime(), + type: z.string().min(1).max(128), + detail: z.discriminatedUnion('kind', [ + z.object({ kind: z.literal('text_delta'), messageId: id, delta: z.string().max(65_536), phase: z.enum(['commentary', 'final_answer']).nullable() }).strict(), + z.object({ kind: z.literal('status'), messageId: id.optional(), approvalId: id.optional(), toolCallId: id.optional(), toolName: z.string().max(256).optional(), errorCode: productErrorCodeSchema.optional() }).strict(), + z.object({ kind: z.literal('resource'), resourceId: id }).strict(), + ]), +}).strict() +export const productProviderSchema = providerSchema.pick({ id: true, displayName: true, enabled: true, status: true, modelCount: true, enabledModelCount: true }).strict() +export const productModelSchema = z.object({ + modelId: modelSchema.shape.modelId, + providerId: modelSchema.shape.providerId, + displayName: modelSchema.shape.displayName, + type: modelSchema.shape.type, + available: modelSchema.shape.available, + enabled: modelSchema.shape.enabled, + capabilities: modelSchema.shape.capabilities, + contextWindow: modelSchema.shape.contextWindow, + maxTokens: modelSchema.shape.maxTokens, + fileInputMimeTypes: modelSchema.shape.fileInputMimeTypes, + reasoningOptions: modelSchema.shape.reasoningOptions, + serviceTiers: modelSchema.shape.serviceTiers, +}).strict() +export { modelSnapshotSchema as productModelSnapshotSchema } diff --git a/apps/buddy/shared/runs/runApi.ts b/apps/buddy/shared/runs/runApi.ts index af51a938f..2c6ca44bf 100644 --- a/apps/buddy/shared/runs/runApi.ts +++ b/apps/buddy/shared/runs/runApi.ts @@ -16,6 +16,7 @@ export const runSchema = z.object({ errorCode: z.string().nullable(), executionProfile: executionProfileSchema, id: idSchema, + generation: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER).optional(), modelId: idSchema, providerId: idSchema, purpose: z.string().min(1), @@ -65,6 +66,7 @@ export const runsRequestSchemas = { }).strict(), ]), runId: z.object({ runId: idSchema }).strict(), + cancel: z.object({ runId: idSchema, expectedRunGeneration: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER).optional() }).strict(), runStateEvent: runEventEnvelopeSchema, } as const diff --git a/apps/buddy/shared/runtime/attachmentHostProtocol.ts b/apps/buddy/shared/runtime/attachmentHostProtocol.ts new file mode 100644 index 000000000..4e38f8229 --- /dev/null +++ b/apps/buddy/shared/runtime/attachmentHostProtocol.ts @@ -0,0 +1,7 @@ +import { z } from 'zod' + +export const ATTACHMENT_PREVIEW_BYTES_LIMIT = 8 * 1024 * 1024 +export const attachmentPreviewBytesSchema = z.object({ + mimeType: z.string().regex(/^image\//), + data: z.string().max(Math.ceil(ATTACHMENT_PREVIEW_BYTES_LIMIT / 3) * 4).regex(/^[a-z0-9+/]*={0,2}$/i), +}).strict() diff --git a/apps/buddy/shared/runtime/credentialProtocol.ts b/apps/buddy/shared/runtime/credentialProtocol.ts index d271d5abf..b87449739 100644 --- a/apps/buddy/shared/runtime/credentialProtocol.ts +++ b/apps/buddy/shared/runtime/credentialProtocol.ts @@ -1,6 +1,6 @@ import { z } from 'zod' -export const credentialNamespaceSchema = z.enum(['providers', 'connectors', 'web']) +export const credentialNamespaceSchema = z.enum(['providers', 'connectors', 'web', 'private-network']) export const credentialTypeSchema = z.enum(['api_key', 'oauth']) export const credentialHostErrorCodeSchema = z.enum([ diff --git a/apps/buddy/shared/runtime/localChatError.ts b/apps/buddy/shared/runtime/localChatError.ts index c31751805..5b6ed2eec 100644 --- a/apps/buddy/shared/runtime/localChatError.ts +++ b/apps/buddy/shared/runtime/localChatError.ts @@ -31,6 +31,12 @@ export type LocalChatErrorCode | 'BUDDY_BUILTIN_PROTECTED' | 'BUDDY_DEFAULT_PROTECTED' | 'BUDDY_RESET_UNAVAILABLE' + | 'COMMAND_RESULT_UNKNOWN' + | 'SOURCE_CHANGED' + | 'QUEUE_CHANGED' + | 'REVIEW_CHANGED' + | 'APPROVAL_ALREADY_RESOLVED' + | 'OBJECT_DELETED' | 'DRAFT_CONFLICT' | 'INPUT_DELIVERY_TARGET_UNAVAILABLE' | 'INPUT_DELIVERY_INCOMPATIBLE' @@ -118,6 +124,12 @@ const LOCAL_CHAT_ERROR_CODES = new Set([ 'BUDDY_BUILTIN_PROTECTED', 'BUDDY_DEFAULT_PROTECTED', 'BUDDY_RESET_UNAVAILABLE', + 'COMMAND_RESULT_UNKNOWN', + 'SOURCE_CHANGED', + 'QUEUE_CHANGED', + 'REVIEW_CHANGED', + 'APPROVAL_ALREADY_RESOLVED', + 'OBJECT_DELETED', 'DRAFT_CONFLICT', 'INPUT_DELIVERY_TARGET_UNAVAILABLE', 'INPUT_DELIVERY_INCOMPATIBLE', diff --git a/apps/buddy/shared/runtime/productRecovery.ts b/apps/buddy/shared/runtime/productRecovery.ts new file mode 100644 index 000000000..41935f2ea --- /dev/null +++ b/apps/buddy/shared/runtime/productRecovery.ts @@ -0,0 +1,16 @@ +import { z } from 'zod' +import { buddyUserContentV1Schema } from '../conversation/buddyUserContent' + +export const productRecoveryItemSchema = z.object({ + id: z.string().regex(/^[a-f0-9]{64}$/), + commandId: z.string(), + createdAt: z.iso.datetime(), + method: z.string(), + state: z.enum(['accepted', 'rejected', 'not_accepted', 'unknown', 'trust_changed']), + conversationId: z.string().nullable(), + content: buddyUserContentV1Schema.nullable(), + canRestore: z.boolean(), +}).strict() +export type ProductRecoveryItem = z.infer +export const productRecoveryTargetSchema = productRecoveryItemSchema.pick({ id: true }) +export const productRecoveryListSchema = z.array(productRecoveryItemSchema) diff --git a/apps/buddy/shared/runtime/profile.ts b/apps/buddy/shared/runtime/profile.ts index 90752eeeb..a907f16e0 100644 --- a/apps/buddy/shared/runtime/profile.ts +++ b/apps/buddy/shared/runtime/profile.ts @@ -1,3 +1,5 @@ export const BUDDY_RUNTIME_PROFILES = ['stable', 'development', 'test'] as const export type BuddyRuntimeProfile = typeof BUDDY_RUNTIME_PROFILES[number] + +export const BUDDY_DEVELOPMENT_RESTART_EXIT_CODE = 75 diff --git a/apps/buddy/shared/service/localServiceProtocol.ts b/apps/buddy/shared/service/localServiceProtocol.ts new file mode 100644 index 000000000..73d5281ff --- /dev/null +++ b/apps/buddy/shared/service/localServiceProtocol.ts @@ -0,0 +1,102 @@ +import { z } from 'zod' +import { runtimePreferencesSchema } from '../runtime/runtimePreferences' + +export const LOCAL_SERVICE_PROTOCOL = 1 +export const SERVICE_FRAME_BYTES = 1024 * 1024 +export const SERVICE_CONNECTION_BYTES = 4 * SERVICE_FRAME_BYTES +const bounded = z.string().min(1).max(8192) +const identity = z.string().min(1).max(256) +const digest = z.string().regex(/^[a-f0-9]{64}$/) +export const serviceProcessIdentitySchema = z.object({ pid: z.number().int().positive(), executable: bounded, startIdentity: identity }).strict() +export const serviceIdentitySchema = z.object({ + protocol: z.literal(LOCAL_SERVICE_PROTOCOL), + profileId: identity, + serviceInstanceId: z.uuid(), + buildId: digest, + sessionId: identity, + userId: identity, +}).strict() +export const serviceLaunchSchema = z.object({ + version: z.literal(1), + profile: z.enum(['stable', 'development', 'test']), + profileRoot: bounded, + defaultRoot: bounded.optional(), + resourcesRoot: bounded, + nodeExecutable: bounded, + coreEntry: bounded, + hostExecutable: bounded, + hostArguments: z.array(z.string().max(8192)).max(8).default([]), + releaseSetPath: bounded, + buildId: digest, + sessionId: identity, + userId: identity, + leaseDirectory: bounded.optional(), + privateDirectoriesExecutable: bounded.optional(), + storageAuthority: z.enum(['legacy_jsonl', 'sqlite']), +}).strict() +export const serviceDiscoverySchema = serviceIdentitySchema.extend({ + process: serviceProcessIdentitySchema, + managementEndpoint: bounded, + productEndpoint: bounded, + phase: z.enum(['starting', 'recovering', 'maintenance', 'ready', 'stopping', 'failed']), +}).strict() +export const serviceHelloSchema = serviceIdentitySchema.extend({ + type: z.literal('hello'), + role: z.enum(['host', 'management', 'product']), + secret: z.string().regex(/^[a-f0-9]{64}$/), +}).strict() +export const serviceWelcomeSchema = serviceIdentitySchema.extend({ type: z.literal('welcome'), connectionId: z.uuid() }).strict() +export const serviceHostBootstrapSchema = z.object({ + version: z.literal(1), + identity: serviceIdentitySchema, + profile: z.enum(['stable', 'development', 'test']), + profileRoot: bounded, + resourcesRoot: bounded, + endpoint: bounded, + managementEndpoint: bounded, + managementSecret: digest, + hostInstanceId: z.uuid(), + secret: z.string().regex(/^[a-f0-9]{64}$/), +}).strict() +export const desktopOpenIntentSchema = z.discriminatedUnion('kind', [ + z.object({ kind: z.literal('tasks'), conversationId: identity.optional() }).strict(), + z.object({ kind: z.literal('settings') }).strict(), +]) +export const desktopOpenResultSchema = z.object({ + accepted: z.boolean(), + uiReady: z.boolean(), + targetVisible: z.boolean(), + visible: z.boolean(), + focused: z.boolean(), +}).strict() +export type DesktopOpenIntent = z.infer +export type DesktopOpenResult = z.infer + +export const serviceStopReasonSchema = z.enum(['user', 'explicit', 'restart', 'maintenance', 'session_end']) +export const serviceManagementRequestSchema = z.discriminatedUnion('method', [ + z.object({ id: z.uuid(), method: z.literal('status') }).strict(), + z.object({ id: z.uuid(), method: z.literal('openDesktop'), expectedServiceInstanceId: z.uuid(), intent: desktopOpenIntentSchema }).strict(), + z.object({ id: z.uuid(), method: z.literal('stop'), expectedServiceInstanceId: z.uuid(), reason: serviceStopReasonSchema }).strict(), +]) +export type ServiceIdentity = z.infer +export type ServiceLaunch = z.infer +export type ServiceDiscovery = z.infer +export type ServiceHello = z.infer +export type ServiceHostBootstrap = z.infer +export type ServiceManagementRequest = z.infer + +export const platformHostReadySchema = z.object({ + hostInstanceId: z.uuid(), + proxy: z.object({ httpProxy: z.string(), httpsProxy: z.string(), noProxy: z.string() }).strict(), + preferences: runtimePreferencesSchema, + display: z.boolean(), +}).strict() +export const serviceRuntimeStateSchema = z.object({ + generation: z.uuid(), + status: z.enum(['stopped', 'starting', 'ready', 'restarting', 'offline', 'stopping']), + maintenance: z.literal('backup').nullable(), + errorCode: z.string().min(1).max(128).nullable(), +}).strict() +export const serviceMaintenanceTicketSchema = z.object({ token: z.uuid(), generation: z.uuid() }).strict() +export const desktopTargetRequestSchema = z.object({ requestId: z.uuid(), intent: desktopOpenIntentSchema }).strict() +export const desktopTargetAckSchema = z.object({ requestId: z.uuid(), opened: z.boolean() }).strict() diff --git a/apps/buddy/shared/settings/settingsCatalog.ts b/apps/buddy/shared/settings/settingsCatalog.ts index ebddb1934..a3b1d7899 100644 --- a/apps/buddy/shared/settings/settingsCatalog.ts +++ b/apps/buddy/shared/settings/settingsCatalog.ts @@ -16,6 +16,7 @@ export const builtinSettingsCatalog = { 'browser': { section: 'integrations', requiresRuntime: false }, 'file-opening': { section: 'integrations', requiresRuntime: false }, 'storage': { section: 'system', requiresRuntime: false }, + 'connections': { section: 'system', requiresRuntime: true }, 'proxy': { section: 'system', requiresRuntime: false }, 'logs': { section: 'system', requiresRuntime: false }, 'about': { section: 'system', requiresRuntime: false }, diff --git a/apps/buddy/shared/storage/storageApi.ts b/apps/buddy/shared/storage/storageApi.ts index cc335c56b..c16adc64e 100644 --- a/apps/buddy/shared/storage/storageApi.ts +++ b/apps/buddy/shared/storage/storageApi.ts @@ -8,7 +8,7 @@ export const STORAGE_CHANNEL = 'lexora:storage' export const STORAGE_CHANGED = 'lexora:storage:changed' export const storageOperationSchema = z.object({ id: z.uuid(), - kind: z.enum(['consolidate', 'relocate', 'restore']), + kind: z.enum(['consolidate', 'relocate', 'restore', 'upgrade']), source: z.string().min(1), target: z.string().min(1), phase: z.enum(['pending', 'copying', 'converting', 'verifying', 'ready', 'failed']), diff --git a/apps/buddy/shared/workbench/workbenchContributionCatalog.ts b/apps/buddy/shared/workbench/workbenchContributionCatalog.ts index fb5521877..e0c1b2bd2 100644 --- a/apps/buddy/shared/workbench/workbenchContributionCatalog.ts +++ b/apps/buddy/shared/workbench/workbenchContributionCatalog.ts @@ -41,7 +41,7 @@ export const workbenchMenus = { export const workbenchDecorations = workbenchRegions satisfies Record export const workbenchRuntimeCapabilities = { - 'taskCleanup': { title: { 'zh-CN': '旧版已移除任务清理', 'en-US': 'Legacy removed task cleanup' }, scope: 'application', description: 'Lexora >=0.22.0: extension.actions commands receive a single-use taskCleanup.run() only from a trusted user menu invocation with legacyTaskCleanup permission. One-time maintenance capability, not a general plugin API. Host freezes eligible targets and revisions; run requires a confirm(preview, signal) callback bound to the original command and only accepts true before Core validates each target and CAS. Plugins own all preview, confirmation, progress and result UI. No View cleanup API, arbitrary task/file/SQLite access, timers or background initiation. Cancellation before confirmation creates no jobs; accepted Core jobs continue after plugin shutdown.' }, + 'taskCleanup': { title: { 'zh-CN': '旧版已移除任务清理', 'en-US': 'Legacy removed task cleanup' }, scope: 'application', description: 'Lexora >=0.22.0: desktop-scoped extension.actions commands receive a single-use taskCleanup.run() only from a trusted user menu invocation with legacyTaskCleanup permission. One-time maintenance capability, not a general plugin API. Host freezes eligible targets and revisions; run requires a confirm(preview, signal) callback bound to the original command and only accepts true before Core validates each target and CAS. Plugins own all preview, confirmation, progress and result UI. No View cleanup API, arbitrary task/file/SQLite access, timers or background initiation. Cancellation before confirmation creates no jobs; accepted Core jobs continue after plugin shutdown.' }, 'inference': { title: { 'zh-CN': '后台模型推理', 'en-US': 'Background model inference' }, scope: 'application', description: 'contributes.inference provides chat, speech, image reading, generation and editing through declared Linux x64 native runtimes. Requires permissions.inference and a background entry; GPU and inference network origins have separate permissions. Plugins own optional resource preparation and management pages; the host owns task routing, cancellation and credential storage. No arbitrary host file access. See inference.md.' }, 'shells': { title: { 'zh-CN': '本机命令解释器', 'en-US': 'Local command interpreters' }, scope: 'application', description: 'API 4 contributes.shells declares fixed candidate IDs and native enabled/executable/priority setting keys. Requires permissions.shells. Host context.shells.detect() takes no arguments and reads bounded detection results, including unavailable candidates. Only user-enabled candidates reach core command execution under existing permissions; no plugin spawn, command input, configuration writes, installers, WSL or PTY. Defaults must leave candidates disabled, paths empty and priorities zero. See shells.md.' }, 'clipboard': { title: { 'zh-CN': '系统剪贴板', 'en-US': 'System clipboard' }, scope: 'application', description: 'Interactive ViewContext.clipboard.read()/write({text?,html?}) require independent clipboardRead/clipboardWrite manifest permissions and a visible focused view in the foreground window. Total UTF-8 content is limited to 4 MiB. Text and HTML writes are atomic. This is installation authorization plus focus, not per-call user activation. Background hosts, Workers, decorations, slots and controls are excluded; browser clipboard permissions remain denied. See views.md.' }, diff --git a/apps/buddy/src/app/bootstrap/DesktopAppProvider.vue b/apps/buddy/src/app/bootstrap/DesktopAppProvider.vue index 5d8507bf7..36c707a51 100644 --- a/apps/buddy/src/app/bootstrap/DesktopAppProvider.vue +++ b/apps/buddy/src/app/bootstrap/DesktopAppProvider.vue @@ -20,6 +20,7 @@ import { DesktopSkillReviewHost } from '@/modules/skills/ui' import { provideExecutionEnvironment, sessionReferenceNavigationKey, useProvideTaskEnvironment, useTaskIndex, useTaskResourcePanel } from '@/modules/tasks' import { useProvideDesktopUpdates } from '@/modules/updates' import { DesktopUpdateHost } from '@/modules/updates/ui' +import { BrowserGuestCatalog } from '@/platform/browser/BrowserGuestCatalog' import DesktopBrowserGuestHost from '@/platform/browser/DesktopBrowserGuestHost.vue' import { useBrowserGuestHost } from '@/platform/browser/useBrowserGuestHost' import { requireDesktopApi } from '@/platform/desktop/desktopApi' @@ -62,6 +63,8 @@ const emit = defineEmits<{ defineSlots<{ default: (props: { shell: DesktopShellBindings }) => unknown }>() const desktopTheme = useDesktopTheme() const api = requireDesktopApi() +const guestCatalog = new BrowserGuestCatalog(api.browser) +onScopeDispose(() => guestCatalog.dispose()) const router = useRouter() const message = useMessage() const appState = useDesktopAppState({ api }) @@ -160,6 +163,7 @@ useProvideExtensionContext({ data: createWorkbenchDataPort({ api: api.localChat, onScopeDispose(workbench.controller.subscribe(() => void nextTick(extensionViews.layout))) const selectedTask = workbench.activeTask const resources = useTaskResourcePanel({ + guestCatalog, scopeSpaceIds: useTaskResourceSpaces({ controller: workbench.controller, pool: workbench.pool, tasks: taskIndex.index.tasks }), activeConversationId: workbench.activeTaskId, activeDraftId: computed(() => selectedTask.value?.workspace.composer.draftId.value ?? null), @@ -447,7 +451,7 @@ watch(() => stores.applicationSettings.config.value?.desktop.language, (language diff --git a/apps/buddy/src/app/workbench/TaskWorkspacePool.ts b/apps/buddy/src/app/workbench/TaskWorkspacePool.ts index 0fe580c79..915bb5b2e 100644 --- a/apps/buddy/src/app/workbench/TaskWorkspacePool.ts +++ b/apps/buddy/src/app/workbench/TaskWorkspacePool.ts @@ -1,31 +1,41 @@ import type { EffectScope } from 'vue' +import type { TaskWorkspaceResource } from './taskResource' import type { TaskCapability, UseTaskCapabilityOptions } from '@/modules/tasks' import type { ResourceRef } from '@/workbench/common/workbench' import { Emitter } from '@buddy-shared/events/Emitter' import { copyEventSnapshot } from '@buddy-shared/events/eventSnapshot' -import { effectScope } from 'vue' -import { createUserQuestionInteractionState, useTaskCapability } from '@/modules/tasks' +import { effectScope, watch } from 'vue' +import { createUserQuestionInteractionState } from '@/modules/tasks' import { resourceKey } from '@/workbench/common/workbench' +import { parseTaskResource, taskResource } from './taskResource' + +export type TaskWorkspaceCreationOptions = Pick + +export interface TaskWorkspacePoolOptions { + conversations: Pick + index: Pick + createTask: (options: TaskWorkspaceCreationOptions) => TaskCapability +} export interface TaskWorkspaceChange { readonly kind: 'ready' | 'released' | 'adopted' - readonly resource: ResourceRef - readonly previous?: ResourceRef + readonly resource: TaskWorkspaceResource + readonly previous?: TaskWorkspaceResource } export class TaskWorkspacePool { readonly #questionInteraction = createUserQuestionInteractionState() readonly #changes = new Emitter(() => console.error('TASK_WORKSPACE_OBSERVER_FAILED')) readonly onDidChange = this.#changes.event - readonly #options: Omit - readonly #entries = new Map() + readonly #options: TaskWorkspacePoolOptions + readonly #entries = new Map() readonly #leases = new Map() readonly #loading = new Map>() - readonly #adopt: (previous: ResourceRef, task: TaskCapability, id: string) => void + readonly #adopt: (previous: TaskWorkspaceResource, task: TaskCapability, resource: TaskWorkspaceResource) => void #disposed = false #retained: Set | null = null - constructor(options: Omit, adopt: (previous: ResourceRef, task: TaskCapability, id: string) => void, readonly prepareLayout: () => Promise) { + constructor(options: TaskWorkspacePoolOptions, adopt: (previous: TaskWorkspaceResource, task: TaskCapability, resource: TaskWorkspaceResource) => void, readonly prepareLayout: () => Promise) { this.#options = options this.#adopt = adopt } @@ -36,15 +46,17 @@ export class TaskWorkspacePool { } open(resource: ResourceRef): Promise { - resource = copyEventSnapshot(resource) - const key = resourceKey(resource) + const target = parseTaskResource(copyEventSnapshot(resource)) + if (!target) + return Promise.reject(new Error('TASK_UNAVAILABLE')) + const key = resourceKey(target) const pending = this.#loading.get(key) if (pending) return pending const existing = this.#entries.get(key) if (existing) return Promise.resolve(existing.task) - const open: Promise = Promise.resolve().then(() => this.#load(resource, () => this.#loading.get(key) === open)).finally(() => { + const open: Promise = Promise.resolve().then(() => this.#load(target, () => this.#loading.get(key) === open)).finally(() => { if (this.#loading.get(key) === open) this.#loading.delete(key) }) @@ -52,47 +64,84 @@ export class TaskWorkspacePool { return open } - async #load(resource: ResourceRef, current: () => boolean): Promise { + async #load(resource: TaskWorkspaceResource, current: () => boolean): Promise { if (resource.scheme === 'draft') await this.prepareLayout() - const conversation = resource.scheme === 'task' ? await this.#options.api.localChat.conversations.get(resource.id) : null + const conversation = resource.scheme === 'task' ? await this.#options.conversations.get(resource.id) : null if (!current() || (resource.scheme === 'task' && !conversation) || this.#disposed || conversation?.deletedAt || (this.#retained && !this.#retained.has(resourceKey(resource)))) throw new Error('TASK_UNAVAILABLE') + const requestedBranchId = resource.scheme === 'task' ? resource.data.branchId : undefined + const branchId = requestedBranchId ?? conversation?.activeBranchId ?? null + if (conversation && requestedBranchId !== undefined) { + const branches = await this.#options.conversations.listBranches(conversation.id) + if (!branches.some(branch => branch.id === branchId && branch.conversationId === conversation.id)) + throw new Error('TASK_BRANCH_UNAVAILABLE') + } if (conversation) { - await this.#options.index.data.ensureConversation(conversation.id) - this.#options.index.data.applyConversation(conversation) + await this.#options.index.ensureConversation(conversation.id) + this.#options.index.applyConversation(conversation) } if (!current() || this.#disposed || (this.#retained && !this.#retained.has(resourceKey(resource)))) throw new Error('TASK_UNAVAILABLE') + const selectedResource = conversation && branchId + ? taskResource(conversation.id, branchId) + : resource + const selectedKey = resourceKey(selectedResource) + const existing = this.#entries.get(selectedKey) + if (existing) { + const shared = await (this.#loading.get(selectedKey) ?? Promise.resolve(existing.task)) + if (!current() || this.#disposed || !existing.ready || this.#entries.get(selectedKey)?.task !== shared) + throw new Error('TASK_UNAVAILABLE') + this.#adoptResource(resource, shared, selectedResource) + return shared + } const scope = effectScope(true) let currentResource = resource - const task = scope.run(() => useTaskCapability({ - ...this.#options, + const task = scope.run(() => this.#options.createTask({ questionInteraction: this.#questionInteraction, initialConversation: conversation, initialTarget: { draftKey: resource.scheme === 'draft' ? resource.id : undefined, conversationId: conversation?.id ?? null, - branchId: conversation?.activeBranchId ?? null, - spaceId: conversation?.spaceId ?? (typeof resource.data.spaceId === 'string' ? resource.data.spaceId : null), - }, - onDraftCommitted: (draftId, id) => { - const previous = currentResource - currentResource = { scheme: 'task', id, data: {} } - this.#entries.delete(resourceKey(previous)) - this.#entries.set(resourceKey(currentResource), { task, scope, ready: true }) - this.#options.onDraftCommitted?.(draftId, id) - this.#adopt(previous, task, id) - this.#changes.fire(Object.freeze({ kind: 'adopted', resource: currentResource, previous })) + branchId, + spaceId: conversation?.spaceId ?? (resource.scheme === 'draft' ? resource.data.spaceId : null), }, }))! - const entry = { task, scope, ready: false } + const entry = { task, scope, ready: false, resource } this.#entries.set(resourceKey(resource), entry) try { await task.initialize() - if (this.#disposed || this.#entries.get(resourceKey(currentResource))?.task !== task || task.workspace.restoration.state.value !== 'ready') + if (this.#disposed || this.#entries.get(resourceKey(currentResource))?.task !== task || !['ready', 'conflict'].includes(task.workspace.restoration.state.value)) throw new Error('TASK_RESTORATION_FAILED') + const concurrent = this.#entries.get(selectedKey) + if (concurrent && concurrent !== entry) { + const shared = await (this.#loading.get(selectedKey) ?? Promise.resolve(concurrent.task)) + if (!current() || this.#disposed || !concurrent.ready || this.#entries.get(selectedKey)?.task !== shared) + throw new Error('TASK_UNAVAILABLE') + this.#entries.delete(resourceKey(currentResource)) + task.dispose() + scope.stop() + this.#adoptResource(resource, shared, selectedResource) + return shared + } entry.ready = true + const adoptSelection = () => { + const id = task.workspace.session.activeConversationId.value + const selectedBranch = task.workspace.session.activeBranchId.value + if (!id || !selectedBranch || this.#disposed) + return + const next = taskResource(id, selectedBranch) + if (resourceKey(currentResource) === resourceKey(next)) + return + const previous = currentResource + currentResource = next + entry.resource = next + this.#entries.delete(resourceKey(previous)) + this.#entries.set(resourceKey(next), entry) + this.#adoptResource(previous, task, next) + } + scope.run(() => watch([task.workspace.session.activeConversationId, task.workspace.session.activeBranchId], adoptSelection)) + adoptSelection() this.#changes.fire(Object.freeze({ kind: 'ready', resource: currentResource })) return task } @@ -105,6 +154,15 @@ export class TaskWorkspacePool { } } + #adoptResource(previous: TaskWorkspaceResource, task: TaskCapability, next: TaskWorkspaceResource): void { + if (resourceKey(previous) === resourceKey(next)) + return + if (this.#retained?.delete(resourceKey(previous))) + this.#retained.add(resourceKey(next)) + this.#adopt(previous, task, next) + this.#changes.fire(Object.freeze({ kind: 'adopted', resource: next, previous })) + } + async flush(): Promise { await Promise.allSettled(this.#loading.values()) const results = await Promise.all([...this.#entries.values()].map(entry => entry.task.flushDrafts())) @@ -157,8 +215,7 @@ export class TaskWorkspacePool { } catch (error) { failures.push(error) } } - const [scheme, id] = JSON.parse(key) as [string, string] - this.#changes.fire(Object.freeze({ kind: 'released', resource: copyEventSnapshot({ scheme, id, data: {} }) })) + this.#changes.fire(Object.freeze({ kind: 'released', resource: copyEventSnapshot(entry.resource) })) } if (failures.length) throw new AggregateError(failures, 'TASK_WORKSPACE_RELEASE_FAILED') diff --git a/apps/buddy/src/app/workbench/__tests__/TaskInputCleanup.spec.ts b/apps/buddy/src/app/workbench/__tests__/TaskInputCleanup.spec.ts index f08e8114e..437786c9d 100644 --- a/apps/buddy/src/app/workbench/__tests__/TaskInputCleanup.spec.ts +++ b/apps/buddy/src/app/workbench/__tests__/TaskInputCleanup.spec.ts @@ -107,7 +107,17 @@ describe('task input cleanup', () => { await restoreTaskInputViews(restoredLayout, { find: async id => id === 'sent' ? record : null }) workbench.restoreLayout(restoredLayout) expect(workbench.layout.views[discarded]).toBeUndefined() - expect(workbench.layout.views[sent]?.resource).toEqual({ scheme: 'task', id: 'task', data: {} }) + expect(workbench.layout.views[sent]?.resource).toEqual({ scheme: 'task', id: 'task', data: { branchId: 'branch' } }) expect(workbench.layout.root).toMatchObject({ kind: 'pane', view: sent }) }) + + it('keeps legacy global and space drafts available for the subsequent migration', async () => { + const workbench = controller() + const global = (await workbench.open({ scheme: 'draft', id: 'global', data: {} }, 'Global'))! + const space = (await workbench.open({ scheme: 'draft', id: 'space', data: { spaceId: 'space' } }, 'Space', { direction: 'right' }))! + const restoredLayout = restoreWorkbenchLayout(workbench.layout) + await restoreTaskInputViews(restoredLayout, { find: async () => null }) + expect(restoredLayout.views[global]?.resource).toEqual({ scheme: 'draft', id: 'global', data: {} }) + expect(restoredLayout.views[space]?.resource).toEqual({ scheme: 'draft', id: 'space', data: { spaceId: 'space' } }) + }) }) diff --git a/apps/buddy/src/app/workbench/__tests__/desktopSelectionReferences.spec.ts b/apps/buddy/src/app/workbench/__tests__/desktopSelectionReferences.spec.ts index f52643a1f..034958aaf 100644 --- a/apps/buddy/src/app/workbench/__tests__/desktopSelectionReferences.spec.ts +++ b/apps/buddy/src/app/workbench/__tests__/desktopSelectionReferences.spec.ts @@ -40,7 +40,7 @@ async function fixture() { status: { isClosing: shallowRef(false) }, execution: { isSending: shallowRef(false), isMutatingBranch: shallowRef(false) }, session: { currentTitle: shallowRef('Same title'), activeConversationId: shallowRef(id) }, - composer: { draftId: drafts.draftId, editorKey: drafts.editorKey, composerContent: drafts.composerContent, updateComposerContent: drafts.updateComposerContent, target: shallowRef({ kind: 'conversation', conversationId: id, branchId: 'branch' }) }, + composer: { content: drafts.content, draftId: drafts.draftId, editorKey: drafts.editorKey, composerContent: drafts.composerContent, updateComposerContent: drafts.updateComposerContent, target: shallowRef({ kind: 'conversation', conversationId: id, branchId: 'branch' }) }, } } as unknown as TaskCapability } const tasks = new Map([['a', task('a', draftsA)], ['b', task('b', draftsB)]]) @@ -59,10 +59,9 @@ async function fixture() { const reveal = vi.fn(async () => true) const selectionScope = shallowRef('task:a') const resources = { allTabs, tabs: visibleTabs, activeTab, browserStates, selectTab, selectionScope, isOpen: shallowRef(true), findArtifact: () => selectionArtifact, openArtifact: async () => activeTab.value = artifactTab, setArtifactViewMode: (_id: string, mode: 'preview' | 'source') => artifactTab.viewMode = mode } as unknown as TaskResourcePanel - let ready = true const locateElement = vi.fn(async () => true) const browser = { locateElement } as unknown as DesktopBrowserApi - const host = scope.run(() => createDesktopSelectionReferences({ controller, pool, language: shallowRef('zh-CN'), resources: () => resources, independent: () => independent, ready: () => ready, locateFile, openFile, browser, navigationVersion: () => version }))! + const host = scope.run(() => createDesktopSelectionReferences({ controller, pool, language: shallowRef('zh-CN'), resources: () => resources, independent: () => independent, ready: () => true, locateFile, openFile, browser, navigationVersion: () => version }))! host.registerReveal(fileId, reveal) host.registerReveal(artifactTab.id, reveal) cleanups.push(() => { @@ -70,7 +69,7 @@ async function fixture() { void controller.dispose() }) const quote = { id: 'q', text: 'Frozen file text', source: { kind: 'file' as const, title: 'auth.ts', file, format: 'source' as const } } - return { a, b, fileId, controller, host, quote, resources, selectionScope, browserStates, draftsA, draftsB, tasks, keyA, locateFile, openFile, locateElement, artifactTab, browserTab, allTabs, visibleTabs, activeTab, selectTab, reveal, navigate: () => version++, independent: () => independent = true, unavailable: () => ready = false, stop: () => scope.stop() } + return { a, b, fileId, controller, host, quote, resources, selectionScope, browserStates, draftsA, draftsB, tasks, keyA, locateFile, openFile, locateElement, artifactTab, browserTab, allTabs, visibleTabs, activeTab, selectTab, reveal, navigate: () => version++, independent: () => independent = true } } describe('desktop resource quote adapter', () => { @@ -95,37 +94,20 @@ describe('desktop resource quote adapter', () => { expect(f.host.capture(f.fileId, f.quote)?.defaultId).toBeNull() }) - it.each(['version', 'path', 'mode', 'scope', 'close', 'switch', 'independent'])('rejects an artifact operation after a %s source change', async (change) => { + it.each(['version', 'scope', 'close'])('rejects an artifact operation after a %s source change', async (change) => { const f = await fixture() f.activeTab.value = { id: f.artifactTab.id } const request = f.host.capture(f.artifactTab.id, artifactQuote)! if (change === 'version') f.artifactTab.artifact = { ...selectionArtifact, updatedAt: '2026-10-03T00:00:01.000Z' } - if (change === 'path') - f.artifactTab.artifact = { ...selectionArtifact, path: 'C:\\fixtures\\renamed.md' } - if (change === 'mode') - f.artifactTab.viewMode = 'source' if (change === 'scope') f.artifactTab.scope = 'task:b' if (change === 'close') f.allTabs.value = f.allTabs.value.filter(tab => tab.id !== f.artifactTab.id) - if (change === 'switch') - f.activeTab.value = { id: 'browser-a' } - if (change === 'independent') - f.independent() expect(f.host.add(request, f.a)).toBe('unavailable') expect(chatComposerDocumentToUserContent(f.draftsA.composerContent.value).resourceQuotes).toBeUndefined() }) - it('does not capture binary or directory artifact placeholders', async () => { - const f = await fixture() - f.activeTab.value = { id: f.artifactTab.id } - for (const artifact of [{ ...selectionArtifact, mimeType: 'application/pdf', name: 'report.pdf' }, { ...selectionArtifact, kind: 'directory' as const }]) { - f.artifactTab.artifact = artifact - expect(f.host.capture(f.artifactTab.id, artifactQuote)).toBeNull() - } - }) - it('reopens artifact sources, selects the recorded representation and reports actual highlight failure', async () => { const f = await fixture() f.visibleTabs.value = [] @@ -197,16 +179,6 @@ describe('desktop resource quote adapter', () => { expect(chatComposerDocumentToUserContent(f.draftsB.composerContent.value).resourceQuotes).toBeUndefined() }) - it('uses the actual layout for split status, not the writable target count', async () => { - const f = await fixture() - f.tasks.get('b')!.workspace.execution.isSending = shallowRef(true) - const request = f.host.capture(f.fileId, f.quote)! - expect(request.targets).toHaveLength(1) - expect(request.isSplit).toBe(true) - await f.controller.close(f.b) - expect(f.host.capture(f.fileId, f.quote)?.isSplit).toBe(false) - }) - it('invalidates a target after branch rebinding', async () => { const f = await fixture() const request = f.host.capture(f.fileId, f.quote)! @@ -222,7 +194,7 @@ describe('desktop resource quote adapter', () => { expect(f.host.add(request, f.a)).toBe('unavailable') }) - it.each(['scope', 'tab', 'independent', 'ready', 'dispose'] as const)('rejects an in-flight file open after a %s boundary changes', async (change) => { + it.each(['scope', 'tab'] as const)('rejects an in-flight file open after a %s boundary changes', async (change) => { const f = await fixture() const lookup = Promise.withResolvers() f.locateFile.mockReturnValueOnce(lookup.promise) @@ -231,19 +203,13 @@ describe('desktop resource quote adapter', () => { f.selectionScope.value = 'task:b' if (change === 'tab') f.activeTab.value = { id: 'other-tab' } - if (change === 'independent') - f.independent() - if (change === 'ready') - f.unavailable() - if (change === 'dispose') - f.stop() lookup.resolve() expect(await pending).toBe(false) expect(f.openFile).not.toHaveBeenCalled() expect(f.reveal).not.toHaveBeenCalled() }) - it.each(['version', 'mode', 'path'] as const)('fences file reveal after its source %s changes', async (change) => { + it('fences file reveal after its source revision changes', async () => { const f = await fixture() const deferred = Promise.withResolvers() let current!: () => boolean @@ -254,10 +220,7 @@ describe('desktop resource quote adapter', () => { const pending = f.host.locate(f.quote) await vi.waitFor(() => expect(current).toBeDefined()) const view = f.controller.layout.views[f.fileId]! - if (change === 'mode') - f.controller.updateView(f.fileId, { state: { ...view.state, mode: 'preview' } }) - else - f.controller.updateView(f.fileId, { resource: { ...view.resource, data: { ...view.resource.data, [change === 'version' ? 'revision' : 'path']: change === 'version' ? 2 : 'other.ts' } } }) + f.controller.updateView(f.fileId, { resource: { ...view.resource, data: { ...view.resource.data, revision: 2 } } }) expect(current()).toBe(false) deferred.resolve(true) expect(await pending).toBe(false) @@ -302,7 +265,7 @@ describe('desktop resource quote adapter', () => { expect(f.activeTab.value.id).toBe(f.artifactTab.id) }) - it.each(['document', 'closed', 'tab'] as const)('does not select a browser tab after its pending locator becomes stale: %s', async (change) => { + it.each(['document', 'tab'] as const)('does not select a browser tab after its pending locator becomes stale: %s', async (change) => { const f = await fixture() f.visibleTabs.value = [f.browserTab, f.artifactTab] const deferred = Promise.withResolvers() @@ -310,8 +273,6 @@ describe('desktop resource quote adapter', () => { const pending = f.host.locate(browserQuote) if (change === 'document') f.browserStates.value['browser-a'] = { ...f.browserStates.value['browser-a'], documentVersion: 2 } - if (change === 'closed') - f.allTabs.value = f.allTabs.value.filter(tab => tab.id !== 'browser-a') if (change === 'tab') f.activeTab.value = { id: f.artifactTab.id } deferred.resolve(true) diff --git a/apps/buddy/src/app/workbench/__tests__/instantTaskSwitch.spec.ts b/apps/buddy/src/app/workbench/__tests__/instantTaskSwitch.spec.ts index 9e466825f..d572aac18 100644 --- a/apps/buddy/src/app/workbench/__tests__/instantTaskSwitch.spec.ts +++ b/apps/buddy/src/app/workbench/__tests__/instantTaskSwitch.spec.ts @@ -1,5 +1,7 @@ import type { LexoraDesktopApi } from '@buddy-electron/shared/desktopApi' -import type { TaskCapability, UseTaskCapabilityOptions } from '@/modules/tasks' +import type { ShallowRef } from 'vue' +import type { TaskWorkspaceCreationOptions } from '../TaskWorkspacePool' +import type { TaskCapability } from '@/modules/tasks' import type { TaskResourcePanel } from '@/modules/tasks/contracts' import type { WorkbenchPersistence } from '@/workbench/services/WorkbenchPersistence' import type { WorkingCopyService } from '@/workbench/services/WorkingCopyService' @@ -10,6 +12,7 @@ import { createMemoryHistory, createRouter } from 'vue-router' import { useDesktopNavigation } from '@/app/bootstrap/useDesktopNavigation' import { DESKTOP_ROUTE_NAMES } from '@/shared/navigation/desktopRoutes' import { ViewRendererRegistry } from '@/workbench/browser/ViewRendererRegistry' +import { resourceKey } from '@/workbench/common/workbench' import { ContributionRegistry } from '@/workbench/services/ContributionRegistry' import { WorkbenchController } from '@/workbench/services/WorkbenchController' import { ActiveTaskProjection } from '../ActiveTaskProjection' @@ -19,8 +22,6 @@ import { useTaskInputLifecycle } from '../useTaskInputLifecycle' vi.mock('../DesktopTaskContribution.vue', () => ({ default: {} })) vi.mock('../DesktopFileContribution.vue', () => ({ default: {} })) -const factory = vi.hoisted(() => ({ create: (_options: unknown): unknown => null })) -vi.mock('@/modules/tasks', async importOriginal => ({ ...await importOriginal(), useTaskCapability: (options: unknown) => factory.create(options) })) const cleanups: Array<() => void | Promise> = [] afterEach(async () => { for (const cleanup of cleanups.splice(0).reverse()) await cleanup() @@ -31,12 +32,15 @@ async function fixture() { let delayed = false let failed = false let canClose = true + let restoredState: 'ready' | 'conflict' | 'failed' = 'ready' + const restorationStates = new Map>() + const disposed = new Set() const initialized = new Set() const errors: unknown[] = [] - factory.create = (raw) => { - const options = raw as UseTaskCapabilityOptions + const createTask = (options: TaskWorkspaceCreationOptions): TaskCapability => { const id = options.initialTarget.conversationId! - const state = shallowRef('ready') + const state = shallowRef(id === 'b' ? restoredState : 'ready') + restorationStates.set(id, state) return { initialize: async () => { initialized.add(id) @@ -45,15 +49,15 @@ async function fixture() { if (id === 'b' && failed) throw new Error('TASK_READ_FAILED') }, - dispose: () => {}, + dispose: () => { disposed.add(id) }, prepareClose: async () => canClose, cancelClose: () => {}, - session: { activeTaskId: shallowRef(id), spaceId: shallowRef(null) }, - workspace: { restoration: { state }, session: { activeConversationId: shallowRef(id) } }, + session: { activeTaskId: shallowRef(id), spaceId: shallowRef(null), currentTitle: shallowRef(id) }, + workspace: { restoration: { state }, session: { activeConversationId: shallowRef(id), activeBranchId: shallowRef(options.initialTarget.branchId) } }, } as unknown as TaskCapability } const api = { localChat: { - conversations: { get: async (id: string) => ({ id, activeBranchId: `${id}-branch`, spaceId: null, deletedAt: null }) }, + conversations: { get: async (id: string) => ({ id, activeBranchId: `${id}-branch`, spaceId: null, deletedAt: null }), listBranches: async (id: string) => [`${id}-branch`, `${id}-fork`].map(branchId => ({ id: branchId, conversationId: id })) }, composerDrafts: {}, } } as unknown as LexoraDesktopApi const index = { data: { @@ -61,7 +65,6 @@ async function fixture() { applyConversation: () => {}, ensureConversation: async () => {}, } } - const pool = new TaskWorkspacePool({ api, index } as unknown as Omit, () => {}, async () => {}) let inputs: ReturnType const controller = new WorkbenchController(new ContributionRegistry(), async (view) => { try { @@ -73,6 +76,12 @@ async function fixture() { } }) registerDesktopContributions(controller, new ViewRendererRegistry(), {} as WorkingCopyService, () => 'en-US') + const pool = new TaskWorkspacePool({ conversations: api.localChat.conversations, index: index.data, createTask }, (previous, _task, resource) => { + for (const view of Object.values(controller.layout.views)) { + if (resourceKey(view.resource) === resourceKey(previous)) + controller.updateView(view.id, { resource }) + } + }, async () => {}) inputs = useTaskInputLifecycle({ api, controller, pool, persistence: {} as WorkbenchPersistence, resources: () => ({}) as TaskResourcePanel, onError: error => errors.push(error) }) const projection = new ActiveTaskProjection(controller, pool) const loads = new Map>() @@ -95,7 +104,9 @@ async function fixture() { controller.registry.dispose() pool.dispose() }) - return { controller, pool, projection, gate, initialized, errors, loads, open, first, delay: () => { + return { api, controller, pool, projection, gate, initialized, errors, loads, open, first, disposed, restorationStates, restoreAs: (state: typeof restoredState) => { + restoredState = state + }, delay: () => { delayed = true }, fail: () => { failed = true @@ -105,6 +116,71 @@ async function fixture() { } describe('instant task switching', () => { + it('keeps two branch views independent and pins legacy task layouts to the locally opened branch', async () => { + const f = await fixture() + expect(f.controller.layout.views[f.first]?.resource).toEqual({ scheme: 'task', id: 'a', data: { branchId: 'a-branch' } }) + const original = f.pool.peek({ scheme: 'task', id: 'a', data: { branchId: 'a-branch' } })! + const resource = { scheme: 'task', id: 'a', data: { branchId: 'a-fork' } } + const view = (await f.controller.open(resource, 'a fork', { direction: 'right' }))! + await f.loads.get(view) + const fork = f.pool.peek(resource)! + expect(fork).not.toBe(original) + expect(original.workspace.session.activeBranchId.value).toBe('a-branch') + expect(fork.workspace.session.activeBranchId.value).toBe('a-fork') + expect(await f.pool.open(resource)).toBe(fork) + f.pool.retain([...f.controller.renderedViews.map(view => view.resource), { scheme: 'task', id: 'a', data: { branchId: 'missing' } }]) + await expect(f.pool.open({ scheme: 'task', id: 'a', data: { branchId: 'missing' } })).rejects.toThrow('TASK_BRANCH_UNAVAILABLE') + expect(f.pool.peek(resource)).toBe(fork) + }) + + it('restores legacy and explicit views of the same branch without replacing its capability', async () => { + const f = await fixture() + f.delay() + const legacy = { scheme: 'task', id: 'b', data: {} } + const pinned = { scheme: 'task', id: 'b', data: { branchId: 'b-branch' } } + f.pool.retain([...f.controller.renderedViews.map(view => view.resource), legacy, pinned]) + const oldView = f.pool.open(legacy) + const branchView = f.pool.open(pinned) + await vi.waitFor(() => expect(f.initialized.has('b')).toBe(true)) + f.gate.resolve() + const [oldTask, branchTask] = await Promise.all([oldView, branchView]) + expect(oldTask).toBe(branchTask) + expect(f.pool.peek(pinned)).toBe(branchTask) + expect(oldTask.workspace.session.activeBranchId.value).toBe('b-branch') + f.pool.retain([...f.controller.renderedViews.map(view => view.resource), legacy, pinned]) + expect(await f.pool.open(legacy)).toBe(branchTask) + }) + + it('retains a conflicted branch view for recovery and activates it when its draft is resolved', async () => { + const f = await fixture() + f.restoreAs('conflict') + const resource = { scheme: 'task', id: 'b', data: { branchId: 'b-fork' } } + const view = (await f.controller.open(resource, 'b fork'))! + const task = await f.loads.get(view) + expect(task).not.toBeNull() + expect(task?.workspace.session.activeBranchId.value).toBe('b-fork') + expect(task?.workspace.restoration.state.value).toBe('conflict') + expect(f.pool.peek(resource)).toBe(task) + expect(f.disposed.has('b')).toBe(false) + expect(f.projection.taskId.value).toBe('b') + expect(f.projection.current.value).toBeNull() + + f.restorationStates.get('b')!.value = 'ready' + expect(f.projection.current.value).toBe(task) + expect(await f.pool.open(resource)).toBe(task) + expect(f.controller.layout.views[view]?.resource).toEqual(resource) + }) + + it('rejects an unavailable initial draft instead of treating it as a recoverable conflict', async () => { + const f = await fixture() + f.restoreAs('failed') + const view = (await f.open('b'))! + expect(await f.loads.get(view)).toBeNull() + expect(f.pool.peek(f.controller.layout.views[view]!.resource)).toBeUndefined() + expect(f.disposed.has('b')).toBe(true) + expect(f.projection.current.value).toBeNull() + }) + it('selects the target immediately and supersedes its load without allowing the late result to replace the next task', async () => { const f = await fixture() f.delay() @@ -184,6 +260,6 @@ describe('instant task switching', () => { f.gate.resolve() await opening expect(branch.value).toBe('notified-branch') - expect(navigation.notificationTarget.value).toEqual({ conversationId: 'b', messageId: 'target-message' }) + expect(navigation.notificationTarget.value).toEqual({ conversationId: 'b', branchId: 'notified-branch', messageId: 'target-message' }) }) }) diff --git a/apps/buddy/src/app/workbench/__tests__/prepareFileClose.spec.ts b/apps/buddy/src/app/workbench/__tests__/prepareFileClose.spec.ts new file mode 100644 index 000000000..5d02463d8 --- /dev/null +++ b/apps/buddy/src/app/workbench/__tests__/prepareFileClose.spec.ts @@ -0,0 +1,61 @@ +import type { ViewClosePlan } from '@/workbench/services/WorkbenchController' +import type { WorkingCopyProvider } from '@/workbench/services/WorkingCopyService' +import { deferred } from '@buddy-tests/deferred' +import { describe, expect, it, onTestFinished } from 'vitest' +import { ContributionRegistry } from '@/workbench/services/ContributionRegistry' +import { WorkbenchController } from '@/workbench/services/WorkbenchController' +import { WorkbenchPersistence } from '@/workbench/services/WorkbenchPersistence' +import { WorkingCopyService } from '@/workbench/services/WorkingCopyService' +import { createFileClosePreparation } from '../prepareFileClose' + +const resource = { scheme: 'file', id: 'document', data: {} } +async function fixture(save: WorkingCopyProvider['save'] = async (_, document) => ({ status: 'saved', document: { ...document, etag: 'saved' } })) { + const registry = new ContributionRegistry() + registry.register('files', scope => scope.view({ locations: ['main'], id: 'file', renderer: 'file', label: 'File', supports: () => true, multiple: true })) + const controller = new WorkbenchController(registry) + const copies = new WorkingCopyService({ read: async () => ({ text: 'disk', etag: 'original' }), save }) + const persistence = new WorkbenchPersistence({ read: async () => null, write: async () => {} }, controller, copies, () => {}) + await persistence.restore() + const id = (await controller.open(resource, 'Document'))! + await copies.open(resource) + copies.edit(resource, 'unsaved') + onTestFinished(async () => { + await persistence.dispose() + await copies.dispose() + await controller.dispose() + }) + return { controller, copies, persistence, view: controller.layout.views[id]! } +} + +describe('file close preparation', () => { + it('keeps later edits when a previously accepted discard becomes stale', async () => { + const f = await fixture() + const prepare = createFileClosePreparation({ ...f, onError: () => {}, confirm: async (_, actions) => { + actions.discard() + return true + } }) + const plan = await prepare(f.view) as ViewClosePlan + expect(plan.validate?.()).toBe(true) + expect(f.copies.get(resource)?.text).toBe('unsaved') + f.copies.edit(resource, 'newer') + expect(plan.validate?.()).toBe(false) + await expect(plan.complete?.({ revision: 1 })).rejects.toThrow('WORKING_COPY_CHANGED_DURING_CLOSE') + expect(f.copies.get(resource)?.text).toBe('newer') + expect(f.copies.dirty(resource)).toBe(true) + }) + + it('does not accept a save that leaves a newer working copy dirty', async () => { + const write = deferred>>() + const f = await fixture(() => write.promise) + const prepare = createFileClosePreparation({ ...f, onError: () => {}, confirm: async (_, actions) => { + const saving = actions.save() + f.copies.edit(resource, 'newer') + write.resolve({ status: 'saved', document: { text: 'unsaved', etag: 'saved' } }) + expect(await saving).toBe(false) + return false + } }) + expect(await prepare(f.view)).toBe(false) + expect(f.copies.get(resource)?.text).toBe('newer') + expect(f.copies.dirty(resource)).toBe(true) + }) +}) diff --git a/apps/buddy/src/app/workbench/__tests__/registerResourceContributions.spec.ts b/apps/buddy/src/app/workbench/__tests__/registerResourceContributions.spec.ts deleted file mode 100644 index 0718a15d3..000000000 --- a/apps/buddy/src/app/workbench/__tests__/registerResourceContributions.spec.ts +++ /dev/null @@ -1,70 +0,0 @@ -import type { LocalSpace } from '@buddy-shared/spaces/spaceApi' -import { expect, it, onTestFinished } from 'vitest' -import { effectScope, onScopeDispose, shallowRef } from 'vue' -import { contextPanelFixture } from '@/modules/tasks/state/context-panel/__tests__/contextPanelFixture' -import { useTaskResourcePanel } from '@/modules/tasks/state/context-panel/useTaskResourcePanel' -import { useWorkbenchLaunchers } from '@/workbench/browser/useWorkbenchLaunchers' -import { ContributionRegistry } from '@/workbench/services/ContributionRegistry' -import { WorkbenchController } from '@/workbench/services/WorkbenchController' -import { registerResourceContributions } from '../registerResourceContributions' - -function createResourcePanel(overrides: Parameters[0] = {}) { - const { scope, options } = contextPanelFixture(overrides) - return scope.run(() => useTaskResourcePanel({ - ...options, - autoCollapse: shallowRef(false), - linkOpening: shallowRef('in-app'), - files: { resolve: async () => { throw new Error('Unexpected file resolution') } }, - openFile: async () => { throw new Error('Unexpected file opening') }, - browser: { - onStateChanged: () => () => {}, - onGuestsChanged: () => () => {}, - listGuests: async () => [], - close: async () => {}, - ensureSession: async () => { throw new Error('Unexpected browser session') }, - openFile: async () => { throw new Error('Unexpected file link') }, - openArtifact: async () => { throw new Error('Unexpected browser artifact') }, - navigate: async () => { throw new Error('Unexpected browser navigation') }, - }, - }))! -} - -function setup(panel: Parameters[1]) { - const registry = new ContributionRegistry() - const scope = effectScope() - onTestFinished(() => scope.stop()) - const controller = new WorkbenchController(registry) - scope.run(() => onScopeDispose(registerResourceContributions(registry, panel, () => 'en-US'))) - const launchers = scope.run(() => useWorkbenchLaunchers(controller))! - return { controller, ...launchers } -} - -it('opens a single root and asks before choosing among roots', async () => { - const now = '2026-09-10T00:00:00.000Z' - const space: LocalSpace = { - id: 'space', - name: 'Files', - icon: 'folder', - iconColor: 'default', - memoryScope: 'space_only', - activeRunCount: 0, - additionalDirectories: [], - revokedAt: null, - createdAt: now, - updatedAt: now, - primaryDirectory: { id: 'directory', spaceId: 'space', root: '/workspace', canonicalRoot: '/workspace', revision: 1, accessGrantedAt: now, resourcesTrustedAt: now, revokedAt: null, createdAt: now, updatedAt: now }, - } - const spaces = shallowRef([space]) - const activeSpace = shallowRef(space) - const panel = createResourcePanel({ spaces, activeSpace }) - const f = setup(() => panel) - await f.launch('files') - expect(panel.activeTab.value).toMatchObject({ kind: 'files', target: { spaceId: 'space', directoryId: 'directory', path: '' } }) - expect(panel.fileSpacePickerOpen.value).toBe(false) - const additional = { ...space.primaryDirectory!, id: 'additional', root: '/extra', canonicalRoot: '/extra' } - spaces.value = [{ ...space, additionalDirectories: [additional] }] - activeSpace.value = spaces.value[0]! - await f.controller.commands.execute('resource.files', { source: 'palette' }) - expect(panel.fileSpacePickerOpen.value).toBe(true) - expect(panel.allTabs.value).toHaveLength(1) -}) diff --git a/apps/buddy/src/app/workbench/__tests__/taskResource.spec.ts b/apps/buddy/src/app/workbench/__tests__/taskResource.spec.ts new file mode 100644 index 000000000..fa0fad5a5 --- /dev/null +++ b/apps/buddy/src/app/workbench/__tests__/taskResource.spec.ts @@ -0,0 +1,17 @@ +import { describe, expect, it } from 'vitest' +import { resourceKey } from '@/workbench/common/workbench' +import { draftResource, isLegacyDraftResource, parseTaskResource } from '../taskResource' + +describe('task resource identity', () => { + it('normalizes old optional metadata while retaining additional JSON data and resource identity', () => { + const oldTask = { scheme: 'task', id: 'task', data: { branchId: null, custom: 'kept' } } + const oldDraft = { scheme: 'draft', id: 'draft', data: { custom: 'kept' } } + expect(parseTaskResource(oldTask)).toEqual({ scheme: 'task', id: 'task', data: { custom: 'kept' } }) + expect(parseTaskResource(oldDraft)).toEqual({ scheme: 'draft', id: 'draft', data: { spaceId: null, custom: 'kept' } }) + expect(resourceKey(parseTaskResource(oldTask)!)).toBe(resourceKey(oldTask)) + expect(resourceKey(parseTaskResource(oldDraft)!)).toBe(resourceKey(oldDraft)) + expect(isLegacyDraftResource(draftResource('global'))).toBe(true) + expect(isLegacyDraftResource(draftResource('space', 'space'))).toBe(true) + expect(isLegacyDraftResource(draftResource('draft', 'space'))).toBe(false) + }) +}) diff --git a/apps/buddy/src/app/workbench/createDesktopSelectionReferences.ts b/apps/buddy/src/app/workbench/createDesktopSelectionReferences.ts index d7e2afbc4..a680fce7d 100644 --- a/apps/buddy/src/app/workbench/createDesktopSelectionReferences.ts +++ b/apps/buddy/src/app/workbench/createDesktopSelectionReferences.ts @@ -6,7 +6,7 @@ import type { SelectionReferenceTarget } from '@/shared/ui/selection/workbenchSe import { buddyUserContentToText } from '@buddy-shared/conversation/buddyUserContent' import { getCurrentScope, onScopeDispose } from 'vue' import { translateBuddy } from '@/i18n/buddyI18n' -import { chatComposerDocumentToUserContent, userContentToChatComposerDocument } from '@/modules/prompt-input' +import { userContentToChatComposerDocument } from '@/modules/prompt-input' import { WorkbenchSelectionReferences } from '@/shared/ui/selection/workbenchSelectionReferences' import { panes } from '@/workbench/common/workbench' import { registerBuiltinSelectionReferenceProviders } from './builtinSelectionReferenceProviders' @@ -44,7 +44,7 @@ export function createDesktopSelectionReferences(options: BuiltinSelectionRefere scope: workspace.session.activeConversationId.value ? `task:${workspace.session.activeConversationId.value}` : `draft:${workspace.composer.draftId.value}`, label: translateBuddy(options.language.value, 'desktop.chat.quoteTargetPane', { index: index + 1, title }), pane: { index: index + 1, title }, - read: () => chatComposerDocumentToUserContent(workspace.composer.composerContent.value), + read: () => workspace.composer.content.value, write: content => workspace.composer.updateComposerContent(buddyUserContentToText(content), userContentToChatComposerDocument(content)), }] }) diff --git a/apps/buddy/src/app/workbench/prepareFileClose.ts b/apps/buddy/src/app/workbench/prepareFileClose.ts new file mode 100644 index 000000000..4eb1fcf9e --- /dev/null +++ b/apps/buddy/src/app/workbench/prepareFileClose.ts @@ -0,0 +1,60 @@ +import type { WorkbenchView } from '@/workbench/common/workbench' +import type { ViewCloseDecision, WorkbenchController } from '@/workbench/services/WorkbenchController' +import type { WorkbenchPersistence } from '@/workbench/services/WorkbenchPersistence' +import type { WorkingCopyService } from '@/workbench/services/WorkingCopyService' +import { resourceKey } from '@/workbench/common/workbench' + +export function createFileClosePreparation(options: { + controller: WorkbenchController + copies: WorkingCopyService + persistence: WorkbenchPersistence + confirm: (title: string, actions: { discard: () => void, save: () => Promise }) => Promise + onError: (error: unknown) => void +}) { + const { controller, copies, persistence } = options + return async (view: WorkbenchView, closing: ReadonlySet = new Set()): Promise => { + const first = [...closing].find(id => resourceKey(controller.layout.views[id]?.resource ?? { scheme: '', id: '', data: {} }) === resourceKey(view.resource)) + if (first && first !== view.id) + return true + if (Object.values(controller.layout.views).some(other => other.id !== view.id && !closing.has(other.id) && resourceKey(other.resource) === resourceKey(view.resource))) + return true + const current = copies.get(view.resource) + if (!current) + return true + if (!current.dirty) + return { validate: () => copies.isCurrent(current) && !copies.dirty(view.resource) } + let decision: ViewCloseDecision = false + const accepted = await options.confirm(view.title, { + discard() { + const accepted = copies.get(view.resource) + decision = accepted + ? { + validate: () => copies.isCurrent(accepted), + complete: async ({ revision }) => { + if (!copies.isCurrent(accepted)) + throw new Error('WORKING_COPY_CHANGED_DURING_CLOSE') + copies.discard(view.resource) + await persistence.flushThrough({ layoutRevision: revision, backupRevision: persistence.backups.revision }) + }, + } + : true + }, + async save() { + try { + const result = await copies.save(view.resource) + if ((result.status !== 'saved' && result.status !== 'unchanged') || result.dirtyAfter) + return false + const saved = copies.get(view.resource)! + await persistence.flushThrough(persistence.capture()) + decision = { validate: () => copies.isCurrent(saved) && !copies.dirty(view.resource) } + return true + } + catch (error) { + options.onError(error) + return false + } + }, + }) + return accepted ? decision : false + } +} diff --git a/apps/buddy/src/app/workbench/restoreTaskInputViews.ts b/apps/buddy/src/app/workbench/restoreTaskInputViews.ts index a2db53155..4104edeb1 100644 --- a/apps/buddy/src/app/workbench/restoreTaskInputViews.ts +++ b/apps/buddy/src/app/workbench/restoreTaskInputViews.ts @@ -1,16 +1,18 @@ import type { LocalChatApi } from '@buddy-electron/shared/localChatApi' import type { WorkbenchLayout } from '@/workbench/common/workbench' import { createPane, panes, removePane } from '@/workbench/common/workbench' +import { isLegacyDraftResource, parseTaskResource, taskResource } from './taskResource' export async function restoreTaskInputViews(layout: WorkbenchLayout, api: Pick): Promise { for (const view of Object.values(layout.views)) { - if (view.resource.scheme !== 'draft' || view.resource.id === 'global' || view.resource.id === view.resource.data.spaceId) + const resource = parseTaskResource(view.resource) + if (!resource || resource.scheme !== 'draft' || isLegacyDraftResource(resource)) continue const draft = await api.find(view.resource.id) if (draft?.scope.kind === 'task') continue if (draft && 'conversationId' in draft.scope) { - view.resource = { scheme: 'task', id: draft.scope.conversationId, data: {} } + view.resource = taskResource(draft.scope.conversationId, 'branchId' in draft.scope ? draft.scope.branchId : undefined) continue } if (draft) diff --git a/apps/buddy/src/app/workbench/taskResource.ts b/apps/buddy/src/app/workbench/taskResource.ts new file mode 100644 index 000000000..553af736d --- /dev/null +++ b/apps/buddy/src/app/workbench/taskResource.ts @@ -0,0 +1,36 @@ +import type { ResourceRef } from '@/workbench/common/workbench' + +export interface TaskResource extends ResourceRef { + readonly scheme: 'task' + readonly data: ResourceRef['data'] & { readonly branchId?: string } +} + +export interface DraftResource extends ResourceRef { + readonly scheme: 'draft' + readonly data: ResourceRef['data'] & { readonly spaceId: string | null } +} + +export type TaskWorkspaceResource = TaskResource | DraftResource + +export function taskResource(id: string, branchId?: string | null): TaskResource { + return { scheme: 'task', id, data: typeof branchId === 'string' ? { branchId } : {} } +} + +export function draftResource(id: string, spaceId: string | null = null): DraftResource { + return { scheme: 'draft', id, data: { spaceId } } +} + +export function parseTaskResource(resource: ResourceRef): TaskWorkspaceResource | null { + if (resource.scheme === 'task') { + const { branchId, ...data } = resource.data + const target = taskResource(resource.id, typeof branchId === 'string' ? branchId : undefined) + return { ...target, data: { ...data, ...target.data } } + } + if (resource.scheme === 'draft') + return { ...draftResource(resource.id), data: { ...resource.data, spaceId: typeof resource.data.spaceId === 'string' ? resource.data.spaceId : null } } + return null +} + +export function isLegacyDraftResource(resource: TaskWorkspaceResource): boolean { + return resource.scheme === 'draft' && (resource.id === 'global' || resource.id === resource.data.spaceId) +} diff --git a/apps/buddy/src/app/workbench/useDesktopWorkbench.ts b/apps/buddy/src/app/workbench/useDesktopWorkbench.ts index 4ea773fed..0e3324c11 100644 --- a/apps/buddy/src/app/workbench/useDesktopWorkbench.ts +++ b/apps/buddy/src/app/workbench/useDesktopWorkbench.ts @@ -4,6 +4,7 @@ import type { BuddyUserContentV1 } from '@buddy-shared/conversation/buddyUserCon import type { ClosePreparation } from '@buddy-shared/lifecycle/closePreparation' import type { ApplicationEvents } from '@buddy-shared/observability/ApplicationEvents' import type { SpaceFileTarget } from '@buddy-shared/spaces/spaceFileApi' +import type { ReadonlyJsonObject } from '@buddy-shared/workbench/workbenchState' import type { Ref } from 'vue' import type { Router } from 'vue-router' import type { DesktopStores } from '../bootstrap/useDesktopAppState' @@ -14,14 +15,13 @@ import type { ChatReadingPositions } from '@/modules/tasks/ui' import type { SelectionReferenceNavigate } from '@/shared/ui/selection/workbenchSelectionReferences' import type { CommandContext, DropPosition, ResourceRef, SplitDirection, WorkbenchView } from '@/workbench/common/workbench' import type { ViewCloseDecision } from '@/workbench/services/WorkbenchController' -import { buddyUserContentToText, getBuddyUserContentResourceIds, hasBuddyUserContent } from '@buddy-shared/conversation/buddyUserContent' +import { buddyUserContentToText, getBuddyUserContentResourceIds } from '@buddy-shared/conversation/buddyUserContent' import { resolveBuiltinFileOpening } from '@buddy-shared/files/fileOpening' import { isSkillAvailable } from '@buddy-shared/skills/skillApi' import { spaceFileTargetSchema } from '@buddy-shared/spaces/spaceFileApi' -import { NButton, useDialog } from 'naive-ui' -import { computed, h, onScopeDispose, shallowReactive, shallowRef } from 'vue' +import { computed, onScopeDispose, shallowReactive, shallowRef, watch } from 'vue' import { fileOpeningHandler } from '@/modules/files' -import { userContentToChatComposerDocument } from '@/modules/prompt-input' +import { useTaskCapability } from '@/modules/tasks' import { desktopRouteLocations } from '@/shared/navigation/desktopRoutes' import { TextModelPool } from '@/workbench/browser/TextModelPool' import { ViewRendererRegistry } from '@/workbench/browser/ViewRendererRegistry' @@ -35,17 +35,21 @@ import { ActiveTaskProjection } from './ActiveTaskProjection' import { ContextTabProjection } from './ContextTabProjection' import { createDesktopSelectionReferences } from './createDesktopSelectionReferences' import { absolute, contains, FileOperationService } from './FileOperationService' +import { createFileClosePreparation } from './prepareFileClose' import { registerDesktopContributions } from './registerDesktopContributions' import { restoreTaskInputViews } from './restoreTaskInputViews' +import { draftResource, isLegacyDraftResource, parseTaskResource, taskResource } from './taskResource' import { TaskWorkspacePool } from './TaskWorkspacePool' import { useTaskInputLifecycle } from './useTaskInputLifecycle' +import { useTaskNavigation } from './useTaskNavigation' +import { useWorkbenchDialogs } from './useWorkbenchDialogs' import { WorkbenchDiagnostics } from './WorkbenchDiagnostics' import { WorkbenchResourceLifetime } from './WorkbenchResourceLifetime' export function useDesktopWorkbench(options: { beforeExtensionClose?: (viewId: string, signal?: AbortSignal) => Promise, fileOpening?: ReturnType, buddyCatalog?: Readonly>, api: LexoraDesktopApi, events: ApplicationEvents, stores: DesktopStores, taskIndex: TaskIndexController, router: Router, resources: () => TaskResourcePanel, onError: (error: unknown) => void }) { const { api, stores, router } = options - const dialog = useDialog() const language = stores.applicationSettings.language + const dialogs = useWorkbenchDialogs(language) const presentation = computed(() => workbenchLabels(language.value)) const labels = () => presentation.value const readingPositions: ChatReadingPositions = new Map() @@ -62,18 +66,33 @@ export function useDesktopWorkbench(options: { beforeExtensionClose?: (viewId: s const renderers = new ViewRendererRegistry() registerDesktopContributions(controller, renderers, copies, () => language.value) const persistence = new WorkbenchPersistence(api.workbench, controller, copies, error => backupError.value = error !== null) - const pool = new TaskWorkspacePool({ - buddyCatalog: options.buddyCatalog, - api, - index: options.taskIndex, - applicationSettings: stores.applicationSettings, - modelProviders: stores.modelProviders, - runtimeSupervisor: stores.runtimeSupervisor, - onDraftCommitted: (draftId, id) => options.resources().adoptDraft(draftId, id), - }, (previous, task, id) => { + const pool: TaskWorkspacePool = new TaskWorkspacePool({ + conversations: api.localChat.conversations, + index: options.taskIndex.data, + createTask: initial => useTaskCapability({ + ...initial, + buddyCatalog: options.buddyCatalog, + api, + index: options.taskIndex, + applicationSettings: stores.applicationSettings, + modelProviders: stores.modelProviders, + runtimeSupervisor: stores.runtimeSupervisor, + onDraftCommitted: (draftId, id) => options.resources().adoptDraft(draftId, id), + openBranch: async (id, branchId, target): Promise => { + await openTask(id, { branchId, ...(target?.beside ? { direction: 'right' as const } : {}) }) + const resource = taskResource(id, branchId) + const task = pool.peek(resource) + const viewId = controller.pane(controller.layout.activePane)?.view + const selected = viewId ? controller.layout.views[viewId]?.resource : undefined + if (!task || !selected || resourceKey(selected) !== resourceKey(resource)) + return false + return target?.editMessageId ? task.workspace.execution.editUserMessage(target.editMessageId) : true + }, + }), + }, (previous, task, resource) => { for (const view of Object.values(controller.layout.views)) { if (resourceKey(view.resource) === resourceKey(previous)) - controller.updateView(view.id, { resource: { scheme: 'task', id, data: {} }, title: task.session.currentTitle.value }) + controller.updateView(view.id, { resource, title: task.session.currentTitle.value }) } void options.taskIndex.index.refresh() }, () => persistence.flush()) @@ -85,12 +104,19 @@ export function useDesktopWorkbench(options: { beforeExtensionClose?: (viewId: s resources: options.resources, onError: options.onError, }) - const confirmedDraftCloses = new Set() const deletedTasks = new Set() const initialized = shallowRef(false) const activity = new ActiveTaskProjection(controller, pool) const activeTask = activity.current const activeTaskId = activity.taskId + function localBranches(): ReadonlyJsonObject { + const value = controller.layout.auxiliary.taskBranches + return value && typeof value === 'object' && !Array.isArray(value) ? value as ReadonlyJsonObject : {} + } + watch(() => [activeTask.value?.workspace.session.activeConversationId.value, activeTask.value?.workspace.session.activeBranchId.value] as const, ([id, branchId]) => { + if (id && branchId && localBranches()[id] !== branchId) + controller.setAuxiliary('taskBranches', { ...localBranches(), [id]: branchId }) + }, { flush: 'sync' }) const contextTabs = new ContextTabProjection(controller, options.resources, () => initialized.value) const resourceLifetime = new WorkbenchResourceLifetime(controller, pool, copies, () => initialized.value) const projections = { reconcile: () => { @@ -104,17 +130,7 @@ export function useDesktopWorkbench(options: { beforeExtensionClose?: (viewId: s copies, controller, flush: () => persistence.flush(), - confirmSave: paths => new Promise((resolve) => { - let settled = false - const finish = (value: boolean) => { - if (!settled) { - settled = true - resolve(value) - } - } - const chinese = language.value === 'zh-CN' - dialog.warning({ title: chinese ? '先保存打开的文件' : 'Save open files first', content: `${chinese ? '这些文件有未保存的修改:' : 'These files have unsaved changes:'}\n${[...new Set(paths)].join('\n')}`, positiveText: chinese ? '保存并继续' : 'Save and continue', negativeText: chinese ? '取消' : 'Cancel', onPositiveClick: () => finish(true), onNegativeClick: () => finish(false), onClose: () => finish(false), onMaskClick: () => finish(false), onEsc: () => finish(false) }) - }), + confirmSave: dialogs.confirmSaveFiles, updateSelection: (source, destination) => { for (const tab of options.resources().allTabs.value) { if (tab.kind !== 'files') @@ -146,29 +162,36 @@ export function useDesktopWorkbench(options: { beforeExtensionClose?: (viewId: s } }, 200)) }) - let navigationVersion = 0 - let routeVersion = 0 - onScopeDispose(router.beforeEach((to) => { - if (to.path !== '/tasks') { - navigationVersion += 1 - routeVersion += 1 - controller.cancelNavigation() - } - })) + const navigation = useTaskNavigation({ api, controller, pool, router, center, chooseReplacement: dialogs.promptUnsentDraftAction, confirmDiscard: dialogs.promptUnsentDraftCloseAction, onError: options.onError }) + const { activateTask } = navigation + const beforeFileClose = createFileClosePreparation({ controller, copies, persistence, confirm: dialogs.confirmFileClose, onError: options.onError }) function center(): string { return controller.pane(controller.layout.activePane)?.id ?? panes(controller.layout.root)[0]!.id } - function openTask(id: string, destination: { signal?: AbortSignal, direction?: SplitDirection } = {}): Promise { - return activateTask({ scheme: 'task', id, data: {} }, options.taskIndex.index.tasks.value.find(task => task.id === id)?.title ?? labels().tasks, destination) + function openTask(id: string, destination: { signal?: AbortSignal, direction?: SplitDirection, branchId?: string } = {}): Promise { + const { branchId, ...placement } = destination + const selectedViewId = controller.pane(controller.layout.activePane)?.view + const selected = selectedViewId ? controller.layout.views[selectedViewId]?.resource : undefined + const selectedResource = selected ? parseTaskResource(selected) : null + const previous = selectedResource?.scheme === 'task' && selectedResource.id === id + ? selectedResource + : Object.values(controller.layout.views).map(view => parseTaskResource(view.resource)).find(resource => resource?.scheme === 'task' && resource.id === id) + const rememberedBranch = localBranches()[id] + const localResource = taskResource(id, typeof rememberedBranch === 'string' ? rememberedBranch : undefined) + const resource = branchId ? taskResource(id, branchId) : previous ?? localResource + return activateTask(resource, options.taskIndex.index.tasks.value.find(task => task.id === id)?.title ?? labels().tasks, placement) + } + function openDraft(id: string): Promise { + return activateTask(draftResource(id), labels().newTask, { direction: 'right' }) } function newTask(destination: { spaceId?: string | null, paneId?: string, direction?: SplitDirection } = {}): Promise { const { spaceId = null, paneId = center(), direction } = destination - return activateTask({ scheme: 'draft', id: crypto.randomUUID(), data: { spaceId } }, labels().newTask, { paneId, direction }) + return activateTask(draftResource(crypto.randomUUID(), spaceId), labels().newTask, { paneId, direction }) } async function prepareNewTaskInput(sourceViewId: string, prepare: (viewId: string) => Promise, signal: AbortSignal): Promise { let accepted = false - const resource: ResourceRef = { scheme: 'draft', id: crypto.randomUUID(), data: { spaceId: null } } + const resource = draftResource(crypto.randomUUID()) await activateTask(resource, labels().newTask, { signal, direction: controller.owner(sourceViewId) ? 'right' : undefined, @@ -210,9 +233,9 @@ export function useDesktopWorkbench(options: { beforeExtensionClose?: (viewId: s return task && pane ? { spaceId: task.session.spaceId.value, paneId: pane.id } : null } async function startTaskWithSkill(name: string, prompt: string, spaceId: string | null = null): Promise { - const version = navigationVersion + const version = navigation.version const catalog = await api.localChat.skills.list(spaceId, true) - if (version !== navigationVersion) + if (version !== navigation.version) return const skill = catalog.skills.find(skill => skill.name === name && isSkillAvailable(skill)) if (!skill) @@ -221,203 +244,13 @@ export function useDesktopWorkbench(options: { beforeExtensionClose?: (viewId: s { type: 'prompt_directive', directive: 'skill', value: skill.name, skill: { id: skill.id, name: skill.name, revision: skill.revision } }, { type: 'text', text: ` ${prompt}` }, ] }] } - await activateTask({ scheme: 'draft', id: crypto.randomUUID(), data: { spaceId } }, labels().newTask, { initialContent: content }) - } - async function isDraftDirty(view: WorkbenchView): Promise { - if (view.resource.scheme !== 'draft') - return false - const task = pool.peek(view.resource) - if (task) { - const text = task.workspace.composer.draft.value.trim() - const resources = task.workspace.composer.resources.value.length - const quotes = ((task.workspace.composer.composerContent.value?.attrs as { quotes?: unknown[] } | undefined)?.quotes?.length ?? 0) > 0 - const sessionReferences = ((task.workspace.composer.composerContent.value?.attrs as { sessionReferences?: unknown[] } | undefined)?.sessionReferences?.length ?? 0) > 0 - const resourceQuotes = (task.workspace.composer.composerContent.value?.attrs?.resourceQuotes?.length ?? 0) > 0 - return Boolean(text || resources || quotes || resourceQuotes || sessionReferences) - } - try { - const draft = await api.localChat.composerDrafts.get(view.resource.id) - return hasBuddyUserContent(draft.content) - } - catch { - return false - } - } - - function promptUnsentDraftAction(): Promise<'split' | 'discard' | 'cancel'> { - return new Promise((resolve) => { - let settled = false - const finish = (choice: 'split' | 'discard' | 'cancel') => { - if (!settled) { - settled = true - resolve(choice) - } - } - const modal = dialog.warning({ - title: labels().unsentDraftPrompt, - content: labels().unsentDraftDetail, - closable: true, - onClose: () => finish('cancel'), - onMaskClick: () => finish('cancel'), - onEsc: () => finish('cancel'), - action: () => h('div', { style: 'display:flex;gap:8px;justify-content:flex-end;width:100%' }, [ - h(NButton, { - size: 'small', - onClick: () => { - finish('cancel') - modal.destroy() - }, - }, () => labels().cancel), - h(NButton, { - size: 'small', - type: 'error', - onClick: () => { - finish('discard') - modal.destroy() - }, - }, () => labels().discardDraft), - h(NButton, { - size: 'small', - type: 'primary', - onClick: () => { - finish('split') - modal.destroy() - }, - }, () => labels().splitOpen), - ]), - }) - }) - } - - function promptUnsentDraftCloseAction(): Promise { - return new Promise((resolve) => { - let settled = false - const finish = (confirmed: boolean) => { - if (!settled) { - settled = true - resolve(confirmed) - } - } - const modal = dialog.warning({ - title: labels().unsentDraftPrompt, - content: labels().unsentDraftCloseDetail, - closable: true, - onClose: () => finish(false), - onMaskClick: () => finish(false), - onEsc: () => finish(false), - action: () => h('div', { style: 'display:flex;gap:8px;justify-content:flex-end;width:100%' }, [ - h(NButton, { - size: 'small', - onClick: () => { - finish(false) - modal.destroy() - }, - }, () => labels().cancel), - h(NButton, { - size: 'small', - type: 'error', - onClick: () => { - finish(true) - modal.destroy() - }, - }, () => labels().discardDraft), - ]), - }) - }) - } - - async function activateTask(resource: ResourceRef, title: string, destination: { paneId?: string, direction?: SplitDirection, move?: boolean, signal?: AbortSignal, initialContent?: BuddyUserContentV1, prepareInput?: (viewId: string) => Promise } = {}) { - if (destination.signal?.aborted) - return - const targetPaneId = destination.paneId ?? center() - const targetPane = controller.pane(targetPaneId) - const currentViewId = targetPane?.view - const currentView = currentViewId ? controller.layout.views[currentViewId] : null - - const isExistingTask = Object.values(controller.layout.views).some(view => - resourceKey(view.resource) === resourceKey(resource) - && (view.location === 'main' || view.location === undefined), - ) - - if (!destination.direction && !isExistingTask && currentView && currentView.resource.scheme === 'draft') { - if (resource.scheme === 'draft' && !destination.initialContent && !destination.prepareInput) { - const currentSpaceId = (currentView.resource.data.spaceId as string | null) ?? null - const targetSpaceId = (resource.data.spaceId as string | null) ?? null - if (currentSpaceId === targetSpaceId) { - controller.focus(currentView.id) - await router.push('/tasks') - return - } - } - if (await isDraftDirty(currentView)) { - if (destination.initialContent || destination.prepareInput) - return activateTask(resource, title, { ...destination, direction: 'right' }) - const action = await promptUnsentDraftAction() - if (action === 'cancel') { - controller.focus(currentView.id) - return - } - if (action === 'split') { - return activateTask(resource, title, { ...destination, direction: 'right' }) - } - confirmedDraftCloses.add(currentView.id) - } - } - - navigationVersion += 1 - const version = routeVersion - let id: string | null = null - try { - if (!destination.prepareInput) - await router.push('/tasks') - if (destination.signal?.aborted || version !== routeVersion) - return - id = await controller.open(resource, title, { paneId: targetPaneId, ...destination }) - } - finally { - if (currentView) - confirmedDraftCloses.delete(currentView.id) - } - if (!id || (!destination.prepareInput && (destination.signal?.aborted || version !== routeVersion))) - return - let prepared = !destination.prepareInput - try { - const opened = controller.layout.views[id] - if (!opened) - return - const task = await pool.open(opened.resource) - if (!destination.signal?.aborted && controller.layout.views[id] && version === routeVersion) { - if (destination.prepareInput) { - prepared = await destination.prepareInput(id) - if (!prepared) - return - await router.push('/tasks') - } - if (destination.initialContent) { - task.workspace.composer.updateComposerContent(buddyUserContentToText(destination.initialContent), userContentToChatComposerDocument(destination.initialContent)) - await task.flushDrafts() - } - } - } - catch (error) { - if (controller.layout.views[id]) - options.onError(error) - } - finally { - if (!prepared && !isExistingTask && controller.layout.views[id]) { - const view = controller.layout.views[id]! - if (view.resource.scheme !== 'draft' || !await isDraftDirty(view)) { - await controller.close(id) - if (version === routeVersion && currentView && controller.layout.views[currentView.id]) - controller.focus(currentView.id) - } - } - } + await activateTask(draftResource(crypto.randomUUID(), spaceId), labels().newTask, { initialContent: content }) } function dropResource(resource: ResourceRef, paneId: string, position: DropPosition) { - if (!['task', 'draft'].includes(resource.scheme)) + const target = parseTaskResource(resource) + if (!target) return - return activateTask(resource, options.taskIndex.index.tasks.value.find(task => task.id === resource.id)?.title ?? labels().newTask, { paneId, direction: position === 'center' ? undefined : position, move: true }) + return activateTask(target, options.taskIndex.index.tasks.value.find(task => task.id === resource.id)?.title ?? labels().newTask, { paneId, direction: position === 'center' ? undefined : position, move: true }) } function fileView(target: SpaceFileTarget, tabId?: string) { return Object.values(controller.layout.views).find(view => ['file', 'file-preview'].includes(view.resource.scheme) @@ -515,12 +348,15 @@ export function useDesktopWorkbench(options: { beforeExtensionClose?: (viewId: s if (initialized.value) return const restored = await persistence.restore(layout => restoreTaskInputViews(layout, api.localChat.composerDrafts)) - const legacyDraftViews = Object.values(controller.layout.views).filter(view => view.resource.scheme === 'draft' && (view.resource.id === 'global' || view.resource.id === view.resource.data.spaceId)) + const legacyDraftViews = Object.values(controller.layout.views).flatMap((view) => { + const resource = parseTaskResource(view.resource) + return resource?.scheme === 'draft' && isLegacyDraftResource(resource) ? [{ view, resource }] : [] + }) if (legacyDraftViews.length) { const drafts = await api.localChat.composerDrafts.list() - for (const view of legacyDraftViews) { + for (const { view, resource } of legacyDraftViews) { const previous = drafts.find(draft => draft.scope.kind === 'task' - && draft.scope.spaceId === (view.resource.data.spaceId ?? null) + && draft.scope.spaceId === resource.data.spaceId && (buddyUserContentToText(draft.content).trim() || getBuddyUserContentResourceIds(draft.content).length)) controller.updateView(view.id, { resource: { ...view.resource, id: previous?.draftId ?? crypto.randomUUID() } }) } @@ -549,6 +385,9 @@ export function useDesktopWorkbench(options: { beforeExtensionClose?: (viewId: s } function discardTask(id: string) { + const remaining = { ...localBranches() } + delete remaining[id] + controller.setAuxiliary('taskBranches', remaining) deletedTasks.add(id) options.resources().discardConversation(id) for (const view of Object.values(controller.layout.views)) { @@ -560,14 +399,8 @@ export function useDesktopWorkbench(options: { beforeExtensionClose?: (viewId: s async function beforeClose(view: WorkbenchView, closing?: ReadonlySet, signal?: AbortSignal): Promise { if (view.resource.scheme === 'task' && deletedTasks.has(view.resource.id)) return true - if (view.resource.scheme === 'draft' && !confirmedDraftCloses.has(view.id)) { - if (await isDraftDirty(view)) { - const confirmed = await promptUnsentDraftCloseAction() - if (!confirmed) - return false - confirmedDraftCloses.add(view.id) - } - } + if (!await navigation.prepareClose(view)) + return false try { const extension = view.resource.scheme === 'extension' ? await options.beforeExtensionClose?.(view.id, signal) : undefined if (extension === false || signal?.aborted) @@ -591,86 +424,10 @@ export function useDesktopWorkbench(options: { beforeExtensionClose?: (viewId: s return false } finally { - confirmedDraftCloses.delete(view.id) + navigation.finishClose(view.id) } } - async function beforeFileClose(view: WorkbenchView, closing: ReadonlySet = new Set()): Promise { - const first = [...closing].find(id => resourceKey(controller.layout.views[id]?.resource ?? { scheme: '', id: '', data: {} }) === resourceKey(view.resource)) - if (first && first !== view.id) - return true - if (Object.values(controller.layout.views).some(other => other.id !== view.id && !closing.has(other.id) && resourceKey(other.resource) === resourceKey(view.resource))) - return true - const current = copies.get(view.resource) - if (!current) - return true - if (!current.dirty) - return { validate: () => copies.isCurrent(current) && !copies.dirty(view.resource) } - return new Promise((resolve) => { - let settled = false - const finish = (value: ViewCloseDecision) => { - if (!settled) { - settled = true - resolve(value) - } - } - const modal = dialog.warning({ - title: labels().savePrompt, - content: view.title, - onClose: () => finish(false), - onMaskClick: () => finish(false), - onEsc: () => finish(false), - action: () => h('div', { style: 'display:flex;gap:8px;justify-content:flex-end;width:100%' }, [ - h(NButton, { - size: 'small', - onClick: () => { - finish(false) - modal.destroy() - }, - }, () => labels().cancel), - h(NButton, { - size: 'small', - type: 'error', - onClick: () => { - const accepted = copies.get(view.resource) - finish(accepted - ? { - validate: () => copies.isCurrent(accepted), - complete: async ({ revision }) => { - if (!copies.isCurrent(accepted)) - throw new Error('WORKING_COPY_CHANGED_DURING_CLOSE') - copies.discard(view.resource) - await persistence.flushThrough({ layoutRevision: revision, backupRevision: persistence.backups.revision }) - }, - } - : true) - modal.destroy() - }, - }, () => labels().discard), - h(NButton, { - size: 'small', - type: 'primary', - onClick: async () => { - const result = await copies.save(view.resource) - if ((result.status === 'saved' || result.status === 'unchanged') && !result.dirtyAfter) { - const accepted = copies.get(view.resource)! - try { - await persistence.flushThrough(persistence.capture()) - finish({ validate: () => copies.isCurrent(accepted) && !copies.dirty(view.resource) }) - modal.destroy() - } - catch { - finish(false) - modal.destroy() - } - } - }, - }, () => labels().save), - ]), - }) - }) - } - onScopeDispose(controller.onDidSettleClose((result) => { if (result.committed && result.status === 'cleanup-pending') options.onError(new AggregateError(result.failures, 'Workbench cleanup pending')) @@ -718,10 +475,10 @@ export function useDesktopWorkbench(options: { beforeExtensionClose?: (viewId: s await inputs.flush().catch(options.onError) return saved } - const selectionReferences = createDesktopSelectionReferences({ controller, pool, language, openFile: (target, current) => createFile(target, undefined, current), browser: api.browser, resources: options.resources, ready: () => initialized.value, independent: () => stores.applicationSettings.config.value?.desktop.contextPanelMode === 'independent', locateFile: target => api.localChat.spaces.readFile(target), navigationVersion: () => navigationVersion }) - return { selectionReferences, api, renderers, fileOperations, fileToolbarTargets, fileView, closeContextFiles, readingPositions, discardTask, prepareTaskDeletion, activeTask, activeTaskId, backupError, controller, copies, models, pool, persistence, initialize, flush, dispose, openTask, newTask, prepareNewTaskInput, showTaskInput, showReferencePage, startTaskWithSkill, openFile, dropResource, language, get initialized() { + const selectionReferences = createDesktopSelectionReferences({ controller, pool, language, openFile: (target, current) => createFile(target, undefined, current), browser: api.browser, resources: options.resources, ready: () => initialized.value, independent: () => stores.applicationSettings.config.value?.desktop.contextPanelMode === 'independent', locateFile: target => api.localChat.spaces.readFile(target), navigationVersion: () => navigation.version }) + return { selectionReferences, api, renderers, fileOperations, fileToolbarTargets, fileView, closeContextFiles, readingPositions, discardTask, prepareTaskDeletion, activeTask, activeTaskId, backupError, controller, copies, models, pool, persistence, initialize, flush, dispose, openTask, openDraft, newTask, prepareNewTaskInput, showTaskInput, showReferencePage, startTaskWithSkill, openFile, dropResource, language, get initialized() { return initialized.value }, get navigationVersion() { - return navigationVersion + return navigation.version } } } diff --git a/apps/buddy/src/app/workbench/useTaskNavigation.ts b/apps/buddy/src/app/workbench/useTaskNavigation.ts new file mode 100644 index 000000000..803f8c770 --- /dev/null +++ b/apps/buddy/src/app/workbench/useTaskNavigation.ts @@ -0,0 +1,152 @@ +import type { LexoraDesktopApi } from '@buddy-electron/shared/desktopApi' +import type { BuddyUserContentV1 } from '@buddy-shared/conversation/buddyUserContent' +import type { Router } from 'vue-router' +import type { TaskWorkspaceResource } from './taskResource' +import type { TaskWorkspacePool } from './TaskWorkspacePool' +import type { SplitDirection, WorkbenchView } from '@/workbench/common/workbench' +import type { WorkbenchController } from '@/workbench/services/WorkbenchController' +import { buddyUserContentToText, hasBuddyUserContent } from '@buddy-shared/conversation/buddyUserContent' +import { onScopeDispose } from 'vue' +import { userContentToChatComposerDocument } from '@/modules/prompt-input' +import { resourceKey } from '@/workbench/common/workbench' +import { parseTaskResource } from './taskResource' + +export function useTaskNavigation(options: { + api: LexoraDesktopApi + controller: WorkbenchController + pool: TaskWorkspacePool + router: Router + center: () => string + chooseReplacement: () => Promise<'split' | 'discard' | 'cancel'> + confirmDiscard: () => Promise + onError: (error: unknown) => void +}) { + const { api, controller, pool, router, onError } = options + const confirmedDraftCloses = new Set() + let navigationVersion = 0 + let routeVersion = 0 + onScopeDispose(router.beforeEach((to) => { + if (to.path !== '/tasks') { + navigationVersion += 1 + routeVersion += 1 + controller.cancelNavigation() + } + })) + async function isDraftDirty(view: WorkbenchView): Promise { + if (view.resource.scheme !== 'draft') + return false + const task = pool.peek(view.resource) + if (task) + return task.workspace.composer.hasUnsentContent.value + try { + const draft = await api.localChat.composerDrafts.get(view.resource.id) + return hasBuddyUserContent(draft.content) + } + catch { + return false + } + } + + async function activateTask(resource: TaskWorkspaceResource, title: string, destination: { paneId?: string, direction?: SplitDirection, move?: boolean, signal?: AbortSignal, initialContent?: BuddyUserContentV1, prepareInput?: (viewId: string) => Promise } = {}) { + if (destination.signal?.aborted) + return + const targetPaneId = destination.paneId ?? options.center() + const targetPane = controller.pane(targetPaneId) + const currentViewId = targetPane?.view + const currentView = currentViewId ? controller.layout.views[currentViewId] : null + + const isExistingTask = Object.values(controller.layout.views).some(view => + resourceKey(view.resource) === resourceKey(resource) + && (view.location === 'main' || view.location === undefined), + ) + + const currentResource = currentView ? parseTaskResource(currentView.resource) : null + if (!destination.direction && !isExistingTask && currentView && currentResource?.scheme === 'draft') { + if (resource.scheme === 'draft' && !destination.initialContent && !destination.prepareInput) { + const currentSpaceId = currentResource.data.spaceId + const targetSpaceId = resource.data.spaceId + if (currentSpaceId === targetSpaceId) { + controller.focus(currentView.id) + await router.push('/tasks') + return + } + } + if (await isDraftDirty(currentView)) { + if (destination.initialContent || destination.prepareInput) + return activateTask(resource, title, { ...destination, direction: 'right' }) + const action = await options.chooseReplacement() + if (action === 'cancel') { + controller.focus(currentView.id) + return + } + if (action === 'split') { + return activateTask(resource, title, { ...destination, direction: 'right' }) + } + confirmedDraftCloses.add(currentView.id) + } + } + + navigationVersion += 1 + const version = routeVersion + let id: string | null = null + try { + if (!destination.prepareInput) + await router.push('/tasks') + if (destination.signal?.aborted || version !== routeVersion) + return + id = await controller.open(resource, title, { paneId: targetPaneId, ...destination }) + } + finally { + if (currentView) + confirmedDraftCloses.delete(currentView.id) + } + if (!id || (!destination.prepareInput && (destination.signal?.aborted || version !== routeVersion))) + return + let prepared = !destination.prepareInput + try { + const opened = controller.layout.views[id] + if (!opened) + return + const task = await pool.open(opened.resource) + if (!destination.signal?.aborted && controller.layout.views[id] && version === routeVersion) { + if (destination.prepareInput) { + prepared = await destination.prepareInput(id) + if (!prepared) + return + await router.push('/tasks') + } + if (destination.initialContent) { + task.workspace.composer.updateComposerContent(buddyUserContentToText(destination.initialContent), userContentToChatComposerDocument(destination.initialContent)) + await task.flushDrafts() + } + } + } + catch (error) { + if (controller.layout.views[id]) + onError(error) + } + finally { + if (!prepared && !isExistingTask && controller.layout.views[id]) { + const view = controller.layout.views[id]! + if (view.resource.scheme !== 'draft' || !await isDraftDirty(view)) { + await controller.close(id) + if (version === routeVersion && currentView && controller.layout.views[currentView.id]) + controller.focus(currentView.id) + } + } + } + } + + async function prepareClose(view: WorkbenchView): Promise { + if (view.resource.scheme !== 'draft' || confirmedDraftCloses.has(view.id) || !await isDraftDirty(view)) + return true + if (!await options.confirmDiscard()) + return false + confirmedDraftCloses.add(view.id) + return true + } + + return { activateTask, prepareClose, finishClose: (id: string) => confirmedDraftCloses.delete(id), get version() { + return navigationVersion + } } +} diff --git a/apps/buddy/src/app/workbench/useTaskResourceSpaces.ts b/apps/buddy/src/app/workbench/useTaskResourceSpaces.ts index def18281b..9fae7c2ea 100644 --- a/apps/buddy/src/app/workbench/useTaskResourceSpaces.ts +++ b/apps/buddy/src/app/workbench/useTaskResourceSpaces.ts @@ -3,6 +3,7 @@ import type { Ref } from 'vue' import type { TaskWorkspacePool } from './TaskWorkspacePool' import type { WorkbenchController } from '@/workbench/services/WorkbenchController' import { computed, onScopeDispose, shallowRef } from 'vue' +import { parseTaskResource } from './taskResource' export function useTaskResourceSpaces(options: { controller: WorkbenchController @@ -12,9 +13,10 @@ export function useTaskResourceSpaces(options: { const { controller, pool } = options const openTasks = shallowRef(collect()) function collect() { - return Object.values(controller.layout.views) - .filter(view => view.resource.scheme === 'task' || view.resource.scheme === 'draft') - .map(({ resource }) => ({ resource, task: pool.peek(resource) })) + return Object.values(controller.layout.views).flatMap((view) => { + const resource = parseTaskResource(view.resource) + return resource ? [{ resource, task: pool.peek(resource) }] : [] + }) } function reconcile() { openTasks.value = collect() @@ -33,7 +35,7 @@ export function useTaskResourceSpaces(options: { if (task) spaces.set(key, task.session.spaceId.value) else if (resource.scheme === 'draft') - spaces.set(key, typeof resource.data.spaceId === 'string' ? resource.data.spaceId : null) + spaces.set(key, resource.data.spaceId) } return spaces }) diff --git a/apps/buddy/src/app/workbench/useWorkbenchDialogs.ts b/apps/buddy/src/app/workbench/useWorkbenchDialogs.ts new file mode 100644 index 000000000..1ba8b1eae --- /dev/null +++ b/apps/buddy/src/app/workbench/useWorkbenchDialogs.ts @@ -0,0 +1,131 @@ +import type { Ref } from 'vue' +import type { BuddyLocale } from '@/i18n/buddyI18n' +import { NButton, useDialog } from 'naive-ui' +import { h } from 'vue' +import { workbenchLabels } from '@/workbench/common/workbenchLabels' + +export function useWorkbenchDialogs(language: Readonly>) { + const dialog = useDialog() + const labels = () => workbenchLabels(language.value) + function promptUnsentDraftAction(): Promise<'split' | 'discard' | 'cancel'> { + return new Promise((resolve) => { + let settled = false + const finish = (choice: 'split' | 'discard' | 'cancel') => { + if (!settled) { + settled = true + resolve(choice) + } + } + const modal = dialog.warning({ + title: labels().unsentDraftPrompt, + content: labels().unsentDraftDetail, + closable: true, + onClose: () => finish('cancel'), + onMaskClick: () => finish('cancel'), + onEsc: () => finish('cancel'), + action: () => h('div', { style: 'display:flex;gap:8px;justify-content:flex-end;width:100%' }, [ + h(NButton, { + size: 'small', + onClick: () => { + finish('cancel') + modal.destroy() + }, + }, () => labels().cancel), + h(NButton, { + size: 'small', + type: 'error', + onClick: () => { + finish('discard') + modal.destroy() + }, + }, () => labels().discardDraft), + h(NButton, { + size: 'small', + type: 'primary', + onClick: () => { + finish('split') + modal.destroy() + }, + }, () => labels().splitOpen), + ]), + }) + }) + } + + function promptUnsentDraftCloseAction(): Promise { + return new Promise((resolve) => { + let settled = false + const finish = (confirmed: boolean) => { + if (!settled) { + settled = true + resolve(confirmed) + } + } + const modal = dialog.warning({ + title: labels().unsentDraftPrompt, + content: labels().unsentDraftCloseDetail, + closable: true, + onClose: () => finish(false), + onMaskClick: () => finish(false), + onEsc: () => finish(false), + action: () => h('div', { style: 'display:flex;gap:8px;justify-content:flex-end;width:100%' }, [ + h(NButton, { + size: 'small', + onClick: () => { + finish(false) + modal.destroy() + }, + }, () => labels().cancel), + h(NButton, { + size: 'small', + type: 'error', + onClick: () => { + finish(true) + modal.destroy() + }, + }, () => labels().discardDraft), + ]), + }) + }) + } + + function confirmSaveFiles(paths: readonly string[]): Promise { + return new Promise((resolve) => { + const chinese = language.value === 'zh-CN' + dialog.warning({ title: chinese ? '先保存打开的文件' : 'Save open files first', content: `${chinese ? '这些文件有未保存的修改:' : 'These files have unsaved changes:'}\n${[...new Set(paths)].join('\n')}`, positiveText: chinese ? '保存并继续' : 'Save and continue', negativeText: chinese ? '取消' : 'Cancel', onPositiveClick: () => resolve(true), onNegativeClick: () => resolve(false), onClose: () => resolve(false), onMaskClick: () => resolve(false), onEsc: () => resolve(false) }) + }) + } + + function confirmFileClose(title: string, actions: { discard: () => void, save: () => Promise }): Promise { + return new Promise((resolve) => { + let settled = false + const modal = dialog.warning({ + title: labels().savePrompt, + content: title, + onClose: () => finish(false), + onMaskClick: () => finish(false), + onEsc: () => finish(false), + action: () => h('div', { style: 'display:flex;gap:8px;justify-content:flex-end;width:100%' }, [ + h(NButton, { size: 'small', onClick: () => finish(false) }, () => labels().cancel), + h(NButton, { size: 'small', type: 'error', onClick: () => { + actions.discard() + finish(true) + } }, () => labels().discard), + h(NButton, { size: 'small', type: 'primary', onClick: async () => { + if (await actions.save()) + finish(true) + } }, () => labels().save), + ]), + }) + function finish(accepted: boolean) { + if (settled) + return + settled = true + resolve(accepted) + modal.destroy() + } + }) + } + + return { confirmSaveFiles, confirmFileClose, promptUnsentDraftAction, promptUnsentDraftCloseAction } +} diff --git a/apps/buddy/src/i18n/locales/en-US/approvals.ts b/apps/buddy/src/i18n/locales/en-US/approvals.ts index ea1db11a7..ff80211d4 100644 --- a/apps/buddy/src/i18n/locales/en-US/approvals.ts +++ b/apps/buddy/src/i18n/locales/en-US/approvals.ts @@ -85,6 +85,9 @@ export default { 'desktop.approval.scopeAuthorized': 'The target is inside an authorized directory', 'desktop.approval.scopeCheck': 'Directory check: {status}', 'desktop.approval.scopeReview': 'Review the operation target before deciding', + 'desktop.approval.expired': 'This approval has expired. Check the current task state.', + 'desktop.approval.incomplete': 'The operation details are incomplete. Only denial is available.', + 'desktop.approval.fullOperation': 'View complete operation', 'desktop.approval.title': 'Approval required: {operation}', 'desktop.approval.target': 'Target', 'desktop.approval.web.provider': 'Source', diff --git a/apps/buddy/src/i18n/locales/en-US/chat.ts b/apps/buddy/src/i18n/locales/en-US/chat.ts index 377c65ea2..dbb56a973 100644 --- a/apps/buddy/src/i18n/locales/en-US/chat.ts +++ b/apps/buddy/src/i18n/locales/en-US/chat.ts @@ -1,6 +1,25 @@ import type zhCN from '../zh-CN/chat' export default { + 'desktop.recovery.pending': '{count} operations need review', + 'desktop.recovery.review': 'Review', + 'desktop.recovery.title': 'Operation recovery', + 'desktop.recovery.description': 'These operations have not been confirmed to you. Check their results, open the original task, or save unaccepted input as a new draft.', + 'desktop.recovery.accepted': 'Operation accepted', + 'desktop.recovery.rejected': 'Operation was not performed', + 'desktop.recovery.not_accepted': 'No acceptance record found', + 'desktop.recovery.unknown': 'Result is unknown. Refresh later.', + 'desktop.recovery.trust_changed': 'Connection identity changed. Original input is kept.', + 'desktop.recovery.openTask': 'Open task', + 'desktop.recovery.restore': 'Save as a new draft', + 'desktop.recovery.acknowledge': 'Reviewed, remove this recovery item', + 'desktop.recovery.refresh': 'Check again', + + 'desktop.chat.commandResultUnknown': 'The operation result is being checked. Your input is kept; wait for recovery before deciding what to do next.', + 'desktop.chat.approvalAlreadyResolved': 'This operation has already been handled or has expired. Check the latest result.', + 'desktop.chat.objectChanged': 'This item has changed. Review its current state before continuing.', + 'desktop.chat.objectDeleted': 'This item has been deleted.', + 'desktop.chat.processToolQueued': 'Waiting for capacity', 'desktop.chat.processToolResourceWaiting': 'Waiting for resources', 'desktop.chat.processToolResourceTimeout': 'Resource wait timed out; command did not start', @@ -604,4 +623,11 @@ export default { 'desktop.chat.draftRestoration.resolveFailed': 'Restoration is incomplete, or the input changed while waiting. Your input is still available. Review it and retry.', 'desktop.chat.draftRestoration.keepLocal': 'Keep input for now', 'desktop.chat.draftRestoration.useSaved': 'Discard input and restore saved content', + 'desktop.chat.openBranchBeside': 'Open to the side', + 'desktop.chat.conversationBranches': 'Conversation branches', + 'desktop.chat.branchesLoading': 'Loading…', + 'desktop.chat.branchesUnavailable': 'Branches could not be loaded. Please try again.', + 'desktop.chat.originalBranch': 'Original branch', + 'desktop.chat.numberedBranch': 'Branch {index}', + 'desktop.chat.viewedBranch': 'Viewing', } satisfies Record diff --git a/apps/buddy/src/i18n/locales/en-US/settings.ts b/apps/buddy/src/i18n/locales/en-US/settings.ts index 990d94c70..e9bfed398 100644 --- a/apps/buddy/src/i18n/locales/en-US/settings.ts +++ b/apps/buddy/src/i18n/locales/en-US/settings.ts @@ -1,6 +1,44 @@ import type zhCN from '../zh-CN/settings' export default { + 'desktop.settings.devices.options': 'Connection options', + 'desktop.settings.devices.automatic': 'Automatic', + 'desktop.settings.devices.save': 'Save', + 'desktop.settings.devices.emptyReady': 'Choose Pair new device above, then scan the code with your phone.', + 'desktop.settings.devices.emptyDisabled': 'Enable mobile connections to pair your phone.', + + 'desktop.settings.devices.title': 'Mobile devices', + 'desktop.settings.devices.enable': 'Allow mobile connections', + 'desktop.settings.devices.description': 'Connect Lexora Android over your local or trusted private network. Only paired devices can access tasks.', + 'desktop.settings.devices.address': 'Computer private IP address', + 'desktop.settings.devices.addressDescription': 'Enter a local or VPN address reachable from your phone. Turn connections off before changing it.', + 'desktop.settings.devices.port': 'Connection port', + 'desktop.settings.devices.failed': 'The operation could not be completed. Check the connection status and retry.', + 'desktop.settings.devices.unavailable': 'Unable to load device connection status. Check that the service is running and retry.', + 'desktop.settings.devices.pair': 'Pair a device', + 'desktop.settings.devices.cancel': 'Cancel pairing', + 'desktop.settings.devices.reset': 'Reset connection identity', + 'desktop.settings.devices.resetConfirm': 'All paired devices will need to scan a new code. Continue?', + 'desktop.settings.devices.expires': 'Scan with Lexora Android. Expires in {seconds} seconds.', + 'desktop.settings.devices.expired': 'This code has expired. Generate a new one.', + 'desktop.settings.devices.empty': 'No paired devices', + 'desktop.settings.devices.revoke': 'Revoke access', + 'desktop.settings.devices.revokeConfirm': 'This device will disconnect immediately and must pair again to reconnect.', + 'desktop.settings.devices.state.pending': 'Awaiting confirmation', + 'desktop.settings.devices.state.active': 'Authorized', + 'desktop.settings.devices.state.revoked': 'Revoked', + 'desktop.settings.devices.state.expired': 'Expired', + 'desktop.settings.devices.error.CREDENTIAL_STORE_UNAVAILABLE': 'Unlock the system keychain and retry.', + 'desktop.settings.devices.error.TLS_IDENTITY_UNAVAILABLE': 'The connection identity is unavailable. Retry or reset the identity and pair again.', + 'desktop.settings.devices.error.ADDRESS_INVALID': 'Enter a valid private IP address.', + 'desktop.settings.devices.error.ADDRESS_IN_USE': 'This port is in use. Turn connections off and choose another port.', + 'desktop.settings.devices.error.ADDRESS_UNAVAILABLE': 'This address is unavailable on this computer. Check the network connection.', + 'desktop.settings.devices.error.LISTEN_FAILED': 'Unable to accept device connections. Check the network and retry.', + 'desktop.settings.exitBehavior': 'When exiting', + 'desktop.settings.exitBehaviorDescription': 'Keeping the service running lets tasks and mobile access continue. A full exit stops the service. Closing a window is separate.', + 'desktop.settings.exitKeepService': 'Exit desktop and keep the service running', + 'desktop.settings.exitStopService': 'Exit completely and stop the service', + 'desktop.settings.experimental': 'Experimental', 'desktop.settings.category.speech': 'Voice', 'desktop.settings.categoryDescription.speech': 'Set up voice input and read aloud.', @@ -283,6 +321,8 @@ export default { 'desktop.settings.category.general': 'General', 'desktop.settings.category.appearance': 'Appearance', 'desktop.settings.category.notifications': 'Notifications', + 'desktop.settings.category.connections': 'Connections', + 'desktop.settings.categoryDescription.connections': 'Manage device connections, pairing, and access.', 'desktop.settings.category.proxy': 'Network proxy', 'desktop.settings.category.about': 'About', 'desktop.settings.category.data': 'Runtime information', diff --git a/apps/buddy/src/i18n/locales/zh-CN/approvals.ts b/apps/buddy/src/i18n/locales/zh-CN/approvals.ts index 094b268bc..0ff3141c4 100644 --- a/apps/buddy/src/i18n/locales/zh-CN/approvals.ts +++ b/apps/buddy/src/i18n/locales/zh-CN/approvals.ts @@ -79,6 +79,9 @@ export default { 'desktop.approval.scopeAuthorized': '目标位于已授权目录内', 'desktop.approval.scopeCheck': '目录校验:{status}', 'desktop.approval.scopeReview': '请核对操作目标后再决定', + 'desktop.approval.expired': '这项审批已过期,请查看任务当前状态。', + 'desktop.approval.incomplete': '操作信息未完整提供,当前只能拒绝。', + 'desktop.approval.fullOperation': '查看完整操作', 'desktop.approval.title': '需要批准:{operation}', 'desktop.approval.target': '操作目标', 'desktop.approval.web.provider': '搜索来源', diff --git a/apps/buddy/src/i18n/locales/zh-CN/chat.ts b/apps/buddy/src/i18n/locales/zh-CN/chat.ts index 625f2d8ac..82c22b035 100644 --- a/apps/buddy/src/i18n/locales/zh-CN/chat.ts +++ b/apps/buddy/src/i18n/locales/zh-CN/chat.ts @@ -1,4 +1,23 @@ export default { + 'desktop.recovery.pending': '有 {count} 项操作需要核对', + 'desktop.recovery.review': '查看', + 'desktop.recovery.title': '操作恢复', + 'desktop.recovery.description': '这里保留了尚未向你确认的操作。核对结果后,可以查看原任务或将未受理的输入保存为新草稿。', + 'desktop.recovery.accepted': '操作已受理', + 'desktop.recovery.rejected': '操作未执行', + 'desktop.recovery.not_accepted': '未找到受理记录', + 'desktop.recovery.unknown': '结果尚未确认,请稍后刷新', + 'desktop.recovery.trust_changed': '连接身份已变化,原输入仍保留', + 'desktop.recovery.openTask': '查看任务', + 'desktop.recovery.restore': '保存为新草稿', + 'desktop.recovery.acknowledge': '已核对,移除此恢复项', + 'desktop.recovery.refresh': '重新核对', + + 'desktop.chat.commandResultUnknown': '正在核对操作结果,输入内容已保留。请等待恢复后再决定下一步。', + 'desktop.chat.approvalAlreadyResolved': '此操作已被处理或已失效,请查看最新结果。', + 'desktop.chat.objectChanged': '内容已经发生变化,请查看最新状态后再操作。', + 'desktop.chat.objectDeleted': '这个项目已被删除。', + 'desktop.chat.processToolQueued': '排队等待', 'desktop.chat.processToolResourceWaiting': '等待资源', 'desktop.chat.processToolResourceTimeout': '等待资源超时,命令未执行', @@ -602,4 +621,11 @@ export default { 'desktop.chat.draftRestoration.resolveFailed': '恢复尚未完成,或等待期间输入又发生变化。当前输入仍保留,请检查后重试。', 'desktop.chat.draftRestoration.keepLocal': '保留输入,稍后处理', 'desktop.chat.draftRestoration.useSaved': '放弃此输入并恢复已保存内容', + 'desktop.chat.openBranchBeside': '在旁边打开', + 'desktop.chat.conversationBranches': '对话分支', + 'desktop.chat.branchesLoading': '正在载入…', + 'desktop.chat.branchesUnavailable': '分支暂时无法载入,请稍后重试', + 'desktop.chat.originalBranch': '原始分支', + 'desktop.chat.numberedBranch': '分支 {index}', + 'desktop.chat.viewedBranch': '当前浏览', } as const diff --git a/apps/buddy/src/i18n/locales/zh-CN/settings.ts b/apps/buddy/src/i18n/locales/zh-CN/settings.ts index a5e053352..161095b8b 100644 --- a/apps/buddy/src/i18n/locales/zh-CN/settings.ts +++ b/apps/buddy/src/i18n/locales/zh-CN/settings.ts @@ -1,4 +1,42 @@ export default { + 'desktop.settings.devices.options': '连接选项', + 'desktop.settings.devices.automatic': '自动选择', + 'desktop.settings.devices.save': '保存', + 'desktop.settings.devices.emptyReady': '点击右上方“配对新设备”,用手机扫码连接。', + 'desktop.settings.devices.emptyDisabled': '开启移动设备连接后,即可扫码配对。', + + 'desktop.settings.devices.title': '移动设备', + 'desktop.settings.devices.enable': '允许移动设备连接', + 'desktop.settings.devices.description': '在同一局域网或可信私网中连接 Lexora Android。只有扫码授权的设备可以访问任务。', + 'desktop.settings.devices.address': '电脑的私网 IP 地址', + 'desktop.settings.devices.addressDescription': '填写手机可访问的局域网或 VPN 地址。修改地址前请先关闭连接。', + 'desktop.settings.devices.port': '连接端口', + 'desktop.settings.devices.failed': '操作未完成,请检查连接状态后重试。', + 'desktop.settings.devices.unavailable': '无法获取设备连接状态,请确认后台服务正常后重试。', + 'desktop.settings.devices.pair': '配对新设备', + 'desktop.settings.devices.cancel': '取消配对', + 'desktop.settings.devices.reset': '重置连接身份', + 'desktop.settings.devices.resetConfirm': '重置后所有已配对设备需要重新扫码连接。是否继续?', + 'desktop.settings.devices.expires': '请用 Lexora Android 扫码,{seconds} 秒后失效。', + 'desktop.settings.devices.expired': '二维码已失效,请重新生成。', + 'desktop.settings.devices.empty': '尚未配对设备', + 'desktop.settings.devices.revoke': '撤销访问', + 'desktop.settings.devices.revokeConfirm': '此设备会立即断开,重新访问需要再次配对。', + 'desktop.settings.devices.state.pending': '等待设备确认', + 'desktop.settings.devices.state.active': '已授权', + 'desktop.settings.devices.state.revoked': '已撤销', + 'desktop.settings.devices.state.expired': '授权已过期', + 'desktop.settings.devices.error.CREDENTIAL_STORE_UNAVAILABLE': '无法读取系统凭据存储,请解锁系统密钥环后重试。', + 'desktop.settings.devices.error.TLS_IDENTITY_UNAVAILABLE': '无法加载连接身份。请重试,或重置身份后重新配对。', + 'desktop.settings.devices.error.ADDRESS_INVALID': '请填写有效的私网 IP 地址。', + 'desktop.settings.devices.error.ADDRESS_IN_USE': '此端口已被使用,请关闭连接后更换端口。', + 'desktop.settings.devices.error.ADDRESS_UNAVAILABLE': '此地址当前不属于这台电脑,请检查网络连接。', + 'desktop.settings.devices.error.LISTEN_FAILED': '无法开启设备连接,请检查网络后重试。', + 'desktop.settings.exitBehavior': '退出行为', + 'desktop.settings.exitBehaviorDescription': '保留后台时,任务与移动端访问继续可用;完全退出会停止后台服务。关闭窗口不受此设置影响。', + 'desktop.settings.exitKeepService': '退出桌面,保留后台服务', + 'desktop.settings.exitStopService': '完全退出,停止后台服务', + 'desktop.settings.experimental': '实验性', 'desktop.settings.category.speech': '语音', 'desktop.settings.categoryDescription.speech': '设置语音输入与回复朗读。', @@ -281,6 +319,8 @@ export default { 'desktop.settings.category.general': '常规', 'desktop.settings.category.appearance': '外观', 'desktop.settings.category.notifications': '通知', + 'desktop.settings.category.connections': '连接', + 'desktop.settings.categoryDescription.connections': '管理设备连接、配对与访问授权。', 'desktop.settings.category.proxy': '网络代理', 'desktop.settings.category.about': '关于', 'desktop.settings.category.data': '运行信息', diff --git a/apps/buddy/src/mobile-reader/DocumentPreview.vue b/apps/buddy/src/mobile-reader/DocumentPreview.vue new file mode 100644 index 000000000..92a54cc46 --- /dev/null +++ b/apps/buddy/src/mobile-reader/DocumentPreview.vue @@ -0,0 +1,40 @@ + + + + + diff --git a/apps/buddy/src/mobile-reader/MessageAvatar.vue b/apps/buddy/src/mobile-reader/MessageAvatar.vue new file mode 100644 index 000000000..655a205b7 --- /dev/null +++ b/apps/buddy/src/mobile-reader/MessageAvatar.vue @@ -0,0 +1,39 @@ + + + + + diff --git a/apps/buddy/src/mobile-reader/MobileReader.vue b/apps/buddy/src/mobile-reader/MobileReader.vue new file mode 100644 index 000000000..2cd498a17 --- /dev/null +++ b/apps/buddy/src/mobile-reader/MobileReader.vue @@ -0,0 +1,79 @@ + + + diff --git a/apps/buddy/src/mobile-reader/ReaderMessage.vue b/apps/buddy/src/mobile-reader/ReaderMessage.vue new file mode 100644 index 000000000..d6604aedf --- /dev/null +++ b/apps/buddy/src/mobile-reader/ReaderMessage.vue @@ -0,0 +1,77 @@ + + + diff --git a/apps/buddy/src/mobile-reader/ReaderMessageActions.vue b/apps/buddy/src/mobile-reader/ReaderMessageActions.vue new file mode 100644 index 000000000..b358d763f --- /dev/null +++ b/apps/buddy/src/mobile-reader/ReaderMessageActions.vue @@ -0,0 +1,34 @@ + + + diff --git a/apps/buddy/src/mobile-reader/ReaderTerminal.vue b/apps/buddy/src/mobile-reader/ReaderTerminal.vue new file mode 100644 index 000000000..dd5bdc914 --- /dev/null +++ b/apps/buddy/src/mobile-reader/ReaderTerminal.vue @@ -0,0 +1,16 @@ + + + + + diff --git a/apps/buddy/src/mobile-reader/ReaderToolCall.vue b/apps/buddy/src/mobile-reader/ReaderToolCall.vue new file mode 100644 index 000000000..4a9e5b690 --- /dev/null +++ b/apps/buddy/src/mobile-reader/ReaderToolCall.vue @@ -0,0 +1,78 @@ + + + diff --git a/apps/buddy/src/mobile-reader/ReaderTurn.vue b/apps/buddy/src/mobile-reader/ReaderTurn.vue new file mode 100644 index 000000000..83e62b8d8 --- /dev/null +++ b/apps/buddy/src/mobile-reader/ReaderTurn.vue @@ -0,0 +1,15 @@ + + + diff --git a/apps/buddy/src/mobile-reader/ReaderTurnContent.vue b/apps/buddy/src/mobile-reader/ReaderTurnContent.vue new file mode 100644 index 000000000..def7ee58b --- /dev/null +++ b/apps/buddy/src/mobile-reader/ReaderTurnContent.vue @@ -0,0 +1,53 @@ + + + diff --git a/apps/buddy/src/mobile-reader/__tests__/readerProjection.spec.ts b/apps/buddy/src/mobile-reader/__tests__/readerProjection.spec.ts new file mode 100644 index 000000000..b76088dae --- /dev/null +++ b/apps/buddy/src/mobile-reader/__tests__/readerProjection.spec.ts @@ -0,0 +1,26 @@ +import { describe, expect, it } from 'vitest' +import { ReaderProjection } from '../readerProjection' +import { 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: {} }) + +describe('reader projection', () => { + it('retains unchanged entries, removes old order entries and isolates topics', () => { + const projection = new ReaderProjection() + const a = { id: 'a', continuation: false, messages: [] } + const b = { id: 'b', continuation: false, messages: [] } + expect(projection.apply(update('one', { order: ['a', 'b'], entries: [a, b] })).entries).toEqual([a, b]) + expect(projection.apply(update('one', { order: ['b'] })).entries).toEqual([b]) + expect(projection.apply(update('two', { order: ['a'], entries: [a] })).entries).toEqual([a]) + expect(projection.apply(update('two', { reset: true })).entries).toEqual([]) + }) + it('appends transcript deltas and replaces the stream on reset', () => { + const projection = new ReaderProjection() + projection.apply(update('one', { transcripts: { run: { events: [event(1)] } } })) + const appended = projection.apply(update('one', { transcripts: { run: { events: [event(2)], append: true } } })) + expect(appended.transcripts.run?.events.map(event => event.sequence)).toEqual([1, 2]) + expect(projection.apply(update('one', { reset: true, transcripts: { run: { events: [event(3)] } } })).transcripts.run?.events).toEqual([event(3)]) + expect(projection.apply(update('two')).transcripts).toEqual({}) + }) +}) diff --git a/apps/buddy/src/mobile-reader/bootstrap.ts b/apps/buddy/src/mobile-reader/bootstrap.ts new file mode 100644 index 000000000..a295a8636 --- /dev/null +++ b/apps/buddy/src/mobile-reader/bootstrap.ts @@ -0,0 +1,10 @@ +import { setKatexLoader, VueRendererMarkdown } from 'markstream-vue' +import { NMessageProvider } from 'naive-ui' +import { createApp, h } from 'vue' +import { markdownStrings } from './markdownStrings' +import MobileReader from './MobileReader.vue' +import 'virtual:uno.css' +import './reader.css' + +setKatexLoader(() => import('katex')) +createApp({ render: () => h(NMessageProvider, null, { default: () => h(MobileReader) }) }).use(VueRendererMarkdown, { defaultI18nMap: markdownStrings }).mount('#app') diff --git a/apps/buddy/src/mobile-reader/document-bootstrap.ts b/apps/buddy/src/mobile-reader/document-bootstrap.ts new file mode 100644 index 000000000..45bb6b85c --- /dev/null +++ b/apps/buddy/src/mobile-reader/document-bootstrap.ts @@ -0,0 +1,10 @@ +import { setKatexLoader, VueRendererMarkdown } from 'markstream-vue' +import { NMessageProvider } from 'naive-ui' +import { createApp, h } from 'vue' +import DocumentPreview from './DocumentPreview.vue' +import { markdownStrings } from './markdownStrings' +import 'virtual:uno.css' +import './reader.css' + +setKatexLoader(() => import('katex')) +createApp({ render: () => h(NMessageProvider, null, { default: () => h(DocumentPreview) }) }).use(VueRendererMarkdown, { defaultI18nMap: markdownStrings }).mount('#app') diff --git a/apps/buddy/src/mobile-reader/document.html b/apps/buddy/src/mobile-reader/document.html new file mode 100644 index 000000000..91ab587e8 --- /dev/null +++ b/apps/buddy/src/mobile-reader/document.html @@ -0,0 +1,13 @@ + + + + + + + Lexora + + +
+ + + diff --git a/apps/buddy/src/mobile-reader/document.ts b/apps/buddy/src/mobile-reader/document.ts new file mode 100644 index 000000000..f7b883888 --- /dev/null +++ b/apps/buddy/src/mobile-reader/document.ts @@ -0,0 +1,2 @@ +import './polyfills' +import './document-bootstrap' diff --git a/apps/buddy/src/mobile-reader/index.html b/apps/buddy/src/mobile-reader/index.html new file mode 100644 index 000000000..427642024 --- /dev/null +++ b/apps/buddy/src/mobile-reader/index.html @@ -0,0 +1,13 @@ + + + + + + + Lexora + + +
+ + + diff --git a/apps/buddy/src/mobile-reader/main.ts b/apps/buddy/src/mobile-reader/main.ts new file mode 100644 index 000000000..7ff43f1a1 --- /dev/null +++ b/apps/buddy/src/mobile-reader/main.ts @@ -0,0 +1,2 @@ +import './polyfills' +import './bootstrap' diff --git a/apps/buddy/src/mobile-reader/markdownStrings.ts b/apps/buddy/src/mobile-reader/markdownStrings.ts new file mode 100644 index 000000000..445d710c2 --- /dev/null +++ b/apps/buddy/src/mobile-reader/markdownStrings.ts @@ -0,0 +1,47 @@ +import { reactive } from 'vue' + +const en = { + 'common.copy': 'Copy', + 'common.selectCopy': 'Select all (copy)', + 'common.copied': 'Copied', + 'common.decrease': 'Decrease', + 'common.reset': 'Reset', + 'common.increase': 'Increase', + 'common.expand': 'Expand', + 'common.collapse': 'Collapse', + 'common.preview': 'Preview', + 'common.source': 'Source', + 'common.export': 'Export', + 'common.open': 'Open', + 'common.minimize': 'Minimize', + 'common.zoomIn': 'Zoom in', + 'common.zoomOut': 'Zoom out', + 'common.resetZoom': 'Reset zoom', + 'image.loadError': 'Image failed to load', + 'image.loading': 'Loading image…', +} +const zh: Record = { + 'common.copy': '复制', + 'common.selectCopy': '全选(复制)', + 'common.copied': '已复制', + 'common.decrease': '缩小', + 'common.reset': '重置', + 'common.increase': '放大', + 'common.expand': '展开', + 'common.collapse': '折叠', + 'common.preview': '预览', + 'common.source': '源代码', + 'common.export': '导出', + 'common.open': '打开', + 'common.minimize': '最小化', + 'common.zoomIn': '放大', + 'common.zoomOut': '缩小', + 'common.resetZoom': '重置缩放', + 'image.loadError': '图片加载失败', + 'image.loading': '正在加载图片…', +} + +export const markdownStrings = reactive({ ...zh }) +export function setMarkdownLanguage(language: string) { + Object.assign(markdownStrings, language === 'en' ? en : zh) +} diff --git a/apps/buddy/src/mobile-reader/polyfills.ts b/apps/buddy/src/mobile-reader/polyfills.ts new file mode 100644 index 000000000..5bf4e0e9f --- /dev/null +++ b/apps/buddy/src/mobile-reader/polyfills.ts @@ -0,0 +1,8 @@ +import 'core-js/actual/array/at' +import 'core-js/actual/array/to-sorted' +import 'core-js/actual/array/find-last' +import 'core-js/actual/array/find-last-index' +import 'core-js/actual/string/replace-all' +import 'core-js/actual/object/has-own' + +import 'core-js/actual/structured-clone' diff --git a/apps/buddy/src/mobile-reader/reader.css b/apps/buddy/src/mobile-reader/reader.css new file mode 100644 index 000000000..bdb670842 --- /dev/null +++ b/apps/buddy/src/mobile-reader/reader.css @@ -0,0 +1,539 @@ +html, body, #app, main { + height: 100%; + margin: 0; + width: 100%; +} +* { + box-sizing: border-box; +} +body { + font-family: system-ui, sans-serif; +} +main { + position: relative; + background: var(--buddy-reading, var(--buddy-surface-canvas)); + color: var(--buddy-reading-fg, var(--buddy-text-primary)); + font-family: var(--buddy-font-body, system-ui, sans-serif); + --reader-muted: var(--buddy-text-muted); + --reader-border: var(--buddy-border-subtle); + --reader-accent: var(--buddy-accent-text); + --reader-user: var(--buddy-user-message-surface); + --reader-user-fg: var(--buddy-text-primary); + --reader-brush: var(--buddy-state-hover); + --reader-panel: var(--buddy-surface-raised); + --buddy-chat-final-font-size: 1em; + --buddy-chat-final-heading-font-size: 1em; +} +.reader-viewport { + height: 100%; + overflow-y: auto; + overflow-x: hidden; + overflow-anchor: none; +} +.reader-content { + position: relative; + width: 100%; + min-height: 100%; +} +.reader-entry { + position: absolute; + top: 0; + left: 0; + width: 100%; + padding: 8px 16px; +} +.reader-message { + min-width: 0; + overflow-wrap: anywhere; + -webkit-tap-highlight-color: transparent; +} +.message-author { + display: flex; + align-items: center; + margin-bottom: 8px; +} +.message-author > * + * { + margin-left: 10px; +} +.author-name { + position: relative; + font-size: .8667em; + font-weight: 500; + color: var(--reader-muted); +} +.user { + display: flex; + flex-wrap: wrap; + justify-content: flex-end; + align-items: flex-start; +} +.user .message-body { + order: 1; + max-width: 92%; + width: fit-content; + padding: 8px 12px; + color: var(--reader-user-fg); + background: var(--reader-user); + border-radius: 10px; +} +.message-time { + color: var(--reader-muted); + font-size: .8em; + font-weight: 400; +} +.message-meta, small { + color: var(--reader-muted); + font-size: .8em; +} +.stream-status { + display: inline-flex; + align-items: center; + color: var(--reader-accent); + font-size: .75em; +} +.stream-status i { + width: 4px; + height: 4px; + border-radius: 50%; + background: currentColor; + margin-right: 3px; + animation: reply-pulse 1.2s ease-in-out infinite; +} +.stream-status i:nth-child(2) { + animation-delay: .15s; +} +.stream-status i:nth-child(3) { + animation-delay: .3s; +} +.stream-status span { + margin-left: 5px; +} +@keyframes reply-pulse { 0%, 80%, 100% { opacity: .35; } 40% { opacity: 1; } } +@media (prefers-reduced-motion: reduce) { .stream-status i { animation: none; } } +.plain-message { + margin: 0; + white-space: pre-wrap; + line-height: var(--buddy-chat-final-line-height); +} +.tool-text { + font-family: monospace; + font-size: .85em; + max-height: 60vh; + overflow: auto; +} +.message-error, .failed > .tool-toggle { + color: var(--buddy-status-danger-text); +} +.dark .message-error, .dark .failed > .tool-toggle { + color: var(--buddy-status-danger-text); +} +button { + font: inherit; + color: var(--reader-accent); + background: transparent; + border: 0; + padding: 10px 8px; + min-height: 48px; + cursor: pointer; + border-radius: 6px; + -webkit-tap-highlight-color: transparent; +} +button:disabled { + opacity: .6; +} +button:focus-visible, a:focus-visible { + outline: 2px solid var(--reader-accent); +} +button:active { + background: var(--reader-brush); +} +.reader-icon { + width: 20px; + height: 20px; + flex: 0 0 auto; +} +.tool-toggle { + display: flex; + align-items: center; + text-align: left; + width: 100%; + color: var(--reader-muted); + font-size: .8125em; +} +.tool-toggle > * + * { + margin-left: 10px; +} +.tool-failure { + font-size: .9em; +} +.disclosure-icon { + margin-left: auto; + width: 16px; +} +.tool .message-body { + padding: 12px 14px; + background: var(--reader-panel); + border: 1px solid var(--reader-border); + border-radius: 10px; +} +.message-resources { + display: flex; + flex-wrap: wrap; + gap: 8px; + width: 100%; + margin-bottom: 8px; +} +.user .message-resources { + order: 0; + justify-content: flex-end; +} +.resource-link { + display: flex; + align-items: center; + width: 260px; + max-width: 92%; + min-height: 48px; + padding: 8px 10px; + text-align: left; + color: var(--buddy-text-primary); + border: 1px solid var(--reader-border); + background: var(--reader-panel); + border-radius: 8px; +} +.resource-icon { + width: 18px; + height: 18px; + color: var(--reader-accent); +} +.resource-info { + min-width: 0; + margin-left: 8px; + font-size: .8667em; + line-height: 1.5; +} +.resource-info > span { + display: block; + overflow: hidden; + white-space: nowrap; + text-overflow: ellipsis; +} +.resource-link small { + display: block; + font-size: .9231em; +} +.latest-button { + position: absolute; + bottom: 8px; + left: 50%; + transform: translateX(-50%); + display: flex; + align-items: center; + justify-content: center; + width: 44px; + height: 44px; + min-height: 44px; + padding: 0; + border-radius: 50%; + background: var(--reader-panel); + color: var(--reader-muted); + border: 1px solid var(--reader-border); + box-shadow: 0 2px 8px #27233012; +} +.reader-empty { + color: var(--reader-muted); + text-align: center; +} +.markstream-vue button { + appearance: none; + font: inherit; +} +.markstream-vue a { + color: var(--reader-accent); +} +.markstream-vue table { + width: 100%; + table-layout: auto; + border-collapse: collapse; + font-size: .9em; +} +.markstream-vue th, .markstream-vue td { + white-space: normal; + overflow-wrap: anywhere; + min-width: 80px; + border: 1px solid var(--reader-border); +} +.markstream-vue pre { + max-width: 100%; + overflow-x: auto; +} +.markstream-vue .code-block-container, .markstream-vue .table-node { + box-shadow: none; +} +.markstream-vue .code-block-container { + border: 1px solid var(--reader-border); + border-radius: 10px; +} +.markstream-vue .katex-display { + overflow-x: auto; + overflow-y: hidden; + padding: 4px 0; +} +.markstream-vue { + content-visibility: visible !important; +} +@media (max-width: 340px) { + .reader-entry { + padding-left: 16px; + padding-right: 16px; + } + +} + +.message-actions { + display: flex; + align-items: center; + min-height: 48px; + gap: 0; + margin-top: 4px; + color: var(--reader-muted); +} +.user .message-actions { + order: 2; + justify-content: flex-end; + width: 100%; +} +.message-actions button { + display: inline-flex; + align-items: center; + justify-content: center; + width: 40px; + flex: 0 0 40px; + min-height: 48px; + padding: 14px 10px; + color: inherit; + border-radius: 6px; +} +.message-actions button:active { + background: var(--reader-user); +} +.message-actions .is-copied { + color: var(--buddy-status-success-text); +} +.message-actions .reader-icon { + width: 18px; + height: 18px; +} +.message-actions .message-time { + padding: 0 6px; + font-variant-numeric: tabular-nums; +} + +.reader-entry.continuation { + padding-top: 2px; + padding-bottom: 6px; +} +.intermediate .message-body { + color: var(--reader-muted); + font-size: .9375em; +} +.reader-tool { + min-width: 0; + border-left: 2px solid var(--reader-border); + padding-left: 8px; +} +.tool-call-header { + display: flex; + align-items: center; + width: 100%; + min-height: 48px; + padding: 8px 4px; + text-align: left; + color: var(--reader-muted); +} +.tool-call-icon { + width: 17px; + height: 17px; + margin-right: 8px; + align-self: flex-start; + margin-top: 3px; +} +.tool-call-heading { + display: flex; + flex-direction: column; + flex: 1 1 auto; + min-width: 0; +} +.tool-call-title { + font-size: .8125em; + line-height: 1.6; +} +.tool-call-name { + margin-left: 8px; + color: var(--reader-muted); + opacity: .7; + font-size: .85em; +} +.tool-call-summary { + display: -webkit-box; + -webkit-box-orient: vertical; + -webkit-line-clamp: 2; + overflow: hidden; + overflow-wrap: anywhere; + font-size: .75em; + line-height: 1.5; + margin-top: 3px; +} +.tool-call-status { + flex: 0 0 auto; + font-size: .6875em; + margin-left: 8px; + opacity: .7; +} +.tool-call-chevron { + width: 16px; + height: 16px; + margin-left: 4px; + transition: transform .15s ease; +} +.reader-tool.expanded .tool-call-chevron { + transform: rotate(90deg); +} +.reader-tool.failed .tool-call-status { + color: var(--buddy-status-danger-text); + opacity: 1; +} +.dark .reader-tool.failed .tool-call-status { + color: var(--buddy-status-danger-text); +} +.tool-call-detail { + margin: 4px 0 8px 4px; + min-width: 0; +} +.tool-output-toolbar { + display: flex; + align-items: center; + justify-content: space-between; + min-height: 38px; + padding-left: 12px; + font-size: .75em; + color: var(--reader-muted); + background: var(--reader-panel); + border-radius: 8px 8px 0 0; +} +.tool-output-toolbar button { + display: grid; + place-items: center; + width: 40px; + min-height: 40px; + color: inherit; + padding: 8px; +} +.tool-output-toolbar .reader-icon { + width: 16px; + height: 16px; +} +.tool-output { + margin: 0; + max-height: 50vh; + overflow: auto; + padding: 8px 12px 14px; + background: var(--reader-panel); + border-radius: 0 0 8px 8px; + font-family: var(--buddy-font-mono); + font-size: .75em; + line-height: 1.6; + tab-size: 2; + white-space: pre-wrap; + overflow-wrap: anywhere; +} +.tool-detail-action { + font-size: .8125em; +} +@media (prefers-reduced-motion: reduce) { .tool-call-chevron { transition: none; } } + +main { + --buddy-text-secondary: var(--reader-muted); + --buddy-chat-gap-turn: 10px; + --buddy-radius-micro: 6px; + --buddy-chat-gap-block: 8px; + --buddy-chat-gap-section: 12px; + --buddy-chat-activity-row-height: 40px; + --buddy-chat-activity-icon-size: 15px; + --buddy-chat-activity-row-gap: 6px; + --buddy-chat-activity-indent: 21px; + --buddy-chat-process-font-size: 13px; + --buddy-chat-process-color: var(--reader-muted); + --buddy-chat-meta-color: var(--reader-muted); + --buddy-chat-meta-font-size: 12px; + --buddy-chat-meta-line-height: 1.5; + --buddy-chat-tool-font-size: 12px; + --buddy-chat-tool-body-color: var(--reader-muted); + --buddy-chat-caption-font-size: 12px; + --buddy-chat-code-font-size: 12px; + --buddy-chat-code-line-height: 1.6; + --buddy-chat-code-color: var(--buddy-text-primary); + --buddy-chat-danger-color: var(--buddy-status-danger-text); + --buddy-terminal-background: var(--reader-panel); +} +.reader-turn-flow { + display: grid; + gap: 6px; + min-width: 0; +} +.reader-turn-flow + .reader-message { + margin-top: 12px; +} +.reader-turn .reader-narration { + font-size: .9375em; + color: var(--reader-muted); + margin-block: 6px; +} +.reader-turn button { + min-height: 40px; +} +.reader-turn .buddy-chat-activity-group__header { + max-width: 100%; +} +.reader-turn .buddy-chat-activity-row__label { + white-space: normal; +} +.reader-turn .buddy-chat-tool__summary { + font-size: 12px; +} +.reader-turn .buddy-chat-tool-details { + max-width: 100%; +} + +.reader-turn .buddy-chat-activity-row, +.reader-turn .buddy-chat-activity-status { + gap: 0; +} +.reader-turn .buddy-chat-activity-row > * + *, +.reader-turn .buddy-chat-activity-status > * + * { + margin-left: 6px; +} +.reader-turn .buddy-chat-activity-status { + flex-wrap: wrap; +} +.reader-turn .buddy-chat-tool__context { + display: none; +} +.reader-turn .buddy-chat-tool__summary { + font-family: var(--buddy-font-mono); +} + +.transcript-retry { + font-size: 12px; + color: var(--reader-muted); + padding: 6px 0; +} + +.reader-turn button:disabled { + opacity: 1; +} +.reader-turn .buddy-chat-tool-toolbar > * + * { + margin-left: 8px; +} +.reader-turn .buddy-chat-tool-toolbar > div:last-child { + margin-left: auto; +} diff --git a/apps/buddy/src/mobile-reader/readerBridge.ts b/apps/buddy/src/mobile-reader/readerBridge.ts new file mode 100644 index 000000000..51be04edf --- /dev/null +++ b/apps/buddy/src/mobile-reader/readerBridge.ts @@ -0,0 +1,99 @@ +import type { LocalRun, LocalRunEvent } from '@buddy-shared/runs/runApi' +import type { ChatAgentTurn } from '../modules/tasks/contracts' +import type { ReaderActionInput, ReaderWireEntry } from './readerProtocol' +import { useTimeoutFn } from '@vueuse/core' +import { onMounted, onUnmounted, provide, shallowRef } from 'vue' +import { setMarkdownLanguage } from './markdownStrings' +import { ReaderProjection } from './readerProjection' +import { readerActionSchema, readerCopiedSchema, readerUpdateSchema } from './readerProtocol' +import { readerLanguageKey } from './readerStrings' +import { readerAvatarsKey } from './useReaderAvatars' +import { readerCopyFeedbackKey } from './useReaderCopyFeedback' + +export type { ReaderMessage } from './readerProtocol' +export interface ReaderTranscript { events: LocalRunEvent[], loaded?: boolean, error?: string | null } +export interface ReaderEntry extends ReaderWireEntry { transcriptError?: string | null, turn?: ChatAgentTurn } + +export function useReaderBridge() { + const copiedMessageId = shallowRef(null) + provide(readerCopyFeedbackKey, copiedMessageId) + const copiedReset = useTimeoutFn(() => copiedMessageId.value = null, 1_400, { immediate: false }) + const appearance = shallowRef>({}) + const typography = shallowRef({ bodySize: 15, bodyLineHeight: 24 }) + const avatars = shallowRef>({}) + provide(readerAvatarsKey, avatars) + const entries = shallowRef([]) + const runs = shallowRef([]) + const transcripts = shallowRef>({}) + const topic = shallowRef('') + const dark = shallowRef(false) + const fontScale = shallowRef(1) + const language = shallowRef('zh') + provide(readerLanguageKey, language) + const resultRunId = shallowRef(null) + let port: MessagePort | undefined + const projection = new ReaderProjection() + + function send(action: ReaderActionInput) { + port?.postMessage(JSON.stringify(readerActionSchema.parse({ ...action, topic: topic.value }))) + } + + function connect(event: MessageEvent) { + if (event.data !== 'lexora-reader' || !event.isTrusted || event.source !== null || !event.ports[0]) + return + port?.close() + port = event.ports[0] + port.onmessage = (message: MessageEvent) => { + let source: unknown + try { + source = JSON.parse(message.data) + } + catch { + return + } + const parsed = readerCopiedSchema.or(readerUpdateSchema).safeParse(source) + if (!parsed.success) + return + const update = parsed.data + if ('type' in update) { + if (update.type === 'copied' && update.topic === topic.value) { + copiedMessageId.value = update.id + copiedReset.start() + } + return + } + if (update.appearance) + appearance.value = update.appearance.variables + if (update.typography) + typography.value = update.typography + const next = projection.apply(update) + if (next.reset) { + copiedMessageId.value = null + copiedReset.stop() + avatars.value = {} + } + if (update.avatars) + avatars.value = update.avatars + runs.value = (update.runs ?? []).map(run => ({ ...run, generation: Number(run.generation) })) + transcripts.value = next.transcripts + topic.value = update.topic + resultRunId.value = update.resultRunId ?? null + dark.value = update.dark + fontScale.value = update.fontScale + language.value = update.language === 'en' ? 'en' : 'zh' + setMarkdownLanguage(language.value) + document.documentElement.lang = language.value + document.title = 'Lexora' + entries.value = next.entries + } + port.start() + send({ action: 'ready' }) + } + + onMounted(() => window.addEventListener('message', connect)) + onUnmounted(() => { + window.removeEventListener('message', connect) + port?.close() + }) + return { entries, runs, transcripts, topic, dark, fontScale, appearance, typography, language, resultRunId, send } +} diff --git a/apps/buddy/src/mobile-reader/readerProjection.ts b/apps/buddy/src/mobile-reader/readerProjection.ts new file mode 100644 index 000000000..4287c4170 --- /dev/null +++ b/apps/buddy/src/mobile-reader/readerProjection.ts @@ -0,0 +1,35 @@ +import type { ReaderUpdate, ReaderWireEntry } from './readerProtocol' + +export class ReaderProjection { + #topic = '' + #items = new Map() + #order: string[] = [] + #transcripts: NonNullable = {} + + apply(update: ReaderUpdate) { + const reset = update.reset || update.topic !== this.#topic + if (reset) { + this.#items.clear() + this.#order = [] + this.#transcripts = {} + } + this.#topic = update.topic + if (update.transcripts) { + const next = { ...this.#transcripts } + for (const [id, transcript] of Object.entries(update.transcripts)) + next[id] = { ...transcript, events: transcript.append ? [...next[id]?.events ?? [], ...transcript.events] : transcript.events } + this.#transcripts = next + } + if (update.order) { + this.#order = update.order + const retained = new Set(this.#order) + for (const id of this.#items.keys()) { + if (!retained.has(id)) + this.#items.delete(id) + } + } + 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 } + } +} diff --git a/apps/buddy/src/mobile-reader/readerProtocol.ts b/apps/buddy/src/mobile-reader/readerProtocol.ts new file mode 100644 index 000000000..f99627e5c --- /dev/null +++ b/apps/buddy/src/mobile-reader/readerProtocol.ts @@ -0,0 +1,76 @@ +import type { z } from 'zod' +import { z as schema } from 'zod' +import { productRunSchema } from '../../shared/productProtocol/queries' +import { publicRunEventSchema } from '../../shared/runs/publicRunEvent' + +const id = schema.string().min(1) +export const readerMessageSchema = schema.object({ + id, + runId: id.nullable(), + role: schema.enum(['user', 'assistant', 'tool']), + authorId: id.nullable(), + author: schema.string(), + createdAt: schema.string().nullable().optional(), + text: schema.string(), + final: schema.boolean(), + markdown: schema.boolean(), + interrupted: schema.boolean(), + truncated: schema.boolean(), + showIdentity: schema.boolean(), + intermediate: schema.boolean(), + toolName: schema.string().nullable(), + toolSummary: schema.string(), + toolTitle: schema.string(), + toolError: schema.boolean(), + needsDetail: schema.boolean(), + loading: schema.boolean(), + error: schema.string().nullable(), + notice: schema.string().nullable(), + hasDetail: schema.boolean(), + detailResource: schema.string().nullable(), + actionsVisible: schema.boolean(), + canEdit: schema.boolean(), + canRegenerate: schema.boolean(), + canFollowup: schema.boolean(), + canCopy: schema.boolean(), + resources: schema.array(schema.object({ id, name: schema.string(), description: schema.string(), available: schema.boolean() }).strict()), +}).strict() +export const readerEntrySchema = schema.object({ id, continuation: schema.boolean(), messages: schema.array(readerMessageSchema) }).strict() +export const readerTranscriptSchema = schema.object({ events: schema.array(publicRunEventSchema), loaded: schema.boolean().optional(), error: schema.string().nullable().optional(), append: schema.boolean().optional() }).strict() +export const readerUpdateSchema = schema.object({ + appearance: schema.object({ variables: schema.record(schema.string(), schema.string()), colors: schema.record(schema.string(), schema.string()).optional() }).strict().optional(), + 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(), + topic: schema.string(), + reset: schema.boolean(), + dark: schema.boolean(), + fontScale: schema.number().positive(), + language: schema.string().optional(), + runs: schema.array(productRunSchema).optional(), + transcripts: schema.record(schema.string(), readerTranscriptSchema).optional(), + order: schema.array(id).optional(), + entries: schema.array(readerEntrySchema), +}).strict() +export const readerCopiedSchema = schema.object({ type: schema.literal('copied'), topic: schema.string(), id }).strict() +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('read-result', { runId: id }), + action('transcript', { runId: id, retry: schema.literal('true').optional() }), + action('message', { id }), + action('edit', { id }), + action('regenerate', { id }), + action('followup', { id }), + action('detail', { id }), + action('resource', { id, resourceId: id }), + action('copy', { id: schema.string().optional(), runId: id.optional(), text: schema.string(), feedback: schema.literal('message').optional() }), + action('link', { href: schema.string() }), +]) +export const READER_SCHEMAS = { update: readerUpdateSchema, copied: readerCopiedSchema, action: readerActionSchema } +export type ReaderMessage = z.infer +export type ReaderWireEntry = z.infer +export type ReaderUpdate = z.infer +export type ReaderAction = z.infer +export type ReaderActionInput = ReaderAction extends infer A ? A extends ReaderAction ? Omit : never : never +export type ReaderSend = (action: ReaderActionInput) => void diff --git a/apps/buddy/src/mobile-reader/readerStrings.ts b/apps/buddy/src/mobile-reader/readerStrings.ts new file mode 100644 index 000000000..bba00fa18 --- /dev/null +++ b/apps/buddy/src/mobile-reader/readerStrings.ts @@ -0,0 +1,62 @@ +import type { InjectionKey, Ref } from 'vue' +import { computed, inject } from 'vue' + +export const readerLanguageKey: InjectionKey>> = Symbol('reader-language') + +const en = { + messageActions: 'Message actions', + copied: 'Copied', + copyMessage: 'Copy message', + branch: 'Branch from here', + more: 'More message actions', + failed: 'Failed', + done: 'Done', + callDetails: 'Call details', + copyDetails: 'Copy call details', + loadingOutput: 'Loading input and output…', + noOutput: 'Completed without text output', + retry: 'Retry', + viewOutput: 'View input and output', + openFull: 'Open full content', + replying: 'Replying', + interrupted: 'This reply did not complete', + truncated: 'This preview is incomplete. Open the saved content for full details.', + loadingSaved: 'Loading saved content…', + viewFull: 'View full content', + previewSaveShare: 'Preview, save or share', + messages: 'Conversation messages', + latest: 'Jump to latest', + retryTranscript: 'Execution details unavailable · Retry', + empty: 'Messages will appear here', +} +const zh: Record = { + messageActions: '消息操作', + copied: '已复制', + copyMessage: '复制消息', + branch: '从这里继续', + more: '更多消息操作', + failed: '失败', + done: '完成', + callDetails: '调用详情', + copyDetails: '复制调用详情', + loadingOutput: '正在读取参数与输出…', + noOutput: '调用已完成,没有文本输出', + retry: '重试', + viewOutput: '查看参数与输出', + openFull: '打开完整内容', + replying: '正在回复', + interrupted: '这次回复未完成', + truncated: '消息预览不完整,详情以已保存内容为准。', + loadingSaved: '正在读取已保存内容…', + viewFull: '查看完整内容', + previewSaveShare: '预览、保存或分享', + messages: '对话消息', + latest: '回到最新', + retryTranscript: '执行过程暂未载入 · 重试', + empty: '消息将在这里显示', +} + +export function useReaderStrings(source?: Readonly>) { + const language = source ?? inject(readerLanguageKey) + return computed(() => language?.value === 'en' ? en : zh) +} diff --git a/apps/buddy/src/mobile-reader/useDocumentPreview.ts b/apps/buddy/src/mobile-reader/useDocumentPreview.ts new file mode 100644 index 000000000..437eadf12 --- /dev/null +++ b/apps/buddy/src/mobile-reader/useDocumentPreview.ts @@ -0,0 +1,56 @@ +import { onMounted, onUnmounted, shallowRef } from 'vue' +import { z } from 'zod' +import { setMarkdownLanguage } from './markdownStrings' + +const documentSchema = z.object({ + text: z.string().max(65536), + dark: z.boolean(), + language: z.enum(['zh', 'en']), + fontScale: z.number().min(0.5).max(3), + appearance: z.record(z.string(), z.string()), + canQuote: z.boolean(), + labels: z.object({ body: z.string(), quote: z.string() }), +}) + +export function useDocumentPreview() { + const document = shallowRef | null>(null) + const selection = shallowRef('') + let port: MessagePort | undefined + function send(action: string, text: string) { + port?.postMessage(JSON.stringify({ action, text })) + } + function select() { + const text = window.getSelection()?.toString().trim() ?? '' + selection.value = text.length <= 8000 ? text : '' + } + function connect(event: MessageEvent) { + if (event.data !== 'lexora-document' || !event.isTrusted || event.source !== null || !event.ports[0]) + return + port?.close() + port = event.ports[0] + port.onmessage = (message: MessageEvent) => { + try { + const parsed = documentSchema.safeParse(JSON.parse(message.data)) + if (parsed.success) { + setMarkdownLanguage(parsed.data.language) + document.value = parsed.data + window.document.documentElement.lang = parsed.data.language === 'zh' ? 'zh-CN' : 'en' + window.document.title = parsed.data.labels.body + } + } + catch {} + } + port.start() + send('ready', '') + } + onMounted(() => { + window.addEventListener('message', connect) + window.document.addEventListener('selectionchange', select) + }) + onUnmounted(() => { + window.removeEventListener('message', connect) + window.document.removeEventListener('selectionchange', select) + port?.close() + }) + return { document, selection, send } +} diff --git a/apps/buddy/src/mobile-reader/useReaderAvatars.ts b/apps/buddy/src/mobile-reader/useReaderAvatars.ts new file mode 100644 index 000000000..2abc53cc0 --- /dev/null +++ b/apps/buddy/src/mobile-reader/useReaderAvatars.ts @@ -0,0 +1,9 @@ +import type { InjectionKey, ShallowRef } from 'vue' +import { computed, inject } from 'vue' + +export const readerAvatarsKey: InjectionKey>> = Symbol('reader-avatars') + +export function useReaderAvatar(authorId: () => string | null) { + const avatars = inject(readerAvatarsKey) + return computed(() => avatars?.value[authorId() ?? '']) +} diff --git a/apps/buddy/src/mobile-reader/useReaderCopyFeedback.ts b/apps/buddy/src/mobile-reader/useReaderCopyFeedback.ts new file mode 100644 index 000000000..ec14d8e37 --- /dev/null +++ b/apps/buddy/src/mobile-reader/useReaderCopyFeedback.ts @@ -0,0 +1,5 @@ +import type { InjectionKey, Ref } from 'vue' +import { inject, shallowRef } from 'vue' + +export const readerCopyFeedbackKey: InjectionKey>> = Symbol('reader-copy-feedback') +export const useReaderCopyFeedback = () => inject(readerCopyFeedbackKey, shallowRef(null)) diff --git a/apps/buddy/src/mobile-reader/useReaderResultRead.ts b/apps/buddy/src/mobile-reader/useReaderResultRead.ts new file mode 100644 index 000000000..04dfb2f17 --- /dev/null +++ b/apps/buddy/src/mobile-reader/useReaderResultRead.ts @@ -0,0 +1,54 @@ +import type { Ref } from 'vue' +import type { ReaderSend } from './readerProtocol' +import { useEventListener, useIntersectionObserver, useMutationObserver } from '@vueuse/core' +import { onScopeDispose, shallowRef, watch } from 'vue' + +export function useReaderResultRead(root: Readonly>, resultRunId: Readonly>, topic: Readonly>, send: ReaderSend) { + const targets = shallowRef([]) + let reported: string | null = null + let frame: number | null = null + let disposed = false + function check() { + frame = null + if (disposed) + return + const id = resultRunId.value + const viewport = root.value + const elements = id && viewport ? [...viewport.querySelectorAll('[data-result-run-id]')].filter(element => element.dataset.resultRunId === id) : [] + if (elements.length !== targets.value.length || elements.some((element, index) => targets.value[index] !== element)) + targets.value = elements + const visible = document.visibilityState === 'visible' && viewport && elements.some((element) => { + const bounds = element.getBoundingClientRect() + const clip = viewport.getBoundingClientRect() + const left = Math.max(0, bounds.left, clip.left) + const right = Math.min(innerWidth, bounds.right, clip.right) + const top = Math.max(0, bounds.top, clip.top) + const bottom = Math.min(innerHeight, bounds.bottom, clip.bottom) + return right > left && bottom > top && element.contains(document.elementFromPoint((left + right) / 2, (top + bottom) / 2)) + }) + if (!id || !visible) { + reported = null + return + } + const key = `${topic.value}:${id}` + if (reported !== key) { + reported = key + send({ action: 'read-result', runId: id }) + } + } + function schedule() { + if (frame === null && !disposed) + frame = requestAnimationFrame(check) + } + watch([root, resultRunId, topic], schedule, { immediate: true, flush: 'post' }) + useMutationObserver(root, schedule, { childList: true, subtree: true, attributes: true, attributeFilter: ['data-result-run-id'] }) + useIntersectionObserver(targets, schedule, { root }) + useEventListener(root, 'scroll', schedule, { passive: true }) + useEventListener(window, 'resize', schedule) + useEventListener(document, 'visibilitychange', schedule) + onScopeDispose(() => { + disposed = true + if (frame !== null) + cancelAnimationFrame(frame) + }) +} diff --git a/apps/buddy/src/mobile-reader/useReaderTurns.ts b/apps/buddy/src/mobile-reader/useReaderTurns.ts new file mode 100644 index 000000000..622bc5dee --- /dev/null +++ b/apps/buddy/src/mobile-reader/useReaderTurns.ts @@ -0,0 +1,72 @@ +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 { computed, watch } from 'vue' +import { createChatAgentTurnReducer } from '../modules/tasks/reader' + +export function useReaderTurns(entries: Ref, runs: Ref, transcripts: Ref>, topic: Ref) { + const cache = new Map }>() + watch(topic, () => cache.clear()) + return computed(() => { + const turns = new Map() + for (const run of runs.value) { + const source = transcripts.value[run.id] + if (!source?.loaded || !source.events.length) + continue + const state = `${run.status}:${run.completedAt}:${run.errorCode}` + let cached = cache.get(run.id) + if (!cached || cached.state !== state || source.events.length < cached.count) { + cached = { count: 0, state, reducer: createChatAgentTurnReducer(run) } + cache.set(run.id, cached) + } + cached.reducer.append(source.events.slice(cached.count)) + cached.count = source.events.length + turns.set(run.id, cached.reducer.project()) + } + const messagesByRun = new Map() + for (const entry of entries.value) { + for (const message of entry.messages) { + if (message.role === 'user' || !message.runId) + continue + const messages = messagesByRun.get(message.runId) ?? [] + messages.push(message) + messagesByRun.set(message.runId, messages) + } + } + const emitted = new Set() + const errors = new Set() + const result: ReaderEntry[] = [] + const appendTurn = (turn: ChatAgentTurn) => { + emitted.add(turn.runId) + result.push({ id: `turn:${turn.runId}`, continuation: false, turn, transcriptError: transcripts.value[turn.runId]?.error, messages: messagesByRun.get(turn.runId) ?? [] }) + } + for (const entry of entries.value) { + const message = entry.messages[0] + const turn = message?.role !== 'user' && message?.runId ? turns.get(message.runId) : null + if (turn) { + if (!emitted.has(turn.runId)) + appendTurn(turn) + } + else { + const error = message?.runId ? transcripts.value[message.runId]?.error : undefined + if (error && message?.runId && !errors.has(message.runId) && message.role !== 'user') { + result.push({ ...entry, transcriptError: error }) + errors.add(message.runId) + } + else { + result.push(entry) + } + for (const candidate of turns.values()) { + if (candidate.triggeringMessageId === message?.id && !messagesByRun.has(candidate.runId) && !emitted.has(candidate.runId)) + appendTurn(candidate) + } + } + } + for (const id of cache.keys()) { + if (!runs.value.some(run => run.id === id)) + cache.delete(id) + } + return result + }) +} diff --git a/apps/buddy/src/mobile-reader/useReaderViewport.ts b/apps/buddy/src/mobile-reader/useReaderViewport.ts new file mode 100644 index 000000000..ec4f535a0 --- /dev/null +++ b/apps/buddy/src/mobile-reader/useReaderViewport.ts @@ -0,0 +1,117 @@ +import type { ComponentPublicInstance, Ref } from 'vue' +import type { ReaderEntry } from './readerBridge' +import { defaultRangeExtractor, elementScroll, useVirtualizer } from '@tanstack/vue-virtual' +import { computed, onMounted, onUnmounted, shallowRef, watch } from 'vue' + +export function useReaderViewport(viewport: Ref, entries: Ref, topic: Ref) { + const followsEnd = shallowRef(true) + const hasMoreBelow = shallowRef(false) + const showLatest = computed(() => !followsEnd.value && hasMoreBelow.value) + const selected = shallowRef([]) + let frame = 0 + let touching = false + let lastTop = 0 + let programmaticTop: number | null = null + const virtualizer = useVirtualizer(computed(() => { + const items = entries.value + return { + count: items.length, + getScrollElement: () => viewport.value, + getItemKey: (index: number) => items[index]!.id, + estimateSize: (index: number) => { + const entry = items[index]! + if (entry.messages[0]?.role === 'tool') + return 70 + return 100 + entry.messages.reduce((height, message) => height + Math.ceil(message.text.length / 28) * 26, 0) + }, + overscan: 3, + paddingStart: 18, + paddingEnd: 24, + 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), + onChange: () => { + report() + follow() + }, + scrollToFn: (offset, options, instance) => { + programmaticTop = offset + (options.adjustments ?? 0) + elementScroll(offset, options, instance) + }, + } + })) + virtualizer.value.shouldAdjustScrollPositionOnItemSizeChange = (item, _delta, instance) => !followsEnd.value && item.end <= (instance.scrollOffset ?? 0) + const rows = computed(() => virtualizer.value.getVirtualItems().flatMap((row) => { + const entry = entries.value[row.index] + return entry ? [{ ...row, entry }] : [] + })) + const height = computed(() => virtualizer.value.getTotalSize()) + + function report() { + const element = viewport.value + hasMoreBelow.value = Boolean(element && virtualizer.value.getTotalSize() - element.clientHeight - element.scrollTop > 24) + } + function follow() { + cancelAnimationFrame(frame) + if (!followsEnd.value || touching || window.getSelection()?.toString()) + return + frame = requestAnimationFrame(() => { + const element = viewport.value + if (followsEnd.value && !touching && element && Math.abs(Math.max(0, virtualizer.value.getTotalSize() - element.clientHeight) - element.scrollTop) > 1) + virtualizer.value.scrollToEnd() + }) + } + function latest() { + followsEnd.value = true + follow() + } + function scroll() { + const element = viewport.value + if (!element) + return + report() + 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 = true + programmaticTop = null + lastTop = element.scrollTop + } + function touch(active: boolean) { + touching = active + if (!active) + follow() + } + function measure(element: Element | ComponentPublicInstance | null) { + if (element instanceof HTMLElement) + virtualizer.value.measureElement(element) + } + function retainSelection() { + const selection = window.getSelection() + if (!selection?.rangeCount || selection.isCollapsed) { + selected.value = [] + return + } + const range = selection.getRangeAt(0) + const indexes = [...viewport.value?.querySelectorAll('[data-index]') ?? []] + .filter(element => range.intersectsNode(element)) + .map(element => Number(element.dataset.index)) + selected.value = indexes + } + watch(topic, () => { + followsEnd.value = true + selected.value = [] + virtualizer.value.measure() + follow() + }) + watch(entries, follow, { flush: 'post' }) + onMounted(() => document.addEventListener('selectionchange', retainSelection)) + onUnmounted(() => { + document.removeEventListener('selectionchange', retainSelection) + cancelAnimationFrame(frame) + }) + return { rows, height, followsEnd, showLatest, latest, scroll, touch, measure } +} diff --git a/apps/buddy/src/modules/buddies/reader.ts b/apps/buddy/src/modules/buddies/reader.ts new file mode 100644 index 000000000..406be365b --- /dev/null +++ b/apps/buddy/src/modules/buddies/reader.ts @@ -0,0 +1 @@ +export { default as BuddyAvatar } from './widgets/BuddyAvatar.vue' diff --git a/apps/buddy/src/modules/extensions/state/__tests__/useExtensionState.spec.ts b/apps/buddy/src/modules/extensions/state/__tests__/useExtensionState.spec.ts index a8889b0eb..1296e9708 100644 --- a/apps/buddy/src/modules/extensions/state/__tests__/useExtensionState.spec.ts +++ b/apps/buddy/src/modules/extensions/state/__tests__/useExtensionState.spec.ts @@ -20,11 +20,9 @@ const extension: ExtensionStatus = { logs: [], } const review: ExtensionReview = { manifest: extension.manifest, token: 'review', installationId: 'job', sha256: 'hash', development: false, currentVersion: null, addedPermissions: [] } -const job: ExtensionInstallation = { id: 'job', name: 'Panel', extensionId: extension.manifest.id, version: '1.0.0', startedAt: '2026-01-01T00:00:00.000Z', status: 'running', stage: 'compile', entries: [], error: null } function setup(overrides: Partial = {}) { let statusChanged = () => {} - let progressChanged = () => {} const api: Partial = { list: async () => [extension], unavailable: async () => [], @@ -34,15 +32,12 @@ function setup(overrides: Partial = {}) { statusChanged = listener return () => {} }, - onInstallationsChanged: (listener) => { - progressChanged = listener - return () => {} - }, + onInstallationsChanged: () => () => {}, ...overrides, } const scope = effectScope() const state = scope.run(() => useExtensionState(api as ExtensionApi))! - return { state, scope, statusChanged: () => statusChanged(), progressChanged: () => progressChanged() } + return { state, scope, statusChanged: () => statusChanged() } } it('publishes installed contributions while installation jobs are pending or fail', async () => { @@ -59,27 +54,6 @@ it('publishes installed contributions while installation jobs are pending or fai finally { scope.stop() } }) -it('coalesces overlapping refreshes and rejects stale and disposed results', async () => { - const delayed = deferred() - let requests = 0 - const { state, scope } = setup({ list: () => ++requests === 1 ? delayed.promise : Promise.resolve([extension]) }) - try { - const previous = state.refresh() - await expect.poll(() => requests).toBe(1) - const refreshes = Array.from({ length: 20 }, () => state.refresh()) - expect(requests).toBe(1) - delayed.resolve([]) - await Promise.all([previous, ...refreshes]) - expect(state.installed.value).toEqual([extension]) - expect(requests).toBe(2) - scope.stop() - await state.refresh() - expect(state.installed.value).toEqual([extension]) - expect(requests).toBe(2) - } - finally { scope.stop() } -}) - it('publishes serial snapshots during a change burst and ignores reads after disposal', async () => { const first = deferred() const second = deferred() @@ -126,28 +100,6 @@ it('deduplicates only the running menu action while allowing other plugin intera } }) -it('updates progress without replacing installed contributions or requesting their data', async () => { - let listRequests = 0 - let jobs = [job] - const { state, scope, progressChanged } = setup({ - list: async () => { - listRequests++ - return [extension] - }, - installations: async () => jobs, - }) - try { - await state.refresh() - const original = state.installed.value - jobs = [{ ...job, stage: 'install' }] - progressChanged() - await expect.poll(() => state.installations.value[0]?.stage).toBe('install') - expect(state.installed.value).toBe(original) - expect(listRequests).toBe(1) - } - finally { scope.stop() } -}) - it('keeps new installs separate from usable plugins and only locks the matching plugin', async () => { const installing = deferred() let installed: ExtensionStatus[] = [] diff --git a/apps/buddy/src/modules/extensions/widgets/DesktopExtensionInstallReview.vue b/apps/buddy/src/modules/extensions/widgets/DesktopExtensionInstallReview.vue index 1de8c8983..3a80fc631 100644 --- a/apps/buddy/src/modules/extensions/widgets/DesktopExtensionInstallReview.vue +++ b/apps/buddy/src/modules/extensions/widgets/DesktopExtensionInstallReview.vue @@ -94,6 +94,20 @@ const source = computed(() => props.review.development ? (english.value ? 'Devel +
+

+ {{ english ? 'Execution lifetime' : '运行范围' }} + + {{ english ? 'Changed' : '已变更' }} + +

+

+ {{ review.manifest.executionScope === 'service' + ? (english ? 'May continue serving an active task after Desktop closes. Starts on demand and stops when no task or view is using it. Window and selection actions still require Desktop.' : '桌面关闭后,可继续为正在执行的任务提供能力。按需启动,无任务或界面使用时停止。窗口和选区操作仍需桌面。') + : (english ? 'Runs while Desktop is open. Closing Desktop cancels its active calls and timers.' : '在桌面打开期间运行,关闭桌面会取消其在途调用并停止定时器。') }} +

+
+ { - const installed = shallowRef([{ manifest: extensionManifestSchema.parse({ schemaVersion: 1, id: 'tests.cleanup', name: 'Cleanup', version: '1.0.0', apiVersion: 4, engines: { lexora: '*' }, contributes: {} }), revision: 'first', enabled: true, compatible: true, development: false, pending: null, state: 'active', generation: crypto.randomUUID(), error: null, activationMs: null, logs: [] }]) + const installed = shallowRef([{ manifest: extensionManifestSchema.parse({ schemaVersion: 1, id: 'tests.cleanup', name: 'Cleanup', version: '1.0.0', apiVersion: 5, engines: { lexora: '*' }, contributes: {} }), revision: 'first', enabled: true, compatible: true, development: false, pending: null, state: 'active', generation: crypto.randomUUID(), error: null, activationMs: null, logs: [] }]) const viewRequest = vi.fn().mockResolvedValue(null) const api: Partial = { openView: async input => ({ id: input.viewId, extensionId: input.extensionId, generation: installed.value[0]!.generation!, token: crypto.randomUUID(), url: 'about:blank' }), diff --git a/apps/buddy/src/modules/extensions/widgets/__tests__/DesktopExtensionInstallReview.spec.ts b/apps/buddy/src/modules/extensions/widgets/__tests__/DesktopExtensionInstallReview.spec.ts index 8afa5d929..79573759d 100644 --- a/apps/buddy/src/modules/extensions/widgets/__tests__/DesktopExtensionInstallReview.spec.ts +++ b/apps/buddy/src/modules/extensions/widgets/__tests__/DesktopExtensionInstallReview.spec.ts @@ -1,6 +1,5 @@ // @vitest-environment jsdom import type { ExtensionReview } from '@buddy-shared/extensions/extensionApi' -import type { InferencePlatform } from '@buddy-shared/extensions/extensionInference' import type { FileOpeningChoice } from '@buddy-shared/files/fileOpening' import { extensionManifestSchema } from '@buddy-shared/extensions/extensionManifest' import { afterEach, beforeEach, expect, it, vi } from 'vitest' @@ -40,36 +39,16 @@ const review: ExtensionReview = { ], } -const capabilityReview: ExtensionReview = { - ...review, - fileAssociations: [], - manifest: extensionManifestSchema.parse({ - ...review.manifest, - id: 'tests.legal', - apiVersion: 4, - contributes: { - skills: [{ id: 'tests.legal.statutes', path: 'skills/statutes/SKILL.md' }], - mcpConnectors: [{ id: 'legal-search', name: 'Legal search', transport: 'streamable-http', url: 'https://legal.example.com/mcp', authentication: 'bearer' }], - subagents: [ - { id: 'law-finder', name: 'Law finder', description: 'Finds statutes', instructions: 'Search statutes first.', tools: [{ source: 'mcp', connector: 'legal-search', name: 'statutes/search' }, { source: 'builtin', name: 'read' }], skills: ['tests.legal.statutes'], requiredSkills: ['tests.legal.statutes'], fileAccess: 'inherit' }, - { id: 'file-reader', name: 'File reader', instructions: 'Read files.', tools: [{ source: 'builtin', name: 'ls' }], fileAccess: 'read_only' }, - ], - }, - }), -} - -async function mount(value: ExtensionReview = review, language = 'en-US', busy = false) { +async function mount(value: ExtensionReview = review) { const current = shallowRef(value) const root = document.createElement('div') document.body.append(root) const submissions: { applyUpdate: boolean, choices: FileOpeningChoice[] }[] = [] const acceptances: boolean[] = [] - const cancellations: boolean[] = [] const app = createApp({ render: () => h(DesktopExtensionInstallReview, { review: current.value, - language, - busy, - onCancel: () => cancellations.push(true), + language: 'en-US', + busy: false, onInstall: (applyUpdate: boolean, choices: FileOpeningChoice[], acceptUnverified: boolean) => { submissions.push({ applyUpdate, choices }) acceptances.push(acceptUnverified) @@ -83,29 +62,9 @@ async function mount(value: ExtensionReview = review, language = 'en-US', busy = await nextTick() const content = document.querySelector('[data-testid="extension-file-associations"]')! const submit = () => document.querySelector('[data-testid="extension-confirm-install"]')!.click() - const dialog = document.querySelector('[data-testid="extension-install-review"]')! - return { content, submissions, submit, dialog, current, cancellations, acceptances } + return { content, submissions, submit, current, acceptances } } -it.each(['zh-CN', 'en-US'])('explains the narrow legacy cleanup permission in %s', async (language) => { - await mount({ - ...review, - currentVersion: '1.0.0', - addedPermissions: ['legacyTaskCleanup'], - manifest: extensionManifestSchema.parse({ - ...review.manifest, - apiVersion: 4, - permissions: { legacyTaskCleanup: true }, - contributes: { views: [{ id: 'test.panel.cleanup', title: 'Cleanup', entry: 'view.js', resource: 'none', location: 'page' }] }, - }), - }, language) - const permission = document.querySelector('[data-permission="legacyTaskCleanup"]')! - expect(permission.textContent).toContain(language === 'zh-CN' ? '元数据' : 'metadata') - expect(permission.textContent).toContain(language === 'zh-CN' ? '不允许删除活动任务或任意文件' : 'Cannot delete active tasks or arbitrary files') - expect(permission.textContent).toContain(language === 'zh-CN' ? '不可恢复' : 'cannot be undone') - expect(permission.querySelector('[data-testid="extension-new-permission"]')).not.toBeNull() -}) - it('requires a new risk acknowledgement for each unverified review while showing its actual permissions', async () => { const unverified: ExtensionReview = { ...review, @@ -136,78 +95,6 @@ it('requires a new risk acknowledgement for each unverified review while showing expect(acceptances).toEqual([true, false]) }) -it.each(['en-US', 'zh-CN'])('reviews each requested data source separately from message access in %s', async (language) => { - const { current } = await mount({ - ...review, - currentVersion: '0.9.0', - addedPermissions: ['data:context.usage'], - manifest: extensionManifestSchema.parse({ ...review.manifest, permissions: { data: ['task.metrics', 'context.usage'], network: ['https://metrics.example.com'] } }), - }, language) - const permissions = document.querySelector('[data-testid="extension-review-permissions"]')! - const text = permissions.textContent ?? '' - expect(text).toContain(language === 'en-US' ? 'Bound data sources' : '绑定范围的数据源') - expect(text).toContain(language === 'en-US' ? 'Task metrics' : '任务统计') - expect(text).toContain(language === 'en-US' ? 'Context usage' : '上下文用量') - expect(text).toContain(language === 'en-US' ? 'No message text, logs or credentials' : '不读取正文、日志或凭据') - expect(text).toContain(language === 'en-US' ? 'New' : '新增') - expect(text).not.toContain(language === 'en-US' ? 'Read messages' : '读取消息') - current.value = { ...review, fileAssociations: [] } - await nextTick() - expect(document.querySelector('[data-testid="extension-review-permissions"]')).toBeNull() -}) - -it.each(['en-US', 'zh-CN'])('explains the declared supervised platforms without claiming OS isolation in %s', async (language) => { - const nativeReview = (platforms: InferencePlatform[]): ExtensionReview => ({ - ...review, - addedPermissions: ['inference'], - manifest: extensionManifestSchema.parse({ - ...review.manifest, - apiVersion: 4, - entry: 'extension.js', - permissions: { inference: true }, - contributes: { inference: { runtimes: [{ id: 'chat', entry: 'native/chat.mjs', platforms }], capabilities: [{ id: 'chat', title: 'Chat', kind: 'chat.completion', runtime: 'chat' }] } }, - }), - }) - const { current } = await mount(nativeReview(['macos-arm64']), language) - const permissions = () => document.querySelector('[data-testid="extension-review-permissions"]')?.textContent ?? '' - expect(permissions()).toContain('macOS') - expect(permissions()).not.toContain('Windows') - expect(permissions()).toContain(language === 'en-US' ? 'files, credentials, devices and network' : '文件、凭据、设备和网络') - expect(permissions()).toContain(language === 'en-US' ? 'do not isolate native code' : '不受插件文件与网络权限范围隔离') - current.value = nativeReview(['windows-x64', 'macos-arm64']) - await nextTick() - expect(permissions()).toContain(language === 'en-US' ? 'Windows and macOS' : 'Windows、macOS') - current.value = nativeReview(['linux-x64']) - await nextTick() - expect(permissions()).not.toContain('macOS') - expect(permissions()).not.toContain('Windows') - expect(permissions()).toContain(language === 'en-US' ? 'prepared model resources' : '已准备的模型资源') -}) - -it('allows closing a duplicate review while the plugin is busy without submitting another install', async () => { - const { dialog, submissions, cancellations } = await mount(review, 'en-US', true) - expect(dialog.querySelector('[data-testid="extension-confirm-install"]')!.disabled).toBe(true) - const cancel = [...dialog.querySelectorAll('button')].find(button => button.textContent?.trim() === 'Cancel')! - expect(cancel.disabled).toBe(false) - cancel.click() - expect(cancellations).toEqual([true]) - expect(submissions).toEqual([]) -}) - -it('preselects requested associations and keeps existing defaults when replacements are cleared', async () => { - const { content, submissions, submit } = await mount() - const checkboxes = content.querySelectorAll('[role="checkbox"]') - expect(checkboxes[0]?.getAttribute('aria-checked')).toBe('true') - expect(checkboxes[1]?.getAttribute('aria-checked')).toBe('true') - submit() - expect(submissions).toEqual([{ applyUpdate: true, choices: [{ extension: '.md', location: 'inline', providerId: 'test.reader' }] }]) - checkboxes[0]!.click() - await nextTick() - submit() - expect(submissions.at(-1)).toEqual({ applyUpdate: true, choices: [] }) - expect(checkboxes[1]?.getAttribute('aria-checked')).toBe('true') -}) - it('leaves opt-out replacements unselected and supports individual and column selection independently', async () => { const { content, submissions, submit } = await mount({ ...review, @@ -264,54 +151,3 @@ it('can restore the current provider after selecting another provider for the sa submit() expect(submissions.at(-1)?.choices).toEqual([]) }) - -it('preselects the first requested provider when several providers apply to the same location', async () => { - const { content, submissions, submit } = await mount({ - ...review, - fileAssociations: [ - { ...review.fileAssociations![0]!, requestDefault: false }, - { ...review.fileAssociations![0]!, providerId: 'test.alternate', title: 'Alternate' }, - { ...review.fileAssociations![0]!, providerId: 'test.third', title: 'Third' }, - ], - }) - const checked = content.querySelectorAll('tbody [aria-checked="true"]') - expect(checked).toHaveLength(1) - expect(checked[0]?.textContent).toContain('Alternate') - submit() - expect(submissions.at(-1)?.choices).toEqual([{ extension: '.md', location: 'inline', providerId: 'test.alternate' }]) -}) - -it('discloses NPC capabilities on demand and resets expanded details for each review', async () => { - const { current } = await mount(capabilityReview) - const text = (selector: string) => document.querySelector(selector)?.textContent ?? '' - expect(text('[data-testid="extension-included-mcp"]')).toContain('https://legal.example.com/mcp') - expect(document.querySelector('[data-testid="extension-review-permissions"]')).toBeNull() - expect(text('[data-testid="extension-included-mcp"]')).toContain('Token authentication') - expect(text('[data-testid="extension-included-mcp"]')).toContain('Off by default') - expect(text('[data-subagent-id="law-finder"]')).toContain('Tools 2 · Skills 1') - expect(text('[data-subagent-id="law-finder"]')).toContain('File access follows the current task') - expect(text('[data-subagent-id="file-reader"]')).toContain('Can read files but not change them') - expect(document.querySelector('[data-testid="extension-agent-details"]')).toBeNull() - const agent = document.querySelector('[data-subagent-id="law-finder"]')! - const toggle = agent.querySelector('[data-testid="extension-agent-details-toggle"]')! - expect(toggle.getAttribute('aria-expanded')).toBe('false') - toggle.click() - await nextTick() - expect(toggle.getAttribute('aria-expanded')).toBe('true') - expect(document.getElementById(toggle.getAttribute('aria-controls')!)).not.toBeNull() - expect(text('[data-subagent-id="law-finder"]')).toContain('statutes/search') - expect(text('[data-subagent-id="law-finder"]')).toContain('Legal search') - expect(text('[data-subagent-id="law-finder"]')).toContain('Built-in tool') - expect(text('[data-subagent-id="law-finder"]')).toContain('Required') - expect(text('[data-subagent-id="law-finder"]')).toContain('tests.legal.statutes') - expect(document.querySelector('[data-subagent-id="file-reader"] [data-testid="extension-agent-details"]')).toBeNull() - toggle.click() - await nextTick() - expect(document.querySelector('[data-testid="extension-agent-details"]')).toBeNull() - toggle.click() - await nextTick() - expect(document.querySelector('[data-testid="extension-agent-details"]')).not.toBeNull() - current.value = { ...capabilityReview, token: 'another-review' } - await nextTick() - expect(document.querySelector('[data-testid="extension-agent-details-toggle"]')?.getAttribute('aria-expanded')).toBe('false') -}) diff --git a/apps/buddy/src/modules/settings/model/__tests__/settingsRegistry.spec.ts b/apps/buddy/src/modules/settings/model/__tests__/settingsRegistry.spec.ts index 2df50a70b..726d7139d 100644 --- a/apps/buddy/src/modules/settings/model/__tests__/settingsRegistry.spec.ts +++ b/apps/buddy/src/modules/settings/model/__tests__/settingsRegistry.spec.ts @@ -1,9 +1,7 @@ import type { SettingsContribution } from '../settingsRegistry' import { extensionSettingItemSchema } from '@buddy-shared/extensions/extensionSettings' -import { builtinSettingsModuleIds, publicSettingsGroups } from '@buddy-shared/settings/settingsCatalog' +import { builtinSettingsModuleIds } from '@buddy-shared/settings/settingsCatalog' import { describe, expect, it } from 'vitest' -import { createMemoryHistory, createRouter } from 'vue-router' -import { settingsRoutes } from '../../routes' import { builtinSettings } from '../builtinSettings' import { extensionSettingsLocation } from '../settingsNavigation' import { SettingsRegistry } from '../settingsRegistry' @@ -29,31 +27,6 @@ describe('settings registry', () => { expect(extensionSettingsLocation(registry.modules, 'tests.settings')).toBeNull() }) - it('opens an owned module, a builtin group or an inline field only for registered owners', () => { - const registry = new SettingsRegistry() - registry.register('settings', builtinSettings) - const contribution = plugin() - const dispose = registry.register('tests.settings', contribution) - expect(extensionSettingsLocation(registry.modules, 'tests.settings')).toEqual({ name: 'desktop.settings.plugin', params: { moduleId: 'tests.settings.module' } }) - expect(extensionSettingsLocation(registry.modules, 'tests')).toBeNull() - registry.register('tests.settings', { ...contribution, modules: [], groups: [{ ...contribution.groups[1]!, module: 'settings.runtime' }] }) - dispose() - expect(extensionSettingsLocation(registry.modules, 'tests.settings')).toEqual({ name: 'desktop.settings.runtime', query: { group: 'tests.settings.extra' } }) - const remove = registry.register('tests.settings', { ...contribution, modules: [], groups: [] }) - expect(extensionSettingsLocation(registry.modules, 'tests.settings')).toEqual({ name: 'desktop.settings.general', query: { group: 'settings.general.general' } }) - remove() - expect(extensionSettingsLocation(registry.modules, 'tests.settings')).toBeNull() - }) - it('projects builtins and extensions into one ordered module/group/item tree', () => { - const registry = new SettingsRegistry() - registry.register('settings', builtinSettings) - registry.register('tests.settings', plugin()) - const general = registry.modules.find(module => module.id === 'settings.general')! - expect(general.groups.slice(0, 3).map(group => group.id)).toEqual(['settings.general.general', 'tests.settings.extra', 'settings.general.context-panel']) - expect(general.groups[0]!.items.map(item => item.id)).toEqual(['tests.settings.inline', 'settings.general.language']) - expect(registry.modules.find(module => module.id === 'tests.settings.module')!.groups[0]!.id).toBe('tests.settings.group') - }) - it('rejects conflicting IDs, missing parents and cross-owner parents without partial publication', () => { const registry = new SettingsRegistry() registry.register('settings', builtinSettings) @@ -89,16 +62,4 @@ describe('settings registry', () => { disposeFirst() expect(registry.modules.some(module => module.id === 'tests.settings.module')).toBe(true) }) - - it('keeps public setting groups and existing builtin URLs backed by registered definitions', () => { - const registry = new SettingsRegistry() - registry.register('settings', builtinSettings) - const router = createRouter({ history: createMemoryHistory(), routes: [...settingsRoutes] }) - for (const [group, module] of Object.entries(publicSettingsGroups)) - expect(registry.modules.find(entry => entry.id === module)!.groups.some(entry => entry.id === group)).toBe(true) - expect(router.resolve('/settings/models/example').meta.settingsModule).toBe('settings.models') - expect(router.resolve('/settings/plugins/tests.settings.module').params.moduleId).toBe('tests.settings.module') - expect(router.resolve('/settings/app').matched.at(-1)!.redirect).toEqual({ name: 'desktop.settings.general' }) - expect(router.resolve('/settings/extensions').matched.at(-1)!.redirect).toEqual({ name: 'desktop.extensions' }) - }) }) diff --git a/apps/buddy/src/modules/settings/model/builtinSettings.ts b/apps/buddy/src/modules/settings/model/builtinSettings.ts index 6ddfb1da2..771bed1b4 100644 --- a/apps/buddy/src/modules/settings/model/builtinSettings.ts +++ b/apps/buddy/src/modules/settings/model/builtinSettings.ts @@ -12,7 +12,7 @@ export const builtinSettings = { page: category === 'general' ? undefined : category, ...builtinSettingsCatalog[category], order, - fill: category === 'logs' || category === 'prompts', + fill: category === 'logs' || category === 'prompts' || category === 'connections', title: category === 'file-opening' ? language => language === 'en-US' ? 'Open with' : '打开方式' : text(`desktop.settings.category.${category}`), description: category === 'task-archive' ? undefined @@ -32,6 +32,7 @@ export const builtinSettings = { })), ], items: [ + { id: 'settings.general.exitBehavior', group: 'settings.general.general', order: 1, kind: 'general', field: 'exitBehavior' }, { id: 'settings.general.pasteTextAsAttachment', group: 'settings.general.composer', order: 0, kind: 'general', field: 'pasteTextAsAttachment' }, { id: 'settings.general.chineseInputTriggers', group: 'settings.general.composer', order: 10, kind: 'general', field: 'chineseInputTriggers' }, ...(['language', 'contextPanelMode', 'contextPanelGlobal', 'contextPanelAutoCollapse'] as GeneralSettingField[]).map((field, order) => ({ diff --git a/apps/buddy/src/modules/settings/model/settingsRegistry.ts b/apps/buddy/src/modules/settings/model/settingsRegistry.ts index 3b90fc910..a4db7f956 100644 --- a/apps/buddy/src/modules/settings/model/settingsRegistry.ts +++ b/apps/buddy/src/modules/settings/model/settingsRegistry.ts @@ -7,7 +7,7 @@ import { builtinSettingsModuleIds, publicSettingsGroups } from '@buddy-shared/se export type SettingsText = string | ((language: BuddyLocale) => string) export type SettingsSection = 'plugins' | 'personal' | 'ai' | 'integrations' | 'system' -export type GeneralSettingField = 'language' | 'contextPanelMode' | 'contextPanelGlobal' | 'contextPanelAutoCollapse' | 'pasteTextAsAttachment' | 'chineseInputTriggers' +export type GeneralSettingField = 'exitBehavior' | 'language' | 'contextPanelMode' | 'contextPanelGlobal' | 'contextPanelAutoCollapse' | 'pasteTextAsAttachment' | 'chineseInputTriggers' export interface SettingsModule { id: string diff --git a/apps/buddy/src/modules/settings/pages/DesktopConnectionsSettingsView.vue b/apps/buddy/src/modules/settings/pages/DesktopConnectionsSettingsView.vue new file mode 100644 index 000000000..0be56a624 --- /dev/null +++ b/apps/buddy/src/modules/settings/pages/DesktopConnectionsSettingsView.vue @@ -0,0 +1,10 @@ + + + diff --git a/apps/buddy/src/modules/settings/pages/settingsPageRenderers.ts b/apps/buddy/src/modules/settings/pages/settingsPageRenderers.ts index 9febce329..aeb24e997 100644 --- a/apps/buddy/src/modules/settings/pages/settingsPageRenderers.ts +++ b/apps/buddy/src/modules/settings/pages/settingsPageRenderers.ts @@ -19,6 +19,7 @@ export const settingsPageRenderers: Record import('./DesktopBrowserSettingsView.vue')), 'file-opening': defineAsyncComponent(() => import('./DesktopFileOpeningSettingsView.vue')), 'storage': defineAsyncComponent(() => import('./DesktopStorageSettingsView.vue')), + 'connections': defineAsyncComponent(() => import('./DesktopConnectionsSettingsView.vue')), 'proxy': defineAsyncComponent(() => import('./DesktopProxySettingsView.vue')), 'logs': defineAsyncComponent(() => import('./DesktopLogsSettingsView.vue')), 'about': defineAsyncComponent(() => import('./DesktopAboutSettingsView.vue')), diff --git a/apps/buddy/src/modules/settings/widgets/app/DesktopGeneralSettingField.vue b/apps/buddy/src/modules/settings/widgets/app/DesktopGeneralSettingField.vue index fa8b8ce9e..79432cc89 100644 --- a/apps/buddy/src/modules/settings/widgets/app/DesktopGeneralSettingField.vue +++ b/apps/buddy/src/modules/settings/widgets/app/DesktopGeneralSettingField.vue @@ -19,7 +19,12 @@ const contextPanelModes = computed(() => [ { label: t('desktop.settings.contextPanelSpace'), value: 'space' }, { label: t('desktop.settings.contextPanelIndependent'), value: 'independent' }, ]) +const exitBehaviors = computed(() => [ + { label: t('desktop.settings.exitKeepService'), value: 'keep_service' }, + { label: t('desktop.settings.exitStopService'), value: 'stop_service' }, +]) const labels = computed(() => ({ + exitBehavior: { title: t('desktop.settings.exitBehavior'), description: t('desktop.settings.exitBehaviorDescription'), testId: 'exit-behavior-setting' }, language: { title: t('settings.language'), description: '', testId: undefined }, contextPanelMode: { title: t('desktop.settings.contextPanelMode'), description: t('desktop.settings.contextPanelModeDescription'), testId: 'context-panel-mode-setting' }, contextPanelGlobal: { title: t('desktop.settings.contextPanelGlobal'), description: t('desktop.settings.contextPanelGlobalDescription'), testId: 'context-panel-global-setting' }, @@ -36,6 +41,7 @@ function save(patch: LexoraConfigPatch) { diff --git a/apps/buddy/src/modules/tasks/widgets/task-index/DesktopTaskMarkManager.vue b/apps/buddy/src/modules/tasks/widgets/task-index/DesktopTaskMarkManager.vue index 662f31f2a..0cd0dc8e3 100644 --- a/apps/buddy/src/modules/tasks/widgets/task-index/DesktopTaskMarkManager.vue +++ b/apps/buddy/src/modules/tasks/widgets/task-index/DesktopTaskMarkManager.vue @@ -11,14 +11,14 @@ import DesktopIcon from '@/shared/ui/icon/DesktopIcon.vue' import DesktopTaskMarkEditor from './DesktopTaskMarkEditor.vue' import DesktopTaskMarkItem from './DesktopTaskMarkItem.vue' -type MarkDefinition = Pick +type MarkDefinition = Pick const props = defineProps<{ marks: TaskMarks, language: BuddyLocale }>() const show = defineModel('show', { required: true }) const { t } = useBuddyI18n(() => props.language) const editing = shallowRef(false) const initial = shallowRef(null) -const deleteId = shallowRef(null) +const deletion = shallowRef(null) const session = shallowRef(0) const readOnly = computed(() => initial.value?.id === SYSTEM_UNREAD_MARK_ID) const definitions = computed(() => [{ @@ -27,7 +27,6 @@ const definitions = computed(() => [{ description: t('desktop.marks.unreadDescription'), color: 'var(--buddy-accent-solid)', }, ...props.marks.items.value]) -const deletion = computed(() => props.marks.items.value.find(mark => mark.id === deleteId.value) ?? null) const heading = computed(() => { if (!editing.value) return t('desktop.marks.manage') @@ -39,7 +38,7 @@ watch(show, (open) => { session.value += 1 editing.value = false initial.value = null - deleteId.value = null + deletion.value = null if (open) void props.marks.refresh() }) @@ -48,7 +47,7 @@ function edit(mark: MarkDefinition | null) { session.value += 1 initial.value = mark editing.value = true - deleteId.value = null + deletion.value = null } function backToList() { @@ -63,14 +62,14 @@ async function save(input: TaskMarkInput) { if (readOnly.value) return const version = session.value - if (await props.marks.save(input, initial.value?.id) && session.value === version && show.value) + if (await props.marks.save(input, initial.value?.id, initial.value?.revision) && session.value === version && show.value) editing.value = false } async function remove() { - const id = deleteId.value - if (id && await props.marks.remove(id) && deleteId.value === id) - deleteId.value = null + const mark = deletion.value + if (mark && await props.marks.remove(mark.id, mark.revision) && deletion.value === mark) + deletion.value = null } @@ -120,14 +119,14 @@ async function remove() { :disabled="marks.busy.value" :language="language" @edit="edit(mark)" - @remove="deleteId = mark.id" + @remove="deletion = mark" />

{{ t('desktop.marks.deleteMessage', { name: deletion.name }) }}

- + {{ t('common.cancel') }} diff --git a/apps/buddy/src/modules/tasks/widgets/task-index/DesktopTaskMoveToSpaceDialog.vue b/apps/buddy/src/modules/tasks/widgets/task-index/DesktopTaskMoveToSpaceDialog.vue index 022e881bc..2872ad3fc 100644 --- a/apps/buddy/src/modules/tasks/widgets/task-index/DesktopTaskMoveToSpaceDialog.vue +++ b/apps/buddy/src/modules/tasks/widgets/task-index/DesktopTaskMoveToSpaceDialog.vue @@ -9,7 +9,7 @@ import { resolveLocalChatErrorMessage } from '@/shared/lib/localChatError' const props = defineProps<{ busy: boolean language: BuddyLocale - moveTaskToSpace: (taskId: string, spaceId: string) => Promise + moveTaskToSpace: (taskId: string, spaceId: string, expectedRevision?: number) => Promise show: boolean spaces: readonly { id: string, name: string }[] task: LocalConversationSummary | null @@ -29,11 +29,13 @@ const taskUnavailable = computed(() => !props.task || props.task.spaceId !== nul const taskBusy = computed(() => props.busy || (props.task && props.task.activity !== 'idle')) const canMove = computed(() => props.show && !taskUnavailable.value && !taskBusy.value && Boolean(selectedSpace.value) && !saving.value) let session = 0 +let expectedRevision: number | undefined watch([() => props.show, () => props.task?.id], () => { session += 1 + expectedRevision = props.task?.metadataRevision selectedSpaceId.value = null saving.value = false -}, { flush: 'sync' }) +}, { flush: 'sync', immediate: true }) onScopeDispose(() => { session += 1 }) @@ -51,7 +53,7 @@ async function confirmMove() { const current = session saving.value = true try { - const moved = await props.moveTaskToSpace(task.id, space.id) + const moved = await props.moveTaskToSpace(task.id, space.id, ...expectedRevision !== undefined ? [expectedRevision] as const : []) if (current !== session) return if (!moved) { diff --git a/apps/buddy/src/modules/tasks/widgets/task-index/DesktopTaskRow.vue b/apps/buddy/src/modules/tasks/widgets/task-index/DesktopTaskRow.vue index 0aebe5932..cb0e50c98 100644 --- a/apps/buddy/src/modules/tasks/widgets/task-index/DesktopTaskRow.vue +++ b/apps/buddy/src/modules/tasks/widgets/task-index/DesktopTaskRow.vue @@ -2,8 +2,8 @@ import type { LocalTaskMark, LocalTaskMarkState } from '@buddy-shared/conversation/taskMarkApi' import type { DropdownOption } from 'naive-ui' import type { TaskActivity } from '../../contracts' - import type { BuddyLocale } from '@/i18n/buddyI18n' + import type { DesktopTaskPinnedDropPosition } from '@/modules/tasks/widgets/task-index/taskPinnedItems' import { Archive20Regular, @@ -14,6 +14,7 @@ import { computed, h, shallowRef, useId, useTemplateRef, watch } from 'vue' import { useBuddyI18n } from '@/i18n/buddyI18n' import DesktopOverflowingLabel from '@/modules/tasks/widgets/task-index/DesktopOverflowingLabel.vue' import DesktopIcon from '@/shared/ui/icon/DesktopIcon.vue' +import BuddyChatActivitySpinner from '../transcript/BuddyChatActivitySpinner.vue' import DesktopTaskActivity from './DesktopTaskActivity.vue' import DesktopTaskMarkSwatch from './DesktopTaskMarkSwatch.vue' import { formatTaskRelativeTime } from './taskRelativeTime' diff --git a/apps/buddy/src/modules/tasks/widgets/task-index/__tests__/DesktopTaskArchiveManager.spec.ts b/apps/buddy/src/modules/tasks/widgets/task-index/__tests__/DesktopTaskArchiveManager.spec.ts index 56dd1669d..3ab5d5b56 100644 --- a/apps/buddy/src/modules/tasks/widgets/task-index/__tests__/DesktopTaskArchiveManager.spec.ts +++ b/apps/buddy/src/modules/tasks/widgets/task-index/__tests__/DesktopTaskArchiveManager.spec.ts @@ -20,7 +20,7 @@ function item(id = 'task', overrides: Partial = {}): Co function space(id: string, name: string, revokedAt: string | null = null): LocalSpace { return { id, name, revokedAt, icon: 'folder', iconColor: 'default', activeRunCount: 0, additionalDirectories: [], primaryDirectory: null, memoryScope: 'space_only', createdAt: '2026-11-01T00:00:00Z', updatedAt: '2026-11-01T00:00:00Z' } } -async function mount(items = [item()], language: 'zh-CN' | 'en-US' = 'zh-CN', spaces: readonly LocalSpace[] = []) { +async function mount(items = [item()], spaces: readonly LocalSpace[] = []) { const ready = shallowRef(true) const list = vi.fn().mockResolvedValue({ items, nextCursor: null }) const remove = vi.fn().mockResolvedValue(true) @@ -43,7 +43,7 @@ async function mount(items = [item()], language: 'zh-CN' | 'en-US' = 'zh-CN', sp } const root = document.createElement('div') document.body.append(root) - const app = createApp({ render: () => h(DesktopTaskArchiveManager, { actions, spaces, language, onView }) }) + const app = createApp({ render: () => h(DesktopTaskArchiveManager, { actions, spaces, language: 'zh-CN', onView }) }) app.mount(root) cleanups.push(() => app.unmount()) await nextTick() @@ -65,15 +65,13 @@ async function openSpaceCleanup(name: string, root: ParentNode) { await vi.waitFor(() => expect(document.querySelector('[data-testid="task-archive-delete-confirm"]')).not.toBeNull()) } -it.each(['zh-CN', 'en-US'] as const)('renders inline with localized dates and keeps viewing separate from restoring in %s', async (language) => { - const f = await mount([item()], language) - expect(f.root.querySelector('[data-testid="task-archive-manager"]')?.tagName).toBe('SECTION') - expect(f.root.querySelector('time')?.textContent).toBe(new Intl.DateTimeFormat(language, { dateStyle: 'medium', timeStyle: 'short' }).format(new Date('2026-11-01T00:00:00Z'))) +it('keeps viewing archived history separate from restoring a task', async () => { + const f = await mount() button('历史任务 task', f.root).click() expect(f.onView.mock.calls).toEqual([['task']]) expect(f.restore).not.toHaveBeenCalled() expect(f.root.querySelector('[data-archive-id="task"]')).not.toBeNull() - button(language === 'zh-CN' ? '取消归档' : 'Unarchive', f.root).click() + button('取消归档', f.root).click() await vi.waitFor(() => expect(f.restore.mock.calls).toEqual([['task', 3]])) }) @@ -118,46 +116,10 @@ it('keeps confirmed IDs and revisions fixed across refresh and reports each dele expect(f.root.querySelector('[data-delete-result="failed"]')?.textContent).toContain('历史任务 two') }) -it('groups tasks by their real space without mislabelling revoked or unresolved spaces as unbound', async () => { - const f = await mount([ - item('active-space', { spaceId: 'space-a' }), - item('removed-space', { spaceId: 'space-old' }), - item('unknown-space', { spaceId: 'space-unknown' }), - item('unbound'), - ], 'zh-CN', [space('space-a', '设计空间'), space('space-old', '旧空间', '2026-11-01T00:00:00Z')]) - expect(f.root.querySelector('[data-archive-space="space-a"]')?.textContent).toContain('设计空间') - const oldSpace = f.root.querySelector('[data-archive-space="space-old"]')! - expect(oldSpace.querySelector('header')?.textContent?.trim()).toBe('旧空间') - expect(oldSpace.querySelector('[data-archive-id="removed-space"]')).not.toBeNull() - expect(button('空间操作:旧空间', oldSpace).disabled).toBe(false) - expect(f.root.querySelector('[data-archive-space="space-unknown"]')?.textContent).toContain('空间信息暂不可用') - expect(f.root.querySelector('[data-archive-space=""]')?.textContent).toContain('未绑定空间') -}) - -it('renders only populated groups and removes a group when its last archived task disappears', async () => { - const first = item('first', { spaceId: 'space-a' }) - const remaining = item('remaining', { spaceId: 'space-b' }) - const f = await mount([first, remaining], 'zh-CN', [ - space('space-a', '设计空间'), - space('space-b', '历史空间', '2026-11-01T00:00:00Z'), - space('space-empty', '没有归档的空间'), - ]) - const groupIds = () => [...f.root.querySelectorAll('[data-archive-space]')].map(group => group.getAttribute('data-archive-space')).sort() - expect(groupIds()).toEqual(['space-a', 'space-b']) - f.list.mockResolvedValue({ items: [remaining], nextCursor: null }) - f.notify() - await vi.waitFor(() => expect(groupIds()).toEqual(['space-b'])) - expect(f.root.querySelector('[data-archive-space="space-b"] [data-archive-id="remaining"]')).not.toBeNull() - f.list.mockResolvedValue({ items: [], nextCursor: null }) - f.notify() - await vi.waitFor(() => expect(f.root.textContent).toContain('没有匹配的任务')) - expect(groupIds()).toEqual([]) -}) - it('previews every archive page in a space and only submits the confirmed immutable targets', async () => { const first = item('visible', { spaceId: 'space-a' }) const second = item('beyond-first-page', { spaceId: 'space-a', metadataRevision: 8 }) - const f = await mount([first], 'zh-CN', [space('space-a', '设计空间')]) + const f = await mount([first], [space('space-a', '设计空间')]) f.list.mockResolvedValueOnce({ items: [first], nextCursor: 'next' }).mockResolvedValueOnce({ items: [second], nextCursor: null }) await openSpaceCleanup('设计空间', f.root) await vi.waitFor(() => expect(document.querySelector('[data-testid="task-archive-delete-confirm"]')?.textContent).toContain('历史任务 beyond-first-page')) @@ -171,21 +133,6 @@ it('previews every archive page in a space and only submits the confirmed immuta await vi.waitFor(() => expect(f.remove.mock.calls).toEqual([['visible', 3], ['beyond-first-page', 8]])) }) -it('passes search to the service and appends cursor pages without claiming a global count', async () => { - const f = await mount() - f.list.mockResolvedValueOnce({ items: [item('matching')], nextCursor: 'next' }) - const input = f.root.querySelector('input')! - input.value = 'matching' - input.dispatchEvent(new Event('input', { bubbles: true })) - await vi.waitFor(() => expect(f.root.querySelector('[data-archive-id="matching"]')).not.toBeNull()) - expect(f.list).toHaveBeenLastCalledWith({ kind: 'managed', query: 'matching', limit: 50 }) - f.list.mockResolvedValueOnce({ items: [item('next')], nextCursor: null }) - button('加载更多', f.root).click() - await vi.waitFor(() => expect(f.root.querySelectorAll('[data-archive-id]')).toHaveLength(2)) - expect(f.list).toHaveBeenLastCalledWith({ kind: 'managed', query: 'matching', limit: 50, cursor: 'next' }) - expect(f.root.textContent).not.toContain('加载更多') -}) - it('distinguishes disconnected and failed reads from an empty archive', async () => { const f = await mount() f.ready.value = false diff --git a/apps/buddy/src/modules/tasks/widgets/task-index/__tests__/DesktopTaskMoveToSpaceDialog.spec.ts b/apps/buddy/src/modules/tasks/widgets/task-index/__tests__/DesktopTaskMoveToSpaceDialog.spec.ts index f5ab71423..f6aced8cb 100644 --- a/apps/buddy/src/modules/tasks/widgets/task-index/__tests__/DesktopTaskMoveToSpaceDialog.spec.ts +++ b/apps/buddy/src/modules/tasks/widgets/task-index/__tests__/DesktopTaskMoveToSpaceDialog.spec.ts @@ -29,9 +29,9 @@ function task(): LocalConversationSummary { return { id: 'task-one', title: 'A task to keep', buddyId: 'buddy-one', activeBranchId: 'branch-one', approvalPolicy: 'manual', executionProfile: 'workspace_write', modelSelection: null, spaceId: null, activity: 'idle', automationOccurrence: null, createdAt: '2026-09-08T00:00:00.000Z', updatedAt: '2026-09-08T00:00:00.000Z', deletedAt: null } } -async function mount() { - const props = shallowReactive({ busy: false, language: 'zh-CN' as const, show: true, spaces: [{ id: 'home', name: 'Home' }, { id: 'project', name: 'Project' }], task: task() as LocalConversationSummary | null }) - const move = vi.fn<(taskId: string, spaceId: string) => Promise>().mockResolvedValue(true) +async function mount(metadataRevision?: number) { + const props = shallowReactive({ busy: false, language: 'zh-CN' as const, show: true, spaces: [{ id: 'home', name: 'Home' }, { id: 'project', name: 'Project' }], task: { ...task(), ...(metadataRevision !== undefined ? { metadataRevision } : {}) } as LocalConversationSummary | null }) + const move = vi.fn<(taskId: string, spaceId: string, expectedRevision?: number) => Promise>().mockResolvedValue(true) const moved: string[] = [] const root = document.createElement('div') document.body.append(root) @@ -60,6 +60,24 @@ async function mount() { } describe('move task into a space dialog', () => { + it('retains the version from opening the dialog after a background task refresh', async () => { + const f = await mount(3) + await f.select('Project') + f.props.task = { ...f.props.task!, metadataRevision: 4 } + await nextTick() + f.move.mockImplementation(async (_id, spaceId, expectedRevision) => { + if (expectedRevision !== f.props.task?.metadataRevision) + return false + f.props.task = { ...f.props.task!, spaceId } + return true + }) + f.button('移入').click() + await vi.waitFor(() => expect(f.move).toHaveBeenCalled()) + expect(f.props.task.spaceId).toBeNull() + expect(f.props.show).toBe(true) + expect(f.moved).toEqual([]) + }) + it('requires a searchable explicit selection and explains the permission and file boundaries', async () => { const f = await mount() expect(f.button('移入').disabled).toBe(true) @@ -92,7 +110,7 @@ describe('move task into a space dialog', () => { expect(f.button('移入').disabled).toBe(true) }) - it.each(['false', 'throw'] as const)('keeps the selected target on %s failure and only retries explicitly', async (failure) => { + it('keeps the selected target on failure and only retries explicitly', async () => { const f = await mount() const pending = deferred() f.move.mockReturnValueOnce(pending.promise) @@ -103,17 +121,14 @@ describe('move task into a space dialog', () => { expect(f.button('移入').disabled).toBe(true) expect(f.button('取消').disabled).toBe(true) expect(f.move.mock.calls).toEqual([['task-one', 'project']]) - if (failure === 'false') - pending.resolve(false) - else - pending.reject(new Error('LEXORA_LOCAL_CHAT_ERROR:CONVERSATION_QUEUE_NOT_EMPTY:0')) + pending.reject(new Error('LEXORA_LOCAL_CHAT_ERROR:CONVERSATION_QUEUE_NOT_EMPTY:0')) await pending.promise.catch(() => {}) await nextTick() expect(f.props.show).toBe(true) expect(f.dialog().textContent).toContain('Project') expect(f.button('移入').disabled).toBe(false) expect(f.moved).toEqual([]) - expect(document.body.textContent).toContain(failure === 'false' ? '移入失败,请重试' : '请先发送或取消待发消息') + expect(document.body.textContent).toContain('请先发送或取消待发消息') f.button('移入').click() await nextTick() expect(f.props.show).toBe(false) @@ -121,50 +136,6 @@ describe('move task into a space dialog', () => { expect(f.move.mock.calls).toEqual([['task-one', 'project'], ['task-one', 'project']]) }) - it('revalidates busy status, task ownership, and target availability', async () => { - const f = await mount() - await f.select('Project') - f.props.task = { ...task(), activity: 'running' } - await nextTick() - expect(f.button('移入').disabled).toBe(true) - expect(f.dialog().textContent).toContain('任务正在执行') - f.props.task = task() - f.props.busy = true - await nextTick() - expect(f.button('移入').disabled).toBe(true) - f.props.busy = false - f.props.task = { ...task(), spaceId: 'another-space' } - await nextTick() - expect(f.button('移入').disabled).toBe(true) - f.props.task = task() - f.props.spaces = f.props.spaces.filter(item => item.id !== 'project') - await nextTick() - expect(f.button('移入').disabled).toBe(true) - expect(f.move.mock.calls).toEqual([]) - }) - - it('disables the selector and confirmation when no target remains', async () => { - const f = await mount() - f.props.spaces = [] - await nextTick() - expect(f.button('移入').disabled).toBe(true) - expect(f.dialog().textContent).toContain('暂无可用空间') - f.button('移入').click() - await nextTick() - expect(f.move.mock.calls).toEqual([]) - }) - - it('keeps the selection when the task and space names update', async () => { - const f = await mount() - await f.select('Project') - f.props.task = { ...task(), title: 'Updated task title' } - f.props.spaces = [{ id: 'project', name: 'Project updated' }] - await nextTick() - expect(f.dialog().textContent).toContain('Updated task title') - expect(f.dialog().textContent).toContain('Project updated') - expect(f.button('移入').disabled).toBe(false) - }) - it('ignores a completed request belonging to a replaced dialog', async () => { const f = await mount() const pending = deferred() diff --git a/apps/buddy/src/modules/tasks/widgets/task-index/useTaskIndexManagement.ts b/apps/buddy/src/modules/tasks/widgets/task-index/useTaskIndexManagement.ts index d67113377..d33de8c98 100644 --- a/apps/buddy/src/modules/tasks/widgets/task-index/useTaskIndexManagement.ts +++ b/apps/buddy/src/modules/tasks/widgets/task-index/useTaskIndexManagement.ts @@ -10,11 +10,11 @@ export interface TaskIndexManagementOptions { spaces: Readonly> getUntitledLabel: () => string onCreateSpace: (input: TaskSpaceInput) => Promise - onDeleteTask: (conversationId: string, expectedRevision: number) => void + onDeleteTask: (conversationId: string, expectedRevision?: number) => void onDeleteSpace: (spaceId: string) => void onNewTask: (spaceId: string) => void onOpenSpaceDirectory: (spaceId: string) => void - onRenameTask: (conversationId: string, title: string) => void + onRenameTask: (conversationId: string, title: string, expectedTitleRevision?: number) => void onUpdateSpace: (input: TaskSpaceInput & { spaceId: string }) => Promise } @@ -48,7 +48,7 @@ export function useTaskIndexManagement(options: TaskIndexManagementOptions) { } function requestTaskDelete(conversation: LocalConversation) { - taskDeleteTarget.value = { ...conversation } + taskDeleteTarget.value = conversation } function confirmTaskRename() { @@ -56,15 +56,14 @@ export function useTaskIndexManagement(options: TaskIndexManagementOptions) { const title = taskTitleDraft.value.trim() if (!target || !title) return - options.onRenameTask(target.id, title) + options.onRenameTask(target.id, title, target.titleRevision) taskRenameTarget.value = null } function confirmTaskDelete() { - const target = taskDeleteTarget.value - if (!target || target.metadataRevision === undefined) + if (!taskDeleteTarget.value) return - options.onDeleteTask(target.id, target.metadataRevision) + options.onDeleteTask(taskDeleteTarget.value.id, taskDeleteTarget.value.metadataRevision) taskDeleteTarget.value = null } diff --git a/apps/buddy/src/modules/tasks/widgets/transcript/BuddyChatActionToolbar.vue b/apps/buddy/src/modules/tasks/widgets/transcript/BuddyChatActionToolbar.vue index 04a4933e4..60a5b49ef 100644 --- a/apps/buddy/src/modules/tasks/widgets/transcript/BuddyChatActionToolbar.vue +++ b/apps/buddy/src/modules/tasks/widgets/transcript/BuddyChatActionToolbar.vue @@ -146,7 +146,7 @@ async function copyContent() {