pi-agent-ide 0.7.1 → 0.7.3
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.
- package/CHANGELOG.md +66 -0
- package/README.md +3 -1
- package/dist/{chunk-PKKDDIWW.js → chunk-2ZMMRINH.js} +1 -1
- package/dist/chunk-6U5OU77O.js +3 -0
- package/dist/chunk-BYZSBXUK.js +2 -0
- package/dist/{chunk-LEEQIQ7D.js → chunk-CEX73RI5.js} +6 -6
- package/dist/chunk-DZWTKF2K.js +1 -0
- package/dist/{chunk-WMO6RNE2.js → chunk-EOKEQ3YH.js} +6 -6
- package/dist/{chunk-HZEY4GT4.js → chunk-H445SHSJ.js} +2 -2
- package/dist/{chunk-4GCWGE2C.js → chunk-HZ5BNF3A.js} +1 -1
- package/dist/chunk-IG56UGSO.js +2 -0
- package/dist/chunk-IICJXZBU.js +6 -0
- package/dist/{chunk-XYS4FSCC.js → chunk-IM24DB3K.js} +1 -1
- package/dist/chunk-MXLSGMRZ.js +3 -0
- package/dist/{chunk-76ZK5ODR.js → chunk-NHKPP2AI.js} +13 -13
- package/dist/chunk-OB46OPMZ.js +1 -0
- package/dist/{chunk-PNU3E7IO.js → chunk-Q6CZ6GBR.js} +2 -2
- package/dist/chunk-QUYXINGZ.js +7 -0
- package/dist/{chunk-OMVRLWMX.js → chunk-SBP3ABH4.js} +1 -1
- package/dist/{chunk-YP46DVVP.js → chunk-STBJO3Z3.js} +1 -1
- package/dist/{chunk-2TRHFMCZ.js → chunk-TXOTNK6B.js} +1 -1
- package/dist/chunk-WLZIUMJL.js +1 -0
- package/dist/chunk-ZIWPKIBZ.js +1 -0
- package/dist/entry-11.js +1 -1
- package/dist/entry-13.js +1 -1
- package/dist/entry-14.js +1 -1
- package/dist/entry-15.js +1 -1
- package/dist/entry-17.js +1 -1
- package/dist/entry-25.js +1 -1
- package/dist/entry-26.js +1 -1
- package/dist/entry-27.js +1 -1
- package/dist/entry-28.js +1 -1
- package/dist/entry-29.js +1 -1
- package/dist/entry-31.js +12 -11
- package/dist/entry-33.js +3 -1
- package/dist/entry-37.js +6 -6
- package/dist/entry-42.js +1 -1
- package/dist/entry-44.js +1 -1
- package/dist/entry-45.js +20 -12
- package/dist/entry-46.js +1 -1
- package/dist/entry-49.js +1 -1
- package/dist/entry-50.js +1 -1
- package/dist/entry-51.js +7 -6
- package/dist/entry-52.js +1 -1
- package/dist/entry-53.js +1 -1
- package/dist/entry-54.js +1 -1
- package/dist/entry-56.js +1 -1
- package/dist/entry-58.js +1 -1
- package/dist/entry-60.js +1 -1
- package/dist/entry-62.js +1 -1
- package/dist/entry-65.js +1 -1
- package/dist/entry-66.js +1 -1
- package/dist/entry-68.js +1 -1
- package/dist/entry-70.js +1 -1
- package/dist/entry-75.js +1 -1
- package/dist/entry-77.js +63 -49
- package/dist/entry-78.js +15 -14
- package/dist/entry-79.js +1 -1
- package/dist/entry-82.js +5 -5
- package/dist/entry-83.js +1 -1
- package/dist/entry-84.js +1 -1
- package/dist/entry-85.js +1 -1
- package/dist/entry-86.js +1 -1
- package/dist/pi-agent-ide.js +34 -11
- package/docs/agent-guides/editing.md +14 -7
- package/docs/agent-guides/git-changes.md +10 -0
- package/docs/agent-guides/read-resources.md +9 -46
- package/docs/agent-guides/search-code.md +7 -1
- package/docs/agent-guides/select-code.md +1 -0
- package/docs/agent-guides/terminal.md +9 -1
- package/docs/configuration.md +22 -1
- package/docs/development.md +4 -0
- package/docs/directory-operations.md +45 -0
- package/docs/releases.md +12 -0
- package/docs/research/codemode-write-context.md +56 -0
- package/docs/research/native-codemode-batch-decision.md +2 -0
- package/docs/structured-results.md +2 -2
- package/docs/tools.md +10 -2
- package/docs/user-hooks.md +26 -0
- package/node_modules/pi-agent-doctor/package.json +1 -1
- package/node_modules/pi-agent-documentation/package.json +1 -1
- package/node_modules/pi-agent-filesystem/package.json +7 -7
- package/node_modules/pi-agent-filesystem-image/package.json +3 -3
- package/node_modules/pi-agent-filesystem-jq/package.json +5 -5
- package/node_modules/pi-agent-filesystem-jq/src/jq.ts +4 -1
- package/node_modules/pi-agent-filesystem-pdf/package.json +3 -3
- package/node_modules/pi-agent-filesystem-text/package.json +3 -3
- package/node_modules/pi-agent-ide-ast/package.json +13 -13
- package/node_modules/pi-agent-ide-ast/src/extension.ts +3 -3
- package/node_modules/pi-agent-ide-ast/src/overflow-handler.ts +9 -4
- package/node_modules/pi-agent-ide-ast/src/search-resolver.ts +0 -3
- package/node_modules/pi-agent-ide-ast/src/select-schema.ts +8 -6
- package/node_modules/pi-agent-ide-ast/src/tool-select.ts +13 -5
- package/node_modules/pi-agent-ide-changes/package.json +9 -8
- package/node_modules/pi-agent-ide-changes/src/tool-index-change.ts +82 -4
- package/node_modules/pi-agent-ide-changes/src/tool-text-undo.ts +8 -11
- package/node_modules/pi-agent-ide-formatter/package.json +3 -3
- package/node_modules/pi-agent-ide-languages/package.json +2 -2
- package/node_modules/pi-agent-ide-lint/package.json +7 -7
- package/node_modules/pi-agent-ide-lsp/index.ts +1 -1
- package/node_modules/pi-agent-ide-lsp/package.json +9 -9
- package/node_modules/pi-agent-ide-lsp/src/declaration-targets.ts +2 -1
- package/node_modules/pi-agent-ide-lsp/src/search-resolver.ts +0 -1
- package/node_modules/pi-agent-image/package.json +2 -2
- package/node_modules/pi-agent-invariant/package.json +1 -1
- package/node_modules/pi-agent-pdf/package.json +2 -2
- package/node_modules/pi-agent-read/package.json +7 -7
- package/node_modules/pi-agent-read/src/api/read-parameters.ts +3 -3
- package/node_modules/pi-agent-read/src/api/tools/read.ts +11 -0
- package/node_modules/pi-agent-read/src/core/extension.ts +1 -1
- package/node_modules/pi-agent-read/src/core/read-core.ts +5 -1
- package/node_modules/pi-agent-read/src/core/tools/read/output-truncation.ts +91 -18
- package/node_modules/pi-agent-read/src/core/tools/read/raw-read.ts +8 -2
- package/node_modules/pi-agent-read/src/core/tools/read/read-renderer.ts +38 -1
- package/node_modules/pi-agent-read/src/core/tools/read/read-result.ts +57 -2
- package/node_modules/pi-agent-read/src/core/tools/tool-read.ts +99 -44
- package/node_modules/pi-agent-resource/package.json +1 -1
- package/node_modules/pi-agent-resource/src/index.ts +1 -0
- package/node_modules/pi-agent-resource/src/result-targets.ts +6 -3
- package/node_modules/{pi-agent-read/src/core/tools/read → pi-agent-resource/src}/temp-resource-store.ts +17 -7
- package/node_modules/pi-agent-search/package.json +5 -5
- package/node_modules/pi-agent-search/src/api/fuzzy.ts +5 -1
- package/node_modules/pi-agent-search/src/api/search-parameters.ts +2 -2
- package/node_modules/pi-agent-search/src/core/extension.ts +2 -1
- package/node_modules/pi-agent-search/src/core/search-core.ts +13 -4
- package/node_modules/pi-agent-search-text/package.json +7 -7
- package/node_modules/pi-agent-search-text/src/resolvers.ts +11 -1
- package/node_modules/pi-agent-search-text/src/scoped-search.ts +1 -1
- package/node_modules/pi-agent-search-text/src/search-presentation.ts +42 -32
- package/node_modules/pi-agent-search-text/src/search-recipe.ts +1 -1
- package/node_modules/pi-agent-search-text/src/search-renderer.ts +6 -0
- package/node_modules/pi-agent-search-text/src/search-result.ts +17 -3
- package/node_modules/pi-agent-text/package.json +2 -2
- package/node_modules/pi-agent-text-anchor-constant/package.json +3 -3
- package/node_modules/pi-agent-text-anchor-exact/package.json +3 -3
- package/node_modules/pi-agent-text-anchor-line-hash/package.json +5 -5
- package/node_modules/pi-agent-text-editor/package.json +9 -9
- package/node_modules/pi-agent-text-editor/src/api/delete-guard.ts +24 -0
- package/node_modules/pi-agent-text-editor/src/api/edit-completion.ts +2 -2
- package/node_modules/pi-agent-text-editor/src/api/mutation-guard.ts +1 -0
- package/node_modules/pi-agent-text-editor/src/api/mutation-result.ts +1 -2
- package/node_modules/pi-agent-text-editor/src/api/native-edit-batch-event.ts +4 -0
- package/node_modules/pi-agent-text-editor/src/api/plugin-protocol.ts +4 -1
- package/node_modules/pi-agent-text-editor/src/api/stale-anchor-message.ts +1 -1
- package/node_modules/pi-agent-text-editor/src/core/delete-policy.ts +284 -0
- package/node_modules/pi-agent-text-editor/src/core/diff-tool.ts +54 -12
- package/node_modules/pi-agent-text-editor/src/core/editing-guidelines.ts +1 -1
- package/node_modules/pi-agent-text-editor/src/core/file-operation-tools.ts +32 -3
- package/node_modules/pi-agent-text-editor/src/core/file-operations.ts +30 -40
- package/node_modules/pi-agent-text-editor/src/core/file-result-targets.ts +1 -1
- package/node_modules/pi-agent-text-editor/src/core/file-transfers.ts +164 -0
- package/node_modules/pi-agent-text-editor/src/core/mutation-result/file-mutation-agent-result.ts +36 -12
- package/node_modules/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +25 -1
- package/node_modules/pi-agent-text-editor/src/core/mutation-result-targets.ts +132 -11
- package/node_modules/pi-agent-text-editor/src/core/native-text-edit-batch.ts +286 -162
- package/node_modules/pi-agent-text-editor/src/core/post-edit-scope.ts +34 -10
- package/node_modules/pi-agent-text-editor/src/core/result-input.ts +24 -4
- package/node_modules/pi-agent-text-editor/src/core/result-transfer.ts +46 -11
- package/node_modules/pi-agent-text-editor/src/core/structured-result.ts +21 -14
- package/node_modules/pi-agent-text-editor/src/core/temporary-directories.ts +96 -0
- package/node_modules/pi-agent-text-editor/src/core/text-change-engine.ts +5 -1
- package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +155 -22
- package/node_modules/pi-agent-text-editor/src/core/text-edit-batch.ts +4 -0
- package/node_modules/pi-agent-text-editor/src/core/text-editor-core.ts +126 -32
- package/node_modules/pi-agent-text-editor/src/core/text-mutation.ts +428 -189
- package/node_modules/pi-agent-text-editor/src/core/write-agent-result.ts +66 -0
- package/node_modules/pi-agent-text-editor/src/tools/text-tool-schema-properties.ts +6 -4
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-copy.ts +15 -6
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-delete.ts +9 -4
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-insert.ts +7 -8
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-move.ts +6 -4
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-replace.ts +2 -3
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-write.ts +10 -4
- package/node_modules/pi-agent-text-editor-renderer/package.json +5 -5
- package/node_modules/pi-agent-text-editor-renderer/src/mutation-panel.ts +11 -0
- package/node_modules/pi-agent-text-editor-renderer/src/renderer.ts +75 -11
- package/node_modules/pi-agent-text-editor-renderer/src/status-summary.ts +18 -3
- package/node_modules/pi-agent-text-editor-stale-anchor/package.json +4 -4
- package/node_modules/pi-agent-tool-call-interception/package.json +2 -2
- package/node_modules/pi-agent-tool-ui/package.json +1 -1
- package/node_modules/pi-agent-web/package.json +7 -7
- package/node_modules/pi-agent-web/src/resolver.ts +11 -13
- package/node_modules/pi-agent-web-html/package.json +2 -2
- package/node_modules/pi-agent-web-image/package.json +3 -3
- package/node_modules/pi-agent-web-pdf/package.json +3 -3
- package/node_modules/pi-agent-web-text/package.json +3 -3
- package/package.json +36 -35
- package/src/api/hooks.ts +24 -0
- package/src/composite/nested-tool-rendering.ts +184 -77
- package/src/composite/output-limits.ts +125 -0
- package/src/composite/responses-tools.ts +25 -0
- package/src/composite/text-results.ts +158 -7
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/package.json +7 -7
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-jq/package.json +5 -5
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-jq/src/jq.ts +4 -1
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-pdf/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/package.json +7 -7
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/package.json +2 -2
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-image/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-pdf/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/src/resolver.ts +11 -13
- package/src/extensions/pi-agent-read/package.json +7 -7
- package/src/extensions/pi-agent-read/src/api/read-parameters.ts +3 -3
- package/src/extensions/pi-agent-read/src/api/tools/read.ts +11 -0
- package/src/extensions/pi-agent-read/src/core/extension.ts +1 -1
- package/src/extensions/pi-agent-read/src/core/read-core.ts +5 -1
- package/src/extensions/pi-agent-read/src/core/tools/read/output-truncation.ts +91 -18
- package/src/extensions/pi-agent-read/src/core/tools/read/raw-read.ts +8 -2
- package/src/extensions/pi-agent-read/src/core/tools/read/read-renderer.ts +38 -1
- package/src/extensions/pi-agent-read/src/core/tools/read/read-result.ts +57 -2
- package/src/extensions/pi-agent-read/src/core/tools/tool-read.ts +99 -44
- package/src/extensions/pi-agent-search/package.json +5 -5
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/package.json +7 -7
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/resolvers.ts +11 -1
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/scoped-search.ts +1 -1
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-presentation.ts +42 -32
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-recipe.ts +1 -1
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-renderer.ts +6 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-result.ts +17 -3
- package/src/extensions/pi-agent-search/src/api/fuzzy.ts +5 -1
- package/src/extensions/pi-agent-search/src/api/search-parameters.ts +2 -2
- package/src/extensions/pi-agent-search/src/core/extension.ts +2 -1
- package/src/extensions/pi-agent-search/src/core/search-core.ts +13 -4
- package/src/extensions/pi-agent-text-editor/package.json +9 -9
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/package.json +3 -3
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/package.json +3 -3
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/package.json +5 -5
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/package.json +5 -5
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/mutation-panel.ts +11 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/renderer.ts +75 -11
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/status-summary.ts +18 -3
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/package.json +4 -4
- package/src/extensions/pi-agent-text-editor/src/api/delete-guard.ts +24 -0
- package/src/extensions/pi-agent-text-editor/src/api/edit-completion.ts +2 -2
- package/src/extensions/pi-agent-text-editor/src/api/mutation-guard.ts +1 -0
- package/src/extensions/pi-agent-text-editor/src/api/mutation-result.ts +1 -2
- package/src/extensions/pi-agent-text-editor/src/api/native-edit-batch-event.ts +4 -0
- package/src/extensions/pi-agent-text-editor/src/api/plugin-protocol.ts +4 -1
- package/src/extensions/pi-agent-text-editor/src/api/stale-anchor-message.ts +1 -1
- package/src/extensions/pi-agent-text-editor/src/core/delete-policy.ts +284 -0
- package/src/extensions/pi-agent-text-editor/src/core/diff-tool.ts +54 -12
- package/src/extensions/pi-agent-text-editor/src/core/editing-guidelines.ts +1 -1
- package/src/extensions/pi-agent-text-editor/src/core/file-operation-tools.ts +32 -3
- package/src/extensions/pi-agent-text-editor/src/core/file-operations.ts +30 -40
- package/src/extensions/pi-agent-text-editor/src/core/file-result-targets.ts +1 -1
- package/src/extensions/pi-agent-text-editor/src/core/file-transfers.ts +164 -0
- package/src/extensions/pi-agent-text-editor/src/core/mutation-result/file-mutation-agent-result.ts +36 -12
- package/src/extensions/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +25 -1
- package/src/extensions/pi-agent-text-editor/src/core/mutation-result-targets.ts +132 -11
- package/src/extensions/pi-agent-text-editor/src/core/native-text-edit-batch.ts +286 -162
- package/src/extensions/pi-agent-text-editor/src/core/post-edit-scope.ts +34 -10
- package/src/extensions/pi-agent-text-editor/src/core/result-input.ts +24 -4
- package/src/extensions/pi-agent-text-editor/src/core/result-transfer.ts +46 -11
- package/src/extensions/pi-agent-text-editor/src/core/structured-result.ts +21 -14
- package/src/extensions/pi-agent-text-editor/src/core/temporary-directories.ts +96 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-change-engine.ts +5 -1
- package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +155 -22
- package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch.ts +4 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-editor-core.ts +126 -32
- package/src/extensions/pi-agent-text-editor/src/core/text-mutation.ts +428 -189
- package/src/extensions/pi-agent-text-editor/src/core/write-agent-result.ts +66 -0
- package/src/extensions/pi-agent-text-editor/src/tools/text-tool-schema-properties.ts +6 -4
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-copy.ts +15 -6
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-delete.ts +9 -4
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-insert.ts +7 -8
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-move.ts +6 -4
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-replace.ts +2 -3
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-write.ts +10 -4
- package/src/pi-agent-ide.ts +11 -0
- package/src/plugins/pi-agent-ide-ast/package.json +13 -13
- package/src/plugins/pi-agent-ide-ast/src/extension.ts +3 -3
- package/src/plugins/pi-agent-ide-ast/src/overflow-handler.ts +9 -4
- package/src/plugins/pi-agent-ide-ast/src/search-resolver.ts +0 -3
- package/src/plugins/pi-agent-ide-ast/src/select-schema.ts +8 -6
- package/src/plugins/pi-agent-ide-ast/src/tool-select.ts +13 -5
- package/src/plugins/pi-agent-ide-changes/package.json +9 -8
- package/src/plugins/pi-agent-ide-changes/src/tool-index-change.ts +82 -4
- package/src/plugins/pi-agent-ide-changes/src/tool-text-undo.ts +8 -11
- package/src/plugins/pi-agent-ide-debugger/index.ts +20 -8
- package/src/plugins/pi-agent-ide-debugger/src/dap-client.ts +5 -0
- package/src/plugins/pi-agent-ide-debugger/src/session-manager.ts +93 -38
- package/src/plugins/pi-agent-ide-diagnostics/src/extension.ts +3 -2
- package/src/plugins/pi-agent-ide-formatter/package.json +3 -3
- package/src/plugins/pi-agent-ide-languages/package.json +2 -2
- package/src/plugins/pi-agent-ide-lint/package.json +7 -7
- package/src/plugins/pi-agent-ide-lsp/index.ts +1 -1
- package/src/plugins/pi-agent-ide-lsp/package.json +9 -9
- package/src/plugins/pi-agent-ide-lsp/src/declaration-targets.ts +2 -1
- package/src/plugins/pi-agent-ide-lsp/src/search-resolver.ts +0 -1
- package/src/plugins/pi-agent-ide-terminal/src/output-limits.ts +3 -3
- package/src/plugins/pi-agent-ide-terminal/src/tools.ts +3 -3
- package/src/plugins/pi-agent-ide-vision/index.ts +1 -0
- package/src/post-edit/gate.ts +13 -2
- package/dist/chunk-4XMY4H4V.js +0 -1
- package/dist/chunk-64M3SQPG.js +0 -3
- package/dist/chunk-CWD4O5PM.js +0 -3
- package/dist/chunk-D3WTDA6V.js +0 -2
- package/dist/chunk-EAZGN5ND.js +0 -7
- package/dist/chunk-GXEGY7GV.js +0 -1
- package/dist/chunk-MBUXFKIW.js +0 -3
- package/dist/chunk-PGIVHICI.js +0 -1
- package/dist/chunk-VMTR74FF.js +0 -2
- package/dist/chunk-WU6ZK6YD.js +0 -1
- package/src/extensions/pi-agent-read/src/core/tools/read/temp-resource-store.ts +0 -105
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { rm, unlink } from "node:fs/promises";
|
|
2
|
+
import { prepareFileTransfer, type TransferKind } from "./file-transfers.js";
|
|
3
|
+
import { prepareDeletion, type DeletePolicyContext } from "./delete-policy.js";
|
|
2
4
|
import path from "node:path";
|
|
3
5
|
import fs from "fs-extra";
|
|
4
6
|
import { Type } from "typebox";
|
|
@@ -9,7 +11,7 @@ export const fileOperations = ["delete", "move", "copy"] as const;
|
|
|
9
11
|
export type FileOperation = (typeof fileOperations)[number];
|
|
10
12
|
const filePath = Type.String({
|
|
11
13
|
minLength: 1,
|
|
12
|
-
description: "Local
|
|
14
|
+
description: "Local file, directory, or symlink path, relative to cwd or absolute.",
|
|
13
15
|
});
|
|
14
16
|
export const deleteFileParameters = Type.Object(
|
|
15
17
|
{ path: filePath },
|
|
@@ -23,6 +25,10 @@ export const transferFileParameters = Type.Object(
|
|
|
23
25
|
{ additionalProperties: false },
|
|
24
26
|
);
|
|
25
27
|
|
|
28
|
+
/** Host dependencies for deletion; no tool argument can replace the filesystem primitive. */
|
|
29
|
+
export interface FileOperationContext extends DeletePolicyContext {
|
|
30
|
+
readonly removeDirectory?: (source: string) => Promise<void>;
|
|
31
|
+
}
|
|
26
32
|
/** Plain receipt; unknown means the filesystem call failed after execution began. */
|
|
27
33
|
export interface FileOperationResult {
|
|
28
34
|
readonly kind: "file-operation";
|
|
@@ -31,6 +37,7 @@ export interface FileOperationResult {
|
|
|
31
37
|
readonly effect: "applied" | "not-applied" | "unknown";
|
|
32
38
|
readonly path?: string;
|
|
33
39
|
readonly target?: string;
|
|
40
|
+
readonly sourceKind?: TransferKind;
|
|
34
41
|
readonly error?: { readonly code: string; readonly message: string };
|
|
35
42
|
}
|
|
36
43
|
|
|
@@ -55,16 +62,18 @@ export function formatFileOperation(value: FileOperationResult): string {
|
|
|
55
62
|
.filter((line) => line !== undefined)
|
|
56
63
|
.join("\n");
|
|
57
64
|
}
|
|
58
|
-
/** Uses
|
|
65
|
+
/** Uses filesystem primitives without decoding contents; Delete applies hooks and safety policy first. */
|
|
59
66
|
export async function executeFileOperation(
|
|
60
67
|
operation: FileOperation,
|
|
61
68
|
input: unknown,
|
|
62
69
|
cwd: string,
|
|
63
70
|
signal?: AbortSignal,
|
|
71
|
+
deletion: FileOperationContext = {},
|
|
64
72
|
): Promise<FileOperationResult> {
|
|
65
73
|
let started = false;
|
|
66
74
|
let source: string | undefined;
|
|
67
75
|
let target: string | undefined;
|
|
76
|
+
let sourceKind: TransferKind | undefined;
|
|
68
77
|
try {
|
|
69
78
|
const schema = operation === "delete" ? deleteFileParameters : transferFileParameters;
|
|
70
79
|
if (!Value.Check(schema, input))
|
|
@@ -75,29 +84,25 @@ export async function executeFileOperation(
|
|
|
75
84
|
source = path.resolve(cwd, args.path);
|
|
76
85
|
target = args.target === undefined ? undefined : path.resolve(cwd, args.target);
|
|
77
86
|
signal?.throwIfAborted();
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
});
|
|
86
|
-
if (
|
|
87
|
-
source === target ||
|
|
88
|
-
(sourceStat.ino === targetStat.ino && sourceStat.dev === targetStat.dev)
|
|
89
|
-
)
|
|
90
|
-
throw Object.assign(new Error("Source and target are the same file"), {
|
|
91
|
-
code: "SAME_FILE",
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
}
|
|
87
|
+
const deleteEvent =
|
|
88
|
+
operation === "delete" ? await prepareDeletion(source, cwd, deletion, signal) : undefined;
|
|
89
|
+
const transfer =
|
|
90
|
+
operation !== "delete" && target !== undefined
|
|
91
|
+
? await prepareFileTransfer(operation, source, target, cwd, deletion, signal)
|
|
92
|
+
: undefined;
|
|
93
|
+
sourceKind = transfer?.sourceKind;
|
|
95
94
|
signal?.throwIfAborted();
|
|
96
95
|
started = true;
|
|
97
|
-
if (
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
96
|
+
if (deleteEvent !== undefined) {
|
|
97
|
+
if (deleteEvent.recursive) {
|
|
98
|
+
if (deletion.removeDirectory !== undefined)
|
|
99
|
+
await deletion.removeDirectory(deleteEvent.resolvedPath);
|
|
100
|
+
else await rm(deleteEvent.resolvedPath, { recursive: true, force: false });
|
|
101
|
+
} else await unlink(deleteEvent.resolvedPath);
|
|
102
|
+
} else if (transfer !== undefined) {
|
|
103
|
+
if (operation === "copy")
|
|
104
|
+
await fs.copy(transfer.source, transfer.target, { overwrite: true, dereference: false });
|
|
105
|
+
else await fs.move(transfer.source, transfer.target, { overwrite: true });
|
|
101
106
|
}
|
|
102
107
|
return {
|
|
103
108
|
kind: "file-operation",
|
|
@@ -106,6 +111,7 @@ export async function executeFileOperation(
|
|
|
106
111
|
effect: "applied",
|
|
107
112
|
path: source,
|
|
108
113
|
...(target === undefined ? {} : { target }),
|
|
114
|
+
...(sourceKind === undefined ? {} : { sourceKind }),
|
|
109
115
|
};
|
|
110
116
|
} catch (error) {
|
|
111
117
|
return {
|
|
@@ -125,19 +131,3 @@ export async function executeFileOperation(
|
|
|
125
131
|
};
|
|
126
132
|
}
|
|
127
133
|
}
|
|
128
|
-
|
|
129
|
-
async function regularFile(file: string) {
|
|
130
|
-
const stat = await lstat(file);
|
|
131
|
-
if (!stat.isFile() || stat.isSymbolicLink())
|
|
132
|
-
throw Object.assign(new Error("Source must be a regular file"), { code: "INVALID_FILE_TYPE" });
|
|
133
|
-
return stat;
|
|
134
|
-
}
|
|
135
|
-
async function optionalStat(file: string) {
|
|
136
|
-
try {
|
|
137
|
-
return await lstat(file);
|
|
138
|
-
} catch (error) {
|
|
139
|
-
if (error !== null && typeof error === "object" && "code" in error && error.code === "ENOENT")
|
|
140
|
-
return undefined;
|
|
141
|
-
throw error;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
@@ -30,7 +30,7 @@ export async function attachFileMutationTargets(
|
|
|
30
30
|
const stat = await lstat(file);
|
|
31
31
|
states.push({ source: file, state: "present" });
|
|
32
32
|
if (!stat.isFile() || stat.isSymbolicLink())
|
|
33
|
-
throw new Error("
|
|
33
|
+
throw new Error("Target is not a regular text file; no text selection is available.");
|
|
34
34
|
const bytes = await readFile(file, { signal });
|
|
35
35
|
const content = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes);
|
|
36
36
|
if (content.includes("\0")) throw new Error("Binary file has no supported text target.");
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import type { Stats } from "node:fs";
|
|
2
|
+
import { lstat, readdir, readlink, realpath } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import {
|
|
5
|
+
assertUnprotectedTransferPath,
|
|
6
|
+
prepareDeletionGuard,
|
|
7
|
+
type DeletePolicyContext,
|
|
8
|
+
type DeletionGuard,
|
|
9
|
+
} from "./delete-policy.js";
|
|
10
|
+
|
|
11
|
+
/** Object kinds supported by byte-preserving whole-object transfers. */
|
|
12
|
+
export type TransferKind = "file" | "directory" | "symlink";
|
|
13
|
+
|
|
14
|
+
/** Preflight transfers and retain canonical paths; never dereference the final link. */
|
|
15
|
+
export async function prepareFileTransfer(
|
|
16
|
+
operation: "copy" | "move",
|
|
17
|
+
source: string,
|
|
18
|
+
target: string,
|
|
19
|
+
cwd: string,
|
|
20
|
+
context: DeletePolicyContext,
|
|
21
|
+
signal?: AbortSignal,
|
|
22
|
+
): Promise<{ source: string; target: string; sourceKind: TransferKind }> {
|
|
23
|
+
const before = await inspectTransfer(operation, source, target, cwd, signal);
|
|
24
|
+
const guards: DeletionGuard[] = [];
|
|
25
|
+
if (operation === "move" && before.sourceKind !== "file") {
|
|
26
|
+
guards.push(await prepareDeletionGuard(source, cwd, context, signal));
|
|
27
|
+
if (before.targetExists) guards.push(await prepareDeletionGuard(target, cwd, context, signal));
|
|
28
|
+
}
|
|
29
|
+
// A later dialog or hook can change an earlier approved object. Recheck all approvals together.
|
|
30
|
+
for (const guard of guards) await guard.verify();
|
|
31
|
+
const after = await inspectTransfer(operation, source, target, cwd, signal);
|
|
32
|
+
if (before.snapshot !== after.snapshot)
|
|
33
|
+
fail(
|
|
34
|
+
"TRANSFER_TARGET_CHANGED",
|
|
35
|
+
"Transfer paths or contents changed during preflight. Nothing was transferred; make a new request.",
|
|
36
|
+
);
|
|
37
|
+
signal?.throwIfAborted();
|
|
38
|
+
return { source: before.source, target: before.target, sourceKind: before.sourceKind };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function inspectTransfer(
|
|
42
|
+
operation: "copy" | "move",
|
|
43
|
+
source: string,
|
|
44
|
+
target: string,
|
|
45
|
+
cwd: string,
|
|
46
|
+
signal?: AbortSignal,
|
|
47
|
+
) {
|
|
48
|
+
signal?.throwIfAborted();
|
|
49
|
+
const resolvedSource = await resolveLeaf(source);
|
|
50
|
+
const resolvedTarget = await resolveLeaf(target);
|
|
51
|
+
const sourceStat = await lstat(resolvedSource);
|
|
52
|
+
const targetStat = await optionalStat(resolvedTarget);
|
|
53
|
+
const sourceKind = kind(sourceStat);
|
|
54
|
+
validatePair(resolvedSource, sourceStat, resolvedTarget, targetStat);
|
|
55
|
+
if (
|
|
56
|
+
sourceKind === "directory" &&
|
|
57
|
+
(contains(resolvedSource, resolvedTarget) || contains(resolvedTarget, resolvedSource))
|
|
58
|
+
)
|
|
59
|
+
fail("OVERLAPPING_PATHS", "Source and target directory trees overlap.");
|
|
60
|
+
if (operation === "move") await assertUnprotectedTransferPath(resolvedSource, cwd, signal);
|
|
61
|
+
if (operation === "move" || sourceKind !== "file")
|
|
62
|
+
await assertUnprotectedTransferPath(resolvedTarget, cwd, signal);
|
|
63
|
+
const entries: unknown[] = [resolvedSource, resolvedTarget];
|
|
64
|
+
await inspectTree(resolvedSource, resolvedTarget, operation === "copy", entries, signal);
|
|
65
|
+
// Move removes the destination as one object; it does not merge child paths.
|
|
66
|
+
if (operation === "move") entries.push(snapshot(targetStat));
|
|
67
|
+
return {
|
|
68
|
+
source: resolvedSource,
|
|
69
|
+
target: resolvedTarget,
|
|
70
|
+
sourceKind,
|
|
71
|
+
targetExists: targetStat !== undefined,
|
|
72
|
+
snapshot: JSON.stringify(entries),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function inspectTree(
|
|
77
|
+
source: string,
|
|
78
|
+
target: string,
|
|
79
|
+
merge: boolean,
|
|
80
|
+
entries: unknown[],
|
|
81
|
+
signal?: AbortSignal,
|
|
82
|
+
): Promise<void> {
|
|
83
|
+
signal?.throwIfAborted();
|
|
84
|
+
const sourceStat = await lstat(source);
|
|
85
|
+
const sourceKind = kind(sourceStat);
|
|
86
|
+
const targetStat = merge ? await optionalStat(target) : undefined;
|
|
87
|
+
if (merge) validatePair(source, sourceStat, target, targetStat);
|
|
88
|
+
entries.push(source, snapshot(sourceStat), snapshot(targetStat));
|
|
89
|
+
if (sourceKind === "symlink") entries.push(await readlink(source));
|
|
90
|
+
if (sourceKind !== "directory") return;
|
|
91
|
+
for (const name of (await readdir(source)).sort())
|
|
92
|
+
await inspectTree(path.join(source, name), path.join(target, name), merge, entries, signal);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function validatePair(
|
|
96
|
+
source: string,
|
|
97
|
+
sourceStat: Stats,
|
|
98
|
+
target: string,
|
|
99
|
+
targetStat: Stats | undefined,
|
|
100
|
+
) {
|
|
101
|
+
if (
|
|
102
|
+
source === target ||
|
|
103
|
+
(targetStat !== undefined &&
|
|
104
|
+
sourceStat.dev === targetStat.dev &&
|
|
105
|
+
sourceStat.ino === targetStat.ino)
|
|
106
|
+
)
|
|
107
|
+
fail("SAME_FILE", "Source and target are the same filesystem object.");
|
|
108
|
+
if (targetStat === undefined) return;
|
|
109
|
+
if (targetStat.isSymbolicLink())
|
|
110
|
+
fail("INVALID_FILE_TYPE", "Transfer destination must not be a symlink.");
|
|
111
|
+
if (kind(sourceStat) !== kind(targetStat))
|
|
112
|
+
fail("INVALID_FILE_TYPE", "Source and target must have the same object type.");
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function kind(stat: Stats): TransferKind {
|
|
116
|
+
if (stat.isSymbolicLink()) return "symlink";
|
|
117
|
+
if (stat.isDirectory()) return "directory";
|
|
118
|
+
if (stat.isFile()) return "file";
|
|
119
|
+
fail("INVALID_FILE_TYPE", "Transfers support regular files, directories, and symlinks only.");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function snapshot(stat: Stats | undefined) {
|
|
123
|
+
return stat === undefined
|
|
124
|
+
? null
|
|
125
|
+
: [stat.dev, stat.ino, stat.mode, stat.size, stat.birthtimeMs, stat.ctimeMs, stat.mtimeMs];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async function optionalStat(file: string) {
|
|
129
|
+
try {
|
|
130
|
+
return await lstat(file);
|
|
131
|
+
} catch (error) {
|
|
132
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
|
|
133
|
+
throw error;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async function resolveLeaf(file: string): Promise<string> {
|
|
138
|
+
return path.join(await resolveParent(path.dirname(file)), path.basename(file));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function resolveParent(directory: string): Promise<string> {
|
|
142
|
+
try {
|
|
143
|
+
return await realpath(directory);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
146
|
+
// A dangling parent link is not a missing directory that we may create.
|
|
147
|
+
if (await optionalStat(directory)) throw error;
|
|
148
|
+
return path.join(await resolveParent(path.dirname(directory)), path.basename(directory));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function contains(parent: string, child: string) {
|
|
153
|
+
const relative = path.relative(parent, child);
|
|
154
|
+
return (
|
|
155
|
+
relative === "" ||
|
|
156
|
+
// This compares containment, not a parent-relative filesystem argument.
|
|
157
|
+
// eslint-disable-next-line repo/no-parent-paths
|
|
158
|
+
(!path.isAbsolute(relative) && relative !== ".." && !relative.startsWith(`..${path.sep}`))
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function fail(code: string, message: string): never {
|
|
163
|
+
throw Object.assign(new Error(message), { code });
|
|
164
|
+
}
|
package/src/extensions/pi-agent-text-editor/src/core/mutation-result/file-mutation-agent-result.ts
CHANGED
|
@@ -220,14 +220,9 @@ export class FileMutationAgentResult {
|
|
|
220
220
|
}
|
|
221
221
|
}
|
|
222
222
|
|
|
223
|
-
// A
|
|
224
|
-
// already applied by the batch.
|
|
225
|
-
const isMutationRejected = fmr.errors.some((error) => error.code === "MUTATION_REJECTED");
|
|
226
|
-
|
|
223
|
+
// A failed call alone does not prove that files stayed unchanged.
|
|
227
224
|
if (fmr.fileChangedStatement !== undefined) {
|
|
228
225
|
lines.push("", fmr.fileChangedStatement);
|
|
229
|
-
} else if (!isMutationRejected) {
|
|
230
|
-
lines.push("", "No file was changed.");
|
|
231
226
|
}
|
|
232
227
|
|
|
233
228
|
// File-level errors (WriteResultDetails blocked/failed, etc.)
|
|
@@ -287,6 +282,27 @@ export class FileMutationAgentResult {
|
|
|
287
282
|
|
|
288
283
|
// ── Success formatting ────────────────────────────────────────────────
|
|
289
284
|
|
|
285
|
+
private formatFormatting(formatting: FileMutationResult["data"]["formatting"]): string {
|
|
286
|
+
const by = formatting?.formatter === undefined ? "" : ` (${formatting.formatter})`;
|
|
287
|
+
switch (formatting?.status) {
|
|
288
|
+
case "changed": {
|
|
289
|
+
return `Formatted${by}.`;
|
|
290
|
+
}
|
|
291
|
+
case "unchanged": {
|
|
292
|
+
return `Already formatted${by}.`;
|
|
293
|
+
}
|
|
294
|
+
case "unavailable": {
|
|
295
|
+
return "No formatter is available for this file.";
|
|
296
|
+
}
|
|
297
|
+
case "failed": {
|
|
298
|
+
return `Formatting failed${by}. Read the saved file before retrying.`;
|
|
299
|
+
}
|
|
300
|
+
default: {
|
|
301
|
+
return `Formatting: ${formatting?.status ?? "not-reported"}${by}.`;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
290
306
|
private formatSuccess(fmr: FileMutationResult): string {
|
|
291
307
|
const operations = fmr.data.operations ?? [];
|
|
292
308
|
const verbs: Record<string, string> = {
|
|
@@ -295,7 +311,6 @@ export class FileMutationAgentResult {
|
|
|
295
311
|
delete: "Deleted selected text",
|
|
296
312
|
copy: "Copied selected text",
|
|
297
313
|
move: "Moved selected text",
|
|
298
|
-
write: "Wrote file content",
|
|
299
314
|
undo: "Restored file content",
|
|
300
315
|
stage: "Staged selected changes",
|
|
301
316
|
unstage: "Unstaged selected changes",
|
|
@@ -304,15 +319,24 @@ export class FileMutationAgentResult {
|
|
|
304
319
|
operations.length === 0
|
|
305
320
|
? "Changes applied."
|
|
306
321
|
: operations
|
|
307
|
-
.map(
|
|
308
|
-
|
|
309
|
-
|
|
322
|
+
.map(({ operation, changes }) =>
|
|
323
|
+
operation === "write"
|
|
324
|
+
? "Saved file."
|
|
325
|
+
: `${verbs[operation] ?? `Applied ${operation}`}: ${changes} text change(s) in this file.`,
|
|
310
326
|
)
|
|
311
327
|
.join("\n"),
|
|
312
|
-
|
|
328
|
+
this.formatFormatting(fmr.data.formatting),
|
|
313
329
|
];
|
|
314
330
|
|
|
315
|
-
for (const status of fmr.data.diffStatuses ?? [])
|
|
331
|
+
for (const status of fmr.data.diffStatuses ?? []) {
|
|
332
|
+
if (
|
|
333
|
+
status.formattingStatus !== undefined &&
|
|
334
|
+
status.formattingStatus === fmr.data.formatting?.status &&
|
|
335
|
+
status.formatter === fmr.data.formatting.formatter
|
|
336
|
+
)
|
|
337
|
+
continue;
|
|
338
|
+
blocks.push(status.text);
|
|
339
|
+
}
|
|
316
340
|
for (const diff of fmr.diffs) {
|
|
317
341
|
const path = /^--- (.+)$/m.exec(diff)?.[1] ?? fmr.path ?? "<unknown file>";
|
|
318
342
|
blocks.push(renderFinalStateFragment(fmr, path).join("\n"));
|
package/src/extensions/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts
CHANGED
|
@@ -29,6 +29,8 @@ export interface MutationFormatting {
|
|
|
29
29
|
export interface MutationDiffStatus {
|
|
30
30
|
/** Formatter identity allows multi-file summaries without parsing display text. */
|
|
31
31
|
readonly formatter?: string;
|
|
32
|
+
/** Observed formatter state for this label, kept distinct from file edit counts. */
|
|
33
|
+
readonly formattingStatus?: MutationFormatting["status"];
|
|
32
34
|
readonly text: string;
|
|
33
35
|
readonly tone?: "muted" | "success" | "warning" | "error";
|
|
34
36
|
}
|
|
@@ -236,6 +238,14 @@ export interface FileMutationData {
|
|
|
236
238
|
|
|
237
239
|
staleDiagnostic?: StaleAnchorDiagnostic | undefined;
|
|
238
240
|
fileChangedStatement?: string | undefined;
|
|
241
|
+
/** Failed rollback resources; an empty list means all attempted writes were restored. */
|
|
242
|
+
rollback?:
|
|
243
|
+
| {
|
|
244
|
+
readonly failedSources: readonly string[];
|
|
245
|
+
/** Existing resources whose restoration write succeeded. */
|
|
246
|
+
readonly restoredSources: readonly string[];
|
|
247
|
+
}
|
|
248
|
+
| undefined;
|
|
239
249
|
|
|
240
250
|
// ── Transient engine fields ──
|
|
241
251
|
snapshot?: MutationSnapshot | undefined;
|
|
@@ -325,7 +335,21 @@ export class FileMutationResult {
|
|
|
325
335
|
readonly data: FileMutationData;
|
|
326
336
|
|
|
327
337
|
public constructor(data?: FileMutationData) {
|
|
328
|
-
|
|
338
|
+
const formatting = data?.formatting;
|
|
339
|
+
this.data =
|
|
340
|
+
formatting === undefined || data?.diffStatuses === undefined
|
|
341
|
+
? (data ?? {})
|
|
342
|
+
: {
|
|
343
|
+
...data,
|
|
344
|
+
diffStatuses: data.diffStatuses.map((status) =>
|
|
345
|
+
status.formattingStatus !== undefined ||
|
|
346
|
+
formatting.formatter === undefined ||
|
|
347
|
+
status.formatter !== formatting.formatter ||
|
|
348
|
+
(status.tone !== "success" && status.tone !== "error")
|
|
349
|
+
? status
|
|
350
|
+
: { ...status, formattingStatus: formatting.status },
|
|
351
|
+
),
|
|
352
|
+
};
|
|
329
353
|
}
|
|
330
354
|
|
|
331
355
|
// ── Getters ──
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
1
3
|
import type { ResultRange, ResultSourceTarget } from "pi-agent-resource";
|
|
2
4
|
import type { ResultTargetStore } from "pi-agent-resource";
|
|
3
5
|
import type { AgentToolResult } from "@earendil-works/pi-coding-agent";
|
|
4
6
|
import type { FileMutationBatchResult } from "#src/api/mutation-result.js";
|
|
5
7
|
import { createTextDocument } from "pi-agent-text";
|
|
6
8
|
import type { TextEditCompletion } from "#src/api/edit-completion.js";
|
|
7
|
-
import type { TextMutationEdit } from "#src/api/mutation-tool.js";
|
|
9
|
+
import type { TextMutation, TextMutationEdit } from "#src/api/mutation-tool.js";
|
|
8
10
|
import { applyTextChanges } from "./text-change-engine.js";
|
|
9
11
|
import { attachFileMutationTargets } from "./file-result-targets.js";
|
|
10
12
|
|
|
@@ -14,10 +16,36 @@ export interface OwnedMutationChanges {
|
|
|
14
16
|
readonly edits: ReadonlyMap<string, TextMutationEdit>;
|
|
15
17
|
}
|
|
16
18
|
|
|
17
|
-
/**
|
|
19
|
+
/** Find Copy destinations with identical text and no secondary resource action. */
|
|
20
|
+
export function unchangedCopySources(
|
|
21
|
+
mutation: TextMutation,
|
|
22
|
+
snapshots: ReadonlyMap<string, string>,
|
|
23
|
+
): ReadonlySet<string> {
|
|
24
|
+
if (mutation.afterWrite !== undefined || mutation.semanticAction !== undefined) return new Set();
|
|
25
|
+
return new Set(
|
|
26
|
+
[...mutation.edits]
|
|
27
|
+
.filter(([source, edit]) => {
|
|
28
|
+
const before = snapshots.get(source);
|
|
29
|
+
return (
|
|
30
|
+
before !== undefined &&
|
|
31
|
+
edit.changes.length > 0 &&
|
|
32
|
+
edit.changes.every(
|
|
33
|
+
(change) =>
|
|
34
|
+
change.allowUnchanged && before.slice(change.from, change.to) === change.insert,
|
|
35
|
+
)
|
|
36
|
+
);
|
|
37
|
+
})
|
|
38
|
+
.map(([source]) => source),
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
/** Map final resource snapshots and unchanged Copy ranges; verify before publishing. */
|
|
18
42
|
export function committedMutationTargets(
|
|
19
43
|
mutations: readonly OwnedMutationChanges[],
|
|
20
|
-
completions: readonly
|
|
44
|
+
completions: readonly Pick<
|
|
45
|
+
TextEditCompletion,
|
|
46
|
+
"source" | "resourceSource" | "resolvedBy" | "before" | "after" | "existed"
|
|
47
|
+
>[],
|
|
48
|
+
unchangedSnapshots: ReadonlyMap<string, string> = new Map(),
|
|
21
49
|
): ReadonlyMap<string, readonly ResultSourceTarget[]> {
|
|
22
50
|
const targets = new Map<string, ResultSourceTarget[]>();
|
|
23
51
|
const sources = new Set(mutations.flatMap((mutation) => [...mutation.edits.keys()]));
|
|
@@ -25,7 +53,7 @@ export function committedMutationTargets(
|
|
|
25
53
|
const completion = completions.findLast(
|
|
26
54
|
(item) => item.source === source || item.resourceSource === source,
|
|
27
55
|
);
|
|
28
|
-
if (
|
|
56
|
+
if (completion && completion.resolvedBy !== "filesystem") continue;
|
|
29
57
|
const changes = mutations
|
|
30
58
|
.flatMap((mutation) =>
|
|
31
59
|
(mutation.edits.get(source)?.changes ?? []).map((change, index) => ({
|
|
@@ -35,12 +63,22 @@ export function committedMutationTargets(
|
|
|
35
63
|
})),
|
|
36
64
|
)
|
|
37
65
|
.sort((left, right) => left.from - right.from);
|
|
38
|
-
const before = completion
|
|
39
|
-
|
|
40
|
-
if (
|
|
66
|
+
const before = completion?.before.content ?? unchangedSnapshots.get(source);
|
|
67
|
+
if (before === undefined) continue;
|
|
68
|
+
if (
|
|
69
|
+
!completion &&
|
|
70
|
+
changes.some(
|
|
71
|
+
(change) =>
|
|
72
|
+
!change.allowUnchanged || before.slice(change.from, change.to) !== change.insert,
|
|
73
|
+
)
|
|
74
|
+
)
|
|
75
|
+
continue;
|
|
76
|
+
const after = completion?.after.content ?? before;
|
|
77
|
+
const resourceSource = completion?.resourceSource ?? source;
|
|
78
|
+
if (applyTextChanges(before, changes, completion?.existed === false).content !== after)
|
|
41
79
|
throw new Error("Mutation targets cannot be mapped to the actual written snapshot.");
|
|
42
80
|
const starts = [0];
|
|
43
|
-
for (const line of createTextDocument(
|
|
81
|
+
for (const line of createTextDocument(resourceSource, after).lines)
|
|
44
82
|
if (line.lineEnding.length > 0)
|
|
45
83
|
starts.push((starts.at(-1) ?? 0) + line.content.length + line.lineEnding.length);
|
|
46
84
|
const position = (offset: number): ResultRange["start"] => {
|
|
@@ -71,7 +109,7 @@ export function committedMutationTargets(
|
|
|
71
109
|
for (const [callId, selected] of ranges) {
|
|
72
110
|
const own = targets.get(callId) ?? [];
|
|
73
111
|
own.push({
|
|
74
|
-
source:
|
|
112
|
+
source: resourceSource,
|
|
75
113
|
expectedContent: after,
|
|
76
114
|
ranges: selected,
|
|
77
115
|
});
|
|
@@ -81,6 +119,81 @@ export function committedMutationTargets(
|
|
|
81
119
|
return targets;
|
|
82
120
|
}
|
|
83
121
|
|
|
122
|
+
/** Explain missing Copy authority without changing its write outcome. */
|
|
123
|
+
export function describeUnavailableCopyTarget(
|
|
124
|
+
result: AgentToolResult<FileMutationBatchResult>,
|
|
125
|
+
): AgentToolResult<FileMutationBatchResult> {
|
|
126
|
+
const notice = "No verified text selection. Read the destination before further edits.";
|
|
127
|
+
if (
|
|
128
|
+
result.isError ||
|
|
129
|
+
typeof result.details.metadata?.targetUnavailable !== "string" ||
|
|
130
|
+
result.content.some((block) => block.type === "text" && block.text === notice)
|
|
131
|
+
)
|
|
132
|
+
return result;
|
|
133
|
+
return {
|
|
134
|
+
...result,
|
|
135
|
+
content: [
|
|
136
|
+
...result.content,
|
|
137
|
+
{
|
|
138
|
+
type: "text",
|
|
139
|
+
text: notice,
|
|
140
|
+
},
|
|
141
|
+
],
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Publish filesystem Write snapshots, never grant file authority to live resources. */
|
|
146
|
+
export async function attachWriteTarget(
|
|
147
|
+
result: AgentToolResult<FileMutationBatchResult>,
|
|
148
|
+
completions: readonly TextEditCompletion[],
|
|
149
|
+
store: ResultTargetStore,
|
|
150
|
+
cwd: string,
|
|
151
|
+
signal?: AbortSignal,
|
|
152
|
+
): Promise<AgentToolResult<FileMutationBatchResult>> {
|
|
153
|
+
if (result.isError) return result;
|
|
154
|
+
const file = result.details.results?.[0]?.data;
|
|
155
|
+
if (file?.path === undefined || file.afterContent === undefined) return result;
|
|
156
|
+
if (completions.some((completion) => completion.resolvedBy !== "filesystem")) return result;
|
|
157
|
+
try {
|
|
158
|
+
const content = file.afterContent;
|
|
159
|
+
const lines = content.split(/\r\n|\r|\n/u);
|
|
160
|
+
const targets: ResultSourceTarget[] = [
|
|
161
|
+
{
|
|
162
|
+
source: path.resolve(
|
|
163
|
+
cwd,
|
|
164
|
+
file.path.startsWith("file://") ? fileURLToPath(file.path) : file.path,
|
|
165
|
+
),
|
|
166
|
+
expectedContent: content,
|
|
167
|
+
ranges: [
|
|
168
|
+
{
|
|
169
|
+
start: { lineNumber: 1, column: 0 },
|
|
170
|
+
end: { lineNumber: lines.length, column: lines.at(-1)?.length ?? 0 },
|
|
171
|
+
},
|
|
172
|
+
],
|
|
173
|
+
},
|
|
174
|
+
];
|
|
175
|
+
await store.verify({ targets, complete: true }, signal);
|
|
176
|
+
return {
|
|
177
|
+
...result,
|
|
178
|
+
details: {
|
|
179
|
+
...result.details,
|
|
180
|
+
metadata: { ...result.details.metadata, resultTarget: store.register(targets, cwd) },
|
|
181
|
+
},
|
|
182
|
+
};
|
|
183
|
+
} catch (error) {
|
|
184
|
+
signal?.throwIfAborted();
|
|
185
|
+
return {
|
|
186
|
+
...result,
|
|
187
|
+
details: {
|
|
188
|
+
...result.details,
|
|
189
|
+
metadata: {
|
|
190
|
+
...result.details.metadata,
|
|
191
|
+
targetUnavailable: error instanceof Error ? error.message : String(error),
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
}
|
|
84
197
|
/** Publish immediate results only when their committed range mapping is verified. */
|
|
85
198
|
export async function attachCommittedMutationTarget(
|
|
86
199
|
result: AgentToolResult<FileMutationBatchResult>,
|
|
@@ -92,8 +205,14 @@ export async function attachCommittedMutationTarget(
|
|
|
92
205
|
wholeFile = false,
|
|
93
206
|
plannedEdits?: ReadonlyMap<string, TextMutationEdit>,
|
|
94
207
|
destinationOnly = false,
|
|
208
|
+
unchangedSnapshots: ReadonlyMap<string, string> = new Map(),
|
|
95
209
|
): Promise<AgentToolResult<FileMutationBatchResult>> {
|
|
96
|
-
if (
|
|
210
|
+
if (
|
|
211
|
+
result.isError ||
|
|
212
|
+
typeof result.details.metadata?.resultTarget === "string" ||
|
|
213
|
+
completions.some((completion) => completion.resolvedBy !== "filesystem")
|
|
214
|
+
)
|
|
215
|
+
return result;
|
|
97
216
|
result = await attachFileMutationTargets(result, store, cwd, signal);
|
|
98
217
|
if (
|
|
99
218
|
typeof result.details.metadata?.resultTarget === "string" ||
|
|
@@ -135,9 +254,11 @@ export async function attachCommittedMutationTarget(
|
|
|
135
254
|
},
|
|
136
255
|
);
|
|
137
256
|
}
|
|
257
|
+
for (const [source, edit] of plannedEdits ?? [])
|
|
258
|
+
if (!edits.has(source) && unchangedSnapshots.has(source)) edits.set(source, edit);
|
|
138
259
|
if (edits.size === 0 && result.details.metadata?.emptyTargets !== true) return result;
|
|
139
260
|
try {
|
|
140
|
-
const mapped = committedMutationTargets([{ callId, edits }], completions);
|
|
261
|
+
const mapped = committedMutationTargets([{ callId, edits }], completions, unchangedSnapshots);
|
|
141
262
|
const targets = mapped.get(callId) ?? [];
|
|
142
263
|
const outputCount = [...edits.values()].filter(
|
|
143
264
|
(edit) => edit.resultChanges?.length !== 0,
|