@waq666/pneuma 0.0.0-stage → 0.7.0

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 (687) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +144 -2
  3. package/bin/dsc.js +91 -0
  4. package/bin/headless.cjs +40 -0
  5. package/lib/adapter/core-runtime.d.ts +11 -0
  6. package/lib/adapter/core-runtime.js +259 -0
  7. package/lib/adapter/host.d.ts +18 -0
  8. package/lib/adapter/host.js +14 -0
  9. package/lib/adapter/runtime.d.ts +15 -0
  10. package/lib/adapter/runtime.js +290 -0
  11. package/lib/adapter/transcript.d.ts +184 -0
  12. package/lib/adapter/transcript.js +605 -0
  13. package/lib/app/AgentsOverlay.d.ts +18 -0
  14. package/lib/app/AgentsOverlay.js +63 -0
  15. package/lib/app/App.d.ts +13 -0
  16. package/lib/app/App.js +1498 -0
  17. package/lib/app/ApprovalCard.d.ts +12 -0
  18. package/lib/app/ApprovalCard.js +76 -0
  19. package/lib/app/AskCard.d.ts +20 -0
  20. package/lib/app/AskCard.js +45 -0
  21. package/lib/app/ChatView.d.ts +32 -0
  22. package/lib/app/ChatView.js +198 -0
  23. package/lib/app/Composer.d.ts +50 -0
  24. package/lib/app/Composer.js +282 -0
  25. package/lib/app/DiffBlock.d.ts +6 -0
  26. package/lib/app/DiffBlock.js +37 -0
  27. package/lib/app/MarkdownView.d.ts +6 -0
  28. package/lib/app/MarkdownView.js +99 -0
  29. package/lib/app/ModelPicker.d.ts +7 -0
  30. package/lib/app/ModelPicker.js +18 -0
  31. package/lib/app/PlanReviewCard.d.ts +13 -0
  32. package/lib/app/PlanReviewCard.js +54 -0
  33. package/lib/app/PresetPicker.d.ts +11 -0
  34. package/lib/app/PresetPicker.js +19 -0
  35. package/lib/app/PreviewOverlay.d.ts +14 -0
  36. package/lib/app/PreviewOverlay.js +22 -0
  37. package/lib/app/SessionPicker.d.ts +43 -0
  38. package/lib/app/SessionPicker.js +56 -0
  39. package/lib/app/SettingsOverlay.d.ts +32 -0
  40. package/lib/app/SettingsOverlay.js +157 -0
  41. package/lib/app/SkillsPicker.d.ts +7 -0
  42. package/lib/app/SkillsPicker.js +19 -0
  43. package/lib/app/StatusBar.d.ts +22 -0
  44. package/lib/app/StatusBar.js +272 -0
  45. package/lib/app/TaskStrips.d.ts +13 -0
  46. package/lib/app/TaskStrips.js +40 -0
  47. package/lib/app/ToolCard.d.ts +5 -0
  48. package/lib/app/ToolCard.js +40 -0
  49. package/lib/app/TranscriptOverlay.d.ts +12 -0
  50. package/lib/app/TranscriptOverlay.js +17 -0
  51. package/lib/app/Welcome.d.ts +6 -0
  52. package/lib/app/Welcome.js +66 -0
  53. package/lib/app/attach.d.ts +16 -0
  54. package/lib/app/attach.js +58 -0
  55. package/lib/app/click.d.ts +27 -0
  56. package/lib/app/click.js +89 -0
  57. package/lib/app/clipboard-image.d.ts +11 -0
  58. package/lib/app/clipboard-image.js +66 -0
  59. package/lib/app/cursor.d.ts +4 -0
  60. package/lib/app/cursor.js +24 -0
  61. package/lib/app/hello.d.ts +11 -0
  62. package/lib/app/hello.js +8 -0
  63. package/lib/app/history-store.d.ts +4 -0
  64. package/lib/app/history-store.js +41 -0
  65. package/lib/app/image-blocks.d.ts +12 -0
  66. package/lib/app/image-blocks.js +125 -0
  67. package/lib/app/markdown.d.ts +54 -0
  68. package/lib/app/markdown.js +189 -0
  69. package/lib/app/settings-model.d.ts +69 -0
  70. package/lib/app/settings-model.js +114 -0
  71. package/lib/app/theme.d.ts +166 -0
  72. package/lib/app/theme.js +141 -0
  73. package/lib/boot.d.ts +1 -0
  74. package/lib/boot.js +34 -0
  75. package/lib/commands.d.ts +49 -0
  76. package/lib/commands.js +115 -0
  77. package/lib/contract.d.ts +1973 -0
  78. package/lib/contract.js +36 -0
  79. package/lib/core/agent-roles.d.ts +52 -0
  80. package/lib/core/agent-roles.js +274 -0
  81. package/lib/core/approval-floor.d.ts +183 -0
  82. package/lib/core/approval-floor.js +679 -0
  83. package/lib/core/approval.d.ts +42 -0
  84. package/lib/core/approval.js +8 -0
  85. package/lib/core/audit.d.ts +61 -0
  86. package/lib/core/audit.js +59 -0
  87. package/lib/core/boot-notices.d.ts +6 -0
  88. package/lib/core/boot-notices.js +61 -0
  89. package/lib/core/cdp/actions.d.ts +182 -0
  90. package/lib/core/cdp/actions.js +649 -0
  91. package/lib/core/cdp/launch.d.ts +192 -0
  92. package/lib/core/cdp/launch.js +491 -0
  93. package/lib/core/cdp/snapshot.d.ts +139 -0
  94. package/lib/core/cdp/snapshot.js +316 -0
  95. package/lib/core/cdp/transport.d.ts +160 -0
  96. package/lib/core/cdp/transport.js +359 -0
  97. package/lib/core/command-policy.d.ts +108 -0
  98. package/lib/core/command-policy.js +580 -0
  99. package/lib/core/commands-completion.d.ts +78 -0
  100. package/lib/core/commands-completion.js +167 -0
  101. package/lib/core/compact-anchors.d.ts +76 -0
  102. package/lib/core/compact-anchors.js +284 -0
  103. package/lib/core/compact.d.ts +110 -0
  104. package/lib/core/compact.js +210 -0
  105. package/lib/core/config-store.d.ts +38 -0
  106. package/lib/core/config-store.js +307 -0
  107. package/lib/core/config.d.ts +36 -0
  108. package/lib/core/config.js +124 -0
  109. package/lib/core/connection-test.d.ts +60 -0
  110. package/lib/core/connection-test.js +84 -0
  111. package/lib/core/desktop-control.d.ts +298 -0
  112. package/lib/core/desktop-control.js +1311 -0
  113. package/lib/core/diff-text.d.ts +74 -0
  114. package/lib/core/diff-text.js +257 -0
  115. package/lib/core/dock-browser.d.ts +137 -0
  116. package/lib/core/dock-browser.js +146 -0
  117. package/lib/core/dock-ops.d.ts +59 -0
  118. package/lib/core/dock-ops.js +104 -0
  119. package/lib/core/dock-session.d.ts +39 -0
  120. package/lib/core/dock-session.js +28 -0
  121. package/lib/core/dsh-compat/hook.d.ts +18 -0
  122. package/lib/core/dsh-compat/hook.js +52 -0
  123. package/lib/core/dsh-compat/resolve-hook.d.ts +5 -0
  124. package/lib/core/dsh-compat/resolve-hook.js +59 -0
  125. package/lib/core/dsh-compat/tools-facade.d.ts +47 -0
  126. package/lib/core/dsh-compat/tools-facade.js +108 -0
  127. package/lib/core/err-text.d.ts +13 -0
  128. package/lib/core/err-text.js +15 -0
  129. package/lib/core/events.d.ts +149 -0
  130. package/lib/core/events.js +1 -0
  131. package/lib/core/git-exec.d.ts +23 -0
  132. package/lib/core/git-exec.js +72 -0
  133. package/lib/core/git-info.d.ts +47 -0
  134. package/lib/core/git-info.js +157 -0
  135. package/lib/core/goal.d.ts +89 -0
  136. package/lib/core/goal.js +189 -0
  137. package/lib/core/hooks.d.ts +272 -0
  138. package/lib/core/hooks.js +818 -0
  139. package/lib/core/host-browser-link.d.ts +59 -0
  140. package/lib/core/host-browser-link.js +9 -0
  141. package/lib/core/host-methods.d.ts +25 -0
  142. package/lib/core/host-methods.js +103 -0
  143. package/lib/core/image-diet.d.ts +77 -0
  144. package/lib/core/image-diet.js +127 -0
  145. package/lib/core/learnings/ledger.d.ts +73 -0
  146. package/lib/core/learnings/ledger.js +273 -0
  147. package/lib/core/learnings/skill-write.d.ts +250 -0
  148. package/lib/core/learnings/skill-write.js +854 -0
  149. package/lib/core/learnings/store.d.ts +102 -0
  150. package/lib/core/learnings/store.js +266 -0
  151. package/lib/core/lifecycle-hooks.d.ts +189 -0
  152. package/lib/core/lifecycle-hooks.js +576 -0
  153. package/lib/core/llm.d.ts +272 -0
  154. package/lib/core/llm.js +548 -0
  155. package/lib/core/lockfile.d.ts +17 -0
  156. package/lib/core/lockfile.js +110 -0
  157. package/lib/core/loop.d.ts +185 -0
  158. package/lib/core/loop.js +587 -0
  159. package/lib/core/lsp/client.d.ts +189 -0
  160. package/lib/core/lsp/client.js +690 -0
  161. package/lib/core/lsp/framing.d.ts +84 -0
  162. package/lib/core/lsp/framing.js +181 -0
  163. package/lib/core/lsp/line-shift.d.ts +28 -0
  164. package/lib/core/lsp/line-shift.js +127 -0
  165. package/lib/core/lsp/manager.d.ts +93 -0
  166. package/lib/core/lsp/manager.js +418 -0
  167. package/lib/core/lsp/normalize.d.ts +32 -0
  168. package/lib/core/lsp/normalize.js +139 -0
  169. package/lib/core/lsp/servers.d.ts +106 -0
  170. package/lib/core/lsp/servers.js +352 -0
  171. package/lib/core/lsp/uri.d.ts +76 -0
  172. package/lib/core/lsp/uri.js +179 -0
  173. package/lib/core/market.d.ts +51 -0
  174. package/lib/core/market.js +358 -0
  175. package/lib/core/mcp.d.ts +201 -0
  176. package/lib/core/mcp.js +836 -0
  177. package/lib/core/memory-eval.d.ts +158 -0
  178. package/lib/core/memory-eval.js +215 -0
  179. package/lib/core/memory.d.ts +266 -0
  180. package/lib/core/memory.js +703 -0
  181. package/lib/core/mention.d.ts +57 -0
  182. package/lib/core/mention.js +129 -0
  183. package/lib/core/microcompact.d.ts +48 -0
  184. package/lib/core/microcompact.js +79 -0
  185. package/lib/core/migrate.d.ts +56 -0
  186. package/lib/core/migrate.js +134 -0
  187. package/lib/core/model-caps.d.ts +83 -0
  188. package/lib/core/model-caps.js +150 -0
  189. package/lib/core/model-discovery.d.ts +19 -0
  190. package/lib/core/model-discovery.js +69 -0
  191. package/lib/core/modes.d.ts +72 -0
  192. package/lib/core/modes.js +152 -0
  193. package/lib/core/native-bridge.d.ts +135 -0
  194. package/lib/core/native-bridge.js +563 -0
  195. package/lib/core/path-policy.d.ts +32 -0
  196. package/lib/core/path-policy.js +248 -0
  197. package/lib/core/permission-presets.d.ts +67 -0
  198. package/lib/core/permission-presets.js +75 -0
  199. package/lib/core/plugin-loader.d.ts +36 -0
  200. package/lib/core/plugin-loader.js +294 -0
  201. package/lib/core/plugin-registry.d.ts +113 -0
  202. package/lib/core/plugin-registry.js +256 -0
  203. package/lib/core/prefs.d.ts +82 -0
  204. package/lib/core/prefs.js +372 -0
  205. package/lib/core/presets.d.ts +80 -0
  206. package/lib/core/presets.js +373 -0
  207. package/lib/core/pricing.d.ts +90 -0
  208. package/lib/core/pricing.js +126 -0
  209. package/lib/core/prompt.d.ts +92 -0
  210. package/lib/core/prompt.js +339 -0
  211. package/lib/core/ptc.d.ts +66 -0
  212. package/lib/core/ptc.js +70 -0
  213. package/lib/core/remote/frames.d.ts +91 -0
  214. package/lib/core/remote/frames.js +174 -0
  215. package/lib/core/remote/notify.d.ts +54 -0
  216. package/lib/core/remote/notify.js +77 -0
  217. package/lib/core/remote/owner.d.ts +32 -0
  218. package/lib/core/remote/owner.js +75 -0
  219. package/lib/core/remote/pairing.d.ts +104 -0
  220. package/lib/core/remote/pairing.js +261 -0
  221. package/lib/core/remote/push.d.ts +80 -0
  222. package/lib/core/remote/push.js +222 -0
  223. package/lib/core/remote/store.d.ts +18 -0
  224. package/lib/core/remote/store.js +59 -0
  225. package/lib/core/remote/tickets.d.ts +20 -0
  226. package/lib/core/remote/tickets.js +49 -0
  227. package/lib/core/remote/uploads.d.ts +63 -0
  228. package/lib/core/remote/uploads.js +161 -0
  229. package/lib/core/request-attribution.d.ts +26 -0
  230. package/lib/core/request-attribution.js +86 -0
  231. package/lib/core/request-invariant.d.ts +61 -0
  232. package/lib/core/request-invariant.js +199 -0
  233. package/lib/core/sandbox/backends.d.ts +64 -0
  234. package/lib/core/sandbox/backends.js +194 -0
  235. package/lib/core/sandbox/execpolicy.d.ts +93 -0
  236. package/lib/core/sandbox/execpolicy.js +452 -0
  237. package/lib/core/sandbox/net-proxy.d.ts +31 -0
  238. package/lib/core/sandbox/net-proxy.js +162 -0
  239. package/lib/core/sandbox/policy.d.ts +211 -0
  240. package/lib/core/sandbox/policy.js +559 -0
  241. package/lib/core/sandbox/win/abi.d.ts +161 -0
  242. package/lib/core/sandbox/win/abi.js +169 -0
  243. package/lib/core/sandbox/win/account.d.ts +71 -0
  244. package/lib/core/sandbox/win/account.js +206 -0
  245. package/lib/core/sandbox/win/acl.d.ts +54 -0
  246. package/lib/core/sandbox/win/acl.js +339 -0
  247. package/lib/core/sandbox/win/backend.d.ts +61 -0
  248. package/lib/core/sandbox/win/backend.js +249 -0
  249. package/lib/core/sandbox/win/doctor.d.ts +24 -0
  250. package/lib/core/sandbox/win/doctor.js +272 -0
  251. package/lib/core/sandbox/win/ffi.d.ts +107 -0
  252. package/lib/core/sandbox/win/ffi.js +166 -0
  253. package/lib/core/sandbox/win/net-abi.d.ts +154 -0
  254. package/lib/core/sandbox/win/net-abi.js +316 -0
  255. package/lib/core/sandbox/win/runner-entry.d.ts +35 -0
  256. package/lib/core/sandbox/win/runner-entry.js +391 -0
  257. package/lib/core/sandbox/win/setup.d.ts +60 -0
  258. package/lib/core/sandbox/win/setup.js +986 -0
  259. package/lib/core/sandbox/win/token.d.ts +35 -0
  260. package/lib/core/sandbox/win/token.js +238 -0
  261. package/lib/core/sandbox/win/wfp.d.ts +41 -0
  262. package/lib/core/sandbox/win/wfp.js +200 -0
  263. package/lib/core/sandbox/win/workspace-sid.d.ts +9 -0
  264. package/lib/core/sandbox/win/workspace-sid.js +45 -0
  265. package/lib/core/schedule/rule.d.ts +184 -0
  266. package/lib/core/schedule/rule.js +745 -0
  267. package/lib/core/schedule/runner.d.ts +185 -0
  268. package/lib/core/schedule/runner.js +678 -0
  269. package/lib/core/schedule/store.d.ts +182 -0
  270. package/lib/core/schedule/store.js +455 -0
  271. package/lib/core/secrets.d.ts +41 -0
  272. package/lib/core/secrets.js +150 -0
  273. package/lib/core/session-export.d.ts +18 -0
  274. package/lib/core/session-export.js +73 -0
  275. package/lib/core/session-index.d.ts +159 -0
  276. package/lib/core/session-index.js +683 -0
  277. package/lib/core/session-meta.d.ts +41 -0
  278. package/lib/core/session-meta.js +97 -0
  279. package/lib/core/session.d.ts +356 -0
  280. package/lib/core/session.js +998 -0
  281. package/lib/core/skills.d.ts +60 -0
  282. package/lib/core/skills.js +171 -0
  283. package/lib/core/spill.d.ts +159 -0
  284. package/lib/core/spill.js +329 -0
  285. package/lib/core/surface-registry.d.ts +45 -0
  286. package/lib/core/surface-registry.js +67 -0
  287. package/lib/core/team-board.d.ts +95 -0
  288. package/lib/core/team-board.js +316 -0
  289. package/lib/core/throughput.d.ts +46 -0
  290. package/lib/core/throughput.js +40 -0
  291. package/lib/core/todo-reminder.d.ts +44 -0
  292. package/lib/core/todo-reminder.js +55 -0
  293. package/lib/core/todo.d.ts +81 -0
  294. package/lib/core/todo.js +222 -0
  295. package/lib/core/token-estimate.d.ts +55 -0
  296. package/lib/core/token-estimate.js +82 -0
  297. package/lib/core/tool-guards.d.ts +103 -0
  298. package/lib/core/tool-guards.js +77 -0
  299. package/lib/core/tool-search.d.ts +152 -0
  300. package/lib/core/tool-search.js +311 -0
  301. package/lib/core/tools/bash.d.ts +281 -0
  302. package/lib/core/tools/bash.js +871 -0
  303. package/lib/core/tools/command-runner.d.ts +84 -0
  304. package/lib/core/tools/command-runner.js +68 -0
  305. package/lib/core/tools/fs-tools.d.ts +23 -0
  306. package/lib/core/tools/fs-tools.js +247 -0
  307. package/lib/core/tools/index.d.ts +8 -0
  308. package/lib/core/tools/index.js +4 -0
  309. package/lib/core/tools/sandbox-args.d.ts +51 -0
  310. package/lib/core/tools/sandbox-args.js +90 -0
  311. package/lib/core/tools/search-tools.d.ts +3 -0
  312. package/lib/core/tools/search-tools.js +153 -0
  313. package/lib/core/tools.d.ts +96 -0
  314. package/lib/core/tools.js +53 -0
  315. package/lib/core/ui-boot-source.d.ts +71 -0
  316. package/lib/core/ui-boot-source.js +121 -0
  317. package/lib/core/ui-bundles.d.ts +249 -0
  318. package/lib/core/ui-bundles.js +830 -0
  319. package/lib/core/ui-combo.d.ts +49 -0
  320. package/lib/core/ui-combo.js +55 -0
  321. package/lib/core/ui-events.d.ts +81 -0
  322. package/lib/core/ui-events.js +364 -0
  323. package/lib/core/ui-hmr.d.ts +36 -0
  324. package/lib/core/ui-hmr.js +128 -0
  325. package/lib/core/ui-methods.d.ts +69 -0
  326. package/lib/core/ui-methods.js +341 -0
  327. package/lib/core/ui-route.d.ts +69 -0
  328. package/lib/core/ui-route.js +122 -0
  329. package/lib/core/ui-server.d.ts +75 -0
  330. package/lib/core/ui-server.js +369 -0
  331. package/lib/core/ui-trust.d.ts +47 -0
  332. package/lib/core/ui-trust.js +164 -0
  333. package/lib/core/untrusted.d.ts +30 -0
  334. package/lib/core/untrusted.js +48 -0
  335. package/lib/core/update-check.d.ts +81 -0
  336. package/lib/core/update-check.js +177 -0
  337. package/lib/core/usage-log.d.ts +82 -0
  338. package/lib/core/usage-log.js +306 -0
  339. package/lib/core/version.d.ts +1 -0
  340. package/lib/core/version.js +37 -0
  341. package/lib/core/web-http.d.ts +57 -0
  342. package/lib/core/web-http.js +137 -0
  343. package/lib/core/web-static.d.ts +31 -0
  344. package/lib/core/web-static.js +75 -0
  345. package/lib/headless.d.ts +1 -0
  346. package/lib/headless.js +31 -0
  347. package/lib/host/kernel.d.ts +33 -0
  348. package/lib/host/kernel.js +451 -0
  349. package/lib/index.d.ts +41 -0
  350. package/lib/index.js +35 -0
  351. package/lib/plugins/agent.d.ts +2 -0
  352. package/lib/plugins/agent.js +274 -0
  353. package/lib/plugins/approval-floor.d.ts +2 -0
  354. package/lib/plugins/approval-floor.js +223 -0
  355. package/lib/plugins/approval.d.ts +16 -0
  356. package/lib/plugins/approval.js +674 -0
  357. package/lib/plugins/ask.d.ts +2 -0
  358. package/lib/plugins/ask.js +245 -0
  359. package/lib/plugins/browser.d.ts +68 -0
  360. package/lib/plugins/browser.js +1341 -0
  361. package/lib/plugins/commands.d.ts +23 -0
  362. package/lib/plugins/commands.js +498 -0
  363. package/lib/plugins/compact.d.ts +21 -0
  364. package/lib/plugins/compact.js +274 -0
  365. package/lib/plugins/computer-use.d.ts +47 -0
  366. package/lib/plugins/computer-use.js +539 -0
  367. package/lib/plugins/desktop-dock.d.ts +2 -0
  368. package/lib/plugins/desktop-dock.js +626 -0
  369. package/lib/plugins/dock-browser.d.ts +34 -0
  370. package/lib/plugins/dock-browser.js +765 -0
  371. package/lib/plugins/dsh-compat.d.ts +25 -0
  372. package/lib/plugins/dsh-compat.js +37 -0
  373. package/lib/plugins/file-review.d.ts +108 -0
  374. package/lib/plugins/file-review.js +393 -0
  375. package/lib/plugins/git.d.ts +26 -0
  376. package/lib/plugins/git.js +132 -0
  377. package/lib/plugins/goal.d.ts +20 -0
  378. package/lib/plugins/goal.js +245 -0
  379. package/lib/plugins/guards.d.ts +14 -0
  380. package/lib/plugins/guards.js +18 -0
  381. package/lib/plugins/hooks.d.ts +19 -0
  382. package/lib/plugins/hooks.js +583 -0
  383. package/lib/plugins/host-stdio.d.ts +88 -0
  384. package/lib/plugins/host-stdio.js +317 -0
  385. package/lib/plugins/image-diet.d.ts +17 -0
  386. package/lib/plugins/image-diet.js +85 -0
  387. package/lib/plugins/lifecycle-hooks.d.ts +26 -0
  388. package/lib/plugins/lifecycle-hooks.js +338 -0
  389. package/lib/plugins/llm.d.ts +13 -0
  390. package/lib/plugins/llm.js +215 -0
  391. package/lib/plugins/lsp.d.ts +54 -0
  392. package/lib/plugins/lsp.js +608 -0
  393. package/lib/plugins/mcp.d.ts +16 -0
  394. package/lib/plugins/mcp.js +673 -0
  395. package/lib/plugins/memory.d.ts +17 -0
  396. package/lib/plugins/memory.js +858 -0
  397. package/lib/plugins/microcompact.d.ts +18 -0
  398. package/lib/plugins/microcompact.js +119 -0
  399. package/lib/plugins/mode.d.ts +18 -0
  400. package/lib/plugins/mode.js +115 -0
  401. package/lib/plugins/permission-presets.d.ts +18 -0
  402. package/lib/plugins/permission-presets.js +135 -0
  403. package/lib/plugins/plan.d.ts +2 -0
  404. package/lib/plugins/plan.js +192 -0
  405. package/lib/plugins/plugin-manager.d.ts +2 -0
  406. package/lib/plugins/plugin-manager.js +165 -0
  407. package/lib/plugins/presets.d.ts +2 -0
  408. package/lib/plugins/presets.js +182 -0
  409. package/lib/plugins/prompt.d.ts +2 -0
  410. package/lib/plugins/prompt.js +264 -0
  411. package/lib/plugins/ptc.d.ts +2 -0
  412. package/lib/plugins/ptc.js +272 -0
  413. package/lib/plugins/remote/http.d.ts +11 -0
  414. package/lib/plugins/remote/http.js +11 -0
  415. package/lib/plugins/remote/routes.d.ts +24 -0
  416. package/lib/plugins/remote/routes.js +384 -0
  417. package/lib/plugins/remote/types.d.ts +135 -0
  418. package/lib/plugins/remote/types.js +87 -0
  419. package/lib/plugins/remote.d.ts +17 -0
  420. package/lib/plugins/remote.js +1028 -0
  421. package/lib/plugins/runtime-api.d.ts +16 -0
  422. package/lib/plugins/runtime-api.js +99 -0
  423. package/lib/plugins/runtime.d.ts +10 -0
  424. package/lib/plugins/runtime.js +451 -0
  425. package/lib/plugins/sandbox.d.ts +2 -0
  426. package/lib/plugins/sandbox.js +761 -0
  427. package/lib/plugins/schedule.d.ts +26 -0
  428. package/lib/plugins/schedule.js +538 -0
  429. package/lib/plugins/self-improve.d.ts +61 -0
  430. package/lib/plugins/self-improve.js +953 -0
  431. package/lib/plugins/session-search.d.ts +16 -0
  432. package/lib/plugins/session-search.js +308 -0
  433. package/lib/plugins/session-title.d.ts +19 -0
  434. package/lib/plugins/session-title.js +109 -0
  435. package/lib/plugins/session.d.ts +7 -0
  436. package/lib/plugins/session.js +296 -0
  437. package/lib/plugins/settings.d.ts +3 -0
  438. package/lib/plugins/settings.js +705 -0
  439. package/lib/plugins/skills.d.ts +2 -0
  440. package/lib/plugins/skills.js +360 -0
  441. package/lib/plugins/spill.d.ts +16 -0
  442. package/lib/plugins/spill.js +200 -0
  443. package/lib/plugins/subagent.d.ts +2 -0
  444. package/lib/plugins/subagent.js +862 -0
  445. package/lib/plugins/surfaces.d.ts +11 -0
  446. package/lib/plugins/surfaces.js +20 -0
  447. package/lib/plugins/team.d.ts +16 -0
  448. package/lib/plugins/team.js +149 -0
  449. package/lib/plugins/todo.d.ts +19 -0
  450. package/lib/plugins/todo.js +188 -0
  451. package/lib/plugins/tool-search.d.ts +2 -0
  452. package/lib/plugins/tool-search.js +425 -0
  453. package/lib/plugins/tools-default.d.ts +12 -0
  454. package/lib/plugins/tools-default.js +275 -0
  455. package/lib/plugins/tools.d.ts +16 -0
  456. package/lib/plugins/tools.js +62 -0
  457. package/lib/plugins/transcript.d.ts +15 -0
  458. package/lib/plugins/transcript.js +311 -0
  459. package/lib/plugins/tui.d.ts +2 -0
  460. package/lib/plugins/tui.js +98 -0
  461. package/lib/plugins/waiting.d.ts +12 -0
  462. package/lib/plugins/waiting.js +34 -0
  463. package/lib/plugins/web-search.d.ts +14 -0
  464. package/lib/plugins/web-search.js +263 -0
  465. package/lib/plugins/webserver.d.ts +48 -0
  466. package/lib/plugins/webserver.js +228 -0
  467. package/lib/remote/assets/app-DAcUCOEn.js +14 -0
  468. package/lib/remote/assets/icon-192.png +0 -0
  469. package/lib/remote/assets/icon-512.png +0 -0
  470. package/lib/remote/assets/index.html +34 -0
  471. package/lib/remote/assets/manifest.json +27 -0
  472. package/lib/remote/assets/style-DIB6729x.css +1 -0
  473. package/lib/remote/assets/sw.js +66 -0
  474. package/lib/services/types.d.ts +1228 -0
  475. package/lib/services/types.js +1 -0
  476. package/lib/tools/config-cli.d.ts +1 -0
  477. package/lib/tools/config-cli.js +89 -0
  478. package/lib/ui/arrival.d.ts +72 -0
  479. package/lib/ui/arrival.js +105 -0
  480. package/lib/ui/assets/app-B70nVIKd.js +69 -0
  481. package/lib/ui/assets/chunk-2UxHyX5q.js +1 -0
  482. package/lib/ui/assets/chunk-B1dDrJ26.js +1 -0
  483. package/lib/ui/assets/chunk-B6aJPvgy.js +1 -0
  484. package/lib/ui/assets/chunk-BEwlwnbL.js +1 -0
  485. package/lib/ui/assets/chunk-BGcgwlCI.js +12 -0
  486. package/lib/ui/assets/chunk-BIGW1oBm.js +1 -0
  487. package/lib/ui/assets/chunk-BPQ3VLAy.js +1 -0
  488. package/lib/ui/assets/chunk-BRH0KZbk.js +141 -0
  489. package/lib/ui/assets/chunk-BX6H_ySx.js +1 -0
  490. package/lib/ui/assets/chunk-B_P3UbPa.js +1 -0
  491. package/lib/ui/assets/chunk-B_TYUkTI.js +1 -0
  492. package/lib/ui/assets/chunk-B_vNuMnf.js +1 -0
  493. package/lib/ui/assets/chunk-BajyYBss.js +10 -0
  494. package/lib/ui/assets/chunk-BdnUsdx6.js +1 -0
  495. package/lib/ui/assets/chunk-Buea-lGh.js +1 -0
  496. package/lib/ui/assets/chunk-C0PvZCCE.js +1 -0
  497. package/lib/ui/assets/chunk-C0m74zRD.js +1 -0
  498. package/lib/ui/assets/chunk-C27-OAKa.js +1 -0
  499. package/lib/ui/assets/chunk-C2oV4EkX.js +1 -0
  500. package/lib/ui/assets/chunk-C7zT0LnQ.js +1 -0
  501. package/lib/ui/assets/chunk-C9GSunVT.js +1 -0
  502. package/lib/ui/assets/chunk-C9tS-k6U.js +1 -0
  503. package/lib/ui/assets/chunk-CHLpvVh8.js +1 -0
  504. package/lib/ui/assets/chunk-CLxacb5B.js +1 -0
  505. package/lib/ui/assets/chunk-COt5Ahok.js +1 -0
  506. package/lib/ui/assets/chunk-CPTIWCL2.js +1 -0
  507. package/lib/ui/assets/chunk-CQLl-L-p.js +1 -0
  508. package/lib/ui/assets/chunk-CUWRgxuq.js +55 -0
  509. package/lib/ui/assets/chunk-CWanVSXC.js +1 -0
  510. package/lib/ui/assets/chunk-Cf5RLm7j.js +1 -0
  511. package/lib/ui/assets/chunk-CickPsom.js +1 -0
  512. package/lib/ui/assets/chunk-Cj5Yp3dK.js +1 -0
  513. package/lib/ui/assets/chunk-Cmh6b_Ma.js +1 -0
  514. package/lib/ui/assets/chunk-Cp-IABpG.js +1 -0
  515. package/lib/ui/assets/chunk-CqE71os6.js +1 -0
  516. package/lib/ui/assets/chunk-CtKP3W-j.js +1 -0
  517. package/lib/ui/assets/chunk-Cvjx9yec.js +1 -0
  518. package/lib/ui/assets/chunk-CxGP3dtY.js +1 -0
  519. package/lib/ui/assets/chunk-CylS5w8V.js +1 -0
  520. package/lib/ui/assets/chunk-D38fbIBN.js +1 -0
  521. package/lib/ui/assets/chunk-D97Zzqfu.js +1 -0
  522. package/lib/ui/assets/chunk-DAi9KRSo.js +1 -0
  523. package/lib/ui/assets/chunk-DHK4yRfg.js +1 -0
  524. package/lib/ui/assets/chunk-DJeabLx-.js +1 -0
  525. package/lib/ui/assets/chunk-DNRxmY5a.js +1 -0
  526. package/lib/ui/assets/chunk-DNSN1z5r.js +1 -0
  527. package/lib/ui/assets/chunk-DNasK_ZG.js +1 -0
  528. package/lib/ui/assets/chunk-DPvrY3IQ.js +1 -0
  529. package/lib/ui/assets/chunk-DXmwc3jG.js +1 -0
  530. package/lib/ui/assets/chunk-Dd0kTxKP.js +1 -0
  531. package/lib/ui/assets/chunk-De0Btta0.js +1 -0
  532. package/lib/ui/assets/chunk-Des-eS-w.js +1 -0
  533. package/lib/ui/assets/chunk-Df6bDoY_.js +1 -0
  534. package/lib/ui/assets/chunk-DijEV5ha.js +1 -0
  535. package/lib/ui/assets/chunk-DkvoVjww.js +1 -0
  536. package/lib/ui/assets/chunk-DlC-OlHO.js +1 -0
  537. package/lib/ui/assets/chunk-DnzrXTVv.js +1 -0
  538. package/lib/ui/assets/chunk-Dualf0xi.js +1 -0
  539. package/lib/ui/assets/chunk-EXT84bkm.js +1 -0
  540. package/lib/ui/assets/chunk-GwR3hkPK.js +1 -0
  541. package/lib/ui/assets/chunk-P80f7IUj.js +1 -0
  542. package/lib/ui/assets/chunk-VOosw3JB.js +1 -0
  543. package/lib/ui/assets/chunk-YQ4lDkt_.js +140 -0
  544. package/lib/ui/assets/chunk-Yzrsuije.js +1 -0
  545. package/lib/ui/assets/chunk-Z72CF80r.js +1 -0
  546. package/lib/ui/assets/chunk-g9-lgVsj.js +1 -0
  547. package/lib/ui/assets/chunk-vGWfd6FD.js +1 -0
  548. package/lib/ui/assets/chunk-wDzz0qaB.js +1 -0
  549. package/lib/ui/assets/chunk-ySF7HWmS.js +1 -0
  550. package/lib/ui/assets/icon.png +0 -0
  551. package/lib/ui/assets/index.html +14 -0
  552. package/lib/ui/assets/pdf.worker.min-CjEcRF4W.mjs +28 -0
  553. package/lib/ui/assets/style-Bq01xH_b.css +1 -0
  554. package/lib/ui/frames.d.ts +67 -0
  555. package/lib/ui/frames.js +146 -0
  556. package/lib/ui/index.d.ts +59 -0
  557. package/lib/ui/index.js +50 -0
  558. package/lib/ui/manifest.d.ts +172 -0
  559. package/lib/ui/manifest.js +158 -0
  560. package/lib/ui/plugin.d.ts +161 -0
  561. package/lib/ui/plugin.js +7 -0
  562. package/lib/ui/plugins/browser/client.js +290 -0
  563. package/lib/ui/plugins/browser/icon.png +0 -0
  564. package/lib/ui/plugins/composer/client.js +1843 -0
  565. package/lib/ui/plugins/composer/icon.png +0 -0
  566. package/lib/ui/plugins/files/client.js +646 -0
  567. package/lib/ui/plugins/files/icon.png +0 -0
  568. package/lib/ui/plugins/manifest.json +107 -0
  569. package/lib/ui/plugins/plugins/client.js +682 -0
  570. package/lib/ui/plugins/plugins/icon.png +0 -0
  571. package/lib/ui/plugins/preview/client.js +197 -0
  572. package/lib/ui/plugins/preview/icon.png +0 -0
  573. package/lib/ui/plugins/prompt/client.js +653 -0
  574. package/lib/ui/plugins/prompt/icon.png +0 -0
  575. package/lib/ui/plugins/sessions/client.js +1577 -0
  576. package/lib/ui/plugins/sessions/icon.png +0 -0
  577. package/lib/ui/plugins/settings/client.js +5470 -0
  578. package/lib/ui/plugins/settings/icon.png +0 -0
  579. package/lib/ui/plugins/skills/client.js +485 -0
  580. package/lib/ui/plugins/skills/icon.png +0 -0
  581. package/lib/ui/plugins/status/client.js +1420 -0
  582. package/lib/ui/plugins/status/icon.png +0 -0
  583. package/lib/ui/plugins/statusbar/client.js +504 -0
  584. package/lib/ui/plugins/statusbar/icon.png +0 -0
  585. package/lib/ui/plugins/tasks/client.js +52 -0
  586. package/lib/ui/plugins/tasks/icon.png +0 -0
  587. package/lib/ui/plugins/team/client.js +527 -0
  588. package/lib/ui/plugins/team/icon.png +0 -0
  589. package/lib/ui/plugins/terminal/client.js +232 -0
  590. package/lib/ui/plugins/terminal/icon.png +0 -0
  591. package/lib/ui/plugins/trace/client.js +1376 -0
  592. package/lib/ui/plugins/trace/icon.png +0 -0
  593. package/lib/ui/plugins/transcript/client.js +2661 -0
  594. package/lib/ui/plugins/transcript/icon.png +0 -0
  595. package/lib/ui/scoped-slots.d.ts +20 -0
  596. package/lib/ui/scoped-slots.js +21 -0
  597. package/lib/ui/sdk/@dsc/ui/index.d.ts +242 -0
  598. package/lib/ui/sdk/@dsc/ui/package.json +16 -0
  599. package/lib/ui/sdk/@dsc/ui-core/contract.d.ts +1973 -0
  600. package/lib/ui/sdk/@dsc/ui-core/core/diff-text.d.ts +74 -0
  601. package/lib/ui/sdk/@dsc/ui-core/core/dock-browser.d.ts +137 -0
  602. package/lib/ui/sdk/@dsc/ui-core/core/dock-ops.d.ts +59 -0
  603. package/lib/ui/sdk/@dsc/ui-core/core/model-caps.d.ts +83 -0
  604. package/lib/ui/sdk/@dsc/ui-core/core/native-bridge.d.ts +135 -0
  605. package/lib/ui/sdk/@dsc/ui-core/core/token-estimate.d.ts +55 -0
  606. package/lib/ui/sdk/@dsc/ui-core/package.json +16 -0
  607. package/lib/ui/sdk/@dsc/ui-core/ui/arrival.d.ts +72 -0
  608. package/lib/ui/sdk/@dsc/ui-core/ui/frames.d.ts +67 -0
  609. package/lib/ui/sdk/@dsc/ui-core/ui/index.d.ts +59 -0
  610. package/lib/ui/sdk/@dsc/ui-core/ui/manifest.d.ts +172 -0
  611. package/lib/ui/sdk/@dsc/ui-core/ui/plugin.d.ts +161 -0
  612. package/lib/ui/sdk/@dsc/ui-core/ui/scoped-slots.d.ts +20 -0
  613. package/lib/ui/sdk/@dsc/ui-core/ui/services-react.d.ts +27 -0
  614. package/lib/ui/sdk/@dsc/ui-core/ui/services.d.ts +63 -0
  615. package/lib/ui/sdk/@dsc/ui-core/ui/slots-react.d.ts +28 -0
  616. package/lib/ui/sdk/@dsc/ui-core/ui/slots.d.ts +481 -0
  617. package/lib/ui/sdk/@dsc/ui-core/ui/store.d.ts +89 -0
  618. package/lib/ui/sdk/@dsc/ui-dockkit/dock-model.d.ts +238 -0
  619. package/lib/ui/sdk/@dsc/ui-dockkit/dock-state.d.ts +163 -0
  620. package/lib/ui/sdk/@dsc/ui-dockkit/dock.d.ts +24 -0
  621. package/lib/ui/sdk/@dsc/ui-dockkit/index.d.ts +15 -0
  622. package/lib/ui/sdk/@dsc/ui-dockkit/package.json +16 -0
  623. package/lib/ui/sdk/@dsc/ui-layout/app.d.ts +74 -0
  624. package/lib/ui/sdk/@dsc/ui-layout/appearance.d.ts +70 -0
  625. package/lib/ui/sdk/@dsc/ui-layout/host-channel.d.ts +112 -0
  626. package/lib/ui/sdk/@dsc/ui-layout/index.d.ts +34 -0
  627. package/lib/ui/sdk/@dsc/ui-layout/package.json +16 -0
  628. package/lib/ui/sdk/@dsc/ui-layout/page-state.d.ts +14 -0
  629. package/lib/ui/sdk/@dsc/ui-layout/review-pane.d.ts +21 -0
  630. package/lib/ui/sdk/@dsc/ui-layout/runtime-context.d.ts +44 -0
  631. package/lib/ui/sdk/@dsc/ui-layout/thread-view.d.ts +28 -0
  632. package/lib/ui/sdk/@dsc/ui-layout/update-button.d.ts +21 -0
  633. package/lib/ui/sdk/@dsc/ui-primitives/code-file-icon-artwork.d.ts +53 -0
  634. package/lib/ui/sdk/@dsc/ui-primitives/code-view.d.ts +15 -0
  635. package/lib/ui/sdk/@dsc/ui-primitives/compat.d.ts +27 -0
  636. package/lib/ui/sdk/@dsc/ui-primitives/completion-sound.d.ts +24 -0
  637. package/lib/ui/sdk/@dsc/ui-primitives/confirm.d.ts +33 -0
  638. package/lib/ui/sdk/@dsc/ui-primitives/css.d.ts +47 -0
  639. package/lib/ui/sdk/@dsc/ui-primitives/diff-rows.d.ts +20 -0
  640. package/lib/ui/sdk/@dsc/ui-primitives/file-icons.d.ts +38 -0
  641. package/lib/ui/sdk/@dsc/ui-primitives/file-type-classify.d.ts +66 -0
  642. package/lib/ui/sdk/@dsc/ui-primitives/file-type-icon.d.ts +57 -0
  643. package/lib/ui/sdk/@dsc/ui-primitives/highlight.d.ts +24 -0
  644. package/lib/ui/sdk/@dsc/ui-primitives/hover-delay.d.ts +17 -0
  645. package/lib/ui/sdk/@dsc/ui-primitives/icons.d.ts +109 -0
  646. package/lib/ui/sdk/@dsc/ui-primitives/index.d.ts +55 -0
  647. package/lib/ui/sdk/@dsc/ui-primitives/lang-table.d.ts +10 -0
  648. package/lib/ui/sdk/@dsc/ui-primitives/markdown.d.ts +20 -0
  649. package/lib/ui/sdk/@dsc/ui-primitives/native.d.ts +41 -0
  650. package/lib/ui/sdk/@dsc/ui-primitives/package.json +16 -0
  651. package/lib/ui/sdk/@dsc/ui-primitives/panels.d.ts +86 -0
  652. package/lib/ui/sdk/@dsc/ui-primitives/paths.d.ts +11 -0
  653. package/lib/ui/sdk/@dsc/ui-primitives/pdf-view.d.ts +13 -0
  654. package/lib/ui/sdk/@dsc/ui-primitives/plugin-text.d.ts +15 -0
  655. package/lib/ui/sdk/@dsc/ui-primitives/select.d.ts +35 -0
  656. package/lib/ui/sdk/@dsc/ui-primitives/table-view.d.ts +23 -0
  657. package/lib/ui/sdk/@dsc/ui-primitives/thread-resizer.d.ts +22 -0
  658. package/lib/ui/sdk/@dsc/ui-primitives/time.d.ts +33 -0
  659. package/lib/ui/sdk/@dsc/ui-primitives/toast.d.ts +16 -0
  660. package/lib/ui/sdk/@dsc/ui-primitives/tokens.d.ts +14 -0
  661. package/lib/ui/sdk/@dsc/ui-primitives/tool-args.d.ts +49 -0
  662. package/lib/ui/sdk/@dsc/ui-primitives/tool-summary.d.ts +64 -0
  663. package/lib/ui/sdk/@dsc/ui-renderer/boot.d.ts +121 -0
  664. package/lib/ui/sdk/@dsc/ui-renderer/hmr.d.ts +43 -0
  665. package/lib/ui/sdk/@dsc/ui-renderer/index.d.ts +20 -0
  666. package/lib/ui/sdk/@dsc/ui-renderer/package.json +16 -0
  667. package/lib/ui/sdk/@dsc/ui-slots/index.d.ts +19 -0
  668. package/lib/ui/sdk/@dsc/ui-slots/package.json +16 -0
  669. package/lib/ui/sdk/@dsc/ui-slots/slots-react.d.ts +48 -0
  670. package/lib/ui/sdk/@dsc/ui-slots/slots.d.ts +102 -0
  671. package/lib/ui/services-react.d.ts +27 -0
  672. package/lib/ui/services-react.js +32 -0
  673. package/lib/ui/services.d.ts +63 -0
  674. package/lib/ui/services.js +121 -0
  675. package/lib/ui/slots-ink.d.ts +26 -0
  676. package/lib/ui/slots-ink.js +78 -0
  677. package/lib/ui/slots-react.d.ts +28 -0
  678. package/lib/ui/slots-react.js +88 -0
  679. package/lib/ui/slots.d.ts +481 -0
  680. package/lib/ui/slots.js +550 -0
  681. package/lib/ui/store.d.ts +89 -0
  682. package/lib/ui/store.js +62 -0
  683. package/lib/ui/tui-plugin-api.d.ts +23 -0
  684. package/lib/ui/tui-plugin-api.js +23 -0
  685. package/lib/ui/tui-plugins.d.ts +47 -0
  686. package/lib/ui/tui-plugins.js +220 -0
  687. package/package.json +58 -6
@@ -0,0 +1,998 @@
1
+ /**
2
+ * 轻量会话存储:内存里就是 OpenAI 协议消息数组(llm 层零转换),磁盘上是
3
+ * append-only JSONL(~/.dsc/sessions/<cwd 压缩>/<id>.jsonl)。恢复 = 逐行
4
+ * 重放。对应 dsh 的 session-persistence-jsonl 的个人版最小实现——没有
5
+ * 修复/投影/ignorable 语义(个人版不需要)。
6
+ *
7
+ * @module dsc/core/session
8
+ */
9
+ import { createWriteStream, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync, promises as fsp } from 'node:fs';
10
+ import { basename, dirname, isAbsolute, join, sep } from 'node:path';
11
+ import { randomUUID } from 'node:crypto';
12
+ import { stripBaseline } from './tools.js';
13
+ import { summarizeChange } from './tools/fs-tools.js';
14
+ import { dropSessionMeta, patchSessionMeta, readSessionMeta } from './session-meta.js';
15
+ import { acquireLock, withExclusiveLock } from './lockfile.js';
16
+ import { dscPath } from './path-policy.js';
17
+ /**
18
+ * 用户拒绝某次工具调用时,写进那条工具结果的固定回执文案(由 loop 的拒绝分支产出)。
19
+ * 2026-02 之前的日志没有单独记 `error` 字段,{@link Session.load} 靠这句话把
20
+ * 「已拒绝」的状态补回来;之后的日志直接读 `error`,这句话只用来兜老数据。
21
+ */
22
+ export const REJECTED_TOOL_TEXT = '用户拒绝了这次工具调用。';
23
+ /** 工具结果的异常标记:`rejected` = 用户拒绝,`tool-error` = 工具执行报错(loop 写入)。 */
24
+ const REJECTED_TOOL_ERROR = 'rejected';
25
+ /**
26
+ * T32:中断回合的合成闭合文案。进程在「assistant 的 tool_calls 已落盘、工具结果
27
+ * 还没写」之间被杀,恢复时给每个孤儿调用补一条这样的结果并落盘(dsh repair.ts 的
28
+ * interruptedTurnClosers 同款语义)——模型必须知道「这个调用已发起、结果未知」,
29
+ * 才能决定是先核查还是重试;只读调用可直接重试,有副作用的必须先核实。
30
+ */
31
+ const INTERRUPTED_TOOL_TEXT = '[回合被打断] 这个工具调用已经发起,但 Pneuma 没来得及记录它的结果——它可能已经产生了' +
32
+ '副作用(写文件、跑命令、发请求)。先核查实际状态(读文件、查进程、看输出),再决定下一步:' +
33
+ '只读调用可以直接重试;有副作用的调用在核实之前不要盲目重跑。';
34
+ /** 压缩 cwd 为目录名:`C:\Users\waq` → `C-Users-waq`。 */
35
+ export function slugCwd(cwd) {
36
+ return cwd.replace(/[\\/:]+/g, '-');
37
+ }
38
+ export function sessionsRoot() {
39
+ return dscPath('sessions');
40
+ }
41
+ /** T30:拿会话写租约;被别的进程占着就抛明确错误(桌面端与 TUI 同开一个工作区的保护)。 */
42
+ function takeWriteLease(file) {
43
+ const lease = acquireLock(file);
44
+ if (lease === null) {
45
+ throw new Error(`这份会话已在另一个 Pneuma 窗口打开:${basename(file)}(同一份会话日志同时只允许一处写入,先关掉那边再试)`);
46
+ }
47
+ return lease;
48
+ }
49
+ /**
50
+ * T31:meta 首行损坏时的抢救。id 用文件名(uuid),createdAt 用文件诞生时间,
51
+ * cwd 从内容里的绝对路径反推:真正的 cwd 一定是这些路径的祖先,且它的 slug 必须
52
+ * 等于日志所在目录名(`sessions/<slugCwd(cwd)>/<uuid>.jsonl` 的布局)——两个条件
53
+ * 夹出来的 cwd 是准的;一条绝对路径都找不到才退到最长公共目录,再不行就抛错
54
+ * (文件保留在原位,交给上层的明确报错)。
55
+ */
56
+ function salvageMeta(file, lines) {
57
+ const paths = new Set();
58
+ for (const line of lines) {
59
+ if (!line.startsWith('{"type":"'))
60
+ continue;
61
+ let record;
62
+ try {
63
+ record = JSON.parse(line);
64
+ }
65
+ catch {
66
+ continue; // 坏行跳过:抢救靠的是幸存的记录
67
+ }
68
+ if (record.type === 'tool' && Array.isArray(record.changes)) {
69
+ for (const change of record.changes) {
70
+ if (change !== null && typeof change === 'object' && typeof change.path === 'string' && change.path !== '') {
71
+ paths.add(change.path);
72
+ }
73
+ }
74
+ }
75
+ if (record.type === 'state' && record.payload !== null && typeof record.payload === 'object') {
76
+ const planFile = record.payload.file;
77
+ if (typeof planFile === 'string' && planFile !== '')
78
+ paths.add(planFile);
79
+ }
80
+ }
81
+ const dirName = basename(dirname(file));
82
+ let cwd = null;
83
+ for (const path of paths) {
84
+ if (!isAbsolute(path))
85
+ continue;
86
+ let dir = dirname(path);
87
+ for (let depth = 0; depth < 12; depth += 1) {
88
+ if (slugCwd(dir) === dirName) {
89
+ cwd = dir;
90
+ break;
91
+ }
92
+ const parent = dirname(dir);
93
+ if (parent === dir)
94
+ break;
95
+ dir = parent;
96
+ }
97
+ if (cwd !== null)
98
+ break;
99
+ }
100
+ if (cwd === null)
101
+ cwd = commonDirectory(paths);
102
+ if (cwd === null) {
103
+ throw new Error(`会话文件头损坏,且无法从内容推断工作目录(文件保留在原位,没有动它):${file}`);
104
+ }
105
+ let createdAt = Date.now();
106
+ try {
107
+ const born = statSync(file).birthtimeMs;
108
+ if (Number.isFinite(born) && born > 0)
109
+ createdAt = Math.floor(born);
110
+ }
111
+ catch {
112
+ // 取不到诞生时间就用现在:只影响列表里的「创建时间」展示
113
+ }
114
+ return { id: basename(file, '.jsonl'), cwd, createdAt };
115
+ }
116
+ /** 一组绝对路径的最长公共目录(大小写不敏感);没有绝对路径返回 null。 */
117
+ function commonDirectory(paths) {
118
+ const dirs = [...paths].filter((p) => isAbsolute(p)).map((p) => dirname(p));
119
+ if (dirs.length === 0)
120
+ return null;
121
+ let candidate = dirs[0];
122
+ const same = (a, b) => a.toLowerCase() === b.toLowerCase();
123
+ for (const dir of dirs) {
124
+ while (!same(dir, candidate) && !dir.toLowerCase().startsWith(candidate.toLowerCase() + sep)) {
125
+ const parent = dirname(candidate);
126
+ if (parent === candidate)
127
+ return null;
128
+ candidate = parent;
129
+ }
130
+ }
131
+ return candidate;
132
+ }
133
+ /** 一个会话:内存消息 + 磁盘日志。 */
134
+ export class Session {
135
+ meta;
136
+ /** 协议消息(不含 system prompt;system 由 loop 在请求组装时加头)。 */
137
+ messages = [];
138
+ file;
139
+ /** 写入流。新会话在第一条日志真正落盘之前是 null,所以没发过消息的会话不留文件。 */
140
+ stream = null;
141
+ /** meta 首行是否已经写进磁盘。 */
142
+ metaWritten;
143
+ /**
144
+ * T30 写租约:一份会话日志同一时刻只允许一个进程写。桌面端与 TUI 从同一份
145
+ * `.last-session` 取默认会话,没有这层时两边会交错 append、重放串线。
146
+ * 只读打开(队友运行记录的 peek、load-bench)可以不租(`load` 的 `lease: false`)。
147
+ */
148
+ lease;
149
+ /**
150
+ * 工具调用的异常标记(callId → `rejected` / `tool-error`)。
151
+ * OpenAI 协议里没有这个概念,所以它不挂在 `messages` 上(否则会被发进请求体),
152
+ * 只在 jsonl 的 tool 记录里存一份,重放历史时单独交给 transcript 决定卡片状态。
153
+ */
154
+ toolErrors = new Map();
155
+ /**
156
+ * 成功的 write / edit 的实际改动(callId → 变更摘要),轮尾「文件已更改」卡的数据源。
157
+ * 与 toolErrors 同一条旁路:不挂 `messages`(协议消息透传给端点),只在 jsonl 的
158
+ * tool 记录里存一份,重放历史时单独交给 transcript 折成 `kind: 'changes'` 条目。
159
+ */
160
+ fileChanges = new Map();
161
+ /**
162
+ * 回合内文件基线(codex `TurnDiffTracker` 的同款思路):本回合第一次动某个文件时
163
+ * 记下「动之前的全文」,回合收尾用它和盘上现值重算一份聚合 diff——同一文件改
164
+ * 多刀时,界面只出一份「回合开始 vs 终态」的准确差异,而不是各刀差异的拼盘。
165
+ * 不落盘:回合结束即清,重启后轮尾卡回退逐刀合并显示。
166
+ */
167
+ turnBaselines = new Map();
168
+ /** 本回合动过的文件(按首次触碰顺序,轮尾聚合 diff 按这个顺序显示)。 */
169
+ turnTouched = [];
170
+ /**
171
+ * 会话状态条目(见 {@link SessionStateMap})。
172
+ * 不发进请求体,恢复历史会话时由 {@link Session.load} 逐行填回来。
173
+ */
174
+ stateById = new Map();
175
+ /** 请求注入备忘(note 记录);恢复会话时从日志重放回来,经 {@link notes} 读出。 */
176
+ noteLog = [];
177
+ constructor(meta, file, initialMessages = [], restored = false, state, initialNotes = [], lease = null) {
178
+ this.meta = meta;
179
+ this.file = file;
180
+ this.messages.push(...initialMessages);
181
+ this.metaWritten = restored;
182
+ this.lease = lease;
183
+ if (state !== undefined)
184
+ for (const [id, payload] of state)
185
+ this.stateById.set(id, payload);
186
+ this.noteLog.push(...initialNotes);
187
+ }
188
+ /**
189
+ * 新建会话:只在内存里,第一条日志写下去时磁盘上才出现这个文件。
190
+ * @param file - 指定落盘路径(队友日志住在 `.teammates` 下,不进会话列表);省略走默认。
191
+ */
192
+ static create(cwd, file) {
193
+ const meta = { id: randomUUID(), cwd, createdAt: Date.now() };
194
+ const target = file ?? join(sessionsRoot(), slugCwd(cwd), `${meta.id}.jsonl`);
195
+ return new Session(meta, target, [], false, undefined, [], takeWriteLease(target));
196
+ }
197
+ /**
198
+ * 从 jsonl 重放恢复(system prompt 照样由 loop 加头)。
199
+ * @param keepPath - true = 后续追加写回读进来的这个路径。
200
+ * 省略时按会话根目录重算路径(归档区里的文件因此会被"搬回"活动区,
201
+ * 这是历史行为,只有读别人家的日志才需要传 true)。
202
+ * @param options.lease - 是否拿写租约(T30)。缺省 true = 拿(恢复要接着写,写的人
203
+ * 还负责把中断回合修复落盘);只读重放(队友记录的 peek)传 false。
204
+ */
205
+ static load(file, keepPath = false, options = {}) {
206
+ const lease = options.lease === false ? null : takeWriteLease(file);
207
+ try {
208
+ return Session.buildFromLog(file, keepPath, lease);
209
+ }
210
+ catch (error) {
211
+ lease?.release();
212
+ throw error;
213
+ }
214
+ }
215
+ /** T30/T31/T32 的装配核心:读日志 → 逐行重放 →(头坏了就抢救)→(握着租约就修复中断回合)。 */
216
+ static buildFromLog(file, keepPath, lease) {
217
+ const lines = readFileSync(file, 'utf8').split(/\r?\n/).filter((line) => line !== '');
218
+ let meta;
219
+ const messages = [];
220
+ const toolErrors = new Map();
221
+ const fileChanges = new Map();
222
+ /** 已应答过的 callId(首条为准)。 */
223
+ const repliedCalls = new Set();
224
+ const state = new Map();
225
+ const notes = [];
226
+ for (const line of lines) {
227
+ let record;
228
+ try {
229
+ record = JSON.parse(line);
230
+ }
231
+ catch {
232
+ continue;
233
+ }
234
+ switch (record.type) {
235
+ case 'meta':
236
+ meta = { id: record.id, cwd: record.cwd, createdAt: record.createdAt };
237
+ break;
238
+ case 'user':
239
+ messages.push({
240
+ role: 'user',
241
+ content: record.images !== undefined && record.images.length > 0
242
+ ? [
243
+ { type: 'text', text: record.text },
244
+ ...record.images.map((url) => ({ type: 'image_url', image_url: { url } })),
245
+ ]
246
+ : record.text,
247
+ // 老日志没有 ts:这里就是 undefined,界面据此降级(不显示时间/用时)
248
+ ...(record.ts === undefined ? {} : { ts: record.ts }),
249
+ });
250
+ break;
251
+ case 'assistant': {
252
+ messages.push({
253
+ role: 'assistant',
254
+ content: record.text,
255
+ ...(record.reasoning !== '' ? { reasoning_content: record.reasoning } : {}),
256
+ ...(record.toolCalls !== undefined && record.toolCalls.length > 0
257
+ ? {
258
+ tool_calls: record.toolCalls.map((call) => ({
259
+ id: call.id,
260
+ type: 'function',
261
+ function: { name: call.name, arguments: call.arguments },
262
+ })),
263
+ }
264
+ : {}),
265
+ ...(record.ts === undefined ? {} : { ts: record.ts }),
266
+ });
267
+ break;
268
+ }
269
+ case 'tool': {
270
+ // 同一个 callId 只应答一次:重复记录(中断修复的合成件 + 被打断回合晚到的
271
+ // 真实结果,见 plugins/session 的同路径短路)从第二条起整个跳过——内存与
272
+ // 之后的每一次请求都不再见到它;日志保持 append-only,不改写历史。
273
+ if (repliedCalls.has(record.callId))
274
+ break;
275
+ repliedCalls.add(record.callId);
276
+ const content = record.images !== undefined && record.images.length > 0
277
+ ? [
278
+ { type: 'text', text: record.text },
279
+ ...record.images.map((url) => ({ type: 'image_url', image_url: { url } })),
280
+ ]
281
+ : record.text;
282
+ messages.push({
283
+ role: 'tool',
284
+ content,
285
+ tool_call_id: record.callId,
286
+ ...(record.ts === undefined ? {} : { ts: record.ts }),
287
+ });
288
+ // 状态取自 error 字段;早于该字段的老日志按固定回执文案把「已拒绝」补回来
289
+ const error = record.error ?? (record.text === REJECTED_TOOL_TEXT ? REJECTED_TOOL_ERROR : undefined);
290
+ if (error !== undefined)
291
+ toolErrors.set(record.callId, error);
292
+ if (record.changes !== undefined)
293
+ fileChanges.set(record.callId, record.changes);
294
+ break;
295
+ }
296
+ case 'summary': {
297
+ // 摘要之前的原始记录还留在磁盘上(append-only),逐条读回来等于这次压缩白做;
298
+ // 所以这里把已经读到的消息换成「摘要 + 末尾 keep 条」。尾部必须在清空之前取。
299
+ // 老日志没有 keep 字段,按 0 走:结果是「摘要 + 摘要之后的记录」。这是刻意的向后兼容,
300
+ // 比把摘要之前的原文整段读回来(压缩等于没压)好得多。
301
+ const keep = record.keep ?? 0;
302
+ const tail = keep > 0 ? messages.slice(-keep) : [];
303
+ messages.length = 0;
304
+ messages.push({
305
+ role: 'user',
306
+ content: record.text,
307
+ ...(record.ts === undefined ? {} : { ts: record.ts }),
308
+ });
309
+ messages.push(...tail);
310
+ break;
311
+ }
312
+ case 'state':
313
+ state.set(record.id, record.payload);
314
+ break;
315
+ case 'note':
316
+ notes.push({ id: record.id, text: record.text, ts: record.ts });
317
+ break;
318
+ // ↓ 老会话日志的兼容分支(这四种记录已不再写入),按现在的条目名归位
319
+ case 'mode':
320
+ state.set('mode', record.mode);
321
+ break;
322
+ case 'todo':
323
+ state.set('todos', record.items);
324
+ break;
325
+ case 'plan':
326
+ state.set('plan', record.plan);
327
+ break;
328
+ case 'goal':
329
+ state.set('goal', record.goal);
330
+ break;
331
+ }
332
+ }
333
+ // T31:meta 首行损坏(半截 JSON)不等于整个会话报废——从内容抢救一份 meta,
334
+ // 抢救不出来才报错(文件原样保留,绝不静默回落「开新会话」让用户无感知丢会话)。
335
+ if (meta === undefined)
336
+ meta = salvageMeta(file, lines);
337
+ const session = new Session(meta, keepPath ? file : join(sessionsRoot(), slugCwd(meta.cwd), `${meta.id}.jsonl`), messages, true, state, notes, lease);
338
+ for (const [callId, error] of toolErrors)
339
+ session.toolErrors.set(callId, error);
340
+ for (const [callId, change] of fileChanges)
341
+ session.fileChanges.set(callId, change);
342
+ // T32:修复只在「我们是写租约持有人」时做——修复要落盘,只读重放(队友记录 peek)不动它。
343
+ if (lease !== null) {
344
+ session.repairInterruptedTurns();
345
+ // 收件箱遗债:入箱后没等到边界就重启/切换的异步输入。修复完历史必然闭合,
346
+ // 直接落库(dsh 的收件箱是持久投影,重启后同样要交货)。
347
+ for (const item of session.takeAsyncInbox())
348
+ session.appendUser(item.text, item.images);
349
+ }
350
+ return session;
351
+ }
352
+ /**
353
+ * 追加用户消息。带图时消息以多模态 content 数组落库(要模型声明了照片输入才发得出去;
354
+ * JSONL 记 text + images 两个字段,跟工具结果带图的写法一致)。
355
+ *
356
+ * `ts` 只写一份给内存与磁盘共用:界面按它显示「这条消息几点发的」,
357
+ * 也是这一轮「用时」的起点(见 contract.ts 的 TranscriptEntry.ts)。
358
+ */
359
+ appendUser(text, images) {
360
+ const withImages = images !== undefined && images.length > 0;
361
+ const ts = Date.now();
362
+ this.messages.push({
363
+ role: 'user',
364
+ content: withImages
365
+ ? [
366
+ { type: 'text', text },
367
+ ...images.map((url) => ({ type: 'image_url', image_url: { url } })),
368
+ ]
369
+ : text,
370
+ ts,
371
+ });
372
+ this.write({ type: 'user', text, ...(withImages ? { images } : {}), ts });
373
+ }
374
+ appendAssistant(text, reasoning, toolCalls) {
375
+ const ts = Date.now();
376
+ this.messages.push({
377
+ role: 'assistant',
378
+ content: text,
379
+ ...(reasoning !== '' ? { reasoning_content: reasoning } : {}),
380
+ ...(toolCalls.length > 0
381
+ ? {
382
+ tool_calls: toolCalls.map((call) => ({
383
+ id: call.id,
384
+ type: 'function',
385
+ function: { name: call.name, arguments: call.arguments },
386
+ })),
387
+ }
388
+ : {}),
389
+ ts,
390
+ });
391
+ this.write({
392
+ type: 'assistant',
393
+ text,
394
+ reasoning,
395
+ ...(toolCalls.length > 0 ? { toolCalls } : {}),
396
+ ts,
397
+ });
398
+ }
399
+ /**
400
+ * 追加工具结果。output 为对象时支持附带图像(data URL),消息以多模态
401
+ * content 数组落库(需端点支持视觉;JSONL 记录 text + images 两字段)。
402
+ * error 是展示状态标记(`rejected` / `tool-error`),随 jsonl 一起落盘,
403
+ * 但不进协议消息——重放历史时由 {@link Session.toolErrors} 提供给界面。
404
+ */
405
+ appendTool(callId, name, output, error) {
406
+ // 同一个 callId 已经有结果就不落第二条:中断修复补过「结果未知」合成件之后,
407
+ // 被打断那一轮晚到的真实结果会走到这里——写下去就是协议上的重复应答(下一次
408
+ // 请求被网关 400)。界面上的实时结果照常由调用方的 tool/result 事件展示。
409
+ if (this.messages.some((message) => message.role === 'tool' && message.tool_call_id === callId))
410
+ return;
411
+ const ts = Date.now();
412
+ const text = typeof output === 'string' ? output : output.text;
413
+ const images = typeof output === 'string' ? undefined : output.images;
414
+ const changes = typeof output === 'string' ? undefined : output.changes;
415
+ const content = images !== undefined && images.length > 0
416
+ ? [
417
+ { type: 'text', text },
418
+ ...images.map((url) => ({ type: 'image_url', image_url: { url } })),
419
+ ]
420
+ : text;
421
+ this.messages.push({ role: 'tool', content, tool_call_id: callId, ts });
422
+ if (error !== undefined)
423
+ this.toolErrors.set(callId, error);
424
+ // baseline(改前全文)是循环回合基线记账的内存字段,进不了日志与旁路:
425
+ // 两处都在入口剥掉,上游忘了剥也漏不出去。
426
+ const clean = changes !== undefined ? stripBaseline(changes) : undefined;
427
+ if (clean !== undefined)
428
+ this.fileChanges.set(callId, clean);
429
+ this.write({
430
+ type: 'tool',
431
+ callId,
432
+ name,
433
+ text,
434
+ ...(images !== undefined && images.length > 0 ? { images } : {}),
435
+ // 内存消息不带 changes(协议消息透传给端点,多字段可能被挑剔的网关判 400);
436
+ // 它只活在日志里,恢复会话时经 fileChanges 旁路还原成轮尾卡。
437
+ ...(clean !== undefined ? { changes: clean } : {}),
438
+ ...(error !== undefined ? { error } : {}),
439
+ ts,
440
+ });
441
+ }
442
+ /**
443
+ * 循环在每次成功的 write / edit 落库时调:本回合首次触碰这个文件就记下「动之前的全文」
444
+ * 当基线,之后同文件的再改不动它(要的正是回合起点,不是各刀起点)。
445
+ */
446
+ recordTurnChange(path, baseline) {
447
+ if (this.turnBaselines.has(path))
448
+ return;
449
+ this.turnBaselines.set(path, baseline);
450
+ this.turnTouched.push(path);
451
+ }
452
+ /**
453
+ * 回合收尾的聚合改动:逐个「本回合动过的文件」读盘上现值,与基线重算一份 diff。
454
+ * 读不到(被删/被挪)或算不出差异(改了又改回去)的文件跳过;取完基线即清空。
455
+ * 产物不带 baseline(剥干净才进事件),落盘也由调用方自理——聚合 diff 不落 jsonl。
456
+ */
457
+ async takeTurnChanges() {
458
+ const touched = this.turnTouched;
459
+ this.turnTouched = [];
460
+ const baselines = this.turnBaselines;
461
+ this.turnBaselines = new Map();
462
+ const files = [];
463
+ for (const path of touched) {
464
+ const baseline = baselines.get(path) ?? '';
465
+ const current = await fsp.readFile(path, 'utf8').catch(() => null);
466
+ if (current === null)
467
+ continue;
468
+ const change = summarizeChange(path, baseline, current);
469
+ if (change !== undefined)
470
+ files.push(stripBaseline(change));
471
+ }
472
+ return files;
473
+ }
474
+ /**
475
+ * T32:中断回合的修复。恢复会话时扫一遍消息,最后一个带 tool_calls 的 assistant
476
+ * 若有配不上结果的调用,就各补一条「已发起、结果未知」的合成结果并落盘——模型据此
477
+ * 知道这个调用可能已有副作用,先核查再决定重试。落盘之后孤儿不再存在,重复恢复幂等。
478
+ * 只在握着写租约时调用(修复要写盘);尾巴之前的孤儿(不该出现)仍由请求侧的
479
+ * sanitizeToolOrphans 兜底。
480
+ */
481
+ repairInterruptedTurns() {
482
+ const answered = new Set();
483
+ for (const message of this.messages) {
484
+ if (message.role === 'tool' && message.tool_call_id !== undefined)
485
+ answered.add(message.tool_call_id);
486
+ }
487
+ let last = -1;
488
+ for (let index = this.messages.length - 1; index >= 0; index -= 1) {
489
+ const message = this.messages[index];
490
+ if (message.role === 'assistant' && message.tool_calls !== undefined && message.tool_calls.length > 0) {
491
+ last = index;
492
+ break;
493
+ }
494
+ if (message.role === 'user')
495
+ break; // 回到用户消息还没见着带调用的 assistant:没有待修复的尾巴
496
+ }
497
+ if (last < 0)
498
+ return;
499
+ for (const call of this.messages[last].tool_calls) {
500
+ if (answered.has(call.id))
501
+ continue;
502
+ this.appendTool(call.id, call.function.name, INTERRUPTED_TOOL_TEXT);
503
+ }
504
+ }
505
+ /**
506
+ * 压缩落库:内存里换成「摘要 + 保留的尾部」,磁盘上追加一条 summary 记录。
507
+ *
508
+ * `keep` 记下保留了多少条尾部消息:日志是 append-only,摘要之前的原始记录一条都不会删,
509
+ * 重放时只能靠这个数字知道该从磁盘记录构建出的消息里留下末尾几条
510
+ * (见 {@link Session.load} 的 `case 'summary'`)。
511
+ * @param summaryText - 摘要正文(第一条保留消息的 content)。
512
+ * @param kept - 压缩后内存里要留的消息,第一条就是摘要本身。
513
+ */
514
+ replaceWithSummary(summaryText, kept) {
515
+ // 摘要那条是压缩现场新造的,补一个落盘时刻;尾部那些本来就在内存里,保持原时间不变
516
+ // (不然压缩会把历史消息的时间全刷成「现在」)。
517
+ const ts = Date.now();
518
+ for (const message of kept)
519
+ if (message.ts === undefined)
520
+ message.ts = ts;
521
+ this.messages.length = 0;
522
+ this.messages.push(...kept);
523
+ // 尾部不重新写盘:那得把 tool 记录的 name 字段反推回来,得不偿失。
524
+ // kept[0] 是摘要本身,所以减一才是尾部条数。
525
+ this.write({ type: 'summary', text: summaryText, keep: Math.max(kept.length - 1, 0), ts });
526
+ }
527
+ /**
528
+ * 读一个会话状态条目(恢复历史会话时由日志重放填回)。
529
+ * @param id - 条目名,见 {@link SessionStateMap}。
530
+ * @returns 这个功能点最后写进去的负载;没写过就是 undefined。
531
+ */
532
+ state(id) {
533
+ // 负载从磁盘 JSON 里来,读的时候不重新校验(各功能点自己负责读懂自己存的东西,
534
+ // 例如 GoalStore.restore 会挑坏值),这里只做条目名的查表。
535
+ return this.stateById.get(id);
536
+ }
537
+ /**
538
+ * 写一个会话状态条目:内存里换掉,磁盘上追加一条(日志是 append-only,最后一条生效)。
539
+ * @param id - 条目名,见 {@link SessionStateMap}。
540
+ * @param payload - 这个功能点自己的负载。
541
+ */
542
+ appendState(id, payload) {
543
+ this.stateById.set(id, payload);
544
+ this.write({ type: 'state', id: String(id), payload });
545
+ }
546
+ /**
547
+ * 异步输入入箱:回合跑动中到达的输入先在这里排队(state 条目持久化,重启不丢),
548
+ * 等 loop 在步骤边界/回合收尾时出账落库。见 {@link SessionStateMap} 的
549
+ * `async-inbox` 注释。
550
+ * @param origin - 这条是用户来源还是机器来源(缺省 `'user'`,老调用点行为不变)。
551
+ * 只有 `'notice'` 会被记进条目(`'user'` 保持条目形状与老状态一致,读时兜底)。
552
+ */
553
+ enqueueAsync(text, images, origin = 'user') {
554
+ const current = this.state('async-inbox')?.items ?? [];
555
+ this.appendState('async-inbox', {
556
+ items: [
557
+ ...current,
558
+ {
559
+ text,
560
+ ...(images !== undefined && images.length > 0 ? { images } : {}),
561
+ ...(origin === 'notice' ? { origin } : {}),
562
+ },
563
+ ],
564
+ });
565
+ }
566
+ /**
567
+ * 收件箱只读快照:界面照它画输入框下方的「排队消息」条(用户来源)与输入框上方的
568
+ * 「待投递通知」条(机器来源)。只偷看,不取货也不动日志——取货是 {@link takeAsyncInbox}。
569
+ */
570
+ asyncInbox() {
571
+ return this.state('async-inbox')?.items ?? [];
572
+ }
573
+ /**
574
+ * 改一条排队中的异步输入(正文换掉,图片与来源不动)。越界或改成空白当空操作。
575
+ * @returns 是否真的改了(调用方据此决定要不要让界面刷新)。
576
+ */
577
+ editAsync(index, text) {
578
+ const items = [...this.asyncInbox()];
579
+ const current = items[index];
580
+ if (current === undefined || text.trim() === '')
581
+ return false;
582
+ const keptImages = current.images !== undefined && current.images.length > 0 ? current.images : undefined;
583
+ items[index] = {
584
+ text,
585
+ ...(keptImages === undefined ? {} : { images: keptImages }),
586
+ ...(current.origin === undefined ? {} : { origin: current.origin }),
587
+ };
588
+ this.appendState('async-inbox', { items });
589
+ return true;
590
+ }
591
+ /** 撤掉一条排队中的异步输入(还没出账,撤了就等于没发过)。 */
592
+ removeAsync(index) {
593
+ const items = [...this.asyncInbox()];
594
+ if (items[index] === undefined)
595
+ return false;
596
+ items.splice(index, 1);
597
+ this.appendState('async-inbox', { items });
598
+ return true;
599
+ }
600
+ /**
601
+ * 把一条排队输入提到队首(插话用,0.6.67):回合被 {@link MiniAgent.cancel} 打断后
602
+ * 收尾出账,队首那条先落库,模型下一句话就先看到它。
603
+ */
604
+ promoteAsync(index) {
605
+ const items = [...this.asyncInbox()];
606
+ const current = items[index];
607
+ if (current === undefined)
608
+ return false;
609
+ // 已经在队首:没有可提前的,算「已经就位」(不写日志)
610
+ if (index === 0)
611
+ return true;
612
+ items.splice(index, 1);
613
+ this.appendState('async-inbox', { items: [current, ...items] });
614
+ return true;
615
+ }
616
+ /**
617
+ * 收件箱出账:取走全部排队中的异步输入并清空条目。空箱是常见路径,不动日志。
618
+ * 取走后由调用方负责 appendUser 落库(顺序上保证 tool 结果已闭合)。
619
+ */
620
+ takeAsyncInbox() {
621
+ const items = this.state('async-inbox')?.items ?? [];
622
+ if (items.length > 0)
623
+ this.appendState('async-inbox', { items: [] });
624
+ return items;
625
+ }
626
+ /** 已记录的请求注入备忘(note 记录),按写入顺序;恢复会话时从日志重放回来。 */
627
+ notes() {
628
+ return this.noteLog;
629
+ }
630
+ /**
631
+ * 追加一条请求注入备忘(note 记录):投影往请求体里塞的、日志上本来没有的内容
632
+ * 必须在此留底,模型看到的东西才能从日志完整重建(dsh 的 Model-visible ⟺ logged)。
633
+ * 备忘不参与重放折叠,界面暂不显示;多次调用按顺序各留一条。
634
+ * @param id - 注入来源的投影名(例如 `lsp-write-diagnostics`)。
635
+ * @param text - 注入的原文。
636
+ */
637
+ appendNote(id, text) {
638
+ const ts = Date.now();
639
+ this.noteLog.push({ id, text, ts });
640
+ this.write({ type: 'note', id, text, ts });
641
+ }
642
+ close() {
643
+ this.stream?.end();
644
+ this.stream = null;
645
+ // T30:写租约随会话关闭一起还(切会话、退出都会走到这)
646
+ this.lease?.release();
647
+ this.lease = null;
648
+ // 回合基线是纯内存的回合内状态:会话关掉(/new、/resume 切走)就没有意义了
649
+ this.turnBaselines = new Map();
650
+ this.turnTouched = [];
651
+ }
652
+ /** 日志有没有落盘:新建但还没发过消息的会话为 false(磁盘上没有它的文件)。 */
653
+ get persisted() {
654
+ return this.metaWritten;
655
+ }
656
+ get filePath() {
657
+ return this.file;
658
+ }
659
+ write(record) {
660
+ // 第一条日志顺带补上 meta 首行:jsonl 的 append-only 顺序不能变
661
+ if (this.metaWritten !== true) {
662
+ this.metaWritten = true;
663
+ this.streamFor().write(`${JSON.stringify({ type: 'meta', ...this.meta })}\n`);
664
+ }
665
+ this.streamFor().write(`${JSON.stringify(record)}\n`);
666
+ }
667
+ /** 第一条日志要落盘时才建工作区目录、开写入流。 */
668
+ streamFor() {
669
+ if (this.stream === null) {
670
+ mkdirSync(dirname(this.file), { recursive: true });
671
+ const stream = createWriteStream(this.file, { flags: 'a', encoding: 'utf8' });
672
+ // 磁盘满/权限错的 error 事件没人接会让进程以未捕获异常崩掉(2026-09-29):
673
+ // 丢掉这条流,下一条日志重新开——丢一条记录好过崩掉整个宿主。
674
+ stream.on('error', () => {
675
+ if (this.stream === stream)
676
+ this.stream = null;
677
+ });
678
+ this.stream = stream;
679
+ }
680
+ return this.stream;
681
+ }
682
+ }
683
+ /** 归档区与回收站(都在 ~/.dsc 下,点开头的名字让正常列表自然跳过)。 */
684
+ export function archivedRoot() {
685
+ return join(sessionsRoot(), '.archived');
686
+ }
687
+ export function trashRoot() {
688
+ return dscPath('.trash');
689
+ }
690
+ /** 回收站保留天数:超期的文件在下次访问归档区时清掉。 */
691
+ const TRASH_RETENTION_DAYS = 30;
692
+ /** 列出全部会话(正常区;归档区与回收站不在内)。 */
693
+ export function listSessions() {
694
+ return scanSessions(sessionsRoot(), readSessionMeta());
695
+ }
696
+ /** 列出归档会话,按归档时间倒序;顺手清一次过期的回收站文件。 */
697
+ export function listArchivedSessions() {
698
+ sweepTrash();
699
+ const metas = readSessionMeta();
700
+ return scanSessions(archivedRoot(), metas)
701
+ .map((item) => ({ ...item, archivedAt: metas[item.id]?.archivedAt ?? item.updatedAt }))
702
+ .sort((a, b) => (b.archivedAt ?? 0) - (a.archivedAt ?? 0));
703
+ }
704
+ /**
705
+ * 扫一个根目录下的 `<工作目录名>/<uuid>.jsonl`。
706
+ * 跳过点开头的条目(`.archived` 归档区)和非目录(`meta.json` sidecar),
707
+ * 单个文件读失败只丢这一条,不影响整个列表。
708
+ */
709
+ function scanSessions(root, metas) {
710
+ if (!existsSync(root))
711
+ return [];
712
+ const out = [];
713
+ for (const dir of readdirSync(root)) {
714
+ if (dir.startsWith('.'))
715
+ continue;
716
+ const dirPath = join(root, dir);
717
+ if (!statSync(dirPath).isDirectory())
718
+ continue;
719
+ for (const file of readdirSync(dirPath)) {
720
+ if (!file.endsWith('.jsonl'))
721
+ continue;
722
+ // 文件名就是 `<uuid>.jsonl`,sidecar 按 uuid 存,所以按文件名取属性
723
+ const item = readSessionFile(join(dirPath, file), metas[basename(file, '.jsonl')]);
724
+ if (item !== null)
725
+ out.push(item);
726
+ }
727
+ }
728
+ return out.sort((a, b) => b.createdAt - a.createdAt);
729
+ }
730
+ /** 读一个会话文件的前几行 + mtime;首行不是 meta(损坏/空文件)时尽量抢救(T31)。 */
731
+ function readSessionFile(filePath, sidecar) {
732
+ try {
733
+ const head = readFileSync(filePath, 'utf8').split(/\r?\n/, 200);
734
+ let record;
735
+ try {
736
+ record = JSON.parse(head[0] ?? '');
737
+ }
738
+ catch {
739
+ record = undefined;
740
+ }
741
+ if (record?.type === 'meta') {
742
+ const derived = head.find((line) => line.startsWith('{"type":"user"'));
743
+ // T16:展示链 用户改名 → 自动生成 → 首条用户消息截断
744
+ const title = sidecar?.title
745
+ ?? sidecar?.autoTitle
746
+ ?? (derived !== undefined
747
+ ? JSON.parse(derived).text.replace(/\s+/g, ' ').trim().slice(0, 60)
748
+ : undefined);
749
+ return {
750
+ id: record.id,
751
+ cwd: record.cwd,
752
+ createdAt: record.createdAt,
753
+ updatedAt: statSync(filePath).mtimeMs,
754
+ ...(title !== undefined ? { title } : {}),
755
+ ...(sidecar?.pinnedAt !== undefined ? { pinnedAt: sidecar.pinnedAt } : {}),
756
+ path: filePath,
757
+ };
758
+ }
759
+ // T31:头行坏了的会话也要在列表里露脸(不然用户连「打开它」的入口都没有)。
760
+ // id 取文件名、标题取幸存的首条用户消息、cwd 从幸存的改动路径反推;推不出 cwd
761
+ // 就给空串(列表照常显示,真去打开时会拿到「头损坏」的明确报错)。
762
+ const paths = new Set();
763
+ // T16:展示链 用户改名 → 自动生成 → 首条用户消息截断
764
+ let title = sidecar?.title ?? sidecar?.autoTitle;
765
+ for (const line of head) {
766
+ if (!line.startsWith('{"type":"'))
767
+ continue;
768
+ try {
769
+ const parsed = JSON.parse(line);
770
+ if (title === undefined && parsed.type === 'user' && typeof parsed.text === 'string' && parsed.text !== '') {
771
+ title = parsed.text.replace(/\s+/g, ' ').trim().slice(0, 60);
772
+ }
773
+ if (Array.isArray(parsed.changes)) {
774
+ for (const change of parsed.changes) {
775
+ if (change !== null && typeof change === 'object' && typeof change.path === 'string' && change.path !== '') {
776
+ paths.add(change.path);
777
+ }
778
+ }
779
+ }
780
+ }
781
+ catch {
782
+ // 坏行跳过
783
+ }
784
+ }
785
+ let createdAt = statSync(filePath).birthtimeMs;
786
+ if (!Number.isFinite(createdAt) || createdAt <= 0)
787
+ createdAt = statSync(filePath).mtimeMs;
788
+ return {
789
+ id: basename(filePath, '.jsonl'),
790
+ cwd: commonDirectory(paths) ?? '',
791
+ createdAt,
792
+ updatedAt: statSync(filePath).mtimeMs,
793
+ ...(title !== undefined ? { title } : {}),
794
+ ...(sidecar?.pinnedAt !== undefined ? { pinnedAt: sidecar.pinnedAt } : {}),
795
+ path: filePath,
796
+ };
797
+ }
798
+ catch {
799
+ return null;
800
+ }
801
+ }
802
+ /**
803
+ * 队友(子智能体)运行记录的根目录。它放在会话根目录下的 `.teammates`,
804
+ * 于是会话列表扫不到它(scanSessions 跳过点开头的条目),
805
+ * 但路径校验仍认它是"自己家的日志",桌面端可以只读打开它。
806
+ */
807
+ export function teammateRoot() {
808
+ return join(sessionsRoot(), '.teammates');
809
+ }
810
+ /** 会话文件必须落在 sessions 根下(归档区也算),才允许被这些操作碰到。 */
811
+ function assertSessionFile(filePath) {
812
+ const root = sessionsRoot();
813
+ const resolved = join(filePath);
814
+ if (!resolved.startsWith(root + '\\') && !resolved.startsWith(root + '/')) {
815
+ throw new Error(`会话路径不在 ${root} 下:${filePath}`);
816
+ }
817
+ if (!resolved.endsWith('.jsonl') || !existsSync(resolved)) {
818
+ throw new Error(`会话文件不存在:${filePath}`);
819
+ }
820
+ // 队友的运行记录是只读史料:归档、永久删除、分叉都不许对它下手。
821
+ for (const sep of ['\\', '/']) {
822
+ if (resolved.startsWith(teammateRoot() + sep)) {
823
+ throw new Error(`这是队友的运行记录,不能归档、永久删除或分叉:${basename(filePath)}`);
824
+ }
825
+ }
826
+ return resolved;
827
+ }
828
+ /** 再加一条:只有活动区的会话能被归档,归档区里的要走恢复。 */
829
+ function assertLiveSessionFile(filePath) {
830
+ const resolved = assertSessionFile(filePath);
831
+ if (resolved.startsWith(archivedRoot() + '\\') || resolved.startsWith(archivedRoot() + '/')) {
832
+ throw new Error(`这个会话已经在归档区:${basename(filePath)}`);
833
+ }
834
+ return resolved;
835
+ }
836
+ /** 把会话从当前位置移到 `from` 下同名层级,返回新路径。 */
837
+ function moveSessionFile(filePath, toRoot) {
838
+ const target = join(toRoot, basename(dirname(filePath)), basename(filePath));
839
+ mkdirSync(dirname(target), { recursive: true });
840
+ renameSync(filePath, target);
841
+ return target;
842
+ }
843
+ /** 归档一个会话:移进 `.archived/<工作目录名>/`,并在 sidecar 记归档时间。 */
844
+ export function archiveSession(filePath) {
845
+ const source = assertLiveSessionFile(filePath);
846
+ // T30:挪文件之前拿写租约——另一个窗口正开着它就明确拒绝,而不是把人家手里的路径挪没
847
+ return withExclusiveLock(source, busyLeaseMessage(source), () => {
848
+ const target = moveSessionFile(source, archivedRoot());
849
+ patchSessionMeta(uuidOf(source), { archivedAt: Date.now() });
850
+ return target;
851
+ });
852
+ }
853
+ /** 恢复一个归档会话:移回正常区,清掉归档时间。 */
854
+ export function restoreSession(archivedPath) {
855
+ const source = join(archivedPath);
856
+ if (!source.startsWith(archivedRoot()) || !existsSync(source))
857
+ throw new Error(`归档会话不存在:${archivedPath}`);
858
+ return withExclusiveLock(source, busyLeaseMessage(source), () => {
859
+ const target = moveSessionFile(source, sessionsRoot());
860
+ patchSessionMeta(uuidOf(source), { archivedAt: null });
861
+ return target;
862
+ });
863
+ }
864
+ /**
865
+ * 永久删除一个会话:移进回收站 `.trash/<工作目录名>/`,超过
866
+ * {@link TRASH_RETENTION_DAYS} 天后由 {@link sweepTrash} 清掉。
867
+ * 会话内容不再出现在任何列表里,但文件还在磁盘上可手工救回。
868
+ */
869
+ export function purgeSession(filePath) {
870
+ // 归档区和活动区都删得掉:设置页传进来的路径就在 .archived/ 里
871
+ const source = assertSessionFile(filePath);
872
+ return withExclusiveLock(source, busyLeaseMessage(source), () => {
873
+ const target = moveSessionFile(source, trashRoot());
874
+ dropSessionMeta(uuidOf(source));
875
+ return target;
876
+ });
877
+ }
878
+ /** 清掉回收站里超过保留期的文件(按 mtime 判断),失败的文件留着下次再清。 */
879
+ export function sweepTrash() {
880
+ const root = trashRoot();
881
+ if (!existsSync(root))
882
+ return 0;
883
+ const deadline = Date.now() - TRASH_RETENTION_DAYS * 24 * 60 * 60 * 1000;
884
+ let removed = 0;
885
+ for (const dir of readdirSync(root)) {
886
+ const dirPath = join(root, dir);
887
+ if (!statSync(dirPath).isDirectory())
888
+ continue;
889
+ for (const file of readdirSync(dirPath)) {
890
+ const filePath = join(dirPath, file);
891
+ try {
892
+ if (statSync(filePath).mtimeMs >= deadline)
893
+ continue;
894
+ unlinkSync(filePath);
895
+ removed += 1;
896
+ }
897
+ catch {
898
+ // 被别的程序占用就留着下次清
899
+ }
900
+ }
901
+ }
902
+ return removed;
903
+ }
904
+ /** 回收站里还留着多少个待清理文件(归档页拿来显示)。 */
905
+ export function countTrashFiles() {
906
+ const root = trashRoot();
907
+ if (!existsSync(root))
908
+ return 0;
909
+ let count = 0;
910
+ for (const dir of readdirSync(root)) {
911
+ const dirPath = join(root, dir);
912
+ if (!existsSync(dirPath) || !statSync(dirPath).isDirectory())
913
+ continue;
914
+ count += readdirSync(dirPath).filter((file) => file.endsWith('.jsonl')).length;
915
+ }
916
+ return count;
917
+ }
918
+ /**
919
+ * 分叉一个会话:复制日志到第 `beforeUserMessage` 条用户消息之前,
920
+ * 首行 meta 换成新 uuid 与新创建时间,返回新文件路径。
921
+ *
922
+ * @param filePath 源会话 jsonl 路径
923
+ * @param beforeUserMessage 以 0 起算的用户消息序号,分叉结果不含这一条及其之后
924
+ */
925
+ export function forkSession(filePath, beforeUserMessage) {
926
+ const source = assertLiveSessionFile(filePath);
927
+ const lines = readFileSync(source, 'utf8').split(/\r?\n/).filter((line) => line !== '');
928
+ const first = JSON.parse(lines[0] ?? '');
929
+ if (first.type !== 'meta')
930
+ throw new Error(`源会话首行不是 meta:${basename(source)}`);
931
+ let seen = 0;
932
+ let cut = lines.length;
933
+ let found = false;
934
+ for (let index = 1; index < lines.length; index += 1) {
935
+ if (!lines[index].startsWith('{"type":"user"'))
936
+ continue;
937
+ if (seen === beforeUserMessage) {
938
+ cut = index;
939
+ found = true;
940
+ break;
941
+ }
942
+ seen += 1;
943
+ }
944
+ if (!found) {
945
+ // 哨兵:beforeUserMessage 等于用户消息总数 = 整本分叉(/fork 命令与选择器
946
+ // Ctrl+F 都传 listUserMessages().length)——保留到文件末尾,含最后的回复与工具记录。
947
+ if (seen === beforeUserMessage) {
948
+ cut = lines.length;
949
+ }
950
+ else {
951
+ throw new Error(`这个会话没有第 ${beforeUserMessage + 1} 条用户消息,只有 ${seen} 条`);
952
+ }
953
+ }
954
+ if (cut <= 1)
955
+ throw new Error('分叉点之前没有任何消息,新会话会是空的');
956
+ const meta = { id: randomUUID(), cwd: first.cwd, createdAt: Date.now() };
957
+ const target = join(sessionsRoot(), slugCwd(first.cwd), `${meta.id}.jsonl`);
958
+ mkdirSync(dirname(target), { recursive: true });
959
+ writeFileSync(target, `${JSON.stringify({ type: 'meta', ...meta })}\n${lines.slice(1, cut).join('\n')}\n`, 'utf8');
960
+ patchSessionMeta(meta.id, { forkedFrom: first.id });
961
+ return target;
962
+ }
963
+ /** 读一个会话里的用户消息(分叉菜单用它选分叉位置,下标一一对应)。 */
964
+ export function readUserMessages(filePath) {
965
+ const lines = readFileSync(filePath, 'utf8').split(/\r?\n/);
966
+ const out = [];
967
+ for (const line of lines) {
968
+ if (!line.startsWith('{"type":"user"'))
969
+ continue;
970
+ try {
971
+ const record = JSON.parse(line);
972
+ out.push(record.text.replace(/\s+/g, ' ').trim().slice(0, 50));
973
+ }
974
+ catch {
975
+ // 坏行跳过,不影响其余可选位置
976
+ }
977
+ }
978
+ return out;
979
+ }
980
+ /** 文件名就是 `<uuid>.jsonl`,取 uuid 用来写 sidecar。 */
981
+ function uuidOf(filePath) {
982
+ return basename(filePath, '.jsonl');
983
+ }
984
+ /** T30:会话库操作撞上「另一个窗口开着它」时的统一说法。 */
985
+ function busyLeaseMessage(filePath) {
986
+ return `会话正在另一个 Pneuma 窗口使用,先关掉那边再操作:${basename(filePath)}`;
987
+ }
988
+ /** 记录/读取 last-session 指针(--resume 无参时的目标)。 */
989
+ const LAST_FILE = dscPath('.last-session');
990
+ export function saveLastSession(session) {
991
+ mkdirSync(dscPath(), { recursive: true });
992
+ writeFileSync(LAST_FILE, session.filePath, 'utf8');
993
+ }
994
+ export function loadLastSessionPath() {
995
+ if (!existsSync(LAST_FILE))
996
+ return null;
997
+ return readFileSync(LAST_FILE, 'utf8').trim() || null;
998
+ }