pi-agent-ide 0.1.0 → 0.2.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 (841) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +38 -10
  3. package/dist/chunk-266RH77Z.js +1 -0
  4. package/dist/chunk-2SKAED2I.js +2 -0
  5. package/dist/chunk-2ZMYT6ZC.js +1 -0
  6. package/dist/chunk-34ZQTTU2.js +2 -0
  7. package/dist/chunk-3H2EU7VQ.js +0 -0
  8. package/dist/chunk-3YK4XMWB.js +0 -0
  9. package/dist/chunk-476MR2QB.js +1 -0
  10. package/dist/chunk-4KQOODGE.js +13 -0
  11. package/dist/chunk-4SQQKEVD.js +16 -0
  12. package/dist/chunk-52BM6J6I.js +6 -0
  13. package/dist/chunk-5A25RATC.js +3 -0
  14. package/dist/chunk-5BWUVAR4.js +1 -0
  15. package/dist/chunk-6DN7NO7L.js +1 -0
  16. package/dist/chunk-6SMFVLTY.js +1 -0
  17. package/dist/chunk-7NDBZ4WX.js +1 -0
  18. package/dist/chunk-AP7Z3VWC.js +1 -0
  19. package/dist/chunk-BYWAG6X3.js +6 -0
  20. package/dist/chunk-DEP55R35.js +1 -0
  21. package/dist/chunk-E2RESHYR.js +1 -0
  22. package/dist/chunk-E4SECPF4.js +1 -0
  23. package/dist/chunk-EEH4WYI7.js +1 -0
  24. package/dist/chunk-EI7MMDWY.js +1 -0
  25. package/dist/chunk-ERVMDQGX.js +1 -0
  26. package/dist/chunk-EW2KVKCN.js +2 -0
  27. package/dist/chunk-F6SESI3P.js +4 -0
  28. package/dist/chunk-GTK5AREE.js +2 -0
  29. package/dist/chunk-HOBTFFQR.js +1 -0
  30. package/dist/chunk-IMVCVPMP.js +3 -0
  31. package/dist/chunk-IOCABSRL.js +1 -0
  32. package/dist/chunk-JASLOB2X.js +1 -0
  33. package/dist/chunk-JMVDFOTQ.js +1 -0
  34. package/dist/chunk-K4ZMRDEI.js +0 -0
  35. package/dist/chunk-KINKBPPX.js +1 -0
  36. package/dist/chunk-KNMWCDDK.js +7 -0
  37. package/dist/chunk-KP37XTDI.js +1 -0
  38. package/dist/chunk-KYVOMBO5.js +1 -0
  39. package/dist/chunk-KZBAJFCV.js +1 -0
  40. package/dist/chunk-LFV6G5Z3.js +1 -0
  41. package/dist/chunk-MB6VVVAF.js +2 -0
  42. package/dist/chunk-MBUXFKIW.js +3 -0
  43. package/dist/chunk-MTTKFJV6.js +1 -0
  44. package/dist/chunk-NI2NK4JU.js +1 -0
  45. package/dist/chunk-NNCZYO4Q.js +0 -0
  46. package/dist/chunk-OBWZ5RYN.js +1 -0
  47. package/dist/chunk-OIL6BB2S.js +33 -0
  48. package/dist/chunk-ORAPGW7W.js +1 -0
  49. package/dist/chunk-OVUYPHDE.js +1 -0
  50. package/dist/chunk-PETQCA7G.js +1 -0
  51. package/dist/chunk-PODWLGNO.js +6 -0
  52. package/dist/chunk-QNZVPFSP.js +3 -0
  53. package/dist/chunk-QW5O42QJ.js +2 -0
  54. package/dist/chunk-SJYDLHEF.js +108 -0
  55. package/dist/chunk-SSZB66C3.js +3 -0
  56. package/dist/chunk-STUZZM5B.js +1 -0
  57. package/dist/chunk-T2RPMEM2.js +1 -0
  58. package/dist/chunk-TG6H6EOI.js +1 -0
  59. package/dist/chunk-TNWYEXFG.js +1 -0
  60. package/dist/chunk-U4D6FT66.js +1 -0
  61. package/dist/chunk-VCCMVQXU.js +1 -0
  62. package/dist/chunk-VFBLX4TG.js +1 -0
  63. package/dist/chunk-VSP2QQPQ.js +2 -0
  64. package/dist/chunk-VUNCBASK.js +10 -0
  65. package/dist/chunk-VWVLBOMU.js +8 -0
  66. package/dist/chunk-W3XYKJ5H.js +1 -0
  67. package/dist/chunk-XGUAL3CA.js +1 -0
  68. package/dist/chunk-XNXQYPVT.js +10 -0
  69. package/dist/chunk-YHUTTZC6.js +2 -0
  70. package/dist/chunk-YPHXLAQF.js +1 -0
  71. package/dist/chunk-YQTLJ5ZV.js +58 -0
  72. package/dist/chunk-ZC65Z3CK.js +1 -0
  73. package/dist/entry-0.js +1 -0
  74. package/dist/entry-1.js +1 -0
  75. package/dist/entry-10.js +1 -0
  76. package/dist/entry-11.js +1 -0
  77. package/dist/entry-12.js +1 -0
  78. package/dist/entry-13.js +1 -0
  79. package/dist/entry-14.js +1 -0
  80. package/dist/entry-16.js +1 -0
  81. package/dist/entry-17.js +1 -0
  82. package/dist/entry-18.js +1 -0
  83. package/dist/entry-19.js +1 -0
  84. package/dist/entry-2.js +1 -0
  85. package/dist/entry-20.js +1 -0
  86. package/dist/entry-21.js +1 -0
  87. package/dist/entry-22.js +1 -0
  88. package/dist/entry-23.js +1 -0
  89. package/dist/entry-24.js +1 -0
  90. package/dist/entry-25.js +1 -0
  91. package/dist/entry-26.js +1 -0
  92. package/dist/entry-27.js +1 -0
  93. package/dist/entry-28.js +1 -0
  94. package/dist/entry-29.js +1 -0
  95. package/dist/entry-3.js +1 -0
  96. package/dist/entry-30.js +1 -0
  97. package/dist/entry-31.js +1 -0
  98. package/dist/entry-32.js +1 -0
  99. package/dist/entry-33.js +1 -0
  100. package/dist/entry-34.js +1 -0
  101. package/dist/entry-35.js +1 -0
  102. package/dist/entry-36.js +1 -0
  103. package/dist/entry-37.js +1 -0
  104. package/dist/entry-38.js +1 -0
  105. package/dist/entry-39.js +1 -0
  106. package/dist/entry-4.js +1 -0
  107. package/dist/entry-40.js +1 -0
  108. package/dist/entry-41.js +1 -0
  109. package/dist/entry-42.js +1 -0
  110. package/dist/entry-43.js +1 -0
  111. package/dist/entry-44.js +1 -0
  112. package/dist/entry-45.js +1 -0
  113. package/dist/entry-46.js +1 -0
  114. package/dist/entry-47.js +1 -0
  115. package/dist/entry-48.js +1 -0
  116. package/dist/entry-49.js +1 -0
  117. package/dist/entry-5.js +1 -0
  118. package/dist/entry-50.js +1 -0
  119. package/dist/entry-51.js +1 -0
  120. package/dist/entry-52.js +1 -0
  121. package/dist/entry-53.js +0 -0
  122. package/dist/entry-54.js +1 -0
  123. package/dist/entry-55.js +0 -0
  124. package/dist/entry-56.js +1 -0
  125. package/dist/entry-57.js +1 -0
  126. package/dist/entry-58.js +1 -0
  127. package/dist/entry-59.js +1 -0
  128. package/dist/entry-6.js +1 -0
  129. package/dist/entry-60.js +1 -0
  130. package/dist/entry-61.js +1 -0
  131. package/dist/entry-62.js +0 -0
  132. package/dist/entry-63.js +1 -0
  133. package/dist/entry-64.js +0 -0
  134. package/dist/entry-65.js +1 -0
  135. package/dist/entry-66.js +1 -0
  136. package/dist/entry-67.js +1 -0
  137. package/dist/entry-68.js +1 -0
  138. package/dist/entry-69.js +1 -0
  139. package/dist/entry-7.js +1 -0
  140. package/dist/entry-70.js +1 -0
  141. package/dist/entry-71.js +1 -0
  142. package/dist/entry-72.js +1 -0
  143. package/dist/entry-73.js +1 -0
  144. package/dist/entry-74.js +1 -0
  145. package/dist/entry-75.js +1 -0
  146. package/dist/entry-76.js +1 -0
  147. package/dist/entry-77.js +1 -0
  148. package/dist/entry-78.js +1 -0
  149. package/dist/entry-79.js +1 -0
  150. package/dist/entry-8.js +1 -0
  151. package/dist/entry-9.js +1 -0
  152. package/dist/pi-agent-ide.js +13 -0
  153. package/docs/architecture.md +30 -14
  154. package/docs/configuration.md +180 -53
  155. package/docs/development.md +116 -4
  156. package/docs/extensions.md +196 -154
  157. package/docs/generated/tool-catalog/c.md +9 -0
  158. package/docs/generated/tool-catalog/cmake.md +9 -0
  159. package/docs/generated/tool-catalog/cpp.md +9 -0
  160. package/docs/generated/tool-catalog/csharp.md +9 -0
  161. package/docs/generated/tool-catalog/css.md +11 -0
  162. package/docs/generated/tool-catalog/dockerfile.md +9 -0
  163. package/docs/generated/tool-catalog/go.md +10 -0
  164. package/docs/generated/tool-catalog/html.md +10 -0
  165. package/docs/generated/tool-catalog/index.md +30 -0
  166. package/docs/generated/tool-catalog/java.md +9 -0
  167. package/docs/generated/tool-catalog/javascript.md +12 -0
  168. package/docs/generated/tool-catalog/json.md +11 -0
  169. package/docs/generated/tool-catalog/kotlin.md +9 -0
  170. package/docs/generated/tool-catalog/lua.md +9 -0
  171. package/docs/generated/tool-catalog/markdown.md +10 -0
  172. package/docs/generated/tool-catalog/php.md +9 -0
  173. package/docs/generated/tool-catalog/python.md +10 -0
  174. package/docs/generated/tool-catalog/ruby.md +9 -0
  175. package/docs/generated/tool-catalog/rust.md +9 -0
  176. package/docs/generated/tool-catalog/shell.md +9 -0
  177. package/docs/generated/tool-catalog/sql.md +9 -0
  178. package/docs/generated/tool-catalog/swift.md +9 -0
  179. package/docs/generated/tool-catalog/terraform.md +9 -0
  180. package/docs/generated/tool-catalog/toml.md +10 -0
  181. package/docs/generated/tool-catalog/typescript.md +12 -0
  182. package/docs/generated/tool-catalog/xml.md +9 -0
  183. package/docs/generated/tool-catalog/yaml.md +10 -0
  184. package/docs/tools.md +88 -6
  185. package/node_modules/pi-agent-doctor/package.json +29 -0
  186. package/node_modules/pi-agent-doctor/runtime/entry-16.js +1 -0
  187. package/node_modules/pi-agent-doctor/runtime/entry-17.js +1 -0
  188. package/node_modules/pi-agent-doctor/runtime/entry-18.js +1 -0
  189. package/node_modules/pi-agent-doctor/runtime/entry-19.js +1 -0
  190. package/node_modules/pi-agent-doctor/src/catalog.ts +55 -0
  191. package/node_modules/pi-agent-doctor/src/connect-plugin.ts +53 -0
  192. package/node_modules/pi-agent-doctor/src/executable.ts +80 -0
  193. package/node_modules/pi-agent-doctor/src/plugin-protocol.ts +147 -0
  194. package/node_modules/pi-agent-filesystem/index.ts +43 -44
  195. package/node_modules/pi-agent-filesystem/package.json +11 -11
  196. package/node_modules/pi-agent-filesystem/runtime/entry-20.js +2 -0
  197. package/node_modules/pi-agent-filesystem/src/resolver.ts +112 -135
  198. package/node_modules/pi-agent-filesystem-image/index.ts +5 -6
  199. package/node_modules/pi-agent-filesystem-image/package.json +8 -8
  200. package/node_modules/pi-agent-filesystem-image/runtime/entry-21.js +2 -0
  201. package/node_modules/pi-agent-filesystem-pdf/index.ts +5 -6
  202. package/node_modules/pi-agent-filesystem-pdf/package.json +6 -6
  203. package/node_modules/pi-agent-filesystem-pdf/runtime/entry-22.js +2 -0
  204. package/node_modules/pi-agent-filesystem-text/index.ts +11 -12
  205. package/node_modules/pi-agent-filesystem-text/package.json +8 -8
  206. package/node_modules/pi-agent-filesystem-text/runtime/entry-23.js +2 -0
  207. package/node_modules/pi-agent-ide-ast/index.ts +1 -1
  208. package/node_modules/pi-agent-ide-ast/package.json +15 -13
  209. package/node_modules/pi-agent-ide-ast/runtime/entry-24.js +1 -0
  210. package/node_modules/pi-agent-ide-ast/runtime/entry-25.js +2 -0
  211. package/node_modules/pi-agent-ide-ast/src/api/scope.ts +1 -1
  212. package/node_modules/pi-agent-ide-ast/src/ast/collect-files.ts +11 -16
  213. package/node_modules/pi-agent-ide-ast/src/ast/manager.ts +292 -322
  214. package/node_modules/pi-agent-ide-ast/src/ast/outline.ts +591 -656
  215. package/node_modules/pi-agent-ide-ast/src/ast/read-text.ts +15 -19
  216. package/node_modules/pi-agent-ide-ast/src/doctor-plugin.ts +130 -0
  217. package/node_modules/pi-agent-ide-ast/src/extension.ts +75 -64
  218. package/node_modules/pi-agent-ide-ast/src/outline-resolver.ts +51 -54
  219. package/node_modules/pi-agent-ide-ast/src/scope-handler.ts +168 -185
  220. package/node_modules/pi-agent-ide-ast/src/scope-resolver.ts +36 -36
  221. package/node_modules/pi-agent-ide-ast/src/search-resolver.ts +118 -129
  222. package/node_modules/pi-agent-ide-changes/index.ts +57 -51
  223. package/node_modules/pi-agent-ide-changes/package.json +14 -12
  224. package/node_modules/pi-agent-ide-changes/runtime/entry-26.js +1 -0
  225. package/node_modules/pi-agent-ide-changes/runtime/entry-27.js +2 -0
  226. package/node_modules/pi-agent-ide-changes/src/change-anchor.ts +74 -78
  227. package/node_modules/pi-agent-ide-changes/src/changes/change-groups.ts +230 -245
  228. package/node_modules/pi-agent-ide-changes/src/changes/change-service.ts +189 -192
  229. package/node_modules/pi-agent-ide-changes/src/changes/change-types.ts +75 -79
  230. package/node_modules/pi-agent-ide-changes/src/changes/git-changes-backend.ts +259 -276
  231. package/node_modules/pi-agent-ide-changes/src/current-change-presenter.ts +164 -190
  232. package/node_modules/pi-agent-ide-changes/src/doctor-plugin.ts +59 -0
  233. package/node_modules/pi-agent-ide-changes/src/index-mutation-queue.ts +10 -9
  234. package/node_modules/pi-agent-ide-changes/src/last-text-transaction-store.ts +49 -62
  235. package/node_modules/pi-agent-ide-changes/src/tool-index-change.ts +98 -93
  236. package/node_modules/pi-agent-ide-changes/src/tool-text-undo.ts +131 -126
  237. package/node_modules/pi-agent-ide-formatter/index.ts +22 -23
  238. package/node_modules/pi-agent-ide-formatter/package.json +6 -3
  239. package/node_modules/pi-agent-ide-formatter/runtime/entry-28.js +2 -0
  240. package/node_modules/pi-agent-ide-formatter/src/catalog.ts +281 -0
  241. package/node_modules/pi-agent-ide-formatter/src/doctor-plugin.ts +156 -0
  242. package/node_modules/pi-agent-ide-formatter/src/formatter.ts +38 -40
  243. package/node_modules/pi-agent-ide-formatter/src/registry.ts +94 -27
  244. package/node_modules/pi-agent-ide-languages/index.ts +24 -0
  245. package/{src/extensions/pi-agent-search/plugins/pi-agent-search-semantic → node_modules/pi-agent-ide-languages}/package.json +6 -7
  246. package/node_modules/pi-agent-ide-languages/runtime/entry-29.js +2 -0
  247. package/node_modules/pi-agent-ide-languages/src/languages.ts +84 -0
  248. package/node_modules/pi-agent-ide-lint/index.ts +66 -225
  249. package/node_modules/pi-agent-ide-lint/package.json +10 -8
  250. package/node_modules/pi-agent-ide-lint/runtime/entry-30.js +2 -0
  251. package/node_modules/pi-agent-ide-lint/src/catalog.ts +295 -0
  252. package/node_modules/pi-agent-ide-lint/src/command-linter.ts +98 -144
  253. package/node_modules/pi-agent-ide-lint/src/diagnostics.ts +182 -0
  254. package/node_modules/pi-agent-ide-lint/src/doctor-plugin.ts +155 -0
  255. package/node_modules/pi-agent-ide-lint/src/registry.ts +94 -27
  256. package/node_modules/pi-agent-ide-lsp/index.ts +111 -231
  257. package/node_modules/pi-agent-ide-lsp/package.json +13 -11
  258. package/node_modules/pi-agent-ide-lsp/runtime/entry-31.js +2 -0
  259. package/node_modules/pi-agent-ide-lsp/src/catalog.ts +248 -0
  260. package/node_modules/pi-agent-ide-lsp/src/code-view-resolvers.ts +109 -116
  261. package/node_modules/pi-agent-ide-lsp/src/diagnostic-source.ts +113 -0
  262. package/node_modules/pi-agent-ide-lsp/src/doctor-plugin.ts +163 -0
  263. package/node_modules/pi-agent-ide-lsp/src/lsp/client.ts +439 -396
  264. package/node_modules/pi-agent-ide-lsp/src/lsp/code-views.ts +421 -458
  265. package/node_modules/pi-agent-ide-lsp/src/lsp/diagnostic-adapters.ts +76 -0
  266. package/node_modules/pi-agent-ide-lsp/src/lsp/diagnostics.ts +143 -159
  267. package/node_modules/pi-agent-ide-lsp/src/lsp/index.ts +25 -20
  268. package/node_modules/pi-agent-ide-lsp/src/lsp/language-map.ts +31 -33
  269. package/node_modules/pi-agent-ide-lsp/src/lsp/lsp-compiler.ts +49 -57
  270. package/node_modules/pi-agent-ide-lsp/src/lsp/manager.ts +350 -415
  271. package/node_modules/pi-agent-ide-lsp/src/lsp/navigation.ts +58 -63
  272. package/node_modules/pi-agent-ide-lsp/src/lsp/registry.ts +196 -77
  273. package/node_modules/pi-agent-ide-lsp/src/lsp/rename.ts +36 -36
  274. package/node_modules/pi-agent-ide-lsp/src/lsp/symbol-search.ts +118 -127
  275. package/node_modules/pi-agent-ide-lsp/src/lsp/symbols.ts +108 -107
  276. package/node_modules/pi-agent-ide-lsp/src/lsp/types.ts +76 -57
  277. package/node_modules/pi-agent-ide-lsp/src/search-resolver.ts +36 -32
  278. package/node_modules/pi-agent-image/package.json +3 -3
  279. package/node_modules/pi-agent-image/runtime/entry-32.js +1 -0
  280. package/node_modules/pi-agent-image/src/image-converter.ts +210 -249
  281. package/node_modules/pi-agent-image/src/index.ts +4 -4
  282. package/node_modules/pi-agent-invariant/package.json +12 -0
  283. package/node_modules/pi-agent-invariant/runtime/entry-33.js +1 -0
  284. package/node_modules/pi-agent-invariant/src/index.ts +8 -0
  285. package/node_modules/pi-agent-pdf/package.json +3 -3
  286. package/node_modules/pi-agent-pdf/runtime/entry-34.js +1 -0
  287. package/node_modules/pi-agent-pdf/src/index.ts +1 -5
  288. package/node_modules/pi-agent-pdf/src/pdf-converter.ts +133 -155
  289. package/node_modules/pi-agent-read/index.ts +1 -1
  290. package/node_modules/pi-agent-read/package.json +15 -13
  291. package/node_modules/pi-agent-read/runtime/entry-35.js +1 -0
  292. package/node_modules/pi-agent-read/runtime/entry-36.js +1 -0
  293. package/node_modules/pi-agent-read/runtime/entry-37.js +1 -0
  294. package/node_modules/pi-agent-read/runtime/entry-38.js +1 -0
  295. package/node_modules/pi-agent-read/runtime/entry-39.js +2 -0
  296. package/node_modules/pi-agent-read/src/api/connect-plugin.ts +38 -51
  297. package/node_modules/pi-agent-read/src/api/plugin-protocol.ts +27 -33
  298. package/node_modules/pi-agent-read/src/api/rendering.ts +3 -4
  299. package/node_modules/pi-agent-read/src/api/tools/read.ts +237 -134
  300. package/node_modules/pi-agent-read/src/core/extension.ts +51 -33
  301. package/node_modules/pi-agent-read/src/core/read-core.ts +353 -330
  302. package/node_modules/pi-agent-read/src/core/tools/read/output-truncation.ts +169 -156
  303. package/node_modules/pi-agent-read/src/core/tools/read/persisted-result.ts +72 -0
  304. package/node_modules/pi-agent-read/src/core/tools/read/read-renderer.ts +381 -313
  305. package/node_modules/pi-agent-read/src/core/tools/read/read-result.ts +169 -154
  306. package/node_modules/pi-agent-read/src/core/tools/read/temp-resource-store.ts +165 -187
  307. package/node_modules/pi-agent-read/src/core/tools/read/views.ts +29 -0
  308. package/node_modules/pi-agent-read/src/core/tools/tool-read.ts +1117 -680
  309. package/node_modules/pi-agent-resource/package.json +5 -2
  310. package/node_modules/pi-agent-resource/runtime/entry-40.js +1 -0
  311. package/node_modules/pi-agent-resource/src/content-conversion/connect-plugin.ts +50 -62
  312. package/node_modules/pi-agent-resource/src/content-conversion/content-converter.ts +28 -34
  313. package/node_modules/pi-agent-resource/src/content-conversion/content-description.ts +16 -20
  314. package/node_modules/pi-agent-resource/src/content-conversion/content-host.ts +53 -58
  315. package/node_modules/pi-agent-resource/src/content-conversion/content-runner.ts +116 -135
  316. package/node_modules/pi-agent-resource/src/content-conversion/plugin-protocol.ts +38 -39
  317. package/node_modules/pi-agent-resource/src/content-conversion/validation.ts +118 -108
  318. package/node_modules/pi-agent-resource/src/content.ts +5 -9
  319. package/node_modules/pi-agent-resource/src/index.ts +51 -40
  320. package/node_modules/pi-agent-resource/src/resolver.ts +11 -13
  321. package/node_modules/pi-agent-resource/src/resource.ts +17 -24
  322. package/node_modules/pi-agent-resource/src/validation.ts +106 -92
  323. package/node_modules/pi-agent-search/package.json +11 -10
  324. package/node_modules/pi-agent-search/runtime/entry-41.js +1 -0
  325. package/node_modules/pi-agent-search/runtime/entry-42.js +1 -0
  326. package/node_modules/pi-agent-search/runtime/entry-43.js +1 -0
  327. package/node_modules/pi-agent-search/runtime/entry-44.js +2 -0
  328. package/node_modules/pi-agent-search/src/api/connect-plugin.ts +37 -47
  329. package/node_modules/pi-agent-search/src/api/plugin-protocol.ts +26 -30
  330. package/node_modules/pi-agent-search/src/api/search.ts +65 -67
  331. package/node_modules/pi-agent-search/src/core/extension.ts +184 -92
  332. package/node_modules/pi-agent-search/src/core/search-config.ts +98 -0
  333. package/node_modules/pi-agent-search/src/core/search-core.ts +332 -357
  334. package/node_modules/pi-agent-search/src/core/search-timeout.ts +54 -0
  335. package/node_modules/pi-agent-search-text/index.ts +90 -38
  336. package/node_modules/pi-agent-search-text/package.json +13 -12
  337. package/node_modules/pi-agent-search-text/runtime/entry-45.js +2 -0
  338. package/node_modules/pi-agent-search-text/runtime/entry-46.js +1 -0
  339. package/node_modules/pi-agent-search-text/src/doctor-plugin.ts +48 -0
  340. package/node_modules/pi-agent-search-text/src/file-search.ts +90 -86
  341. package/node_modules/pi-agent-search-text/src/persisted-result.ts +56 -0
  342. package/node_modules/pi-agent-search-text/src/resolvers.ts +158 -161
  343. package/node_modules/pi-agent-search-text/src/ripgrep.ts +16 -0
  344. package/node_modules/pi-agent-search-text/src/search-backend.ts +215 -249
  345. package/node_modules/pi-agent-search-text/src/search-query.ts +262 -102
  346. package/node_modules/pi-agent-search-text/src/search-recipe.ts +68 -0
  347. package/node_modules/pi-agent-search-text/src/search-renderer.ts +275 -345
  348. package/node_modules/pi-agent-search-text/src/search-result.ts +152 -157
  349. package/node_modules/pi-agent-search-text/src/search-session.ts +438 -278
  350. package/node_modules/pi-agent-text/package.json +6 -3
  351. package/node_modules/pi-agent-text/runtime/entry-47.js +1 -0
  352. package/node_modules/pi-agent-text/src/index.ts +53 -22
  353. package/node_modules/pi-agent-text/src/presented-text.ts +15 -17
  354. package/node_modules/pi-agent-text/src/source-line.ts +33 -36
  355. package/node_modules/pi-agent-text/src/stored-text.ts +14 -0
  356. package/node_modules/pi-agent-text/src/text-anchor.ts +264 -117
  357. package/node_modules/pi-agent-text/src/text-converter.ts +71 -84
  358. package/node_modules/pi-agent-text/src/text-document.ts +120 -110
  359. package/node_modules/pi-agent-text/src/text-target.ts +105 -0
  360. package/node_modules/pi-agent-text-anchor-constant/index.ts +18 -20
  361. package/node_modules/pi-agent-text-anchor-constant/package.json +9 -6
  362. package/node_modules/pi-agent-text-anchor-constant/runtime/entry-48.js +2 -0
  363. package/node_modules/pi-agent-text-anchor-constant/src/resolver.ts +35 -36
  364. package/node_modules/pi-agent-text-anchor-exact/index.ts +34 -0
  365. package/node_modules/pi-agent-text-anchor-exact/package.json +27 -0
  366. package/node_modules/pi-agent-text-anchor-exact/runtime/entry-49.js +1 -0
  367. package/node_modules/pi-agent-text-anchor-exact/runtime/entry-50.js +2 -0
  368. package/node_modules/pi-agent-text-anchor-exact/src/anchor.ts +230 -0
  369. package/node_modules/pi-agent-text-anchor-exact/src/config.ts +159 -0
  370. package/node_modules/pi-agent-text-anchor-exact/src/fuzzy-worker.ts +100 -0
  371. package/node_modules/pi-agent-text-anchor-exact/src/recovery.ts +151 -0
  372. package/node_modules/pi-agent-text-anchor-line-hash/index.ts +40 -36
  373. package/node_modules/pi-agent-text-anchor-line-hash/package.json +16 -12
  374. package/node_modules/pi-agent-text-anchor-line-hash/runtime/entry-51.js +1 -0
  375. package/node_modules/pi-agent-text-anchor-line-hash/runtime/entry-52.js +2 -0
  376. package/node_modules/pi-agent-text-anchor-line-hash/src/anchor.ts +105 -84
  377. package/node_modules/pi-agent-text-anchor-line-hash/src/read-handler.ts +43 -42
  378. package/node_modules/pi-agent-text-editor/index.ts +7 -9
  379. package/node_modules/pi-agent-text-editor/package.json +26 -25
  380. package/node_modules/pi-agent-text-editor/runtime/entry-53.js +1 -0
  381. package/node_modules/pi-agent-text-editor/runtime/entry-54.js +1 -0
  382. package/node_modules/pi-agent-text-editor/runtime/entry-55.js +1 -0
  383. package/node_modules/pi-agent-text-editor/runtime/entry-56.js +1 -0
  384. package/node_modules/pi-agent-text-editor/runtime/entry-57.js +1 -0
  385. package/node_modules/pi-agent-text-editor/runtime/entry-58.js +1 -0
  386. package/node_modules/pi-agent-text-editor/runtime/entry-59.js +1 -0
  387. package/node_modules/pi-agent-text-editor/runtime/entry-60.js +1 -0
  388. package/node_modules/pi-agent-text-editor/runtime/entry-61.js +1 -0
  389. package/node_modules/pi-agent-text-editor/runtime/entry-62.js +1 -0
  390. package/node_modules/pi-agent-text-editor/runtime/entry-63.js +1 -0
  391. package/node_modules/pi-agent-text-editor/runtime/entry-64.js +1 -0
  392. package/node_modules/pi-agent-text-editor/runtime/entry-65.js +1 -0
  393. package/node_modules/pi-agent-text-editor/runtime/entry-66.js +1 -0
  394. package/node_modules/pi-agent-text-editor/runtime/entry-67.js +1 -0
  395. package/node_modules/pi-agent-text-editor/runtime/entry-68.js +2 -0
  396. package/node_modules/pi-agent-text-editor/src/api/anchor-inspection.ts +18 -18
  397. package/node_modules/pi-agent-text-editor/src/api/connect-plugin.ts +40 -50
  398. package/node_modules/pi-agent-text-editor/src/api/edit-completion.ts +10 -13
  399. package/node_modules/pi-agent-text-editor/src/api/edit-pipeline.ts +31 -36
  400. package/node_modules/pi-agent-text-editor/src/api/last-resolved-resource.ts +25 -27
  401. package/node_modules/pi-agent-text-editor/src/api/mutation-guard.ts +26 -31
  402. package/node_modules/pi-agent-text-editor/src/api/mutation-preview.ts +18 -21
  403. package/node_modules/pi-agent-text-editor/src/api/mutation-result.ts +20 -19
  404. package/node_modules/pi-agent-text-editor/src/api/mutation-tool.ts +154 -131
  405. package/node_modules/pi-agent-text-editor/src/api/plugin-protocol.ts +173 -144
  406. package/node_modules/pi-agent-text-editor/src/api/post-edit.ts +93 -103
  407. package/node_modules/pi-agent-text-editor/src/api/stale-anchor-message.ts +17 -14
  408. package/node_modules/pi-agent-text-editor/src/api/text-selection-anchor.ts +85 -99
  409. package/node_modules/pi-agent-text-editor/src/api/tool-call-interceptor.ts +10 -10
  410. package/node_modules/pi-agent-text-editor/src/api/tool-renderer.ts +18 -22
  411. package/node_modules/pi-agent-text-editor/src/core/builtin-edit-filter.ts +49 -48
  412. package/node_modules/pi-agent-text-editor/src/core/extension.ts +136 -127
  413. package/node_modules/pi-agent-text-editor/src/core/mutation-result/agent-diff.ts +279 -313
  414. package/node_modules/pi-agent-text-editor/src/core/mutation-result/diff.ts +85 -95
  415. package/node_modules/pi-agent-text-editor/src/core/mutation-result/file-mutation-agent-result.ts +233 -289
  416. package/node_modules/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +553 -582
  417. package/node_modules/pi-agent-text-editor/src/core/mutation-result/final-state-fragment.ts +64 -0
  418. package/node_modules/pi-agent-text-editor/src/core/mutation-result/inline-hints.ts +86 -98
  419. package/node_modules/pi-agent-text-editor/src/core/read-fragment-resolver.ts +83 -0
  420. package/node_modules/pi-agent-text-editor/src/core/text-anchor-recovery.ts +12 -11
  421. package/node_modules/pi-agent-text-editor/src/core/text-anchor-registry.ts +369 -335
  422. package/node_modules/pi-agent-text-editor/src/core/text-change-engine.ts +184 -214
  423. package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-coordinator.ts +476 -511
  424. package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-execution.ts +93 -105
  425. package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-recovery.ts +31 -33
  426. package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +526 -372
  427. package/node_modules/pi-agent-text-editor/src/core/text-edit-batch.ts +111 -87
  428. package/node_modules/pi-agent-text-editor/src/core/text-editor-config.ts +95 -0
  429. package/node_modules/pi-agent-text-editor/src/core/text-editor-core.ts +1899 -2027
  430. package/node_modules/pi-agent-text-editor/src/core/text-mutation-anchor-error.ts +26 -22
  431. package/node_modules/pi-agent-text-editor/src/core/text-mutation.ts +1043 -634
  432. package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/coordinator.ts +979 -1032
  433. package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/partial-json-resolver.ts +19 -23
  434. package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/partial-tool-recovery.ts +270 -273
  435. package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/rendering.ts +4 -2
  436. package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/streaming-provider.ts +353 -403
  437. package/node_modules/pi-agent-text-editor/src/core/tool-description.ts +9 -11
  438. package/node_modules/pi-agent-text-editor/src/tools/extension.ts +7 -8
  439. package/node_modules/pi-agent-text-editor/src/tools/text-selection.ts +289 -75
  440. package/node_modules/pi-agent-text-editor/src/tools/tool-text-copy.ts +100 -106
  441. package/node_modules/pi-agent-text-editor/src/tools/tool-text-delete.ts +73 -66
  442. package/node_modules/pi-agent-text-editor/src/tools/tool-text-insert.ts +64 -42
  443. package/node_modules/pi-agent-text-editor/src/tools/tool-text-move.ts +116 -124
  444. package/node_modules/pi-agent-text-editor/src/tools/tool-text-replace.ts +78 -58
  445. package/node_modules/pi-agent-text-editor/src/tools/tool-text-write.ts +26 -17
  446. package/node_modules/pi-agent-text-editor-argument-order/index.ts +16 -19
  447. package/node_modules/pi-agent-text-editor-argument-order/package.json +5 -5
  448. package/node_modules/pi-agent-text-editor-argument-order/runtime/entry-69.js +2 -0
  449. package/node_modules/pi-agent-text-editor-argument-order/src/argument-order-guard.ts +79 -91
  450. package/node_modules/pi-agent-text-editor-overwrite/index.ts +297 -323
  451. package/node_modules/pi-agent-text-editor-overwrite/package.json +6 -5
  452. package/node_modules/pi-agent-text-editor-overwrite/runtime/entry-70.js +2 -0
  453. package/node_modules/pi-agent-text-editor-renderer/index.ts +31 -12
  454. package/node_modules/pi-agent-text-editor-renderer/package.json +10 -7
  455. package/node_modules/pi-agent-text-editor-renderer/runtime/entry-71.js +2 -0
  456. package/node_modules/pi-agent-text-editor-renderer/src/animation-pressure.ts +50 -0
  457. package/node_modules/pi-agent-text-editor-renderer/src/config.ts +73 -0
  458. package/node_modules/pi-agent-text-editor-renderer/src/diff-model.ts +347 -359
  459. package/node_modules/pi-agent-text-editor-renderer/src/diff-renderer.ts +255 -216
  460. package/node_modules/pi-agent-text-editor-renderer/src/diff-theme.ts +297 -329
  461. package/node_modules/pi-agent-text-editor-renderer/src/diff-viewport.ts +19 -25
  462. package/node_modules/pi-agent-text-editor-renderer/src/frozen-viewport.ts +160 -168
  463. package/node_modules/pi-agent-text-editor-renderer/src/mutation-panel.ts +284 -180
  464. package/node_modules/pi-agent-text-editor-renderer/src/mutation-projection.ts +550 -329
  465. package/node_modules/pi-agent-text-editor-renderer/src/mutation-result.ts +142 -118
  466. package/node_modules/pi-agent-text-editor-renderer/src/persisted-result.ts +47 -0
  467. package/node_modules/pi-agent-text-editor-renderer/src/render-resource.ts +10 -4
  468. package/node_modules/pi-agent-text-editor-renderer/src/renderer.ts +744 -631
  469. package/node_modules/pi-agent-text-editor-renderer/src/typing-interpolation.ts +215 -157
  470. package/node_modules/pi-agent-text-editor-stale-anchor/index.ts +335 -335
  471. package/node_modules/pi-agent-text-editor-stale-anchor/package.json +7 -6
  472. package/node_modules/pi-agent-text-editor-stale-anchor/runtime/entry-72.js +2 -0
  473. package/node_modules/pi-agent-tool-call-interception/package.json +5 -2
  474. package/node_modules/pi-agent-tool-call-interception/runtime/entry-73.js +1 -0
  475. package/node_modules/pi-agent-tool-call-interception/src/index.ts +473 -350
  476. package/node_modules/pi-agent-tool-ui/package.json +15 -0
  477. package/node_modules/pi-agent-tool-ui/runtime/entry-74.js +1 -0
  478. package/node_modules/pi-agent-tool-ui/src/index.ts +318 -0
  479. package/node_modules/pi-agent-web/index.ts +35 -26
  480. package/node_modules/pi-agent-web/package.json +12 -10
  481. package/node_modules/pi-agent-web/runtime/entry-75.js +2 -0
  482. package/node_modules/pi-agent-web/src/browser-loader.ts +180 -0
  483. package/node_modules/pi-agent-web/src/doctor-plugin.ts +41 -0
  484. package/node_modules/pi-agent-web/src/resolver.ts +228 -146
  485. package/node_modules/pi-agent-web-html/index.ts +5 -6
  486. package/node_modules/pi-agent-web-html/package.json +9 -6
  487. package/node_modules/pi-agent-web-html/runtime/entry-76.js +2 -0
  488. package/node_modules/pi-agent-web-html/src/extract-readable-html.ts +72 -77
  489. package/node_modules/pi-agent-web-html/src/html-converter.ts +62 -75
  490. package/node_modules/pi-agent-web-image/index.ts +5 -6
  491. package/node_modules/pi-agent-web-image/package.json +6 -6
  492. package/node_modules/pi-agent-web-image/runtime/entry-77.js +2 -0
  493. package/node_modules/pi-agent-web-pdf/index.ts +5 -6
  494. package/node_modules/pi-agent-web-pdf/package.json +6 -6
  495. package/node_modules/pi-agent-web-pdf/runtime/entry-78.js +2 -0
  496. package/node_modules/pi-agent-web-text/index.ts +5 -6
  497. package/node_modules/pi-agent-web-text/package.json +6 -6
  498. package/node_modules/pi-agent-web-text/runtime/entry-79.js +2 -0
  499. package/package.json +101 -67
  500. package/src/api/code-view.ts +21 -17
  501. package/src/api/connect-doctor-plugin.ts +1 -0
  502. package/src/api/connect-plugin.ts +38 -48
  503. package/src/api/connect-tip-provider.ts +49 -0
  504. package/src/api/doctor.ts +1 -0
  505. package/src/api/plugin-protocol.ts +71 -40
  506. package/src/api/read.ts +6 -1
  507. package/src/api/resource.ts +3 -1
  508. package/src/api/search.ts +5 -1
  509. package/src/api/text-editor.ts +4 -1
  510. package/src/api/text.ts +3 -1
  511. package/src/api/tips.ts +77 -0
  512. package/src/api/tool-catalog.ts +1 -0
  513. package/src/api/tool-config/formatters.json +215 -0
  514. package/src/api/tool-config/linters.json +257 -0
  515. package/src/api/tool-config/lsp-servers.json +275 -0
  516. package/src/api/tool-config.ts +569 -0
  517. package/src/api/toolchain.ts +13 -18
  518. package/src/code-view/diagnostic-view.ts +150 -58
  519. package/src/code-view/reference.ts +64 -82
  520. package/src/code-view/source-view.ts +159 -180
  521. package/src/composite/builtin-extensions.ts +47 -37
  522. package/src/composite/extensions-config.ts +137 -0
  523. package/src/composite/selection.ts +109 -52
  524. package/src/core/diagnostic-store.ts +244 -0
  525. package/src/core/extension.ts +71 -35
  526. package/src/core/ide-core.ts +126 -98
  527. package/src/doctor/config-writer.ts +115 -0
  528. package/src/doctor/core.ts +221 -0
  529. package/src/doctor/discovery.ts +223 -0
  530. package/src/doctor/extension.ts +119 -0
  531. package/src/doctor/index.ts +1 -0
  532. package/src/doctor/inventory.ts +109 -0
  533. package/src/doctor/run.ts +177 -0
  534. package/src/doctor/tip-provider.ts +92 -0
  535. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/index.ts +43 -44
  536. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/package.json +11 -11
  537. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/index.ts +5 -6
  538. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/package.json +8 -8
  539. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/runtime/entry-21.js +2 -0
  540. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-pdf/index.ts +5 -6
  541. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-pdf/package.json +6 -6
  542. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-pdf/runtime/entry-22.js +2 -0
  543. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/index.ts +11 -12
  544. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/package.json +8 -8
  545. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/runtime/entry-23.js +2 -0
  546. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/runtime/entry-20.js +2 -0
  547. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/src/resolver.ts +112 -135
  548. package/src/extensions/pi-agent-read/extensions/pi-agent-web/index.ts +35 -26
  549. package/src/extensions/pi-agent-read/extensions/pi-agent-web/package.json +12 -10
  550. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/index.ts +5 -6
  551. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/package.json +9 -6
  552. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/runtime/entry-76.js +2 -0
  553. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/src/extract-readable-html.ts +72 -77
  554. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/src/html-converter.ts +62 -75
  555. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-image/index.ts +5 -6
  556. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-image/package.json +6 -6
  557. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-image/runtime/entry-77.js +2 -0
  558. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-pdf/index.ts +5 -6
  559. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-pdf/package.json +6 -6
  560. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-pdf/runtime/entry-78.js +2 -0
  561. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/index.ts +5 -6
  562. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/package.json +6 -6
  563. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/runtime/entry-79.js +2 -0
  564. package/src/extensions/pi-agent-read/extensions/pi-agent-web/runtime/entry-75.js +2 -0
  565. package/src/extensions/pi-agent-read/extensions/pi-agent-web/src/browser-loader.ts +180 -0
  566. package/src/extensions/pi-agent-read/extensions/pi-agent-web/src/doctor-plugin.ts +41 -0
  567. package/src/extensions/pi-agent-read/extensions/pi-agent-web/src/resolver.ts +228 -146
  568. package/src/extensions/pi-agent-read/index.ts +1 -1
  569. package/src/extensions/pi-agent-read/package.json +15 -13
  570. package/src/extensions/pi-agent-read/runtime/entry-35.js +1 -0
  571. package/src/extensions/pi-agent-read/runtime/entry-36.js +1 -0
  572. package/src/extensions/pi-agent-read/runtime/entry-37.js +1 -0
  573. package/src/extensions/pi-agent-read/runtime/entry-38.js +1 -0
  574. package/src/extensions/pi-agent-read/runtime/entry-39.js +2 -0
  575. package/src/extensions/pi-agent-read/src/api/connect-plugin.ts +38 -51
  576. package/src/extensions/pi-agent-read/src/api/plugin-protocol.ts +27 -33
  577. package/src/extensions/pi-agent-read/src/api/rendering.ts +3 -4
  578. package/src/extensions/pi-agent-read/src/api/tools/read.ts +237 -134
  579. package/src/extensions/pi-agent-read/src/core/extension.ts +51 -33
  580. package/src/extensions/pi-agent-read/src/core/read-core.ts +353 -330
  581. package/src/extensions/pi-agent-read/src/core/tools/read/output-truncation.ts +169 -156
  582. package/src/extensions/pi-agent-read/src/core/tools/read/persisted-result.ts +72 -0
  583. package/src/extensions/pi-agent-read/src/core/tools/read/read-renderer.ts +381 -313
  584. package/src/extensions/pi-agent-read/src/core/tools/read/read-result.ts +169 -154
  585. package/src/extensions/pi-agent-read/src/core/tools/read/temp-resource-store.ts +165 -187
  586. package/src/extensions/pi-agent-read/src/core/tools/read/views.ts +29 -0
  587. package/src/extensions/pi-agent-read/src/core/tools/tool-read.ts +1117 -680
  588. package/src/extensions/pi-agent-search/package.json +11 -10
  589. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/index.ts +90 -38
  590. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/package.json +13 -12
  591. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/runtime/entry-45.js +2 -0
  592. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/runtime/entry-46.js +1 -0
  593. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/doctor-plugin.ts +48 -0
  594. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/file-search.ts +90 -86
  595. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/persisted-result.ts +56 -0
  596. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/resolvers.ts +158 -161
  597. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/ripgrep.ts +16 -0
  598. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-backend.ts +215 -249
  599. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-query.ts +262 -102
  600. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-recipe.ts +68 -0
  601. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-renderer.ts +275 -345
  602. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-result.ts +152 -157
  603. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-session.ts +438 -278
  604. package/src/extensions/pi-agent-search/runtime/entry-41.js +1 -0
  605. package/src/extensions/pi-agent-search/runtime/entry-42.js +1 -0
  606. package/src/extensions/pi-agent-search/runtime/entry-43.js +1 -0
  607. package/src/extensions/pi-agent-search/runtime/entry-44.js +2 -0
  608. package/src/extensions/pi-agent-search/src/api/connect-plugin.ts +37 -47
  609. package/src/extensions/pi-agent-search/src/api/plugin-protocol.ts +26 -30
  610. package/src/extensions/pi-agent-search/src/api/search.ts +65 -67
  611. package/src/extensions/pi-agent-search/src/core/extension.ts +184 -92
  612. package/src/extensions/pi-agent-search/src/core/search-config.ts +98 -0
  613. package/src/extensions/pi-agent-search/src/core/search-core.ts +332 -357
  614. package/src/extensions/pi-agent-search/src/core/search-timeout.ts +54 -0
  615. package/src/extensions/pi-agent-text-editor/index.ts +7 -9
  616. package/src/extensions/pi-agent-text-editor/package.json +26 -25
  617. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/index.ts +18 -20
  618. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/package.json +9 -6
  619. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/runtime/entry-48.js +2 -0
  620. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/src/resolver.ts +35 -36
  621. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/index.ts +34 -0
  622. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/package.json +27 -0
  623. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/runtime/entry-49.js +1 -0
  624. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/runtime/entry-50.js +2 -0
  625. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/src/anchor.ts +230 -0
  626. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/src/config.ts +159 -0
  627. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/src/fuzzy-worker.ts +100 -0
  628. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/src/recovery.ts +151 -0
  629. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/index.ts +40 -36
  630. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/package.json +16 -12
  631. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/runtime/entry-51.js +1 -0
  632. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/runtime/entry-52.js +2 -0
  633. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/src/anchor.ts +105 -84
  634. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/src/read-handler.ts +43 -42
  635. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-argument-order/index.ts +16 -19
  636. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-argument-order/package.json +5 -5
  637. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-argument-order/runtime/entry-69.js +2 -0
  638. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-argument-order/src/argument-order-guard.ts +79 -91
  639. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-overwrite/index.ts +297 -323
  640. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-overwrite/package.json +6 -5
  641. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-overwrite/runtime/entry-70.js +2 -0
  642. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/index.ts +31 -12
  643. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/package.json +10 -7
  644. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/runtime/entry-71.js +2 -0
  645. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/animation-pressure.ts +50 -0
  646. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/config.ts +73 -0
  647. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/diff-model.ts +347 -359
  648. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/diff-renderer.ts +255 -216
  649. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/diff-theme.ts +297 -329
  650. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/diff-viewport.ts +19 -25
  651. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/frozen-viewport.ts +160 -168
  652. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/mutation-panel.ts +284 -180
  653. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/mutation-projection.ts +550 -329
  654. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/mutation-result.ts +142 -118
  655. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/persisted-result.ts +47 -0
  656. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/render-resource.ts +10 -4
  657. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/renderer.ts +744 -631
  658. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/typing-interpolation.ts +215 -157
  659. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/index.ts +335 -335
  660. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/package.json +7 -6
  661. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/runtime/entry-72.js +2 -0
  662. package/src/extensions/pi-agent-text-editor/runtime/entry-53.js +1 -0
  663. package/src/extensions/pi-agent-text-editor/runtime/entry-54.js +1 -0
  664. package/src/extensions/pi-agent-text-editor/runtime/entry-55.js +1 -0
  665. package/src/extensions/pi-agent-text-editor/runtime/entry-56.js +1 -0
  666. package/src/extensions/pi-agent-text-editor/runtime/entry-57.js +1 -0
  667. package/src/extensions/pi-agent-text-editor/runtime/entry-58.js +1 -0
  668. package/src/extensions/pi-agent-text-editor/runtime/entry-59.js +1 -0
  669. package/src/extensions/pi-agent-text-editor/runtime/entry-60.js +1 -0
  670. package/src/extensions/pi-agent-text-editor/runtime/entry-61.js +1 -0
  671. package/src/extensions/pi-agent-text-editor/runtime/entry-62.js +1 -0
  672. package/src/extensions/pi-agent-text-editor/runtime/entry-63.js +1 -0
  673. package/src/extensions/pi-agent-text-editor/runtime/entry-64.js +1 -0
  674. package/src/extensions/pi-agent-text-editor/runtime/entry-65.js +1 -0
  675. package/src/extensions/pi-agent-text-editor/runtime/entry-66.js +1 -0
  676. package/src/extensions/pi-agent-text-editor/runtime/entry-67.js +1 -0
  677. package/src/extensions/pi-agent-text-editor/runtime/entry-68.js +2 -0
  678. package/src/extensions/pi-agent-text-editor/src/api/anchor-inspection.ts +18 -18
  679. package/src/extensions/pi-agent-text-editor/src/api/connect-plugin.ts +40 -50
  680. package/src/extensions/pi-agent-text-editor/src/api/edit-completion.ts +10 -13
  681. package/src/extensions/pi-agent-text-editor/src/api/edit-pipeline.ts +31 -36
  682. package/src/extensions/pi-agent-text-editor/src/api/last-resolved-resource.ts +25 -27
  683. package/src/extensions/pi-agent-text-editor/src/api/mutation-guard.ts +26 -31
  684. package/src/extensions/pi-agent-text-editor/src/api/mutation-preview.ts +18 -21
  685. package/src/extensions/pi-agent-text-editor/src/api/mutation-result.ts +20 -19
  686. package/src/extensions/pi-agent-text-editor/src/api/mutation-tool.ts +154 -131
  687. package/src/extensions/pi-agent-text-editor/src/api/plugin-protocol.ts +173 -144
  688. package/src/extensions/pi-agent-text-editor/src/api/post-edit.ts +93 -103
  689. package/src/extensions/pi-agent-text-editor/src/api/stale-anchor-message.ts +17 -14
  690. package/src/extensions/pi-agent-text-editor/src/api/text-selection-anchor.ts +85 -99
  691. package/src/extensions/pi-agent-text-editor/src/api/tool-call-interceptor.ts +10 -10
  692. package/src/extensions/pi-agent-text-editor/src/api/tool-renderer.ts +18 -22
  693. package/src/extensions/pi-agent-text-editor/src/core/builtin-edit-filter.ts +49 -48
  694. package/src/extensions/pi-agent-text-editor/src/core/extension.ts +136 -127
  695. package/src/extensions/pi-agent-text-editor/src/core/mutation-result/agent-diff.ts +279 -313
  696. package/src/extensions/pi-agent-text-editor/src/core/mutation-result/diff.ts +85 -95
  697. package/src/extensions/pi-agent-text-editor/src/core/mutation-result/file-mutation-agent-result.ts +233 -289
  698. package/src/extensions/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +553 -582
  699. package/src/extensions/pi-agent-text-editor/src/core/mutation-result/final-state-fragment.ts +64 -0
  700. package/src/extensions/pi-agent-text-editor/src/core/mutation-result/inline-hints.ts +86 -98
  701. package/src/extensions/pi-agent-text-editor/src/core/read-fragment-resolver.ts +83 -0
  702. package/src/extensions/pi-agent-text-editor/src/core/text-anchor-recovery.ts +12 -11
  703. package/src/extensions/pi-agent-text-editor/src/core/text-anchor-registry.ts +369 -335
  704. package/src/extensions/pi-agent-text-editor/src/core/text-change-engine.ts +184 -214
  705. package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-coordinator.ts +476 -511
  706. package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-execution.ts +93 -105
  707. package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-recovery.ts +31 -33
  708. package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +526 -372
  709. package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch.ts +111 -87
  710. package/src/extensions/pi-agent-text-editor/src/core/text-editor-config.ts +95 -0
  711. package/src/extensions/pi-agent-text-editor/src/core/text-editor-core.ts +1899 -2027
  712. package/src/extensions/pi-agent-text-editor/src/core/text-mutation-anchor-error.ts +26 -22
  713. package/src/extensions/pi-agent-text-editor/src/core/text-mutation.ts +1043 -634
  714. package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/coordinator.ts +979 -1032
  715. package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/partial-json-resolver.ts +19 -23
  716. package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/partial-tool-recovery.ts +270 -273
  717. package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/rendering.ts +4 -2
  718. package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/streaming-provider.ts +353 -403
  719. package/src/extensions/pi-agent-text-editor/src/core/tool-description.ts +9 -11
  720. package/src/extensions/pi-agent-text-editor/src/tools/extension.ts +7 -8
  721. package/src/extensions/pi-agent-text-editor/src/tools/text-selection.ts +289 -75
  722. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-copy.ts +100 -106
  723. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-delete.ts +73 -66
  724. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-insert.ts +64 -42
  725. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-move.ts +116 -124
  726. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-replace.ts +78 -58
  727. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-write.ts +26 -17
  728. package/src/pi-agent-ide.ts +22 -10
  729. package/src/plugins/pi-agent-ide-ast/index.ts +1 -1
  730. package/src/plugins/pi-agent-ide-ast/package.json +15 -13
  731. package/src/plugins/pi-agent-ide-ast/runtime/entry-24.js +1 -0
  732. package/src/plugins/pi-agent-ide-ast/runtime/entry-25.js +2 -0
  733. package/src/plugins/pi-agent-ide-ast/src/api/scope.ts +1 -1
  734. package/src/plugins/pi-agent-ide-ast/src/ast/collect-files.ts +11 -16
  735. package/src/plugins/pi-agent-ide-ast/src/ast/manager.ts +292 -322
  736. package/src/plugins/pi-agent-ide-ast/src/ast/outline.ts +591 -656
  737. package/src/plugins/pi-agent-ide-ast/src/ast/read-text.ts +15 -19
  738. package/src/plugins/pi-agent-ide-ast/src/doctor-plugin.ts +130 -0
  739. package/src/plugins/pi-agent-ide-ast/src/extension.ts +75 -64
  740. package/src/plugins/pi-agent-ide-ast/src/outline-resolver.ts +51 -54
  741. package/src/plugins/pi-agent-ide-ast/src/scope-handler.ts +168 -185
  742. package/src/plugins/pi-agent-ide-ast/src/scope-resolver.ts +36 -36
  743. package/src/plugins/pi-agent-ide-ast/src/search-resolver.ts +118 -129
  744. package/src/plugins/pi-agent-ide-changes/index.ts +57 -51
  745. package/src/plugins/pi-agent-ide-changes/package.json +14 -12
  746. package/src/plugins/pi-agent-ide-changes/runtime/entry-26.js +1 -0
  747. package/src/plugins/pi-agent-ide-changes/runtime/entry-27.js +2 -0
  748. package/src/plugins/pi-agent-ide-changes/src/change-anchor.ts +74 -78
  749. package/src/plugins/pi-agent-ide-changes/src/changes/change-groups.ts +230 -245
  750. package/src/plugins/pi-agent-ide-changes/src/changes/change-service.ts +189 -192
  751. package/src/plugins/pi-agent-ide-changes/src/changes/change-types.ts +75 -79
  752. package/src/plugins/pi-agent-ide-changes/src/changes/git-changes-backend.ts +259 -276
  753. package/src/plugins/pi-agent-ide-changes/src/current-change-presenter.ts +164 -190
  754. package/src/plugins/pi-agent-ide-changes/src/doctor-plugin.ts +59 -0
  755. package/src/plugins/pi-agent-ide-changes/src/index-mutation-queue.ts +10 -9
  756. package/src/plugins/pi-agent-ide-changes/src/last-text-transaction-store.ts +49 -62
  757. package/src/plugins/pi-agent-ide-changes/src/tool-index-change.ts +98 -93
  758. package/src/plugins/pi-agent-ide-changes/src/tool-text-undo.ts +131 -126
  759. package/src/plugins/pi-agent-ide-diagnostics/index.ts +1 -0
  760. package/src/plugins/pi-agent-ide-diagnostics/src/extension.ts +191 -0
  761. package/src/plugins/pi-agent-ide-formatter/index.ts +22 -23
  762. package/src/plugins/pi-agent-ide-formatter/package.json +6 -3
  763. package/src/plugins/pi-agent-ide-formatter/runtime/entry-28.js +2 -0
  764. package/src/plugins/pi-agent-ide-formatter/src/catalog.ts +281 -0
  765. package/src/plugins/pi-agent-ide-formatter/src/doctor-plugin.ts +156 -0
  766. package/src/plugins/pi-agent-ide-formatter/src/formatter.ts +38 -40
  767. package/src/plugins/pi-agent-ide-formatter/src/registry.ts +94 -27
  768. package/src/plugins/pi-agent-ide-languages/index.ts +24 -0
  769. package/{node_modules/pi-agent-search-semantic → src/plugins/pi-agent-ide-languages}/package.json +6 -7
  770. package/src/plugins/pi-agent-ide-languages/runtime/entry-29.js +2 -0
  771. package/src/plugins/pi-agent-ide-languages/src/languages.ts +84 -0
  772. package/src/plugins/pi-agent-ide-lint/index.ts +66 -225
  773. package/src/plugins/pi-agent-ide-lint/package.json +10 -8
  774. package/src/plugins/pi-agent-ide-lint/runtime/entry-30.js +2 -0
  775. package/src/plugins/pi-agent-ide-lint/src/catalog.ts +295 -0
  776. package/src/plugins/pi-agent-ide-lint/src/command-linter.ts +98 -144
  777. package/src/plugins/pi-agent-ide-lint/src/diagnostics.ts +182 -0
  778. package/src/plugins/pi-agent-ide-lint/src/doctor-plugin.ts +155 -0
  779. package/src/plugins/pi-agent-ide-lint/src/registry.ts +94 -27
  780. package/src/plugins/pi-agent-ide-lsp/index.ts +111 -231
  781. package/src/plugins/pi-agent-ide-lsp/package.json +13 -11
  782. package/src/plugins/pi-agent-ide-lsp/runtime/entry-31.js +2 -0
  783. package/src/plugins/pi-agent-ide-lsp/src/catalog.ts +248 -0
  784. package/src/plugins/pi-agent-ide-lsp/src/code-view-resolvers.ts +109 -116
  785. package/src/plugins/pi-agent-ide-lsp/src/diagnostic-source.ts +113 -0
  786. package/src/plugins/pi-agent-ide-lsp/src/doctor-plugin.ts +163 -0
  787. package/src/plugins/pi-agent-ide-lsp/src/lsp/client.ts +439 -396
  788. package/src/plugins/pi-agent-ide-lsp/src/lsp/code-views.ts +421 -458
  789. package/src/plugins/pi-agent-ide-lsp/src/lsp/diagnostic-adapters.ts +76 -0
  790. package/src/plugins/pi-agent-ide-lsp/src/lsp/diagnostics.ts +143 -159
  791. package/src/plugins/pi-agent-ide-lsp/src/lsp/index.ts +25 -20
  792. package/src/plugins/pi-agent-ide-lsp/src/lsp/language-map.ts +31 -33
  793. package/src/plugins/pi-agent-ide-lsp/src/lsp/lsp-compiler.ts +49 -57
  794. package/src/plugins/pi-agent-ide-lsp/src/lsp/manager.ts +350 -415
  795. package/src/plugins/pi-agent-ide-lsp/src/lsp/navigation.ts +58 -63
  796. package/src/plugins/pi-agent-ide-lsp/src/lsp/registry.ts +196 -77
  797. package/src/plugins/pi-agent-ide-lsp/src/lsp/rename.ts +36 -36
  798. package/src/plugins/pi-agent-ide-lsp/src/lsp/symbol-search.ts +118 -127
  799. package/src/plugins/pi-agent-ide-lsp/src/lsp/symbols.ts +108 -107
  800. package/src/plugins/pi-agent-ide-lsp/src/lsp/types.ts +76 -57
  801. package/src/plugins/pi-agent-ide-lsp/src/search-resolver.ts +36 -32
  802. package/src/post-edit/gate.ts +18 -150
  803. package/src/tips/core.ts +86 -0
  804. package/src/tips/extension.ts +115 -0
  805. package/src/tips/persistence.ts +225 -0
  806. package/src/tips/tip-callout.ts +77 -0
  807. package/src/toolchain/diagnostic.ts +3 -4
  808. package/src/toolchain/registry.ts +133 -170
  809. package/src/toolchain/types.ts +83 -128
  810. package/node_modules/pi-agent-ide-formatter/src/types.ts +0 -10
  811. package/node_modules/pi-agent-ide-lint/src/command-runner.ts +0 -51
  812. package/node_modules/pi-agent-ide-lint/src/types.ts +0 -10
  813. package/node_modules/pi-agent-search-semantic/index.ts +0 -105
  814. package/node_modules/pi-agent-search-text/src/lucene-query-parser.d.ts +0 -22
  815. package/node_modules/pi-agent-search-web/index.ts +0 -74
  816. package/node_modules/pi-agent-search-web/package.json +0 -19
  817. package/node_modules/pi-agent-search-web/src/config.ts +0 -499
  818. package/node_modules/pi-agent-search-web/src/providers/duckduckgo-html.ts +0 -78
  819. package/node_modules/pi-agent-search-web/src/providers/endpoints.ts +0 -89
  820. package/node_modules/pi-agent-search-web/src/providers/request.ts +0 -430
  821. package/node_modules/pi-agent-search-web/src/providers/response.ts +0 -402
  822. package/node_modules/pi-agent-search-web/src/search-format.ts +0 -29
  823. package/node_modules/pi-agent-search-web/src/search.ts +0 -434
  824. package/node_modules/pi-agent-search-web/src/types.ts +0 -153
  825. package/src/composite/config.ts +0 -94
  826. package/src/extensions/pi-agent-search/plugins/pi-agent-search-semantic/index.ts +0 -105
  827. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/lucene-query-parser.d.ts +0 -22
  828. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/index.ts +0 -74
  829. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/package.json +0 -19
  830. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/config.ts +0 -499
  831. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/providers/duckduckgo-html.ts +0 -78
  832. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/providers/endpoints.ts +0 -89
  833. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/providers/request.ts +0 -430
  834. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/providers/response.ts +0 -402
  835. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/search-format.ts +0 -29
  836. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/search.ts +0 -434
  837. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/types.ts +0 -153
  838. package/src/plugins/pi-agent-ide-formatter/src/types.ts +0 -10
  839. package/src/plugins/pi-agent-ide-lint/src/command-runner.ts +0 -51
  840. package/src/plugins/pi-agent-ide-lint/src/types.ts +0 -10
  841. package/src/toolchain/gate.ts +0 -322
@@ -1,756 +1,1165 @@
1
+ import { requiredValue } from "pi-agent-invariant";
1
2
  import path from "node:path";
3
+ import type {
4
+ TextAnchorRecoveryCandidateRange,
5
+ TextAnchorRecoveryRange,
6
+ TextAnchorRejection,
7
+ TextSelectionRange,
8
+ TextTarget,
9
+ } from "pi-agent-text";
2
10
 
3
11
  import {
4
- type AgentToolResult,
5
- defineTool,
6
- type ExtensionContext,
7
- type ToolDefinition,
12
+ type AgentToolResult,
13
+ defineTool,
14
+ type ExtensionContext,
15
+ type ToolDefinition,
8
16
  } from "@earendil-works/pi-coding-agent";
9
17
 
10
- import { type FileMutationBatchResult, FileMutationResult } from "#src/api/mutation-result.js";
11
18
  import {
12
- mutationSource,
13
- type TextMutation,
14
- type TextMutationContext,
15
- type TextMutationEdit,
16
- type TextMutationToolRegistration,
19
+ type FileMutationBatchResult,
20
+ FileMutationResult,
21
+ type MutationResultPresentation,
22
+ } from "#src/api/mutation-result.js";
23
+ import {
24
+ isMutationAnchorValue,
25
+ mutationSource,
26
+ type TextMutation,
27
+ type TextMutationContext,
28
+ type TextMutationEdit,
29
+ type TextMutationToolRegistration,
17
30
  } from "#src/api/mutation-tool.js";
18
31
  import { isTextMutationResultContributionData } from "#src/api/post-edit.js";
19
- import { formatStaleAnchorMessage, type StaleAnchorMessageDetails } from "#src/api/stale-anchor-message.js";
32
+ import {
33
+ formatStaleAnchorMessage,
34
+ type StaleAnchorMessageDetails,
35
+ } from "#src/api/stale-anchor-message.js";
20
36
  import { createUnifiedDiff } from "#src/core/mutation-result/diff.js";
21
37
  import { FileMutationAgentResult } from "#src/core/mutation-result/file-mutation-agent-result.js";
22
38
  import { readTextAnchorRecovery } from "#src/core/text-anchor-recovery.js";
23
39
  import { applyTextChanges, TextChangeDocument } from "#src/core/text-change-engine.js";
40
+ import { resolvedTextAnchorType } from "#src/core/text-anchor-registry.js";
24
41
  import { executeTextToolWithBatch } from "#src/core/text-edit-batch-registrar.js";
25
42
  import {
26
- contextualizeTextMutationAnchorError,
27
- TextMutationAnchorResolutionError,
43
+ contextualizeTextMutationAnchorError,
44
+ TextMutationAnchorAggregateError,
45
+ TextMutationAnchorResolutionError,
28
46
  } from "#src/core/text-mutation-anchor-error.js";
29
47
  import {
30
- type ToolCallInterceptionRenderStore,
31
- withToolCallInterceptionRendering,
48
+ TOOL_CALL_INTERCEPTION_ANCHOR_RENDER_PATCH,
49
+ type ToolCallInterceptionRenderStore,
50
+ withToolCallInterceptionRendering,
32
51
  } from "#src/core/tool-call-interceptor/rendering.js";
33
52
  import { appendSchemaFieldOrder } from "#src/core/tool-description.js";
34
53
 
35
54
  import type { TextEditExecutionOutcome } from "#src/api/edit-pipeline.js";
36
- import type { TextMutationPreviewOutcome, TextMutationPreviewRequest } from "#src/api/mutation-preview.js";
37
55
  import type {
38
- ResolveResourceTextAnchor,
39
- TextEditorCore,
40
- TextResourceEditFailure,
41
- TextResourceEditOutcome,
42
- TextResourcesEditOutcome,
56
+ TextMutationPreviewOutcome,
57
+ TextMutationPreviewRequest,
58
+ } from "#src/api/mutation-preview.js";
59
+ import type {
60
+ ResolveResourceTextAnchor,
61
+ TextEditorCore,
62
+ TextResourceEditFailure,
63
+ TextResourceEditOutcome,
64
+ TextResourcesEditOutcome,
43
65
  } from "#src/core/text-editor-core.js";
44
- import type { TextAnchor } from "pi-agent-text";
66
+ import { renderTextAnchor, type TextAnchor } from "pi-agent-text";
67
+ import { TextSelectionAnchor } from "#src/api/text-selection-anchor.js";
68
+ import type { ToolCallAnchorRenderState } from "pi-agent-tool-call-interception";
45
69
  import type { Static, TSchema } from "typebox";
46
70
 
47
- export function createTextTool<TParams extends TSchema>(
48
- core: TextEditorCore,
49
- definition: TextMutationToolRegistration<TParams>,
50
- annotations: ToolCallInterceptionRenderStore,
51
- pluginPromptGuideline: () => string | undefined,
52
- getLastResolvedSource: () => string | undefined,
53
- ): ToolDefinition<TParams, FileMutationBatchResult>
54
- {
55
- const renderer = core.getToolRenderer(definition.name);
56
-
57
- const tool = withToolCallInterceptionRendering<TParams, FileMutationBatchResult, unknown>(
58
- defineTool<TParams, FileMutationBatchResult, unknown>({
59
- name: definition.name,
60
- label: definition.name,
61
- description: appendSchemaFieldOrder(definition.description, definition.parameters),
62
- parameters: definition.parameters,
63
- prepareArguments: (args) =>
64
- prepareGuardedArguments(
65
- definition.parameters,
66
- args,
67
- definition.source.inherited ? definition.source.field : undefined,
68
- getLastResolvedSource(),
69
- ),
70
- ...(renderer?.renderShell === undefined ? {} : { renderShell: renderer.renderShell }),
71
- ...(renderer?.renderCall === undefined ? {} : { renderCall: renderer.renderCall }),
72
- ...(renderer?.renderResult === undefined ? {} : { renderResult: renderer.renderResult }),
73
- async execute(toolCallId, params, signal, onUpdate, context)
74
- {
75
- const directExecute = () =>
76
- executeTextMutation(core, definition, params as unknown as Static<TParams>, signal, context);
77
- return executeTextToolWithBatch(toolCallId, directExecute, signal, onUpdate, context);
78
- },
79
- }),
80
- annotations,
81
- );
82
- Object.defineProperty(tool, "promptGuidelines", {
83
- enumerable: true,
84
- get(): string[] | undefined
85
- {
86
- const guideline = pluginPromptGuideline();
87
- return guideline === undefined ? undefined : [guideline];
88
- },
89
- });
90
- return tool;
71
+ export function createTextTool<TParameters extends TSchema>(
72
+ core: TextEditorCore,
73
+ definition: TextMutationToolRegistration<TParameters>,
74
+ annotations: ToolCallInterceptionRenderStore,
75
+ pluginPromptGuideline: () => string | undefined,
76
+ getLastResolvedSource: () => string | undefined,
77
+ ): ToolDefinition<TParameters, FileMutationBatchResult> {
78
+ const renderer = core.getToolRenderer(definition.name);
79
+
80
+ const tool = withToolCallInterceptionRendering<TParameters, FileMutationBatchResult, unknown>(
81
+ defineTool<TParameters, FileMutationBatchResult, unknown>({
82
+ name: definition.name,
83
+ label: definition.name,
84
+
85
+ promptSnippet: definition.promptSnippet,
86
+ description: appendSchemaFieldOrder(definition.description, definition.parameters),
87
+ parameters: definition.parameters,
88
+ prepareArguments: (arguments_) =>
89
+ // oxlint-disable-next-line typescript/no-unsafe-return -- TypeBox resolves only concrete tool schemas.
90
+ prepareGuardedArguments(
91
+ definition.parameters,
92
+ arguments_,
93
+ definition.source.inherited ? definition.source.field : undefined,
94
+ getLastResolvedSource(),
95
+ definition.anchors ?? [],
96
+ ),
97
+ ...(renderer?.renderShell !== undefined && { renderShell: renderer.renderShell }),
98
+ ...(renderer?.renderCall !== undefined && { renderCall: renderer.renderCall }),
99
+ ...(renderer?.renderResult !== undefined && { renderResult: renderer.renderResult }),
100
+ async execute(toolCallId, parameters, signal, onUpdate, context) {
101
+ const directExecute = () =>
102
+ executeTextMutation(
103
+ core,
104
+ definition,
105
+ asMutationParameters<TParameters>(parameters),
106
+ signal,
107
+ context,
108
+ (field, state) =>
109
+ annotations.resolveArguments(toolCallId, {
110
+ [TOOL_CALL_INTERCEPTION_ANCHOR_RENDER_PATCH]: { [field]: state },
111
+ }),
112
+ getLastResolvedSource(),
113
+ );
114
+ return executeTextToolWithBatch(toolCallId, directExecute, signal, onUpdate, context);
115
+ },
116
+ }),
117
+ annotations,
118
+ );
119
+ Object.defineProperty(tool, "promptGuidelines", {
120
+ enumerable: true,
121
+ get(): string[] | undefined {
122
+ const pluginGuideline = pluginPromptGuideline();
123
+ const guidelines = [
124
+ ...(pluginGuideline === undefined ? [] : [pluginGuideline]),
125
+ ...(definition.promptGuidelines ?? []),
126
+ ];
127
+ return guidelines.length === 0 ? undefined : guidelines;
128
+ },
129
+ });
130
+ return tool;
91
131
  }
92
132
 
93
- function prepareGuardedArguments<TParams extends TSchema>(
94
- schema: TParams,
95
- args: unknown,
96
- sourceField: string | undefined,
97
- lastResolvedSource: string | undefined,
98
- ): ReturnType<NonNullable<ToolDefinition<TParams, FileMutationBatchResult>["prepareArguments"]>>
99
- {
100
- const prepared = args !== null && typeof args === "object" && !Array.isArray(args)
101
- ? { ...(args as Record<string, unknown>) }
102
- : {};
103
- const required = (schema as { required?: unknown; }).required;
104
- const properties = (schema as { properties?: Record<string, { type?: unknown; }>; }).properties;
105
-
106
- if (sourceField !== undefined && !(sourceField in prepared) && lastResolvedSource !== undefined)
107
- {
108
- prepared[sourceField] = lastResolvedSource;
109
- }
110
-
111
- if (Array.isArray(required))
112
- {
113
- for (const field of required)
114
- {
115
- if (typeof field === "string" && !(field in prepared) && properties?.[field]?.type === "string")
116
- {
117
- prepared[field] = "";
118
- }
119
- }
133
+ function prepareGuardedArguments<TParameters extends TSchema>(
134
+ schema: TParameters,
135
+ arguments_: unknown,
136
+ sourceField: string | undefined,
137
+ lastResolvedSource: string | undefined,
138
+ anchorFields: NonNullable<TextMutationToolRegistration["anchors"]>,
139
+ ): ReturnType<
140
+ NonNullable<ToolDefinition<TParameters, FileMutationBatchResult>["prepareArguments"]>
141
+ > {
142
+ const prepared =
143
+ arguments_ !== null && typeof arguments_ === "object" && !Array.isArray(arguments_)
144
+ ? { ...(arguments_ as Record<string, unknown>) }
145
+ : {};
146
+ const required = (schema as { required?: unknown }).required;
147
+ const properties = (schema as { properties?: Record<string, { type?: unknown }> }).properties;
148
+
149
+ // Anchor ownership is asynchronous; defer its source fallback until resources resolve.
150
+ const hasExplicitAnchor = anchorFields.some((descriptor) =>
151
+ isMutationAnchorValue(descriptor, prepared[descriptor.field]),
152
+ );
153
+ if (
154
+ sourceField !== undefined &&
155
+ !(sourceField in prepared) &&
156
+ lastResolvedSource !== undefined &&
157
+ !hasExplicitAnchor
158
+ ) {
159
+ prepared[sourceField] = lastResolvedSource;
160
+ }
161
+
162
+ if (Array.isArray(required)) {
163
+ for (const field of required) {
164
+ if (
165
+ typeof field === "string" &&
166
+ !(field in prepared) &&
167
+ properties?.[field]?.type === "string"
168
+ ) {
169
+ prepared[field] = "";
170
+ }
120
171
  }
172
+ }
121
173
 
122
- return prepared as ReturnType<NonNullable<ToolDefinition<TParams, FileMutationBatchResult>["prepareArguments"]>>;
174
+ // oxlint-disable-next-line typescript/no-unsafe-return -- TypeBox resolves only concrete tool schemas.
175
+ return prepared as ReturnType<
176
+ NonNullable<ToolDefinition<TParameters, FileMutationBatchResult>["prepareArguments"]>
177
+ >;
123
178
  }
124
179
 
125
- interface ResolvedMutationSources
126
- {
127
- readonly fields: ReadonlyMap<string, string>;
128
- readonly anchors: ReadonlyMap<string, readonly string[]>;
129
- readonly resources: readonly string[];
180
+ interface ResolvedMutationSources {
181
+ readonly fields: ReadonlyMap<string, string>;
182
+ readonly anchors: ReadonlyMap<string, readonly string[]>;
183
+ readonly implicitTargets: ReadonlyMap<string, readonly TextTarget[]>;
184
+ readonly targets: readonly TextTarget[];
185
+ readonly resources: readonly string[];
130
186
  }
131
187
 
132
188
  async function resolveMutationSources(
133
- core: TextEditorCore,
134
- definition: TextMutationToolRegistration,
135
- input: Readonly<Record<string, unknown>>,
136
- context: { readonly cwd: string; readonly signal?: AbortSignal; },
137
- ): Promise<ResolvedMutationSources>
138
- {
139
- const expandedByAnchor = new Map<string, readonly string[]>();
140
- const expandedBySourceField = new Map<string, readonly string[]>();
141
-
142
- for (const descriptor of definition.anchors ?? [])
143
- {
144
- const value = input[descriptor.field];
145
-
146
- if (typeof value !== "string" || value.length === 0)
147
- {
148
- continue;
149
- }
150
-
151
- let expanded: readonly string[] | undefined;
152
-
153
- try
154
- {
155
- expanded = await core.resolveTextAnchorResources(value, descriptor.kinds, context);
156
- }
157
- catch (error)
158
- {
159
- const source = input[descriptor.sourceField];
160
- throw contextualizeTextMutationAnchorError(
161
- error,
162
- definition.name,
163
- descriptor.field,
164
- typeof source === "string" ? source : "",
165
- value,
166
- );
167
- }
189
+ core: TextEditorCore,
190
+ definition: TextMutationToolRegistration,
191
+ input: Readonly<Record<string, unknown>>,
192
+ context: { readonly cwd: string; readonly signal?: AbortSignal },
193
+ lastResolvedSource?: string,
194
+ ): Promise<ResolvedMutationSources> {
195
+ const expandedByAnchor = new Map<string, readonly TextTarget[]>();
196
+ const expandedBySourceField = new Map<string, readonly TextTarget[]>();
197
+ const expandedFromExplicitSource = new Set<string>();
198
+ const explicitlyScopedSourceFields = new Set<string>();
199
+ const allKinds = [
200
+ ...new Set((definition.anchors ?? []).flatMap((descriptor) => descriptor.kinds)),
201
+ ];
202
+ const resolveTargets = async (
203
+ value: string,
204
+ field: string,
205
+ source: string,
206
+ ): Promise<readonly TextTarget[] | undefined> => {
207
+ try {
208
+ return await core.resolveTextAnchorResources(value, allKinds, context);
209
+ } catch (error) {
210
+ throw contextualizeTextMutationAnchorError(error, definition.name, field, source, value);
211
+ }
212
+ };
213
+
214
+ for (const descriptor of definition.anchors ?? []) {
215
+ const value = input[descriptor.field];
216
+ if (!isMutationAnchorValue(descriptor, value)) continue;
217
+ const expanded = await resolveTargets(
218
+ value,
219
+ descriptor.field,
220
+ typeof input[descriptor.sourceField] === "string"
221
+ ? String(input[descriptor.sourceField])
222
+ : "",
223
+ );
224
+ if (expanded === undefined) continue;
225
+ expandedByAnchor.set(descriptor.field, expanded);
226
+ const existingTargets = expandedBySourceField.get(descriptor.sourceField);
227
+ if (existingTargets !== undefined && !sameTargetSources(existingTargets, expanded)) {
228
+ throw new Error(`Incompatible targets for ${descriptor.sourceField}.`);
229
+ }
230
+ expandedBySourceField.set(
231
+ descriptor.sourceField,
232
+ mergeTargets(expandedBySourceField.get(descriptor.sourceField) ?? [], expanded),
233
+ );
234
+ }
235
+
236
+ for (const descriptor of [definition.source, ...(definition.source.targets ?? [])]) {
237
+ const explicit = input[descriptor.field];
238
+ if (typeof explicit !== "string" || explicit.length === 0) continue;
239
+ const expanded = await resolveTargets(explicit, descriptor.field, explicit);
240
+ if (expanded !== undefined) {
241
+ expandedFromExplicitSource.add(descriptor.field);
242
+ expandedBySourceField.set(
243
+ descriptor.field,
244
+ mergeTargets(expandedBySourceField.get(descriptor.field) ?? [], expanded),
245
+ );
246
+ }
247
+ }
248
+
249
+ const fields = new Map<string, string>();
250
+ const sourceDescriptors = [definition.source, ...(definition.source.targets ?? [])];
251
+ for (const descriptor of sourceDescriptors) {
252
+ const explicit = input[descriptor.field];
253
+ const explicitSource =
254
+ typeof explicit === "string" && explicit.length > 0 ? explicit : undefined;
255
+ const fallback = "fallbackTo" in descriptor ? fields.get(descriptor.fallbackTo) : undefined;
256
+ let expanded = expandedBySourceField.get(descriptor.field);
257
+ if (expanded !== undefined) {
258
+ if (explicitSource !== undefined && !expandedFromExplicitSource.has(descriptor.field)) {
259
+ const scoped = expanded.filter((target) =>
260
+ sameResource(explicitSource, target.source, context.cwd),
261
+ );
168
262
 
169
- if (expanded === undefined)
170
- {
171
- continue;
263
+ if (scoped.length === 0) {
264
+ throw new Error(`Anchor does not belong to ${explicitSource}.`);
172
265
  }
173
266
 
174
- const previous = expandedBySourceField.get(descriptor.sourceField);
175
-
176
- if (previous !== undefined && !sameResources(previous, expanded, context.cwd))
177
- {
178
- throw new Error(`Mutation anchors for ${descriptor.sourceField} refer to different resources.`);
267
+ if (scoped.length !== expanded.length) {
268
+ expanded = scoped;
269
+ expandedBySourceField.set(descriptor.field, scoped);
270
+ explicitlyScopedSourceFields.add(descriptor.field);
179
271
  }
180
-
181
- expandedByAnchor.set(descriptor.field, expanded);
182
- expandedBySourceField.set(descriptor.sourceField, expanded);
272
+ }
273
+ fields.set(descriptor.field, requiredValue(expanded[0]).source);
274
+ continue;
183
275
  }
276
+ // Resource-owning anchors win. Only an omitted primary source may use read history.
277
+ const inherited =
278
+ descriptor.field === definition.source.field &&
279
+ definition.source.inherited === true &&
280
+ !(descriptor.field in input)
281
+ ? lastResolvedSource
282
+ : undefined;
283
+ const source = explicitSource ?? fallback ?? inherited;
284
+ if (source === undefined) throw new Error(`${descriptor.field} is required`);
285
+ fields.set(descriptor.field, source);
286
+ }
287
+
288
+ const anchors = new Map<string, readonly string[]>();
289
+ const implicitTargets = new Map<string, readonly TextTarget[]>();
290
+ for (const descriptor of definition.anchors ?? []) {
291
+ const targets = expandedBySourceField.get(descriptor.sourceField);
292
+
293
+ const hasExplicitAnchor = isMutationAnchorValue(descriptor, input[descriptor.field]);
294
+ const hasImplicitSourceSelection = expandedFromExplicitSource.has(descriptor.sourceField);
295
+ if (!hasExplicitAnchor && !hasImplicitSourceSelection) continue;
296
+ if (
297
+ targets !== undefined &&
298
+ (hasImplicitSourceSelection || !expandedByAnchor.has(descriptor.field))
299
+ ) {
300
+ implicitTargets.set(descriptor.field, targets);
301
+ }
302
+ }
303
+
304
+ for (const descriptor of definition.anchors ?? []) {
305
+ const expanded = expandedByAnchor.get(descriptor.field);
306
+ const value = input[descriptor.field];
307
+
308
+ if (typeof value === "string" && !isMutationAnchorValue(descriptor, value)) continue;
309
+ const source = fields.get(descriptor.sourceField);
310
+ if (expanded !== undefined) {
311
+ anchors.set(
312
+ descriptor.field,
313
+ source !== undefined &&
314
+ (expanded.length === 1 || explicitlyScopedSourceFields.has(descriptor.sourceField))
315
+ ? [source]
316
+ : [...new Set(expanded.map((target) => target.source))],
317
+ );
318
+ } else if (source !== undefined) {
319
+ const selected = expandedBySourceField.get(descriptor.sourceField);
320
+ const explicitAnchor = input[descriptor.field];
321
+ anchors.set(
322
+ descriptor.field,
323
+ selected !== undefined && typeof explicitAnchor === "string" && explicitAnchor.length > 0
324
+ ? [...new Set(selected.map((target) => target.source))]
325
+ : [source],
326
+ );
327
+ }
328
+ }
329
+ const targets = [...expandedBySourceField.values()]
330
+ .flat()
331
+ .reduce<TextTarget[]>((all, target) => mergeTargets(all, [target]), []);
332
+ return {
333
+ fields,
334
+ anchors,
335
+ implicitTargets,
336
+ targets,
337
+ resources: [...new Set([...fields.values(), ...targets.map((target) => target.source)])],
338
+ };
339
+ }
184
340
 
185
- const fields = new Map<string, string>();
186
- const sourceDescriptors = [definition.source, ...(definition.source.targets ?? [])];
187
-
188
- for (const descriptor of sourceDescriptors)
189
- {
190
- const explicit = input[descriptor.field];
191
- const explicitSource = typeof explicit === "string" && explicit.length > 0 ? explicit : undefined;
192
- const fallback = "fallbackTo" in descriptor ? fields.get(descriptor.fallbackTo) : undefined;
193
- const expanded = expandedBySourceField.get(descriptor.field);
194
-
195
- if (expanded !== undefined)
196
- {
197
- if (explicitSource === undefined)
198
- {
199
- fields.set(descriptor.field, expanded[0]!);
200
- }
201
- else
202
- {
203
- if (expanded.length !== 1)
204
- {
205
- throw new Error(
206
- `${descriptor.field} must be omitted when a search anchor selects multiple resources.`,
207
- );
208
- }
209
-
210
- if (!sameResource(explicitSource, expanded[0]!, context.cwd))
211
- {
212
- throw new Error(`Anchor does not belong to ${explicitSource}.`);
213
- }
214
-
215
- fields.set(descriptor.field, explicitSource);
216
- }
217
-
218
- continue;
219
- }
220
-
221
- const source = explicitSource ?? fallback;
222
-
223
- if (source === undefined)
224
- {
225
- throw new Error(`${descriptor.field} is required`);
226
- }
341
+ function sameTargetSources(left: readonly TextTarget[], right: readonly TextTarget[]): boolean {
342
+ const leftSources = new Set(left.map((target) => target.source));
343
+ const rightSources = new Set(right.map((target) => target.source));
344
+ return (
345
+ leftSources.size === rightSources.size &&
346
+ [...leftSources].every((source) => rightSources.has(source))
347
+ );
348
+ }
227
349
 
228
- fields.set(descriptor.field, source);
350
+ function mergeTargets(left: readonly TextTarget[], right: readonly TextTarget[]): TextTarget[] {
351
+ const merged = new Map<string, TextTarget>();
352
+ for (const target of [...left, ...right]) {
353
+ const previous = merged.get(target.source);
354
+ if (previous === undefined) {
355
+ merged.set(target.source, target);
356
+ continue;
229
357
  }
358
+ const ranges = [...(previous.ranges ?? []), ...(target.ranges ?? [])];
359
+ merged.set(target.source, {
360
+ source: target.source,
361
+ ...(ranges.length > 0 && { ranges: deduplicateRanges(ranges) }),
362
+ });
363
+ }
364
+ return [...merged.values()];
365
+ }
230
366
 
231
- const anchors = new Map<string, readonly string[]>();
367
+ function deduplicateRanges(ranges: readonly TextSelectionRange[]): readonly TextSelectionRange[] {
368
+ const merged = new Map<string, TextSelectionRange>();
369
+ for (const range of ranges) {
370
+ const key = JSON.stringify({ start: range.start, end: range.end });
371
+ const previous = merged.get(key);
372
+ merged.set(key, {
373
+ ...range,
374
+ ...(previous?.linewise === true || range.linewise === true ? { linewise: true } : {}),
375
+ });
376
+ }
377
+ return [...merged.values()];
378
+ }
232
379
 
233
- for (const descriptor of definition.anchors ?? [])
234
- {
235
- const expanded = expandedByAnchor.get(descriptor.field);
236
- const source = fields.get(descriptor.sourceField);
380
+ function naturalLineSelection(content: string, lineNumber: number): TextSelectionRange {
381
+ const lines = content.split(/\r\n|\r|\n/u);
382
+ const line = lines[lineNumber - 1];
383
+ if (line === undefined) {
384
+ throw new Error(`Position anchor line ${lineNumber} is outside the source.`);
385
+ }
386
+ const hasFollowingLine = lineNumber < lines.length;
387
+ return {
388
+ start: { lineNumber, column: 0 },
389
+ end: hasFollowingLine
390
+ ? { lineNumber: lineNumber + 1, column: 0 }
391
+ : { lineNumber, column: line.length },
392
+ linewise: true,
393
+ };
394
+ }
237
395
 
238
- if (expanded !== undefined)
239
- {
240
- anchors.set(
241
- descriptor.field,
242
- source !== undefined && expanded.length === 1 ? [source] : expanded,
243
- );
244
- }
245
- else if (source !== undefined)
246
- {
247
- anchors.set(descriptor.field, [source]);
396
+ function mergeResolvedAnchors(
397
+ documentFor: (source: string) => TextChangeDocument,
398
+ field: string,
399
+ explicit: ReadonlyMap<string, TextAnchor>,
400
+ implicit: ReadonlyMap<string, TextSelectionAnchor>,
401
+ ): ReadonlyMap<string, TextAnchor> {
402
+ const sources = new Set([...explicit.keys(), ...implicit.keys()]);
403
+ return new Map(
404
+ [...sources].map((source) => {
405
+ const explicitAnchor = explicit.get(source);
406
+ const implicitAnchor = implicit.get(source);
407
+ if (explicitAnchor === undefined) return [source, requiredValue(implicitAnchor)] as const;
408
+ if (implicitAnchor === undefined) return [source, explicitAnchor] as const;
409
+
410
+ const explicitSelection = TextSelectionAnchor.is(explicitAnchor)
411
+ ? explicitAnchor
412
+ : new TextSelectionAnchor(field, source, [
413
+ naturalLineSelection(documentFor(source).content, explicitAnchor.lineNumber),
414
+ ]);
415
+ const ranges = [...explicitSelection.ranges, ...implicitAnchor.ranges].sort(
416
+ (left, right) =>
417
+ left.start.lineNumber - right.start.lineNumber || left.start.column - right.start.column,
418
+ );
419
+ const unique = deduplicateRanges(ranges);
420
+ for (let index = 1; index < unique.length; index++) {
421
+ const previous = requiredValue(unique[index - 1]);
422
+ const current = requiredValue(unique[index]);
423
+ if (
424
+ current.start.lineNumber < previous.end.lineNumber ||
425
+ (current.start.lineNumber === previous.end.lineNumber &&
426
+ current.start.column < previous.end.column)
427
+ ) {
428
+ throw new Error(`Anchor ${field} selections overlap or are ambiguous.`);
248
429
  }
249
- }
430
+ }
431
+ return [source, new TextSelectionAnchor(field, source, unique)] as const;
432
+ }),
433
+ );
434
+ }
435
+ interface MutationExecutionContext extends TextMutationContext {
436
+ resultPresentationFor(source: string): MutationResultPresentation;
437
+ }
250
438
 
251
- return {
252
- fields,
253
- anchors,
254
- resources: [...new Set([...fields.values(), ...[...anchors.values()].flat()])],
255
- };
439
+ interface ExecutedTextMutation extends TextMutation {
440
+ readonly resultPresentations: ReadonlyMap<string, MutationResultPresentation>;
256
441
  }
257
442
 
258
443
  function createMutationContext(
259
- definition: TextMutationToolRegistration,
260
- input: Readonly<Record<string, unknown>>,
261
- invocation: { readonly cwd: string; readonly signal?: AbortSignal; },
262
- sources: ResolvedMutationSources,
263
- texts: ReadonlyMap<string, string>,
264
- resolveResourceAnchor: ResolveResourceTextAnchor,
265
- ): TextMutationContext
266
- {
267
- const documents = new Map([...texts].map(([source, text]) => [source, new TextChangeDocument(text)]));
268
- const anchorCache = new Map<string, Promise<ReadonlyMap<string, TextAnchor>>>();
269
- const sourceFor = (field: string): string =>
270
- {
271
- const source = sources.fields.get(field);
444
+ definition: TextMutationToolRegistration,
445
+ input: Readonly<Record<string, unknown>>,
446
+ invocation: { readonly cwd: string; readonly signal?: AbortSignal },
447
+ sources: ResolvedMutationSources,
448
+ texts: ReadonlyMap<string, string>,
449
+ resolveResourceAnchor: ResolveResourceTextAnchor,
450
+ publishAnchorRenderState?: (field: string, state: ToolCallAnchorRenderState) => void,
451
+ ): MutationExecutionContext {
452
+ const documents = new Map(
453
+ [...texts].map(([source, text]) => [source, new TextChangeDocument(text)]),
454
+ );
455
+ const anchorCache = new Map<string, Promise<ReadonlyMap<string, TextAnchor>>>();
456
+
457
+ const majorAnchorSources = new Set<string>();
458
+ const sourceFor = (field: string): string => {
459
+ const source = sources.fields.get(field);
460
+
461
+ if (source === undefined) {
462
+ throw new Error(`Unknown mutation source field ${field}`);
463
+ }
272
464
 
273
- if (source === undefined)
274
- {
275
- throw new Error(`Unknown mutation source field ${field}`);
276
- }
465
+ return source;
466
+ };
467
+ const documentFor = (source: string): TextChangeDocument => {
468
+ const document = documents.get(source);
277
469
 
278
- return source;
279
- };
280
- const documentFor = (source: string): TextChangeDocument =>
281
- {
282
- const document = documents.get(source);
470
+ if (document === undefined) {
471
+ throw new Error(`Unknown mutation resource ${source}`);
472
+ }
283
473
 
284
- if (document === undefined)
285
- {
286
- throw new Error(`Unknown mutation resource ${source}`);
287
- }
474
+ return document;
475
+ };
476
+ const resolveAnchors = (field: string): Promise<ReadonlyMap<string, TextAnchor>> => {
477
+ const cached = anchorCache.get(field);
288
478
 
289
- return document;
290
- };
291
- const resolveAnchors = (field: string): Promise<ReadonlyMap<string, TextAnchor>> =>
292
- {
293
- const cached = anchorCache.get(field);
479
+ if (cached !== undefined) {
480
+ return cached;
481
+ }
294
482
 
295
- if (cached !== undefined)
296
- {
297
- return cached;
298
- }
483
+ const pending = (async () => {
484
+ const descriptor = (definition.anchors ?? []).find((anchor) => anchor.field === field);
485
+ const value = input[field];
486
+ const anchorSources = sources.anchors.get(field);
487
+ const implicitTargets = sources.implicitTargets.get(field);
299
488
 
300
- const pending = (async () =>
301
- {
302
- const descriptor = (definition.anchors ?? []).find((anchor) => anchor.field === field);
303
- const value = input[field];
304
- const anchorSources = sources.anchors.get(field);
305
-
306
- if (descriptor === undefined || typeof value !== "string" || anchorSources === undefined)
307
- {
308
- throw new Error(`Mutation tool ${definition.name} tried to resolve undeclared anchor ${field}`);
309
- }
310
-
311
- return new Map(
312
- await Promise.all(anchorSources.map(async (source) =>
313
- {
314
- try
315
- {
316
- return [source, await resolveResourceAnchor(source, value, descriptor.kinds)] as const;
317
- }
318
- catch (error)
319
- {
320
- throw contextualizeTextMutationAnchorError(error, definition.name, field, source, value);
489
+ if (descriptor === undefined) {
490
+ throw new Error(
491
+ `Mutation tool ${definition.name} tried to resolve undeclared anchor ${field}`,
492
+ );
493
+ }
494
+
495
+ const explicitAnchors =
496
+ typeof value === "string" && anchorSources !== undefined
497
+ ? new Map(
498
+ await Promise.all(
499
+ anchorSources.map(async (source) => {
500
+ try {
501
+ const anchor = await resolveResourceAnchor(source, value, descriptor.kinds);
502
+
503
+ if (resolvedTextAnchorType(anchor) === "major") {
504
+ majorAnchorSources.add(source);
321
505
  }
322
- })),
323
- );
324
- })();
325
- anchorCache.set(field, pending);
326
- return pending;
327
- };
506
+ const rendered = renderTextAnchor(anchor, value, { source, anchor });
507
+ publishAnchorRenderState?.(field, {
508
+ kind: "resolved",
509
+ full: rendered.full,
510
+ compact: rendered.compact,
511
+ resolverId: rendered.resolverId,
512
+ });
513
+ return [source, anchor] as const;
514
+ } catch (error) {
515
+ publishAnchorRenderState?.(field, { kind: "failed" });
516
+ throw contextualizeTextMutationAnchorError(
517
+ error,
518
+ definition.name,
519
+ field,
520
+ source,
521
+ value,
522
+ );
523
+ }
524
+ }),
525
+ ),
526
+ )
527
+ : undefined;
528
+
529
+ if (implicitTargets !== undefined) {
530
+ const implicit = new Map(
531
+ implicitTargets.map(
532
+ (target) =>
533
+ [
534
+ target.source,
535
+ new TextSelectionAnchor(field, target.source, target.ranges ?? []),
536
+ ] as const,
537
+ ),
538
+ );
539
+ if (explicitAnchors === undefined) return implicit;
540
+ return mergeResolvedAnchors(documentFor, field, explicitAnchors, implicit);
541
+ }
328
542
 
329
- return {
330
- cwd: invocation.cwd,
331
- ...(invocation.signal === undefined ? {} : { signal: invocation.signal }),
332
- sourceDocument: documentFor(sourceFor(definition.source.field)),
333
- sourceFor,
334
- documentFor,
335
- targetDocument(field): TextChangeDocument
336
- {
337
- if (!(definition.source.targets ?? []).some((target) => target.field === field))
338
- {
339
- throw new Error(`Mutation tool ${definition.name} tried to access undeclared target ${field}`);
340
- }
543
+ if (explicitAnchors === undefined) {
544
+ throw new Error(
545
+ `Mutation tool ${definition.name} tried to resolve undeclared anchor ${field}`,
546
+ );
547
+ }
548
+ return explicitAnchors;
549
+ })();
550
+ anchorCache.set(field, pending);
551
+ return pending;
552
+ };
553
+
554
+ return {
555
+ cwd: invocation.cwd,
556
+ ...(invocation.signal !== undefined && { signal: invocation.signal }),
557
+ sourceDocument: documentFor(sourceFor(definition.source.field)),
558
+ sourceFor,
559
+ documentFor,
560
+ targetDocument(field): TextChangeDocument {
561
+ if ((definition.source.targets ?? []).every((target) => target.field !== field)) {
562
+ throw new Error(
563
+ `Mutation tool ${definition.name} tried to access undeclared target ${field}`,
564
+ );
565
+ }
341
566
 
342
- return documentFor(sourceFor(field));
343
- },
344
- resolveAnchors,
345
- async resolveAnchor(field): Promise<TextAnchor>
346
- {
347
- const resolved = await resolveAnchors(field);
567
+ return documentFor(sourceFor(field));
568
+ },
569
+ resolveAnchors,
570
+ async resolveAnchor(field): Promise<TextAnchor> {
571
+ const resolved = await resolveAnchors(field);
348
572
 
349
- if (resolved.size !== 1)
350
- {
351
- throw new Error(
352
- `Anchor ${field} selects multiple resources; use an operation that supports a search set.`,
353
- );
354
- }
573
+ if (resolved.size !== 1) {
574
+ throw new Error(
575
+ `Anchor ${field} selects multiple resources; use an operation that supports a search set.`,
576
+ );
577
+ }
578
+
579
+ return requiredValue(resolved.values().next().value);
580
+ },
581
+
582
+ resultPresentationFor(source): MutationResultPresentation {
583
+ return [...majorAnchorSources].some((candidate) =>
584
+ sameResource(candidate, source, invocation.cwd),
585
+ )
586
+ ? "major-anchor"
587
+ : "plain";
588
+ },
589
+ };
590
+ }
355
591
 
356
- return resolved.values().next().value!;
357
- },
358
- };
592
+ function withResultPresentations(
593
+ mutation: TextMutation,
594
+ context: MutationExecutionContext,
595
+ ): ExecutedTextMutation {
596
+ return {
597
+ ...mutation,
598
+ resultPresentations: new Map(
599
+ [...mutation.edits.keys()].map((source) => [source, context.resultPresentationFor(source)]),
600
+ ),
601
+ };
359
602
  }
360
603
 
361
- function sameResources(left: readonly string[], right: readonly string[], cwd: string): boolean
362
- {
363
- const leftKeys = left.map((source) => resourceIdentity(source, cwd)).sort();
364
- const rightKeys = right.map((source) => resourceIdentity(source, cwd)).sort();
365
- return leftKeys.length === rightKeys.length && leftKeys.every((key, index) => key === rightKeys[index]);
604
+ const preflightMutationTools = new Set(["replace", "delete", "insert", "copy", "move"]);
605
+ /** Resolves every supplied anchor field for built-in span mutation tools. */
606
+ export async function preflightMutationAnchors(
607
+ definition: TextMutationToolRegistration,
608
+ input: Readonly<Record<string, unknown>>,
609
+ context: TextMutationContext,
610
+ ): Promise<void> {
611
+ if (!preflightMutationTools.has(definition.name)) {
612
+ return;
613
+ }
614
+ const failures: TextMutationAnchorResolutionError[] = [];
615
+ await Promise.all(
616
+ (definition.anchors ?? []).map(async (descriptor) => {
617
+ if (!isMutationAnchorValue(descriptor, input[descriptor.field])) {
618
+ return;
619
+ }
620
+ try {
621
+ await context.resolveAnchors(descriptor.field);
622
+ } catch (error) {
623
+ if (error instanceof TextMutationAnchorResolutionError) {
624
+ failures.push(error);
625
+ return;
626
+ }
627
+ throw error;
628
+ }
629
+ }),
630
+ );
631
+ if (failures.length > 0) {
632
+ throw new TextMutationAnchorAggregateError(failures);
633
+ }
366
634
  }
367
635
 
368
- function sameResource(left: string, right: string, cwd: string): boolean
369
- {
370
- return resourceIdentity(left, cwd) === resourceIdentity(right, cwd);
636
+ function sameResource(left: string, right: string, cwd: string): boolean {
637
+ return resourceIdentity(left, cwd) === resourceIdentity(right, cwd);
371
638
  }
372
639
 
373
- function resourceIdentity(source: string, cwd: string): string
374
- {
375
- if (/^[a-z][a-z\d+.-]*:\/\//iu.test(source))
376
- {
377
- return source;
378
- }
640
+ function resourceIdentity(source: string, cwd: string): string {
641
+ if (/^[a-z][a-z\d+.-]*:\/\//iu.test(source)) {
642
+ return source;
643
+ }
379
644
 
380
- const fileSource = source.startsWith("@") ? source.slice(1) : source;
381
- return path.resolve(cwd, fileSource);
645
+ const fileSource = source.startsWith("@") ? source.slice(1) : source;
646
+ return path.resolve(cwd, fileSource);
382
647
  }
383
648
 
384
649
  export async function previewTextMutation(
385
- core: TextEditorCore,
386
- request: TextMutationPreviewRequest,
387
- ): Promise<TextMutationPreviewOutcome>
388
- {
389
- const definition = core.getMutationTools().find((candidate) => candidate.name === request.tool);
650
+ core: TextEditorCore,
651
+ request: TextMutationPreviewRequest,
652
+ ): Promise<TextMutationPreviewOutcome> {
653
+ const definition = core.getMutationTools().find((candidate) => candidate.name === request.tool);
654
+
655
+ if (definition === undefined) {
656
+ return { kind: "failed", reason: `Unknown mutation tool ${request.tool}` };
657
+ }
658
+
659
+ try {
660
+ const sources = await resolveMutationSources(core, definition, request.input, request);
661
+ return await core.previewTexts(
662
+ sources.resources.map((source) => ({
663
+ source,
664
+ read: true,
665
+ ...(definition.name === "write" && { allowReadFailure: true }),
666
+ })),
667
+ { cwd: request.cwd, ...(request.signal !== undefined && { signal: request.signal }) },
668
+ async (texts, resolveAnchor) => {
669
+ const mutationContext = createMutationContext(
670
+ definition,
671
+ request.input,
672
+ request,
673
+ sources,
674
+ texts,
675
+ resolveAnchor,
676
+ );
677
+ await preflightMutationAnchors(definition, request.input, mutationContext);
678
+ const mutation = await definition.mutate(mutationContext, request.input);
390
679
 
391
- if (definition === undefined)
392
- {
393
- return { kind: "failed", reason: `Unknown mutation tool ${request.tool}` };
394
- }
680
+ return {
681
+ changes: new Map([...mutation.edits].map(([source, edit]) => [source, edit.changes])),
682
+ result: mutation,
683
+ };
684
+ },
685
+ );
686
+ } catch (error) {
687
+ return { kind: "failed", reason: error instanceof Error ? error.message : String(error) };
688
+ }
689
+ }
395
690
 
396
- try
691
+ /** Executes a mutation, using read history only when neither a source nor an anchor owns it. */
692
+ export async function executeTextMutation<TParameters extends TSchema>(
693
+ core: TextEditorCore,
694
+ definition: TextMutationToolRegistration<TParameters>,
695
+ parameters: Static<TParameters>,
696
+ signal: AbortSignal | undefined,
697
+ context: ExtensionContext,
698
+ publishAnchorRenderState?: (field: string, state: ToolCallAnchorRenderState) => void,
699
+ lastResolvedSource?: string,
700
+ ): Promise<AgentToolResult<FileMutationBatchResult>> {
701
+ const source = mutationSource(definition, parameters);
702
+ const pipeline = await core.executeEdit(
703
+ definition.name,
397
704
  {
398
- const sources = await resolveMutationSources(core, definition, request.input, request);
399
- return await core.previewTexts(
400
- sources.resources.map((source) => ({
401
- source,
402
- read: true,
403
- ...(definition.name === "write" ? { allowReadFailure: true } : {}),
404
- })),
405
- { cwd: request.cwd, ...(request.signal === undefined ? {} : { signal: request.signal }) },
406
- async (texts, resolveAnchor) =>
407
- {
408
- const mutation = await definition.mutate(
409
- createMutationContext(definition, request.input, request, sources, texts, resolveAnchor),
410
- request.input,
411
- );
412
-
413
- return {
414
- changes: new Map([...mutation.edits].map(([source, edit]) => [source, edit.changes])),
415
- result: mutation,
416
- };
417
- },
705
+ cwd: context.cwd,
706
+ input: parameters,
707
+ ...(signal !== undefined && { signal }),
708
+ },
709
+ async (state) => {
710
+ try {
711
+ const sources = await resolveMutationSources(
712
+ core,
713
+ definition,
714
+ state.input,
715
+ state,
716
+ lastResolvedSource,
418
717
  );
419
- }
420
- catch (error)
421
- {
422
- return { kind: "failed", reason: error instanceof Error ? error.message : String(error) };
423
- }
424
- }
425
-
426
- export async function executeTextMutation<TParams extends TSchema>(
427
- core: TextEditorCore,
428
- definition: TextMutationToolRegistration<TParams>,
429
- params: Static<TParams>,
430
- signal: AbortSignal | undefined,
431
- context: ExtensionContext,
432
- ): Promise<AgentToolResult<FileMutationBatchResult>>
433
- {
434
- const source = mutationSource(definition, params);
435
- const pipeline = await core.executeEdit(
436
- definition.name,
437
- {
438
- cwd: context.cwd,
439
- input: params,
440
- ...(signal === undefined ? {} : { signal }),
441
- },
442
- async (state) =>
443
- {
444
- try
445
- {
446
- const sources = await resolveMutationSources(core, definition, state.input, state);
447
- const outcome = await core.editTexts(
448
- sources.resources.map((resourceSource) => ({
449
- source: resourceSource,
450
- read: true,
451
- ...(definition.name === "write" ? { allowReadFailure: true } : {}),
452
- })),
453
- {
454
- cwd: state.cwd,
455
- intent: definition.intent ?? "edit",
456
- ...(state.signal === undefined ? {} : { signal: state.signal }),
457
- },
458
- async (texts, resolveAnchor) =>
459
- {
460
- const mutation = await definition.mutate(
461
- createMutationContext(definition, state.input, state, sources, texts, resolveAnchor),
462
- state.input,
463
- );
464
- return {
465
- changes: new Map(
466
- [...mutation.edits].map(([editSource, edit]) => [editSource, edit.changes]),
467
- ),
468
- result: mutation,
469
- };
470
- },
471
- );
472
- return await completeTextMutation(outcome, mutationSource(definition, state.input));
473
- }
474
- catch (error)
475
- {
476
- return failedResourceEdit(
477
- mutationSource(definition, state.input),
478
- "INVALID_REQUEST",
479
- errorMessage(error),
480
- error,
481
- );
482
- }
483
- },
484
- );
718
+ const outcome = await core.editTexts(
719
+ sources.resources.map((resourceSource) => ({
720
+ source: resourceSource,
721
+ read: true,
722
+ ...(definition.name === "write" && { allowReadFailure: true }),
723
+ })),
724
+ {
725
+ cwd: state.cwd,
726
+ intent: definition.intent ?? "edit",
727
+ ...(state.signal !== undefined && { signal: state.signal }),
728
+ },
729
+ async (texts, resolveAnchor) => {
730
+ const mutationContext = createMutationContext(
731
+ definition,
732
+ state.input,
733
+ state,
734
+ sources,
735
+ texts,
736
+ resolveAnchor,
737
+ publishAnchorRenderState,
738
+ );
739
+ await preflightMutationAnchors(definition, state.input, mutationContext);
740
+ const mutation = await definition.mutate(mutationContext, state.input);
741
+ const executedMutation = withResultPresentations(mutation, mutationContext);
742
+ return {
743
+ changes: new Map(
744
+ [...mutation.edits].map(([editSource, edit]) => [editSource, edit.changes]),
745
+ ),
746
+ result: executedMutation,
747
+ };
748
+ },
749
+ );
750
+ return await completeTextMutation(outcome, mutationSource(definition, state.input));
751
+ } catch (error) {
752
+ return failedResourceEdit(
753
+ mutationSource(definition, state.input),
754
+ "INVALID_REQUEST",
755
+ errorMessage(error),
756
+ error,
757
+ );
758
+ }
759
+ },
760
+ );
485
761
 
486
- return buildToolResult(core, pipeline, source, context);
762
+ return buildToolResult(core, pipeline, source, context);
487
763
  }
488
764
 
489
765
  export function mutationSources(
490
- definition: TextMutationToolRegistration,
491
- params: Readonly<Record<string, unknown>>,
492
- ): ReadonlyMap<string, string>
493
- {
494
- const sources = new Map<string, string>();
495
- const primary = params[definition.source.field];
496
-
497
- if (typeof primary !== "string" || primary.length === 0)
498
- {
499
- throw new Error(`${definition.source.field} is required`);
500
- }
766
+ definition: TextMutationToolRegistration,
767
+ parameters: Readonly<Record<string, unknown>>,
768
+ ): ReadonlyMap<string, string> {
769
+ const sources = new Map<string, string>();
770
+ const primary = parameters[definition.source.field];
501
771
 
502
- sources.set(definition.source.field, primary);
772
+ if (typeof primary !== "string" || primary.length === 0) {
773
+ throw new Error(`${definition.source.field} is required`);
774
+ }
503
775
 
504
- for (const target of definition.source.targets ?? [])
505
- {
506
- const explicit = params[target.field];
507
- const fallback = sources.get(target.fallbackTo);
508
- const source = typeof explicit === "string" && explicit.length > 0 ? explicit : fallback;
776
+ sources.set(definition.source.field, primary);
509
777
 
510
- if (source === undefined)
511
- {
512
- throw new Error(`${target.field} is required`);
513
- }
778
+ for (const target of definition.source.targets ?? []) {
779
+ const explicit = parameters[target.field];
780
+ const fallback = sources.get(target.fallbackTo);
781
+ const source = typeof explicit === "string" && explicit.length > 0 ? explicit : fallback;
514
782
 
515
- sources.set(target.field, source);
783
+ if (source === undefined) {
784
+ throw new Error(`${target.field} is required`);
516
785
  }
517
786
 
518
- return sources;
787
+ sources.set(target.field, source);
788
+ }
789
+
790
+ return sources;
519
791
  }
520
792
 
521
- type CompletedTextResource = Extract<TextResourceEditOutcome<unknown>, { readonly kind: "completed"; }>;
793
+ type CompletedTextResource = Extract<
794
+ TextResourceEditOutcome<unknown>,
795
+ { readonly kind: "completed" }
796
+ >;
522
797
 
523
798
  export function buildSuccessfulTextMutationResult(
524
- resource: CompletedTextResource,
525
- resultSource: string,
526
- edit: TextMutationEdit,
527
- diffAfterContent = resource.after.content,
528
- ): FileMutationResult
529
- {
530
- const before = resource.before.content;
531
- const finalAfter = resource.after.content;
532
- const applied = applyTextChanges(before, edit.changes);
533
- const unified = createUnifiedDiff(resultSource, before, diffAfterContent);
534
- const mutationResultData = resource.postEditContributions
535
- .map((contribution) => contribution.data)
536
- .find(isTextMutationResultContributionData);
537
- const rawChanges = applied.changes.map((change, editIndex) => ({
538
- editIndex,
539
- fromA: change.fromBefore,
540
- toA: change.toBefore,
541
- fromB: change.fromAfter,
542
- toB: change.toAfter,
543
- removedText: change.removedText,
544
- insertedText: change.insertedText,
545
- }));
546
-
547
- return new FileMutationResult({
548
- ...mutationResultData,
549
- ok: true,
550
- path: resultSource,
551
- diffs: [unified.diff],
552
- files: [{ path: resultSource, action: edit.action }],
553
- editCount: 1,
554
- addedLines: unified.stats.added,
555
- removedLines: unified.stats.removed,
556
- beforeContentMap: { [resultSource]: before },
557
- afterContent: finalAfter,
558
- rawChanges,
559
- afterDocument: resource.after,
560
- });
799
+ resource: CompletedTextResource,
800
+ resultSource: string,
801
+ edit: TextMutationEdit,
802
+ diffAfterContent = resource.after.content,
803
+ resultPresentation: MutationResultPresentation = "plain",
804
+ editCount = 1,
805
+ ): FileMutationResult {
806
+ const before = resource.before.content;
807
+ const finalAfter = resource.after.content;
808
+ const applied = applyTextChanges(before, edit.changes);
809
+ const unified = createUnifiedDiff(resultSource, before, diffAfterContent);
810
+ const mutationResultData = resource.postEditContributions
811
+ .map((contribution) => contribution.data)
812
+ .find(isTextMutationResultContributionData);
813
+ const rawChanges = applied.changes.map((change, editIndex) => ({
814
+ editIndex,
815
+ fromA: change.fromBefore,
816
+ toA: change.toBefore,
817
+ fromB: change.fromAfter,
818
+ toB: change.toAfter,
819
+ removedText: change.removedText,
820
+ insertedText: change.insertedText,
821
+ }));
822
+
823
+ return new FileMutationResult({
824
+ ...mutationResultData,
825
+ ok: true,
826
+ path: resultSource,
827
+ diffs: [unified.diff],
828
+ files: [{ path: resultSource, action: edit.action }],
829
+ editCount,
830
+ addedLines: unified.stats.added,
831
+ removedLines: unified.stats.removed,
832
+ beforeContentMap: { [resultSource]: before },
833
+ afterContent: finalAfter,
834
+ rawChanges,
835
+ afterDocument: resource.after,
836
+
837
+ resultPresentation,
838
+ });
561
839
  }
562
840
 
563
841
  async function completeTextMutation(
564
- outcome: TextResourcesEditOutcome<TextMutation>,
565
- source: string,
566
- ): Promise<TextResourcesEditOutcome<TextMutation>>
567
- {
568
- if (outcome.kind === "failed" || outcome.result.afterWrite === undefined)
569
- {
570
- return outcome;
571
- }
572
-
573
- try
574
- {
575
- await outcome.result.afterWrite();
576
- return outcome;
577
- }
578
- catch (error)
579
- {
580
- return {
581
- kind: "failed",
582
- failure: {
583
- code: "POST_WRITE_FAILED",
584
- source,
585
- message: `Post-write action failed for ${source}`,
586
- cause: error,
587
- },
588
- completed: outcome.resources.map((resource) => resource.source),
589
- };
590
- }
842
+ outcome: TextResourcesEditOutcome<TextMutation>,
843
+ source: string,
844
+ ): Promise<TextResourcesEditOutcome<TextMutation>> {
845
+ if (outcome.kind === "failed" || outcome.result.afterWrite === undefined) {
846
+ return outcome;
847
+ }
848
+
849
+ try {
850
+ await outcome.result.afterWrite();
851
+ return outcome;
852
+ } catch (error) {
853
+ return {
854
+ kind: "failed",
855
+ failure: {
856
+ code: "POST_WRITE_FAILED",
857
+ source,
858
+ message: `Post-write action failed for ${source}`,
859
+ cause: error,
860
+ },
861
+ completed: outcome.resources.map((resource) => resource.source),
862
+ };
863
+ }
591
864
  }
592
865
 
593
866
  async function buildToolResult(
594
- core: TextEditorCore,
595
- pipeline: TextEditExecutionOutcome,
596
- source: string,
597
- context: ExtensionContext,
598
- ): Promise<AgentToolResult<FileMutationBatchResult>>
599
- {
600
- if (pipeline.kind === "failed")
601
- {
602
- return failureToolResult(source, pipeline.failure.code, pipeline.failure.message, "not-applied");
603
- }
604
-
605
- const outcome = pipeline.state.result;
867
+ core: TextEditorCore,
868
+ pipeline: TextEditExecutionOutcome,
869
+ source: string,
870
+ context: ExtensionContext,
871
+ ): Promise<AgentToolResult<FileMutationBatchResult>> {
872
+ if (pipeline.kind === "failed") {
873
+ return failureToolResult(
874
+ source,
875
+ pipeline.failure.code,
876
+ pipeline.failure.message,
877
+ "not-applied",
878
+ );
879
+ }
606
880
 
607
- if (!isResourcesEditOutcome(outcome))
608
- {
609
- return failureToolResult(source, "INVALID_RESULT", "Text editor returned an invalid edit result.", "unknown");
610
- }
881
+ const outcome = pipeline.state.result;
611
882
 
612
- if (outcome.kind === "failed")
613
- {
614
- const recovery = await anchorFailureToolResult(core, outcome.failure, context);
883
+ if (!isResourcesEditOutcome(outcome)) {
884
+ return failureToolResult(
885
+ source,
886
+ "INVALID_RESULT",
887
+ "Text editor returned an invalid edit result.",
888
+ "unknown",
889
+ );
890
+ }
615
891
 
616
- if (recovery !== undefined)
617
- {
618
- return recovery;
619
- }
892
+ if (outcome.kind === "failed") {
893
+ const recovery = await anchorFailureToolResult(core, outcome.failure, context);
620
894
 
621
- const completed = outcome.completed.length === 0 ? "" : ` Completed writes: ${outcome.completed.join(", ")}.`;
622
- const effect = outcome.completed.length === 0 ? "not-applied" : "applied";
623
- const message = `${outcome.failure.message.replace(/[.!?]+$/u, "")}.${completed}`;
624
- return failureToolResult(source, outcome.failure.code, message, effect);
895
+ if (recovery !== undefined) {
896
+ return recovery;
625
897
  }
626
898
 
627
- const mutation = outcome.result as TextMutation;
628
- const results = outcome.resources.flatMap((resource) =>
629
- {
630
- const resultSource = resource.source;
631
- const edit = mutation.edits.get(resultSource);
632
- return edit === undefined ? [] : [buildSuccessfulTextMutationResult(resource, resultSource, edit)];
633
- });
634
-
635
- return {
636
- content: [new FileMutationAgentResult(results).toTextContent()],
637
- details: { results },
638
- };
899
+ const completed =
900
+ outcome.completed.length === 0 ? "" : ` Completed writes: ${outcome.completed.join(", ")}.`;
901
+ const effect = outcome.completed.length === 0 ? "not-applied" : "applied";
902
+ const message = `${outcome.failure.message.replace(/[.!?]+$/u, "")}.${completed}`;
903
+ return failureToolResult(source, outcome.failure.code, message, effect);
904
+ }
905
+
906
+ const mutation = outcome.result as ExecutedTextMutation;
907
+ const results = outcome.resources.flatMap((resource) => {
908
+ const resultSource = resource.source;
909
+ const edit = mutation.edits.get(resultSource);
910
+ return edit === undefined
911
+ ? []
912
+ : [
913
+ buildSuccessfulTextMutationResult(
914
+ resource,
915
+ resultSource,
916
+ edit,
917
+ undefined,
918
+ mutation.resultPresentations.get(resultSource) ?? "plain",
919
+ ),
920
+ ];
921
+ });
922
+
923
+ return {
924
+ content: [new FileMutationAgentResult(results).toTextContent()],
925
+ details: { results },
926
+ };
639
927
  }
640
928
 
641
929
  async function anchorFailureToolResult(
642
- core: TextEditorCore,
643
- failure: TextResourceEditFailure,
644
- context: ExtensionContext,
645
- ): Promise<AgentToolResult<FileMutationBatchResult> | undefined>
646
- {
647
- if (!(failure.cause instanceof TextMutationAnchorResolutionError))
648
- {
649
- return undefined;
930
+ core: TextEditorCore,
931
+ failure: TextResourceEditFailure,
932
+ context: ExtensionContext,
933
+ ): Promise<AgentToolResult<FileMutationBatchResult> | undefined> {
934
+ if (failure.cause instanceof TextMutationAnchorAggregateError) {
935
+ const recovered = await Promise.all(
936
+ failure.cause.failures.map((cause) =>
937
+ anchorFailureToolResult(core, { ...failure, source: cause.source, cause }, context),
938
+ ),
939
+ );
940
+ const results: FileMutationResult[] = [];
941
+ const anchorRecoveries: NonNullable<FileMutationBatchResult["anchorRecoveries"]>[number][] = [];
942
+ const messages: string[] = [];
943
+ for (const item of recovered) {
944
+ if (item === undefined) {
945
+ continue;
946
+ }
947
+ results.push(...(item.details.results ?? []));
948
+ anchorRecoveries.push(...(item.details.anchorRecoveries ?? []));
949
+ for (const block of item.content) {
950
+ if (block.type === "text") {
951
+ messages.push(block.text);
952
+ }
953
+ }
650
954
  }
651
-
652
- const contextual = failure.cause;
653
- const resolution = contextual.resolution;
654
- const range = resolution.rejection?.contextRange;
655
-
656
- if (range === undefined)
657
- {
658
- return undefined;
955
+ if (results.length === 0) {
956
+ return undefined;
659
957
  }
660
-
661
- const read = await readTextAnchorRecovery(core, { path: contextual.source, ...range }, {
662
- cwd: context.cwd,
663
- ...(context.signal === undefined ? {} : { signal: context.signal }),
664
- });
665
-
666
- if (read === undefined || read.isError === true)
667
- {
668
- return undefined;
958
+ return {
959
+ content: [{ type: "text", text: messages.join("\n\n") }],
960
+ details: { results, anchorRecoveries },
961
+ };
962
+ }
963
+ if (!(failure.cause instanceof TextMutationAnchorResolutionError)) {
964
+ return undefined;
965
+ }
966
+
967
+ const contextual = failure.cause;
968
+ const resolution = contextual.resolution;
969
+ if (resolution.recovery === undefined) {
970
+ await resolution.refreshRecovery();
971
+ }
972
+ const recovery = resolution.recovery;
973
+ if (recovery?.kind === "timed-out") {
974
+ return failureToolResult(
975
+ contextual.source,
976
+ failure.code,
977
+ `${failure.message}; recovery timed out`,
978
+ "not-applied",
979
+ );
980
+ }
981
+
982
+ const windows =
983
+ recovery?.kind === "candidates"
984
+ ? recoveryWindows(
985
+ recovery.candidates.map(({ range }) => range),
986
+ core.recoveryContextLines(),
987
+ )
988
+ : resolution.rejection?.contextRange === undefined
989
+ ? []
990
+ : [resolution.rejection.contextRange];
991
+ if (windows.length === 0) {
992
+ return undefined;
993
+ }
994
+
995
+ const reads = await Promise.all(
996
+ windows.map((range) =>
997
+ readTextAnchorRecovery(
998
+ core,
999
+ { path: contextual.source, ...range },
1000
+ {
1001
+ cwd: context.cwd,
1002
+ ...(context.signal !== undefined && { signal: context.signal }),
1003
+ },
1004
+ ),
1005
+ ),
1006
+ );
1007
+ const recoveryTexts: string[] = [];
1008
+ for (const read of reads) {
1009
+ if (read === undefined || read.isError === true) {
1010
+ return undefined;
669
1011
  }
1012
+ recoveryTexts.push(
1013
+ ...read.content.filter((block) => block.type === "text").map((block) => block.text),
1014
+ );
1015
+ }
1016
+
1017
+ const result = new FileMutationResult({
1018
+ ok: false,
1019
+ path: contextual.source,
1020
+ errors: [{ path: contextual.source, code: failure.code, reason: failure.message }],
1021
+ });
1022
+ const recoveryContext = recoveryTexts.join("\n");
1023
+ const rejectionCode = resolution.rejection?.code;
1024
+ const content =
1025
+ rejectionCode === "stale"
1026
+ ? formatStaleAnchorMessage(
1027
+ {
1028
+ guardId: "stale-anchor",
1029
+ effect: "not-applied",
1030
+ toolName: contextual.toolName,
1031
+ field: contextual.field,
1032
+ path: contextual.source,
1033
+ anchor: contextual.anchor,
1034
+ ...(recoveryContext.length > 0 && { context: recoveryContext }),
1035
+ } satisfies StaleAnchorMessageDetails,
1036
+ resolution.message,
1037
+ )
1038
+ : formatRejectedAnchorMessage(contextual, rejectionCode, resolution.message, recoveryContext);
1039
+
1040
+ return {
1041
+ content: [{ type: "text", text: content }],
1042
+ details: {
1043
+ results: [result],
1044
+ effect: "not-applied",
1045
+ anchorRecovery: true,
1046
+ ...(recovery?.kind === "candidates" && {
1047
+ anchorRecoveries: [
1048
+ {
1049
+ field: contextual.field,
1050
+ path: contextual.source,
1051
+ anchor: contextual.anchor,
1052
+ total: recovery.total,
1053
+ candidates: recovery.candidates,
1054
+ },
1055
+ ],
1056
+ }),
1057
+ } as FileMutationBatchResult,
1058
+ };
1059
+ }
670
1060
 
671
- const result = new FileMutationResult({
672
- ok: false,
673
- path: contextual.source,
674
- errors: [{ path: contextual.source, code: failure.code, reason: failure.message }],
675
- });
676
- const recoveryContext = read.content
677
- .filter((block) => block.type === "text")
678
- .map((block) => block.text)
679
- .join("\n");
680
- const details: StaleAnchorMessageDetails = {
681
- guardId: "stale-anchor",
682
- effect: "not-applied",
683
- toolName: contextual.toolName,
684
- field: contextual.field,
685
- path: contextual.source,
686
- anchor: contextual.anchor,
687
- ...(recoveryContext.length === 0 ? {} : { context: recoveryContext }),
688
- };
1061
+ function formatRejectedAnchorMessage(
1062
+ failure: TextMutationAnchorResolutionError,
1063
+ code: TextAnchorRejection["code"] | undefined,
1064
+ reason: string,
1065
+ recoveryContext: string,
1066
+ ): string {
1067
+ const state =
1068
+ code === "ambiguous"
1069
+ ? "is ambiguous"
1070
+ : code === "missing"
1071
+ ? "was not found"
1072
+ : code === "invalid"
1073
+ ? "is invalid"
1074
+ : "could not be resolved";
1075
+ const guidance =
1076
+ code === "ambiguous"
1077
+ ? "Use a unique text span or one of the candidate anchors below."
1078
+ : "If the intended text is represented below, use its candidate anchor. Otherwise, reread the relevant section and choose a current anchor.";
1079
+ const context = recoveryContext.length === 0 ? "" : `\n\n${recoveryContext}`;
1080
+ return `[SYSTEM] ${failure.toolName} blocked: ${failure.field} anchor "${failure.anchor}" ${state}. ${guidance} (${reason})${context}`;
1081
+ }
689
1082
 
690
- return {
691
- content: [{ type: "text", text: formatStaleAnchorMessage(details, resolution.message) }],
692
- details: {
693
- results: [result],
694
- effect: "not-applied",
695
- anchorRecovery: true,
696
- } as FileMutationBatchResult,
697
- };
1083
+ function recoveryWindows(
1084
+ ranges: readonly TextAnchorRecoveryCandidateRange[],
1085
+ contextLines: number,
1086
+ ): TextAnchorRecoveryRange[] {
1087
+ const windows = ranges
1088
+ .map((range) => ({
1089
+ offset: Math.max(1, range.start.lineNumber - contextLines),
1090
+ limit: range.end.lineNumber - range.start.lineNumber + 1 + contextLines * 2,
1091
+ }))
1092
+ .sort((left, right) => left.offset - right.offset);
1093
+ const merged: TextAnchorRecoveryRange[] = [];
1094
+ for (const window of windows) {
1095
+ const previous = merged[merged.length - 1];
1096
+ if (previous === undefined || window.offset > previous.offset + previous.limit) {
1097
+ merged.push(window);
1098
+ continue;
1099
+ }
1100
+ const end = Math.max(previous.offset + previous.limit, window.offset + window.limit);
1101
+ merged[merged.length - 1] = { offset: previous.offset, limit: end - previous.offset };
1102
+ }
1103
+ return merged;
698
1104
  }
699
1105
 
700
1106
  export async function buildFailedTextMutationResult(
701
- core: TextEditorCore,
702
- failure: TextResourceEditFailure,
703
- context: ExtensionContext,
704
- effect: "applied" | "not-applied" | "unknown" = "not-applied",
705
- ): Promise<AgentToolResult<FileMutationBatchResult>>
706
- {
707
- return effect === "not-applied"
708
- ? await anchorFailureToolResult(core, failure, context)
709
- ?? failureToolResult(failure.source, failure.code, failure.message, effect)
710
- : failureToolResult(failure.source, failure.code, failure.message, effect);
1107
+ core: TextEditorCore,
1108
+ failure: TextResourceEditFailure,
1109
+ context: ExtensionContext,
1110
+ effect: "applied" | "not-applied" | "unknown" = "not-applied",
1111
+ ): Promise<AgentToolResult<FileMutationBatchResult>> {
1112
+ return effect === "not-applied"
1113
+ ? ((await anchorFailureToolResult(core, failure, context)) ??
1114
+ failureToolResult(failure.source, failure.code, failure.message, effect))
1115
+ : failureToolResult(failure.source, failure.code, failure.message, effect);
711
1116
  }
712
1117
 
713
1118
  function failureToolResult(
714
- source: string,
715
- code: string,
716
- reason: string,
717
- effect: "not-applied" | "applied" | "unknown",
718
- ): { content: [{ type: "text"; text: string; }]; details: FileMutationBatchResult; }
719
- {
720
- const result = new FileMutationResult({
721
- ok: false,
722
- path: source,
723
- errors: [{ path: source, code, reason }],
724
- });
725
-
726
- return {
727
- content: [new FileMutationAgentResult(result).toTextContent()],
728
- details: { results: [result], effect } as FileMutationBatchResult,
729
- };
1119
+ source: string,
1120
+ code: string,
1121
+ reason: string,
1122
+ effect: "not-applied" | "applied" | "unknown",
1123
+ ): { content: [{ type: "text"; text: string }]; details: FileMutationBatchResult } {
1124
+ const result = new FileMutationResult({
1125
+ ok: false,
1126
+ path: source,
1127
+ errors: [{ path: source, code, reason }],
1128
+ });
1129
+
1130
+ return {
1131
+ content: [new FileMutationAgentResult(result).toTextContent()],
1132
+ details: { results: [result], effect } as FileMutationBatchResult,
1133
+ };
730
1134
  }
731
1135
 
732
1136
  function failedResourceEdit(
733
- source: string,
734
- code: TextResourceEditFailure["code"],
735
- message: string,
736
- cause?: unknown,
737
- ): TextResourcesEditOutcome<never>
738
- {
739
- return {
740
- kind: "failed",
741
- failure: { code, source, message, ...(cause === undefined ? {} : { cause }) },
742
- completed: [],
743
- };
1137
+ source: string,
1138
+ code: TextResourceEditFailure["code"],
1139
+ message: string,
1140
+ cause?: unknown,
1141
+ ): TextResourcesEditOutcome<never> {
1142
+ return {
1143
+ kind: "failed",
1144
+ failure: { code, source, message, ...(cause !== undefined && { cause }) },
1145
+ completed: [],
1146
+ };
1147
+ }
1148
+
1149
+ function isResourcesEditOutcome(value: unknown): value is TextResourcesEditOutcome<unknown> {
1150
+ return (
1151
+ typeof value === "object" &&
1152
+ value !== null &&
1153
+ ((value as { kind?: unknown }).kind === "completed" ||
1154
+ (value as { kind?: unknown }).kind === "failed")
1155
+ );
744
1156
  }
745
1157
 
746
- function isResourcesEditOutcome(value: unknown): value is TextResourcesEditOutcome<unknown>
747
- {
748
- return typeof value === "object"
749
- && value !== null
750
- && ((value as { kind?: unknown; }).kind === "completed" || (value as { kind?: unknown; }).kind === "failed");
1158
+ function asMutationParameters<TParameters extends TSchema>(value: unknown): Static<TParameters> {
1159
+ // oxlint-disable-next-line typescript/no-unsafe-return -- TypeBox resolves only concrete tool schemas.
1160
+ return value as Static<TParameters>;
751
1161
  }
752
1162
 
753
- function errorMessage(error: unknown): string
754
- {
755
- return error instanceof Error ? error.message : String(error);
1163
+ function errorMessage(error: unknown): string {
1164
+ return error instanceof Error ? error.message : String(error);
756
1165
  }