pi-agent-ide 0.5.0 → 0.6.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 (342) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/README.md +81 -23
  3. package/assets/gallery/apply-editing.png +0 -0
  4. package/assets/gallery/debugging.png +0 -0
  5. package/assets/gallery/read-web.png +0 -0
  6. package/assets/gallery/search-replace.png +0 -0
  7. package/assets/gallery/terminal.png +0 -0
  8. package/assets/gallery/tool-editing.png +0 -0
  9. package/dist/chunk-22CLOM2D.js +1 -0
  10. package/dist/{chunk-LY6U5EAD.js → chunk-2527OQ4Y.js} +1 -1
  11. package/dist/chunk-3XJ3KDPZ.js +16 -0
  12. package/dist/chunk-5JELU6RV.js +7 -0
  13. package/dist/chunk-DDKHZZDO.js +5 -0
  14. package/dist/chunk-FEDVTZJG.js +1 -0
  15. package/dist/chunk-HSM6R7IB.js +27 -0
  16. package/dist/chunk-K5G4FDXD.js +1 -0
  17. package/dist/chunk-M5IVSCR2.js +1 -0
  18. package/dist/chunk-OYWIZII6.js +1 -0
  19. package/dist/chunk-P7XO5CIP.js +2 -0
  20. package/dist/chunk-PCZH7GOX.js +26 -0
  21. package/dist/{chunk-DQXPQOF7.js → chunk-SQ7ZTGOP.js} +1 -1
  22. package/dist/entry-10.js +1 -1
  23. package/dist/entry-11.js +1 -1
  24. package/dist/entry-12.js +1 -1
  25. package/dist/entry-13.js +1 -1
  26. package/dist/entry-14.js +1 -1
  27. package/dist/entry-15.js +1 -1
  28. package/dist/entry-16.js +1 -0
  29. package/dist/entry-18.js +1 -1
  30. package/dist/entry-19.js +1 -1
  31. package/dist/entry-20.js +1 -1
  32. package/dist/entry-21.js +1 -1
  33. package/dist/entry-22.js +1 -2
  34. package/dist/entry-23.js +2 -1
  35. package/dist/entry-24.js +1 -1
  36. package/dist/entry-25.js +1 -1
  37. package/dist/entry-26.js +1 -1
  38. package/dist/entry-27.js +1 -12
  39. package/dist/entry-28.js +12 -1
  40. package/dist/entry-29.js +1 -1
  41. package/dist/entry-30.js +1 -1
  42. package/dist/entry-31.js +1 -1
  43. package/dist/entry-32.js +1 -4
  44. package/dist/entry-33.js +3 -8
  45. package/dist/entry-34.js +9 -1
  46. package/dist/entry-35.js +1 -1
  47. package/dist/entry-36.js +1 -1
  48. package/dist/entry-37.js +1 -1
  49. package/dist/entry-38.js +1 -1
  50. package/dist/entry-39.js +1 -1
  51. package/dist/entry-40.js +1 -1
  52. package/dist/entry-41.js +1 -18
  53. package/dist/entry-42.js +18 -1
  54. package/dist/entry-43.js +1 -1
  55. package/dist/entry-44.js +1 -1
  56. package/dist/entry-45.js +1 -1
  57. package/dist/entry-46.js +1 -7
  58. package/dist/entry-47.js +7 -11
  59. package/dist/entry-48.js +11 -1
  60. package/dist/entry-49.js +1 -1
  61. package/dist/entry-50.js +1 -1
  62. package/dist/entry-51.js +1 -1
  63. package/dist/entry-52.js +1 -1
  64. package/dist/entry-53.js +1 -1
  65. package/dist/entry-54.js +1 -1
  66. package/dist/entry-55.js +1 -1
  67. package/dist/entry-56.js +1 -0
  68. package/dist/entry-57.js +0 -1
  69. package/dist/entry-58.js +1 -0
  70. package/dist/entry-59.js +0 -1
  71. package/dist/entry-60.js +1 -1
  72. package/dist/entry-61.js +1 -1
  73. package/dist/entry-62.js +1 -1
  74. package/dist/entry-63.js +1 -1
  75. package/dist/entry-64.js +1 -1
  76. package/dist/entry-65.js +1 -0
  77. package/dist/entry-66.js +0 -1
  78. package/dist/entry-67.js +1 -0
  79. package/dist/entry-68.js +0 -1
  80. package/dist/entry-69.js +1 -1
  81. package/dist/entry-70.js +1 -1
  82. package/dist/entry-71.js +1 -203
  83. package/dist/entry-72.js +254 -25
  84. package/dist/entry-73.js +30 -2
  85. package/dist/entry-74.js +2 -1
  86. package/dist/entry-75.js +1 -1
  87. package/dist/entry-76.js +1 -1
  88. package/dist/entry-77.js +3 -3
  89. package/dist/entry-78.js +3 -1
  90. package/dist/entry-79.js +1 -1
  91. package/dist/entry-80.js +1 -1
  92. package/dist/entry-81.js +1 -0
  93. package/dist/pi-agent-ide.js +3 -3
  94. package/docs/configuration.md +3 -1
  95. package/docs/development.md +0 -6
  96. package/docs/tools.md +12 -0
  97. package/node_modules/pi-agent-doctor/package.json +6 -6
  98. package/node_modules/pi-agent-doctor/runtime/entry-22.js +1 -0
  99. package/node_modules/pi-agent-filesystem/package.json +7 -7
  100. package/node_modules/pi-agent-filesystem-image/package.json +4 -4
  101. package/node_modules/pi-agent-filesystem-pdf/package.json +4 -4
  102. package/node_modules/pi-agent-filesystem-text/package.json +4 -4
  103. package/node_modules/pi-agent-filesystem-text/runtime/entry-26.js +2 -0
  104. package/node_modules/pi-agent-ide-ast/package.json +12 -11
  105. package/node_modules/pi-agent-ide-ast/runtime/entry-27.js +0 -1
  106. package/node_modules/pi-agent-ide-ast/runtime/entry-28.js +2 -0
  107. package/node_modules/pi-agent-ide-ast/src/search-resolver.ts +8 -6
  108. package/node_modules/pi-agent-ide-changes/package.json +8 -8
  109. package/node_modules/pi-agent-ide-changes/runtime/entry-29.js +0 -1
  110. package/node_modules/pi-agent-ide-formatter/package.json +4 -4
  111. package/node_modules/pi-agent-ide-languages/package.json +3 -3
  112. package/node_modules/pi-agent-ide-lint/package.json +8 -8
  113. package/node_modules/pi-agent-ide-lint/src/command-linter.ts +3 -1
  114. package/node_modules/pi-agent-ide-lsp/package.json +10 -10
  115. package/node_modules/pi-agent-ide-lsp/runtime/entry-34.js +2 -0
  116. package/node_modules/pi-agent-image/package.json +3 -3
  117. package/node_modules/pi-agent-invariant/package.json +2 -2
  118. package/node_modules/pi-agent-pdf/package.json +3 -3
  119. package/node_modules/pi-agent-read/package.json +11 -11
  120. package/node_modules/pi-agent-read/runtime/entry-41.js +0 -1
  121. package/node_modules/pi-agent-read/runtime/entry-42.js +2 -0
  122. package/node_modules/pi-agent-read/src/api/read-parameters.ts +2 -2
  123. package/node_modules/pi-agent-read/src/core/tools/tool-read.ts +11 -6
  124. package/node_modules/pi-agent-resource/package.json +2 -2
  125. package/node_modules/pi-agent-search/package.json +7 -7
  126. package/node_modules/pi-agent-search/runtime/entry-46.js +0 -1
  127. package/node_modules/pi-agent-search-text/package.json +9 -9
  128. package/node_modules/pi-agent-search-text/runtime/entry-48.js +1 -0
  129. package/node_modules/pi-agent-search-text/src/resolvers.ts +32 -27
  130. package/node_modules/pi-agent-search-text/src/search-session.ts +27 -1
  131. package/node_modules/pi-agent-text/package.json +3 -3
  132. package/node_modules/pi-agent-text/runtime/entry-50.js +1 -0
  133. package/node_modules/pi-agent-text-anchor-constant/package.json +4 -4
  134. package/node_modules/pi-agent-text-anchor-constant/runtime/entry-51.js +2 -0
  135. package/node_modules/pi-agent-text-anchor-exact/index.ts +3 -3
  136. package/node_modules/pi-agent-text-anchor-exact/package.json +5 -5
  137. package/node_modules/pi-agent-text-anchor-exact/runtime/entry-52.js +0 -1
  138. package/node_modules/pi-agent-text-anchor-exact/runtime/entry-53.js +2 -0
  139. package/node_modules/pi-agent-text-anchor-exact/src/config.ts +2 -2
  140. package/node_modules/pi-agent-text-anchor-line-hash/package.json +7 -7
  141. package/node_modules/pi-agent-text-anchor-line-hash/runtime/entry-54.js +0 -1
  142. package/node_modules/pi-agent-text-anchor-line-hash/runtime/entry-55.js +2 -0
  143. package/node_modules/pi-agent-text-editor/package.json +25 -25
  144. package/node_modules/pi-agent-text-editor/runtime/entry-71.js +0 -1
  145. package/node_modules/pi-agent-text-editor/src/core/apply/execution.ts +125 -3
  146. package/node_modules/pi-agent-text-editor/src/core/apply/mixed-source.ts +1 -1
  147. package/node_modules/pi-agent-text-editor/src/core/apply/mutation-outcome.ts +14 -0
  148. package/node_modules/pi-agent-text-editor/src/core/apply/output.ts +32 -3
  149. package/node_modules/pi-agent-text-editor/src/core/apply/prompt.ts +21 -19
  150. package/node_modules/pi-agent-text-editor/src/core/apply/renderer.ts +37 -0
  151. package/node_modules/pi-agent-text-editor/src/core/apply/runtime.ts +85 -34
  152. package/node_modules/pi-agent-text-editor/src/core/apply/tool.ts +2 -3
  153. package/node_modules/pi-agent-text-editor/src/core/apply/transaction.ts +589 -241
  154. package/node_modules/pi-agent-text-editor/src/core/post-edit-scope.ts +5 -0
  155. package/node_modules/pi-agent-text-editor/src/core/text-editor-config.ts +3 -3
  156. package/node_modules/pi-agent-text-editor/src/core/text-editor-core.ts +9 -1
  157. package/node_modules/pi-agent-text-editor/src/tools/text-tool-schema-properties.ts +53 -0
  158. package/node_modules/pi-agent-text-editor/src/tools/tool-text-copy.ts +11 -41
  159. package/node_modules/pi-agent-text-editor/src/tools/tool-text-delete.ts +8 -18
  160. package/node_modules/pi-agent-text-editor/src/tools/tool-text-move.ts +11 -41
  161. package/node_modules/pi-agent-text-editor/src/tools/tool-text-replace.ts +9 -19
  162. package/node_modules/pi-agent-text-editor-renderer/package.json +6 -6
  163. package/node_modules/pi-agent-text-editor-stale-anchor/package.json +5 -5
  164. package/node_modules/pi-agent-text-editor-stale-anchor/runtime/entry-74.js +2 -0
  165. package/node_modules/pi-agent-tool-call-interception/package.json +3 -3
  166. package/node_modules/pi-agent-tool-ui/package.json +2 -2
  167. package/node_modules/pi-agent-tool-ui/runtime/entry-76.js +1 -0
  168. package/node_modules/pi-agent-web/package.json +7 -7
  169. package/node_modules/pi-agent-web/src/resolver.ts +128 -1
  170. package/node_modules/pi-agent-web-html/package.json +3 -3
  171. package/node_modules/pi-agent-web-image/package.json +4 -4
  172. package/node_modules/pi-agent-web-pdf/package.json +4 -4
  173. package/node_modules/pi-agent-web-text/package.json +4 -4
  174. package/node_modules/pi-agent-web-text/runtime/entry-81.js +2 -0
  175. package/package.json +44 -39
  176. package/src/api/path-identity.ts +15 -0
  177. package/src/composite/builtin-extensions.ts +7 -2
  178. package/src/composite/module-labels.ts +4 -0
  179. package/src/composite/module-settings.ts +34 -14
  180. package/src/composite/preferences.ts +46 -3
  181. package/src/composite/selection.ts +5 -1
  182. package/src/doctor/inventory.ts +7 -2
  183. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/package.json +7 -7
  184. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/package.json +4 -4
  185. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-pdf/package.json +4 -4
  186. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/package.json +4 -4
  187. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/runtime/entry-26.js +2 -0
  188. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/runtime/entry-23.js +2 -0
  189. package/src/extensions/pi-agent-read/extensions/pi-agent-web/package.json +7 -7
  190. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/package.json +3 -3
  191. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-image/package.json +4 -4
  192. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-pdf/package.json +4 -4
  193. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/package.json +4 -4
  194. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/runtime/entry-81.js +2 -0
  195. package/src/extensions/pi-agent-read/extensions/pi-agent-web/runtime/entry-77.js +2 -0
  196. package/src/extensions/pi-agent-read/extensions/pi-agent-web/src/resolver.ts +128 -1
  197. package/src/extensions/pi-agent-read/package.json +11 -11
  198. package/src/extensions/pi-agent-read/runtime/entry-41.js +0 -1
  199. package/src/extensions/pi-agent-read/runtime/entry-42.js +2 -0
  200. package/src/extensions/pi-agent-read/src/api/read-parameters.ts +2 -2
  201. package/src/extensions/pi-agent-read/src/core/tools/tool-read.ts +11 -6
  202. package/src/extensions/pi-agent-search/package.json +7 -7
  203. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/package.json +9 -9
  204. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/runtime/entry-48.js +1 -0
  205. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/runtime/entry-49.js +1 -0
  206. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/resolvers.ts +32 -27
  207. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-session.ts +27 -1
  208. package/src/extensions/pi-agent-search/runtime/entry-46.js +0 -1
  209. package/src/extensions/pi-agent-search/runtime/entry-47.js +2 -0
  210. package/src/extensions/pi-agent-text-editor/package.json +25 -25
  211. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/package.json +4 -4
  212. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/runtime/entry-51.js +2 -0
  213. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/index.ts +3 -3
  214. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/package.json +5 -5
  215. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/runtime/entry-52.js +0 -1
  216. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/runtime/entry-53.js +2 -0
  217. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/src/config.ts +2 -2
  218. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/package.json +7 -7
  219. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/runtime/entry-54.js +0 -1
  220. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/runtime/entry-55.js +2 -0
  221. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/package.json +6 -6
  222. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/package.json +5 -5
  223. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/runtime/entry-74.js +2 -0
  224. package/src/extensions/pi-agent-text-editor/runtime/entry-71.js +0 -1
  225. package/src/extensions/pi-agent-text-editor/runtime/entry-72.js +2 -0
  226. package/src/extensions/pi-agent-text-editor/src/core/apply/execution.ts +125 -3
  227. package/src/extensions/pi-agent-text-editor/src/core/apply/mixed-source.ts +1 -1
  228. package/src/extensions/pi-agent-text-editor/src/core/apply/mutation-outcome.ts +14 -0
  229. package/src/extensions/pi-agent-text-editor/src/core/apply/output.ts +32 -3
  230. package/src/extensions/pi-agent-text-editor/src/core/apply/prompt.ts +21 -19
  231. package/src/extensions/pi-agent-text-editor/src/core/apply/renderer.ts +37 -0
  232. package/src/extensions/pi-agent-text-editor/src/core/apply/runtime.ts +85 -34
  233. package/src/extensions/pi-agent-text-editor/src/core/apply/tool.ts +2 -3
  234. package/src/extensions/pi-agent-text-editor/src/core/apply/transaction.ts +589 -241
  235. package/src/extensions/pi-agent-text-editor/src/core/post-edit-scope.ts +5 -0
  236. package/src/extensions/pi-agent-text-editor/src/core/text-editor-config.ts +3 -3
  237. package/src/extensions/pi-agent-text-editor/src/core/text-editor-core.ts +9 -1
  238. package/src/extensions/pi-agent-text-editor/src/tools/text-tool-schema-properties.ts +53 -0
  239. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-copy.ts +11 -41
  240. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-delete.ts +8 -18
  241. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-move.ts +11 -41
  242. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-replace.ts +9 -19
  243. package/src/pi-agent-ide.ts +15 -2
  244. package/src/plugins/pi-agent-ide-ast/package.json +12 -11
  245. package/src/plugins/pi-agent-ide-ast/runtime/entry-27.js +0 -1
  246. package/src/plugins/pi-agent-ide-ast/runtime/entry-28.js +2 -0
  247. package/src/plugins/pi-agent-ide-ast/src/search-resolver.ts +8 -6
  248. package/src/plugins/pi-agent-ide-changes/package.json +8 -8
  249. package/src/plugins/pi-agent-ide-changes/runtime/entry-29.js +0 -1
  250. package/src/plugins/pi-agent-ide-debugger/index.ts +3 -9
  251. package/src/plugins/pi-agent-ide-debugger/src/adapter-executables.ts +31 -0
  252. package/src/plugins/pi-agent-ide-debugger/src/breakpoint-view.ts +3 -1
  253. package/src/plugins/pi-agent-ide-debugger/src/catalog.ts +24 -23
  254. package/src/plugins/pi-agent-ide-debugger/src/dap-client.ts +5 -2
  255. package/src/plugins/pi-agent-ide-debugger/src/doctor-plugin.ts +55 -17
  256. package/src/plugins/pi-agent-ide-debugger/src/session-manager.ts +55 -22
  257. package/src/plugins/pi-agent-ide-formatter/package.json +4 -4
  258. package/src/plugins/pi-agent-ide-languages/package.json +3 -3
  259. package/src/plugins/pi-agent-ide-lint/package.json +8 -8
  260. package/src/plugins/pi-agent-ide-lint/src/command-linter.ts +3 -1
  261. package/src/plugins/pi-agent-ide-lsp/package.json +10 -10
  262. package/src/plugins/pi-agent-ide-lsp/runtime/entry-34.js +2 -0
  263. package/src/plugins/pi-agent-ide-processes/src/registry.ts +6 -0
  264. package/src/plugins/pi-agent-ide-processes/src/ui.ts +28 -11
  265. package/src/plugins/pi-agent-ide-terminal/index.ts +1 -1
  266. package/src/plugins/pi-agent-ide-terminal/src/process-provider.ts +2 -0
  267. package/src/plugins/pi-agent-ide-terminal/src/search.ts +32 -2
  268. package/src/plugins/pi-agent-ide-terminal/src/session-manager.ts +5 -4
  269. package/src/plugins/pi-agent-ide-terminal/src/tools.ts +2 -2
  270. package/src/plugins/pi-agent-ide-terminal/src/types.ts +3 -1
  271. package/src/plugins/pi-agent-ide-terminal/src/ui.ts +1 -1
  272. package/src/plugins/pi-agent-ide-vision/index.ts +232 -0
  273. package/src/plugins/pi-agent-ide-vision/src/vision.ts +530 -0
  274. package/src/plugins/pi-agent-ide-vision/src/windows-display-capture.ps1 +30 -0
  275. package/src/plugins/pi-agent-ide-vision/src/windows-window-capture.ps1 +33 -0
  276. package/dist/chunk-5PLV4HLG.js +0 -1
  277. package/dist/chunk-BGUSBUUD.js +0 -26
  278. package/dist/chunk-G725MARR.js +0 -16
  279. package/dist/chunk-HKXDP2FD.js +0 -7
  280. package/dist/chunk-J4RTW44E.js +0 -1
  281. package/dist/chunk-QH4ZUDOD.js +0 -1
  282. package/dist/chunk-WGGIQ7IJ.js +0 -1
  283. package/dist/chunk-ZG2MWXCR.js +0 -2
  284. package/dist/chunk-ZUMN6U77.js +0 -27
  285. package/dist/entry-17.js +0 -1
  286. package/docs/evaluation.md +0 -14
  287. package/node_modules/pi-agent-doctor/runtime/entry-17.js +0 -1
  288. package/node_modules/pi-agent-filesystem/runtime/entry-22.js +0 -2
  289. package/node_modules/pi-agent-ide-ast/runtime/entry-26.js +0 -1
  290. package/node_modules/pi-agent-ide-changes/runtime/entry-28.js +0 -1
  291. package/node_modules/pi-agent-image/runtime/entry-34.js +0 -1
  292. package/node_modules/pi-agent-resource/runtime/entry-42.js +0 -1
  293. package/node_modules/pi-agent-text-anchor-constant/runtime/entry-50.js +0 -2
  294. package/node_modules/pi-agent-text-anchor-exact/runtime/entry-51.js +0 -1
  295. package/node_modules/pi-agent-text-anchor-line-hash/runtime/entry-53.js +0 -1
  296. package/node_modules/pi-agent-text-editor/runtime/entry-55.js +0 -1
  297. package/node_modules/pi-agent-tool-call-interception/runtime/entry-74.js +0 -1
  298. package/node_modules/pi-agent-web/runtime/entry-76.js +0 -2
  299. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/runtime/entry-23.js +0 -2
  300. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/runtime/entry-22.js +0 -2
  301. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/runtime/entry-77.js +0 -2
  302. package/src/extensions/pi-agent-read/extensions/pi-agent-web/runtime/entry-76.js +0 -2
  303. package/src/extensions/pi-agent-read/runtime/entry-37.js +0 -1
  304. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/runtime/entry-47.js +0 -2
  305. package/src/extensions/pi-agent-search/runtime/entry-43.js +0 -1
  306. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/runtime/entry-50.js +0 -2
  307. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/runtime/entry-51.js +0 -1
  308. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/runtime/entry-53.js +0 -1
  309. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/runtime/entry-72.js +0 -2
  310. package/src/extensions/pi-agent-text-editor/runtime/entry-55.js +0 -1
  311. package/src/plugins/pi-agent-ide-ast/runtime/entry-26.js +0 -1
  312. package/src/plugins/pi-agent-ide-changes/runtime/entry-28.js +0 -1
  313. /package/node_modules/{pi-agent-filesystem-image → pi-agent-filesystem}/runtime/entry-23.js +0 -0
  314. /package/node_modules/{pi-agent-filesystem-pdf → pi-agent-filesystem-image}/runtime/entry-24.js +0 -0
  315. /package/node_modules/{pi-agent-filesystem-text → pi-agent-filesystem-pdf}/runtime/entry-25.js +0 -0
  316. /package/node_modules/{pi-agent-ide-formatter → pi-agent-ide-changes}/runtime/entry-30.js +0 -0
  317. /package/node_modules/{pi-agent-ide-languages → pi-agent-ide-formatter}/runtime/entry-31.js +0 -0
  318. /package/node_modules/{pi-agent-ide-lint → pi-agent-ide-languages}/runtime/entry-32.js +0 -0
  319. /package/node_modules/{pi-agent-ide-lsp → pi-agent-ide-lint}/runtime/entry-33.js +0 -0
  320. /package/node_modules/{pi-agent-invariant → pi-agent-image}/runtime/entry-35.js +0 -0
  321. /package/node_modules/{pi-agent-pdf → pi-agent-invariant}/runtime/entry-36.js +0 -0
  322. /package/node_modules/{pi-agent-read → pi-agent-pdf}/runtime/entry-37.js +0 -0
  323. /package/node_modules/{pi-agent-search → pi-agent-resource}/runtime/entry-43.js +0 -0
  324. /package/node_modules/{pi-agent-search-text → pi-agent-search}/runtime/entry-47.js +0 -0
  325. /package/node_modules/{pi-agent-text → pi-agent-search-text}/runtime/entry-49.js +0 -0
  326. /package/node_modules/{pi-agent-text-editor-renderer → pi-agent-text-editor}/runtime/entry-72.js +0 -0
  327. /package/node_modules/{pi-agent-text-editor-stale-anchor → pi-agent-text-editor-renderer}/runtime/entry-73.js +0 -0
  328. /package/node_modules/{pi-agent-tool-ui → pi-agent-tool-call-interception}/runtime/entry-75.js +0 -0
  329. /package/node_modules/{pi-agent-web-html → pi-agent-web}/runtime/entry-77.js +0 -0
  330. /package/node_modules/{pi-agent-web-image → pi-agent-web-html}/runtime/entry-78.js +0 -0
  331. /package/node_modules/{pi-agent-web-pdf → pi-agent-web-image}/runtime/entry-79.js +0 -0
  332. /package/node_modules/{pi-agent-web-text → pi-agent-web-pdf}/runtime/entry-80.js +0 -0
  333. /package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/{pi-agent-filesystem-pdf → pi-agent-filesystem-image}/runtime/entry-24.js +0 -0
  334. /package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/{pi-agent-filesystem-text → pi-agent-filesystem-pdf}/runtime/entry-25.js +0 -0
  335. /package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/{pi-agent-web-image → pi-agent-web-html}/runtime/entry-78.js +0 -0
  336. /package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/{pi-agent-web-pdf → pi-agent-web-image}/runtime/entry-79.js +0 -0
  337. /package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/{pi-agent-web-text → pi-agent-web-pdf}/runtime/entry-80.js +0 -0
  338. /package/src/extensions/pi-agent-text-editor/plugins/{pi-agent-text-editor-stale-anchor → pi-agent-text-editor-renderer}/runtime/entry-73.js +0 -0
  339. /package/src/plugins/{pi-agent-ide-formatter → pi-agent-ide-changes}/runtime/entry-30.js +0 -0
  340. /package/src/plugins/{pi-agent-ide-languages → pi-agent-ide-formatter}/runtime/entry-31.js +0 -0
  341. /package/src/plugins/{pi-agent-ide-lint → pi-agent-ide-languages}/runtime/entry-32.js +0 -0
  342. /package/src/plugins/{pi-agent-ide-lsp → pi-agent-ide-lint}/runtime/entry-33.js +0 -0
@@ -1,9 +1,12 @@
1
1
  import { lstat, mkdir, readFile, rm, writeFile } from "node:fs/promises";
2
2
  import path from "node:path";
3
3
  import fs from "fs-extra";
4
+ import { requiredValue } from "pi-agent-invariant";
4
5
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
5
6
  import { applyTextChanges, type TextChange } from "#src/core/text-change-engine.js";
6
7
  import type { TextEditorCore } from "#src/core/text-editor-core.js";
8
+
9
+ import { forgetDeferredPostEdit } from "#src/core/post-edit-scope.js";
7
10
  import type {
8
11
  ScriptMutationFile,
9
12
  ScriptMutationOutcome,
@@ -14,176 +17,574 @@ export interface EditorSnapshot {
14
17
  readonly source: string;
15
18
  readonly content: string;
16
19
  }
17
-
18
20
  interface Selection {
19
21
  readonly document: string;
20
22
  readonly from: number;
21
23
  readonly to: number;
22
24
  readonly text: string;
25
+ readonly linewise?: boolean;
23
26
  }
24
-
25
27
  interface TextOperation {
26
28
  readonly kind: "replace";
27
29
  readonly selection: Selection;
28
30
  readonly text: string;
29
31
  }
30
-
32
+ type TextTransferOperation = {
33
+ readonly sources: readonly Selection[];
34
+ readonly destinations: readonly Selection[];
35
+ readonly text: string;
36
+ } & ({ readonly kind: "text-copy" } | { readonly kind: "text-move" });
37
+ interface WarningOperation {
38
+ readonly kind: "warning";
39
+ readonly document: string;
40
+ readonly query?: string;
41
+ readonly warning?: unknown;
42
+ }
31
43
  interface CreateOperation {
32
44
  readonly kind: "create";
33
45
  readonly path: string;
34
46
  readonly content: string;
35
47
  }
36
-
37
48
  interface FileOperation {
38
49
  readonly kind: "delete" | "copy" | "move";
39
50
  readonly path: string;
40
51
  readonly target?: string;
41
52
  readonly overwrite?: boolean;
42
53
  }
43
-
54
+ type Operation =
55
+ | TextOperation
56
+ | TextTransferOperation
57
+ | WarningOperation
58
+ | CreateOperation
59
+ | FileOperation;
44
60
  export interface EditorTransactionRequest {
45
61
  readonly snapshots: readonly EditorSnapshot[];
46
- readonly operations: readonly (TextOperation | CreateOperation | FileOperation)[];
62
+ readonly operations: readonly Operation[];
47
63
  }
48
-
49
- interface SavedPath {
64
+ /** Captured path state used to restore one failed operation. */
65
+ export interface SavedPath {
50
66
  readonly path: string;
51
67
  readonly existed: boolean;
52
68
  readonly bytes?: Uint8Array;
53
69
  }
70
+ export interface ApplyOperationOutcome {
71
+ readonly index: number;
72
+ readonly kind: Operation["kind"];
73
+ readonly status: "applied" | "warning" | "failed" | "unknown" | "blocked";
74
+ readonly effect: "applied" | "not-applied" | "unknown";
75
+ readonly resources: readonly string[];
76
+ readonly error?: { readonly code: string; readonly message: string };
77
+ readonly warning?: {
78
+ readonly code: string;
79
+ readonly message: string;
80
+ readonly presentation?: unknown;
81
+ };
82
+ }
83
+
84
+ export type TransactionEditor = Pick<
85
+ TextEditorCore,
86
+ "editTexts" | "postProcessFile" | "recordApplyUndo"
87
+ >;
54
88
 
55
- /** Commit one snapshot-guarded Apply transaction and restore touched paths after a write failure. */
89
+ /** Optional transaction execution dependencies for deterministic failure handling. */
90
+ export interface TransactionExecutionOptions {
91
+ /** Restore one captured path after an operation fails. */
92
+ readonly restorePath?: (state: SavedPath) => Promise<void>;
93
+ }
94
+
95
+ /** Commit staged operations independently and return every outcome in staging order. */
56
96
  export async function executeEditorTransaction(
57
- editor: TextEditorCore,
97
+ editor: TransactionEditor,
58
98
  input: unknown,
59
99
  signal: AbortSignal,
60
- context: ExtensionContext,
100
+ context: Pick<ExtensionContext, "cwd">,
101
+ options: TransactionExecutionOptions = {},
61
102
  ): Promise<ScriptMutationOutcome> {
62
103
  const request = parseRequest(input);
63
- if (request.operations.length === 0) throw invalid("apply() requires at least one staged change");
64
-
104
+ if (request.operations.length === 0) throw invalid("flush() requires at least one staged change");
65
105
  const snapshots = new Map(request.snapshots.map((snapshot) => [snapshot.id, snapshot]));
66
106
  if (snapshots.size !== request.snapshots.length) throw invalid("Duplicate editor snapshot id");
67
- const textChanges = new Map<string, TextChange[]>();
68
- const creates: CreateOperation[] = [];
69
- const fileOperations: FileOperation[] = [];
70
107
 
71
- for (const operation of request.operations) {
108
+ const accepted = new Map<string, TextChange[]>();
109
+ const unknown = new Set<string>();
110
+ const journal = new Map<string, SavedPath>();
111
+ const files: ScriptMutationFile[] = [];
112
+ const operations: ApplyOperationOutcome[] = [];
113
+ const cwd = context.cwd;
114
+
115
+ for (const [index, operation] of request.operations.entries()) {
116
+ const resources = operationResources(operation, snapshots, cwd);
117
+ if (operation.kind === "warning") {
118
+ operations.push({
119
+ index,
120
+ kind: "warning",
121
+ status: "warning",
122
+ effect: "not-applied",
123
+ resources,
124
+ warning: warningDetails(operation),
125
+ });
126
+ continue;
127
+ }
128
+ if (resources.some((resource) => unknown.has(resource))) {
129
+ operations.push({
130
+ index,
131
+ kind: operation.kind,
132
+ status: "blocked",
133
+ effect: "unknown",
134
+ resources,
135
+ error: {
136
+ code: "DEPENDENCY_UNKNOWN",
137
+ message: "A prior operation left a required resource in an unknown state.",
138
+ },
139
+ });
140
+ continue;
141
+ }
72
142
  if (operation.kind === "replace") {
73
143
  const snapshot = snapshots.get(operation.selection.document);
74
- if (snapshot === undefined) throw invalid("Selection belongs to an unknown editor snapshot");
75
- const { from, to, text } = operation.selection;
76
- if (
77
- !Number.isInteger(from) ||
78
- !Number.isInteger(to) ||
79
- from < 0 ||
80
- to < from ||
81
- to > snapshot.content.length
82
- )
83
- throw invalid(`Selection ${from}-${to} is outside ${snapshot.source}`);
84
- if (snapshot.content.slice(from, to) !== text)
85
- throw invalid(`Selection text does not match ${snapshot.source}`);
86
- const changes = textChanges.get(snapshot.source) ?? [];
87
- changes.push({ from, to, insert: operation.text });
88
- textChanges.set(snapshot.source, changes);
89
- } else if (operation.kind === "create") creates.push(operation);
90
- else fileOperations.push(operation);
144
+ const prior = snapshot === undefined ? [] : (accepted.get(snapshot.source) ?? []);
145
+ if (prior.some((change) => overlaps(change, operation.selection))) {
146
+ operations.push(
147
+ failed(
148
+ index,
149
+ operation,
150
+ resources,
151
+ invalid(`Overlapping selection in ${snapshot?.source ?? "unknown snapshot"}`),
152
+ ),
153
+ );
154
+ continue;
155
+ }
156
+ }
157
+
158
+ let saved: readonly SavedPath[] = [];
159
+ try {
160
+ await validateOperation(operation, snapshots, accepted, cwd);
161
+ saved = await Promise.all(resources.map(savePath));
162
+ const produced = await executeOperation(operation, snapshots, accepted, editor, cwd, signal);
163
+ for (const state of saved) if (!journal.has(state.path)) journal.set(state.path, state);
164
+ files.push(...produced);
165
+ operations.push({
166
+ index,
167
+ kind: operation.kind,
168
+ status: "applied",
169
+ effect: "applied",
170
+ resources,
171
+ });
172
+ } catch (error) {
173
+ const rollbackErrors = await rollback(saved, options.restorePath);
174
+ if (rollbackErrors.length > 0) {
175
+ for (const resource of resources) unknown.add(resource);
176
+ operations.push({
177
+ index,
178
+ kind: operation.kind,
179
+ status: "unknown",
180
+ effect: "unknown",
181
+ resources,
182
+ error: codedError(error, rollbackErrors),
183
+ });
184
+ } else operations.push(failed(index, operation, resources, error));
185
+ }
91
186
  }
92
187
 
93
- validateTextChanges(textChanges);
94
- const cwd = context.cwd;
95
- const touched = touchedPaths(cwd, textChanges, creates, fileOperations);
96
- await preflight(touched, snapshots, textChanges, creates, fileOperations, cwd);
97
- const saved = await Promise.all([...touched].map(savePath));
98
- const files: ScriptMutationFile[] = [];
188
+ const successful = operations.some(({ effect }) => effect === "applied");
189
+ const transaction = successful ? await editor.recordApplyUndo([...journal.values()]) : undefined;
190
+ return {
191
+ operation: "apply",
192
+ ok: operations.length > 0 && operations.every(({ status }) => status === "applied"),
193
+ effect: unknown.size > 0 ? "unknown" : successful ? "applied" : "not-applied",
194
+ ...(transaction === undefined ? {} : { transaction }),
195
+ files,
196
+ completed: [
197
+ ...new Set(
198
+ operations
199
+ .filter(({ effect }) => effect === "applied")
200
+ .flatMap(({ resources }) => resources),
201
+ ),
202
+ ],
203
+ errors: operations.flatMap((item) =>
204
+ item.status === "warning" || item.error === undefined
205
+ ? []
206
+ : [{ source: item.resources.join(", "), ...item.error }],
207
+ ),
208
+ operations,
209
+ };
210
+ }
99
211
 
100
- try {
101
- if (textChanges.size > 0 || creates.length > 0) {
102
- const createMap = new Map(
103
- creates.map((item) => [path.resolve(cwd, item.path), item.content]),
104
- );
105
- const sources = new Set([...textChanges.keys(), ...createMap.keys()]);
106
- const outcome = await editor.editTexts(
107
- [...sources].map((source) => ({
108
- source,
109
- read: true,
110
- ...(createMap.has(source) && { allowReadFailure: true }),
111
- })),
112
- { cwd, signal, intent: "edit" },
113
- (texts) => {
114
- for (const snapshot of snapshots.values()) {
115
- if (textChanges.has(snapshot.source) && texts.get(snapshot.source) !== snapshot.content)
116
- throw stale(snapshot.source);
117
- }
118
- const changes = new Map<string, readonly TextChange[]>();
119
- for (const source of sources) {
120
- const before = texts.get(source) ?? "";
121
- const planned = textChanges.get(source) ?? [
122
- { from: 0, to: 0, insert: createMap.get(source) ?? "" },
123
- ];
124
- changes.set(source, planned);
125
- const applied = applyTextChanges(before, planned, createMap.has(source));
126
- files.push({
127
- source,
128
- before: createMap.has(source) ? null : before,
129
- after: applied.content,
130
- action: createMap.has(source) ? "created" : "edited",
131
- changes: applied.changes.map((change, editIndex) => ({
132
- editIndex,
133
- fromA: change.fromBefore,
134
- toA: change.toBefore,
135
- fromB: change.fromAfter,
136
- toB: change.toAfter,
137
- removedText: change.removedText,
138
- insertedText: change.insertedText,
139
- })),
140
- formatting: { status: "not-reported" },
141
- });
142
- }
143
- return { changes, result: undefined };
144
- },
212
+ async function validateOperation(
213
+ operation: Operation,
214
+ snapshots: ReadonlyMap<string, EditorSnapshot>,
215
+ accepted: ReadonlyMap<string, readonly TextChange[]>,
216
+ cwd: string,
217
+ ): Promise<void> {
218
+ if (operation.kind === "warning") return;
219
+ if (operation.kind === "text-copy" || operation.kind === "text-move") {
220
+ const selected =
221
+ operation.kind === "text-move"
222
+ ? [...operation.destinations, ...operation.sources]
223
+ : [...operation.destinations];
224
+ for (const selection of [...operation.sources, ...operation.destinations])
225
+ validateSelection(
226
+ { kind: "replace", selection, text: "" },
227
+ snapshots.get(selection.document),
145
228
  );
146
- if (outcome.kind === "failed")
147
- throw outcome.failure.cause instanceof Error
148
- ? outcome.failure.cause
149
- : new Error(outcome.failure.message);
229
+ for (const [index, selection] of selected.entries()) {
230
+ const snapshot = requiredValue(snapshots.get(selection.document));
231
+ const prior = accepted.get(snapshot.source) ?? [];
232
+ if (
233
+ prior.some((change) => overlaps(change, selection)) ||
234
+ selected
235
+ .slice(0, index)
236
+ .some((other) => other.document === selection.document && rangesOverlap(other, selection))
237
+ )
238
+ throw invalid(`Overlapping selection in ${snapshot.source}`);
150
239
  }
151
-
152
- for (const operation of fileOperations) await performFileOperation(operation, cwd);
153
- for (const target of fileOperations.flatMap((operation) =>
154
- operation.target === undefined ? [] : [path.resolve(cwd, operation.target)],
240
+ for (const document of new Set(
241
+ [...operation.sources, ...operation.destinations].map(({ document }) => document),
155
242
  )) {
156
- if ((await optionalStat(target)) !== undefined)
157
- await editor.postProcessFile(target, { cwd, signal });
243
+ const snapshot = requiredValue(snapshots.get(document));
244
+ const expected = applyTextChanges(
245
+ snapshot.content,
246
+ accepted.get(snapshot.source) ?? [],
247
+ ).content;
248
+ let current;
249
+ try {
250
+ current = await readFile(snapshot.source, "utf8");
251
+ } catch {
252
+ throw stale(snapshot.source);
253
+ }
254
+ if (current !== expected) throw stale(snapshot.source);
158
255
  }
159
- const transaction = await editor.recordApplyUndo(saved);
160
- return {
161
- operation: "apply",
162
- transaction,
163
- ok: true,
164
- effect: "applied",
165
- files,
166
- completed: [...touched],
167
- errors: [],
168
- };
169
- } catch (error) {
170
- const rollbackErrors = await rollback(saved);
171
- throw Object.assign(new Error(error instanceof Error ? error.message : String(error)), {
172
- code: "TRANSACTION_FAILED",
173
- details: {
174
- effect: rollbackErrors.length === 0 ? "rolled-back" : "rollback-incomplete",
175
- files: [...touched],
176
- rollbackErrors,
256
+ return;
257
+ }
258
+ if (operation.kind === "replace") {
259
+ const snapshot = snapshots.get(operation.selection.document);
260
+ validateSelection(operation, snapshot);
261
+ const expected = applyTextChanges(
262
+ snapshot.content,
263
+ accepted.get(snapshot.source) ?? [],
264
+ ).content;
265
+ let current: string;
266
+ try {
267
+ current = await readFile(snapshot.source, "utf8");
268
+ } catch {
269
+ throw stale(snapshot.source);
270
+ }
271
+ if (current !== expected) throw stale(snapshot.source);
272
+ return;
273
+ }
274
+ const source = path.resolve(cwd, operation.path);
275
+ const sourceStat = await optionalStat(source);
276
+ if (operation.kind === "create") {
277
+ if (sourceStat !== undefined) throw invalid(`Create target exists: ${operation.path}`);
278
+ return;
279
+ }
280
+ if (sourceStat === undefined) throw invalid(`Source does not exist: ${operation.path}`);
281
+ if (!sourceStat.isFile() || sourceStat.isSymbolicLink())
282
+ throw invalid(`${source} is not a regular file`);
283
+ if (operation.kind === "delete") return;
284
+ const target = path.resolve(cwd, operation.target ?? "");
285
+ if (source === target) throw invalid("Source and target are the same file");
286
+ const targetStat = await optionalStat(target);
287
+ if (targetStat !== undefined && (!targetStat.isFile() || targetStat.isSymbolicLink()))
288
+ throw invalid(`${target} is not a regular file`);
289
+ if (targetStat !== undefined && operation.overwrite !== true)
290
+ throw invalid(`Target exists: ${operation.target}`);
291
+ }
292
+
293
+ async function executeOperation(
294
+ operation: Operation,
295
+ snapshots: ReadonlyMap<string, EditorSnapshot>,
296
+ accepted: Map<string, TextChange[]>,
297
+ editor: TransactionEditor,
298
+ cwd: string,
299
+ signal: AbortSignal,
300
+ ): Promise<ScriptMutationFile[]> {
301
+ if (operation.kind === "warning") return [];
302
+ if (operation.kind === "text-copy" || operation.kind === "text-move")
303
+ return executeTextTransfer(operation, snapshots, accepted, editor, cwd, signal);
304
+ if (operation.kind === "replace") {
305
+ const snapshot = snapshots.get(operation.selection.document);
306
+ if (snapshot === undefined) throw invalid("Selection belongs to an unknown editor snapshot");
307
+ const prior = accepted.get(snapshot.source) ?? [];
308
+ const next = [
309
+ ...prior,
310
+ {
311
+ from: operation.selection.from,
312
+ to: operation.selection.to,
313
+ insert: preserveLineEnding(operation.selection, operation.text),
177
314
  },
315
+ ];
316
+ const before = applyTextChanges(snapshot.content, prior).content;
317
+ const applied = applyTextChanges(snapshot.content, next);
318
+ await editOne(editor, snapshot.source, before, applied.content, cwd, signal, false);
319
+ accepted.set(snapshot.source, next);
320
+ return [
321
+ {
322
+ source: snapshot.source,
323
+ before,
324
+ after: applied.content,
325
+ action: "edited",
326
+ changes: applied.changes.map((change, editIndex) => ({
327
+ editIndex,
328
+ fromA: change.fromBefore,
329
+ toA: change.toBefore,
330
+ fromB: change.fromAfter,
331
+ toB: change.toAfter,
332
+ removedText: change.removedText,
333
+ insertedText: change.insertedText,
334
+ })),
335
+ formatting: { status: "not-reported" },
336
+ },
337
+ ];
338
+ }
339
+ if (operation.kind === "create") {
340
+ const source = path.resolve(cwd, operation.path);
341
+ await editOne(editor, source, "", operation.content, cwd, signal, true);
342
+ return [
343
+ {
344
+ source,
345
+ before: null,
346
+ after: operation.content,
347
+ action: "created",
348
+ changes: [],
349
+ formatting: { status: "not-reported" },
350
+ },
351
+ ];
352
+ }
353
+ await performFileOperation(operation, cwd);
354
+ if (operation.kind === "delete" || operation.kind === "move") {
355
+ forgetDeferredPostEdit(path.resolve(cwd, operation.path));
356
+ }
357
+ if (operation.target !== undefined) {
358
+ const target = path.resolve(cwd, operation.target);
359
+ if ((await optionalStat(target)) !== undefined)
360
+ await editor.postProcessFile(target, { cwd, signal });
361
+ }
362
+ return [];
363
+ }
364
+
365
+ function preserveLineEnding(selection: Selection, insert: string): string {
366
+ if (insert.length === 0 || selection.linewise !== true || /(?:\r\n|\r|\n)$/u.test(insert))
367
+ return insert;
368
+ return insert + (/(?:\r\n|\r|\n)$/u.exec(selection.text)?.[0] ?? "");
369
+ }
370
+ function rangesOverlap(left: Selection, right: Selection): boolean {
371
+ return left.from < right.to && right.from < left.to;
372
+ }
373
+ async function executeTextTransfer(
374
+ operation: TextTransferOperation,
375
+ snapshots: ReadonlyMap<string, EditorSnapshot>,
376
+ accepted: Map<string, TextChange[]>,
377
+ editor: TransactionEditor,
378
+ cwd: string,
379
+ signal: AbortSignal,
380
+ ): Promise<ScriptMutationFile[]> {
381
+ const additions = new Map<string, TextChange[]>();
382
+ for (const destination of operation.destinations) {
383
+ const snapshot = requiredValue(snapshots.get(destination.document));
384
+ const changes = additions.get(snapshot.source) ?? [];
385
+ changes.push({ from: destination.from, to: destination.to, insert: operation.text });
386
+ additions.set(snapshot.source, changes);
387
+ }
388
+ if (operation.kind === "text-move")
389
+ for (const source of operation.sources) {
390
+ const snapshot = requiredValue(snapshots.get(source.document));
391
+ const changes = additions.get(snapshot.source) ?? [];
392
+ changes.push({ from: source.from, to: source.to, insert: "" });
393
+ additions.set(snapshot.source, changes);
394
+ }
395
+ const sourcePaths = [...additions.keys()];
396
+ const before = new Map(
397
+ sourcePaths.map((source) => {
398
+ const snapshot = requiredValue(
399
+ [...snapshots.values()].find((item) => item.source === source),
400
+ );
401
+ return [
402
+ source,
403
+ applyTextChanges(snapshot.content, accepted.get(source) ?? []).content,
404
+ ] as const;
405
+ }),
406
+ );
407
+ const after = new Map<string, string>();
408
+ const outcome = await editor.editTexts(
409
+ sourcePaths.map((source) => ({ source, read: true })),
410
+ { cwd, signal, intent: "edit" },
411
+ (texts) => {
412
+ const changes = new Map<string, TextChange[]>();
413
+ for (const source of sourcePaths) {
414
+ if (texts.get(source) !== before.get(source)) throw stale(source);
415
+ const combined = [...(accepted.get(source) ?? []), ...(additions.get(source) ?? [])];
416
+ const snapshot = requiredValue(
417
+ [...snapshots.values()].find((item) => item.source === source),
418
+ );
419
+ after.set(source, applyTextChanges(snapshot.content, combined).content);
420
+ changes.set(source, additions.get(source) ?? []);
421
+ }
422
+ return { changes, result: undefined };
423
+ },
424
+ );
425
+ if (outcome.kind === "failed")
426
+ throw outcome.failure.cause instanceof Error
427
+ ? outcome.failure.cause
428
+ : new Error(outcome.failure.message);
429
+ const files: ScriptMutationFile[] = [];
430
+ for (const source of sourcePaths) {
431
+ const snapshot = requiredValue([...snapshots.values()].find((item) => item.source === source));
432
+ const combined = [...(accepted.get(source) ?? []), ...(additions.get(source) ?? [])];
433
+ accepted.set(source, combined);
434
+ const applied = applyTextChanges(snapshot.content, combined);
435
+ files.push({
436
+ source,
437
+ before: requiredValue(before.get(source)),
438
+ after: requiredValue(after.get(source)),
439
+ action: "edited",
440
+ changes: applied.changes.map((change, editIndex) => ({
441
+ editIndex,
442
+ fromA: change.fromBefore,
443
+ toA: change.toBefore,
444
+ fromB: change.fromAfter,
445
+ toB: change.toAfter,
446
+ removedText: change.removedText,
447
+ insertedText: change.insertedText,
448
+ })),
449
+ formatting: { status: "not-reported" },
178
450
  });
179
451
  }
452
+ return files;
453
+ }
454
+ async function editOne(
455
+ editor: TransactionEditor,
456
+ source: string,
457
+ before: string,
458
+ after: string,
459
+ cwd: string,
460
+ signal: AbortSignal,
461
+ create: boolean,
462
+ ): Promise<void> {
463
+ const outcome = await editor.editTexts(
464
+ [{ source, read: true, ...(create && { allowReadFailure: true }) }],
465
+ { cwd, signal, intent: "edit" },
466
+ (texts) => {
467
+ if ((texts.get(source) ?? "") !== before) throw stale(source);
468
+ return {
469
+ changes: new Map([[source, [{ from: 0, to: before.length, insert: after }]]]),
470
+ result: undefined,
471
+ };
472
+ },
473
+ );
474
+ if (outcome.kind === "failed")
475
+ throw outcome.failure.cause instanceof Error
476
+ ? outcome.failure.cause
477
+ : new Error(outcome.failure.message);
478
+ }
479
+
480
+ function validateSelection(
481
+ operation: TextOperation,
482
+ snapshot: EditorSnapshot | undefined,
483
+ ): asserts snapshot is EditorSnapshot {
484
+ if (snapshot === undefined) throw invalid("Selection belongs to an unknown editor snapshot");
485
+ const { from, to, text } = operation.selection;
486
+ if (
487
+ !Number.isInteger(from) ||
488
+ !Number.isInteger(to) ||
489
+ from < 0 ||
490
+ to < from ||
491
+ to > snapshot.content.length
492
+ )
493
+ throw invalid(`Selection ${from}-${to} is outside ${snapshot.source}`);
494
+ if (snapshot.content.slice(from, to) !== text)
495
+ throw invalid(`Selection text does not match ${snapshot.source}`);
496
+ }
497
+ function overlaps(change: TextChange, selection: Selection): boolean {
498
+ return selection.from < change.to && change.from < selection.to;
499
+ }
500
+ function operationResources(
501
+ operation: Operation,
502
+ snapshots: ReadonlyMap<string, EditorSnapshot>,
503
+ cwd: string,
504
+ ): string[] {
505
+ if (operation.kind === "text-copy" || operation.kind === "text-move")
506
+ return [
507
+ ...new Set(
508
+ [...operation.sources, ...operation.destinations].flatMap((selection) => {
509
+ const snapshot = snapshots.get(selection.document);
510
+ return snapshot === undefined ? [] : [path.resolve(cwd, snapshot.source)];
511
+ }),
512
+ ),
513
+ ];
514
+ if (operation.kind === "warning") {
515
+ const snapshot = snapshots.get(operation.document);
516
+ return snapshot === undefined ? [] : [path.resolve(cwd, snapshot.source)];
517
+ }
518
+ if (operation.kind === "replace") {
519
+ const snapshot = snapshots.get(operation.selection.document);
520
+ return snapshot === undefined ? [] : [path.resolve(cwd, snapshot.source)];
521
+ }
522
+ return [
523
+ path.resolve(cwd, operation.path),
524
+ ...("target" in operation && operation.target !== undefined
525
+ ? [path.resolve(cwd, operation.target)]
526
+ : []),
527
+ ];
528
+ }
529
+ function warningDetails(operation: WarningOperation): {
530
+ code: string;
531
+ message: string;
532
+ presentation?: unknown;
533
+ } {
534
+ if (operation.warning !== null && typeof operation.warning === "object") {
535
+ const value = operation.warning as Record<string, unknown>;
536
+ return {
537
+ code: typeof value.code === "string" ? value.code : "EMPTY_SELECTION",
538
+ message:
539
+ typeof value.message === "string"
540
+ ? value.message
541
+ : "SelectionSet is empty; no edit was applied.",
542
+ ...(value.presentation !== undefined && { presentation: value.presentation }),
543
+ };
544
+ }
545
+ return {
546
+ code: "EMPTY_SELECTION",
547
+ message:
548
+ operation.query === undefined
549
+ ? "SelectionSet is empty; no edit was applied."
550
+ : `SelectionSet for "${operation.query}" is empty; no edit was applied.`,
551
+ };
552
+ }
553
+ function failed(
554
+ index: number,
555
+ operation: Operation,
556
+ resources: readonly string[],
557
+ error: unknown,
558
+ ): ApplyOperationOutcome {
559
+ return {
560
+ index,
561
+ kind: operation.kind,
562
+ status: "failed",
563
+ effect: "not-applied",
564
+ resources,
565
+ error: codedError(error),
566
+ };
567
+ }
568
+ function codedError(
569
+ error: unknown,
570
+ rollbackErrors: readonly string[] = [],
571
+ ): { code: string; message: string } {
572
+ const code =
573
+ error !== null && typeof error === "object" && "code" in error
574
+ ? String(error.code)
575
+ : "OPERATION_FAILED";
576
+ const message = error instanceof Error ? error.message : String(error);
577
+ return {
578
+ code: rollbackErrors.length ? "ROLLBACK_INCOMPLETE" : code,
579
+ message: rollbackErrors.length
580
+ ? `${message}; rollback failed: ${rollbackErrors.join("; ")}`
581
+ : message,
582
+ };
180
583
  }
181
584
 
182
585
  function parseRequest(value: unknown): EditorTransactionRequest {
183
- const request = record(value, "apply() transaction");
184
- const rawSnapshots = array(request.snapshots, "snapshots");
185
- const rawOperations = array(request.operations, "operations");
186
- const snapshots = rawSnapshots.map((value, index) => {
586
+ const request = record(value, "flush() transaction");
587
+ const snapshots = array(request.snapshots, "snapshots").map((value, index) => {
187
588
  const item = record(value, `snapshot ${index + 1}`);
188
589
  return {
189
590
  id: string(item.id, "snapshot id"),
@@ -191,43 +592,66 @@ function parseRequest(value: unknown): EditorTransactionRequest {
191
592
  content: text(item.content, "snapshot content"),
192
593
  };
193
594
  });
194
- const operations = rawOperations.map(
195
- (value, index): TextOperation | CreateOperation | FileOperation => {
196
- const item = record(value, `operation ${index + 1}`);
197
- const kind = string(item.kind, "operation kind");
198
- if (kind === "replace") {
199
- const selected = record(item.selection, "selection");
200
- return {
201
- kind,
202
- selection: {
203
- document: string(selected.document, "selection document"),
204
- from: integer(selected.from, "selection from"),
205
- to: integer(selected.to, "selection to"),
206
- text: text(selected.text, "selection text"),
207
- },
208
- text: text(item.text, "replacement text"),
209
- };
210
- }
211
- if (kind === "create")
212
- return {
213
- kind,
214
- path: string(item.path, "create path"),
215
- content: text(item.content, "create content"),
216
- };
217
- if (kind === "delete") return { kind, path: string(item.path, "delete path") };
218
- if (kind === "copy" || kind === "move")
219
- return {
220
- kind,
221
- path: string(item.path, `${kind} path`),
222
- target: string(item.target, `${kind} target`),
223
- ...(item.overwrite === true && { overwrite: true }),
224
- };
225
- throw invalid(`Unknown transaction operation: ${kind}`);
226
- },
227
- );
595
+ const operations = array(request.operations, "operations").map((value, index): Operation => {
596
+ const item = record(value, `operation ${index + 1}`);
597
+ const kind = string(item.kind, "operation kind");
598
+ if (kind === "replace") {
599
+ const selected = record(item.selection, "selection");
600
+ return {
601
+ kind,
602
+ selection: {
603
+ document: string(selected.document, "selection document"),
604
+ from: integer(selected.from, "selection from"),
605
+ to: integer(selected.to, "selection to"),
606
+ text: text(selected.text, "selection text"),
607
+ ...(selected.linewise === true && { linewise: true }),
608
+ },
609
+ text: text(item.text, "replacement text"),
610
+ };
611
+ }
612
+ if (kind === "text-copy" || kind === "text-move")
613
+ return {
614
+ kind,
615
+ sources: selections(item.sources, "transfer sources"),
616
+ destinations: selections(item.destinations, "transfer destinations"),
617
+ text: text(item.text, "transfer text"),
618
+ };
619
+ if (kind === "warning")
620
+ return {
621
+ kind,
622
+ document: string(item.document, "warning document"),
623
+ ...(typeof item.query === "string" && { query: item.query }),
624
+ ...(item.warning !== undefined && { warning: item.warning }),
625
+ };
626
+ if (kind === "create")
627
+ return {
628
+ kind,
629
+ path: string(item.path, "create path"),
630
+ content: text(item.content, "create content"),
631
+ };
632
+ if (kind === "delete") return { kind, path: string(item.path, "delete path") };
633
+ if (kind === "copy" || kind === "move")
634
+ return {
635
+ kind,
636
+ path: string(item.path, `${kind} path`),
637
+ target: string(item.target, `${kind} target`),
638
+ ...(item.overwrite === true && { overwrite: true }),
639
+ };
640
+ throw invalid(`Unknown transaction operation: ${kind}`);
641
+ });
228
642
  return { snapshots, operations };
229
643
  }
230
-
644
+ function selections(value: unknown, label: string): Selection[] {
645
+ return array(value, label).map((entry, index) => {
646
+ const item = record(entry, `${label} ${index + 1}`);
647
+ return {
648
+ document: string(item.document, "selection document"),
649
+ from: integer(item.from, "selection from"),
650
+ to: integer(item.to, "selection to"),
651
+ text: text(item.text, "selection text"),
652
+ };
653
+ });
654
+ }
231
655
  function record(value: unknown, label: string): Record<string, unknown> {
232
656
  if (value === null || typeof value !== "object" || Array.isArray(value))
233
657
  throw invalid(`${label} must be an object`);
@@ -250,82 +674,6 @@ function integer(value: unknown, label: string): number {
250
674
  if (!Number.isInteger(value)) throw invalid(`${label} must be an integer`);
251
675
  return value as number;
252
676
  }
253
-
254
- function validateTextChanges(changes: ReadonlyMap<string, readonly TextChange[]>): void {
255
- for (const [source, items] of changes) {
256
- const ordered = [...items].sort((a, b) => a.from - b.from || a.to - b.to);
257
- for (let index = 1; index < ordered.length; index += 1) {
258
- const previous = ordered[index - 1];
259
- const current = ordered[index];
260
- if (previous !== undefined && current !== undefined && current.from < previous.to)
261
- throw invalid(`Overlapping selections in ${source}`);
262
- }
263
- }
264
- }
265
-
266
- function touchedPaths(
267
- cwd: string,
268
- changes: ReadonlyMap<string, unknown>,
269
- creates: readonly CreateOperation[],
270
- operations: readonly FileOperation[],
271
- ): Set<string> {
272
- const result = new Set([...changes.keys()].map((source) => path.resolve(cwd, source)));
273
- for (const item of creates) result.add(path.resolve(cwd, item.path));
274
- for (const item of operations) {
275
- result.add(path.resolve(cwd, item.path));
276
- if (item.target !== undefined) result.add(path.resolve(cwd, item.target));
277
- }
278
- return result;
279
- }
280
-
281
- async function preflight(
282
- touched: ReadonlySet<string>,
283
- snapshots: ReadonlyMap<string, EditorSnapshot>,
284
- changes: ReadonlyMap<string, unknown>,
285
- creates: readonly CreateOperation[],
286
- operations: readonly FileOperation[],
287
- cwd: string,
288
- ): Promise<void> {
289
- for (const file of touched) {
290
- const stat = await optionalStat(file);
291
- if (stat !== undefined && (!stat.isFile() || stat.isSymbolicLink()))
292
- throw invalid(`${file} is not a regular file`);
293
- }
294
- for (const snapshot of snapshots.values()) {
295
- if (!changes.has(snapshot.source)) continue;
296
- let current: string;
297
- try {
298
- current = await readFile(snapshot.source, "utf8");
299
- } catch {
300
- throw stale(snapshot.source);
301
- }
302
- if (current !== snapshot.content) throw stale(snapshot.source);
303
- }
304
- const createPaths = creates.map((item) => path.resolve(cwd, item.path));
305
- if (new Set(createPaths).size !== createPaths.length)
306
- throw invalid("A transaction cannot create the same path more than once");
307
- for (const item of creates)
308
- if ((await optionalStat(path.resolve(cwd, item.path))) !== undefined)
309
- throw invalid(`Create target exists: ${item.path}`);
310
- const existence = new Map<string, boolean>();
311
- for (const file of touched) existence.set(file, (await optionalStat(file)) !== undefined);
312
- for (const item of creates) existence.set(path.resolve(cwd, item.path), true);
313
- for (const item of operations) {
314
- const source = path.resolve(cwd, item.path);
315
- if (existence.get(source) !== true) throw invalid(`Source does not exist: ${item.path}`);
316
- if (item.kind === "delete") {
317
- existence.set(source, false);
318
- continue;
319
- }
320
- const target = path.resolve(cwd, item.target ?? "");
321
- if (source === target) throw invalid("Source and target are the same file");
322
- if (existence.get(target) === true && item.overwrite !== true)
323
- throw invalid(`Target exists: ${item.target}`);
324
- existence.set(target, true);
325
- if (item.kind === "move") existence.set(source, false);
326
- }
327
- }
328
-
329
677
  async function performFileOperation(operation: FileOperation, cwd: string): Promise<void> {
330
678
  const source = path.resolve(cwd, operation.path);
331
679
  if (operation.kind === "delete") {
@@ -338,7 +686,6 @@ async function performFileOperation(operation: FileOperation, cwd: string): Prom
338
686
  await fs.copy(source, target, { overwrite: operation.overwrite === true, errorOnExist: true });
339
687
  else await fs.move(source, target, { overwrite: operation.overwrite === true });
340
688
  }
341
-
342
689
  async function savePath(file: string): Promise<SavedPath> {
343
690
  try {
344
691
  return { path: file, existed: true, bytes: await readFile(file) };
@@ -347,23 +694,26 @@ async function savePath(file: string): Promise<SavedPath> {
347
694
  throw error;
348
695
  }
349
696
  }
350
-
351
- async function rollback(saved: readonly SavedPath[]): Promise<readonly string[]> {
697
+ async function rollback(
698
+ saved: readonly SavedPath[],
699
+ restorePath: (state: SavedPath) => Promise<void> = restoreSavedPath,
700
+ ): Promise<readonly string[]> {
352
701
  const errors: string[] = [];
353
- for (const item of [...saved].reverse()) {
702
+ for (const item of [...saved].reverse())
354
703
  try {
355
- if (item.existed) {
356
- await mkdir(path.dirname(item.path), { recursive: true });
357
- await writeFile(item.path, item.bytes ?? new Uint8Array());
358
- } else await rm(item.path, { force: true });
704
+ await restorePath(item);
359
705
  } catch (error) {
360
706
  if (!item.existed && (isCode(error, "ENOENT") || isCode(error, "ENOTDIR"))) continue;
361
707
  errors.push(`${item.path}: ${error instanceof Error ? error.message : String(error)}`);
362
708
  }
363
- }
364
709
  return errors;
365
710
  }
366
-
711
+ async function restoreSavedPath(item: SavedPath): Promise<void> {
712
+ if (item.existed) {
713
+ await mkdir(path.dirname(item.path), { recursive: true });
714
+ await writeFile(item.path, item.bytes ?? new Uint8Array());
715
+ } else await rm(item.path, { force: true });
716
+ }
367
717
  async function optionalStat(file: string) {
368
718
  try {
369
719
  return await lstat(file);
@@ -372,16 +722,14 @@ async function optionalStat(file: string) {
372
722
  throw error;
373
723
  }
374
724
  }
375
-
376
725
  function isCode(error: unknown, code: string): boolean {
377
726
  return error !== null && typeof error === "object" && "code" in error && error.code === code;
378
727
  }
379
-
380
728
  function invalid(message: string): Error {
381
729
  return Object.assign(new Error(message), { code: "INVALID_TRANSACTION" });
382
730
  }
383
731
  function stale(source: string): Error {
384
- return Object.assign(new Error(`Snapshot changed before apply(): ${source}`), {
732
+ return Object.assign(new Error(`Snapshot changed before flush(): ${source}`), {
385
733
  code: "STALE_SNAPSHOT",
386
734
  });
387
735
  }