@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
|
@@ -15,37 +15,13 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import { invalidateResponsesStatefulChainForRetainedRewrite as invalidateRetainedRewrite } from './IContent.js';
|
|
18
|
-
import type { IContent, ToolCallBlock, ToolResponseBlock } from './IContent.js';
|
|
19
|
-
import { EventEmitter } from 'events';
|
|
20
18
|
import { isDeepStrictEqual } from 'node:util';
|
|
21
|
-
|
|
22
|
-
import
|
|
23
|
-
import type { RuntimeTokenizer as ITokenizer } from '../../runtime/contracts/RuntimeTokenizer.js';
|
|
24
|
-
import { DebugLogger } from '../../debug/index.js';
|
|
25
|
-
import { randomUUID } from 'crypto';
|
|
26
|
-
import { canonicalizeToolCallId } from './canonicalToolIds.js';
|
|
27
|
-
import type { DensityResult } from '../../core/compression/types.js';
|
|
28
|
-
import {
|
|
29
|
-
estimateContentTokens as estimateContentTokensImpl,
|
|
30
|
-
estimateTokensForContents as estimateTokensForContentsImpl,
|
|
31
|
-
simpleTokenEstimateForText,
|
|
32
|
-
type TokenizerProvider,
|
|
33
|
-
} from './historyTokenEstimation.js';
|
|
34
|
-
import {
|
|
35
|
-
validateDensityResult,
|
|
36
|
-
applyDensityMutations,
|
|
37
|
-
} from './densityValidation.js';
|
|
19
|
+
import type { IContent, ToolCallBlock, ToolResponseBlock } from './IContent.js';
|
|
20
|
+
import { estimateContentTokens as estimateContentTokensImpl } from './historyTokenEstimation.js';
|
|
38
21
|
import {
|
|
39
22
|
computeStatistics,
|
|
40
23
|
type ConversationStatistics,
|
|
41
|
-
logContentAdded,
|
|
42
|
-
logQueuedDuringCompression,
|
|
43
24
|
} from './curationDebugLogger.js';
|
|
44
|
-
import {
|
|
45
|
-
type HistoryServiceEventEmitter,
|
|
46
|
-
type CompressionConfig,
|
|
47
|
-
} from './historyEventTypes.js';
|
|
48
|
-
import { getTokenizerForModel } from './historyTokenizerAdapter.js';
|
|
49
25
|
import {
|
|
50
26
|
collectRespondedCallIds,
|
|
51
27
|
getMissingToolCalls,
|
|
@@ -60,646 +36,30 @@ import {
|
|
|
60
36
|
summarizeOldHistory as summarizeOldHistoryHelper,
|
|
61
37
|
} from './historyContextWindow.js';
|
|
62
38
|
import {
|
|
63
|
-
ChronologyStamper,
|
|
64
39
|
buildChronologyTrace,
|
|
65
40
|
type ChronologyTraceEntry,
|
|
66
41
|
} from './historyChronology.js';
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
kind: 'asynchronous';
|
|
80
|
-
execute: () => Promise<void>;
|
|
81
|
-
resolve: () => void;
|
|
82
|
-
reject: (error: unknown) => void;
|
|
83
|
-
};
|
|
42
|
+
import { HistoryServiceCore } from './HistoryServiceCore.js';
|
|
43
|
+
import { sanitizeProviderHistoryForSerialization } from './historyCloneUtils.js';
|
|
44
|
+
|
|
45
|
+
export type {
|
|
46
|
+
CompressionConfig,
|
|
47
|
+
HistoryBatchOptions,
|
|
48
|
+
HistoryBatchParticipant,
|
|
49
|
+
HistoryBatchPublication,
|
|
50
|
+
HistoryMediaOwner,
|
|
51
|
+
HistoryOwnedMediaReservation,
|
|
52
|
+
PreparedHistoryBatchEffect,
|
|
53
|
+
} from './HistoryServiceCore.js';
|
|
84
54
|
|
|
85
55
|
/**
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
56
|
+
* Provider-neutral conversation history service.
|
|
57
|
+
*
|
|
58
|
+
* Mutation, ownership, chronology, and token-accounting mechanics live in the
|
|
59
|
+
* cohesive base implementation. This class owns history queries, lifecycle,
|
|
60
|
+
* compression coordination, and serialization.
|
|
89
61
|
*/
|
|
90
|
-
export class HistoryService
|
|
91
|
-
extends EventEmitter
|
|
92
|
-
implements HistoryServiceEventEmitter
|
|
93
|
-
{
|
|
94
|
-
private history: IContent[] = [];
|
|
95
|
-
private totalTokens: number = 0;
|
|
96
|
-
private baseTokenOffset: number = 0;
|
|
97
|
-
private tokenizerCache = new Map<string, ITokenizer>();
|
|
98
|
-
private tokenizerLock: Promise<void> = Promise.resolve();
|
|
99
|
-
private pendingTokenizerFailure: { error: unknown } | undefined;
|
|
100
|
-
private syncGeneration: number = 0;
|
|
101
|
-
private historyMutationInProgress = false;
|
|
102
|
-
private historyMutationQueue: QueuedHistoryMutation[] = [];
|
|
103
|
-
private logger = new DebugLogger('llxprt:history:service');
|
|
104
|
-
|
|
105
|
-
private chronology = new ChronologyStamper();
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Monotonic cache anchor: the highest chronology `seq` that must remain in
|
|
109
|
-
* the preserved head across every subsequent middle-out compression. Once a
|
|
110
|
-
* head entry is preserved by one compression, no later compression may drop
|
|
111
|
-
* it, which keeps the provider-visible prefix byte-identical (#3070).
|
|
112
|
-
*
|
|
113
|
-
* Survives the compression clear/rebuild; reset explicitly by session-reset
|
|
114
|
-
* and history-restore paths.
|
|
115
|
-
*/
|
|
116
|
-
private cacheAnchorSeq: number = 0;
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* @plan:PLAN-20260603-ISSUE1584.P05
|
|
120
|
-
* @requirement:REQ-DEP-001
|
|
121
|
-
* @pseudocode component-boundaries.md C-CB-01, lines 10-15
|
|
122
|
-
*
|
|
123
|
-
* Injected tokenizer factory. When provided, HistoryService uses the factory
|
|
124
|
-
* to obtain tokenizers instead of constructing provider tokenizers directly.
|
|
125
|
-
* This eliminates the core→providers import dependency on the injection path.
|
|
126
|
-
*/
|
|
127
|
-
private tokenizerFactory?: RuntimeTokenizerFactory;
|
|
128
|
-
private activeTokenizationModel = 'gpt-4.1';
|
|
129
|
-
private activeTokenizationProvider?: string;
|
|
130
|
-
|
|
131
|
-
private static readonly COMPRESSION_QUEUE_HIGH_WATER = 4096;
|
|
132
|
-
private isCompressing: boolean = false;
|
|
133
|
-
private pendingOperations: Array<() => void> = [];
|
|
134
|
-
private pendingCompressionHighWaterReported: boolean = false;
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* @plan:PLAN-20260603-ISSUE1584.P05
|
|
138
|
-
* @requirement:REQ-DEP-001
|
|
139
|
-
* @pseudocode component-boundaries.md C-CB-01, lines 10-15
|
|
140
|
-
*
|
|
141
|
-
* Set the tokenizer factory for injection-based tokenizer resolution.
|
|
142
|
-
* When set, getTokenizerForModel will prefer the factory over
|
|
143
|
-
* constructing provider tokenizers directly.
|
|
144
|
-
*/
|
|
145
|
-
setTokenizerFactory(factory: RuntimeTokenizerFactory): void {
|
|
146
|
-
this.tokenizerFactory = factory;
|
|
147
|
-
this.tokenizerCache.clear();
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
setActiveTokenizationTarget(
|
|
151
|
-
modelName: string,
|
|
152
|
-
activeProvider?: string,
|
|
153
|
-
): void {
|
|
154
|
-
this.activeTokenizationModel = modelName;
|
|
155
|
-
this.activeTokenizationProvider = activeProvider;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Get or create tokenizer for a specific model.
|
|
160
|
-
*
|
|
161
|
-
* @plan:PLAN-20260603-ISSUE1584.P05
|
|
162
|
-
* @requirement:REQ-DEP-001
|
|
163
|
-
* @pseudocode component-boundaries.md C-CB-01, lines 10-15
|
|
164
|
-
*
|
|
165
|
-
* When a RuntimeTokenizerFactory is injected, it is preferred over
|
|
166
|
-
* direct provider tokenizer construction. This removes the core→providers
|
|
167
|
-
* dependency when using the injection path.
|
|
168
|
-
*/
|
|
169
|
-
private getTokenizerForModel(
|
|
170
|
-
modelName: string,
|
|
171
|
-
activeProvider?: string,
|
|
172
|
-
): ITokenizer {
|
|
173
|
-
return getTokenizerForModel(activeProvider, modelName, {
|
|
174
|
-
tokenizerCache: this.tokenizerCache,
|
|
175
|
-
tokenizerFactory: this.tokenizerFactory,
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Generate a new canonical history tool ID.
|
|
181
|
-
* Format: hist_tool_<hash>
|
|
182
|
-
*/
|
|
183
|
-
generateHistoryId(
|
|
184
|
-
turnKey: string,
|
|
185
|
-
callIndex: number,
|
|
186
|
-
providerName?: string,
|
|
187
|
-
rawId?: string,
|
|
188
|
-
toolName?: string,
|
|
189
|
-
): string {
|
|
190
|
-
return canonicalizeToolCallId({
|
|
191
|
-
providerName,
|
|
192
|
-
rawId,
|
|
193
|
-
toolName,
|
|
194
|
-
turnKey,
|
|
195
|
-
callIndex,
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Get a callback suitable for passing into converters
|
|
201
|
-
* which will generate normalized history IDs on demand.
|
|
202
|
-
*/
|
|
203
|
-
getIdGeneratorCallback(turnKey?: string): () => string {
|
|
204
|
-
let callIndex = 0;
|
|
205
|
-
const stableTurnKey = turnKey ?? this.generateTurnKey();
|
|
206
|
-
return () => this.generateHistoryId(stableTurnKey, callIndex++);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
generateTurnKey(): string {
|
|
210
|
-
return `turn_${randomUUID()}`;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Get the current total token count including base offset (system prompt).
|
|
215
|
-
*
|
|
216
|
-
* This value is used for compression threshold calculations and should always
|
|
217
|
-
* reflect the total context size that will be sent to the API.
|
|
218
|
-
*
|
|
219
|
-
* @returns baseTokenOffset + totalTokens (history tokens)
|
|
220
|
-
*/
|
|
221
|
-
getTotalTokens(): number {
|
|
222
|
-
return this.baseTokenOffset + this.totalTokens;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
getBaseTokenOffset(): number {
|
|
226
|
-
return this.baseTokenOffset;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
async estimateTokensForText(
|
|
230
|
-
text: string,
|
|
231
|
-
modelName = this.activeTokenizationModel,
|
|
232
|
-
): Promise<number> {
|
|
233
|
-
if (!text) {
|
|
234
|
-
return 0;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
const tokenizer = this.getTokenizerForModel(
|
|
238
|
-
modelName,
|
|
239
|
-
this.activeTokenizationProvider,
|
|
240
|
-
);
|
|
241
|
-
try {
|
|
242
|
-
return await tokenizer.countTokens(text);
|
|
243
|
-
} catch (error) {
|
|
244
|
-
if (tokenizer.fallbackPolicy === 'deny') {
|
|
245
|
-
throw error;
|
|
246
|
-
}
|
|
247
|
-
this.logger.debug(
|
|
248
|
-
'Error counting tokens for raw text, using fallback:',
|
|
249
|
-
error,
|
|
250
|
-
);
|
|
251
|
-
return simpleTokenEstimateForText(text);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* Set a base offset that is always included in the total token count.
|
|
257
|
-
* Useful for accounting for system prompts or other fixed overhead.
|
|
258
|
-
*
|
|
259
|
-
* The system prompt token count should be set once at chat start using this method.
|
|
260
|
-
* This offset is included in getTotalTokens() to ensure compression threshold
|
|
261
|
-
* calculations account for the full context size (system prompt + history).
|
|
262
|
-
*
|
|
263
|
-
* NOTE: The system prompt itself is NEVER compressed - only conversation history
|
|
264
|
-
* returned by getCurated() is subject to compression.
|
|
265
|
-
*
|
|
266
|
-
* @param offset - Number of tokens in the system prompt or fixed overhead
|
|
267
|
-
*/
|
|
268
|
-
setBaseTokenOffset(offset: number): void {
|
|
269
|
-
const normalized = Math.max(0, Math.floor(offset));
|
|
270
|
-
const delta = normalized - this.baseTokenOffset;
|
|
271
|
-
this.baseTokenOffset = normalized;
|
|
272
|
-
|
|
273
|
-
if (delta !== 0) {
|
|
274
|
-
this.emit('tokensUpdated', {
|
|
275
|
-
totalTokens: this.getTotalTokens(),
|
|
276
|
-
addedTokens: delta,
|
|
277
|
-
contentId: null,
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* Sync the total token count to match actual prompt tokens from a provider.
|
|
284
|
-
* This adjusts the baseTokenOffset so estimates align with the real count.
|
|
285
|
-
*/
|
|
286
|
-
syncTotalTokens(actualTotal: number): void {
|
|
287
|
-
if (!Number.isFinite(actualTotal)) {
|
|
288
|
-
this.logger.debug('Skipping syncTotalTokens for non-finite value', {
|
|
289
|
-
actualTotal,
|
|
290
|
-
});
|
|
291
|
-
return;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
const normalized = Math.max(0, Math.floor(actualTotal));
|
|
295
|
-
const generation = this.syncGeneration;
|
|
296
|
-
this.observeTokenizerOperation(
|
|
297
|
-
this.runSerializedTokenOperation(() => {
|
|
298
|
-
if (generation !== this.syncGeneration) return;
|
|
299
|
-
|
|
300
|
-
const currentTotal = this.getTotalTokens();
|
|
301
|
-
const drift = normalized - currentTotal;
|
|
302
|
-
|
|
303
|
-
if (drift === 0) {
|
|
304
|
-
return;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
this.baseTokenOffset += drift;
|
|
308
|
-
|
|
309
|
-
this.emit('tokensUpdated', {
|
|
310
|
-
totalTokens: this.getTotalTokens(),
|
|
311
|
-
addedTokens: drift,
|
|
312
|
-
contentId: null,
|
|
313
|
-
});
|
|
314
|
-
}),
|
|
315
|
-
);
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
private runSerializedTokenOperation<T>(
|
|
319
|
-
operation: () => T | Promise<T>,
|
|
320
|
-
): Promise<T> {
|
|
321
|
-
const result = this.tokenizerLock.then(operation);
|
|
322
|
-
this.tokenizerLock = result.then(
|
|
323
|
-
() => undefined,
|
|
324
|
-
() => undefined,
|
|
325
|
-
);
|
|
326
|
-
return result;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
private observeTokenizerOperation(operation: Promise<void>): void {
|
|
330
|
-
void operation.catch((error: unknown) => {
|
|
331
|
-
this.pendingTokenizerFailure ??= { error };
|
|
332
|
-
this.logger.error('Asynchronous token accounting failed', error);
|
|
333
|
-
});
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
private invalidatePendingSyncs(): void {
|
|
337
|
-
this.syncGeneration++;
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
private runSynchronousHistoryMutation(execute: () => void): void {
|
|
341
|
-
if (this.historyMutationInProgress) {
|
|
342
|
-
this.historyMutationQueue.push({ kind: 'synchronous', execute });
|
|
343
|
-
return;
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
this.historyMutationInProgress = true;
|
|
347
|
-
let failure: MutationFailure = { failed: false };
|
|
348
|
-
try {
|
|
349
|
-
execute();
|
|
350
|
-
} catch (error: unknown) {
|
|
351
|
-
failure = { failed: true, error };
|
|
352
|
-
}
|
|
353
|
-
const queuedFailure = this.drainSynchronousHistoryMutations();
|
|
354
|
-
this.historyMutationInProgress = false;
|
|
355
|
-
this.processHistoryMutationQueue();
|
|
356
|
-
|
|
357
|
-
const combinedFailure = combineMutationFailures(failure, queuedFailure);
|
|
358
|
-
if (combinedFailure.failed) throw combinedFailure.error;
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
private drainSynchronousHistoryMutations(): MutationFailure {
|
|
362
|
-
let failure: MutationFailure = { failed: false };
|
|
363
|
-
while (this.historyMutationQueue[0]?.kind === 'synchronous') {
|
|
364
|
-
const mutation = this.historyMutationQueue.shift();
|
|
365
|
-
if (mutation?.kind !== 'synchronous') break;
|
|
366
|
-
try {
|
|
367
|
-
mutation.execute();
|
|
368
|
-
} catch (error: unknown) {
|
|
369
|
-
failure = combineMutationFailures(failure, { failed: true, error });
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
return failure;
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
private enqueueAsynchronousHistoryMutation(
|
|
376
|
-
execute: () => Promise<void>,
|
|
377
|
-
): Promise<void> {
|
|
378
|
-
return new Promise((resolve, reject) => {
|
|
379
|
-
this.historyMutationQueue.push({
|
|
380
|
-
kind: 'asynchronous',
|
|
381
|
-
execute,
|
|
382
|
-
resolve,
|
|
383
|
-
reject,
|
|
384
|
-
});
|
|
385
|
-
this.processHistoryMutationQueue();
|
|
386
|
-
});
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
private processHistoryMutationQueue(): void {
|
|
390
|
-
if (this.historyMutationInProgress) return;
|
|
391
|
-
const mutation = this.historyMutationQueue.shift();
|
|
392
|
-
if (mutation === undefined) return;
|
|
393
|
-
if (mutation.kind === 'synchronous') {
|
|
394
|
-
this.runSynchronousHistoryMutation(mutation.execute);
|
|
395
|
-
return;
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
this.historyMutationInProgress = true;
|
|
399
|
-
void mutation.execute().then(
|
|
400
|
-
() =>
|
|
401
|
-
this.completeAsynchronousHistoryMutation(mutation, { failed: false }),
|
|
402
|
-
(error: unknown) =>
|
|
403
|
-
this.completeAsynchronousHistoryMutation(mutation, {
|
|
404
|
-
failed: true,
|
|
405
|
-
error,
|
|
406
|
-
}),
|
|
407
|
-
);
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
private completeAsynchronousHistoryMutation(
|
|
411
|
-
mutation: Extract<QueuedHistoryMutation, { kind: 'asynchronous' }>,
|
|
412
|
-
failure: MutationFailure,
|
|
413
|
-
): void {
|
|
414
|
-
const queuedFailure = this.drainSynchronousHistoryMutations();
|
|
415
|
-
this.historyMutationInProgress = false;
|
|
416
|
-
const result = combineMutationFailures(failure, queuedFailure);
|
|
417
|
-
if (result.failed) mutation.reject(result.error);
|
|
418
|
-
else mutation.resolve();
|
|
419
|
-
this.processHistoryMutationQueue();
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
resetTokenAccounting(): void {
|
|
423
|
-
this.invalidatePendingSyncs();
|
|
424
|
-
this.baseTokenOffset = 0;
|
|
425
|
-
this.emit('tokensUpdated', {
|
|
426
|
-
totalTokens: this.getTotalTokens(),
|
|
427
|
-
addedTokens: 0,
|
|
428
|
-
contentId: null,
|
|
429
|
-
});
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
/**
|
|
433
|
-
* Add content to the history.
|
|
434
|
-
* Zero-block turns are rejected at insertion (issue #2410): they corrupt
|
|
435
|
-
* provider-facing history (z.ai rejects empty human turns with HTTP 400
|
|
436
|
-
* error 1213). All other content with a valid speaker is accepted.
|
|
437
|
-
*/
|
|
438
|
-
add(content: IContent, modelName?: string): void {
|
|
439
|
-
if (this.isCompressing) {
|
|
440
|
-
logQueuedDuringCompression(this.logger, content);
|
|
441
|
-
this.queueCompressionOperation(() => {
|
|
442
|
-
this.runSynchronousHistoryMutation(() => {
|
|
443
|
-
this.addInternal(content, modelName);
|
|
444
|
-
});
|
|
445
|
-
});
|
|
446
|
-
return;
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
this.runSynchronousHistoryMutation(() => {
|
|
450
|
-
this.addInternal(content, modelName);
|
|
451
|
-
});
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
/**
|
|
455
|
-
* Queues an operation that arrived while compression held the history lock.
|
|
456
|
-
*
|
|
457
|
-
* Operations are never dropped and never rejected: `add()` is on the
|
|
458
|
-
* streaming path, so failing here would lose conversation content and could
|
|
459
|
-
* break a turn. `startCompression`/`endCompression` are balanced in a
|
|
460
|
-
* `finally` by the only caller (`CompressionHandler.performCompression`), so
|
|
461
|
-
* the lock is always released and the queue is bounded by how long a single
|
|
462
|
-
* compression takes. Crossing the high-water mark is reported once so an
|
|
463
|
-
* unbalanced lock would be diagnosable rather than silent (issue #2852).
|
|
464
|
-
*/
|
|
465
|
-
private queueCompressionOperation(operation: () => void): void {
|
|
466
|
-
this.pendingOperations.push(operation);
|
|
467
|
-
if (
|
|
468
|
-
!this.pendingCompressionHighWaterReported &&
|
|
469
|
-
this.pendingOperations.length >=
|
|
470
|
-
HistoryService.COMPRESSION_QUEUE_HIGH_WATER
|
|
471
|
-
) {
|
|
472
|
-
this.pendingCompressionHighWaterReported = true;
|
|
473
|
-
this.logger.error(
|
|
474
|
-
'History compression queue exceeded its high-water mark; the compression lock is being held for an unexpectedly long time. No operations are dropped.',
|
|
475
|
-
{ pendingCount: this.pendingOperations.length },
|
|
476
|
-
);
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
private addInternal(content: IContent, modelName?: string): void {
|
|
481
|
-
// Reject zero-block turns: a Content with no blocks corrupts provider-
|
|
482
|
-
// facing history (notably z.ai rejects empty human turns with HTTP 400
|
|
483
|
-
// error 1213, issue #2410). This is a systemic safety net — earlier
|
|
484
|
-
// layers should prevent these from reaching history, but we enforce the
|
|
485
|
-
// invariant here as the last line of defense.
|
|
486
|
-
const hasValidSpeaker = ['human', 'ai', 'tool'].includes(content.speaker);
|
|
487
|
-
const hasBlocks =
|
|
488
|
-
Array.isArray(content.blocks) && content.blocks.length > 0;
|
|
489
|
-
const accepted = hasValidSpeaker && hasBlocks;
|
|
490
|
-
|
|
491
|
-
if (accepted) {
|
|
492
|
-
// Stamp chronology only once the content is known to be accepted, so a
|
|
493
|
-
// rejected turn never consumes a sequence number (#1721).
|
|
494
|
-
this.chronology.stamp(content);
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
logContentAdded(this.logger, content, modelName);
|
|
498
|
-
|
|
499
|
-
if (!accepted) {
|
|
500
|
-
this.logger.debug(
|
|
501
|
-
hasValidSpeaker
|
|
502
|
-
? 'Content rejected - zero blocks (issue #2410):'
|
|
503
|
-
: 'Content rejected - invalid speaker:',
|
|
504
|
-
content.speaker,
|
|
505
|
-
);
|
|
506
|
-
return;
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
const generation = this.syncGeneration;
|
|
510
|
-
this.history.push(content);
|
|
511
|
-
|
|
512
|
-
try {
|
|
513
|
-
this.emit('contentAdded', content);
|
|
514
|
-
} catch (error: unknown) {
|
|
515
|
-
// Roll back the insertion. The consumed chronology sequence number is
|
|
516
|
-
// intentionally NOT reclaimed: sequence numbers are never reused, and
|
|
517
|
-
// the resulting gap truthfully records that an item was removed.
|
|
518
|
-
this.history.pop();
|
|
519
|
-
throw error;
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
// Update token count asynchronously but atomically
|
|
523
|
-
this.observeTokenizerOperation(
|
|
524
|
-
this.updateTokenCount(content, modelName, generation),
|
|
525
|
-
);
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
/**
|
|
529
|
-
* Atomically update token count for new content
|
|
530
|
-
*/
|
|
531
|
-
private updateTokenCount(
|
|
532
|
-
content: IContent,
|
|
533
|
-
modelName?: string,
|
|
534
|
-
generation = this.syncGeneration,
|
|
535
|
-
): Promise<void> {
|
|
536
|
-
return this.runSerializedTokenOperation(async () => {
|
|
537
|
-
// Always derive token counts from the stored content to avoid double counting
|
|
538
|
-
// when providers attach aggregate usage metadata (which already includes prompt tokens).
|
|
539
|
-
const defaultModel = modelName ?? this.activeTokenizationModel;
|
|
540
|
-
const contentTokens = await this.estimateContentTokens(
|
|
541
|
-
content,
|
|
542
|
-
defaultModel,
|
|
543
|
-
);
|
|
544
|
-
if (generation !== this.syncGeneration) return;
|
|
545
|
-
|
|
546
|
-
// Atomically update the total
|
|
547
|
-
this.totalTokens += contentTokens;
|
|
548
|
-
|
|
549
|
-
// Emit event with updated count
|
|
550
|
-
const eventData = {
|
|
551
|
-
totalTokens: this.getTotalTokens(),
|
|
552
|
-
addedTokens: contentTokens,
|
|
553
|
-
contentId: content.metadata?.id,
|
|
554
|
-
};
|
|
555
|
-
|
|
556
|
-
this.logger.debug('Emitting tokensUpdated:', eventData);
|
|
557
|
-
|
|
558
|
-
this.emit('tokensUpdated', eventData);
|
|
559
|
-
});
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
/**
|
|
563
|
-
* Estimate token count for content using tokenizer
|
|
564
|
-
*/
|
|
565
|
-
private async estimateContentTokens(
|
|
566
|
-
content: IContent,
|
|
567
|
-
modelName: string,
|
|
568
|
-
): Promise<number> {
|
|
569
|
-
return estimateContentTokensImpl(
|
|
570
|
-
content,
|
|
571
|
-
modelName,
|
|
572
|
-
this.tokenizerProvider(),
|
|
573
|
-
this.logger,
|
|
574
|
-
);
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
/** Provide the TokenizerProvider interface for the token estimation helpers. */
|
|
578
|
-
private tokenizerProvider(
|
|
579
|
-
activeProvider = this.activeTokenizationProvider,
|
|
580
|
-
): TokenizerProvider {
|
|
581
|
-
return {
|
|
582
|
-
getTokenizerForModel: (modelName: string) =>
|
|
583
|
-
this.getTokenizerForModel(modelName, activeProvider),
|
|
584
|
-
activeProvider,
|
|
585
|
-
};
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
/**
|
|
589
|
-
* Add multiple contents to the history.
|
|
590
|
-
*
|
|
591
|
-
* Iterates a snapshot because `add` appends to `this.history`: if `contents`
|
|
592
|
-
* aliases the backing array (`getRawHistory()`), a live iterator would keep
|
|
593
|
-
* consuming its own appends and never terminate. `replaceAll` is already
|
|
594
|
-
* immune the same way — its `filter` produces a fresh array before use.
|
|
595
|
-
*/
|
|
596
|
-
addAll(contents: readonly IContent[], modelName?: string): void {
|
|
597
|
-
for (const content of [...contents]) {
|
|
598
|
-
this.add(content, modelName);
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
async replaceAll(contents: IContent[], modelName?: string): Promise<void> {
|
|
603
|
-
const accepted = contents.filter(
|
|
604
|
-
(content) =>
|
|
605
|
-
['human', 'ai', 'tool'].includes(content.speaker) &&
|
|
606
|
-
Array.isArray(content.blocks) &&
|
|
607
|
-
content.blocks.length > 0,
|
|
608
|
-
);
|
|
609
|
-
return this.enqueueAsynchronousHistoryMutation(() =>
|
|
610
|
-
this.replaceAllInternal(accepted, modelName),
|
|
611
|
-
);
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
private async replaceAllInternal(
|
|
615
|
-
accepted: IContent[],
|
|
616
|
-
modelName?: string,
|
|
617
|
-
): Promise<void> {
|
|
618
|
-
await this.waitForTokenUpdates();
|
|
619
|
-
const replacementTokens = await this.estimateTokensForContents(
|
|
620
|
-
accepted,
|
|
621
|
-
modelName,
|
|
622
|
-
);
|
|
623
|
-
const previousHistory = this.history;
|
|
624
|
-
const previousTokens = this.totalTokens;
|
|
625
|
-
this.invalidatePendingSyncs();
|
|
626
|
-
// Uphold the chronology invariant on this insertion path too: items that
|
|
627
|
-
// already carry a marker keep it, and anything new is stamped (#1721).
|
|
628
|
-
for (const content of accepted) {
|
|
629
|
-
this.chronology.stamp(content);
|
|
630
|
-
}
|
|
631
|
-
this.history = [...accepted];
|
|
632
|
-
this.totalTokens = replacementTokens;
|
|
633
|
-
try {
|
|
634
|
-
this.emit('tokensUpdated', {
|
|
635
|
-
totalTokens: this.getTotalTokens(),
|
|
636
|
-
addedTokens: replacementTokens - previousTokens,
|
|
637
|
-
contentId: null,
|
|
638
|
-
});
|
|
639
|
-
} catch (error: unknown) {
|
|
640
|
-
this.invalidatePendingSyncs();
|
|
641
|
-
this.history = previousHistory;
|
|
642
|
-
this.totalTokens = previousTokens;
|
|
643
|
-
throw error;
|
|
644
|
-
}
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
/**
|
|
648
|
-
* Estimate total tokens for hypothetical contents without mutating history.
|
|
649
|
-
*/
|
|
650
|
-
async estimateTokensForContents(
|
|
651
|
-
contents: IContent[],
|
|
652
|
-
modelName?: string,
|
|
653
|
-
): Promise<number> {
|
|
654
|
-
return estimateTokensForContentsImpl(
|
|
655
|
-
contents,
|
|
656
|
-
modelName,
|
|
657
|
-
this.tokenizerProvider(),
|
|
658
|
-
this.logger,
|
|
659
|
-
);
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
/**
|
|
663
|
-
* Wait for any in-flight token updates to complete.
|
|
664
|
-
*/
|
|
665
|
-
async waitForTokenUpdates(): Promise<void> {
|
|
666
|
-
await this.tokenizerLock;
|
|
667
|
-
const failure = this.pendingTokenizerFailure;
|
|
668
|
-
this.pendingTokenizerFailure = undefined;
|
|
669
|
-
if (failure !== undefined) throw failure.error;
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
/**
|
|
673
|
-
* Apply a density optimization result to the raw history.
|
|
674
|
-
*
|
|
675
|
-
* @plan PLAN-20260211-HIGHDENSITY.P08
|
|
676
|
-
* @requirement REQ-HD-003.1, REQ-HD-003.2, REQ-HD-003.3, REQ-HD-001.6, REQ-HD-001.7
|
|
677
|
-
* @pseudocode history-service.md lines 20-82
|
|
678
|
-
*/
|
|
679
|
-
async applyDensityResult(result: DensityResult): Promise<void> {
|
|
680
|
-
validateDensityResult(result, this.history.length);
|
|
681
|
-
// Each density replacement takes over the chronology position of the item
|
|
682
|
-
// it replaces, so the surviving history keeps an unbroken sequence.
|
|
683
|
-
// densityValidation stays free of chronology knowledge.
|
|
684
|
-
for (const [index, replacement] of result.replacements) {
|
|
685
|
-
const replacedMarker = this.history[index].metadata?.chronology;
|
|
686
|
-
if (replacedMarker !== undefined) {
|
|
687
|
-
this.chronology.inherit(replacement, replacedMarker);
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
applyDensityMutations(this.history, result);
|
|
691
|
-
|
|
692
|
-
this.logger.debug('Density: applied result', {
|
|
693
|
-
replacements: result.replacements.size,
|
|
694
|
-
removals: result.removals.length,
|
|
695
|
-
newHistoryLength: this.history.length,
|
|
696
|
-
metadata: result.metadata,
|
|
697
|
-
});
|
|
698
|
-
|
|
699
|
-
// T1: Full recalculation through tokenizerLock
|
|
700
|
-
await this.recalculateTotalTokens();
|
|
701
|
-
}
|
|
702
|
-
|
|
62
|
+
export class HistoryService extends HistoryServiceCore {
|
|
703
63
|
/**
|
|
704
64
|
* Immutably replace a single tool_response block with a replacement
|
|
705
65
|
* tool_response block, preserving callId/toolName invariants.
|
|
@@ -729,6 +89,10 @@ export class HistoryService
|
|
|
729
89
|
return replaced;
|
|
730
90
|
}
|
|
731
91
|
|
|
92
|
+
/**
|
|
93
|
+
* Runs inside the asynchronous mutation FIFO so a concurrent add() observes
|
|
94
|
+
* the replacement as one step rather than landing mid-recalculation.
|
|
95
|
+
*/
|
|
732
96
|
private async replaceToolResponseBlockInternal(
|
|
733
97
|
entryIndex: number,
|
|
734
98
|
blockIndex: number,
|
|
@@ -863,10 +227,13 @@ export class HistoryService
|
|
|
863
227
|
this.totalTokens = 0;
|
|
864
228
|
this.baseTokenOffset = 0;
|
|
865
229
|
this.isCompressing = false;
|
|
866
|
-
this.pendingOperations
|
|
230
|
+
this.pendingOperations.clear();
|
|
867
231
|
this.tokenizerCache.clear();
|
|
868
232
|
this.tokenizerLock = Promise.resolve();
|
|
869
233
|
this.pendingTokenizerFailure = undefined;
|
|
234
|
+
if (this.mediaOwner !== undefined) {
|
|
235
|
+
this.enqueueSynchronousOwnershipReleaseAll();
|
|
236
|
+
}
|
|
870
237
|
// Chronology counters are intentionally NOT reset: seq must never be reused
|
|
871
238
|
// (NG8) so that items added after dispose() never collide with earlier ones.
|
|
872
239
|
}
|
|
@@ -875,20 +242,19 @@ export class HistoryService
|
|
|
875
242
|
* Clear all history
|
|
876
243
|
*/
|
|
877
244
|
clear(): void {
|
|
878
|
-
|
|
245
|
+
const clearAndRelease = (): void => {
|
|
246
|
+
this.runSynchronousHistoryMutation(() => {
|
|
247
|
+
this.clearInternal();
|
|
248
|
+
});
|
|
249
|
+
this.enqueueSynchronousOwnershipReleaseAll();
|
|
250
|
+
};
|
|
879
251
|
if (this.isCompressing) {
|
|
880
252
|
this.logger.debug('Queueing clear operation during compression');
|
|
881
|
-
this.queueCompressionOperation(
|
|
882
|
-
this.runSynchronousHistoryMutation(() => {
|
|
883
|
-
this.clearInternal();
|
|
884
|
-
});
|
|
885
|
-
});
|
|
253
|
+
this.queueCompressionOperation(clearAndRelease);
|
|
886
254
|
return;
|
|
887
255
|
}
|
|
888
256
|
|
|
889
|
-
|
|
890
|
-
this.clearInternal();
|
|
891
|
-
});
|
|
257
|
+
clearAndRelease();
|
|
892
258
|
}
|
|
893
259
|
|
|
894
260
|
private clearInternal(): void {
|
|
@@ -937,7 +303,9 @@ export class HistoryService
|
|
|
937
303
|
removeLastIfMatches(content: IContent): boolean {
|
|
938
304
|
const last = this.history[this.history.length - 1];
|
|
939
305
|
if (last === content) {
|
|
306
|
+
const previous = [...this.history];
|
|
940
307
|
this.history.pop();
|
|
308
|
+
this.enqueueSynchronousOwnershipReconcile(previous, () => this.history);
|
|
941
309
|
return true;
|
|
942
310
|
}
|
|
943
311
|
return false;
|
|
@@ -945,8 +313,10 @@ export class HistoryService
|
|
|
945
313
|
|
|
946
314
|
/** Pop the last content from history. */
|
|
947
315
|
pop(): IContent | undefined {
|
|
316
|
+
const previous = [...this.history];
|
|
948
317
|
const removed = this.history.pop();
|
|
949
318
|
if (removed) {
|
|
319
|
+
this.enqueueSynchronousOwnershipReconcile(previous, () => this.history);
|
|
950
320
|
// Recalculate tokens since we removed content
|
|
951
321
|
// This is less efficient but ensures accuracy
|
|
952
322
|
this.observeTokenizerOperation(this.recalculateTokens());
|
|
@@ -1019,9 +389,9 @@ export class HistoryService
|
|
|
1019
389
|
return this.history.length === 0;
|
|
1020
390
|
}
|
|
1021
391
|
|
|
1022
|
-
/** Clone the history
|
|
392
|
+
/** Clone the history without serializing immutable media payloads. */
|
|
1023
393
|
clone(): IContent[] {
|
|
1024
|
-
return
|
|
394
|
+
return sanitizeProviderHistoryForSerialization(this.history);
|
|
1025
395
|
}
|
|
1026
396
|
|
|
1027
397
|
/**
|
|
@@ -1067,9 +437,21 @@ export class HistoryService
|
|
|
1067
437
|
/**
|
|
1068
438
|
* Get curated history with circular references removed for providers.
|
|
1069
439
|
* This ensures the history can be safely serialized and sent to providers.
|
|
440
|
+
* A request-scoped override lets semantic purge prepare an isolated candidate
|
|
441
|
+
* without mutating the live conversation before provider success.
|
|
1070
442
|
*/
|
|
1071
|
-
getCuratedForProvider(
|
|
1072
|
-
|
|
443
|
+
getCuratedForProvider(
|
|
444
|
+
tailContents: IContent[] = [],
|
|
445
|
+
historyOverride?: readonly IContent[],
|
|
446
|
+
): IContent[] {
|
|
447
|
+
const curated =
|
|
448
|
+
historyOverride === undefined
|
|
449
|
+
? this.getCurated()
|
|
450
|
+
: buildCuratedHistory(
|
|
451
|
+
this.logger,
|
|
452
|
+
[...historyOverride],
|
|
453
|
+
this.isCompressing,
|
|
454
|
+
);
|
|
1073
455
|
return buildProviderContent(curated, tailContents, this.logger);
|
|
1074
456
|
}
|
|
1075
457
|
|
|
@@ -1138,8 +520,8 @@ export class HistoryService
|
|
|
1138
520
|
/**
|
|
1139
521
|
* Mark compression as complete
|
|
1140
522
|
* This will flush all queued operations.
|
|
1141
|
-
*
|
|
1142
|
-
*
|
|
523
|
+
* When summary and itemsCompressed are provided, emits a compressionEnded
|
|
524
|
+
* event so the recording service can log the compression.
|
|
1143
525
|
*/
|
|
1144
526
|
endCompression(summary?: IContent, itemsCompressed?: number): void {
|
|
1145
527
|
this.logger.debug('Compression complete - unlocking history', {
|
|
@@ -1147,28 +529,21 @@ export class HistoryService
|
|
|
1147
529
|
});
|
|
1148
530
|
|
|
1149
531
|
this.isCompressing = false;
|
|
1150
|
-
this.pendingCompressionHighWaterReported = false;
|
|
1151
532
|
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
533
|
+
this.pendingOperations.flush(() => {
|
|
534
|
+
// Route the release events through the same mutation FIFO as the dequeued
|
|
535
|
+
// closures: if an asynchronous mutation was in flight, its rebuild
|
|
536
|
+
// contentAdded must land BEFORE these events (staying inside the recording
|
|
537
|
+
// suppression window), then the streaming content after them (#3264). When
|
|
538
|
+
// nothing is in flight this executes inline, behavior unchanged.
|
|
539
|
+
this.runSynchronousHistoryMutation(() => {
|
|
540
|
+
this.emit('compressionLockReleased');
|
|
541
|
+
|
|
542
|
+
if (summary && itemsCompressed !== undefined) {
|
|
543
|
+
this.emit('compressionEnded', summary, itemsCompressed);
|
|
544
|
+
}
|
|
545
|
+
});
|
|
1162
546
|
});
|
|
1163
|
-
|
|
1164
|
-
// Emitted after the queue drain so flushed rebuild content stays inside the
|
|
1165
|
-
// recording suppression window; fires unconditionally so a summary-less
|
|
1166
|
-
// end (no-op/failed compression) cannot wedge recording. (Issue #3263)
|
|
1167
|
-
this.emit('compressionLockReleased');
|
|
1168
|
-
|
|
1169
|
-
if (summary && itemsCompressed !== undefined) {
|
|
1170
|
-
this.emit('compressionEnded', summary, itemsCompressed);
|
|
1171
|
-
}
|
|
1172
547
|
}
|
|
1173
548
|
|
|
1174
549
|
/**
|