@downcity/agent 1.1.430 → 1.1.437

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 (488) hide show
  1. package/README.md +13 -11
  2. package/bin/agent/Agent.d.ts +3 -3
  3. package/bin/agent/Agent.d.ts.map +1 -1
  4. package/bin/agent/Agent.js +38 -8
  5. package/bin/agent/Agent.js.map +1 -1
  6. package/bin/agent/AgentInstructions.js +2 -2
  7. package/bin/agent/AgentInstructions.js.map +1 -1
  8. package/bin/agent/AgentMemoryStorage.d.ts.map +1 -1
  9. package/bin/agent/AgentMemoryStorage.js +1 -0
  10. package/bin/agent/AgentMemoryStorage.js.map +1 -1
  11. package/bin/agent/AgentSessions.d.ts +5 -1
  12. package/bin/agent/AgentSessions.d.ts.map +1 -1
  13. package/bin/agent/AgentSessions.js +17 -9
  14. package/bin/agent/AgentSessions.js.map +1 -1
  15. package/bin/executor/Executor.d.ts +9 -27
  16. package/bin/executor/Executor.d.ts.map +1 -1
  17. package/bin/executor/Executor.js +32 -127
  18. package/bin/executor/Executor.js.map +1 -1
  19. package/bin/executor/composer/system/default/SystemPromptAssets.d.ts +0 -4
  20. package/bin/executor/composer/system/default/SystemPromptAssets.d.ts.map +1 -1
  21. package/bin/executor/composer/system/default/SystemPromptAssets.js +0 -5
  22. package/bin/executor/composer/system/default/SystemPromptAssets.js.map +1 -1
  23. package/bin/executor/core-engine/CoreEngineContextCompaction.d.ts +3 -9
  24. package/bin/executor/core-engine/CoreEngineContextCompaction.d.ts.map +1 -1
  25. package/bin/executor/core-engine/CoreEngineContextCompaction.js +4 -130
  26. package/bin/executor/core-engine/CoreEngineContextCompaction.js.map +1 -1
  27. package/bin/executor/core-engine/CoreEngineRunner.d.ts +3 -30
  28. package/bin/executor/core-engine/CoreEngineRunner.d.ts.map +1 -1
  29. package/bin/executor/core-engine/CoreEngineRunner.js +38 -194
  30. package/bin/executor/core-engine/CoreEngineRunner.js.map +1 -1
  31. package/bin/executor/core-engine/CoreEngineSignals.d.ts +6 -6
  32. package/bin/executor/core-engine/CoreEngineSignals.d.ts.map +1 -1
  33. package/bin/executor/core-engine/CoreEngineSignals.js +1 -1
  34. package/bin/executor/core-engine/CoreEngineSignals.js.map +1 -1
  35. package/bin/executor/messages/SessionAttachmentMapper.d.ts +2 -2
  36. package/bin/executor/messages/SessionAttachmentMapper.d.ts.map +1 -1
  37. package/bin/executor/messages/SessionAttachmentMapper.js.map +1 -1
  38. package/bin/executor/messages/SessionMessageLog.d.ts +3 -3
  39. package/bin/executor/messages/SessionMessageLog.d.ts.map +1 -1
  40. package/bin/executor/messages/SessionMessageLog.js.map +1 -1
  41. package/bin/executor/messages/SessionModelMessages.d.ts +0 -3
  42. package/bin/executor/messages/SessionModelMessages.d.ts.map +1 -1
  43. package/bin/executor/messages/SessionModelMessages.js +80 -97
  44. package/bin/executor/messages/SessionModelMessages.js.map +1 -1
  45. package/bin/executor/model/ModelStepRunner.d.ts +2 -6
  46. package/bin/executor/model/ModelStepRunner.d.ts.map +1 -1
  47. package/bin/executor/model/ModelStepRunner.js +0 -28
  48. package/bin/executor/model/ModelStepRunner.js.map +1 -1
  49. package/bin/executor/services/ExecutorRecoveryPolicy.d.ts +7 -64
  50. package/bin/executor/services/ExecutorRecoveryPolicy.d.ts.map +1 -1
  51. package/bin/executor/services/ExecutorRecoveryPolicy.js +29 -45
  52. package/bin/executor/services/ExecutorRecoveryPolicy.js.map +1 -1
  53. package/bin/executor/types/SessionHistoryMeta.d.ts +1 -3
  54. package/bin/executor/types/SessionHistoryMeta.d.ts.map +1 -1
  55. package/bin/executor/types/SessionHistoryMeta.js +1 -1
  56. package/bin/index.d.ts +9 -13
  57. package/bin/index.d.ts.map +1 -1
  58. package/bin/index.js +2 -4
  59. package/bin/index.js.map +1 -1
  60. package/bin/session/DefaultSessionComposer.d.ts +13 -7
  61. package/bin/session/DefaultSessionComposer.d.ts.map +1 -1
  62. package/bin/session/DefaultSessionComposer.js +26 -21
  63. package/bin/session/DefaultSessionComposer.js.map +1 -1
  64. package/bin/session/Session.d.ts +12 -19
  65. package/bin/session/Session.d.ts.map +1 -1
  66. package/bin/session/Session.js +70 -202
  67. package/bin/session/Session.js.map +1 -1
  68. package/bin/session/SessionComposition.d.ts +0 -2
  69. package/bin/session/SessionComposition.d.ts.map +1 -1
  70. package/bin/session/SessionComposition.js +30 -54
  71. package/bin/session/SessionComposition.js.map +1 -1
  72. package/bin/session/SessionLoop.d.ts +17 -16
  73. package/bin/session/SessionLoop.d.ts.map +1 -1
  74. package/bin/session/SessionLoop.js +217 -320
  75. package/bin/session/SessionLoop.js.map +1 -1
  76. package/bin/session/SessionMessages.d.ts +52 -44
  77. package/bin/session/SessionMessages.d.ts.map +1 -1
  78. package/bin/session/SessionMessages.js +285 -187
  79. package/bin/session/SessionMessages.js.map +1 -1
  80. package/bin/session/SessionQueue.d.ts +1 -1
  81. package/bin/session/SessionQueue.d.ts.map +1 -1
  82. package/bin/session/SessionQueue.js +2 -1
  83. package/bin/session/SessionQueue.js.map +1 -1
  84. package/bin/session/SessionState.d.ts +3 -7
  85. package/bin/session/SessionState.d.ts.map +1 -1
  86. package/bin/session/SessionState.js +50 -89
  87. package/bin/session/SessionState.js.map +1 -1
  88. package/bin/session/SessionSystem.d.ts +5 -1
  89. package/bin/session/SessionSystem.d.ts.map +1 -1
  90. package/bin/session/SessionSystem.js +40 -0
  91. package/bin/session/SessionSystem.js.map +1 -1
  92. package/bin/session/SessionTitle.d.ts +3 -3
  93. package/bin/session/SessionTitle.d.ts.map +1 -1
  94. package/bin/session/SessionTitle.js +2 -2
  95. package/bin/session/browse/Browse.d.ts +32 -46
  96. package/bin/session/browse/Browse.d.ts.map +1 -1
  97. package/bin/session/browse/Browse.js +104 -360
  98. package/bin/session/browse/Browse.js.map +1 -1
  99. package/bin/session/browse/SessionMessageTimeline.d.ts.map +1 -1
  100. package/bin/session/browse/SessionMessageTimeline.js +61 -37
  101. package/bin/session/browse/SessionMessageTimeline.js.map +1 -1
  102. package/bin/session/composer/policies/AdaptivePartContextPolicy.d.ts +35 -0
  103. package/bin/session/composer/policies/AdaptivePartContextPolicy.d.ts.map +1 -0
  104. package/bin/session/composer/policies/AdaptivePartContextPolicy.js +256 -0
  105. package/bin/session/composer/policies/AdaptivePartContextPolicy.js.map +1 -0
  106. package/bin/session/composer/policies/FullHistoryContextPolicy.d.ts +24 -0
  107. package/bin/session/composer/policies/FullHistoryContextPolicy.d.ts.map +1 -0
  108. package/bin/session/composer/policies/FullHistoryContextPolicy.js +31 -0
  109. package/bin/session/composer/policies/FullHistoryContextPolicy.js.map +1 -0
  110. package/bin/session/composer/policies/SessionSummaryPrompts.d.ts +16 -0
  111. package/bin/session/composer/policies/SessionSummaryPrompts.d.ts.map +1 -0
  112. package/bin/session/composer/policies/SessionSummaryPrompts.js +32 -0
  113. package/bin/session/composer/policies/SessionSummaryPrompts.js.map +1 -0
  114. package/bin/session/execution/SessionAssistantOutputAdapter.d.ts +15 -4
  115. package/bin/session/execution/SessionAssistantOutputAdapter.d.ts.map +1 -1
  116. package/bin/session/execution/SessionAssistantOutputAdapter.js +7 -3
  117. package/bin/session/execution/SessionAssistantOutputAdapter.js.map +1 -1
  118. package/bin/session/messages/{SessionActionMessageWriter.d.ts → SessionAgentActionPartWriter.d.ts} +7 -7
  119. package/bin/session/messages/SessionAgentActionPartWriter.d.ts.map +1 -0
  120. package/bin/session/messages/{SessionActionMessageWriter.js → SessionAgentActionPartWriter.js} +7 -7
  121. package/bin/session/messages/SessionAgentActionPartWriter.js.map +1 -0
  122. package/bin/session/messages/SessionAgentContent.d.ts +10 -0
  123. package/bin/session/messages/SessionAgentContent.d.ts.map +1 -0
  124. package/bin/session/messages/SessionAgentContent.js +44 -0
  125. package/bin/session/messages/SessionAgentContent.js.map +1 -0
  126. package/bin/session/messages/SessionAgentMessageState.d.ts +50 -0
  127. package/bin/session/messages/SessionAgentMessageState.d.ts.map +1 -0
  128. package/bin/session/messages/SessionAgentMessageState.js +283 -0
  129. package/bin/session/messages/SessionAgentMessageState.js.map +1 -0
  130. package/bin/session/messages/{SessionAssistantMessageWriter.d.ts → SessionAgentMessageWriter.d.ts} +22 -8
  131. package/bin/session/messages/SessionAgentMessageWriter.d.ts.map +1 -0
  132. package/bin/session/messages/{SessionAssistantMessageWriter.js → SessionAgentMessageWriter.js} +54 -42
  133. package/bin/session/messages/SessionAgentMessageWriter.js.map +1 -0
  134. package/bin/session/messages/SessionForkMessageFiles.d.ts +11 -0
  135. package/bin/session/messages/SessionForkMessageFiles.d.ts.map +1 -1
  136. package/bin/session/messages/SessionForkMessageFiles.js +8 -2
  137. package/bin/session/messages/SessionForkMessageFiles.js.map +1 -1
  138. package/bin/session/messages/SessionMessageInteractionWriter.d.ts +6 -6
  139. package/bin/session/messages/SessionMessageInteractionWriter.d.ts.map +1 -1
  140. package/bin/session/messages/SessionMessageInteractionWriter.js +4 -4
  141. package/bin/session/messages/SessionMessageInteractionWriter.js.map +1 -1
  142. package/bin/session/messages/SessionMessageText.d.ts +3 -3
  143. package/bin/session/messages/SessionMessageText.d.ts.map +1 -1
  144. package/bin/session/messages/SessionMessageText.js +0 -2
  145. package/bin/session/messages/SessionMessageText.js.map +1 -1
  146. package/bin/session/messages/SessionTurnFileDiffData.d.ts +3 -3
  147. package/bin/session/messages/SessionTurnFileDiffData.d.ts.map +1 -1
  148. package/bin/session/messages/SessionTurnFileDiffData.js.map +1 -1
  149. package/bin/session/messages/SessionUserMessage.d.ts +3 -3
  150. package/bin/session/messages/SessionUserMessage.d.ts.map +1 -1
  151. package/bin/session/messages/SessionUserMessage.js +6 -3
  152. package/bin/session/messages/SessionUserMessage.js.map +1 -1
  153. package/bin/session/preview/SessionMessagePreview.d.ts.map +1 -1
  154. package/bin/session/preview/SessionMessagePreview.js +25 -9
  155. package/bin/session/preview/SessionMessagePreview.js.map +1 -1
  156. package/bin/session/runtime/SessionTurnCompletion.d.ts +15 -0
  157. package/bin/session/runtime/SessionTurnCompletion.d.ts.map +1 -0
  158. package/bin/session/runtime/SessionTurnCompletion.js +126 -0
  159. package/bin/session/runtime/SessionTurnCompletion.js.map +1 -0
  160. package/bin/session/runtime/SessionTurnContext.d.ts.map +1 -1
  161. package/bin/session/runtime/SessionTurnContext.js +14 -19
  162. package/bin/session/runtime/SessionTurnContext.js.map +1 -1
  163. package/bin/session/runtime/SessionTurnFileDiff.d.ts +12 -0
  164. package/bin/session/runtime/SessionTurnFileDiff.d.ts.map +1 -0
  165. package/bin/session/runtime/SessionTurnFileDiff.js +71 -0
  166. package/bin/session/runtime/SessionTurnFileDiff.js.map +1 -0
  167. package/bin/session/storage/LocalSessionDataStore.d.ts +6 -41
  168. package/bin/session/storage/LocalSessionDataStore.d.ts.map +1 -1
  169. package/bin/session/storage/LocalSessionDataStore.js +19 -101
  170. package/bin/session/storage/LocalSessionDataStore.js.map +1 -1
  171. package/bin/session/storage/LocalSessionStore.d.ts +13 -5
  172. package/bin/session/storage/LocalSessionStore.d.ts.map +1 -1
  173. package/bin/session/storage/LocalSessionStore.js +153 -30
  174. package/bin/session/storage/LocalSessionStore.js.map +1 -1
  175. package/bin/session/storage/LocalStorePaths.d.ts +4 -16
  176. package/bin/session/storage/LocalStorePaths.d.ts.map +1 -1
  177. package/bin/session/storage/LocalStorePaths.js +7 -31
  178. package/bin/session/storage/LocalStorePaths.js.map +1 -1
  179. package/bin/session/storage/Metadata.d.ts +4 -4
  180. package/bin/session/storage/Metadata.d.ts.map +1 -1
  181. package/bin/session/storage/Metadata.js +3 -11
  182. package/bin/session/storage/Metadata.js.map +1 -1
  183. package/bin/session/storage/RuntimeSessionPort.d.ts +4 -4
  184. package/bin/session/storage/RuntimeSessionPort.d.ts.map +1 -1
  185. package/bin/session/storage/RuntimeSessionPort.js +3 -3
  186. package/bin/session/storage/RuntimeSessionPort.js.map +1 -1
  187. package/bin/session/storage/SessionStorageCodec.d.ts +59 -0
  188. package/bin/session/storage/SessionStorageCodec.d.ts.map +1 -0
  189. package/bin/session/storage/SessionStorageCodec.js +331 -0
  190. package/bin/session/storage/SessionStorageCodec.js.map +1 -0
  191. package/bin/session/storage/SessionStorageSchema.d.ts +11 -0
  192. package/bin/session/storage/SessionStorageSchema.d.ts.map +1 -0
  193. package/bin/session/storage/SessionStorageSchema.js +131 -0
  194. package/bin/session/storage/SessionStorageSchema.js.map +1 -0
  195. package/bin/session/storage/SqliteSessionStorage.d.ts +120 -0
  196. package/bin/session/storage/SqliteSessionStorage.d.ts.map +1 -0
  197. package/bin/session/storage/SqliteSessionStorage.js +581 -0
  198. package/bin/session/storage/SqliteSessionStorage.js.map +1 -0
  199. package/bin/tools/ask/AskQuestionsTool.d.ts.map +1 -1
  200. package/bin/tools/ask/AskQuestionsTool.js +3 -1
  201. package/bin/tools/ask/AskQuestionsTool.js.map +1 -1
  202. package/bin/types/action/ActionResult.d.ts +9 -9
  203. package/bin/types/action/ActionResult.d.ts.map +1 -1
  204. package/bin/types/action/ActionResult.js +1 -1
  205. package/bin/types/agent/AgentOptions.d.ts +10 -0
  206. package/bin/types/agent/AgentOptions.d.ts.map +1 -1
  207. package/bin/types/agent/AgentStorage.d.ts +3 -1
  208. package/bin/types/agent/AgentStorage.d.ts.map +1 -1
  209. package/bin/types/agent/SessionActor.d.ts +0 -9
  210. package/bin/types/agent/SessionActor.d.ts.map +1 -1
  211. package/bin/types/agent/SessionTypes.d.ts +5 -5
  212. package/bin/types/agent/SessionTypes.d.ts.map +1 -1
  213. package/bin/types/executor/SessionAssistantOutput.d.ts +15 -4
  214. package/bin/types/executor/SessionAssistantOutput.d.ts.map +1 -1
  215. package/bin/types/executor/SessionTurnContext.d.ts +17 -19
  216. package/bin/types/executor/SessionTurnContext.d.ts.map +1 -1
  217. package/bin/types/sdk/AgentSessionAction.d.ts +1 -1
  218. package/bin/types/sdk/AgentSessionAction.js +1 -1
  219. package/bin/types/sdk/AgentSessionPrompt.d.ts +14 -5
  220. package/bin/types/sdk/AgentSessionPrompt.d.ts.map +1 -1
  221. package/bin/types/sdk/AgentSessionPrompt.js +1 -1
  222. package/bin/types/sdk/AgentSessionPrompt.js.map +1 -1
  223. package/bin/types/session/AdaptivePartContextPolicy.d.ts +31 -0
  224. package/bin/types/session/AdaptivePartContextPolicy.d.ts.map +1 -0
  225. package/bin/types/session/AdaptivePartContextPolicy.js +7 -0
  226. package/bin/types/session/AdaptivePartContextPolicy.js.map +1 -0
  227. package/bin/types/session/{SessionAssistantMessageState.d.ts → SessionAgentMessageState.d.ts} +7 -7
  228. package/bin/types/session/SessionAgentMessageState.d.ts.map +1 -0
  229. package/bin/types/session/{SessionAssistantMessageState.js → SessionAgentMessageState.js} +1 -1
  230. package/bin/types/session/SessionAgentMessageState.js.map +1 -0
  231. package/bin/types/session/SessionCommand.d.ts +19 -7
  232. package/bin/types/session/SessionCommand.d.ts.map +1 -1
  233. package/bin/types/session/SessionComposer.d.ts +43 -43
  234. package/bin/types/session/SessionComposer.d.ts.map +1 -1
  235. package/bin/types/session/SessionComposer.js +2 -2
  236. package/bin/types/session/SessionComposition.d.ts +2 -5
  237. package/bin/types/session/SessionComposition.d.ts.map +1 -1
  238. package/bin/types/session/SessionContextPolicy.d.ts +70 -0
  239. package/bin/types/session/SessionContextPolicy.d.ts.map +1 -0
  240. package/bin/types/session/SessionContextPolicy.js +8 -0
  241. package/bin/types/session/SessionContextPolicy.js.map +1 -0
  242. package/bin/types/session/SessionExecution.d.ts +5 -32
  243. package/bin/types/session/SessionExecution.d.ts.map +1 -1
  244. package/bin/types/session/SessionLoop.d.ts +3 -5
  245. package/bin/types/session/SessionLoop.d.ts.map +1 -1
  246. package/bin/types/session/SessionMessages.d.ts +18 -28
  247. package/bin/types/session/SessionMessages.d.ts.map +1 -1
  248. package/bin/types/session/SessionOptions.d.ts +5 -5
  249. package/bin/types/session/SessionOptions.d.ts.map +1 -1
  250. package/bin/types/session/SessionPort.d.ts +8 -9
  251. package/bin/types/session/SessionPort.d.ts.map +1 -1
  252. package/bin/types/session/SessionState.d.ts +2 -5
  253. package/bin/types/session/SessionState.d.ts.map +1 -1
  254. package/bin/types/session/SessionSystem.d.ts +14 -0
  255. package/bin/types/session/SessionSystem.d.ts.map +1 -1
  256. package/bin/types/session/SessionTool.d.ts +2 -2
  257. package/bin/types/session/SessionTool.d.ts.map +1 -1
  258. package/bin/types/session/SessionTurnCompletion.d.ts +23 -0
  259. package/bin/types/session/SessionTurnCompletion.d.ts.map +1 -0
  260. package/bin/types/session/SessionTurnCompletion.js +8 -0
  261. package/bin/types/session/SessionTurnCompletion.js.map +1 -0
  262. package/bin/types/session/SessionTurnFileDiff.d.ts +42 -0
  263. package/bin/types/session/SessionTurnFileDiff.d.ts.map +1 -0
  264. package/bin/types/session/SessionTurnFileDiff.js +8 -0
  265. package/bin/types/session/SessionTurnFileDiff.js.map +1 -0
  266. package/bin/types/store/LocalStore.d.ts +6 -13
  267. package/bin/types/store/LocalStore.d.ts.map +1 -1
  268. package/bin/types/store/LocalStore.js +1 -1
  269. package/bin/types/store/SessionStorage.d.ts +103 -0
  270. package/bin/types/store/SessionStorage.d.ts.map +1 -0
  271. package/bin/types/store/SessionStorage.js +8 -0
  272. package/bin/types/store/SessionStorage.js.map +1 -0
  273. package/bin/types/store/SessionStore.d.ts +5 -3
  274. package/bin/types/store/SessionStore.d.ts.map +1 -1
  275. package/package.json +2 -2
  276. package/scripts/core-engine-context-compaction.test.mjs +96 -219
  277. package/scripts/core-engine-model-stream.test.mjs +2 -4
  278. package/scripts/executor-failure-result.test.mjs +14 -17
  279. package/scripts/session-agent-message-part-order.test.mjs +63 -0
  280. package/scripts/session-initialization.test.mjs +141 -0
  281. package/scripts/session-message-timeline.test.mjs +5 -6
  282. package/scripts/session-messages-error-part.test.mjs +175 -0
  283. package/scripts/session-messages-error.test.mjs +47 -6
  284. package/scripts/session-prompt-idempotency.test.mjs +91 -0
  285. package/scripts/session-queue.test.mjs +13 -12
  286. package/scripts/session-storage-sqlite.test.mjs +209 -0
  287. package/scripts/session-turn-context.test.mjs +21 -33
  288. package/src/agent/Agent.ts +48 -12
  289. package/src/agent/AgentInstructions.ts +2 -2
  290. package/src/agent/AgentMemoryStorage.ts +1 -0
  291. package/src/agent/AgentSessions.ts +31 -12
  292. package/src/executor/Executor.ts +47 -184
  293. package/src/executor/README.md +12 -13
  294. package/src/executor/composer/system/default/SystemPromptAssets.ts +0 -6
  295. package/src/executor/core-engine/CoreEngineContextCompaction.ts +3 -135
  296. package/src/executor/core-engine/CoreEngineRunner.ts +51 -273
  297. package/src/executor/core-engine/CoreEngineSignals.ts +9 -9
  298. package/src/executor/messages/SessionAttachmentMapper.ts +4 -4
  299. package/src/executor/messages/SessionMessageLog.ts +3 -3
  300. package/src/executor/messages/SessionModelMessages.ts +85 -103
  301. package/src/executor/model/ModelStepRunner.ts +9 -41
  302. package/src/executor/services/ExecutorRecoveryPolicy.ts +34 -127
  303. package/src/executor/types/SessionHistoryMeta.ts +1 -3
  304. package/src/index.ts +39 -47
  305. package/src/session/DefaultSessionComposer.ts +33 -30
  306. package/src/session/Session.ts +96 -227
  307. package/src/session/SessionComposition.ts +28 -65
  308. package/src/session/SessionLoop.ts +267 -370
  309. package/src/session/SessionMessages.ts +363 -242
  310. package/src/session/SessionQueue.ts +3 -2
  311. package/src/session/SessionState.ts +55 -101
  312. package/src/session/SessionSystem.ts +51 -1
  313. package/src/session/SessionTitle.ts +4 -4
  314. package/src/session/browse/Browse.ts +182 -468
  315. package/src/session/browse/SessionMessageTimeline.ts +63 -38
  316. package/src/session/composer/policies/AdaptivePartContextPolicy.ts +307 -0
  317. package/src/session/composer/policies/FullHistoryContextPolicy.ts +36 -0
  318. package/src/session/composer/policies/SessionSummaryPrompts.ts +42 -0
  319. package/src/session/execution/SessionAssistantOutputAdapter.ts +25 -11
  320. package/src/session/messages/{SessionActionMessageWriter.ts → SessionAgentActionPartWriter.ts} +8 -8
  321. package/src/session/messages/SessionAgentContent.ts +56 -0
  322. package/src/session/messages/SessionAgentMessageState.ts +378 -0
  323. package/src/session/messages/{SessionAssistantMessageWriter.ts → SessionAgentMessageWriter.ts} +89 -60
  324. package/src/session/messages/SessionForkMessageFiles.ts +25 -1
  325. package/src/session/messages/SessionMessageInteractionWriter.ts +19 -19
  326. package/src/session/messages/SessionMessageText.ts +3 -4
  327. package/src/session/messages/SessionTurnFileDiffData.ts +3 -3
  328. package/src/session/messages/SessionUserMessage.ts +10 -7
  329. package/src/session/preview/SessionMessagePreview.ts +26 -9
  330. package/src/session/runtime/SessionTurnCompletion.ts +159 -0
  331. package/src/session/runtime/SessionTurnContext.ts +20 -21
  332. package/src/session/runtime/SessionTurnFileDiff.ts +105 -0
  333. package/src/session/storage/LocalSessionDataStore.ts +27 -176
  334. package/src/session/storage/LocalSessionStore.ts +170 -38
  335. package/src/session/storage/LocalStorePaths.ts +14 -86
  336. package/src/session/storage/Metadata.ts +4 -13
  337. package/src/session/storage/RuntimeSessionPort.ts +7 -7
  338. package/src/session/storage/SessionStorageCodec.ts +465 -0
  339. package/src/session/storage/SessionStorageSchema.ts +132 -0
  340. package/src/session/storage/SqliteSessionStorage.ts +776 -0
  341. package/src/tools/ask/AskQuestionsTool.ts +3 -1
  342. package/src/types/action/ActionResult.ts +10 -10
  343. package/src/types/agent/AgentOptions.ts +11 -0
  344. package/src/types/agent/AgentStorage.ts +3 -1
  345. package/src/types/agent/SessionActor.ts +0 -10
  346. package/src/types/agent/SessionTypes.ts +5 -5
  347. package/src/types/executor/SessionAssistantOutput.ts +15 -4
  348. package/src/types/executor/SessionTurnContext.ts +25 -22
  349. package/src/types/sdk/AgentSessionAction.ts +1 -1
  350. package/src/types/sdk/AgentSessionPrompt.ts +15 -5
  351. package/src/types/session/AdaptivePartContextPolicy.ts +37 -0
  352. package/src/types/session/{SessionAssistantMessageState.ts → SessionAgentMessageState.ts} +6 -6
  353. package/src/types/session/SessionCommand.ts +21 -7
  354. package/src/types/session/SessionComposer.ts +48 -48
  355. package/src/types/session/SessionComposition.ts +2 -5
  356. package/src/types/session/SessionContextPolicy.ts +77 -0
  357. package/src/types/session/SessionExecution.ts +11 -36
  358. package/src/types/session/SessionLoop.ts +3 -8
  359. package/src/types/session/SessionMessages.ts +19 -29
  360. package/src/types/session/SessionOptions.ts +5 -5
  361. package/src/types/session/SessionPort.ts +10 -10
  362. package/src/types/session/SessionState.ts +2 -5
  363. package/src/types/session/SessionSystem.ts +19 -0
  364. package/src/types/session/SessionTool.ts +2 -2
  365. package/src/types/session/SessionTurnCompletion.ts +24 -0
  366. package/src/types/session/SessionTurnFileDiff.ts +44 -0
  367. package/src/types/store/LocalStore.ts +6 -14
  368. package/src/types/store/SessionStorage.ts +121 -0
  369. package/src/types/store/SessionStore.ts +6 -3
  370. package/tsconfig.tsbuildinfo +1 -1
  371. package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.d.ts +0 -45
  372. package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.d.ts.map +0 -1
  373. package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.js +0 -144
  374. package/bin/executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.js.map +0 -1
  375. package/bin/executor/composer/system/SessionSystemComposer.d.ts +0 -23
  376. package/bin/executor/composer/system/SessionSystemComposer.d.ts.map +0 -1
  377. package/bin/executor/composer/system/SessionSystemComposer.js +0 -9
  378. package/bin/executor/composer/system/SessionSystemComposer.js.map +0 -1
  379. package/bin/executor/composer/system/default/DefaultSessionSystemComposer.d.ts +0 -43
  380. package/bin/executor/composer/system/default/DefaultSessionSystemComposer.d.ts.map +0 -1
  381. package/bin/executor/composer/system/default/DefaultSessionSystemComposer.js +0 -42
  382. package/bin/executor/composer/system/default/DefaultSessionSystemComposer.js.map +0 -1
  383. package/bin/executor/composer/system/default/PromptRenderer.d.ts +0 -35
  384. package/bin/executor/composer/system/default/PromptRenderer.d.ts.map +0 -1
  385. package/bin/executor/composer/system/default/PromptRenderer.js +0 -23
  386. package/bin/executor/composer/system/default/PromptRenderer.js.map +0 -1
  387. package/bin/executor/composer/system/default/SystemDomain.d.ts +0 -151
  388. package/bin/executor/composer/system/default/SystemDomain.d.ts.map +0 -1
  389. package/bin/executor/composer/system/default/SystemDomain.js +0 -162
  390. package/bin/executor/composer/system/default/SystemDomain.js.map +0 -1
  391. package/bin/executor/composer/system/default/assets/task.prompt.d.ts +0 -7
  392. package/bin/executor/composer/system/default/assets/task.prompt.d.ts.map +0 -1
  393. package/bin/executor/composer/system/default/assets/task.prompt.js +0 -8
  394. package/bin/executor/composer/system/default/assets/task.prompt.js.map +0 -1
  395. package/bin/executor/composer/system/default/variables/GeoContext.d.ts +0 -14
  396. package/bin/executor/composer/system/default/variables/GeoContext.d.ts.map +0 -1
  397. package/bin/executor/composer/system/default/variables/GeoContext.js +0 -150
  398. package/bin/executor/composer/system/default/variables/GeoContext.js.map +0 -1
  399. package/bin/executor/composer/system/default/variables/PromptTypes.d.ts +0 -36
  400. package/bin/executor/composer/system/default/variables/PromptTypes.d.ts.map +0 -1
  401. package/bin/executor/composer/system/default/variables/PromptTypes.js +0 -9
  402. package/bin/executor/composer/system/default/variables/PromptTypes.js.map +0 -1
  403. package/bin/executor/composer/system/default/variables/VariableReplacer.d.ts +0 -61
  404. package/bin/executor/composer/system/default/variables/VariableReplacer.d.ts.map +0 -1
  405. package/bin/executor/composer/system/default/variables/VariableReplacer.js +0 -93
  406. package/bin/executor/composer/system/default/variables/VariableReplacer.js.map +0 -1
  407. package/bin/executor/core-engine/CoreEngineMessageState.d.ts +0 -63
  408. package/bin/executor/core-engine/CoreEngineMessageState.d.ts.map +0 -1
  409. package/bin/executor/core-engine/CoreEngineMessageState.js +0 -81
  410. package/bin/executor/core-engine/CoreEngineMessageState.js.map +0 -1
  411. package/bin/session/SessionCommand.d.ts +0 -24
  412. package/bin/session/SessionCommand.d.ts.map +0 -1
  413. package/bin/session/SessionCommand.js +0 -35
  414. package/bin/session/SessionCommand.js.map +0 -1
  415. package/bin/session/messages/SessionActionMessageWriter.d.ts.map +0 -1
  416. package/bin/session/messages/SessionActionMessageWriter.js.map +0 -1
  417. package/bin/session/messages/SessionAssistantMessageState.d.ts +0 -49
  418. package/bin/session/messages/SessionAssistantMessageState.d.ts.map +0 -1
  419. package/bin/session/messages/SessionAssistantMessageState.js +0 -296
  420. package/bin/session/messages/SessionAssistantMessageState.js.map +0 -1
  421. package/bin/session/messages/SessionAssistantMessageWriter.d.ts.map +0 -1
  422. package/bin/session/messages/SessionAssistantMessageWriter.js.map +0 -1
  423. package/bin/session/messages/SessionMessageCompaction.d.ts +0 -29
  424. package/bin/session/messages/SessionMessageCompaction.d.ts.map +0 -1
  425. package/bin/session/messages/SessionMessageCompaction.js +0 -128
  426. package/bin/session/messages/SessionMessageCompaction.js.map +0 -1
  427. package/bin/session/runtime/SessionCompactOperation.d.ts +0 -29
  428. package/bin/session/runtime/SessionCompactOperation.d.ts.map +0 -1
  429. package/bin/session/runtime/SessionCompactOperation.js +0 -66
  430. package/bin/session/runtime/SessionCompactOperation.js.map +0 -1
  431. package/bin/session/runtime/SessionHistoryCompaction.d.ts +0 -22
  432. package/bin/session/runtime/SessionHistoryCompaction.d.ts.map +0 -1
  433. package/bin/session/runtime/SessionHistoryCompaction.js +0 -31
  434. package/bin/session/runtime/SessionHistoryCompaction.js.map +0 -1
  435. package/bin/session/storage/JsonlSessionMessageStore.d.ts +0 -77
  436. package/bin/session/storage/JsonlSessionMessageStore.d.ts.map +0 -1
  437. package/bin/session/storage/JsonlSessionMessageStore.js +0 -419
  438. package/bin/session/storage/JsonlSessionMessageStore.js.map +0 -1
  439. package/bin/session/storage/SessionMessageStoreFactory.d.ts +0 -12
  440. package/bin/session/storage/SessionMessageStoreFactory.d.ts.map +0 -1
  441. package/bin/session/storage/SessionMessageStoreFactory.js +0 -13
  442. package/bin/session/storage/SessionMessageStoreFactory.js.map +0 -1
  443. package/bin/types/common/Template.d.ts +0 -9
  444. package/bin/types/common/Template.d.ts.map +0 -1
  445. package/bin/types/common/Template.js +0 -9
  446. package/bin/types/common/Template.js.map +0 -1
  447. package/bin/types/sdk/AgentSessionCompact.d.ts +0 -30
  448. package/bin/types/sdk/AgentSessionCompact.d.ts.map +0 -1
  449. package/bin/types/sdk/AgentSessionCompact.js +0 -8
  450. package/bin/types/sdk/AgentSessionCompact.js.map +0 -1
  451. package/bin/types/session/SessionAssistantMessageState.d.ts.map +0 -1
  452. package/bin/types/session/SessionAssistantMessageState.js.map +0 -1
  453. package/bin/types/session/SessionSegment.d.ts +0 -57
  454. package/bin/types/session/SessionSegment.d.ts.map +0 -1
  455. package/bin/types/session/SessionSegment.js +0 -8
  456. package/bin/types/session/SessionSegment.js.map +0 -1
  457. package/bin/types/store/SessionDataStore.d.ts +0 -88
  458. package/bin/types/store/SessionDataStore.d.ts.map +0 -1
  459. package/bin/types/store/SessionDataStore.js +0 -9
  460. package/bin/types/store/SessionDataStore.js.map +0 -1
  461. package/bin/utils/Template.d.ts +0 -17
  462. package/bin/utils/Template.d.ts.map +0 -1
  463. package/bin/utils/Template.js +0 -24
  464. package/bin/utils/Template.js.map +0 -1
  465. package/scripts/session-compaction-prompts.test.mjs +0 -49
  466. package/src/executor/composer/compaction/jsonl/JsonlSessionCompactionPrompts.ts +0 -162
  467. package/src/executor/composer/system/SessionSystemComposer.ts +0 -25
  468. package/src/executor/composer/system/default/DefaultSessionSystemComposer.ts +0 -76
  469. package/src/executor/composer/system/default/PromptRenderer.ts +0 -52
  470. package/src/executor/composer/system/default/SystemDomain.ts +0 -301
  471. package/src/executor/composer/system/default/assets/task.prompt.ts +0 -9
  472. package/src/executor/composer/system/default/assets/task.prompt.ts.txt +0 -33
  473. package/src/executor/composer/system/default/variables/GeoContext.ts +0 -180
  474. package/src/executor/composer/system/default/variables/PromptTypes.ts +0 -37
  475. package/src/executor/composer/system/default/variables/VariableReplacer.ts +0 -154
  476. package/src/executor/core-engine/CoreEngineMessageState.ts +0 -117
  477. package/src/session/SessionCommand.ts +0 -42
  478. package/src/session/messages/SessionAssistantMessageState.ts +0 -392
  479. package/src/session/messages/SessionMessageCompaction.ts +0 -160
  480. package/src/session/runtime/SessionCompactOperation.ts +0 -93
  481. package/src/session/runtime/SessionHistoryCompaction.ts +0 -45
  482. package/src/session/storage/JsonlSessionMessageStore.ts +0 -513
  483. package/src/session/storage/SessionMessageStoreFactory.ts +0 -18
  484. package/src/types/common/Template.ts +0 -9
  485. package/src/types/sdk/AgentSessionCompact.ts +0 -32
  486. package/src/types/session/SessionSegment.ts +0 -62
  487. package/src/types/store/SessionDataStore.ts +0 -113
  488. package/src/utils/Template.ts +0 -28
@@ -4,7 +4,7 @@
4
4
  * 关键点(中文)
5
5
  * - 面向 `new Agent(...)` 的本地会话使用场景。
6
6
  * - 对外保留稳定 Session facade,把状态、turn、view 逻辑下沉到独立 service。
7
- * - 内部使用 `SessionMessages` 统一管理 Active、Segment 与流式 Assistant 草稿。
7
+ * - 内部使用 `SessionMessages` 管理 SQLite 中的 canonical Message 聚合。
8
8
  */
9
9
 
10
10
  import { Executor } from "@executor/Executor.js";
@@ -25,7 +25,6 @@ import type {
25
25
  AgentSessionSystemSnapshot,
26
26
  } from "@/types/agent/SessionTypes.js";
27
27
  import type { AgentSession } from "@/types/agent/SessionActor.js";
28
- import { resolve_system_timezone } from "@/session/storage/Metadata.js";
29
28
  import { create_runtime_session_port } from "@/session/storage/RuntimeSessionPort.js";
30
29
  import type { SessionPort } from "@/types/session/SessionPort.js";
31
30
  import type { SessionMutationSubscriber, SessionMutationUnsubscribe } from "@downcity/type";
@@ -38,37 +37,37 @@ import type {
38
37
  import type { ListSessionMessagesInput, SessionMessagePage } from "@downcity/type";
39
38
  import type { AgentSessionPromptInput } from "@/types/sdk/AgentSessionPrompt.js";
40
39
  import type { AgentSessionStopResult } from "@/types/sdk/AgentSessionStop.js";
41
- import type { AgentSessionCompactHandle } from "@/types/sdk/AgentSessionCompact.js";
42
40
  import type { AgentSessionTurnHandle } from "@/types/sdk/AgentSessionTurn.js";
43
41
  import { SessionEventHub } from "@/session/runtime/SessionEventHub.js";
44
- import { create_session_compact_operation } from "@/session/runtime/SessionCompactOperation.js";
45
- import { run_session_history_compaction } from "@/session/runtime/SessionHistoryCompaction.js";
46
- import { SessionState } from "@/session/SessionState.js";
42
+ import {
43
+ create_session_local_state,
44
+ SessionState,
45
+ } from "@/session/SessionState.js";
47
46
  import { SessionLoop } from "@/session/SessionLoop.js";
48
47
  import { SessionQueue } from "@/session/SessionQueue.js";
49
- import { SessionCommand } from "@/session/SessionCommand.js";
50
48
  import type { SessionLocalState } from "@/types/session/SessionLocalState.js";
51
49
  import type { SessionOptions } from "@/types/session/SessionOptions.js";
52
50
  import type { SessionHookRuntime } from "@downcity/type";
53
51
  import { SessionInteractions } from "@/session/control/SessionInteractions.js";
54
52
  import { SessionShellApprovalAdapter } from "@/session/execution/tools/SessionShellApprovalAdapter.js";
55
53
  import { DefaultSessionComposer } from "@/session/DefaultSessionComposer.js";
56
- import type {
57
- SessionComposer,
58
- SessionCompactionPlan,
59
- } from "@/types/session/SessionComposer.js";
60
- import type { SessionCompactHistory } from "@/types/session/SessionExecution.js";
54
+ import type { SessionComposer } from "@/types/session/SessionComposer.js";
61
55
  import { generate_id } from "@/utils/Id.js";
62
56
  import { nanoid } from "nanoid";
63
57
  import { build_session_info } from "@/session/browse/Browse.js";
64
- import { ensure_session_title } from "@/session/SessionTitle.js";
65
- import type { SessionMessage } from "@downcity/type";
66
58
  import type { SessionActionEventInput } from "@downcity/type";
67
- import type { SessionCommandOptions } from "@/types/session/SessionCommand.js";
68
- import type { SessionDataStore } from "@/types/store/SessionDataStore.js";
59
+ import type { SessionStorage } from "@/types/store/SessionStorage.js";
60
+ import type {
61
+ AppendExternalSessionAgentMessageInput,
62
+ AppendExternalSessionUserMessageInput,
63
+ } from "@/types/session/SessionMessages.js";
69
64
  import { SessionComposition } from "@/session/SessionComposition.js";
70
- import { relocate_fork_message_files } from "@/session/messages/SessionForkMessageFiles.js";
65
+ import {
66
+ relocate_fork_message_files,
67
+ resolve_session_fork_messages,
68
+ } from "@/session/messages/SessionForkMessageFiles.js";
71
69
  import { create_session_model_request_warning } from "@/session/runtime/SessionModelRequestWarning.js";
70
+ import type { SessionContextRecoveryReason } from "@/types/session/SessionContextPolicy.js";
72
71
 
73
72
  /**
74
73
  * SDK 本地 Session。
@@ -82,14 +81,15 @@ export class Session implements AgentSession {
82
81
  readonly origin: SessionOptions["origin"];
83
82
 
84
83
  private readonly workspace_path: string;
85
- private readonly store: SessionDataStore;
86
- private readonly get_session_store: SessionOptions["get_session_store"];
84
+ private readonly store: SessionStorage;
85
+ private readonly create_session_store: SessionOptions["create_session_store"];
87
86
  private readonly register_forked_session: SessionOptions["register_forked_session"];
88
87
  private readonly get_tools: SessionOptions["get_tools"];
89
88
  private readonly logger: SessionOptions["logger"];
90
89
  private readonly get_managed_plugin_system_blocks: SessionOptions["get_managed_plugin_system_blocks"];
91
90
  private readonly ensure_configured_hook?: SessionOptions["ensure_configured"];
92
91
  private readonly composer: SessionComposer;
92
+ private readonly create_composer: () => SessionComposer;
93
93
  private readonly session_messages: SessionMessages;
94
94
  private readonly executor: Executor;
95
95
  private readonly events: SessionEventHub;
@@ -118,7 +118,7 @@ export class Session implements AgentSession {
118
118
  this.origin = options.origin;
119
119
  this.workspace_path = String(options.workspace_path || "").trim();
120
120
  this.store = options.store;
121
- this.get_session_store = options.get_session_store;
121
+ this.create_session_store = options.create_session_store;
122
122
  this.register_forked_session = options.register_forked_session;
123
123
  this.get_tools = options.get_tools;
124
124
  this.logger = options.logger;
@@ -128,7 +128,9 @@ export class Session implements AgentSession {
128
128
  this.get_instruction_system_blocks = options.get_instruction_system_blocks;
129
129
  this.get_managed_plugin_system_blocks = options.get_managed_plugin_system_blocks;
130
130
  this.ensure_configured_hook = options.ensure_configured;
131
- this.composer = options.composer || new DefaultSessionComposer();
131
+ this.create_composer = options.create_composer ||
132
+ (() => new DefaultSessionComposer());
133
+ this.composer = this.create_composer();
132
134
  if (!this.id) {
133
135
  throw new Error("Session requires a non-empty session_id");
134
136
  }
@@ -142,7 +144,7 @@ export class Session implements AgentSession {
142
144
  this.events = new SessionEventHub();
143
145
  this.session_messages = new SessionMessages({
144
146
  session_id: this.id,
145
- store: this.store.messages,
147
+ store: this.store,
146
148
  attachment_store: this.store.attachments,
147
149
  publish: (mutation) => {
148
150
  this.events.publish(mutation);
@@ -156,14 +158,13 @@ export class Session implements AgentSession {
156
158
  session_id: this.id,
157
159
  interactions: this.session_interactions,
158
160
  });
159
- this.local_state = this.create_local_state();
161
+ this.local_state = create_session_local_state();
160
162
  this.session_composition = new SessionComposition({
161
163
  agent_id: this.agent_id,
162
164
  session_id: this.id,
163
165
  session_origin: this.origin,
164
166
  workspace_path: this.workspace_path,
165
167
  store: this.store,
166
- messages: this.session_messages,
167
168
  composer: this.composer,
168
169
  get_tools: this.get_tools,
169
170
  instruction_system_blocks: options.instruction_system_blocks,
@@ -183,7 +184,6 @@ export class Session implements AgentSession {
183
184
  session_id: this.id,
184
185
  origin: this.origin,
185
186
  store: this.store,
186
- messages: this.session_messages,
187
187
  state: this.local_state,
188
188
  logger: this.logger,
189
189
  ensure_configured_hook: this.ensure_configured_hook
@@ -201,7 +201,9 @@ export class Session implements AgentSession {
201
201
  session_origin: this.origin,
202
202
  workspace_path: this.workspace_path,
203
203
  executor: this.executor,
204
- compact_history: async (input) => await this.compact_history(input),
204
+ maintain_context: async () => {
205
+ await this.recover_context("usage_pressure");
206
+ },
205
207
  state: this.state,
206
208
  events: this.events,
207
209
  logger: this.logger,
@@ -228,7 +230,15 @@ export class Session implements AgentSession {
228
230
  );
229
231
  })();
230
232
  }
231
- await this.initialize_promise;
233
+ const initialize_promise = this.initialize_promise;
234
+ try {
235
+ await initialize_promise;
236
+ } catch (error) {
237
+ if (this.initialize_promise === initialize_promise) {
238
+ this.initialize_promise = null;
239
+ }
240
+ throw error;
241
+ }
232
242
  return this;
233
243
  }
234
244
 
@@ -330,7 +340,8 @@ export class Session implements AgentSession {
330
340
  publish_mutation,
331
341
  }
332
342
  : undefined;
333
- this.enqueue_command({
343
+ this.session_loop.enqueue_command({
344
+ kind: "maintenance",
334
345
  execute: async () => {
335
346
  if (model_result) this.state.apply_model_config(model_result.config);
336
347
  if (security_changed && next_approval_mode) {
@@ -361,57 +372,6 @@ export class Session implements AgentSession {
361
372
  return await this.session_loop.stop();
362
373
  }
363
374
 
364
- /**
365
- * 把一次显式历史压缩加入当前 Session 的有序输入队列。
366
- */
367
- async compact(): Promise<AgentSessionCompactHandle> {
368
- await this.state.ensure_runnable();
369
- const compact_id = `compact:${this.id}:${generate_id()}`;
370
- const operation = create_session_compact_operation({
371
- compact_id,
372
- run: async () => await this.session_loop.compact_history(compact_id),
373
- log_error: async (error_message) => {
374
- await this.logger.log("warn", "[agent] session compact command failed", {
375
- session_id: this.id,
376
- compact_id,
377
- error: error_message,
378
- });
379
- },
380
- publish_finish: (final_result) => {
381
- this.events.publish({
382
- mutation_id: nanoid(),
383
- variant: "compact",
384
- type: "finish",
385
- session_id: this.id,
386
- compact_id,
387
- status: final_result.success ? "completed" : "failed",
388
- compacted: final_result.compacted,
389
- reason: final_result.reason,
390
- created_at: Date.now(),
391
- ...(final_result.error ? { error: final_result.error } : {}),
392
- });
393
- },
394
- });
395
- this.enqueue_command({
396
- execute: operation.execute,
397
- });
398
- this.events.publish({
399
- mutation_id: nanoid(),
400
- variant: "compact",
401
- type: "start",
402
- session_id: this.id,
403
- compact_id,
404
- status: "queued",
405
- created_at: Date.now(),
406
- });
407
- return operation.handle;
408
- }
409
-
410
- /** 创建一个具体 Session Command 对象并加入当前 FIFO。 */
411
- private enqueue_command(options: SessionCommandOptions): void {
412
- this.session_queue.enqueue_command(new SessionCommand(options));
413
- }
414
-
415
375
  /**
416
376
  * 订阅当前 Session 的未来事件。
417
377
  */
@@ -451,44 +411,29 @@ export class Session implements AgentSession {
451
411
  async append_user_message(input: {
452
412
  text: string;
453
413
  }): Promise<void> {
454
- const appended = await this.session_messages.append_external_user_message({
414
+ await this.append_external_user_message({
455
415
  text: String(input.text || "").trim(),
456
416
  });
457
- if (!appended) return;
458
- this.state.touch_metadata_in_background();
459
- this.state.schedule_title_generation();
460
417
  }
461
418
 
462
419
  /**
463
- * 追加一条 assistant 文本消息。
420
+ * 追加一条 Agent 文本消息。
464
421
  */
465
- async append_assistant_message(input: {
422
+ async append_agent_message(input: {
466
423
  text: string;
467
424
  }): Promise<void> {
468
- const appended = await this.session_messages.append_external_assistant_message({
425
+ await this.append_external_agent_message({
469
426
  text: String(input.text || "").trim(),
470
427
  });
471
- if (appended) await this.state.touch_metadata();
472
428
  }
473
429
 
474
430
  /**
475
431
  * 读取当前 session 详情。
476
432
  */
477
433
  async get_info(): Promise<AgentSessionInfo> {
478
- const [metadata, snapshot] = await Promise.all([
479
- this.store.read_metadata(),
480
- this.session_messages.context_snapshot(),
481
- ]);
482
- const metadata_with_title = metadata.title
483
- ? metadata
484
- : await ensure_session_title({
485
- session_id: this.id,
486
- store: this.store,
487
- messages: snapshot.messages,
488
- logger: this.logger,
489
- });
434
+ const metadata = await this.store.read_metadata();
490
435
  const model_label = String(
491
- metadata_with_title.model_label ||
436
+ metadata.model_label ||
492
437
  read_model_label(this.get_selected_model()) ||
493
438
  "",
494
439
  ).trim();
@@ -497,10 +442,9 @@ export class Session implements AgentSession {
497
442
  agent_id: this.agent_id,
498
443
  session_id: this.id,
499
444
  metadata: {
500
- ...metadata_with_title,
445
+ ...metadata,
501
446
  ...(model_label ? { model_label } : {}),
502
447
  },
503
- messages: snapshot.messages,
504
448
  executing: this.is_executing(),
505
449
  });
506
450
  }
@@ -536,7 +480,12 @@ export class Session implements AgentSession {
536
480
  const include_message = typeof input === "string" || input?.include_message !== false;
537
481
  const messages = await this.session_messages.list_history_messages();
538
482
  const fork_messages = message_id
539
- ? this.resolve_fork_messages(messages, message_id, include_message)
483
+ ? resolve_session_fork_messages({
484
+ session_id: this.id,
485
+ messages,
486
+ message_id,
487
+ include_message,
488
+ })
540
489
  : messages;
541
490
  const action_id = `history-forking:${this.id}:${Date.now()}:${nanoid(8)}`;
542
491
  await this.emit_action_event({
@@ -582,23 +531,6 @@ export class Session implements AgentSession {
582
531
  }
583
532
  }
584
533
 
585
- /** 截取 Fork 目标 Message 之前的历史,并按调用语义决定是否包含锚点。 */
586
- private resolve_fork_messages(
587
- messages: SessionMessage[],
588
- message_id: string,
589
- include_message: boolean,
590
- ): SessionMessage[] {
591
- const target_index = messages.findIndex(
592
- (message) => message.message_id === message_id,
593
- );
594
- if (target_index < 0) {
595
- throw new Error(
596
- `Cannot fork session "${this.id}": message_id "${message_id}" not found.`,
597
- );
598
- }
599
- return messages.slice(0, include_message ? target_index + 1 : target_index);
600
- }
601
-
602
534
  /**
603
535
  * 返回供宿主运行时使用的 Session 端口。
604
536
  */
@@ -608,26 +540,15 @@ export class Session implements AgentSession {
608
540
  session_id: this.id,
609
541
  get_model: () => this.get_model(),
610
542
  get_executor: () => this.executor,
543
+ messages: async () => await this.session_messages.list_history_messages(),
611
544
  prompt: async (input) => await this.prompt(input),
612
545
  stop: async () => await this.stop(),
613
546
  subscribe: (subscriber) => this.subscribe(subscriber),
614
- append_user_message: async (message_params) => {
615
- const appended = await this.session_messages.append_external_user_message(
616
- message_params,
617
- );
618
- if (!appended) return;
619
- this.state.touch_metadata_in_background();
620
- this.state.schedule_title_generation();
621
- },
622
- append_assistant_message: async (message_params) => {
623
- const appended = await this.session_messages.append_external_assistant_message({
624
- parts: message_params.parts,
625
- text: message_params.text,
626
- });
627
- if (appended) await this.state.touch_metadata();
628
- },
547
+ append_user_message: async (message_params) =>
548
+ await this.append_external_user_message(message_params),
549
+ append_agent_message: async (message_params) =>
550
+ await this.append_external_agent_message(message_params),
629
551
  is_executing: () => this.is_executing(),
630
- context: async () => await this.session_messages.context_snapshot(),
631
552
  ensure_ready_for_execution: async () => {
632
553
  await this.ensure_ready_for_execution();
633
554
  },
@@ -654,8 +575,8 @@ export class Session implements AgentSession {
654
575
  workspace_path: this.workspace_path,
655
576
  ...(this.workspace_id ? { workspace_id: this.workspace_id } : {}),
656
577
  origin: this.origin,
657
- store: this.get_session_store(session_id),
658
- get_session_store: this.get_session_store,
578
+ store: this.create_session_store(session_id),
579
+ create_session_store: this.create_session_store,
659
580
  register_forked_session: this.register_forked_session,
660
581
  session_id: session_id,
661
582
  get_tools: this.get_tools,
@@ -667,7 +588,7 @@ export class Session implements AgentSession {
667
588
  get_managed_plugin_system_blocks: this.get_managed_plugin_system_blocks,
668
589
  ensure_configured: this.ensure_configured_hook,
669
590
  get_agent_model: this.get_agent_model,
670
- composer: this.composer,
591
+ create_composer: this.create_composer,
671
592
  });
672
593
  }
673
594
 
@@ -685,18 +606,6 @@ export class Session implements AgentSession {
685
606
  return new session_class(options) as this;
686
607
  }
687
608
 
688
- private create_local_state(): SessionLocalState {
689
- return {
690
- session_config: {},
691
- effective_session_config: {},
692
- configured_approval_mode: "ask",
693
- created_at: Date.now(),
694
- timezone: resolve_system_timezone(),
695
- initialize_promise: null,
696
- ensure_configured_promise: null,
697
- };
698
- }
699
-
700
609
  /** 创建只依赖统一 Composer 的 Turn Executor。 */
701
610
  private create_executor(): Executor {
702
611
  return new Executor({
@@ -707,91 +616,32 @@ export class Session implements AgentSession {
707
616
  turn_context,
708
617
  retry_count,
709
618
  ),
710
- compact_history: async (input) => await this.compact_history(input),
711
- get_model: () => this.get_model(),
619
+ recover_context: async (reason) => await this.recover_context(reason),
712
620
  logger: this.logger,
713
621
  get_hooks: () => this.get_hooks(),
714
622
  apply_system_snapshot: (input) => this.session_composition.apply_snapshot(input),
715
623
  });
716
624
  }
717
625
 
718
- /** 生成并提交 canonical 历史压缩;该职责不进入 Executor。 */
719
- private async compact_history(
720
- input: Parameters<SessionCompactHistory>[0],
721
- ): ReturnType<SessionCompactHistory> {
722
- return await run_session_history_compaction({
723
- turn_id: input.turn_id,
724
- create_plan: async () => await this.composer.compact({
725
- session: this.session_composition.compose_identity(),
726
- model: this.get_model(),
727
- history: await this.session_messages.context_snapshot(),
728
- on_model_request_failure: (notice) => {
729
- this.events.publish(create_session_model_request_warning({
730
- session_id: this.id,
731
- turn_id: input.turn_id,
732
- notice,
733
- }));
734
- },
735
- }),
736
- commit_plan: async (plan) => {
737
- await this.commit_compaction_plan(plan, input.turn_id);
738
- },
739
- log_error: async (error_message) => {
740
- await this.logger.log("warn", "[agent] session history compaction failed", {
626
+ /** Composer Context Policy 尝试推进派生上下文状态。 */
627
+ private async recover_context(
628
+ reason: SessionContextRecoveryReason,
629
+ ): Promise<boolean> {
630
+ return await this.composer.recover_context({
631
+ session: this.session_composition.compose_identity(),
632
+ model: this.get_model(),
633
+ storage: this.store,
634
+ reason,
635
+ on_model_request_failure: (notice) => {
636
+ this.events.publish(create_session_model_request_warning({
741
637
  session_id: this.id,
742
- ...(input.turn_id ? { turn_id: input.turn_id } : {}),
743
- error: error_message,
744
- });
638
+ turn_id: this.session_loop.current_turn_id(),
639
+ notice,
640
+ }));
745
641
  },
746
642
  });
747
643
  }
748
644
 
749
- /** 提交 Composer 生成的 Segment 压缩计划。 */
750
- private async commit_compaction_plan(
751
- plan: SessionCompactionPlan,
752
- turn_id?: string,
753
- ): Promise<void> {
754
- const action_id = `compacting:${this.id}:${generate_id()}`;
755
- await this.emit_action_event({
756
- action_id,
757
- action_type: "history-compaction",
758
- title: "Compacting session messages",
759
- status: "running",
760
- ...(turn_id
761
- ? { turn_id }
762
- : {}),
763
- });
764
- try {
765
- await this.session_messages.compact_active({
766
- through_sequence: plan.through_sequence,
767
- summary: plan.summary,
768
- });
769
- await this.emit_action_event({
770
- action_id,
771
- action_type: "history-compaction",
772
- title: "Session messages compacted",
773
- description: `Closed Active through Message ${plan.boundary_message_id}.`,
774
- status: "completed",
775
- ...(turn_id
776
- ? { turn_id }
777
- : {}),
778
- });
779
- await this.state.touch_metadata();
780
- } catch (error) {
781
- await this.emit_action_event({
782
- action_id,
783
- action_type: "history-compaction",
784
- title: "Session messages compact failed",
785
- description: error instanceof Error ? error.message : String(error),
786
- status: "failed",
787
- ...(turn_id
788
- ? { turn_id }
789
- : {}),
790
- });
791
- throw error;
792
- }
793
- }
794
-
795
645
  /**
796
646
  * 返回当前 Session 实际使用的模型实例。
797
647
  *
@@ -819,7 +669,27 @@ export class Session implements AgentSession {
819
669
  );
820
670
  }
821
671
 
822
- /** 持久化并发布一条 canonical Action Message */
672
+ /** 追加外部 User Message,并统一触发 Metadata 与标题更新。 */
673
+ private async append_external_user_message(
674
+ input: AppendExternalSessionUserMessageInput,
675
+ ): Promise<void> {
676
+ const appended = await this.session_messages.append_external_user_message(
677
+ input,
678
+ );
679
+ if (!appended) return;
680
+ this.state.schedule_title_generation();
681
+ }
682
+
683
+ /** 追加外部 Assistant Message,并统一提交 Metadata。 */
684
+ private async append_external_agent_message(
685
+ input: AppendExternalSessionAgentMessageInput,
686
+ ): Promise<void> {
687
+ await this.session_messages.append_external_agent_message(
688
+ input,
689
+ );
690
+ }
691
+
692
+ /** 持久化并发布一条只包含 Action Part 的 canonical Agent Message。 */
823
693
  private async emit_action_event(input: SessionActionEventInput): Promise<void> {
824
694
  const action_id = String(input.action_id || "").trim() ||
825
695
  `action:${this.id}:${Date.now()}`;
@@ -831,7 +701,6 @@ export class Session implements AgentSession {
831
701
  ...(input.description ? { description: input.description } : {}),
832
702
  status: input.status,
833
703
  });
834
- await this.state.touch_metadata();
835
704
  }
836
705
 
837
706
  }