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