pi-agent-ide 0.7.1 → 0.7.2

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 (275) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/dist/chunk-6U5OU77O.js +3 -0
  3. package/dist/chunk-EURHXLN3.js +2 -0
  4. package/dist/{chunk-WMO6RNE2.js → chunk-F6IWZKOG.js} +6 -6
  5. package/dist/{chunk-HZEY4GT4.js → chunk-H445SHSJ.js} +2 -2
  6. package/dist/chunk-IG56UGSO.js +2 -0
  7. package/dist/chunk-IICJXZBU.js +6 -0
  8. package/dist/{chunk-XYS4FSCC.js → chunk-IM24DB3K.js} +1 -1
  9. package/dist/{chunk-PKKDDIWW.js → chunk-KOVPDKSJ.js} +1 -1
  10. package/dist/chunk-MXLSGMRZ.js +3 -0
  11. package/dist/chunk-OB46OPMZ.js +1 -0
  12. package/dist/chunk-PKSQT6TC.js +1 -0
  13. package/dist/{chunk-PNU3E7IO.js → chunk-Q6CZ6GBR.js} +2 -2
  14. package/dist/chunk-QUYXINGZ.js +7 -0
  15. package/dist/{chunk-OMVRLWMX.js → chunk-SBP3ABH4.js} +1 -1
  16. package/dist/{chunk-4GCWGE2C.js → chunk-SJ4UZ4GU.js} +1 -1
  17. package/dist/{chunk-2TRHFMCZ.js → chunk-SNF7PDDX.js} +1 -1
  18. package/dist/{chunk-YP46DVVP.js → chunk-STBJO3Z3.js} +1 -1
  19. package/dist/{chunk-76ZK5ODR.js → chunk-TQGE7RH6.js} +13 -13
  20. package/dist/{chunk-LEEQIQ7D.js → chunk-XDG7EQRN.js} +6 -6
  21. package/dist/chunk-YWQS33DF.js +1 -0
  22. package/dist/chunk-ZIWPKIBZ.js +1 -0
  23. package/dist/entry-11.js +1 -1
  24. package/dist/entry-13.js +1 -1
  25. package/dist/entry-14.js +1 -1
  26. package/dist/entry-15.js +1 -1
  27. package/dist/entry-17.js +1 -1
  28. package/dist/entry-25.js +1 -1
  29. package/dist/entry-26.js +1 -1
  30. package/dist/entry-27.js +1 -1
  31. package/dist/entry-28.js +1 -1
  32. package/dist/entry-29.js +1 -1
  33. package/dist/entry-31.js +13 -12
  34. package/dist/entry-33.js +2 -1
  35. package/dist/entry-37.js +2 -2
  36. package/dist/entry-42.js +1 -1
  37. package/dist/entry-44.js +1 -1
  38. package/dist/entry-45.js +20 -12
  39. package/dist/entry-46.js +1 -1
  40. package/dist/entry-49.js +1 -1
  41. package/dist/entry-50.js +1 -1
  42. package/dist/entry-51.js +7 -6
  43. package/dist/entry-52.js +1 -1
  44. package/dist/entry-53.js +1 -1
  45. package/dist/entry-54.js +1 -1
  46. package/dist/entry-56.js +1 -1
  47. package/dist/entry-58.js +1 -1
  48. package/dist/entry-60.js +1 -1
  49. package/dist/entry-62.js +1 -1
  50. package/dist/entry-65.js +1 -1
  51. package/dist/entry-66.js +1 -1
  52. package/dist/entry-68.js +1 -1
  53. package/dist/entry-70.js +1 -1
  54. package/dist/entry-75.js +1 -1
  55. package/dist/entry-77.js +51 -42
  56. package/dist/entry-78.js +14 -14
  57. package/dist/entry-79.js +1 -1
  58. package/dist/entry-82.js +5 -5
  59. package/dist/entry-83.js +1 -1
  60. package/dist/entry-84.js +1 -1
  61. package/dist/entry-85.js +1 -1
  62. package/dist/entry-86.js +1 -1
  63. package/dist/pi-agent-ide.js +34 -11
  64. package/docs/agent-guides/editing.md +5 -5
  65. package/docs/agent-guides/git-changes.md +10 -0
  66. package/docs/agent-guides/read-resources.md +9 -46
  67. package/docs/agent-guides/search-code.md +7 -1
  68. package/docs/agent-guides/select-code.md +1 -0
  69. package/docs/agent-guides/terminal.md +9 -1
  70. package/docs/configuration.md +1 -1
  71. package/docs/releases.md +4 -0
  72. package/docs/research/native-codemode-batch-decision.md +2 -0
  73. package/docs/structured-results.md +2 -2
  74. package/docs/tools.md +10 -2
  75. package/node_modules/pi-agent-doctor/package.json +1 -1
  76. package/node_modules/pi-agent-documentation/package.json +1 -1
  77. package/node_modules/pi-agent-filesystem/package.json +7 -7
  78. package/node_modules/pi-agent-filesystem-image/package.json +3 -3
  79. package/node_modules/pi-agent-filesystem-jq/package.json +5 -5
  80. package/node_modules/pi-agent-filesystem-jq/src/jq.ts +4 -1
  81. package/node_modules/pi-agent-filesystem-pdf/package.json +3 -3
  82. package/node_modules/pi-agent-filesystem-text/package.json +3 -3
  83. package/node_modules/pi-agent-ide-ast/package.json +13 -13
  84. package/node_modules/pi-agent-ide-ast/src/extension.ts +0 -2
  85. package/node_modules/pi-agent-ide-ast/src/overflow-handler.ts +2 -2
  86. package/node_modules/pi-agent-ide-ast/src/search-resolver.ts +0 -3
  87. package/node_modules/pi-agent-ide-ast/src/select-schema.ts +8 -6
  88. package/node_modules/pi-agent-ide-ast/src/tool-select.ts +13 -5
  89. package/node_modules/pi-agent-ide-changes/package.json +8 -8
  90. package/node_modules/pi-agent-ide-changes/src/tool-index-change.ts +66 -4
  91. package/node_modules/pi-agent-ide-changes/src/tool-text-undo.ts +8 -11
  92. package/node_modules/pi-agent-ide-formatter/package.json +3 -3
  93. package/node_modules/pi-agent-ide-languages/package.json +2 -2
  94. package/node_modules/pi-agent-ide-lint/package.json +7 -7
  95. package/node_modules/pi-agent-ide-lsp/package.json +9 -9
  96. package/node_modules/pi-agent-ide-lsp/src/search-resolver.ts +0 -1
  97. package/node_modules/pi-agent-image/package.json +2 -2
  98. package/node_modules/pi-agent-invariant/package.json +1 -1
  99. package/node_modules/pi-agent-pdf/package.json +2 -2
  100. package/node_modules/pi-agent-read/package.json +7 -7
  101. package/node_modules/pi-agent-read/src/api/read-parameters.ts +3 -3
  102. package/node_modules/pi-agent-read/src/api/tools/read.ts +11 -0
  103. package/node_modules/pi-agent-read/src/core/extension.ts +1 -1
  104. package/node_modules/pi-agent-read/src/core/read-core.ts +5 -1
  105. package/node_modules/pi-agent-read/src/core/tools/read/output-truncation.ts +91 -18
  106. package/node_modules/pi-agent-read/src/core/tools/read/raw-read.ts +8 -2
  107. package/node_modules/pi-agent-read/src/core/tools/read/read-renderer.ts +38 -1
  108. package/node_modules/pi-agent-read/src/core/tools/read/read-result.ts +57 -2
  109. package/node_modules/pi-agent-read/src/core/tools/tool-read.ts +99 -44
  110. package/node_modules/pi-agent-resource/package.json +1 -1
  111. package/node_modules/pi-agent-resource/src/index.ts +1 -0
  112. package/node_modules/pi-agent-resource/src/result-targets.ts +6 -3
  113. package/node_modules/{pi-agent-read/src/core/tools/read → pi-agent-resource/src}/temp-resource-store.ts +17 -7
  114. package/node_modules/pi-agent-search/package.json +5 -5
  115. package/node_modules/pi-agent-search/src/api/fuzzy.ts +5 -1
  116. package/node_modules/pi-agent-search/src/api/search-parameters.ts +2 -2
  117. package/node_modules/pi-agent-search/src/core/extension.ts +2 -1
  118. package/node_modules/pi-agent-search/src/core/search-core.ts +13 -4
  119. package/node_modules/pi-agent-search-text/package.json +7 -7
  120. package/node_modules/pi-agent-search-text/src/resolvers.ts +11 -1
  121. package/node_modules/pi-agent-search-text/src/scoped-search.ts +1 -1
  122. package/node_modules/pi-agent-search-text/src/search-presentation.ts +42 -32
  123. package/node_modules/pi-agent-search-text/src/search-recipe.ts +1 -1
  124. package/node_modules/pi-agent-search-text/src/search-renderer.ts +6 -0
  125. package/node_modules/pi-agent-search-text/src/search-result.ts +17 -3
  126. package/node_modules/pi-agent-text/package.json +2 -2
  127. package/node_modules/pi-agent-text-anchor-constant/package.json +3 -3
  128. package/node_modules/pi-agent-text-anchor-exact/package.json +3 -3
  129. package/node_modules/pi-agent-text-anchor-line-hash/package.json +5 -5
  130. package/node_modules/pi-agent-text-editor/package.json +9 -9
  131. package/node_modules/pi-agent-text-editor/src/api/edit-completion.ts +2 -2
  132. package/node_modules/pi-agent-text-editor/src/api/mutation-result.ts +1 -2
  133. package/node_modules/pi-agent-text-editor/src/api/native-edit-batch-event.ts +4 -0
  134. package/node_modules/pi-agent-text-editor/src/api/stale-anchor-message.ts +1 -1
  135. package/node_modules/pi-agent-text-editor/src/core/diff-tool.ts +54 -12
  136. package/node_modules/pi-agent-text-editor/src/core/editing-guidelines.ts +1 -1
  137. package/node_modules/pi-agent-text-editor/src/core/mutation-result/file-mutation-agent-result.ts +35 -6
  138. package/node_modules/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +19 -1
  139. package/node_modules/pi-agent-text-editor/src/core/mutation-result-targets.ts +132 -11
  140. package/node_modules/pi-agent-text-editor/src/core/native-text-edit-batch.ts +232 -151
  141. package/node_modules/pi-agent-text-editor/src/core/post-edit-scope.ts +34 -10
  142. package/node_modules/pi-agent-text-editor/src/core/result-input.ts +24 -4
  143. package/node_modules/pi-agent-text-editor/src/core/result-transfer.ts +7 -2
  144. package/node_modules/pi-agent-text-editor/src/core/structured-result.ts +14 -13
  145. package/node_modules/pi-agent-text-editor/src/core/text-change-engine.ts +5 -1
  146. package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +152 -22
  147. package/node_modules/pi-agent-text-editor/src/core/text-edit-batch.ts +4 -0
  148. package/node_modules/pi-agent-text-editor/src/core/text-editor-core.ts +76 -32
  149. package/node_modules/pi-agent-text-editor/src/core/text-mutation.ts +381 -187
  150. package/node_modules/pi-agent-text-editor/src/tools/text-tool-schema-properties.ts +4 -2
  151. package/node_modules/pi-agent-text-editor/src/tools/tool-text-copy.ts +9 -2
  152. package/node_modules/pi-agent-text-editor/src/tools/tool-text-delete.ts +9 -4
  153. package/node_modules/pi-agent-text-editor/src/tools/tool-text-insert.ts +7 -8
  154. package/node_modules/pi-agent-text-editor/src/tools/tool-text-replace.ts +2 -3
  155. package/node_modules/pi-agent-text-editor/src/tools/tool-text-write.ts +10 -4
  156. package/node_modules/pi-agent-text-editor-renderer/package.json +5 -5
  157. package/node_modules/pi-agent-text-editor-renderer/src/mutation-panel.ts +11 -0
  158. package/node_modules/pi-agent-text-editor-renderer/src/renderer.ts +54 -5
  159. package/node_modules/pi-agent-text-editor-renderer/src/status-summary.ts +18 -3
  160. package/node_modules/pi-agent-text-editor-stale-anchor/package.json +4 -4
  161. package/node_modules/pi-agent-tool-call-interception/package.json +2 -2
  162. package/node_modules/pi-agent-tool-ui/package.json +1 -1
  163. package/node_modules/pi-agent-web/package.json +7 -7
  164. package/node_modules/pi-agent-web/src/resolver.ts +11 -13
  165. package/node_modules/pi-agent-web-html/package.json +2 -2
  166. package/node_modules/pi-agent-web-image/package.json +3 -3
  167. package/node_modules/pi-agent-web-pdf/package.json +3 -3
  168. package/node_modules/pi-agent-web-text/package.json +3 -3
  169. package/package.json +35 -35
  170. package/src/composite/nested-tool-rendering.ts +64 -6
  171. package/src/composite/output-limits.ts +125 -0
  172. package/src/composite/responses-tools.ts +25 -0
  173. package/src/composite/text-results.ts +158 -7
  174. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/package.json +7 -7
  175. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/package.json +3 -3
  176. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-jq/package.json +5 -5
  177. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-jq/src/jq.ts +4 -1
  178. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-pdf/package.json +3 -3
  179. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/package.json +3 -3
  180. package/src/extensions/pi-agent-read/extensions/pi-agent-web/package.json +7 -7
  181. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/package.json +2 -2
  182. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-image/package.json +3 -3
  183. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-pdf/package.json +3 -3
  184. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/package.json +3 -3
  185. package/src/extensions/pi-agent-read/extensions/pi-agent-web/src/resolver.ts +11 -13
  186. package/src/extensions/pi-agent-read/package.json +7 -7
  187. package/src/extensions/pi-agent-read/src/api/read-parameters.ts +3 -3
  188. package/src/extensions/pi-agent-read/src/api/tools/read.ts +11 -0
  189. package/src/extensions/pi-agent-read/src/core/extension.ts +1 -1
  190. package/src/extensions/pi-agent-read/src/core/read-core.ts +5 -1
  191. package/src/extensions/pi-agent-read/src/core/tools/read/output-truncation.ts +91 -18
  192. package/src/extensions/pi-agent-read/src/core/tools/read/raw-read.ts +8 -2
  193. package/src/extensions/pi-agent-read/src/core/tools/read/read-renderer.ts +38 -1
  194. package/src/extensions/pi-agent-read/src/core/tools/read/read-result.ts +57 -2
  195. package/src/extensions/pi-agent-read/src/core/tools/tool-read.ts +99 -44
  196. package/src/extensions/pi-agent-search/package.json +5 -5
  197. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/package.json +7 -7
  198. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/resolvers.ts +11 -1
  199. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/scoped-search.ts +1 -1
  200. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-presentation.ts +42 -32
  201. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-recipe.ts +1 -1
  202. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-renderer.ts +6 -0
  203. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-result.ts +17 -3
  204. package/src/extensions/pi-agent-search/src/api/fuzzy.ts +5 -1
  205. package/src/extensions/pi-agent-search/src/api/search-parameters.ts +2 -2
  206. package/src/extensions/pi-agent-search/src/core/extension.ts +2 -1
  207. package/src/extensions/pi-agent-search/src/core/search-core.ts +13 -4
  208. package/src/extensions/pi-agent-text-editor/package.json +9 -9
  209. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/package.json +3 -3
  210. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/package.json +3 -3
  211. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/package.json +5 -5
  212. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/package.json +5 -5
  213. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/mutation-panel.ts +11 -0
  214. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/renderer.ts +54 -5
  215. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/status-summary.ts +18 -3
  216. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/package.json +4 -4
  217. package/src/extensions/pi-agent-text-editor/src/api/edit-completion.ts +2 -2
  218. package/src/extensions/pi-agent-text-editor/src/api/mutation-result.ts +1 -2
  219. package/src/extensions/pi-agent-text-editor/src/api/native-edit-batch-event.ts +4 -0
  220. package/src/extensions/pi-agent-text-editor/src/api/stale-anchor-message.ts +1 -1
  221. package/src/extensions/pi-agent-text-editor/src/core/diff-tool.ts +54 -12
  222. package/src/extensions/pi-agent-text-editor/src/core/editing-guidelines.ts +1 -1
  223. package/src/extensions/pi-agent-text-editor/src/core/mutation-result/file-mutation-agent-result.ts +35 -6
  224. package/src/extensions/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +19 -1
  225. package/src/extensions/pi-agent-text-editor/src/core/mutation-result-targets.ts +132 -11
  226. package/src/extensions/pi-agent-text-editor/src/core/native-text-edit-batch.ts +232 -151
  227. package/src/extensions/pi-agent-text-editor/src/core/post-edit-scope.ts +34 -10
  228. package/src/extensions/pi-agent-text-editor/src/core/result-input.ts +24 -4
  229. package/src/extensions/pi-agent-text-editor/src/core/result-transfer.ts +7 -2
  230. package/src/extensions/pi-agent-text-editor/src/core/structured-result.ts +14 -13
  231. package/src/extensions/pi-agent-text-editor/src/core/text-change-engine.ts +5 -1
  232. package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +152 -22
  233. package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch.ts +4 -0
  234. package/src/extensions/pi-agent-text-editor/src/core/text-editor-core.ts +76 -32
  235. package/src/extensions/pi-agent-text-editor/src/core/text-mutation.ts +381 -187
  236. package/src/extensions/pi-agent-text-editor/src/tools/text-tool-schema-properties.ts +4 -2
  237. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-copy.ts +9 -2
  238. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-delete.ts +9 -4
  239. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-insert.ts +7 -8
  240. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-replace.ts +2 -3
  241. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-write.ts +10 -4
  242. package/src/pi-agent-ide.ts +11 -0
  243. package/src/plugins/pi-agent-ide-ast/package.json +13 -13
  244. package/src/plugins/pi-agent-ide-ast/src/extension.ts +0 -2
  245. package/src/plugins/pi-agent-ide-ast/src/overflow-handler.ts +2 -2
  246. package/src/plugins/pi-agent-ide-ast/src/search-resolver.ts +0 -3
  247. package/src/plugins/pi-agent-ide-ast/src/select-schema.ts +8 -6
  248. package/src/plugins/pi-agent-ide-ast/src/tool-select.ts +13 -5
  249. package/src/plugins/pi-agent-ide-changes/package.json +8 -8
  250. package/src/plugins/pi-agent-ide-changes/src/tool-index-change.ts +66 -4
  251. package/src/plugins/pi-agent-ide-changes/src/tool-text-undo.ts +8 -11
  252. package/src/plugins/pi-agent-ide-debugger/index.ts +17 -7
  253. package/src/plugins/pi-agent-ide-debugger/src/dap-client.ts +5 -0
  254. package/src/plugins/pi-agent-ide-debugger/src/session-manager.ts +93 -38
  255. package/src/plugins/pi-agent-ide-diagnostics/src/extension.ts +3 -2
  256. package/src/plugins/pi-agent-ide-formatter/package.json +3 -3
  257. package/src/plugins/pi-agent-ide-languages/package.json +2 -2
  258. package/src/plugins/pi-agent-ide-lint/package.json +7 -7
  259. package/src/plugins/pi-agent-ide-lsp/package.json +9 -9
  260. package/src/plugins/pi-agent-ide-lsp/src/search-resolver.ts +0 -1
  261. package/src/plugins/pi-agent-ide-terminal/src/output-limits.ts +3 -3
  262. package/src/plugins/pi-agent-ide-terminal/src/tools.ts +3 -3
  263. package/src/plugins/pi-agent-ide-vision/index.ts +1 -0
  264. package/src/post-edit/gate.ts +13 -2
  265. package/dist/chunk-4XMY4H4V.js +0 -1
  266. package/dist/chunk-64M3SQPG.js +0 -3
  267. package/dist/chunk-CWD4O5PM.js +0 -3
  268. package/dist/chunk-D3WTDA6V.js +0 -2
  269. package/dist/chunk-EAZGN5ND.js +0 -7
  270. package/dist/chunk-GXEGY7GV.js +0 -1
  271. package/dist/chunk-MBUXFKIW.js +0 -3
  272. package/dist/chunk-PGIVHICI.js +0 -1
  273. package/dist/chunk-VMTR74FF.js +0 -2
  274. package/dist/chunk-WU6ZK6YD.js +0 -1
  275. package/src/extensions/pi-agent-read/src/core/tools/read/temp-resource-store.ts +0 -105
@@ -1,10 +1,14 @@
1
1
  import { AsyncLocalStorage } from "node:async_hooks";
2
+ import { requiredValue } from "pi-agent-invariant";
2
3
  import { ResourceScheduler, resourceAccesses } from "pi-agent-resource";
3
4
  import type { TextResourceEditOutcome } from "./text-editor-core.js";
4
5
 
5
6
  type Completed = Exclude<TextResourceEditOutcome<unknown>, { readonly kind: "failed" }>;
6
7
  type Finalize = () => Promise<Completed>;
7
- const active = new AsyncLocalStorage<Map<string, Finalize>>();
8
+ const active = new AsyncLocalStorage<{
9
+ readonly pending: Map<string, Finalize>;
10
+ readonly immediate: boolean;
11
+ }>();
8
12
  const notifications = new AsyncLocalStorage<Array<() => void>>();
9
13
 
10
14
  /** Schedule observers only after every file in the current finalization has settled. */
@@ -27,27 +31,32 @@ export async function collectPostEditNotifications<T>(work: () => Promise<T>): P
27
31
 
28
32
  /** Replace intermediate post-processing with the latest written state of this resource. */
29
33
  export function deferPostEdit(source: string, finalize: Finalize): boolean {
30
- const pending = active.getStore();
31
- if (!pending) return false;
32
- pending.set(source, finalize);
34
+ const scope = active.getStore();
35
+ if (!scope) return false;
36
+ if (scope.immediate) {
37
+ scope.pending.delete(source);
38
+ return false;
39
+ }
40
+ scope.pending.set(source, finalize);
33
41
  return true;
34
42
  }
35
43
 
36
44
  /** Optional read enrichment must not start checks on an intermediate edited snapshot. */
37
45
  export function hasDeferredPostEdit(source: string): boolean {
38
- return active.getStore()?.has(source) ?? false;
46
+ return active.getStore()?.pending.has(source) ?? false;
39
47
  }
40
48
 
41
49
  /** Stop deferred processing when a later operation removes or moves the resource. */
42
50
  export function forgetDeferredPostEdit(source: string): void {
43
- active.getStore()?.delete(source);
51
+ active.getStore()?.pending.delete(source);
44
52
  }
45
53
  /** Writes stay immediate; finishing drains each surviving final resource once. */
46
54
  export function createPostEditScope(cwd = process.cwd()) {
47
55
  const pending = new Map<string, Finalize>();
48
56
  return {
49
- run<T>(operation: () => T): T {
50
- return active.run(pending, operation);
57
+ /** Immediate processing replaces older deferred work for the same written resource. */
58
+ run<T>(operation: () => T, immediate = false): T {
59
+ return active.run({ pending, immediate }, operation);
51
60
  },
52
61
  forget(source: string): void {
53
62
  pending.delete(source);
@@ -56,7 +65,11 @@ export function createPostEditScope(cwd = process.cwd()) {
56
65
  sources(): readonly string[] {
57
66
  return [...pending.keys()];
58
67
  },
59
- async finish(onCompleted?: (outcome: Completed) => void): Promise<Completed[]> {
68
+ /** Report each final resource outcome; a failed finalization does not undo its write. */
69
+ async finish(
70
+ onCompleted?: (outcome: Completed) => void,
71
+ onFailed?: (source: string, error: unknown) => void,
72
+ ): Promise<Completed[]> {
60
73
  const work = [...pending];
61
74
  pending.clear();
62
75
  const completed: Completed[] = [];
@@ -68,9 +81,15 @@ export function createPostEditScope(cwd = process.cwd()) {
68
81
  scheduler.run(resourceAccesses(source, cwd, "write"), finalize),
69
82
  ),
70
83
  );
71
- for (const outcome of outcomes) {
84
+ for (const [index, outcome] of outcomes.entries()) {
85
+ const source = requiredValue(work[index])[0];
72
86
  if (outcome.status === "rejected") {
73
87
  errors.push(outcome.reason);
88
+ try {
89
+ onFailed?.(source, outcome.reason);
90
+ } catch (error) {
91
+ errors.push(error);
92
+ }
74
93
  continue;
75
94
  }
76
95
  completed.push(outcome.value);
@@ -78,6 +97,11 @@ export function createPostEditScope(cwd = process.cwd()) {
78
97
  onCompleted?.(outcome.value);
79
98
  } catch (error) {
80
99
  errors.push(error);
100
+ try {
101
+ onFailed?.(source, error);
102
+ } catch (failure) {
103
+ errors.push(failure);
104
+ }
81
105
  }
82
106
  }
83
107
  });
@@ -1,12 +1,30 @@
1
1
  import type { ResolvedResultTargets } from "pi-agent-resource";
2
2
  import { TextChangeDocument } from "./text-change-engine.js";
3
3
 
4
- /** Require a verified selection of one complete file before a whole-file operation. */
5
- export function wholeFileResultSource(result: ResolvedResultTargets): string {
4
+ const writePartialReason =
5
+ "Write received a partial file selection. Use replace to edit that selection, or Read the whole file before deliberately replacing all its contents.";
6
+
7
+ /** Require one complete file; name Write to provide its specific recovery instructions. */
8
+ export function wholeFileResultSource(
9
+ result: ResolvedResultTargets,
10
+ operation?: "write" | "undo",
11
+ ): string {
6
12
  const target = result.targets[0];
7
13
  const range = target?.ranges[0];
8
- if (result.targets.length !== 1 || target?.ranges.length !== 1 || range === undefined)
14
+ if (result.targets.length !== 1 || target?.ranges.length !== 1 || range === undefined) {
15
+ if (operation === "write") {
16
+ if (result.targets.length === 0)
17
+ throw new Error(
18
+ "Write received no file selection. Read the intended whole file before replacing it.",
19
+ );
20
+ if (result.targets.length > 1)
21
+ throw new Error(
22
+ "Write received multiple files. Choose one file and Read its whole contents before replacing it.",
23
+ );
24
+ throw new Error(writePartialReason);
25
+ }
9
26
  throw new Error("This operation requires one whole-file result target.");
27
+ }
10
28
  const document = new TextChangeDocument(target.expectedContent);
11
29
  const selected = document.range(
12
30
  range.start.lineNumber,
@@ -16,7 +34,9 @@ export function wholeFileResultSource(result: ResolvedResultTargets): string {
16
34
  );
17
35
  if (selected.from !== 0 || selected.to !== document.length)
18
36
  throw new Error(
19
- "This operation requires one whole-file result target; partial scopes are not widened.",
37
+ operation === "write"
38
+ ? writePartialReason
39
+ : "This operation requires one whole-file result target; partial scopes are not widened.",
20
40
  );
21
41
  return target.source;
22
42
  }
@@ -46,7 +46,7 @@ export async function prepareResultTransfer(
46
46
  const selected = store.resolveOrdered(input[field], cwd);
47
47
  if (!selected.complete)
48
48
  throw new Error(
49
- "Incomplete result targets cannot establish a complete transfer; repeat Search.",
49
+ "Incomplete result targets cannot establish a complete transfer. Run Search again with a higher limit or a narrower query, then use the complete result.",
50
50
  );
51
51
  await store.verify(selected, signal);
52
52
  input[field] = store.register(selected.targets, cwd);
@@ -168,7 +168,12 @@ export async function prepareResultTransfer(
168
168
  for (const span of sources)
169
169
  append(span, { from: span.from, to: span.to, insert: "" }, false);
170
170
  }
171
- for (const transfer of planned) append(transfer.target, transfer.change, true);
171
+ for (const transfer of planned)
172
+ append(
173
+ transfer.target,
174
+ operation === "copy" ? { ...transfer.change, allowUnchanged: true } : transfer.change,
175
+ true,
176
+ );
172
177
  return { edits };
173
178
  },
174
179
  };
@@ -1,7 +1,6 @@
1
1
  import { Type, type Static } from "typebox";
2
2
  import {
3
3
  resultError,
4
- resultErrorSchema,
5
4
  structuredResultSchema,
6
5
  withStructuredResult,
7
6
  type StructuredResult,
@@ -36,7 +35,7 @@ export function mutationDataSchema(operation: string) {
36
35
  operation: Type.Literal(operation),
37
36
  ...receipt,
38
37
  operationId: Type.Optional(Type.String()),
39
- ...(operation === "delete" || operation === "flush"
38
+ ...(operation === "delete"
40
39
  ? {}
41
40
  : {
42
41
  target: Type.Optional(Type.String()),
@@ -50,17 +49,18 @@ export function mutationDataSchema(operation: string) {
50
49
  export function mutationResultSchema(operation: string) {
51
50
  return structuredResultSchema(mutationDataSchema(operation));
52
51
  }
53
- const batchOperation = Type.Object(
54
- { id: Type.String(), operation: Type.String(), effect, errors: Type.Array(resultErrorSchema) },
55
- { additionalProperties: false },
56
- );
57
- /** Flush owns the batch journal; individual file tools do not carry it. */
58
- export const flushDataSchema = Type.Object(
59
- { operation: Type.Literal("flush"), ...receipt, operations: Type.Array(batchOperation) },
60
- { additionalProperties: false },
61
- );
62
- export const flushOutputSchema = structuredResultSchema(flushDataSchema);
63
- export type FlushData = Static<typeof flushDataSchema>;
52
+ /** Observed file and operation effects retained on the parent native script. */
53
+ export interface BatchMutationData {
54
+ operation: "batch";
55
+ effect: MutationData["effect"];
56
+ files: MutationData["files"];
57
+ operations: {
58
+ id: string;
59
+ operation: string;
60
+ effect: MutationData["effect"];
61
+ errors: ResultError[];
62
+ }[];
63
+ }
64
64
  /** Small internal receipt used to collect observed file effects. */
65
65
  export interface MutationData {
66
66
  operation: string;
@@ -206,6 +206,7 @@ export function mutationOutcome(
206
206
  )
207
207
  errors.push({ code: "UNKNOWN_RESULT", message: "Mutation effects were not reported" });
208
208
  const effect =
209
+ details.effect === "unknown" ||
209
210
  unique.some((file) => file.effect === "unknown") ||
210
211
  errors.some((error) => error.code === "UNKNOWN_RESULT")
211
212
  ? "unknown"
@@ -8,6 +8,8 @@ export interface TextRange {
8
8
 
9
9
  export interface TextChange extends TextRange {
10
10
  readonly insert: string;
11
+ /** Copy may retain a destination range even when its text is already identical. */
12
+ readonly allowUnchanged?: true;
11
13
  }
12
14
 
13
15
  export interface AppliedTextChange {
@@ -220,7 +222,9 @@ export function applyTextChanges(
220
222
  validateChanges(source.length, ordered);
221
223
  if (
222
224
  !(creating && source.length === 0) &&
223
- ordered.some((change) => source.slice(change.from, change.to) === change.insert)
225
+ ordered.some(
226
+ (change) => !change.allowUnchanged && source.slice(change.from, change.to) === change.insert,
227
+ )
224
228
  ) {
225
229
  throw new Error("Text changes must change the document.");
226
230
  }
@@ -1,3 +1,9 @@
1
+ import { connectResultTargets, type ResultTargetStore } from "pi-agent-resource";
2
+ import {
3
+ committedMutationTargets,
4
+ describeUnavailableCopyTarget,
5
+ unchangedCopySources,
6
+ } from "./mutation-result-targets.js";
1
7
  import { requiredValue } from "pi-agent-invariant";
2
8
  import { renderTextAnchor } from "pi-agent-text";
3
9
  import {
@@ -29,15 +35,21 @@ import {
29
35
  TextMutationAnchorResolutionError,
30
36
  } from "#src/core/text-mutation-anchor-error.js";
31
37
  import {
38
+ buildFailedCopyWriteResult,
32
39
  buildFailedTextMutationResult,
33
40
  buildSuccessfulTextMutationResult,
34
41
  mutationSources,
35
42
  preflightMutationAnchors,
43
+ textMutationOperationReceipts,
36
44
  } from "#src/core/text-mutation.js";
37
45
  import { registerBlockedToolCallSink } from "#src/core/tool-call-interceptor/coordinator.js";
38
46
 
39
47
  import type { TextEditIntent } from "#src/api/edit-completion.js";
40
- import type { FileMutationResult, MutationResultPresentation } from "#src/api/mutation-result.js";
48
+ import type {
49
+ FileMutationBatchResult,
50
+ FileMutationResult,
51
+ MutationResultPresentation,
52
+ } from "#src/api/mutation-result.js";
41
53
  import type { AnyTextMutationToolRegistration, TextMutation } from "#src/api/mutation-tool.js";
42
54
  import type { BatchExecutionReporter } from "#src/core/text-edit-batch-execution.js";
43
55
  import type {
@@ -90,6 +102,7 @@ export function setTextEditBatchRenderArgumentSink(
90
102
  }
91
103
 
92
104
  export function registerTextEditBatching(pi: ExtensionAPI, core: TextEditorCore): void {
105
+ const resultTargets = connectResultTargets(pi);
93
106
  const sourceTools: string[] = [];
94
107
  const registrations = new Map<string, AnyTextMutationToolRegistration>();
95
108
  const renderArguments = (toolCallId: string, patch: Readonly<Record<string, unknown>>): void =>
@@ -159,6 +172,8 @@ export function registerTextEditBatching(pi: ExtensionAPI, core: TextEditorCore)
159
172
  context,
160
173
  reporter,
161
174
  renderArguments,
175
+ undefined,
176
+ resultTargets,
162
177
  ),
163
178
  splitResult: splitTextBatchResult,
164
179
  onRenderArguments: renderArguments,
@@ -301,6 +316,7 @@ export async function executeRegisteredTextBatch(
301
316
  reporter: BatchExecutionReporter,
302
317
  renderArguments: (toolCallId: string, patch: Readonly<Record<string, unknown>>) => void,
303
318
  planned?: PlannedTextBatch,
319
+ resultTargets?: ResultTargetStore,
304
320
  ): Promise<AgentToolResult<TextBatchDetails>> {
305
321
  const prepared = parameters.edits.map((entry) => {
306
322
  const registration = registrations.get(entry.op);
@@ -339,12 +355,15 @@ export async function executeRegisteredTextBatch(
339
355
  }
340
356
 
341
357
  let outcome: TextResourcesEditOutcome<PlannedTextBatch>;
358
+ let executionPlan = planned;
359
+ const guardedSnapshots = new Map<string, string>();
342
360
 
343
361
  try {
344
362
  outcome = await core.editTexts(
345
363
  [...requests.values()],
346
364
  { cwd: context.cwd, intent, ...(signal !== undefined && { signal }) },
347
365
  async (texts, resolveAnchor) => {
366
+ for (const [source, content] of texts) guardedSnapshots.set(source, content);
348
367
  for (const [source, expected] of parameters.expectedContent ?? []) {
349
368
  if (!texts.has(source) || texts.get(source) !== expected) {
350
369
  throw new Error(`Snapshot source ${source} changed before the edit batch.`);
@@ -361,6 +380,7 @@ export async function executeRegisteredTextBatch(
361
380
  signal,
362
381
  renderArguments,
363
382
  ));
383
+ executionPlan = result;
364
384
  return { changes: result.changes, result };
365
385
  },
366
386
  );
@@ -375,12 +395,43 @@ export async function executeRegisteredTextBatch(
375
395
  }
376
396
 
377
397
  if (outcome.kind === "failed") {
398
+ const copyResults = new Map<string, AgentToolResult<FileMutationBatchResult>>();
399
+ if (outcome.failure.code === "WRITE_FAILED") {
400
+ for (const { callId, mutation } of executionPlan?.mutations ?? []) {
401
+ if (mutation.operation !== "copy") continue;
402
+ const unrestored = (outcome.failure.rollback?.failed ?? outcome.completed).filter(
403
+ (source) => mutation.edits.has(source),
404
+ );
405
+ const destination = unrestored[0] ?? mutation.edits.keys().next().value;
406
+ copyResults.set(
407
+ callId,
408
+ buildFailedCopyWriteResult(
409
+ {
410
+ ...outcome.failure,
411
+ source: destination ?? outcome.failure.source,
412
+ ...(outcome.failure.rollback !== undefined && {
413
+ rollback: {
414
+ failed: outcome.failure.rollback.failed.filter((source) =>
415
+ mutation.edits.has(source),
416
+ ),
417
+ originallyMissing: outcome.failure.rollback.originallyMissing.filter((source) =>
418
+ mutation.edits.has(source),
419
+ ),
420
+ },
421
+ }),
422
+ },
423
+ unrestored,
424
+ ),
425
+ );
426
+ }
427
+ }
378
428
  return failTextBatch(
379
429
  core,
380
430
  prepared.map(({ callId }) => callId),
381
431
  outcome.failure,
382
432
  context,
383
433
  reporter,
434
+ copyResults,
384
435
  );
385
436
  }
386
437
 
@@ -433,7 +484,7 @@ export async function executeRegisteredTextBatch(
433
484
  mutation.resultPresentations.get(source) ?? "plain",
434
485
 
435
486
  1,
436
- [{ operation: mutation.operation, changes: edit.changes.length }],
487
+ textMutationOperationReceipts(mutation.operation, edit, resource.before.content),
437
488
 
438
489
  completedMutations.flatMap((peer) =>
439
490
  peer.callId === callId
@@ -451,7 +502,12 @@ export async function executeRegisteredTextBatch(
451
502
  displayResults.push(...callDisplayResults);
452
503
  callIdsByDisplayResult.push(...callDisplayResults.map(() => callId));
453
504
 
505
+ const unchangedCopy =
506
+ mutation.operation === "copy" &&
507
+ mutation.edits.size > 0 &&
508
+ unchangedCopySources(mutation, guardedSnapshots).size === mutation.edits.size;
454
509
  for (const source of mutation.edits.keys()) {
510
+ if (unchangedCopy && finalCallIdBySource.has(source)) continue;
455
511
  finalCallIdBySource.set(source, callId);
456
512
  finalPresentationBySource.set(source, mutation.resultPresentations.get(source) ?? "plain");
457
513
  }
@@ -483,7 +539,7 @@ export async function executeRegisteredTextBatch(
483
539
  const edit = mutation.edits.get(source);
484
540
  return edit === undefined
485
541
  ? []
486
- : [{ operation: mutation.operation, changes: edit.changes.length }];
542
+ : textMutationOperationReceipts(mutation.operation, edit, resource.before.content);
487
543
  }),
488
544
  );
489
545
  resultsByCallId.get(finalCallId)?.push(result);
@@ -491,17 +547,82 @@ export async function executeRegisteredTextBatch(
491
547
  callIdsByResult.push(finalCallId);
492
548
  }
493
549
 
550
+ const copyResults = new Map<string, AgentToolResult<FileMutationBatchResult>>();
551
+ if (resultTargets) {
552
+ const copies = completedMutations.filter(
553
+ ({ mutation }) => mutation.operation === "copy" && mutation.edits.size > 0,
554
+ );
555
+ for (const { callId, mutation } of copies) {
556
+ const sources = new Set(mutation.edits.keys());
557
+ let metadata: FileMutationBatchResult["metadata"];
558
+ try {
559
+ const mapped = committedMutationTargets(
560
+ completedMutations.map(({ callId, mutation }) => ({
561
+ callId,
562
+ edits: new Map([...mutation.edits].filter(([source]) => sources.has(source))),
563
+ })),
564
+ // Copy read these destinations successfully, so they existed before the batch.
565
+ outcome.resources
566
+ .filter((resource) => sources.has(resource.source))
567
+ .map((resource) => ({
568
+ source: resource.source,
569
+ resourceSource: resource.after.source,
570
+ resolvedBy: resource.resolvedBy,
571
+ before: resource.before,
572
+ after: resource.after,
573
+ existed: true,
574
+ })),
575
+ guardedSnapshots,
576
+ );
577
+ const targets = mapped.get(callId);
578
+ const expectedTargets = [...mutation.edits.values()].filter(
579
+ (edit) => edit.resultChanges?.length !== 0,
580
+ ).length;
581
+ if (!targets || targets.length !== expectedTargets)
582
+ throw new Error("This operation did not produce confirmed filesystem targets.");
583
+ const reference = resultTargets.register(targets, context.cwd);
584
+ await resultTargets.verify(resultTargets.resolve(reference, context.cwd), signal);
585
+ metadata = { resultTarget: reference };
586
+ } catch (error) {
587
+ signal?.throwIfAborted();
588
+ metadata = { targetUnavailable: errorMessage(error) };
589
+ }
590
+ const unchanged =
591
+ unchangedCopySources(mutation, guardedSnapshots).size === mutation.edits.size;
592
+ const ownResults = resultsByCallId.get(callId) ?? [];
593
+ const displayed = displayResultsByCallId.get(callId) ?? [];
594
+ const receipt = textBatchResult(
595
+ ownResults.length > 0 ? ownResults : displayed,
596
+ [],
597
+ displayed,
598
+ [],
599
+ );
600
+ copyResults.set(
601
+ callId,
602
+ describeUnavailableCopyTarget({
603
+ ...receipt,
604
+ ...(unchanged
605
+ ? {
606
+ content: [{ type: "text", text: "No changes: destination already has this text." }],
607
+ }
608
+ : {}),
609
+ details: { ...receipt.details, effect: unchanged ? "not-applied" : "applied", metadata },
610
+ }),
611
+ );
612
+ }
613
+ }
494
614
  for (const { callId } of completedMutations) {
495
615
  const callResults = resultsByCallId.get(callId) ?? [];
496
616
  const callDisplayResults = displayResultsByCallId.get(callId) ?? [];
497
617
  reporter.complete(
498
618
  callId,
499
- textBatchResult(
500
- callResults,
501
- callResults.map(() => callId),
502
- callDisplayResults,
503
- callDisplayResults.map(() => callId),
504
- ),
619
+ copyResults.get(callId) ??
620
+ textBatchResult(
621
+ callResults,
622
+ callResults.map(() => callId),
623
+ callDisplayResults,
624
+ callDisplayResults.map(() => callId),
625
+ ),
505
626
  );
506
627
  }
507
628
 
@@ -556,7 +677,10 @@ export async function executeRegisteredTextBatch(
556
677
  callIdsByDisplayResult: [...callIdsByDisplayResult],
557
678
  },
558
679
  });
559
- return textBatchResult(results, callIdsByResult, displayResults, callIdsByDisplayResult);
680
+ const result = textBatchResult(results, callIdsByResult, displayResults, callIdsByDisplayResult);
681
+ return copyResults.size === 0
682
+ ? result
683
+ : { ...result, details: { ...result.details, copyResults } };
560
684
  }
561
685
 
562
686
  async function failTextBatch(
@@ -565,32 +689,38 @@ async function failTextBatch(
565
689
  error: unknown,
566
690
  context: ExtensionContext,
567
691
  reporter: BatchExecutionReporter,
692
+ copyResults: ReadonlyMap<string, AgentToolResult<FileMutationBatchResult>> = new Map(),
568
693
  ): Promise<AgentToolResult<TextBatchDetails>> {
569
- const cause = error;
570
694
  const failure = isTextResourceEditFailure(error)
571
695
  ? error
572
696
  : {
573
697
  code: "INVALID_REQUEST" as const,
574
698
  source: "",
575
- message: errorMessage(cause),
576
- cause,
699
+ message: errorMessage(error),
700
+ cause: error,
577
701
  };
702
+ const cause = Object.assign(new Error(failure.message), { code: failure.code });
578
703
  const failedCallId = callIds[0];
579
704
  const failedResult = await buildFailedTextMutationResult(core, failure, context);
580
-
705
+ const results: FileMutationResult[] = [];
706
+ const callIdsByResult: string[] = [];
581
707
  for (const callId of callIds) {
708
+ const copyResult = copyResults.get(callId);
709
+ const result = copyResult ?? (callId === failedCallId ? failedResult : undefined);
582
710
  reporter.fail(callId, {
583
711
  error: cause,
584
- effect: "not-applied",
585
- ...(callId === failedCallId && { result: failedResult }),
712
+ effect: copyResult?.details.effect ?? failedResult.details.effect ?? "not-applied",
713
+ ...(result !== undefined && { result }),
586
714
  });
715
+ for (const item of result?.details.results ?? []) {
716
+ results.push(item);
717
+ callIdsByResult.push(callId);
718
+ }
587
719
  }
588
-
589
- const results = failedResult.details.results ?? [];
590
- return textBatchResult(
591
- results,
592
- results.map(() => failedCallId ?? ""),
593
- );
720
+ const result = textBatchResult(results, callIdsByResult);
721
+ return copyResults.size === 0
722
+ ? result
723
+ : { ...result, details: { ...result.details, copyResults } };
594
724
  }
595
725
 
596
726
  function isTextResourceEditFailure(value: unknown): value is TextResourceEditFailure {
@@ -28,6 +28,8 @@ export interface TextBatchDetails extends FileMutationBatchResult {
28
28
  readonly displayResults?: readonly FileMutationResult[];
29
29
  /** Original call id for each user-facing result. */
30
30
  readonly callIdsByDisplayResult?: readonly string[];
31
+ /** Per-call Copy receipts retain destination authority and their own failure evidence. */
32
+ readonly copyResults?: ReadonlyMap<string, AgentToolResult<FileMutationBatchResult>>;
31
33
  }
32
34
 
33
35
  function resultPath(result: FileMutationResult): string | undefined {
@@ -54,6 +56,8 @@ export function splitTextBatchResult(
54
56
 
55
57
  return new Map(
56
58
  calls.map((call) => {
59
+ const copyResult = details?.copyResults?.get(call.id);
60
+ if (copyResult) return [call.id, copyResult];
57
61
  const agentResults = coalesceResults(agentResultsByCall.get(call.id) ?? []);
58
62
  const displayResults = coalesceResults(displayResultsByCall.get(call.id) ?? []);
59
63
  return [