@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.
Files changed (417) hide show
  1. package/dist/index.d.ts +1 -1
  2. package/dist/index.js +1 -1
  3. package/dist/index.js.map +1 -1
  4. package/dist/prompt-config/defaults/default-prompts.json +3 -3
  5. package/dist/src/config/agentClientLifecycle.d.ts +4 -3
  6. package/dist/src/config/agentClientLifecycle.js +27 -14
  7. package/dist/src/config/agentClientLifecycle.js.map +1 -1
  8. package/dist/src/config/config.d.ts +2 -5
  9. package/dist/src/config/config.js +10 -23
  10. package/dist/src/config/config.js.map +1 -1
  11. package/dist/src/config/configBase.d.ts +5 -1
  12. package/dist/src/config/configBase.js +27 -6
  13. package/dist/src/config/configBase.js.map +1 -1
  14. package/dist/src/config/configBaseCore.d.ts +26 -8
  15. package/dist/src/config/configBaseCore.js +30 -26
  16. package/dist/src/config/configBaseCore.js.map +1 -1
  17. package/dist/src/config/configMcpRuntime.d.ts +16 -0
  18. package/dist/src/config/configMcpRuntime.js +31 -0
  19. package/dist/src/config/configMcpRuntime.js.map +1 -0
  20. package/dist/src/config/configMediaDefaults.d.ts +10 -0
  21. package/dist/src/config/configMediaDefaults.js +17 -0
  22. package/dist/src/config/configMediaDefaults.js.map +1 -0
  23. package/dist/src/config/configTestHarness.d.ts +4 -0
  24. package/dist/src/config/configTypes.d.ts +4 -54
  25. package/dist/src/config/configTypes.js +6 -82
  26. package/dist/src/config/configTypes.js.map +1 -1
  27. package/dist/src/config/index.d.ts +1 -2
  28. package/dist/src/config/index.js +1 -2
  29. package/dist/src/config/index.js.map +1 -1
  30. package/dist/src/config/models.d.ts +0 -4
  31. package/dist/src/config/models.js +0 -4
  32. package/dist/src/config/models.js.map +1 -1
  33. package/dist/src/config/toolRegistryFactory.d.ts +2 -2
  34. package/dist/src/core/clientContract.d.ts +4 -6
  35. package/dist/src/core/compression/transcriptPathNotice.d.ts +22 -0
  36. package/dist/src/core/compression/transcriptPathNotice.js +29 -0
  37. package/dist/src/core/compression/transcriptPathNotice.js.map +1 -0
  38. package/dist/src/core/contentGenerator.d.ts +2 -5
  39. package/dist/src/core/contentGenerator.js +10 -24
  40. package/dist/src/core/contentGenerator.js.map +1 -1
  41. package/dist/src/core/logger.d.ts +66 -0
  42. package/dist/src/core/logger.js +345 -30
  43. package/dist/src/core/logger.js.map +1 -1
  44. package/dist/src/core/prompts.d.ts +0 -1
  45. package/dist/src/core/prompts.js +5 -79
  46. package/dist/src/core/prompts.js.map +1 -1
  47. package/dist/src/core/turn.d.ts +3 -3
  48. package/dist/src/hooks/hookTranslator.js +5 -4
  49. package/dist/src/hooks/hookTranslator.js.map +1 -1
  50. package/dist/src/index.d.ts +8 -6
  51. package/dist/src/index.js +8 -6
  52. package/dist/src/index.js.map +1 -1
  53. package/dist/src/llm-types/index.d.ts +1 -1
  54. package/dist/src/llm-types/modelEnvelope.d.ts +2 -1
  55. package/dist/src/llm-types/modelEnvelope.js +17 -1
  56. package/dist/src/llm-types/modelEnvelope.js.map +1 -1
  57. package/dist/src/models/schema.d.ts +40 -40
  58. package/dist/src/policy/policies/read-only.toml +0 -5
  59. package/dist/src/prompt-config/TemplateEngine.js +0 -3
  60. package/dist/src/prompt-config/TemplateEngine.js.map +1 -1
  61. package/dist/src/prompt-config/defaults/providers/gemini/core.md +0 -2
  62. package/dist/src/prompt-config/defaults/providers/gemini/models/gemini-2.5-flash/core.md +0 -2
  63. package/dist/src/prompt-config/defaults/providers/gemini/models/gemini-2.5-flash/gemini-2-5-flash/core.md +0 -2
  64. package/dist/src/prompt-config/prompt-loader.d.ts +1 -1
  65. package/dist/src/prompt-config/prompt-loader.js +4 -4
  66. package/dist/src/prompt-config/prompt-loader.js.map +1 -1
  67. package/dist/src/prompt-config/types.d.ts +0 -1
  68. package/dist/src/recording/CheckpointService.d.ts +4 -0
  69. package/dist/src/recording/CheckpointService.js +67 -46
  70. package/dist/src/recording/CheckpointService.js.map +1 -1
  71. package/dist/src/recording/HistoryMutationService.d.ts +1 -1
  72. package/dist/src/recording/HistoryMutationService.js +27 -4
  73. package/dist/src/recording/HistoryMutationService.js.map +1 -1
  74. package/dist/src/recording/RecordingIntegration.d.ts +36 -2
  75. package/dist/src/recording/RecordingIntegration.js +224 -7
  76. package/dist/src/recording/RecordingIntegration.js.map +1 -1
  77. package/dist/src/recording/ReplayEngine.d.ts +8 -10
  78. package/dist/src/recording/ReplayEngine.js +161 -95
  79. package/dist/src/recording/ReplayEngine.js.map +1 -1
  80. package/dist/src/recording/SessionDiscovery.d.ts +4 -2
  81. package/dist/src/recording/SessionDiscovery.js +13 -5
  82. package/dist/src/recording/SessionDiscovery.js.map +1 -1
  83. package/dist/src/recording/SessionRecordingService.d.ts +23 -3
  84. package/dist/src/recording/SessionRecordingService.js +209 -51
  85. package/dist/src/recording/SessionRecordingService.js.map +1 -1
  86. package/dist/src/recording/SessionTransitionService.d.ts +7 -0
  87. package/dist/src/recording/SessionTransitionService.js +66 -13
  88. package/dist/src/recording/SessionTransitionService.js.map +1 -1
  89. package/dist/src/recording/index.d.ts +1 -0
  90. package/dist/src/recording/index.js +1 -0
  91. package/dist/src/recording/index.js.map +1 -1
  92. package/dist/src/recording/janitor/cleanupTypes.d.ts +3 -0
  93. package/dist/src/recording/janitor/cleanupTypes.js.map +1 -1
  94. package/dist/src/recording/janitor/mediaReclamation.d.ts +23 -0
  95. package/dist/src/recording/janitor/mediaReclamation.js +195 -0
  96. package/dist/src/recording/janitor/mediaReclamation.js.map +1 -0
  97. package/dist/src/recording/janitor/sessionJanitor.js +47 -1
  98. package/dist/src/recording/janitor/sessionJanitor.js.map +1 -1
  99. package/dist/src/recording/janitor/sessionScanner.d.ts +12 -9
  100. package/dist/src/recording/janitor/sessionScanner.js +248 -193
  101. package/dist/src/recording/janitor/sessionScanner.js.map +1 -1
  102. package/dist/src/recording/replay-test-helpers.d.ts +10 -1
  103. package/dist/src/recording/replay-test-helpers.js +24 -2
  104. package/dist/src/recording/replay-test-helpers.js.map +1 -1
  105. package/dist/src/recording/replayCheckpointMetadata.d.ts +28 -0
  106. package/dist/src/recording/replayCheckpointMetadata.js +96 -0
  107. package/dist/src/recording/replayCheckpointMetadata.js.map +1 -0
  108. package/dist/src/recording/replayErrorFormatting.d.ts +6 -0
  109. package/dist/src/recording/replayErrorFormatting.js +38 -0
  110. package/dist/src/recording/replayErrorFormatting.js.map +1 -0
  111. package/dist/src/recording/resumeSession.d.ts +3 -0
  112. package/dist/src/recording/resumeSession.js +7 -1
  113. package/dist/src/recording/resumeSession.js.map +1 -1
  114. package/dist/src/recording/semanticMediaPurgeReplayValidation.d.ts +14 -0
  115. package/dist/src/recording/semanticMediaPurgeReplayValidation.js +24 -0
  116. package/dist/src/recording/semanticMediaPurgeReplayValidation.js.map +1 -0
  117. package/dist/src/recording/session-media-package-blobs.d.ts +8 -0
  118. package/dist/src/recording/session-media-package-blobs.js +72 -0
  119. package/dist/src/recording/session-media-package-blobs.js.map +1 -0
  120. package/dist/src/recording/session-media-package-import.d.ts +26 -0
  121. package/dist/src/recording/session-media-package-import.js +124 -0
  122. package/dist/src/recording/session-media-package-import.js.map +1 -0
  123. package/dist/src/recording/session-media-package-state.d.ts +19 -0
  124. package/dist/src/recording/session-media-package-state.js +56 -0
  125. package/dist/src/recording/session-media-package-state.js.map +1 -0
  126. package/dist/src/recording/session-media-package-validation.d.ts +59 -0
  127. package/dist/src/recording/session-media-package-validation.js +286 -0
  128. package/dist/src/recording/session-media-package-validation.js.map +1 -0
  129. package/dist/src/recording/session-media-package-writer.d.ts +18 -0
  130. package/dist/src/recording/session-media-package-writer.js +70 -0
  131. package/dist/src/recording/session-media-package-writer.js.map +1 -0
  132. package/dist/src/recording/session-media-package.d.ts +27 -0
  133. package/dist/src/recording/session-media-package.js +366 -0
  134. package/dist/src/recording/session-media-package.js.map +1 -0
  135. package/dist/src/recording/types.d.ts +34 -2
  136. package/dist/src/recording/types.js.map +1 -1
  137. package/dist/src/runtime/AgentRuntimeContext.d.ts +22 -5
  138. package/dist/src/runtime/AgentRuntimeLoader.d.ts +6 -0
  139. package/dist/src/runtime/AgentRuntimeLoader.js +25 -6
  140. package/dist/src/runtime/AgentRuntimeLoader.js.map +1 -1
  141. package/dist/src/runtime/contracts/PromptEstimation.d.ts +1 -0
  142. package/dist/src/runtime/contracts/PromptEstimation.js.map +1 -1
  143. package/dist/src/runtime/contracts/RuntimeProvider.d.ts +3 -2
  144. package/dist/src/runtime/contracts/RuntimeProviderManager.d.ts +0 -2
  145. package/dist/src/runtime/createAgentRuntimeContext.js +28 -1
  146. package/dist/src/runtime/createAgentRuntimeContext.js.map +1 -1
  147. package/dist/src/runtime/providerRuntimeContext.d.ts +12 -0
  148. package/dist/src/runtime/providerRuntimeContext.js +3 -0
  149. package/dist/src/runtime/providerRuntimeContext.js.map +1 -1
  150. package/dist/src/runtime/runtimeAdapters.js +2 -2
  151. package/dist/src/runtime/runtimeAdapters.js.map +1 -1
  152. package/dist/src/services/history/ContentConverters.d.ts +11 -7
  153. package/dist/src/services/history/ContentConverters.js +11 -7
  154. package/dist/src/services/history/ContentConverters.js.map +1 -1
  155. package/dist/src/services/history/HistoryService.d.ts +18 -180
  156. package/dist/src/services/history/HistoryService.js +53 -523
  157. package/dist/src/services/history/HistoryService.js.map +1 -1
  158. package/dist/src/services/history/HistoryServiceCore.d.ts +278 -0
  159. package/dist/src/services/history/HistoryServiceCore.js +727 -0
  160. package/dist/src/services/history/HistoryServiceCore.js.map +1 -0
  161. package/dist/src/services/history/IContent.d.ts +118 -16
  162. package/dist/src/services/history/IContent.js +284 -4
  163. package/dist/src/services/history/IContent.js.map +1 -1
  164. package/dist/src/services/history/contentSize.js +22 -2
  165. package/dist/src/services/history/contentSize.js.map +1 -1
  166. package/dist/src/services/history/historyChronology.d.ts +7 -0
  167. package/dist/src/services/history/historyChronology.js +35 -3
  168. package/dist/src/services/history/historyChronology.js.map +1 -1
  169. package/dist/src/services/history/historyCloneUtils.d.ts +5 -4
  170. package/dist/src/services/history/historyCloneUtils.js +23 -16
  171. package/dist/src/services/history/historyCloneUtils.js.map +1 -1
  172. package/dist/src/services/history/historyCompressionQueue.d.ts +33 -0
  173. package/dist/src/services/history/historyCompressionQueue.js +81 -0
  174. package/dist/src/services/history/historyCompressionQueue.js.map +1 -0
  175. package/dist/src/services/history/historyEventTypes.d.ts +3 -0
  176. package/dist/src/services/history/historyMutationFailure.d.ts +15 -0
  177. package/dist/src/services/history/historyMutationFailure.js +22 -0
  178. package/dist/src/services/history/historyMutationFailure.js.map +1 -1
  179. package/dist/src/services/history/historyProviderPipeline.d.ts +1 -1
  180. package/dist/src/services/history/historyProviderPipeline.js +15 -5
  181. package/dist/src/services/history/historyProviderPipeline.js.map +1 -1
  182. package/dist/src/services/history/historyTokenEstimation.js +10 -6
  183. package/dist/src/services/history/historyTokenEstimation.js.map +1 -1
  184. package/dist/src/services/history/historyToolNormalization.d.ts +2 -0
  185. package/dist/src/services/history/historyToolNormalization.js +26 -9
  186. package/dist/src/services/history/historyToolNormalization.js.map +1 -1
  187. package/dist/src/services/history/provider-file-binding.d.ts +11 -0
  188. package/dist/src/services/history/provider-file-binding.js +70 -0
  189. package/dist/src/services/history/provider-file-binding.js.map +1 -0
  190. package/dist/src/services/history/semantic-media-purge.d.ts +66 -0
  191. package/dist/src/services/history/semantic-media-purge.js +404 -0
  192. package/dist/src/services/history/semantic-media-purge.js.map +1 -0
  193. package/dist/src/storage/SessionPersistenceService.d.ts +53 -2
  194. package/dist/src/storage/SessionPersistenceService.js +435 -23
  195. package/dist/src/storage/SessionPersistenceService.js.map +1 -1
  196. package/dist/src/storage/history-media-ownership.d.ts +38 -0
  197. package/dist/src/storage/history-media-ownership.js +136 -0
  198. package/dist/src/storage/history-media-ownership.js.map +1 -0
  199. package/dist/src/storage/local-media-store-child.d.ts +6 -0
  200. package/dist/src/storage/local-media-store-child.js +140 -0
  201. package/dist/src/storage/local-media-store-child.js.map +1 -0
  202. package/dist/src/storage/local-media-store-files.d.ts +44 -0
  203. package/dist/src/storage/local-media-store-files.js +460 -0
  204. package/dist/src/storage/local-media-store-files.js.map +1 -0
  205. package/dist/src/storage/local-media-store-lock-recovery.d.ts +15 -0
  206. package/dist/src/storage/local-media-store-lock-recovery.js +112 -0
  207. package/dist/src/storage/local-media-store-lock-recovery.js.map +1 -0
  208. package/dist/src/storage/local-media-store-persistence.d.ts +48 -0
  209. package/dist/src/storage/local-media-store-persistence.js +443 -0
  210. package/dist/src/storage/local-media-store-persistence.js.map +1 -0
  211. package/dist/src/storage/local-media-store-quota.d.ts +6 -0
  212. package/dist/src/storage/local-media-store-quota.js +66 -0
  213. package/dist/src/storage/local-media-store-quota.js.map +1 -0
  214. package/dist/src/storage/local-media-store-reservations.d.ts +55 -0
  215. package/dist/src/storage/local-media-store-reservations.js +377 -0
  216. package/dist/src/storage/local-media-store-reservations.js.map +1 -0
  217. package/dist/src/storage/local-media-store-types.d.ts +110 -0
  218. package/dist/src/storage/local-media-store-types.js +35 -0
  219. package/dist/src/storage/local-media-store-types.js.map +1 -0
  220. package/dist/src/storage/local-media-store-validation.d.ts +34 -0
  221. package/dist/src/storage/local-media-store-validation.js +449 -0
  222. package/dist/src/storage/local-media-store-validation.js.map +1 -0
  223. package/dist/src/storage/local-media-store.d.ts +47 -0
  224. package/dist/src/storage/local-media-store.js +353 -0
  225. package/dist/src/storage/local-media-store.js.map +1 -0
  226. package/dist/src/storage/media-admission-service.d.ts +49 -0
  227. package/dist/src/storage/media-admission-service.js +333 -0
  228. package/dist/src/storage/media-admission-service.js.map +1 -0
  229. package/dist/src/storage/media-lifecycle-metrics.d.ts +75 -0
  230. package/dist/src/storage/media-lifecycle-metrics.js +105 -0
  231. package/dist/src/storage/media-lifecycle-metrics.js.map +1 -0
  232. package/dist/src/storage/media-reference-lifecycle.d.ts +16 -0
  233. package/dist/src/storage/media-reference-lifecycle.js +84 -0
  234. package/dist/src/storage/media-reference-lifecycle.js.map +1 -0
  235. package/dist/src/storage/request-media-resolver.d.ts +67 -0
  236. package/dist/src/storage/request-media-resolver.js +325 -0
  237. package/dist/src/storage/request-media-resolver.js.map +1 -0
  238. package/dist/src/storage/session-media-service-factories.d.ts +19 -0
  239. package/dist/src/storage/session-media-service-factories.js +34 -0
  240. package/dist/src/storage/session-media-service-factories.js.map +1 -0
  241. package/dist/src/storage/session-persistence-helpers.d.ts +10 -0
  242. package/dist/src/storage/session-persistence-helpers.js +46 -0
  243. package/dist/src/storage/session-persistence-helpers.js.map +1 -0
  244. package/dist/src/test-utils/config.d.ts +2 -0
  245. package/dist/src/test-utils/config.js +5 -5
  246. package/dist/src/test-utils/config.js.map +1 -1
  247. package/dist/src/test-utils/runtime.d.ts +2 -0
  248. package/dist/src/test-utils/runtime.js +1 -2
  249. package/dist/src/test-utils/runtime.js.map +1 -1
  250. package/dist/src/tools-adapters/CoreShellToolHostAdapter.js +1 -9
  251. package/dist/src/tools-adapters/CoreShellToolHostAdapter.js.map +1 -1
  252. package/dist/src/tools-adapters/CoreSkillServiceAdapter.d.ts +0 -2
  253. package/dist/src/tools-adapters/CoreSkillServiceAdapter.js +0 -17
  254. package/dist/src/tools-adapters/CoreSkillServiceAdapter.js.map +1 -1
  255. package/dist/src/tools-adapters/CoreToolHostAdapter.d.ts +0 -21
  256. package/dist/src/tools-adapters/CoreToolHostAdapter.js +0 -8
  257. package/dist/src/tools-adapters/CoreToolHostAdapter.js.map +1 -1
  258. package/dist/src/utils/environmentContext.d.ts +8 -9
  259. package/dist/src/utils/environmentContext.js +11 -22
  260. package/dist/src/utils/environmentContext.js.map +1 -1
  261. package/dist/src/utils/errorParsing.d.ts +1 -2
  262. package/dist/src/utils/errorParsing.js +22 -41
  263. package/dist/src/utils/errorParsing.js.map +1 -1
  264. package/dist/src/utils/generateContentResponseUtilities.js +12 -0
  265. package/dist/src/utils/generateContentResponseUtilities.js.map +1 -1
  266. package/dist/src/utils/summarizer.d.ts +1 -2
  267. package/dist/src/utils/summarizer.js +6 -5
  268. package/dist/src/utils/summarizer.js.map +1 -1
  269. package/dist/src/utils/tool-utils.js +7 -1
  270. package/dist/src/utils/tool-utils.js.map +1 -1
  271. package/index.ts +0 -4
  272. package/package.json +38 -30
  273. package/src/config/agentClientLifecycle.ts +53 -22
  274. package/src/config/config.ts +11 -26
  275. package/src/config/configBase.ts +46 -7
  276. package/src/config/configBaseCore.ts +38 -36
  277. package/src/config/configMcpRuntime.ts +46 -0
  278. package/src/config/configMediaDefaults.ts +19 -0
  279. package/src/config/configTypes.ts +13 -53
  280. package/src/config/index.ts +1 -4
  281. package/src/config/models.ts +0 -6
  282. package/src/config/toolRegistryFactory.ts +2 -2
  283. package/src/core/clientContract.ts +4 -6
  284. package/src/core/compression/transcriptPathNotice.ts +31 -0
  285. package/src/core/contentGenerator.ts +15 -39
  286. package/src/core/logger.ts +370 -39
  287. package/src/core/prompts.ts +4 -114
  288. package/src/core/turn.ts +3 -3
  289. package/src/hooks/hookTranslator.ts +6 -4
  290. package/src/index.ts +7 -7
  291. package/src/llm-types/index.ts +5 -0
  292. package/src/llm-types/modelEnvelope.ts +26 -2
  293. package/src/policy/policies/read-only.toml +0 -5
  294. package/src/prompt-config/TemplateEngine.ts +0 -3
  295. package/src/prompt-config/defaults/providers/gemini/core.md +0 -2
  296. package/src/prompt-config/defaults/providers/gemini/models/gemini-2.5-flash/core.md +0 -2
  297. package/src/prompt-config/defaults/providers/gemini/models/gemini-2.5-flash/gemini-2-5-flash/core.md +0 -2
  298. package/src/prompt-config/prompt-loader.ts +7 -8
  299. package/src/prompt-config/types.ts +0 -1
  300. package/src/recording/CheckpointService.ts +110 -45
  301. package/src/recording/HistoryMutationService.ts +28 -3
  302. package/src/recording/RecordingIntegration.ts +258 -11
  303. package/src/recording/ReplayEngine.ts +220 -110
  304. package/src/recording/SessionDiscovery.ts +30 -4
  305. package/src/recording/SessionRecordingService.ts +253 -55
  306. package/src/recording/SessionTransitionService.ts +75 -12
  307. package/src/recording/index.ts +7 -0
  308. package/src/recording/janitor/cleanupTypes.ts +4 -0
  309. package/src/recording/janitor/mediaReclamation.ts +295 -0
  310. package/src/recording/janitor/sessionJanitor.ts +54 -1
  311. package/src/recording/janitor/sessionScanner.ts +319 -227
  312. package/src/recording/replay-test-helpers.ts +33 -2
  313. package/src/recording/replayCheckpointMetadata.ts +115 -0
  314. package/src/recording/replayErrorFormatting.ts +43 -0
  315. package/src/recording/resumeSession.ts +10 -0
  316. package/src/recording/semanticMediaPurgeReplayValidation.ts +35 -0
  317. package/src/recording/session-media-package-blobs.ts +116 -0
  318. package/src/recording/session-media-package-import.ts +189 -0
  319. package/src/recording/session-media-package-state.ts +120 -0
  320. package/src/recording/session-media-package-validation.ts +400 -0
  321. package/src/recording/session-media-package-writer.ts +117 -0
  322. package/src/recording/session-media-package.ts +692 -0
  323. package/src/recording/types.ts +37 -2
  324. package/src/runtime/AgentRuntimeContext.ts +28 -5
  325. package/src/runtime/AgentRuntimeLoader.ts +32 -7
  326. package/src/runtime/contracts/PromptEstimation.ts +1 -0
  327. package/src/runtime/contracts/RuntimeProvider.ts +3 -8
  328. package/src/runtime/contracts/RuntimeProviderManager.ts +0 -2
  329. package/src/runtime/createAgentRuntimeContext.ts +43 -1
  330. package/src/runtime/providerRuntimeContext.ts +22 -0
  331. package/src/runtime/runtimeAdapters.ts +2 -2
  332. package/src/services/history/ContentConverters.ts +11 -7
  333. package/src/services/history/HistoryService.ts +71 -696
  334. package/src/services/history/HistoryServiceCore.ts +1055 -0
  335. package/src/services/history/IContent.ts +460 -20
  336. package/src/services/history/contentSize.ts +22 -2
  337. package/src/services/history/historyChronology.ts +53 -3
  338. package/src/services/history/historyCloneUtils.ts +26 -22
  339. package/src/services/history/historyCompressionQueue.ts +102 -0
  340. package/src/services/history/historyEventTypes.ts +9 -0
  341. package/src/services/history/historyMutationFailure.ts +33 -0
  342. package/src/services/history/historyProviderPipeline.ts +19 -5
  343. package/src/services/history/historyTokenEstimation.ts +9 -5
  344. package/src/services/history/historyToolNormalization.ts +43 -11
  345. package/src/services/history/provider-file-binding.ts +115 -0
  346. package/src/services/history/semantic-media-purge.ts +593 -0
  347. package/src/storage/SessionPersistenceService.ts +667 -30
  348. package/src/storage/history-media-ownership.ts +185 -0
  349. package/src/storage/local-media-store-child.ts +163 -0
  350. package/src/storage/local-media-store-files.ts +643 -0
  351. package/src/storage/local-media-store-lock-recovery.ts +159 -0
  352. package/src/storage/local-media-store-persistence.ts +737 -0
  353. package/src/storage/local-media-store-quota.ts +111 -0
  354. package/src/storage/local-media-store-reservations.ts +485 -0
  355. package/src/storage/local-media-store-types.ts +161 -0
  356. package/src/storage/local-media-store-validation.ts +643 -0
  357. package/src/storage/local-media-store.ts +696 -0
  358. package/src/storage/media-admission-service.ts +512 -0
  359. package/src/storage/media-lifecycle-metrics.ts +228 -0
  360. package/src/storage/media-reference-lifecycle.ts +164 -0
  361. package/src/storage/request-media-resolver.ts +491 -0
  362. package/src/storage/session-media-service-factories.ts +48 -0
  363. package/src/storage/session-persistence-helpers.ts +85 -0
  364. package/src/test-utils/config.ts +7 -5
  365. package/src/test-utils/runtime.ts +3 -2
  366. package/src/tools-adapters/CoreShellToolHostAdapter.ts +1 -11
  367. package/src/tools-adapters/CoreSkillServiceAdapter.ts +0 -23
  368. package/src/tools-adapters/CoreToolHostAdapter.ts +0 -36
  369. package/src/utils/environmentContext.ts +11 -30
  370. package/src/utils/errorParsing.ts +19 -69
  371. package/src/utils/generateContentResponseUtilities.ts +28 -1
  372. package/src/utils/summarizer.ts +8 -10
  373. package/src/utils/tool-utils.ts +7 -1
  374. package/dist/src/code_assist/codeAssist.d.ts +0 -19
  375. package/dist/src/code_assist/codeAssist.js +0 -41
  376. package/dist/src/code_assist/codeAssist.js.map +0 -1
  377. package/dist/src/code_assist/contentGeneratorAdapters.d.ts +0 -49
  378. package/dist/src/code_assist/contentGeneratorAdapters.js +0 -197
  379. package/dist/src/code_assist/contentGeneratorAdapters.js.map +0 -1
  380. package/dist/src/code_assist/converter.d.ts +0 -74
  381. package/dist/src/code_assist/converter.js +0 -158
  382. package/dist/src/code_assist/converter.js.map +0 -1
  383. package/dist/src/code_assist/googleGenAIWrapper.d.ts +0 -27
  384. package/dist/src/code_assist/googleGenAIWrapper.js +0 -46
  385. package/dist/src/code_assist/googleGenAIWrapper.js.map +0 -1
  386. package/dist/src/code_assist/oauth-credential-storage.d.ts +0 -30
  387. package/dist/src/code_assist/oauth-credential-storage.js +0 -135
  388. package/dist/src/code_assist/oauth-credential-storage.js.map +0 -1
  389. package/dist/src/code_assist/server.d.ts +0 -44
  390. package/dist/src/code_assist/server.js +0 -154
  391. package/dist/src/code_assist/server.js.map +0 -1
  392. package/dist/src/code_assist/setup.d.ts +0 -20
  393. package/dist/src/code_assist/setup.js +0 -126
  394. package/dist/src/code_assist/setup.js.map +0 -1
  395. package/dist/src/code_assist/types.d.ts +0 -161
  396. package/dist/src/code_assist/types.js +0 -46
  397. package/dist/src/code_assist/types.js.map +0 -1
  398. package/dist/src/utils/getFolderStructure.d.ts +0 -31
  399. package/dist/src/utils/getFolderStructure.js +0 -240
  400. package/dist/src/utils/getFolderStructure.js.map +0 -1
  401. package/dist/src/utils/installationManager.d.ts +0 -16
  402. package/dist/src/utils/installationManager.js +0 -51
  403. package/dist/src/utils/installationManager.js.map +0 -1
  404. package/dist/src/utils/llm-edit-fixer.d.ts +0 -25
  405. package/dist/src/utils/llm-edit-fixer.js +0 -120
  406. package/dist/src/utils/llm-edit-fixer.js.map +0 -1
  407. package/src/code_assist/codeAssist.ts +0 -59
  408. package/src/code_assist/contentGeneratorAdapters.ts +0 -275
  409. package/src/code_assist/converter.ts +0 -288
  410. package/src/code_assist/googleGenAIWrapper.ts +0 -90
  411. package/src/code_assist/oauth-credential-storage.ts +0 -171
  412. package/src/code_assist/server.ts +0 -263
  413. package/src/code_assist/setup.ts +0 -189
  414. package/src/code_assist/types.ts +0 -201
  415. package/src/utils/getFolderStructure.ts +0 -382
  416. package/src/utils/installationManager.ts +0 -59
  417. 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
- // @plan:PLAN-20260603-ISSUE1584.P05 RuntimeTokenizerFactory used for injection path
22
- import type { RuntimeTokenizerFactory } from '../../runtime/contracts/RuntimeTokenizerFactory.js';
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
- // Preserve the CompressionConfig export from the same path for consumers.
69
- export type { CompressionConfig };
70
-
71
- import {
72
- type MutationFailure,
73
- combineMutationFailures,
74
- } from './historyMutationFailure.js';
75
-
76
- type QueuedHistoryMutation =
77
- | { kind: 'synchronous'; execute: () => void }
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
- * Service for managing conversation history in a provider-agnostic way.
87
- * All history is stored as IContent. Providers are responsible for converting
88
- * to/from their own formats.
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
- // If compression is active, queue this operation
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
- this.runSynchronousHistoryMutation(() => {
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 (deep copy). */
392
+ /** Clone the history without serializing immutable media payloads. */
1023
393
  clone(): IContent[] {
1024
- return JSON.parse(JSON.stringify(this.history));
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(tailContents: IContent[] = []): IContent[] {
1072
- const curated = this.getCurated();
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
- * Always emits a compressionLockReleased event so recording can stop suppressing
1142
- * content, while compressionEnded stays conditional on summary + itemsCompressed.
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
- // Flush all pending operations
1153
- const operations = this.pendingOperations;
1154
- this.pendingOperations = [];
1155
-
1156
- for (const operation of operations) {
1157
- operation();
1158
- }
1159
-
1160
- this.logger.debug('Flushed pending operations', {
1161
- count: operations.length,
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
  /**