@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
@@ -14,284 +14,376 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
- /**
18
- * Global session scanner for the janitor.
19
- *
20
- * Scans every direct child of `Storage.getGlobalTempDir()` whose name is
21
- * exactly a 64-character lowercase hexadecimal project hash. Within each
22
- * project hash directory it discovers `chats/session-*.jsonl` (raw recordings)
23
- * and `chats/archive/session-*.jsonl.gz` (cold archives). It uses bounded
24
- * concurrency and the canonical header reader — never reading entire files
25
- * into memory.
26
- *
27
- * Blast-radius safety (AC-10): symlinks are never followed; unknown files are
28
- * ignored; non-hash directories are skipped.
29
- */
30
-
17
+ import type { Dirent, Stats } from 'node:fs';
31
18
  import * as fs from 'node:fs/promises';
32
- import type { Stats } from 'node:fs';
33
19
  import * as path from 'node:path';
34
20
  import type { SessionCandidate } from './cleanupTypes.js';
35
21
  import { readSessionJsonlHeader } from './sessionHeaderReader.js';
36
22
 
37
- /** Regex matching a 64-character lowercase hex project hash directory name. */
38
23
  const PROJECT_HASH_RE = /^[0-9a-f]{64}$/;
39
-
40
- /** Bounded concurrency for stat/header operations within a directory. */
41
- const MAX_CONCURRENT_OPS = 8;
42
-
43
- /** Archive sub-directory name inside a chats directory. */
44
24
  export const ARCHIVE_DIR_NAME = 'archive';
45
-
46
- /** Prefix and suffix for raw session recordings. */
47
25
  const SESSION_PREFIX = 'session-';
48
26
  const SESSION_JSONL_SUFFIX = '.jsonl';
49
27
  const ARCHIVE_SUFFIX = '.jsonl.gz';
50
28
 
51
- /** Result of scanning the global temp tree. */
29
+ export interface SessionScanLimits {
30
+ readonly maxProjects: number;
31
+ readonly maxFiles: number;
32
+ readonly maxCandidateBytes: number;
33
+ }
34
+
35
+ const DEFAULT_SCAN_LIMITS: SessionScanLimits = {
36
+ maxProjects: 4096,
37
+ maxFiles: 100_000,
38
+ maxCandidateBytes: 16 * 1024 * 1024 * 1024,
39
+ };
40
+
52
41
  export interface ScanResult {
53
42
  readonly candidates: readonly SessionCandidate[];
54
43
  readonly chatsDirs: readonly string[];
55
- /** Number of non-benign (non-ENOENT) per-project scan failures (OCR 38/39). */
56
44
  readonly scanErrorCount: number;
45
+ readonly scanSkippedCount: number;
57
46
  }
58
47
 
59
- /** Internal batch result from scanning a single directory. */
60
- interface ScanBatch {
61
- readonly candidates: SessionCandidate[];
62
- readonly errors: number;
48
+ interface ScanBudget {
49
+ readonly limits: SessionScanLimits;
50
+ files: number;
51
+ bytes: number;
52
+ skipped: number;
53
+ errors: number;
54
+ exhausted: boolean;
63
55
  }
64
56
 
65
- const EMPTY_BATCH: ScanBatch = { candidates: [], errors: 0 };
57
+ function hasErrnoCode(error: unknown, code: string): boolean {
58
+ return (
59
+ typeof error === 'object' &&
60
+ error !== null &&
61
+ Reflect.get(error, 'code') === code
62
+ );
63
+ }
66
64
 
67
- /**
68
- * Determine whether a file uses allocated blocks and return the allocated
69
- * size when available, falling back to file length otherwise. On filesystems
70
- * that do not expose blocks (e.g. some Windows setups), this returns
71
- * `stat.size`.
72
- */
73
- function getFileSize(stat: Stats): number {
74
- const allocated =
75
- typeof stat.blocks === 'number' && stat.blksize > 0 ? stat.blocks * 512 : 0;
76
- return allocated > 0 ? allocated : stat.size;
65
+ function positiveLimit(value: number, name: string): number {
66
+ if (!Number.isSafeInteger(value) || value <= 0) {
67
+ throw new Error(`Invalid session scan limit ${name}`);
68
+ }
69
+ return value;
77
70
  }
78
71
 
79
- /**
80
- * Run an async mapper over an array with a bounded number of concurrent
81
- * operations, preserving the input order in the output.
82
- */
83
- async function boundedMap<T, R>(
84
- items: readonly T[],
85
- limit: number,
86
- mapper: (item: T) => Promise<R>,
87
- ): Promise<R[]> {
88
- const results = new Array<R>(items.length);
89
- let nextIndex = 0;
90
- const worker = async (): Promise<void> => {
91
- while (nextIndex < items.length) {
92
- const i = nextIndex++;
93
- results[i] = await mapper(items[i]);
94
- }
72
+ function validatedLimits(limits: SessionScanLimits): SessionScanLimits {
73
+ return {
74
+ maxProjects: positiveLimit(limits.maxProjects, 'maxProjects'),
75
+ maxFiles: positiveLimit(limits.maxFiles, 'maxFiles'),
76
+ maxCandidateBytes: positiveLimit(
77
+ limits.maxCandidateBytes,
78
+ 'maxCandidateBytes',
79
+ ),
95
80
  };
96
- const workerCount = Math.min(limit, items.length);
97
- await Promise.all(Array.from({ length: workerCount }, worker));
98
- return results;
99
81
  }
100
82
 
101
- /**
102
- * Scan the global temp directory tree for all session recordings and cold
103
- * archives across every 64-hex project-hash directory.
104
- *
105
- * @param globalTempDir - The return value of `Storage.getGlobalTempDir()`.
106
- * @param currentSessionId - The current process's session ID (to mark as active).
107
- * @returns Discovered candidates and the list of chats directories found.
108
- */
109
- export async function scanGlobalSessions(
110
- globalTempDir: string,
111
- currentSessionId?: string,
112
- ): Promise<ScanResult> {
113
- let topEntries: string[];
114
- try {
115
- topEntries = await fs.readdir(globalTempDir);
116
- } catch (error: unknown) {
117
- if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
118
- return { candidates: [], chatsDirs: [], scanErrorCount: 0 };
119
- }
120
- throw error;
121
- }
122
-
123
- const hashDirs = topEntries.filter((name) => PROJECT_HASH_RE.test(name));
124
- const chatsDirs: string[] = [];
125
- const allCandidates: SessionCandidate[] = [];
126
- let scanErrorCount = 0;
127
-
128
- // Process project-hash directories with bounded concurrency.
129
- await boundedMap(hashDirs, MAX_CONCURRENT_OPS, async (hashDirName) => {
130
- const hashDirPath = path.join(globalTempDir, hashDirName);
131
- const chatsDir = path.join(hashDirPath, 'chats');
83
+ function getFileSize(fileStat: Stats): number {
84
+ const allocated =
85
+ typeof fileStat.blocks === 'number' && fileStat.blksize > 0
86
+ ? fileStat.blocks * 512
87
+ : 0;
88
+ return allocated > 0 ? allocated : fileStat.size;
89
+ }
132
90
 
133
- let lstat: Stats;
134
- try {
135
- lstat = await fs.lstat(hashDirPath);
136
- } catch {
137
- return; // vanished between readdir and lstat — benign.
138
- }
139
- // Never follow symlinks (AC-10).
140
- if (lstat.isSymbolicLink()) return;
141
- if (!lstat.isDirectory()) return;
91
+ function markBudgetExhausted(budget: ScanBudget): void {
92
+ if (!budget.exhausted) {
93
+ budget.exhausted = true;
94
+ budget.skipped += 1;
95
+ }
96
+ }
142
97
 
143
- // Never follow symlinks at any directory boundary (AC-10). The chats
144
- // directory itself must be a real directory, not a symlink pointing
145
- // outside the project hash tree.
146
- let chatsExists = true;
147
- try {
148
- const chatsLstat = await fs.lstat(chatsDir);
149
- if (chatsLstat.isSymbolicLink() || !chatsLstat.isDirectory()) {
150
- chatsExists = false;
151
- }
152
- } catch {
153
- chatsExists = false;
154
- }
155
- if (!chatsExists) return;
98
+ function candidateBudgetAtLimit(budget: ScanBudget): boolean {
99
+ return (
100
+ budget.exhausted ||
101
+ budget.files >= budget.limits.maxFiles ||
102
+ budget.bytes >= budget.limits.maxCandidateBytes
103
+ );
104
+ }
156
105
 
157
- chatsDirs.push(chatsDir);
106
+ function reserveCandidateBudget(fileStat: Stats, budget: ScanBudget): boolean {
107
+ if (
108
+ candidateBudgetAtLimit(budget) ||
109
+ fileStat.size > budget.limits.maxCandidateBytes - budget.bytes
110
+ ) {
111
+ markBudgetExhausted(budget);
112
+ return false;
113
+ }
114
+ budget.files += 1;
115
+ budget.bytes += fileStat.size;
116
+ return true;
117
+ }
158
118
 
159
- const rawBatch = await scanRawSessions(
160
- chatsDir,
161
- hashDirName,
162
- currentSessionId,
163
- );
164
- allCandidates.push(...rawBatch.candidates);
165
- scanErrorCount += rawBatch.errors;
119
+ function rawFileName(name: string): boolean {
120
+ return (
121
+ name.startsWith(SESSION_PREFIX) &&
122
+ name.endsWith(SESSION_JSONL_SUFFIX) &&
123
+ !name.endsWith(ARCHIVE_SUFFIX)
124
+ );
125
+ }
166
126
 
167
- const archiveDir = path.join(chatsDir, ARCHIVE_DIR_NAME);
168
- const archiveBatch = await scanArchiveSessions(archiveDir, hashDirName);
169
- allCandidates.push(...archiveBatch.candidates);
170
- scanErrorCount += archiveBatch.errors;
171
- });
127
+ function archiveFileName(name: string): boolean {
128
+ return name.startsWith(SESSION_PREFIX) && name.endsWith(ARCHIVE_SUFFIX);
129
+ }
172
130
 
173
- return { candidates: allCandidates, chatsDirs, scanErrorCount };
131
+ async function scanRawSessionEntry(
132
+ entry: Dirent,
133
+ chatsDir: string,
134
+ projectHashDir: string,
135
+ currentSessionId: string | undefined,
136
+ budget: ScanBudget,
137
+ ): Promise<SessionCandidate | undefined> {
138
+ if (!entry.isFile() || !rawFileName(entry.name)) return undefined;
139
+ const filePath = path.join(chatsDir, entry.name);
140
+ try {
141
+ const fileStat = await fs.lstat(filePath);
142
+ if (fileStat.isSymbolicLink() || !fileStat.isFile()) return undefined;
143
+ if (!reserveCandidateBudget(fileStat, budget)) return undefined;
144
+ const header = await readSessionJsonlHeader(filePath);
145
+ return {
146
+ kind: 'raw',
147
+ filePath,
148
+ fileName: entry.name,
149
+ containerDir: chatsDir,
150
+ projectHashDir,
151
+ sessionId: header?.sessionId ?? null,
152
+ isCurrentSession:
153
+ header?.sessionId !== undefined &&
154
+ currentSessionId !== undefined &&
155
+ header.sessionId === currentSessionId,
156
+ sizeBytes: getFileSize(fileStat),
157
+ mtime: fileStat.mtime,
158
+ dev: fileStat.dev,
159
+ ino: fileStat.ino,
160
+ };
161
+ } catch {
162
+ budget.errors += 1;
163
+ return undefined;
164
+ }
174
165
  }
175
166
 
176
- /** Scan `session-*.jsonl` files inside a chats directory. */
177
167
  async function scanRawSessions(
178
168
  chatsDir: string,
179
169
  projectHashDir: string,
180
- currentSessionId?: string,
181
- ): Promise<ScanBatch> {
182
- let files: string[];
170
+ currentSessionId: string | undefined,
171
+ budget: ScanBudget,
172
+ ): Promise<SessionCandidate[]> {
173
+ const candidates: SessionCandidate[] = [];
174
+ let directory: Awaited<ReturnType<typeof fs.opendir>>;
183
175
  try {
184
- files = await fs.readdir(chatsDir);
185
- } catch (error: unknown) {
186
- const code = (error as NodeJS.ErrnoException).code;
187
- if (code === 'ENOENT') return EMPTY_BATCH;
188
- // Non-ENOENT error (EACCES, EIO, …) — count as a scan failure (OCR 38/39).
189
- return { candidates: [], errors: 1 };
176
+ directory = await fs.opendir(chatsDir);
177
+ } catch (error) {
178
+ if (!hasErrnoCode(error, 'ENOENT')) budget.errors += 1;
179
+ return candidates;
190
180
  }
191
-
192
- const sessionFiles = files.filter(
193
- (f) =>
194
- f.startsWith(SESSION_PREFIX) &&
195
- f.endsWith(SESSION_JSONL_SUFFIX) &&
196
- !f.endsWith(ARCHIVE_SUFFIX),
197
- );
198
-
199
- let perFileErrors = 0;
200
- const candidates = (
201
- await boundedMap(sessionFiles, MAX_CONCURRENT_OPS, async (fileName) => {
202
- const filePath = path.join(chatsDir, fileName);
203
- try {
204
- const lstat = await fs.lstat(filePath);
205
- if (lstat.isSymbolicLink()) return null;
206
- if (!lstat.isFile()) return null;
207
- const header = await readSessionJsonlHeader(filePath);
208
- const candidate: SessionCandidate = {
209
- kind: 'raw',
210
- filePath,
211
- fileName,
212
- containerDir: chatsDir,
213
- projectHashDir,
214
- sessionId: header?.sessionId ?? null,
215
- isCurrentSession:
216
- header?.sessionId != null &&
217
- currentSessionId != null &&
218
- header.sessionId === currentSessionId,
219
- sizeBytes: getFileSize(lstat),
220
- mtime: lstat.mtime,
221
- dev: lstat.dev,
222
- ino: lstat.ino,
223
- };
224
- return candidate;
225
- } catch {
226
- perFileErrors++;
227
- return null;
181
+ try {
182
+ for await (const entry of directory) {
183
+ if (candidateBudgetAtLimit(budget)) {
184
+ markBudgetExhausted(budget);
185
+ break;
228
186
  }
229
- })
230
- ).filter((c): c is SessionCandidate => c !== null);
187
+ const candidate = await scanRawSessionEntry(
188
+ entry,
189
+ chatsDir,
190
+ projectHashDir,
191
+ currentSessionId,
192
+ budget,
193
+ );
194
+ if (candidate !== undefined) candidates.push(candidate);
195
+ }
196
+ } catch (error) {
197
+ if (!hasErrnoCode(error, 'ENOENT')) budget.errors += 1;
198
+ }
199
+ return candidates;
200
+ }
231
201
 
232
- return { candidates, errors: perFileErrors };
202
+ async function scanArchiveSessionEntry(
203
+ entry: Dirent,
204
+ archiveDir: string,
205
+ projectHashDir: string,
206
+ budget: ScanBudget,
207
+ ): Promise<SessionCandidate | undefined> {
208
+ if (!entry.isFile() || !archiveFileName(entry.name)) return undefined;
209
+ const filePath = path.join(archiveDir, entry.name);
210
+ try {
211
+ const fileStat = await fs.lstat(filePath);
212
+ if (fileStat.isSymbolicLink() || !fileStat.isFile()) return undefined;
213
+ if (!reserveCandidateBudget(fileStat, budget)) return undefined;
214
+ return {
215
+ kind: 'archive',
216
+ filePath,
217
+ fileName: entry.name,
218
+ containerDir: archiveDir,
219
+ projectHashDir,
220
+ sessionId: null,
221
+ isCurrentSession: false,
222
+ sizeBytes: getFileSize(fileStat),
223
+ mtime: fileStat.mtime,
224
+ dev: fileStat.dev,
225
+ ino: fileStat.ino,
226
+ };
227
+ } catch {
228
+ budget.errors += 1;
229
+ return undefined;
230
+ }
233
231
  }
234
232
 
235
- /** Scan `session-*.jsonl.gz` archive files inside a chats/archive directory. */
236
233
  async function scanArchiveSessions(
237
234
  archiveDir: string,
238
235
  projectHashDir: string,
239
- ): Promise<ScanBatch> {
240
- // Never follow a symlinked archive directory, and require a real directory
241
- // (not a regular file) so readdir does not throw ENOTDIR (AC-10, OCR 38).
236
+ budget: ScanBudget,
237
+ ): Promise<SessionCandidate[]> {
238
+ const candidates: SessionCandidate[] = [];
239
+ if (budget.exhausted || candidateBudgetAtLimit(budget)) return candidates;
242
240
  try {
243
- const archiveLstat = await fs.lstat(archiveDir);
244
- if (archiveLstat.isSymbolicLink() || !archiveLstat.isDirectory()) {
245
- return EMPTY_BATCH;
241
+ const archiveStat = await fs.lstat(archiveDir);
242
+ if (archiveStat.isSymbolicLink() || !archiveStat.isDirectory()) {
243
+ return candidates;
246
244
  }
247
- } catch (error: unknown) {
248
- const code = (error as NodeJS.ErrnoException).code;
249
- if (code === 'ENOENT') return EMPTY_BATCH;
250
- // Non-ENOENT error (EACCES, EIO, …) — count as a scan failure (OCR 39).
251
- return { candidates: [], errors: 1 };
245
+ } catch (error) {
246
+ if (!hasErrnoCode(error, 'ENOENT')) budget.errors += 1;
247
+ return candidates;
252
248
  }
253
-
254
- let files: string[];
249
+ let directory: Awaited<ReturnType<typeof fs.opendir>>;
250
+ try {
251
+ directory = await fs.opendir(archiveDir);
252
+ } catch (error) {
253
+ if (!hasErrnoCode(error, 'ENOENT')) budget.errors += 1;
254
+ return candidates;
255
+ }
256
+ try {
257
+ for await (const entry of directory) {
258
+ if (candidateBudgetAtLimit(budget)) {
259
+ markBudgetExhausted(budget);
260
+ break;
261
+ }
262
+ const candidate = await scanArchiveSessionEntry(
263
+ entry,
264
+ archiveDir,
265
+ projectHashDir,
266
+ budget,
267
+ );
268
+ if (candidate !== undefined) candidates.push(candidate);
269
+ }
270
+ } catch (error) {
271
+ if (!hasErrnoCode(error, 'ENOENT')) budget.errors += 1;
272
+ }
273
+ return candidates;
274
+ }
275
+ async function validatedChatsDirectory(
276
+ globalTempDir: string,
277
+ projectName: string,
278
+ budget: ScanBudget,
279
+ ): Promise<string | undefined> {
280
+ const projectPath = path.join(globalTempDir, projectName);
281
+ const chatsDir = path.join(projectPath, 'chats');
255
282
  try {
256
- files = await fs.readdir(archiveDir);
257
- } catch (error: unknown) {
258
- const code = (error as NodeJS.ErrnoException).code;
259
- if (code === 'ENOENT') return EMPTY_BATCH;
260
- return { candidates: [], errors: 1 };
283
+ const projectStat = await fs.lstat(projectPath);
284
+ if (projectStat.isSymbolicLink() || !projectStat.isDirectory()) {
285
+ return undefined;
286
+ }
287
+ const chatsStat = await fs.lstat(chatsDir);
288
+ if (chatsStat.isSymbolicLink() || !chatsStat.isDirectory()) {
289
+ return undefined;
290
+ }
291
+ return chatsDir;
292
+ } catch (error) {
293
+ if (!hasErrnoCode(error, 'ENOENT')) budget.errors += 1;
294
+ return undefined;
261
295
  }
296
+ }
262
297
 
263
- const archiveFiles = files.filter(
264
- (f) => f.startsWith(SESSION_PREFIX) && f.endsWith(ARCHIVE_SUFFIX),
298
+ async function appendProjectSessions(
299
+ globalTempDir: string,
300
+ projectName: string,
301
+ currentSessionId: string | undefined,
302
+ budget: ScanBudget,
303
+ candidates: SessionCandidate[],
304
+ chatsDirs: string[],
305
+ ): Promise<void> {
306
+ const chatsDir = await validatedChatsDirectory(
307
+ globalTempDir,
308
+ projectName,
309
+ budget,
265
310
  );
311
+ if (chatsDir === undefined) return;
312
+ chatsDirs.push(chatsDir);
313
+ candidates.push(
314
+ ...(await scanRawSessions(chatsDir, projectName, currentSessionId, budget)),
315
+ );
316
+ if (budget.exhausted) return;
317
+ candidates.push(
318
+ ...(await scanArchiveSessions(
319
+ path.join(chatsDir, ARCHIVE_DIR_NAME),
320
+ projectName,
321
+ budget,
322
+ )),
323
+ );
324
+ }
325
+ function projectBudgetAtLimit(projects: number, budget: ScanBudget): boolean {
326
+ if (projects < budget.limits.maxProjects && !budget.exhausted) return false;
327
+ if (!budget.exhausted) budget.skipped += 1;
328
+ return true;
329
+ }
266
330
 
267
- let perFileErrors = 0;
268
- const candidates = (
269
- await boundedMap(archiveFiles, MAX_CONCURRENT_OPS, async (fileName) => {
270
- const filePath = path.join(archiveDir, fileName);
271
- try {
272
- const lstat = await fs.lstat(filePath);
273
- if (lstat.isSymbolicLink()) return null;
274
- if (!lstat.isFile()) return null;
275
- const candidate: SessionCandidate = {
276
- kind: 'archive',
277
- filePath,
278
- fileName,
279
- containerDir: archiveDir,
280
- projectHashDir,
281
- sessionId: null,
282
- isCurrentSession: false,
283
- sizeBytes: getFileSize(lstat),
284
- mtime: lstat.mtime,
285
- dev: lstat.dev,
286
- ino: lstat.ino,
287
- };
288
- return candidate;
289
- } catch {
290
- perFileErrors++;
291
- return null;
292
- }
293
- })
294
- ).filter((c): c is SessionCandidate => c !== null);
295
-
296
- return { candidates, errors: perFileErrors };
331
+ /**
332
+ * Scan session recordings sequentially under explicit candidate and byte limits.
333
+ *
334
+ * @param globalTempDir - Global session temporary directory.
335
+ * @param currentSessionId - Session identifier that must be marked active.
336
+ * @param limits - Finite project, file, and aggregate-byte bounds.
337
+ * @returns Candidates, discovered chats directories, errors, and skipped entries.
338
+ */
339
+ export async function scanGlobalSessions(
340
+ globalTempDir: string,
341
+ currentSessionId?: string,
342
+ limits: SessionScanLimits = DEFAULT_SCAN_LIMITS,
343
+ ): Promise<ScanResult> {
344
+ const budget: ScanBudget = {
345
+ limits: validatedLimits(limits),
346
+ files: 0,
347
+ bytes: 0,
348
+ skipped: 0,
349
+ errors: 0,
350
+ exhausted: false,
351
+ };
352
+ const candidates: SessionCandidate[] = [];
353
+ const chatsDirs: string[] = [];
354
+ let projects = 0;
355
+ let directory: Awaited<ReturnType<typeof fs.opendir>>;
356
+ try {
357
+ directory = await fs.opendir(globalTempDir);
358
+ } catch (error) {
359
+ if (hasErrnoCode(error, 'ENOENT')) {
360
+ return {
361
+ candidates,
362
+ chatsDirs,
363
+ scanErrorCount: 0,
364
+ scanSkippedCount: 0,
365
+ };
366
+ }
367
+ throw error;
368
+ }
369
+ for await (const entry of directory) {
370
+ if (PROJECT_HASH_RE.test(entry.name)) {
371
+ if (projectBudgetAtLimit(projects, budget)) break;
372
+ projects += 1;
373
+ await appendProjectSessions(
374
+ globalTempDir,
375
+ entry.name,
376
+ currentSessionId,
377
+ budget,
378
+ candidates,
379
+ chatsDirs,
380
+ );
381
+ }
382
+ }
383
+ return {
384
+ candidates,
385
+ chatsDirs,
386
+ scanErrorCount: budget.errors,
387
+ scanSkippedCount: budget.skipped,
388
+ };
297
389
  }
@@ -138,17 +138,48 @@ export function compressedLine(
138
138
 
139
139
  /**
140
140
  * Build a valid JSONL line for a rewind event.
141
+ *
142
+ * `cutSeq` is the chronology `seq` of the first removed item (#2934). When it
143
+ * is omitted the payload carries no `cutSeq` key, matching legacy count-only
144
+ * rewind events.
141
145
  */
142
- export function rewindLine(seq: number, itemsRemoved: number): string {
146
+ export function rewindLine(
147
+ seq: number,
148
+ itemsRemoved: number,
149
+ cutSeq?: number,
150
+ ): string {
143
151
  return JSON.stringify({
144
152
  v: 1,
145
153
  seq,
146
154
  ts: new Date().toISOString(),
147
155
  type: 'rewind',
148
- payload: { itemsRemoved },
156
+ payload: cutSeq === undefined ? { itemsRemoved } : { itemsRemoved, cutSeq },
149
157
  });
150
158
  }
151
159
 
160
+ /**
161
+ * Build a text content entry carrying an explicit chronology marker, as
162
+ * HistoryService stamps them before the entry reaches the journal (#1721).
163
+ */
164
+ export function makeMarkedContent(
165
+ text: string,
166
+ speaker: IContent['speaker'],
167
+ chronologySeq: number,
168
+ ): IContent {
169
+ return {
170
+ speaker,
171
+ blocks: [{ type: 'text', text }],
172
+ metadata: {
173
+ chronology: {
174
+ seq: chronologySeq,
175
+ userTurn: 1,
176
+ step: chronologySeq,
177
+ recordedAt: 1787000000000,
178
+ },
179
+ },
180
+ };
181
+ }
182
+
152
183
  /**
153
184
  * Build a valid JSONL line for a provider_switch event.
154
185
  */