@vybestack/llxprt-code-core 0.11.0 → 0.12.0-nightly.260910.2aac6841e
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/prompt-config/defaults/default-prompts.json +3 -3
- package/dist/src/config/agentClientLifecycle.d.ts +4 -3
- package/dist/src/config/agentClientLifecycle.js +27 -14
- package/dist/src/config/agentClientLifecycle.js.map +1 -1
- package/dist/src/config/config.d.ts +2 -5
- package/dist/src/config/config.js +10 -23
- package/dist/src/config/config.js.map +1 -1
- package/dist/src/config/configBase.d.ts +5 -1
- package/dist/src/config/configBase.js +27 -6
- package/dist/src/config/configBase.js.map +1 -1
- package/dist/src/config/configBaseCore.d.ts +26 -8
- package/dist/src/config/configBaseCore.js +30 -26
- package/dist/src/config/configBaseCore.js.map +1 -1
- package/dist/src/config/configMcpRuntime.d.ts +16 -0
- package/dist/src/config/configMcpRuntime.js +31 -0
- package/dist/src/config/configMcpRuntime.js.map +1 -0
- package/dist/src/config/configMediaDefaults.d.ts +10 -0
- package/dist/src/config/configMediaDefaults.js +17 -0
- package/dist/src/config/configMediaDefaults.js.map +1 -0
- package/dist/src/config/configTestHarness.d.ts +4 -0
- package/dist/src/config/configTypes.d.ts +4 -54
- package/dist/src/config/configTypes.js +6 -82
- package/dist/src/config/configTypes.js.map +1 -1
- package/dist/src/config/index.d.ts +1 -2
- package/dist/src/config/index.js +1 -2
- package/dist/src/config/index.js.map +1 -1
- package/dist/src/config/models.d.ts +0 -4
- package/dist/src/config/models.js +0 -4
- package/dist/src/config/models.js.map +1 -1
- package/dist/src/config/toolRegistryFactory.d.ts +2 -2
- package/dist/src/core/clientContract.d.ts +4 -6
- package/dist/src/core/compression/transcriptPathNotice.d.ts +22 -0
- package/dist/src/core/compression/transcriptPathNotice.js +29 -0
- package/dist/src/core/compression/transcriptPathNotice.js.map +1 -0
- package/dist/src/core/contentGenerator.d.ts +2 -5
- package/dist/src/core/contentGenerator.js +10 -24
- package/dist/src/core/contentGenerator.js.map +1 -1
- package/dist/src/core/logger.d.ts +66 -0
- package/dist/src/core/logger.js +345 -30
- package/dist/src/core/logger.js.map +1 -1
- package/dist/src/core/prompts.d.ts +0 -1
- package/dist/src/core/prompts.js +5 -79
- package/dist/src/core/prompts.js.map +1 -1
- package/dist/src/core/turn.d.ts +3 -3
- package/dist/src/hooks/hookTranslator.js +5 -4
- package/dist/src/hooks/hookTranslator.js.map +1 -1
- package/dist/src/index.d.ts +8 -6
- package/dist/src/index.js +8 -6
- package/dist/src/index.js.map +1 -1
- package/dist/src/llm-types/index.d.ts +1 -1
- package/dist/src/llm-types/modelEnvelope.d.ts +2 -1
- package/dist/src/llm-types/modelEnvelope.js +17 -1
- package/dist/src/llm-types/modelEnvelope.js.map +1 -1
- package/dist/src/models/schema.d.ts +40 -40
- package/dist/src/policy/policies/read-only.toml +0 -5
- package/dist/src/prompt-config/TemplateEngine.js +0 -3
- package/dist/src/prompt-config/TemplateEngine.js.map +1 -1
- package/dist/src/prompt-config/defaults/providers/gemini/core.md +0 -2
- package/dist/src/prompt-config/defaults/providers/gemini/models/gemini-2.5-flash/core.md +0 -2
- package/dist/src/prompt-config/defaults/providers/gemini/models/gemini-2.5-flash/gemini-2-5-flash/core.md +0 -2
- package/dist/src/prompt-config/prompt-loader.d.ts +1 -1
- package/dist/src/prompt-config/prompt-loader.js +4 -4
- package/dist/src/prompt-config/prompt-loader.js.map +1 -1
- package/dist/src/prompt-config/types.d.ts +0 -1
- package/dist/src/recording/CheckpointService.d.ts +4 -0
- package/dist/src/recording/CheckpointService.js +67 -46
- package/dist/src/recording/CheckpointService.js.map +1 -1
- package/dist/src/recording/HistoryMutationService.d.ts +1 -1
- package/dist/src/recording/HistoryMutationService.js +27 -4
- package/dist/src/recording/HistoryMutationService.js.map +1 -1
- package/dist/src/recording/RecordingIntegration.d.ts +36 -2
- package/dist/src/recording/RecordingIntegration.js +224 -7
- package/dist/src/recording/RecordingIntegration.js.map +1 -1
- package/dist/src/recording/ReplayEngine.d.ts +8 -10
- package/dist/src/recording/ReplayEngine.js +161 -95
- package/dist/src/recording/ReplayEngine.js.map +1 -1
- package/dist/src/recording/SessionDiscovery.d.ts +4 -2
- package/dist/src/recording/SessionDiscovery.js +13 -5
- package/dist/src/recording/SessionDiscovery.js.map +1 -1
- package/dist/src/recording/SessionRecordingService.d.ts +23 -3
- package/dist/src/recording/SessionRecordingService.js +209 -51
- package/dist/src/recording/SessionRecordingService.js.map +1 -1
- package/dist/src/recording/SessionTransitionService.d.ts +7 -0
- package/dist/src/recording/SessionTransitionService.js +66 -13
- package/dist/src/recording/SessionTransitionService.js.map +1 -1
- package/dist/src/recording/index.d.ts +1 -0
- package/dist/src/recording/index.js +1 -0
- package/dist/src/recording/index.js.map +1 -1
- package/dist/src/recording/janitor/cleanupTypes.d.ts +3 -0
- package/dist/src/recording/janitor/cleanupTypes.js.map +1 -1
- package/dist/src/recording/janitor/mediaReclamation.d.ts +23 -0
- package/dist/src/recording/janitor/mediaReclamation.js +195 -0
- package/dist/src/recording/janitor/mediaReclamation.js.map +1 -0
- package/dist/src/recording/janitor/sessionJanitor.js +47 -1
- package/dist/src/recording/janitor/sessionJanitor.js.map +1 -1
- package/dist/src/recording/janitor/sessionScanner.d.ts +12 -9
- package/dist/src/recording/janitor/sessionScanner.js +248 -193
- package/dist/src/recording/janitor/sessionScanner.js.map +1 -1
- package/dist/src/recording/replay-test-helpers.d.ts +10 -1
- package/dist/src/recording/replay-test-helpers.js +24 -2
- package/dist/src/recording/replay-test-helpers.js.map +1 -1
- package/dist/src/recording/replayCheckpointMetadata.d.ts +28 -0
- package/dist/src/recording/replayCheckpointMetadata.js +96 -0
- package/dist/src/recording/replayCheckpointMetadata.js.map +1 -0
- package/dist/src/recording/replayErrorFormatting.d.ts +6 -0
- package/dist/src/recording/replayErrorFormatting.js +38 -0
- package/dist/src/recording/replayErrorFormatting.js.map +1 -0
- package/dist/src/recording/resumeSession.d.ts +3 -0
- package/dist/src/recording/resumeSession.js +7 -1
- package/dist/src/recording/resumeSession.js.map +1 -1
- package/dist/src/recording/semanticMediaPurgeReplayValidation.d.ts +14 -0
- package/dist/src/recording/semanticMediaPurgeReplayValidation.js +24 -0
- package/dist/src/recording/semanticMediaPurgeReplayValidation.js.map +1 -0
- package/dist/src/recording/session-media-package-blobs.d.ts +8 -0
- package/dist/src/recording/session-media-package-blobs.js +72 -0
- package/dist/src/recording/session-media-package-blobs.js.map +1 -0
- package/dist/src/recording/session-media-package-import.d.ts +26 -0
- package/dist/src/recording/session-media-package-import.js +124 -0
- package/dist/src/recording/session-media-package-import.js.map +1 -0
- package/dist/src/recording/session-media-package-state.d.ts +19 -0
- package/dist/src/recording/session-media-package-state.js +56 -0
- package/dist/src/recording/session-media-package-state.js.map +1 -0
- package/dist/src/recording/session-media-package-validation.d.ts +59 -0
- package/dist/src/recording/session-media-package-validation.js +286 -0
- package/dist/src/recording/session-media-package-validation.js.map +1 -0
- package/dist/src/recording/session-media-package-writer.d.ts +18 -0
- package/dist/src/recording/session-media-package-writer.js +70 -0
- package/dist/src/recording/session-media-package-writer.js.map +1 -0
- package/dist/src/recording/session-media-package.d.ts +27 -0
- package/dist/src/recording/session-media-package.js +366 -0
- package/dist/src/recording/session-media-package.js.map +1 -0
- package/dist/src/recording/types.d.ts +34 -2
- package/dist/src/recording/types.js.map +1 -1
- package/dist/src/runtime/AgentRuntimeContext.d.ts +22 -5
- package/dist/src/runtime/AgentRuntimeLoader.d.ts +6 -0
- package/dist/src/runtime/AgentRuntimeLoader.js +25 -6
- package/dist/src/runtime/AgentRuntimeLoader.js.map +1 -1
- package/dist/src/runtime/contracts/PromptEstimation.d.ts +1 -0
- package/dist/src/runtime/contracts/PromptEstimation.js.map +1 -1
- package/dist/src/runtime/contracts/RuntimeProvider.d.ts +3 -2
- package/dist/src/runtime/contracts/RuntimeProviderManager.d.ts +0 -2
- package/dist/src/runtime/createAgentRuntimeContext.js +28 -1
- package/dist/src/runtime/createAgentRuntimeContext.js.map +1 -1
- package/dist/src/runtime/providerRuntimeContext.d.ts +12 -0
- package/dist/src/runtime/providerRuntimeContext.js +3 -0
- package/dist/src/runtime/providerRuntimeContext.js.map +1 -1
- package/dist/src/runtime/runtimeAdapters.js +2 -2
- package/dist/src/runtime/runtimeAdapters.js.map +1 -1
- package/dist/src/services/history/ContentConverters.d.ts +11 -7
- package/dist/src/services/history/ContentConverters.js +11 -7
- package/dist/src/services/history/ContentConverters.js.map +1 -1
- package/dist/src/services/history/HistoryService.d.ts +18 -180
- package/dist/src/services/history/HistoryService.js +53 -523
- package/dist/src/services/history/HistoryService.js.map +1 -1
- package/dist/src/services/history/HistoryServiceCore.d.ts +278 -0
- package/dist/src/services/history/HistoryServiceCore.js +727 -0
- package/dist/src/services/history/HistoryServiceCore.js.map +1 -0
- package/dist/src/services/history/IContent.d.ts +118 -16
- package/dist/src/services/history/IContent.js +284 -4
- package/dist/src/services/history/IContent.js.map +1 -1
- package/dist/src/services/history/contentSize.js +22 -2
- package/dist/src/services/history/contentSize.js.map +1 -1
- package/dist/src/services/history/historyChronology.d.ts +7 -0
- package/dist/src/services/history/historyChronology.js +35 -3
- package/dist/src/services/history/historyChronology.js.map +1 -1
- package/dist/src/services/history/historyCloneUtils.d.ts +5 -4
- package/dist/src/services/history/historyCloneUtils.js +23 -16
- package/dist/src/services/history/historyCloneUtils.js.map +1 -1
- package/dist/src/services/history/historyCompressionQueue.d.ts +33 -0
- package/dist/src/services/history/historyCompressionQueue.js +81 -0
- package/dist/src/services/history/historyCompressionQueue.js.map +1 -0
- package/dist/src/services/history/historyEventTypes.d.ts +3 -0
- package/dist/src/services/history/historyMutationFailure.d.ts +15 -0
- package/dist/src/services/history/historyMutationFailure.js +22 -0
- package/dist/src/services/history/historyMutationFailure.js.map +1 -1
- package/dist/src/services/history/historyProviderPipeline.d.ts +1 -1
- package/dist/src/services/history/historyProviderPipeline.js +15 -5
- package/dist/src/services/history/historyProviderPipeline.js.map +1 -1
- package/dist/src/services/history/historyTokenEstimation.js +10 -6
- package/dist/src/services/history/historyTokenEstimation.js.map +1 -1
- package/dist/src/services/history/historyToolNormalization.d.ts +2 -0
- package/dist/src/services/history/historyToolNormalization.js +26 -9
- package/dist/src/services/history/historyToolNormalization.js.map +1 -1
- package/dist/src/services/history/provider-file-binding.d.ts +11 -0
- package/dist/src/services/history/provider-file-binding.js +70 -0
- package/dist/src/services/history/provider-file-binding.js.map +1 -0
- package/dist/src/services/history/semantic-media-purge.d.ts +66 -0
- package/dist/src/services/history/semantic-media-purge.js +404 -0
- package/dist/src/services/history/semantic-media-purge.js.map +1 -0
- package/dist/src/storage/SessionPersistenceService.d.ts +53 -2
- package/dist/src/storage/SessionPersistenceService.js +435 -23
- package/dist/src/storage/SessionPersistenceService.js.map +1 -1
- package/dist/src/storage/history-media-ownership.d.ts +38 -0
- package/dist/src/storage/history-media-ownership.js +136 -0
- package/dist/src/storage/history-media-ownership.js.map +1 -0
- package/dist/src/storage/local-media-store-child.d.ts +6 -0
- package/dist/src/storage/local-media-store-child.js +140 -0
- package/dist/src/storage/local-media-store-child.js.map +1 -0
- package/dist/src/storage/local-media-store-files.d.ts +44 -0
- package/dist/src/storage/local-media-store-files.js +460 -0
- package/dist/src/storage/local-media-store-files.js.map +1 -0
- package/dist/src/storage/local-media-store-lock-recovery.d.ts +15 -0
- package/dist/src/storage/local-media-store-lock-recovery.js +112 -0
- package/dist/src/storage/local-media-store-lock-recovery.js.map +1 -0
- package/dist/src/storage/local-media-store-persistence.d.ts +48 -0
- package/dist/src/storage/local-media-store-persistence.js +443 -0
- package/dist/src/storage/local-media-store-persistence.js.map +1 -0
- package/dist/src/storage/local-media-store-quota.d.ts +6 -0
- package/dist/src/storage/local-media-store-quota.js +66 -0
- package/dist/src/storage/local-media-store-quota.js.map +1 -0
- package/dist/src/storage/local-media-store-reservations.d.ts +55 -0
- package/dist/src/storage/local-media-store-reservations.js +377 -0
- package/dist/src/storage/local-media-store-reservations.js.map +1 -0
- package/dist/src/storage/local-media-store-types.d.ts +110 -0
- package/dist/src/storage/local-media-store-types.js +35 -0
- package/dist/src/storage/local-media-store-types.js.map +1 -0
- package/dist/src/storage/local-media-store-validation.d.ts +34 -0
- package/dist/src/storage/local-media-store-validation.js +449 -0
- package/dist/src/storage/local-media-store-validation.js.map +1 -0
- package/dist/src/storage/local-media-store.d.ts +47 -0
- package/dist/src/storage/local-media-store.js +353 -0
- package/dist/src/storage/local-media-store.js.map +1 -0
- package/dist/src/storage/media-admission-service.d.ts +49 -0
- package/dist/src/storage/media-admission-service.js +333 -0
- package/dist/src/storage/media-admission-service.js.map +1 -0
- package/dist/src/storage/media-lifecycle-metrics.d.ts +75 -0
- package/dist/src/storage/media-lifecycle-metrics.js +105 -0
- package/dist/src/storage/media-lifecycle-metrics.js.map +1 -0
- package/dist/src/storage/media-reference-lifecycle.d.ts +16 -0
- package/dist/src/storage/media-reference-lifecycle.js +84 -0
- package/dist/src/storage/media-reference-lifecycle.js.map +1 -0
- package/dist/src/storage/request-media-resolver.d.ts +67 -0
- package/dist/src/storage/request-media-resolver.js +325 -0
- package/dist/src/storage/request-media-resolver.js.map +1 -0
- package/dist/src/storage/session-media-service-factories.d.ts +19 -0
- package/dist/src/storage/session-media-service-factories.js +34 -0
- package/dist/src/storage/session-media-service-factories.js.map +1 -0
- package/dist/src/storage/session-persistence-helpers.d.ts +10 -0
- package/dist/src/storage/session-persistence-helpers.js +46 -0
- package/dist/src/storage/session-persistence-helpers.js.map +1 -0
- package/dist/src/test-utils/config.d.ts +2 -0
- package/dist/src/test-utils/config.js +5 -5
- package/dist/src/test-utils/config.js.map +1 -1
- package/dist/src/test-utils/runtime.d.ts +2 -0
- package/dist/src/test-utils/runtime.js +1 -2
- package/dist/src/test-utils/runtime.js.map +1 -1
- package/dist/src/tools-adapters/CoreShellToolHostAdapter.js +1 -9
- package/dist/src/tools-adapters/CoreShellToolHostAdapter.js.map +1 -1
- package/dist/src/tools-adapters/CoreSkillServiceAdapter.d.ts +0 -2
- package/dist/src/tools-adapters/CoreSkillServiceAdapter.js +0 -17
- package/dist/src/tools-adapters/CoreSkillServiceAdapter.js.map +1 -1
- package/dist/src/tools-adapters/CoreToolHostAdapter.d.ts +0 -21
- package/dist/src/tools-adapters/CoreToolHostAdapter.js +0 -8
- package/dist/src/tools-adapters/CoreToolHostAdapter.js.map +1 -1
- package/dist/src/utils/environmentContext.d.ts +8 -9
- package/dist/src/utils/environmentContext.js +11 -22
- package/dist/src/utils/environmentContext.js.map +1 -1
- package/dist/src/utils/errorParsing.d.ts +1 -2
- package/dist/src/utils/errorParsing.js +22 -41
- package/dist/src/utils/errorParsing.js.map +1 -1
- package/dist/src/utils/generateContentResponseUtilities.js +12 -0
- package/dist/src/utils/generateContentResponseUtilities.js.map +1 -1
- package/dist/src/utils/summarizer.d.ts +1 -2
- package/dist/src/utils/summarizer.js +6 -5
- package/dist/src/utils/summarizer.js.map +1 -1
- package/dist/src/utils/tool-utils.js +7 -1
- package/dist/src/utils/tool-utils.js.map +1 -1
- package/index.ts +0 -4
- package/package.json +38 -30
- package/src/config/agentClientLifecycle.ts +53 -22
- package/src/config/config.ts +11 -26
- package/src/config/configBase.ts +46 -7
- package/src/config/configBaseCore.ts +38 -36
- package/src/config/configMcpRuntime.ts +46 -0
- package/src/config/configMediaDefaults.ts +19 -0
- package/src/config/configTypes.ts +13 -53
- package/src/config/index.ts +1 -4
- package/src/config/models.ts +0 -6
- package/src/config/toolRegistryFactory.ts +2 -2
- package/src/core/clientContract.ts +4 -6
- package/src/core/compression/transcriptPathNotice.ts +31 -0
- package/src/core/contentGenerator.ts +15 -39
- package/src/core/logger.ts +370 -39
- package/src/core/prompts.ts +4 -114
- package/src/core/turn.ts +3 -3
- package/src/hooks/hookTranslator.ts +6 -4
- package/src/index.ts +7 -7
- package/src/llm-types/index.ts +5 -0
- package/src/llm-types/modelEnvelope.ts +26 -2
- package/src/policy/policies/read-only.toml +0 -5
- package/src/prompt-config/TemplateEngine.ts +0 -3
- package/src/prompt-config/defaults/providers/gemini/core.md +0 -2
- package/src/prompt-config/defaults/providers/gemini/models/gemini-2.5-flash/core.md +0 -2
- package/src/prompt-config/defaults/providers/gemini/models/gemini-2.5-flash/gemini-2-5-flash/core.md +0 -2
- package/src/prompt-config/prompt-loader.ts +7 -8
- package/src/prompt-config/types.ts +0 -1
- package/src/recording/CheckpointService.ts +110 -45
- package/src/recording/HistoryMutationService.ts +28 -3
- package/src/recording/RecordingIntegration.ts +258 -11
- package/src/recording/ReplayEngine.ts +220 -110
- package/src/recording/SessionDiscovery.ts +30 -4
- package/src/recording/SessionRecordingService.ts +253 -55
- package/src/recording/SessionTransitionService.ts +75 -12
- package/src/recording/index.ts +7 -0
- package/src/recording/janitor/cleanupTypes.ts +4 -0
- package/src/recording/janitor/mediaReclamation.ts +295 -0
- package/src/recording/janitor/sessionJanitor.ts +54 -1
- package/src/recording/janitor/sessionScanner.ts +319 -227
- package/src/recording/replay-test-helpers.ts +33 -2
- package/src/recording/replayCheckpointMetadata.ts +115 -0
- package/src/recording/replayErrorFormatting.ts +43 -0
- package/src/recording/resumeSession.ts +10 -0
- package/src/recording/semanticMediaPurgeReplayValidation.ts +35 -0
- package/src/recording/session-media-package-blobs.ts +116 -0
- package/src/recording/session-media-package-import.ts +189 -0
- package/src/recording/session-media-package-state.ts +120 -0
- package/src/recording/session-media-package-validation.ts +400 -0
- package/src/recording/session-media-package-writer.ts +117 -0
- package/src/recording/session-media-package.ts +692 -0
- package/src/recording/types.ts +37 -2
- package/src/runtime/AgentRuntimeContext.ts +28 -5
- package/src/runtime/AgentRuntimeLoader.ts +32 -7
- package/src/runtime/contracts/PromptEstimation.ts +1 -0
- package/src/runtime/contracts/RuntimeProvider.ts +3 -8
- package/src/runtime/contracts/RuntimeProviderManager.ts +0 -2
- package/src/runtime/createAgentRuntimeContext.ts +43 -1
- package/src/runtime/providerRuntimeContext.ts +22 -0
- package/src/runtime/runtimeAdapters.ts +2 -2
- package/src/services/history/ContentConverters.ts +11 -7
- package/src/services/history/HistoryService.ts +71 -696
- package/src/services/history/HistoryServiceCore.ts +1055 -0
- package/src/services/history/IContent.ts +460 -20
- package/src/services/history/contentSize.ts +22 -2
- package/src/services/history/historyChronology.ts +53 -3
- package/src/services/history/historyCloneUtils.ts +26 -22
- package/src/services/history/historyCompressionQueue.ts +102 -0
- package/src/services/history/historyEventTypes.ts +9 -0
- package/src/services/history/historyMutationFailure.ts +33 -0
- package/src/services/history/historyProviderPipeline.ts +19 -5
- package/src/services/history/historyTokenEstimation.ts +9 -5
- package/src/services/history/historyToolNormalization.ts +43 -11
- package/src/services/history/provider-file-binding.ts +115 -0
- package/src/services/history/semantic-media-purge.ts +593 -0
- package/src/storage/SessionPersistenceService.ts +667 -30
- package/src/storage/history-media-ownership.ts +185 -0
- package/src/storage/local-media-store-child.ts +163 -0
- package/src/storage/local-media-store-files.ts +643 -0
- package/src/storage/local-media-store-lock-recovery.ts +159 -0
- package/src/storage/local-media-store-persistence.ts +737 -0
- package/src/storage/local-media-store-quota.ts +111 -0
- package/src/storage/local-media-store-reservations.ts +485 -0
- package/src/storage/local-media-store-types.ts +161 -0
- package/src/storage/local-media-store-validation.ts +643 -0
- package/src/storage/local-media-store.ts +696 -0
- package/src/storage/media-admission-service.ts +512 -0
- package/src/storage/media-lifecycle-metrics.ts +228 -0
- package/src/storage/media-reference-lifecycle.ts +164 -0
- package/src/storage/request-media-resolver.ts +491 -0
- package/src/storage/session-media-service-factories.ts +48 -0
- package/src/storage/session-persistence-helpers.ts +85 -0
- package/src/test-utils/config.ts +7 -5
- package/src/test-utils/runtime.ts +3 -2
- package/src/tools-adapters/CoreShellToolHostAdapter.ts +1 -11
- package/src/tools-adapters/CoreSkillServiceAdapter.ts +0 -23
- package/src/tools-adapters/CoreToolHostAdapter.ts +0 -36
- package/src/utils/environmentContext.ts +11 -30
- package/src/utils/errorParsing.ts +19 -69
- package/src/utils/generateContentResponseUtilities.ts +28 -1
- package/src/utils/summarizer.ts +8 -10
- package/src/utils/tool-utils.ts +7 -1
- package/dist/src/code_assist/codeAssist.d.ts +0 -19
- package/dist/src/code_assist/codeAssist.js +0 -41
- package/dist/src/code_assist/codeAssist.js.map +0 -1
- package/dist/src/code_assist/contentGeneratorAdapters.d.ts +0 -49
- package/dist/src/code_assist/contentGeneratorAdapters.js +0 -197
- package/dist/src/code_assist/contentGeneratorAdapters.js.map +0 -1
- package/dist/src/code_assist/converter.d.ts +0 -74
- package/dist/src/code_assist/converter.js +0 -158
- package/dist/src/code_assist/converter.js.map +0 -1
- package/dist/src/code_assist/googleGenAIWrapper.d.ts +0 -27
- package/dist/src/code_assist/googleGenAIWrapper.js +0 -46
- package/dist/src/code_assist/googleGenAIWrapper.js.map +0 -1
- package/dist/src/code_assist/oauth-credential-storage.d.ts +0 -30
- package/dist/src/code_assist/oauth-credential-storage.js +0 -135
- package/dist/src/code_assist/oauth-credential-storage.js.map +0 -1
- package/dist/src/code_assist/server.d.ts +0 -44
- package/dist/src/code_assist/server.js +0 -154
- package/dist/src/code_assist/server.js.map +0 -1
- package/dist/src/code_assist/setup.d.ts +0 -20
- package/dist/src/code_assist/setup.js +0 -126
- package/dist/src/code_assist/setup.js.map +0 -1
- package/dist/src/code_assist/types.d.ts +0 -161
- package/dist/src/code_assist/types.js +0 -46
- package/dist/src/code_assist/types.js.map +0 -1
- package/dist/src/utils/getFolderStructure.d.ts +0 -31
- package/dist/src/utils/getFolderStructure.js +0 -240
- package/dist/src/utils/getFolderStructure.js.map +0 -1
- package/dist/src/utils/installationManager.d.ts +0 -16
- package/dist/src/utils/installationManager.js +0 -51
- package/dist/src/utils/installationManager.js.map +0 -1
- package/dist/src/utils/llm-edit-fixer.d.ts +0 -25
- package/dist/src/utils/llm-edit-fixer.js +0 -120
- package/dist/src/utils/llm-edit-fixer.js.map +0 -1
- package/src/code_assist/codeAssist.ts +0 -59
- package/src/code_assist/contentGeneratorAdapters.ts +0 -275
- package/src/code_assist/converter.ts +0 -288
- package/src/code_assist/googleGenAIWrapper.ts +0 -90
- package/src/code_assist/oauth-credential-storage.ts +0 -171
- package/src/code_assist/server.ts +0 -263
- package/src/code_assist/setup.ts +0 -189
- package/src/code_assist/types.ts +0 -201
- package/src/utils/getFolderStructure.ts +0 -382
- package/src/utils/installationManager.ts +0 -59
- package/src/utils/llm-edit-fixer.ts +0 -157
|
@@ -0,0 +1,737 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Vybestack LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
8
|
+
import {
|
|
9
|
+
lstat,
|
|
10
|
+
open,
|
|
11
|
+
readFile,
|
|
12
|
+
rename,
|
|
13
|
+
unlink,
|
|
14
|
+
utimes,
|
|
15
|
+
} from 'node:fs/promises';
|
|
16
|
+
import { hostname } from 'node:os';
|
|
17
|
+
import { isAbsolute, join } from 'node:path';
|
|
18
|
+
import {
|
|
19
|
+
isMediaReferenceBlock,
|
|
20
|
+
type MediaReferenceBlock,
|
|
21
|
+
type MediaStoredObject,
|
|
22
|
+
} from '../services/history/IContent.js';
|
|
23
|
+
import { LocalMediaStoreFiles } from './local-media-store-files.js';
|
|
24
|
+
import { recoverStaleStoreLock } from './local-media-store-lock-recovery.js';
|
|
25
|
+
import { measureStoredBytesWithinBound } from './local-media-store-quota.js';
|
|
26
|
+
import { LocalMediaStoreReservations } from './local-media-store-reservations.js';
|
|
27
|
+
import {
|
|
28
|
+
MediaObjectCorruptError,
|
|
29
|
+
MediaObjectHashMismatchError,
|
|
30
|
+
MediaObjectMissingError,
|
|
31
|
+
MediaStoreError,
|
|
32
|
+
type LocalMediaStoreOptions,
|
|
33
|
+
type MediaReclamationResult,
|
|
34
|
+
type PublishedMediaObjectPath,
|
|
35
|
+
type ReservationRecord,
|
|
36
|
+
type StoreLockOwner,
|
|
37
|
+
} from './local-media-store-types.js';
|
|
38
|
+
import {
|
|
39
|
+
LOCK_VERSION,
|
|
40
|
+
RESERVATION_VERSION,
|
|
41
|
+
contentIdFor,
|
|
42
|
+
hasErrnoCode,
|
|
43
|
+
isNonNegativeSafeInteger,
|
|
44
|
+
parseStoreLockOwner,
|
|
45
|
+
requiredObjects,
|
|
46
|
+
validateContentId,
|
|
47
|
+
validateOwnerId,
|
|
48
|
+
validatePositiveDuration,
|
|
49
|
+
validateStoredObjectMetadata,
|
|
50
|
+
wrapError,
|
|
51
|
+
} from './local-media-store-validation.js';
|
|
52
|
+
|
|
53
|
+
const FILE_MODE = 0o600;
|
|
54
|
+
const DEFAULT_LOCK_TIMEOUT_MS = 2_000;
|
|
55
|
+
const DEFAULT_STALE_LOCK_MS = 30_000;
|
|
56
|
+
const DEFAULT_RESERVATION_LEASE_MS = 30_000;
|
|
57
|
+
const DEFAULT_QUOTA_SCAN_MAX_ENTRIES = 10_000;
|
|
58
|
+
const LOCK_RETRY_MS = 10;
|
|
59
|
+
|
|
60
|
+
function sleep(milliseconds: number): Promise<void> {
|
|
61
|
+
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface LeaseHeartbeat {
|
|
65
|
+
stop(): Promise<unknown | undefined>;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export class LocalMediaStorePersistence extends LocalMediaStoreReservations {
|
|
69
|
+
readonly rootDirectory: string;
|
|
70
|
+
readonly quotaBytes: number;
|
|
71
|
+
protected override readonly files: LocalMediaStoreFiles;
|
|
72
|
+
private readonly lockTimeoutMs: number;
|
|
73
|
+
private readonly staleLockMs: number;
|
|
74
|
+
protected override readonly reservationLeaseMs: number;
|
|
75
|
+
private readonly quotaScanMaxEntries: number;
|
|
76
|
+
|
|
77
|
+
constructor(options: LocalMediaStoreOptions) {
|
|
78
|
+
super();
|
|
79
|
+
this.validateOptions(options);
|
|
80
|
+
const lockTimeoutMs = options.lockTimeoutMs ?? DEFAULT_LOCK_TIMEOUT_MS;
|
|
81
|
+
const staleLockMs = options.staleLockMs ?? DEFAULT_STALE_LOCK_MS;
|
|
82
|
+
const reservationLeaseMs =
|
|
83
|
+
options.reservationLeaseMs ?? DEFAULT_RESERVATION_LEASE_MS;
|
|
84
|
+
const quotaScanMaxEntries =
|
|
85
|
+
options.quotaScanMaxEntries ?? DEFAULT_QUOTA_SCAN_MAX_ENTRIES;
|
|
86
|
+
validatePositiveDuration(lockTimeoutMs, 'Lock timeout');
|
|
87
|
+
validatePositiveDuration(staleLockMs, 'Stale lock duration');
|
|
88
|
+
validatePositiveDuration(reservationLeaseMs, 'Reservation lease duration');
|
|
89
|
+
validatePositiveDuration(quotaScanMaxEntries, 'Quota scan entry bound');
|
|
90
|
+
this.rootDirectory = options.rootDirectory;
|
|
91
|
+
this.quotaBytes = options.quotaBytes;
|
|
92
|
+
this.files = new LocalMediaStoreFiles(
|
|
93
|
+
options.rootDirectory,
|
|
94
|
+
options.fileOperations,
|
|
95
|
+
);
|
|
96
|
+
this.lockTimeoutMs = lockTimeoutMs;
|
|
97
|
+
this.staleLockMs = staleLockMs;
|
|
98
|
+
this.reservationLeaseMs = reservationLeaseMs;
|
|
99
|
+
this.quotaScanMaxEntries = quotaScanMaxEntries;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async runExclusive<T>(
|
|
103
|
+
operation: string,
|
|
104
|
+
contentId: string | undefined,
|
|
105
|
+
work: () => Promise<T>,
|
|
106
|
+
): Promise<T> {
|
|
107
|
+
this.assertOpen(operation, contentId);
|
|
108
|
+
await this.files.ensureDirectories(contentId);
|
|
109
|
+
this.assertInstanceLeaseHealthy(contentId);
|
|
110
|
+
const owner = await this.acquireStoreLock(operation, contentId);
|
|
111
|
+
const heartbeat = this.startStoreLockHeartbeat(owner, operation, contentId);
|
|
112
|
+
let outcome:
|
|
113
|
+
| { readonly ok: true; readonly value: T }
|
|
114
|
+
| { readonly ok: false; readonly error: unknown };
|
|
115
|
+
try {
|
|
116
|
+
outcome = { ok: true, value: await work() };
|
|
117
|
+
} catch (error) {
|
|
118
|
+
outcome = { ok: false, error };
|
|
119
|
+
}
|
|
120
|
+
const heartbeatFailure = await heartbeat.stop();
|
|
121
|
+
if (heartbeatFailure !== undefined) {
|
|
122
|
+
outcome = {
|
|
123
|
+
ok: false,
|
|
124
|
+
error: outcome.ok
|
|
125
|
+
? heartbeatFailure
|
|
126
|
+
: new AggregateError([outcome.error, heartbeatFailure]),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
await this.releaseAfterWork(owner, operation, contentId, outcome);
|
|
130
|
+
if (!outcome.ok) throw outcome.error;
|
|
131
|
+
return outcome.value;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async readObjectVerified(reference: MediaStoredObject): Promise<Uint8Array> {
|
|
135
|
+
return this.runExclusive('read verified', reference.contentId, () =>
|
|
136
|
+
this.readObjectVerifiedUnlocked(reference, 'read verified'),
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async readObjectVerifiedUnlocked(
|
|
141
|
+
reference: MediaStoredObject,
|
|
142
|
+
operation: string,
|
|
143
|
+
): Promise<Uint8Array> {
|
|
144
|
+
validateStoredObjectMetadata(reference, operation);
|
|
145
|
+
const objectPath = this.files.objectPath(reference.contentId);
|
|
146
|
+
const storedBytes = await this.files.readRegularFile(
|
|
147
|
+
objectPath,
|
|
148
|
+
reference.contentId,
|
|
149
|
+
operation,
|
|
150
|
+
);
|
|
151
|
+
if (storedBytes.byteLength !== reference.byteLength) {
|
|
152
|
+
throw new MediaObjectCorruptError(
|
|
153
|
+
operation,
|
|
154
|
+
reference.contentId,
|
|
155
|
+
new Error(
|
|
156
|
+
`Stored byte length ${storedBytes.byteLength} does not match ${reference.byteLength}`,
|
|
157
|
+
),
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
if (contentIdFor(storedBytes) !== reference.contentId) {
|
|
161
|
+
throw new MediaObjectHashMismatchError(operation, reference.contentId);
|
|
162
|
+
}
|
|
163
|
+
return storedBytes;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async objectExistsUnlocked(contentId: string): Promise<boolean> {
|
|
167
|
+
try {
|
|
168
|
+
const objectStat = await lstat(this.files.objectPath(contentId));
|
|
169
|
+
if (!objectStat.isFile()) {
|
|
170
|
+
throw new MediaObjectCorruptError(
|
|
171
|
+
'inspect admitted object',
|
|
172
|
+
contentId,
|
|
173
|
+
new Error('Stored object is not a regular file'),
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
return true;
|
|
177
|
+
} catch (error) {
|
|
178
|
+
if (hasErrnoCode(error, 'ENOENT')) return false;
|
|
179
|
+
throw wrapError('inspect admitted object', contentId, error);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async measureStoredBytesUnlocked(): Promise<number> {
|
|
184
|
+
return measureStoredBytesWithinBound(
|
|
185
|
+
this.files.objectDirectory,
|
|
186
|
+
this.quotaScanMaxEntries,
|
|
187
|
+
this.lockTimeoutMs,
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
enforceQuota(
|
|
192
|
+
storedBytes: number,
|
|
193
|
+
additionalBytes: number,
|
|
194
|
+
contentId: string,
|
|
195
|
+
): void {
|
|
196
|
+
if (additionalBytes > this.quotaBytes - storedBytes) {
|
|
197
|
+
throw new MediaStoreError(
|
|
198
|
+
'enforce spool quota',
|
|
199
|
+
contentId,
|
|
200
|
+
new Error(
|
|
201
|
+
`Admission exceeds quota: ${storedBytes} + ${additionalBytes} > ${this.quotaBytes}`,
|
|
202
|
+
),
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async publishObjectBytes(
|
|
208
|
+
bytes: Uint8Array,
|
|
209
|
+
contentId: string,
|
|
210
|
+
operation: string,
|
|
211
|
+
): Promise<string | undefined> {
|
|
212
|
+
return this.files.publishObjectBytes(bytes, contentId, operation);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
async publishObjectFile(
|
|
216
|
+
sourcePath: string,
|
|
217
|
+
contentId: string,
|
|
218
|
+
expectedByteLength: number,
|
|
219
|
+
operation: string,
|
|
220
|
+
): Promise<string | undefined> {
|
|
221
|
+
return this.files.publishObjectFile(
|
|
222
|
+
sourcePath,
|
|
223
|
+
contentId,
|
|
224
|
+
expectedByteLength,
|
|
225
|
+
operation,
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async rollbackPublishedPaths(paths: readonly string[]): Promise<unknown[]> {
|
|
230
|
+
return this.files.rollbackPublishedPaths(paths);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
async rollbackStagedObjects(
|
|
234
|
+
published: readonly PublishedMediaObjectPath[],
|
|
235
|
+
): Promise<void> {
|
|
236
|
+
await this.runExclusive(
|
|
237
|
+
'rollback staged object batch',
|
|
238
|
+
undefined,
|
|
239
|
+
async () => {
|
|
240
|
+
const removable: string[] = [];
|
|
241
|
+
for (const entry of published) {
|
|
242
|
+
if (!(await this.hasReservationsUnlocked(entry.contentId))) {
|
|
243
|
+
removable.push(entry.path);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const failures = await this.files.rollbackPublishedPaths(removable);
|
|
247
|
+
if (failures.length > 0) {
|
|
248
|
+
throw new MediaStoreError(
|
|
249
|
+
'rollback staged object batch',
|
|
250
|
+
undefined,
|
|
251
|
+
new AggregateError(failures),
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
},
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
async getStoredByteLength(): Promise<number> {
|
|
259
|
+
return this.runExclusive('measure spool quota', undefined, () =>
|
|
260
|
+
this.measureStoredBytesUnlocked(),
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
async reserveAndReadVerified(
|
|
265
|
+
reference: MediaReferenceBlock,
|
|
266
|
+
ownerId: string,
|
|
267
|
+
): Promise<Uint8Array> {
|
|
268
|
+
if (!isMediaReferenceBlock(reference)) {
|
|
269
|
+
throw new MediaObjectCorruptError(
|
|
270
|
+
'reserve reference',
|
|
271
|
+
undefined,
|
|
272
|
+
new Error('Malformed media reference'),
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
const contentId = reference.contentId;
|
|
276
|
+
validateOwnerId(ownerId, contentId, 'reserve reference');
|
|
277
|
+
return this.runExclusive('reserve reference', contentId, () =>
|
|
278
|
+
this.reserveUnlocked(reference, ownerId),
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async release(contentId: string, ownerId: string): Promise<void> {
|
|
283
|
+
validateContentId(contentId, 'release reference');
|
|
284
|
+
validateOwnerId(ownerId, contentId, 'release reference');
|
|
285
|
+
await this.runExclusive('release reference', contentId, async () => {
|
|
286
|
+
const ownerDigest = createHash('sha256').update(ownerId).digest('hex');
|
|
287
|
+
const record = await this.readReservationIfPresent(
|
|
288
|
+
contentId,
|
|
289
|
+
ownerDigest,
|
|
290
|
+
);
|
|
291
|
+
if (record === undefined) return;
|
|
292
|
+
for (const relatedContentId of record.relatedContentIds) {
|
|
293
|
+
await this.removeReservation(relatedContentId, ownerDigest);
|
|
294
|
+
}
|
|
295
|
+
await this.files.syncDirectory(this.files.reservationDirectory);
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
async hasReservations(contentId: string): Promise<boolean> {
|
|
300
|
+
validateContentId(contentId, 'inspect references');
|
|
301
|
+
return this.runExclusive('inspect references', contentId, () =>
|
|
302
|
+
this.hasReservationsUnlocked(contentId),
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
async reclaimUnreferenced(
|
|
307
|
+
protectedContentIds: ReadonlySet<string>,
|
|
308
|
+
staleTemporaryBefore: number,
|
|
309
|
+
): Promise<MediaReclamationResult> {
|
|
310
|
+
if (!Number.isFinite(staleTemporaryBefore)) {
|
|
311
|
+
throw new MediaStoreError(
|
|
312
|
+
'reclaim media',
|
|
313
|
+
undefined,
|
|
314
|
+
new Error('Stale temporary cutoff must be finite'),
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
const objectCreatedBefore = Date.now();
|
|
318
|
+
await this.runExclusive('prepare media reclamation', undefined, () =>
|
|
319
|
+
Promise.resolve(),
|
|
320
|
+
);
|
|
321
|
+
const candidates = await this.scanReclamationCandidates(
|
|
322
|
+
protectedContentIds,
|
|
323
|
+
objectCreatedBefore,
|
|
324
|
+
);
|
|
325
|
+
let objectsRemoved = 0;
|
|
326
|
+
for (const candidate of candidates) {
|
|
327
|
+
const removed = await this.runExclusive(
|
|
328
|
+
'reclaim media object',
|
|
329
|
+
candidate.contentId,
|
|
330
|
+
() =>
|
|
331
|
+
this.reclaimObjectUnlocked(
|
|
332
|
+
candidate,
|
|
333
|
+
protectedContentIds,
|
|
334
|
+
objectCreatedBefore,
|
|
335
|
+
),
|
|
336
|
+
);
|
|
337
|
+
if (removed) objectsRemoved += 1;
|
|
338
|
+
}
|
|
339
|
+
const temporaryFilesRemoved = await this.runExclusive(
|
|
340
|
+
'reclaim temporary media',
|
|
341
|
+
undefined,
|
|
342
|
+
() => this.reclaimTemporaryFilesUnlocked(staleTemporaryBefore),
|
|
343
|
+
);
|
|
344
|
+
return { objectsRemoved, temporaryFilesRemoved };
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
private validateOptions(options: LocalMediaStoreOptions): void {
|
|
348
|
+
if (
|
|
349
|
+
!isAbsolute(options.rootDirectory) ||
|
|
350
|
+
options.rootDirectory.length === 0
|
|
351
|
+
) {
|
|
352
|
+
throw new MediaStoreError(
|
|
353
|
+
'initialize',
|
|
354
|
+
undefined,
|
|
355
|
+
new Error('Root directory must be absolute'),
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
if (!isNonNegativeSafeInteger(options.quotaBytes)) {
|
|
359
|
+
throw new MediaStoreError(
|
|
360
|
+
'validate quota',
|
|
361
|
+
undefined,
|
|
362
|
+
new Error('Quota must be a non-negative safe integer'),
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
private startStoreLockHeartbeat(
|
|
368
|
+
owner: StoreLockOwner,
|
|
369
|
+
operation: string,
|
|
370
|
+
contentId: string | undefined,
|
|
371
|
+
): LeaseHeartbeat {
|
|
372
|
+
const intervalMs = Math.max(1, Math.floor(this.staleLockMs / 3));
|
|
373
|
+
let stopped = false;
|
|
374
|
+
let refresh = Promise.resolve();
|
|
375
|
+
let failure: unknown;
|
|
376
|
+
const timer = setInterval(() => {
|
|
377
|
+
if (stopped || failure !== undefined) return;
|
|
378
|
+
refresh = refresh.then(async () => {
|
|
379
|
+
try {
|
|
380
|
+
await this.refreshStoreLock(owner, operation, contentId);
|
|
381
|
+
} catch (error) {
|
|
382
|
+
failure = error;
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
}, intervalMs);
|
|
386
|
+
timer.unref();
|
|
387
|
+
return {
|
|
388
|
+
stop: async (): Promise<unknown | undefined> => {
|
|
389
|
+
stopped = true;
|
|
390
|
+
clearInterval(timer);
|
|
391
|
+
await refresh;
|
|
392
|
+
return failure;
|
|
393
|
+
},
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
private async refreshStoreLock(
|
|
398
|
+
owner: StoreLockOwner,
|
|
399
|
+
operation: string,
|
|
400
|
+
contentId: string | undefined,
|
|
401
|
+
): Promise<void> {
|
|
402
|
+
try {
|
|
403
|
+
const before = await lstat(this.files.lockPath);
|
|
404
|
+
const parsed: unknown = JSON.parse(
|
|
405
|
+
await readFile(this.files.lockPath, 'utf8'),
|
|
406
|
+
);
|
|
407
|
+
const current = parseStoreLockOwner(parsed);
|
|
408
|
+
if (current?.token !== owner.token || before.nlink !== 1) {
|
|
409
|
+
throw new Error('Store lock ownership changed during heartbeat');
|
|
410
|
+
}
|
|
411
|
+
const now = new Date();
|
|
412
|
+
await utimes(this.files.lockPath, now, now);
|
|
413
|
+
const after = await lstat(this.files.lockPath);
|
|
414
|
+
if (
|
|
415
|
+
before.dev !== after.dev ||
|
|
416
|
+
before.ino !== after.ino ||
|
|
417
|
+
after.nlink !== 1
|
|
418
|
+
) {
|
|
419
|
+
throw new Error('Store lock identity changed during heartbeat');
|
|
420
|
+
}
|
|
421
|
+
} catch (error) {
|
|
422
|
+
throw wrapError(
|
|
423
|
+
`heartbeat store lock for ${operation}`,
|
|
424
|
+
contentId,
|
|
425
|
+
error,
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
private async releaseAfterWork<T>(
|
|
431
|
+
owner: StoreLockOwner,
|
|
432
|
+
operation: string,
|
|
433
|
+
contentId: string | undefined,
|
|
434
|
+
outcome:
|
|
435
|
+
| { readonly ok: true; readonly value: T }
|
|
436
|
+
| { readonly ok: false; readonly error: unknown },
|
|
437
|
+
): Promise<void> {
|
|
438
|
+
try {
|
|
439
|
+
await this.releaseStoreLock(owner, operation, contentId);
|
|
440
|
+
} catch (releaseError) {
|
|
441
|
+
if (!outcome.ok) {
|
|
442
|
+
throw new MediaStoreError(
|
|
443
|
+
`${operation} and release lock`,
|
|
444
|
+
contentId,
|
|
445
|
+
new AggregateError([outcome.error, releaseError]),
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
throw releaseError;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
private async acquireStoreLock(
|
|
453
|
+
operation: string,
|
|
454
|
+
contentId: string | undefined,
|
|
455
|
+
): Promise<StoreLockOwner> {
|
|
456
|
+
const deadline = Date.now() + this.lockTimeoutMs;
|
|
457
|
+
let owner = await this.tryAcquireStoreLock(operation, contentId);
|
|
458
|
+
while (owner === undefined) {
|
|
459
|
+
const recovered = await this.recoverStaleLock(operation, contentId);
|
|
460
|
+
if (!recovered) {
|
|
461
|
+
if (Date.now() >= deadline) {
|
|
462
|
+
throw new MediaStoreError(
|
|
463
|
+
`acquire store lock for ${operation}`,
|
|
464
|
+
contentId,
|
|
465
|
+
new Error(`Lock contention exceeded ${this.lockTimeoutMs}ms`),
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
await sleep(
|
|
469
|
+
Math.min(LOCK_RETRY_MS, Math.max(1, deadline - Date.now())),
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
owner = await this.tryAcquireStoreLock(operation, contentId);
|
|
473
|
+
}
|
|
474
|
+
return owner;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
private async tryAcquireStoreLock(
|
|
478
|
+
operation: string,
|
|
479
|
+
contentId: string | undefined,
|
|
480
|
+
): Promise<StoreLockOwner | undefined> {
|
|
481
|
+
const owner: StoreLockOwner = {
|
|
482
|
+
version: LOCK_VERSION,
|
|
483
|
+
token: randomUUID(),
|
|
484
|
+
pid: process.pid,
|
|
485
|
+
hostname: hostname(),
|
|
486
|
+
createdAt: Date.now(),
|
|
487
|
+
};
|
|
488
|
+
let handle: Awaited<ReturnType<typeof open>> | undefined;
|
|
489
|
+
let identity:
|
|
490
|
+
| { readonly device: number; readonly inode: number }
|
|
491
|
+
| undefined;
|
|
492
|
+
try {
|
|
493
|
+
handle = await open(this.files.lockPath, 'wx', FILE_MODE);
|
|
494
|
+
const metadata = await handle.stat();
|
|
495
|
+
identity = { device: metadata.dev, inode: metadata.ino };
|
|
496
|
+
await handle.writeFile(JSON.stringify(owner));
|
|
497
|
+
await handle.sync();
|
|
498
|
+
await handle.close();
|
|
499
|
+
handle = undefined;
|
|
500
|
+
await this.files.syncDirectory(this.files.lockDirectory);
|
|
501
|
+
return owner;
|
|
502
|
+
} catch (error) {
|
|
503
|
+
if (identity === undefined && hasErrnoCode(error, 'EEXIST')) {
|
|
504
|
+
return undefined;
|
|
505
|
+
}
|
|
506
|
+
const cleanupFailures = await this.cleanupOwnedLockAfterFailure(
|
|
507
|
+
handle,
|
|
508
|
+
identity,
|
|
509
|
+
);
|
|
510
|
+
const failure =
|
|
511
|
+
cleanupFailures.length === 0
|
|
512
|
+
? error
|
|
513
|
+
: new AggregateError(
|
|
514
|
+
[error, ...cleanupFailures],
|
|
515
|
+
'Store lock initialization and cleanup failed',
|
|
516
|
+
);
|
|
517
|
+
throw wrapError(
|
|
518
|
+
`acquire store lock for ${operation}`,
|
|
519
|
+
contentId,
|
|
520
|
+
failure,
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
private async cleanupOwnedLockAfterFailure(
|
|
526
|
+
handle: Awaited<ReturnType<typeof open>> | undefined,
|
|
527
|
+
identity: { readonly device: number; readonly inode: number } | undefined,
|
|
528
|
+
): Promise<unknown[]> {
|
|
529
|
+
const failures: unknown[] = [];
|
|
530
|
+
if (handle !== undefined) {
|
|
531
|
+
try {
|
|
532
|
+
await handle.close();
|
|
533
|
+
} catch (error) {
|
|
534
|
+
failures.push(error);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
if (identity === undefined) return failures;
|
|
538
|
+
try {
|
|
539
|
+
const current = await lstat(this.files.lockPath);
|
|
540
|
+
if (current.dev === identity.device && current.ino === identity.inode) {
|
|
541
|
+
await unlink(this.files.lockPath);
|
|
542
|
+
await this.files.syncDirectory(this.files.lockDirectory);
|
|
543
|
+
}
|
|
544
|
+
} catch (error) {
|
|
545
|
+
if (!hasErrnoCode(error, 'ENOENT')) failures.push(error);
|
|
546
|
+
}
|
|
547
|
+
return failures;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
private recoverStaleLock(
|
|
551
|
+
operation: string,
|
|
552
|
+
contentId: string | undefined,
|
|
553
|
+
): Promise<boolean> {
|
|
554
|
+
return recoverStaleStoreLock({
|
|
555
|
+
lockPath: this.files.lockPath,
|
|
556
|
+
lockDirectory: this.files.lockDirectory,
|
|
557
|
+
staleLockMs: this.staleLockMs,
|
|
558
|
+
operation,
|
|
559
|
+
contentId,
|
|
560
|
+
syncDirectory: () => this.files.syncDirectory(this.files.lockDirectory),
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
private async releaseStoreLock(
|
|
565
|
+
owner: StoreLockOwner,
|
|
566
|
+
operation: string,
|
|
567
|
+
contentId: string | undefined,
|
|
568
|
+
): Promise<void> {
|
|
569
|
+
let before: Awaited<ReturnType<typeof lstat>>;
|
|
570
|
+
let after: Awaited<ReturnType<typeof lstat>>;
|
|
571
|
+
let current: StoreLockOwner | undefined;
|
|
572
|
+
try {
|
|
573
|
+
before = await lstat(this.files.lockPath);
|
|
574
|
+
const parsed: unknown = JSON.parse(
|
|
575
|
+
await readFile(this.files.lockPath, 'utf8'),
|
|
576
|
+
);
|
|
577
|
+
current = parseStoreLockOwner(parsed);
|
|
578
|
+
after = await lstat(this.files.lockPath);
|
|
579
|
+
} catch (error) {
|
|
580
|
+
throw wrapError(`verify store lock for ${operation}`, contentId, error);
|
|
581
|
+
}
|
|
582
|
+
const invalidFileType = !before.isFile() || !after.isFile();
|
|
583
|
+
const inodeChanged = before.dev !== after.dev || before.ino !== after.ino;
|
|
584
|
+
const claimPresent = before.nlink !== 1 || after.nlink !== 1;
|
|
585
|
+
const identityChanged = invalidFileType || inodeChanged || claimPresent;
|
|
586
|
+
if (
|
|
587
|
+
current === undefined ||
|
|
588
|
+
current.token !== owner.token ||
|
|
589
|
+
identityChanged
|
|
590
|
+
) {
|
|
591
|
+
throw new MediaStoreError(
|
|
592
|
+
`verify store lock for ${operation}`,
|
|
593
|
+
contentId,
|
|
594
|
+
new Error('Store lock ownership changed before release'),
|
|
595
|
+
);
|
|
596
|
+
}
|
|
597
|
+
const releasedPath = join(
|
|
598
|
+
this.files.lockDirectory,
|
|
599
|
+
`store.lock.${owner.token}.released`,
|
|
600
|
+
);
|
|
601
|
+
try {
|
|
602
|
+
await rename(this.files.lockPath, releasedPath);
|
|
603
|
+
await unlink(releasedPath);
|
|
604
|
+
await this.files.syncDirectory(this.files.lockDirectory);
|
|
605
|
+
} catch (error) {
|
|
606
|
+
throw wrapError(`release store lock for ${operation}`, contentId, error);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
private async rollbackReservationAfterReadFailure(
|
|
611
|
+
objects: readonly MediaStoredObject[],
|
|
612
|
+
ownerDigest: string,
|
|
613
|
+
error: unknown,
|
|
614
|
+
): Promise<never> {
|
|
615
|
+
const failures: unknown[] = [];
|
|
616
|
+
for (const object of objects) {
|
|
617
|
+
try {
|
|
618
|
+
await this.removeReservation(object.contentId, ownerDigest);
|
|
619
|
+
} catch (cleanupError) {
|
|
620
|
+
failures.push(cleanupError);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
try {
|
|
624
|
+
await this.files.syncDirectory(this.files.reservationDirectory);
|
|
625
|
+
} catch (cleanupError) {
|
|
626
|
+
failures.push(cleanupError);
|
|
627
|
+
}
|
|
628
|
+
if (failures.length > 0) {
|
|
629
|
+
throw new AggregateError(
|
|
630
|
+
[error, ...failures],
|
|
631
|
+
'Media reservation and verification cleanup failed',
|
|
632
|
+
);
|
|
633
|
+
}
|
|
634
|
+
throw error;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
private async reserveUnlocked(
|
|
638
|
+
reference: MediaReferenceBlock,
|
|
639
|
+
ownerId: string,
|
|
640
|
+
): Promise<Uint8Array> {
|
|
641
|
+
const objects = requiredObjects(reference);
|
|
642
|
+
await this.verifyObjectsExistForReservation(objects);
|
|
643
|
+
await this.ensureInstanceLeaseUnlocked();
|
|
644
|
+
const ownerDigest = createHash('sha256').update(ownerId).digest('hex');
|
|
645
|
+
const existing = await this.readReservationIfPresent(
|
|
646
|
+
reference.contentId,
|
|
647
|
+
ownerDigest,
|
|
648
|
+
);
|
|
649
|
+
if (
|
|
650
|
+
existing !== undefined &&
|
|
651
|
+
this.reservationMatches(existing, objects) &&
|
|
652
|
+
(Date.now() < existing.expiresAt ||
|
|
653
|
+
(await this.instanceLeaseIsFresh(existing.instanceId)))
|
|
654
|
+
) {
|
|
655
|
+
try {
|
|
656
|
+
return await this.readObjectVerifiedUnlocked(
|
|
657
|
+
reference.selectedObject,
|
|
658
|
+
'reserve reference',
|
|
659
|
+
);
|
|
660
|
+
} catch (error) {
|
|
661
|
+
return this.rollbackReservationAfterReadFailure(
|
|
662
|
+
objects,
|
|
663
|
+
ownerDigest,
|
|
664
|
+
error,
|
|
665
|
+
);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
const record = this.buildReservationRecord(objects, ownerDigest);
|
|
669
|
+
try {
|
|
670
|
+
for (const object of objects) {
|
|
671
|
+
const reservationPath = await this.prepareReservationPath(
|
|
672
|
+
object.contentId,
|
|
673
|
+
ownerDigest,
|
|
674
|
+
);
|
|
675
|
+
await this.files.replacePublishedBytes(
|
|
676
|
+
Buffer.from(JSON.stringify(record), 'utf8'),
|
|
677
|
+
reservationPath,
|
|
678
|
+
object.contentId,
|
|
679
|
+
'reserve reference',
|
|
680
|
+
);
|
|
681
|
+
}
|
|
682
|
+
return await this.readObjectVerifiedUnlocked(
|
|
683
|
+
reference.selectedObject,
|
|
684
|
+
'reserve reference',
|
|
685
|
+
);
|
|
686
|
+
} catch (error) {
|
|
687
|
+
return this.rollbackReservationAfterReadFailure(
|
|
688
|
+
objects,
|
|
689
|
+
ownerDigest,
|
|
690
|
+
error,
|
|
691
|
+
);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
private async verifyObjectsExistForReservation(
|
|
696
|
+
objects: readonly MediaStoredObject[],
|
|
697
|
+
): Promise<void> {
|
|
698
|
+
for (const object of objects) {
|
|
699
|
+
if (!(await this.objectExistsUnlocked(object.contentId))) {
|
|
700
|
+
throw new MediaObjectMissingError(
|
|
701
|
+
'reserve reference',
|
|
702
|
+
object.contentId,
|
|
703
|
+
);
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
private buildReservationRecord(
|
|
709
|
+
objects: readonly MediaStoredObject[],
|
|
710
|
+
ownerDigest: string,
|
|
711
|
+
): ReservationRecord {
|
|
712
|
+
const now = Date.now();
|
|
713
|
+
return {
|
|
714
|
+
version: RESERVATION_VERSION,
|
|
715
|
+
ownerDigest,
|
|
716
|
+
instanceId: this.instanceId,
|
|
717
|
+
pid: process.pid,
|
|
718
|
+
hostname: hostname(),
|
|
719
|
+
createdAt: now,
|
|
720
|
+
expiresAt: now + this.reservationLeaseMs,
|
|
721
|
+
relatedContentIds: objects.map((object) => object.contentId),
|
|
722
|
+
};
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
private reservationMatches(
|
|
726
|
+
record: ReservationRecord,
|
|
727
|
+
objects: readonly MediaStoredObject[],
|
|
728
|
+
): boolean {
|
|
729
|
+
const contentIds = objects.map((object) => object.contentId);
|
|
730
|
+
return (
|
|
731
|
+
record.relatedContentIds.length === contentIds.length &&
|
|
732
|
+
record.relatedContentIds.every(
|
|
733
|
+
(contentId, index) => contentId === contentIds[index],
|
|
734
|
+
)
|
|
735
|
+
);
|
|
736
|
+
}
|
|
737
|
+
}
|