@myagentroam/agent 0.9.62 → 0.9.64
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/LICENSE +202 -0
- package/NOTICE +4 -0
- package/dist/cli/codex-auth-file.js +268 -3
- package/dist/cli/main.js +427 -2
- package/dist/error.js +66 -2
- package/dist/host/contracts.js +20 -2
- package/dist/host/local-agent-host.js +146 -2
- package/dist/host/local-process-executor.js +39 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +12 -2
- package/dist/model/anthropic-messages.js +474 -2
- package/dist/model/anthropic-thinking.js +33 -2
- package/dist/model/catalog-file.js +88 -2
- package/dist/model/configuration.js +137 -2
- package/dist/model/contracts.d.ts +2 -0
- package/dist/model/contracts.js +1 -2
- package/dist/model/http.js +529 -2
- package/dist/model/openai-responses.d.ts +2 -0
- package/dist/model/openai-responses.js +1602 -2
- package/dist/model/runtime-credential.js +35 -2
- package/dist/model/sse.js +199 -2
- package/dist/prompts/compact.js +25 -2
- package/dist/prompts/core.js +37 -2
- package/dist/prompts/execution-budget.js +10 -2
- package/dist/prompts/index.d.ts +3 -2
- package/dist/prompts/index.js +47 -2
- package/dist/prompts/modes.js +25 -2
- package/dist/prompts/output.js +1 -2
- package/dist/prompts/resources.d.ts +1 -1
- package/dist/prompts/resources.js +31 -2
- package/dist/prompts/subagent.d.ts +6 -0
- package/dist/prompts/subagent.js +29 -2
- package/dist/prompts/title.js +9 -2
- package/dist/prompts/workflow.js +32 -2
- package/dist/runtime/async-queue.js +62 -2
- package/dist/runtime/builtin-skills.js +19 -2
- package/dist/runtime/byted-ark-imagegen-skill.js +18 -2
- package/dist/runtime/codex-imagegen-skill.js +35 -2
- package/dist/runtime/compact.js +225 -2
- package/dist/runtime/context-gc-runtime.d.ts +35 -0
- package/dist/runtime/context-gc-runtime.js +163 -0
- package/dist/runtime/context-gc-tokens.d.ts +24 -0
- package/dist/runtime/context-gc-tokens.js +83 -0
- package/dist/runtime/context-gc.d.ts +66 -0
- package/dist/runtime/context-gc.js +909 -0
- package/dist/runtime/context-projection.d.ts +23 -0
- package/dist/runtime/context-projection.js +203 -0
- package/dist/runtime/environment-context.js +41 -2
- package/dist/runtime/execution-policy.js +13 -2
- package/dist/runtime/instructions.js +298 -2
- package/dist/runtime/normalize.js +40 -2
- package/dist/runtime/plan-output.js +95 -2
- package/dist/runtime/rollout-budget.d.ts +2 -0
- package/dist/runtime/rollout-budget.js +92 -2
- package/dist/runtime/token-budget.d.ts +2 -0
- package/dist/runtime/token-budget.js +44 -2
- package/dist/sdk/agent.js +1804 -2
- package/dist/sdk/types.d.ts +15 -5
- package/dist/sdk/types.js +1 -2
- package/dist/session/catalog.d.ts +5 -4
- package/dist/session/catalog.js +133 -2
- package/dist/session/context-gc-checkpoint.d.ts +148 -0
- package/dist/session/context-gc-checkpoint.js +665 -0
- package/dist/session/context-gc-replacements.d.ts +52 -0
- package/dist/session/context-gc-replacements.js +226 -0
- package/dist/session/context-gc-subagent.d.ts +43 -0
- package/dist/session/context-gc-subagent.js +267 -0
- package/dist/session/jsonl-store.d.ts +42 -4
- package/dist/session/jsonl-store.js +1657 -2
- package/dist/subagent/scheduler.d.ts +35 -2
- package/dist/subagent/scheduler.js +249 -2
- package/dist/subagent/session-controller.d.ts +94 -0
- package/dist/subagent/session-controller.js +675 -0
- package/dist/tools/agent-cancel.js +28 -2
- package/dist/tools/{task.d.ts → agent-message.d.ts} +2 -2
- package/dist/tools/agent-message.js +51 -0
- package/dist/tools/agent-start.js +84 -2
- package/dist/tools/agent-wait.js +53 -2
- package/dist/tools/apply-patch.js +478 -2
- package/dist/tools/code-mode.js +370 -2
- package/dist/tools/codex-image-gen.js +312 -2
- package/dist/tools/exec.js +455 -2
- package/dist/tools/execution-limits.d.ts +1 -0
- package/dist/tools/execution-limits.js +44 -2
- package/dist/tools/local-registry.js +233 -2
- package/dist/tools/local-workspace-tools.js +28 -2
- package/dist/tools/question.js +124 -2
- package/dist/tools/read-context.d.ts +16 -0
- package/dist/tools/read-context.js +85 -0
- package/dist/tools/read.d.ts +13 -0
- package/dist/tools/read.js +294 -2
- package/dist/tools/registry.js +76 -2
- package/dist/tools/runtime-context.d.ts +9 -2
- package/dist/tools/runtime-context.js +1 -2
- package/dist/tools/runtime.js +21 -2
- package/dist/tools/scheduler.js +41 -2
- package/dist/tools/shared/path-resolver.js +133 -2
- package/dist/tools/skill.js +37 -2
- package/dist/tools/todo-read.js +10 -2
- package/dist/tools/todo-write.js +65 -2
- package/dist/tools/todo.js +67 -2
- package/dist/tools/view-image.js +81 -2
- package/dist/tools/web-fetch.js +11 -2
- package/dist/tools/web-search.js +14 -2
- package/package.json +9 -7
- package/README.md +0 -3
- package/dist/runtime/prompt.d.ts +0 -9
- package/dist/runtime/prompt.js +0 -2
- package/dist/tools/shared/secret-redaction.d.ts +0 -4
- package/dist/tools/shared/secret-redaction.js +0 -2
- package/dist/tools/task-cancel.d.ts +0 -7
- package/dist/tools/task-cancel.js +0 -2
- package/dist/tools/task-output.d.ts +0 -7
- package/dist/tools/task-output.js +0 -2
- package/dist/tools/task-tools.d.ts +0 -36
- package/dist/tools/task-tools.js +0 -2
- package/dist/tools/task.js +0 -2
- package/dist/tools/todo-tools.d.ts +0 -25
- package/dist/tools/todo-tools.js +0 -2
|
@@ -1,2 +1,478 @@
|
|
|
1
|
-
|
|
2
|
-
const _0x4c3e54=_0x566f;(function(_0x2a8053,_0x33bfb4){const _0x3746b9=_0x566f,_0x2f05f0=_0x2a8053();while(!![]){try{const _0x5ec849=-parseInt(_0x3746b9(0x183))/(0x1735+0x62f+-0x1*0x1d63)+parseInt(_0x3746b9(0x164))/(0xa05+0x2*-0x12b9+0x1*0x1b6f)*(-parseInt(_0x3746b9(0x194))/(-0xfc*-0x4+0x1feb*-0x1+0x1bfe))+-parseInt(_0x3746b9(0x180))/(0x140c+-0x1bbe+0x292*0x3)+-parseInt(_0x3746b9(0x163))/(-0x1*-0x2d9+0x2d6+0x19*-0x3a)+-parseInt(_0x3746b9(0x188))/(-0x21f5+0x139f+0xe5c)+-parseInt(_0x3746b9(0x1a6))/(-0x940+0x12a3+0x4ae*-0x2)*(-parseInt(_0x3746b9(0x16b))/(0x163a+0x47c*0x5+-0x164f*0x2))+parseInt(_0x3746b9(0x1bc))/(-0x4c1*-0x1+-0x65+-0x453)*(parseInt(_0x3746b9(0x156))/(-0x1eb9+-0x20ac+0x3f6f));if(_0x5ec849===_0x33bfb4)break;else _0x2f05f0['push'](_0x2f05f0['shift']());}catch(_0x4371fb){_0x2f05f0['push'](_0x2f05f0['shift']());}}}(_0x4557,0x9424a+0x65985+-0x9b050));function _0x566f(_0x563b5b,_0x145426){_0x563b5b=_0x563b5b-(0x1732*-0x1+0x36d*0x8+0x2*-0x179);const _0x147b1f=_0x4557();let _0x27021b=_0x147b1f[_0x563b5b];if(_0x566f['ItCnCt']===undefined){var _0x3f159d=function(_0x33044b){const _0x3c6e01='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=';let _0xd322da='',_0x562271='';for(let _0x9f1ad8=-0x2228+0x887+0x2d9*0x9,_0x521b9b,_0x46d92a,_0x28c25c=0x1f29*-0x1+-0x1bb4+0x3add;_0x46d92a=_0x33044b['charAt'](_0x28c25c++);~_0x46d92a&&(_0x521b9b=_0x9f1ad8%(0x1*-0x1619+-0xc3*-0x28+-0x85b*0x1)?_0x521b9b*(0x10*0x16d+-0x1*0xd9d+-0x8f3)+_0x46d92a:_0x46d92a,_0x9f1ad8++%(0x1*-0xb7d+0x2*0x282+0x67d))?_0xd322da+=String['fromCharCode'](0x10*-0x225+-0x71f*0x1+0x2a6e&_0x521b9b>>(-(0x1e03+0x2*0xdd3+-0x39a7)*_0x9f1ad8&0xc*-0x40+-0x1a6*0x7+-0x3a4*-0x4)):0x1f53+-0xa4*0x26+-0x6fb){_0x46d92a=_0x3c6e01['indexOf'](_0x46d92a);}for(let _0x2e7884=0x199*0x6+-0x2b*-0x22+-0xf4c,_0xdf8fdf=_0xd322da['length'];_0x2e7884<_0xdf8fdf;_0x2e7884++){_0x562271+='%'+('00'+_0xd322da['charCodeAt'](_0x2e7884)['toString'](0x100*-0x8+0x22*0x1d+0x436))['slice'](-(0xb98+0x1ac5+-0x265b));}return decodeURIComponent(_0x562271);};_0x566f['nAiQgD']=_0x3f159d,_0x566f['lseFQs']={},_0x566f['ItCnCt']=!![];}const _0x34d993=_0x147b1f[0x3*-0x8a7+0x1*0x232f+-0x49d*0x2];_0x566f['UZUYon']!==_0x34d993&&(_0x566f['lseFQs']={},_0x566f['UZUYon']=_0x34d993);const _0x56d360=_0x566f['lseFQs'][_0x563b5b];return _0x56d360===undefined?(_0x27021b=_0x566f['nAiQgD'](_0x27021b),_0x566f['lseFQs'][_0x563b5b]=_0x27021b):_0x27021b=_0x56d360,_0x27021b;}import{randomUUID}from'node:crypto';import{chmod,lstat,mkdir,readFile,rename,rm,rmdir,writeFile}from'node:fs/promises';import{dirname,isAbsolute,parse,resolve}from'node:path';import{TextDecoder}from'node:util';import{MarAgentError}from'../error.js';import{TOOL_EXECUTION_LIMITS}from'./execution-limits.js';export const applyPatchToolDefinition={'name':_0x4c3e54(0x1ba),'parallelSafety':_0x4c3e54(0x16f),'description':_0x4c3e54(0x17f)+TOOL_EXECUTION_LIMITS[_0x4c3e54(0x197)]/(0x19223*-0x13+-0x15d7f3+0x43b08c)+_0x4c3e54(0x158)+TOOL_EXECUTION_LIMITS[_0x4c3e54(0x1b2)]+_0x4c3e54(0x17a)+TOOL_EXECUTION_LIMITS[_0x4c3e54(0x17b)]/(0xc47a9+0x182381+-0x146b2a)+_0x4c3e54(0x161)+TOOL_EXECUTION_LIMITS[_0x4c3e54(0x151)]/(0x2*0xc7acb+0x105d62+-0xca97c*0x2)+_0x4c3e54(0x146),'inputMode':_0x4c3e54(0x148),'outputSchema':{'type':_0x4c3e54(0x1c8),'required':[_0x4c3e54(0x170),_0x4c3e54(0x195)],'properties':{'content':{'type':_0x4c3e54(0x1d1)},'data':{'type':_0x4c3e54(0x1c8),'required':[_0x4c3e54(0x1a2),_0x4c3e54(0x1c9)],'properties':{'files':{'type':_0x4c3e54(0x15e),'items':{'type':_0x4c3e54(0x1c8),'required':[_0x4c3e54(0x191),_0x4c3e54(0x1cf),_0x4c3e54(0x182),_0x4c3e54(0x166)],'properties':{'path':{'type':_0x4c3e54(0x1d1)},'targetPath':{'type':_0x4c3e54(0x1d1)},'operation':{'enum':[_0x4c3e54(0x1aa),_0x4c3e54(0x15c),_0x4c3e54(0x1c4),_0x4c3e54(0x19f)]},'addedLines':{'type':_0x4c3e54(0x153)},'deletedLines':{'type':_0x4c3e54(0x153)}}}},'totals':{'type':_0x4c3e54(0x1c8),'required':[_0x4c3e54(0x1a2),_0x4c3e54(0x182),_0x4c3e54(0x166)],'properties':{'files':{'type':_0x4c3e54(0x153)},'addedLines':{'type':_0x4c3e54(0x153)},'deletedLines':{'type':_0x4c3e54(0x153)}}}}}}},'inputSchema':{'type':_0x4c3e54(0x1c8),'properties':{'patch':{'type':_0x4c3e54(0x1d1)}},'required':[_0x4c3e54(0x19d)],'additionalProperties':![]}};function _0x4557(){const _0x48f22c=['ugf0y2GGAhvUAYbSAw5LCYbTDxn0ihn0yxj0ihDPDgGGC3bHy2uSicSSig9Yic0U','Aw5JBhvKzxm','uhjLzMvYCMvKigzVCIbWCMvJAxnLlcbYzxzPzxDHyMXLigXVy2fSigvKAxrZlIbhCM91CcbZBwfSBcWGCMvSyxrLzcbPBxbSzw1LBNrHDgLVBIbHBMqGDgvZDcbJAgfUz2vZigLUig9UzsbWyxrJAcb3AgvUihrOzxKGyxjLigfSCMvHzhKGDw5KzxjZDg9Vzc4Gu3bSAxqGBgfYz2uGB3iGAw5KzxbLBMrLBNqGy2HHBMDLCYWGyw5KihDHAxqGzM9YigLUDgvYBwvKAwf0zsbYzxn1BhrZig9UBhKGD2HLBIb0AguGBMv4DcbLzgL0igrLCgvUzhmGB24GDgHLBs4Gu3vIBwL0igeGCMf3icOQkIbczwDPBIbqyxrJAcaVicOQkIbfBMqGugf0y2GGCgf0y2GUifn1ChbVCNrZiefKzcbgAwXLlcbvCgrHDguGrMLSzsWGrgvSzxrLiezPBguSigfUzcbnB3zLihrVlIbvCgrHDguGAhvUA3mGywnJzxb0iebaig9YiebaidXJAgfUz2uGy29UDgv4Dd47ignVBNrLEhqTB25SEsbODw5RCYbTyxKGywXZBYbIzsb1C2vKigfZig9YzgvYzwqGyw5JAg9YCYbIzwzVCMuGysbSyxrLCIbJAgfUz2LUzYbODw5RlIbdB250zxH0igLZihnLyxjJAgvKigzVCNDHCMqGD2L0AcbdB2rLEc1ZDhLSzsbLEgfJDcWGD2HPDgvZCgfJzs10B2XLCMfUDcWGDgHLBIbJB21TB24Gvw5Py29KzsbWDw5JDhvHDgLVBI10B2XLCMfUDcbTyxrJAgLUzY4GvgHLignVBxbSzxrLihbHDgnOigLZihzHBgLKyxrLzcbHBMqGy29TBwL0DgvKigf0B21Py2fSBhKUiev4Axn0Aw5NigzPBguGCgvYBwLZC2LVBNmSigXPBMuGzw5KAw5NCYWGyw5KigzPBMfSlw5LD2XPBMuGC3rHDguGyxjLihbYzxnLCNzLzc4GuMv0DxjUCYbJAgfUz2vKigzPBguGCgf0AhmGyw5Kig9WzxjHDgLVBNmGD2L0AcbWzxiTzMLSzsbHBMqGDg90ywWGBgLUzsbJB3vUDhmUieXPBwL0CZOG','mJqXmJq4ogHrseHZDW','y2HHBMDLq29UDgv4Da','ywrKzwrmAw5LCW','nZa2ntKYwKLxD0js','BgvUz3rO','ru5pru5u','tufsx0fhru5ux1bbvenix1jpteXcqunlx0zbsuXfra','AgfZ','mJy4ndKWnenYBe9UvW','C2XPy2u','yw5JAg9Y','Cgf0y2HmAw5LCW','Cgf0Ahm','zxzLCNK','qwrKzwqGzMLSzxmGBxvZDcbJB250ywLUigf0igXLyxn0ig9UzsbWyxrJAcbSAw5LlG','ugf0y2GGzxHJzwvKCYb0AguGAw5WDxqGyNL0zsbSAw1PDc4','AhvUA3m','Cgf0Aa','lM1HCI1WyxrJAc0','ugf0y2GGzxHJzwvKCYb0AguGzMLSzsbSAw1PDc4','mZzzAw1huwm','zgf0yq','CMvZB2X2zq','Cgf0y2HnyxHcExrLCW','tufsx0fhru5ux1bbvenix0nptKzmsunu','zxHWzwn0zwrfEgLZDgLUzW','ugf0y2GGzw52zwXVCguGAxmGAw52ywXPzc4','4OcC4OcD4OcE4OcF','ugf0y2GGy2HHBMDLignVBNrLEhqGBxvZDcbIzsbUB24Tzw1WDhKU','Cgf0y2G','yNL0zuXLBMD0Aa','Bw92zwq','DhjPBq','ugf0y2GGBxvZDcbIzsbHihn0CMLUzY4','zMLSzxm','ignVDwXKig5VDcbMAw5KignOyw5NzsbJB250zxH0igLUia','ugf0y2GGy29TBwL0igzHAwXLzcbHBMqGCM9SBgjHy2SGy291BgqGBM90ihjLC3rVCMuGzxzLCNKGzMLSzs4','DgfYz2v0','mJfrCer2yMe','zw50CMLLCW','ywXS','zgvSzxrL','ywrKzwq','DgfYz2v0ugf0Aa','tufsx0fhru5ux1rpt0XFt1vuufvux0XjtuLu','ugf0y2GGCgf0AhmGBxvZDcbIzsbUB24Tzw1WDhKGyw5KihvUCgfKzgvKlG','C2L6zq','ugf0y2GGy2HHBMDLignVBNrLEhqGAgvHzgvYCYbTDxn0ihvZzsbaqcbVCIbaqca8y29UDgv4Dd4U','ugf0y2GGC291CMnLCYbTDxn0igjLihjLz3vSyxiGzMLSzxmU','ugf0y2HLzcbMAwXLCYbTDxn0igjLihzHBgLKifvurI04lG','Cgf0y2HnyxHgAwXLCW','DxrMltG','CMvKDwnL','qeaG','ugf0y2HLzcbMAwXLCYbJB250ywLUig1PEgvKignHCNjPywDLihjLDhvYBNmU','vxbKyxrLig9WzxjHDgLVBNmGBxvZDcbJAgfUz2uGB3iGBw92zsbHigzPBguU','y29Kzq','C3rHCNrZv2L0Aa','yxbWBhLFCgf0y2G','lNrTCa','odfLEhzwsvO','C3bSAxq','BgLUzxm','A2LUza','ywrK','ugf0y2GGB3bLCMf0Aw9UigLZihvUC3vWCg9YDgvKlG','kIOQia','kIOQie1VDMuGDg86ia','zgvSzxrLza','ywXSu2v0DgXLza','4Ocy4Ocz4OcA4OcB','BwfW','B2jQzwn0','Dg90ywXZ','4Ocq4Ocr4Ocs4Oct4Ocu4Ocv4OIs','kIOQiefKzcbgAwXLoIa','ugf0y2GGy29UDgfPBNmGDw5ZDxbWB3j0zwqGyMfYzsbJyxjYAwfNzsbYzxr1CM5ZlG','ignVDwXKig5VDcbMAw5Kigv4CgvJDgvKigXPBMvZigLUia','kIOQierLBgv0zsbgAwXLoIa','B3bLCMf0Aw9U','CMv2zxjZzq','C3rYAw5N','AM9PBG','ugf0y2GGy29UDgfPBNmGzhvWBgLJyxrLig9Yig92zxjSyxbWAw5NihbHDgHZlG','ChvZAa','ie1PqIb3B3jRAw5NihnLDc4Gtufsx0fhru5ux1bbvenix0nptKzmsunuig1Lyw5Zig5VihbHCNrPywWGzwrPDcb3yxmGyxbWBgLLzdSGAhvUAYbTyxrJAcbJB25MBgLJDhmGAwrLBNrPzNKGDgHLigfMzMvJDgvKihbHDgGGyw5KigH1BMSGC28GEw91ignHBIbYzxjLywqGDgHHDcbMAwXLigjLzM9YzsbYzxrYEwLUzY4','cIOQkIbfBMqGugf0y2G','zNjLzwzVCM0','y29UDgv4Da','DxbKyxrL','Dg9tDhjPBMC','lMjHAW','kIOQiejLz2LUifbHDgnOcG','zNjVBq','AxngAwXL','qwrKzwqGzMLSzsbSAw5LCYbTDxn0ihn0yxj0ihDPDgGGkY4','Cgf0y2HnyxHuB3rHBej5DgvZ','D3jPDgu','BNvTyMvY','q29UDgv4Dc1VBMX5ihbHDgnOigH1BMTZig11C3qGChjLy2vKzsbHignOyw5NAw5NigH1BMSU','sw5Zzxj0Aw9UigH1BMTZihjLCxvPCMuGy29UDgv4Dc4','mJqXote1mfbzuuPuDa','ugf0y2GGD29YA2LUzYbZzxqGzxHJzwvKCYb0AguGyNL0zsbSAw1PDc4','ie1PqIbWyxrJAcbPBNb1DcWG','ugf0y2HLzcbMAwXLigv4y2vLzhmGDgHLigj5DguGBgLTAxqU','CMvWBgfJzufSBa','zgvSzxrPB24','Bw9KAwzPzwq','yMfJA3vW','yxjYyxK','WQdIGilIGipIGitIGixIGiBIGiFIGiJIGiNIGiRIGk/IGz/JGia','DgvTCg9Yyxj5','ie1PqIbWzxiGzMLSzsWGyw5Kia','C3bSAwnL','mZa2nJC2nujYCxH5DG','mtyXnZrAy05StK8','DhjPBuvUza','zgvSzxrLzeXPBMvZ','zxHLy3v0zq','y2f0y2G','CMvUyw1L','kIOQifvWzgf0zsbgAwXLoIa','mtGWodG3mKH5AKjRzW','ugf0y2GGAhvUAYa','ugf0y2GGBxvZDcbJB250ywLUigf0igXLyxn0ig9UzsbVCgvYyxrPB24U','tufsx0fhru5ux1rpt0XFsu5qvvrFsu5wquXjra','C2vYAwfS','y29UDgvUDa','ywrKAxrPB24','qsbWyxrJAcb0yxjNzxqGy2HHBMDLzcbHzNrLCIbWCMvMBgLNAhqU','B2XKtgLUzxm','Bw9Kzq','DMfSDwu','zw5KC1DPDgG','CM9VDa','ugf0y2GGDgfYz2v0igfSCMvHzhKGzxHPC3rZlG','DxrMoa','igzPBgvZlca','Cgf0y2HnyxHgAwXLqNL0zxm','zgvJB2rL'];_0x4557=function(){return _0x48f22c;};return _0x4557();}const defaultFileOperations={'rename':rename,'rm':rm},utf8Decoder=new TextDecoder(_0x4c3e54(0x1b3),{'fatal':!![]});export class ApplyPatchTool{[_0x4c3e54(0x18c)];#fileOperations;constructor(_0x38bf9f,_0x1df49a={}){const _0xbb0d8a=_0x4c3e54;this[_0xbb0d8a(0x18c)]=_0x38bf9f,this.#fileOperations={...defaultFileOperations,..._0x1df49a};}async[_0x4c3e54(0x167)](_0x34a7f1){const _0x3259ec=_0x4c3e54,_0xfc6da5=parsePatch(_0x34a7f1),{mutations:_0x430522,results:_0x5bc414}=await this.#preflight(_0xfc6da5);return await this.#commit(_0x430522),{'files':_0x5bc414,'totals':{'files':_0x5bc414[_0x3259ec(0x184)],'addedLines':_0x5bc414[_0x3259ec(0x1b4)]((_0x1793b1,_0x6de92d)=>_0x1793b1+_0x6de92d[_0x3259ec(0x182)],0x10*0x16d+-0x1*0xd9d+-0x933),'deletedLines':_0x5bc414[_0x3259ec(0x1b4)]((_0x53258e,_0x597059)=>_0x53258e+_0x597059[_0x3259ec(0x166)],0x1*-0xb7d+0x2*0x282+0x679)}};}async #preflight(_0x10333d){const _0x31cfde=_0x4c3e54,_0x1dfe63=[],_0x17c99b=[],_0x31f484=new Set(),_0x52d2cb=_0xf8632d=>{const _0x5f4e7c=_0x566f,_0x8127b4=resolve(_0xf8632d);if(_0x31f484[_0x5f4e7c(0x187)](_0x8127b4))invalid(_0x5f4e7c(0x144));_0x31f484[_0x5f4e7c(0x1c0)](_0x8127b4);};let _0x3cc6ad=0x10*-0x225+-0x71f*0x1+0x296f;for(const _0x133a44 of _0x10333d){if(_0x133a44[_0x31cfde(0x1bf)]===_0x31cfde(0x1c0)){const _0x3fae3b=await this[_0x31cfde(0x18c)][_0x31cfde(0x196)](_0x133a44[_0x31cfde(0x191)],!![],_0x31cfde(0x152));_0x52d2cb(_0x3fae3b),await assertMissing(_0x3fae3b);const _0x24223f=Buffer[_0x31cfde(0x14e)](_0x133a44[_0x31cfde(0x1be)][_0x31cfde(0x1d2)]('\x0a')+'\x0a');_0x3cc6ad=enforceContentLimits(_0x24223f,_0x3cc6ad),_0x1dfe63[_0x31cfde(0x145)]({'path':_0x133a44[_0x31cfde(0x191)],'target':_0x3fae3b,'content':_0x24223f,'expectedExisting':![]}),_0x17c99b[_0x31cfde(0x145)]({'path':_0x133a44[_0x31cfde(0x191)],'operation':_0x31cfde(0x1aa),'addedLines':_0x133a44[_0x31cfde(0x1be)][_0x31cfde(0x184)],'deletedLines':0x0});continue;}const _0x4261d6=await this[_0x31cfde(0x18c)][_0x31cfde(0x196)](_0x133a44[_0x31cfde(0x191)],![],_0x31cfde(0x152));_0x52d2cb(_0x4261d6);const _0x462310=await lstat(_0x4261d6);if(!_0x462310[_0x31cfde(0x14f)]())invalid(_0x31cfde(0x1b0));const _0x55be5a=await readBoundedFile(_0x4261d6);_0x3cc6ad=enforceContentLimits(_0x55be5a,_0x3cc6ad);if(_0x133a44[_0x31cfde(0x1bf)]===_0x31cfde(0x1a9)){_0x1dfe63[_0x31cfde(0x145)]({'path':_0x133a44[_0x31cfde(0x191)],'target':_0x4261d6,'expectedExisting':!![]}),_0x17c99b[_0x31cfde(0x145)]({'path':_0x133a44[_0x31cfde(0x191)],'operation':_0x31cfde(0x1c4),'addedLines':0x0,'deletedLines':logicalLineCount(_0x55be5a)});continue;}const _0x44af4a=_0x133a44[_0x31cfde(0x190)][_0x31cfde(0x184)]?applyHunks(_0x55be5a,_0x133a44[_0x31cfde(0x190)],_0x133a44[_0x31cfde(0x191)]):_0x55be5a;_0x3cc6ad=enforceContentLimits(_0x44af4a,_0x3cc6ad);const _0x17263c=_0x133a44[_0x31cfde(0x190)][_0x31cfde(0x1b4)]((_0x498be0,_0x587763)=>_0x498be0+_0x587763[_0x31cfde(0x182)],0x1e03+0x2*0xdd3+-0x39a9),_0x1b41f3=_0x133a44[_0x31cfde(0x190)][_0x31cfde(0x1b4)]((_0x3f39e2,_0x56bd42)=>_0x3f39e2+_0x56bd42[_0x31cfde(0x166)],0xc*-0x40+-0x1a6*0x7+-0xe8a*-0x1);if(_0x133a44[_0x31cfde(0x1ab)]){const _0xe5be8=await this[_0x31cfde(0x18c)][_0x31cfde(0x196)](_0x133a44[_0x31cfde(0x1ab)],!![],_0x31cfde(0x152));_0x52d2cb(_0xe5be8),await assertMissing(_0xe5be8),_0x1dfe63[_0x31cfde(0x145)]({'path':_0x133a44[_0x31cfde(0x191)],'target':_0x4261d6,'expectedExisting':!![]}),_0x1dfe63[_0x31cfde(0x145)]({'path':_0x133a44[_0x31cfde(0x1ab)],'target':_0xe5be8,'content':_0x44af4a,'mode':_0x462310[_0x31cfde(0x174)]&0x1f53+-0xa4*0x26+0x904,'expectedExisting':![]}),_0x17c99b[_0x31cfde(0x145)]({'path':_0x133a44[_0x31cfde(0x191)],'targetPath':_0x133a44[_0x31cfde(0x1ab)],'operation':_0x31cfde(0x19f),'addedLines':_0x17263c,'deletedLines':_0x1b41f3});}else _0x1dfe63[_0x31cfde(0x145)]({'path':_0x133a44[_0x31cfde(0x191)],'target':_0x4261d6,'content':_0x44af4a,'mode':_0x462310[_0x31cfde(0x174)]&0x199*0x6+-0x2b*-0x22+0xb3,'expectedExisting':!![]}),_0x17c99b[_0x31cfde(0x145)]({'path':_0x133a44[_0x31cfde(0x191)],'operation':_0x31cfde(0x15c),'addedLines':_0x17263c,'deletedLines':_0x1b41f3});}return{'mutations':_0x1dfe63,'results':_0x17c99b};}async #commit(_0x450bb5){const _0x27ab22=_0x4c3e54,_0x341bf2=[],_0x19d167=[],_0x140b0d=[],_0x52cd89=[];try{for(const _0x158daf of _0x450bb5){if(!_0x158daf[_0x27ab22(0x170)])continue;await ensureDirectory(dirname(_0x158daf[_0x27ab22(0x1a5)]),_0x52cd89);const _0x4841dd=_0x158daf[_0x27ab22(0x1a5)]+_0x27ab22(0x192)+randomUUID()+_0x27ab22(0x1bb);await writeFile(_0x4841dd,_0x158daf[_0x27ab22(0x170)],{'flag':'wx'});if(_0x158daf[_0x27ab22(0x174)]!==undefined)await chmod(_0x4841dd,_0x158daf[_0x27ab22(0x174)]);_0x341bf2[_0x27ab22(0x145)]({..._0x158daf,'temporary':_0x4841dd});}for(const _0x3b6c39 of _0x450bb5)if(await exists(_0x3b6c39[_0x27ab22(0x1a5)])!==_0x3b6c39[_0x27ab22(0x199)])throw new MarAgentError(_0x27ab22(0x198),_0x27ab22(0x172));for(const _0x2f9dc2 of _0x450bb5){if(!await exists(_0x2f9dc2[_0x27ab22(0x1a5)]))continue;const _0x5ac4bd=_0x2f9dc2[_0x27ab22(0x1a5)]+_0x27ab22(0x192)+randomUUID()+_0x27ab22(0x14c);await this.#fileOperations[_0x27ab22(0x169)](_0x2f9dc2[_0x27ab22(0x1a5)],_0x5ac4bd),_0x19d167[_0x27ab22(0x145)]({'target':_0x2f9dc2[_0x27ab22(0x1a5)],'backup':_0x5ac4bd});}for(const _0x59ec7d of _0x341bf2){await this.#fileOperations[_0x27ab22(0x169)](_0x59ec7d[_0x27ab22(0x160)],_0x59ec7d[_0x27ab22(0x1a5)]),_0x140b0d[_0x27ab22(0x145)](_0x59ec7d[_0x27ab22(0x1a5)]);}}catch(_0x42b72c){let _0x146a0c;for(const _0x20e245 of _0x140b0d[_0x27ab22(0x1d0)]())await this.#fileOperations['rm'](_0x20e245,{'force':!![]})[_0x27ab22(0x168)](_0x13b0c3=>_0x146a0c??=_0x13b0c3);for(const _0x4f9c95 of _0x19d167[_0x27ab22(0x1d0)]())await this.#fileOperations[_0x27ab22(0x169)](_0x4f9c95[_0x27ab22(0x15d)],_0x4f9c95[_0x27ab22(0x1a5)])[_0x27ab22(0x168)](_0x20d820=>_0x146a0c??=_0x20d820);await Promise[_0x27ab22(0x1c5)](_0x341bf2[_0x27ab22(0x1c7)](_0x30513c=>this.#fileOperations['rm'](_0x30513c[_0x27ab22(0x160)],{'force':!![]})));for(const _0x2e4322 of _0x52cd89[_0x27ab22(0x1d0)]())await rmdir(_0x2e4322)[_0x27ab22(0x168)](()=>undefined);if(_0x146a0c)throw new MarAgentError(_0x27ab22(0x186),_0x27ab22(0x1a4),{'cause':_0x146a0c});throw _0x42b72c;}await Promise[_0x27ab22(0x1a8)](_0x19d167[_0x27ab22(0x1c7)](_0x3aee67=>this.#fileOperations['rm'](_0x3aee67[_0x27ab22(0x15d)],{'force':!![]})));}}function parsePatch(_0xb6614c){const _0x261992=_0x4c3e54;if(typeof _0xb6614c!==_0x261992(0x1d1))invalid(_0x261992(0x1a1));if(Buffer[_0x261992(0x19e)](_0xb6614c)>TOOL_EXECUTION_LIMITS[_0x261992(0x197)])throw new MarAgentError(_0x261992(0x1ac),_0x261992(0x18f));const _0x1aef54=_0xb6614c[_0x261992(0x15a)]('\x0d\x0a','\x0a');if(_0x1aef54[_0x261992(0x17e)]('\x0d'))invalid(_0x261992(0x1cc));const _0x34fc08=_0x1aef54[_0x261992(0x176)]('\x0a')?_0x1aef54[_0x261992(0x189)](0x100*-0x8+0x22*0x1d+0x426,-(0xb98+0x1ac5+-0x265c)):_0x1aef54;if(!_0x34fc08[_0x261992(0x1b9)](_0x261992(0x14d))||!_0x34fc08[_0x261992(0x176)](_0x261992(0x147)))invalid(_0x261992(0x19a));const _0x76b559=_0x34fc08[_0x261992(0x189)](_0x261992(0x14d)[_0x261992(0x184)],-_0x261992(0x147)[_0x261992(0x184)]);if(!_0x76b559)invalid(_0x261992(0x16d));const _0x19483e=_0x76b559[_0x261992(0x1bd)]('\x0a'),_0x88a397=[],_0x25e8b8=new Set();let _0x592f8c=0x3*-0x8a7+0x1*0x232f+-0x49d*0x2;const _0xca656a=_0x398076=>{const _0x4c22fd=_0x261992;if(!_0x398076||_0x398076[_0x4c22fd(0x1a0)]()!==_0x398076)invalid(_0x4c22fd(0x1ad));const _0x8321b6=isAbsolute(_0x398076)?resolve(_0x398076):resolve('/',_0x398076);if(_0x25e8b8[_0x4c22fd(0x187)](_0x8321b6))invalid(_0x4c22fd(0x144));_0x25e8b8[_0x4c22fd(0x1c0)](_0x8321b6);};while(_0x592f8c<_0x19483e[_0x261992(0x184)]){const _0xd3f76f=_0x19483e[_0x592f8c++];if(_0xd3f76f[_0x261992(0x1b9)](_0x261992(0x1cb))){const _0xe37694=_0xd3f76f[_0x261992(0x189)](_0x261992(0x1cb)[_0x261992(0x184)]);_0xca656a(_0xe37694);const _0x256e84=[];while(_0x592f8c<_0x19483e[_0x261992(0x184)]&&!_0x19483e[_0x592f8c][_0x261992(0x1b9)](_0x261992(0x1c2))){const _0x1cf940=_0x19483e[_0x592f8c++];if(!_0x1cf940[_0x261992(0x1b9)]('+'))invalid(_0x261992(0x150));_0x256e84[_0x261992(0x145)](_0x1cf940[_0x261992(0x189)](0x1f94+-0x166b+0x1*-0x928));}if(!_0x256e84[_0x261992(0x184)])invalid(_0x261992(0x18e));_0x88a397[_0x261992(0x145)]({'kind':_0x261992(0x1c0),'path':_0xe37694,'lines':_0x256e84});}else{if(_0xd3f76f[_0x261992(0x1b9)](_0x261992(0x1ce))){const _0x205751=_0xd3f76f[_0x261992(0x189)](_0x261992(0x1ce)[_0x261992(0x184)]);_0xca656a(_0x205751),_0x88a397[_0x261992(0x145)]({'kind':_0x261992(0x1a9),'path':_0x205751});}else{if(_0xd3f76f[_0x261992(0x1b9)](_0x261992(0x16a))){const _0x2b3512=_0xd3f76f[_0x261992(0x189)](_0x261992(0x16a)[_0x261992(0x184)]);_0xca656a(_0x2b3512);let _0x331d25;_0x19483e[_0x592f8c]?.[_0x261992(0x1b9)](_0x261992(0x1c3))&&(_0x331d25=_0x19483e[_0x592f8c++][_0x261992(0x189)](_0x261992(0x1c3)[_0x261992(0x184)]),_0xca656a(_0x331d25));const _0x59a7ff=[];while(_0x592f8c<_0x19483e[_0x261992(0x184)]&&_0x19483e[_0x592f8c][_0x261992(0x1b9)]('@@')){const _0x4ffe8b=_0x19483e[_0x592f8c++];let _0x5bb7b9;if(_0x4ffe8b!=='@@'){if(!_0x4ffe8b[_0x261992(0x1b9)](_0x261992(0x1b5)))invalid(_0x261992(0x1af));_0x5bb7b9=_0x4ffe8b[_0x261992(0x189)](-0x2011*-0x1+-0x2ba*-0x1+0x54*-0x6a);if(!_0x5bb7b9[_0x261992(0x1a0)]())invalid(_0x261992(0x19c));}const _0x579cc1=[],_0x3c2226=[];let _0x55de3d=-0x5ad+-0xff+0x6ac,_0x5e5f22=0x25da+-0x21d7+-0x4f*0xd;while(_0x592f8c<_0x19483e[_0x261992(0x184)]&&!_0x19483e[_0x592f8c][_0x261992(0x1b9)]('@@')&&!_0x19483e[_0x592f8c][_0x261992(0x1b9)](_0x261992(0x1c2))){const _0x24a757=_0x19483e[_0x592f8c++];if(_0x24a757[_0x261992(0x1b9)]('\x20')){const _0x4d68dc=_0x24a757[_0x261992(0x189)](-0x203e*0x1+0x206c+-0x2d);_0x579cc1[_0x261992(0x145)](_0x4d68dc),_0x3c2226[_0x261992(0x145)]({'kind':_0x261992(0x149),'value':_0x4d68dc});}else{if(_0x24a757[_0x261992(0x1b9)]('+')){const _0x11f8d6=_0x24a757[_0x261992(0x189)](-0x1a5e*0x1+0x4*0x504+-0x13*-0x55);_0x3c2226[_0x261992(0x145)]({'kind':_0x261992(0x171),'value':_0x11f8d6}),_0x55de3d++;}else{if(_0x24a757[_0x261992(0x1b9)]('-')){const _0xb39269=_0x24a757[_0x261992(0x189)](0x1*-0x20c6+-0x8*0x133+0x2a5f*0x1);_0x579cc1[_0x261992(0x145)](_0xb39269),_0x3c2226[_0x261992(0x145)]({'kind':_0x261992(0x15b),'value':_0xb39269}),_0x5e5f22++;}else invalid(_0x261992(0x17d));}}}if(!_0x579cc1[_0x261992(0x184)])invalid(_0x261992(0x155));_0x59a7ff[_0x261992(0x145)]({..._0x5bb7b9===undefined?{}:{'changeContext':_0x5bb7b9},'oldLines':_0x579cc1,'patchLines':_0x3c2226,'addedLines':_0x55de3d,'deletedLines':_0x5e5f22,'anchor':!_0x55de3d&&!_0x5e5f22});}let _0x2aed14=![];for(let _0x59ed36=_0x59a7ff[_0x261992(0x184)]-(0x22b4+-0x1615+-0x5*0x286);_0x59ed36>=-0xe7c+0x195*-0x11+0x21*0x141;_0x59ed36--){const _0x3d26c9=_0x59a7ff[_0x59ed36];if(_0x3d26c9[_0x261992(0x18a)]){if(!_0x2aed14)invalid(_0x261992(0x154));}else _0x2aed14=!![];}if(!_0x2aed14&&!_0x331d25)invalid(_0x261992(0x1b7));_0x88a397[_0x261992(0x145)]({'kind':_0x261992(0x14a),'path':_0x2b3512,..._0x331d25?{'targetPath':_0x331d25}:{},'hunks':_0x59a7ff});}else invalid(_0x261992(0x1c1));}}if(_0x88a397[_0x261992(0x184)]>TOOL_EXECUTION_LIMITS[_0x261992(0x1b2)])throw new MarAgentError(_0x261992(0x1ac),_0x261992(0x193));}return _0x88a397;}function applyHunks(_0x575dce,_0x23a99c,_0x6034b0){const _0x149b75=_0x4c3e54;let _0x74cd93;try{_0x74cd93=utf8Decoder[_0x149b75(0x17c)](_0x575dce);}catch(_0x299b69){throw new MarAgentError(_0x149b75(0x198),_0x149b75(0x1b1),{'cause':_0x299b69});}const _0x368c23=_0x74cd93[_0x149b75(0x17e)]('\x0d\x0a')?'\x0d\x0a':'\x0a';if(_0x74cd93[_0x149b75(0x15a)]('\x0d\x0a','')[_0x149b75(0x17e)]('\x0d'))invalid(_0x149b75(0x1b6));const _0x338a49=_0x74cd93[_0x149b75(0x15a)]('\x0d\x0a','\x0a'),_0xc217d4=_0x338a49[_0x149b75(0x176)]('\x0a');let _0x52b6a6=(_0xc217d4?_0x338a49[_0x149b75(0x189)](-0x6f4+0x2*-0xdc5+0x6e6*0x5,-(-0xa*-0x1a9+0x5*-0x44b+0x4de)):_0x338a49)[_0x149b75(0x1bd)]('\x0a');if(_0x52b6a6[_0x149b75(0x184)]===0xb46+0x11e4+-0x1d29&&_0x52b6a6[-0x3*0x66+-0x2362+0x2494]==='')_0x52b6a6=[];let _0x43cc70=-0x525+-0x62d*0x5+0x13e*0x1d;for(const [_0x1b5c9f,_0x3fcee8]of _0x23a99c[_0x149b75(0x1a7)]()){if(_0x3fcee8[_0x149b75(0x181)]!==undefined){const _0x519c72=seekSequence(_0x52b6a6,[_0x3fcee8[_0x149b75(0x181)]],_0x43cc70);if(_0x519c72===undefined)throw new MarAgentError(_0x149b75(0x198),_0x149b75(0x16c)+(_0x1b5c9f+(0xf17+-0x10d8*-0x2+-0x30c6))+_0x149b75(0x1a3)+_0x6034b0+':\x0a'+summarizePatchLines([_0x3fcee8[_0x149b75(0x181)]]));_0x43cc70=_0x519c72+(0x2*-0xc05+0x12a*0x1a+-0x639);}const _0x2f011f=seekSequence(_0x52b6a6,_0x3fcee8[_0x149b75(0x173)],_0x43cc70);if(_0x2f011f===undefined)throw new MarAgentError(_0x149b75(0x198),_0x149b75(0x16c)+(_0x1b5c9f+(0x12*0x33+-0x103+0x5e*-0x7))+_0x149b75(0x1cd)+_0x6034b0+':\x0a'+summarizePatchLines(_0x3fcee8[_0x149b75(0x173)]));if(_0x3fcee8[_0x149b75(0x18a)]){_0x43cc70=_0x2f011f+_0x3fcee8[_0x149b75(0x173)][_0x149b75(0x184)];continue;}const _0x1056bd=materializeHunk(_0x52b6a6,_0x2f011f,_0x3fcee8[_0x149b75(0x18b)]);_0x52b6a6[_0x149b75(0x162)](_0x2f011f,_0x3fcee8[_0x149b75(0x173)][_0x149b75(0x184)],..._0x1056bd),_0x43cc70=_0x2f011f+_0x1056bd[_0x149b75(0x184)];}return Buffer[_0x149b75(0x14e)](''+_0x52b6a6[_0x149b75(0x1d2)](_0x368c23)+(_0xc217d4?_0x368c23:''));}function materializeHunk(_0x2e2f2e,_0x49a06d,_0x4a16ae){const _0x18d0c8=_0x4c3e54,_0x49b302=[];let _0x44bb4c=-0x5*0x5af+-0x1789+0x33f4;for(const _0x5db739 of _0x4a16ae){if(_0x5db739[_0x18d0c8(0x1bf)]===_0x18d0c8(0x171)){_0x49b302[_0x18d0c8(0x145)](_0x5db739[_0x18d0c8(0x175)]);continue;}if(_0x5db739[_0x18d0c8(0x1bf)]===_0x18d0c8(0x149))_0x49b302[_0x18d0c8(0x145)](_0x2e2f2e[_0x49a06d+_0x44bb4c]);_0x44bb4c++;}return _0x49b302;}function seekSequence(_0x135645,_0x5d02af,_0x11dba2){const _0x1ce4f0=_0x4c3e54;if(!_0x5d02af[_0x1ce4f0(0x184)])return _0x11dba2;if(_0x5d02af[_0x1ce4f0(0x184)]>_0x135645[_0x1ce4f0(0x184)])return undefined;const _0x5aeba7=[(_0x145867,_0x89b769)=>_0x145867===_0x89b769,(_0x3ed9d8,_0x2adff4)=>_0x3ed9d8[_0x1ce4f0(0x165)]()===_0x2adff4[_0x1ce4f0(0x165)](),(_0x316017,_0x4a6b09)=>_0x316017[_0x1ce4f0(0x1a0)]()===_0x4a6b09[_0x1ce4f0(0x1a0)](),(_0x5b5d30,_0x335960)=>normalizePatchContext(_0x5b5d30)===normalizePatchContext(_0x335960)];for(const _0x88c0ac of _0x5aeba7)for(let _0x1859d2=_0x11dba2;_0x1859d2<=_0x135645[_0x1ce4f0(0x184)]-_0x5d02af[_0x1ce4f0(0x184)];_0x1859d2++)if(_0x5d02af[_0x1ce4f0(0x18d)]((_0x5f13eb,_0x1473ec)=>_0x88c0ac(_0x135645[_0x1859d2+_0x1473ec],_0x5f13eb)))return _0x1859d2;return undefined;}function normalizePatchContext(_0x55331f){const _0x5780a3=_0x4c3e54;return[..._0x55331f[_0x5780a3(0x1a0)]()][_0x5780a3(0x1c7)](_0x1a1ac4=>{const _0x34d4f5=_0x5780a3;if(_0x34d4f5(0x1ca)[_0x34d4f5(0x17e)](_0x1a1ac4))return'-';if(_0x34d4f5(0x1c6)[_0x34d4f5(0x17e)](_0x1a1ac4))return'\x27';if(_0x34d4f5(0x19b)[_0x34d4f5(0x17e)](_0x1a1ac4))return'\x22';if(_0x34d4f5(0x15f)[_0x34d4f5(0x17e)](_0x1a1ac4))return'\x20';return _0x1a1ac4;})[_0x5780a3(0x1d2)]('');}function summarizePatchLines(_0x310f33){const _0x5c6825=_0x4c3e54,_0x12cef9=_0x310f33[_0x5c6825(0x1d2)]('\x0a');return _0x12cef9[_0x5c6825(0x184)]<=-0x73*0x2b+0x2*0x1262+-0xd8b?_0x12cef9:[..._0x12cef9][_0x5c6825(0x189)](0x1ae*-0x13+-0x62f*-0x1+-0x3ad*-0x7,0xb94+-0x1*0x1bd2+-0x1*-0x1426)[_0x5c6825(0x1d2)]('')+'…';}function logicalLineCount(_0x57d10c){const _0x27cbd2=_0x4c3e54;if(!_0x57d10c[_0x27cbd2(0x184)])return 0x1a16+-0x2*0xa38+0x5a6*-0x1;const _0xedfeca=_0x57d10c[_0x27cbd2(0x14b)](_0x27cbd2(0x179))[_0x27cbd2(0x15a)]('\x0d\x0a','\x0a');return _0xedfeca[_0x27cbd2(0x176)]('\x0a')?_0xedfeca[_0x27cbd2(0x189)](0x131+0x513+0x2*-0x322,-(-0x198b+0x39b+0x1*0x15f1))[_0x27cbd2(0x1bd)]('\x0a')[_0x27cbd2(0x184)]:_0xedfeca[_0x27cbd2(0x1bd)]('\x0a')[_0x27cbd2(0x184)];}async function readBoundedFile(_0x119cf3){const _0x3d8b5e=_0x4c3e54,_0x96fa23=await lstat(_0x119cf3);if(_0x96fa23[_0x3d8b5e(0x1ae)]>TOOL_EXECUTION_LIMITS[_0x3d8b5e(0x17b)])throw new MarAgentError(_0x3d8b5e(0x1ac),_0x3d8b5e(0x159));return readFile(_0x119cf3);}function enforceContentLimits(_0x26278f,_0x483450){const _0x265ca7=_0x4c3e54;if(_0x26278f[_0x265ca7(0x19e)]>TOOL_EXECUTION_LIMITS[_0x265ca7(0x17b)])throw new MarAgentError(_0x265ca7(0x1ac),_0x265ca7(0x159));const _0x536f55=_0x483450+_0x26278f[_0x265ca7(0x19e)];if(_0x536f55>TOOL_EXECUTION_LIMITS[_0x265ca7(0x151)])throw new MarAgentError(_0x265ca7(0x1ac),_0x265ca7(0x157));return _0x536f55;}async function assertMissing(_0x3abb03){const _0x365d6c=_0x4c3e54;if(await exists(_0x3abb03))throw new MarAgentError(_0x365d6c(0x198),_0x365d6c(0x178));}async function ensureDirectory(_0x46494f,_0x25226f){const _0x1a7f3a=_0x4c3e54,_0x41c884=[];let _0x13c9c7=_0x46494f;const _0x5941b1=parse(_0x46494f)[_0x1a7f3a(0x177)];while(_0x13c9c7!==_0x5941b1&&!await exists(_0x13c9c7)){_0x41c884[_0x1a7f3a(0x145)](_0x13c9c7),_0x13c9c7=dirname(_0x13c9c7);}await mkdir(_0x46494f,{'recursive':!![]}),_0x25226f[_0x1a7f3a(0x145)](..._0x41c884[_0x1a7f3a(0x1d0)]());}async function exists(_0x557e44){const _0x2e54f9=_0x4c3e54;try{return await lstat(_0x557e44),!![];}catch(_0x32279b){if(_0x32279b[_0x2e54f9(0x1b8)]===_0x2e54f9(0x185))return![];throw _0x32279b;}}function invalid(_0x565049){const _0x472180=_0x4c3e54;throw new MarAgentError(_0x472180(0x16e),_0x565049);}
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { chmod, lstat, mkdir, readFile, rename, rm, rmdir, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { dirname, isAbsolute, parse, resolve } from 'node:path';
|
|
4
|
+
import { TextDecoder } from 'node:util';
|
|
5
|
+
import { MarAgentError } from '../error.js';
|
|
6
|
+
import { TOOL_EXECUTION_LIMITS } from './execution-limits.js';
|
|
7
|
+
export const applyPatchToolDefinition = {
|
|
8
|
+
name: 'apply_patch',
|
|
9
|
+
parallelSafety: 'serial',
|
|
10
|
+
description: `Preferred for precise, reviewable local edits. Group small, related implementation and test changes in one patch when they are already understood. Split large or independent changes, and wait for intermediate results only when the next edit depends on them. Submit a raw *** Begin Patch / *** End Patch patch. Supports Add File, Update File, Delete File, and Move to. Update hunks accept @@ or @@ <change context>; context-only hunks may also be used as ordered anchors before a later changing hunk. Context is searched forward with Codex-style exact, whitespace-tolerant, then common Unicode punctuation-tolerant matching. The complete patch is validated and committed atomically. Existing file permissions, line endings, and final-newline state are preserved. Returns changed file paths and operations with per-file and total line counts. Limits: ${TOOL_EXECUTION_LIMITS.patchMaxBytes / 1_048_576} MiB patch input, ${TOOL_EXECUTION_LIMITS.patchMaxFiles} files, ${TOOL_EXECUTION_LIMITS.patchMaxFileBytes / 1_048_576} MiB per file, and ${TOOL_EXECUTION_LIMITS.patchMaxTotalBytes / 1_048_576} MiB working set. MAR_AGENT_PATCH_CONFLICT means no partial edit was applied; hunk match conflicts identify the affected path and hunk so you can reread that file before retrying.`,
|
|
11
|
+
inputMode: 'freeform',
|
|
12
|
+
outputSchema: {
|
|
13
|
+
type: 'object',
|
|
14
|
+
required: ['content', 'data'],
|
|
15
|
+
properties: {
|
|
16
|
+
content: { type: 'string' },
|
|
17
|
+
data: {
|
|
18
|
+
type: 'object',
|
|
19
|
+
required: ['files', 'totals'],
|
|
20
|
+
properties: {
|
|
21
|
+
files: {
|
|
22
|
+
type: 'array',
|
|
23
|
+
items: {
|
|
24
|
+
type: 'object',
|
|
25
|
+
required: ['path', 'operation', 'addedLines', 'deletedLines'],
|
|
26
|
+
properties: {
|
|
27
|
+
path: { type: 'string' },
|
|
28
|
+
targetPath: { type: 'string' },
|
|
29
|
+
operation: { enum: ['added', 'modified', 'deleted', 'moved'] },
|
|
30
|
+
addedLines: { type: 'number' },
|
|
31
|
+
deletedLines: { type: 'number' }
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
totals: {
|
|
36
|
+
type: 'object',
|
|
37
|
+
required: ['files', 'addedLines', 'deletedLines'],
|
|
38
|
+
properties: {
|
|
39
|
+
files: { type: 'number' },
|
|
40
|
+
addedLines: { type: 'number' },
|
|
41
|
+
deletedLines: { type: 'number' }
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
inputSchema: {
|
|
49
|
+
type: 'object',
|
|
50
|
+
properties: { patch: { type: 'string' } },
|
|
51
|
+
required: ['patch'],
|
|
52
|
+
additionalProperties: false
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const defaultFileOperations = { rename, rm };
|
|
56
|
+
const utf8Decoder = new TextDecoder('utf-8', { fatal: true });
|
|
57
|
+
export class ApplyPatchTool {
|
|
58
|
+
paths;
|
|
59
|
+
#fileOperations;
|
|
60
|
+
constructor(paths, operations = {}) {
|
|
61
|
+
this.paths = paths;
|
|
62
|
+
this.#fileOperations = { ...defaultFileOperations, ...operations };
|
|
63
|
+
}
|
|
64
|
+
async execute(patch) {
|
|
65
|
+
const changes = parsePatch(patch);
|
|
66
|
+
const { mutations, results } = await this.#preflight(changes);
|
|
67
|
+
await this.#commit(mutations);
|
|
68
|
+
return {
|
|
69
|
+
files: results,
|
|
70
|
+
totals: {
|
|
71
|
+
files: results.length,
|
|
72
|
+
addedLines: results.reduce((total, file) => total + file.addedLines, 0),
|
|
73
|
+
deletedLines: results.reduce((total, file) => total + file.deletedLines, 0)
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
async #preflight(changes) {
|
|
78
|
+
const mutations = [];
|
|
79
|
+
const results = [];
|
|
80
|
+
const claimedTargets = new Set();
|
|
81
|
+
const claimTarget = (target) => {
|
|
82
|
+
const key = resolve(target);
|
|
83
|
+
if (claimedTargets.has(key))
|
|
84
|
+
invalid('Patch contains duplicate or overlapping paths.');
|
|
85
|
+
claimedTargets.add(key);
|
|
86
|
+
};
|
|
87
|
+
let totalBytes = 0;
|
|
88
|
+
for (const change of changes) {
|
|
89
|
+
if (change.kind === 'add') {
|
|
90
|
+
const target = await this.paths.resolve(change.path, true, 'write');
|
|
91
|
+
claimTarget(target);
|
|
92
|
+
await assertMissing(target);
|
|
93
|
+
const content = Buffer.from(`${change.lines.join('\n')}\n`);
|
|
94
|
+
totalBytes = enforceContentLimits(content, totalBytes);
|
|
95
|
+
mutations.push({ path: change.path, target, content, expectedExisting: false });
|
|
96
|
+
results.push({
|
|
97
|
+
path: change.path,
|
|
98
|
+
operation: 'added',
|
|
99
|
+
addedLines: change.lines.length,
|
|
100
|
+
deletedLines: 0
|
|
101
|
+
});
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
const source = await this.paths.resolve(change.path, false, 'write');
|
|
105
|
+
claimTarget(source);
|
|
106
|
+
const sourceStat = await lstat(source);
|
|
107
|
+
if (!sourceStat.isFile())
|
|
108
|
+
invalid('Patch sources must be regular files.');
|
|
109
|
+
const original = await readBoundedFile(source);
|
|
110
|
+
totalBytes = enforceContentLimits(original, totalBytes);
|
|
111
|
+
if (change.kind === 'delete') {
|
|
112
|
+
mutations.push({ path: change.path, target: source, expectedExisting: true });
|
|
113
|
+
results.push({
|
|
114
|
+
path: change.path,
|
|
115
|
+
operation: 'deleted',
|
|
116
|
+
addedLines: 0,
|
|
117
|
+
deletedLines: logicalLineCount(original)
|
|
118
|
+
});
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
const content = change.hunks.length
|
|
122
|
+
? applyHunks(original, change.hunks, change.path)
|
|
123
|
+
: original;
|
|
124
|
+
totalBytes = enforceContentLimits(content, totalBytes);
|
|
125
|
+
const addedLines = change.hunks.reduce((sum, hunk) => sum + hunk.addedLines, 0);
|
|
126
|
+
const deletedLines = change.hunks.reduce((sum, hunk) => sum + hunk.deletedLines, 0);
|
|
127
|
+
if (change.targetPath) {
|
|
128
|
+
const target = await this.paths.resolve(change.targetPath, true, 'write');
|
|
129
|
+
claimTarget(target);
|
|
130
|
+
await assertMissing(target);
|
|
131
|
+
mutations.push({ path: change.path, target: source, expectedExisting: true });
|
|
132
|
+
mutations.push({
|
|
133
|
+
path: change.targetPath,
|
|
134
|
+
target,
|
|
135
|
+
content,
|
|
136
|
+
mode: sourceStat.mode & 0o7777,
|
|
137
|
+
expectedExisting: false
|
|
138
|
+
});
|
|
139
|
+
results.push({
|
|
140
|
+
path: change.path,
|
|
141
|
+
targetPath: change.targetPath,
|
|
142
|
+
operation: 'moved',
|
|
143
|
+
addedLines,
|
|
144
|
+
deletedLines
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
mutations.push({
|
|
149
|
+
path: change.path,
|
|
150
|
+
target: source,
|
|
151
|
+
content,
|
|
152
|
+
mode: sourceStat.mode & 0o7777,
|
|
153
|
+
expectedExisting: true
|
|
154
|
+
});
|
|
155
|
+
results.push({
|
|
156
|
+
path: change.path,
|
|
157
|
+
operation: 'modified',
|
|
158
|
+
addedLines,
|
|
159
|
+
deletedLines
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return { mutations, results };
|
|
164
|
+
}
|
|
165
|
+
async #commit(mutations) {
|
|
166
|
+
const staged = [];
|
|
167
|
+
const backups = [];
|
|
168
|
+
const installed = [];
|
|
169
|
+
const createdDirectories = [];
|
|
170
|
+
try {
|
|
171
|
+
for (const mutation of mutations) {
|
|
172
|
+
if (!mutation.content)
|
|
173
|
+
continue;
|
|
174
|
+
await ensureDirectory(dirname(mutation.target), createdDirectories);
|
|
175
|
+
const temporary = `${mutation.target}.mar-patch-${randomUUID()}.tmp`;
|
|
176
|
+
await writeFile(temporary, mutation.content, { flag: 'wx' });
|
|
177
|
+
if (mutation.mode !== undefined)
|
|
178
|
+
await chmod(temporary, mutation.mode);
|
|
179
|
+
staged.push({ ...mutation, temporary });
|
|
180
|
+
}
|
|
181
|
+
for (const mutation of mutations)
|
|
182
|
+
if ((await exists(mutation.target)) !== mutation.expectedExisting)
|
|
183
|
+
throw new MarAgentError('MAR_AGENT_PATCH_CONFLICT', 'A patch target changed after preflight.');
|
|
184
|
+
for (const mutation of mutations) {
|
|
185
|
+
if (!(await exists(mutation.target)))
|
|
186
|
+
continue;
|
|
187
|
+
const backup = `${mutation.target}.mar-patch-${randomUUID()}.bak`;
|
|
188
|
+
await this.#fileOperations.rename(mutation.target, backup);
|
|
189
|
+
backups.push({ target: mutation.target, backup });
|
|
190
|
+
}
|
|
191
|
+
for (const item of staged) {
|
|
192
|
+
await this.#fileOperations.rename(item.temporary, item.target);
|
|
193
|
+
installed.push(item.target);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
catch (error) {
|
|
197
|
+
let rollbackError;
|
|
198
|
+
for (const target of installed.reverse())
|
|
199
|
+
await this.#fileOperations
|
|
200
|
+
.rm(target, { force: true })
|
|
201
|
+
.catch((cause) => (rollbackError ??= cause));
|
|
202
|
+
for (const item of backups.reverse())
|
|
203
|
+
await this.#fileOperations
|
|
204
|
+
.rename(item.backup, item.target)
|
|
205
|
+
.catch((cause) => (rollbackError ??= cause));
|
|
206
|
+
await Promise.allSettled(staged.map((item) => this.#fileOperations.rm(item.temporary, { force: true })));
|
|
207
|
+
for (const directory of createdDirectories.reverse())
|
|
208
|
+
await rmdir(directory).catch(() => undefined);
|
|
209
|
+
if (rollbackError)
|
|
210
|
+
throw new MarAgentError('MAR_AGENT_PATCH_ROLLBACK_FAILED', 'Patch commit failed and rollback could not restore every file.', { cause: rollbackError });
|
|
211
|
+
throw error;
|
|
212
|
+
}
|
|
213
|
+
await Promise.all(backups.map((item) => this.#fileOperations.rm(item.backup, { force: true })));
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
function parsePatch(patch) {
|
|
217
|
+
if (typeof patch !== 'string')
|
|
218
|
+
invalid('Patch must be a string.');
|
|
219
|
+
if (Buffer.byteLength(patch) > TOOL_EXECUTION_LIMITS.patchMaxBytes)
|
|
220
|
+
throw new MarAgentError('MAR_AGENT_TOOL_OUTPUT_LIMIT', 'Patch exceeds the input byte limit.');
|
|
221
|
+
const normalized = patch.replaceAll('\r\n', '\n');
|
|
222
|
+
if (normalized.includes('\r'))
|
|
223
|
+
invalid('Patch contains unsupported bare carriage returns.');
|
|
224
|
+
const value = normalized.endsWith('\n') ? normalized.slice(0, -1) : normalized;
|
|
225
|
+
if (!value.startsWith('*** Begin Patch\n') || !value.endsWith('\n*** End Patch'))
|
|
226
|
+
invalid('Patch envelope is invalid.');
|
|
227
|
+
const body = value.slice('*** Begin Patch\n'.length, -'\n*** End Patch'.length);
|
|
228
|
+
if (!body)
|
|
229
|
+
invalid('Patch must contain at least one operation.');
|
|
230
|
+
const lines = body.split('\n');
|
|
231
|
+
const changes = [];
|
|
232
|
+
const claimed = new Set();
|
|
233
|
+
let index = 0;
|
|
234
|
+
const claim = (path) => {
|
|
235
|
+
if (!path || path.trim() !== path)
|
|
236
|
+
invalid('Patch paths must be non-empty and unpadded.');
|
|
237
|
+
const key = isAbsolute(path) ? resolve(path) : resolve('/', path);
|
|
238
|
+
if (claimed.has(key))
|
|
239
|
+
invalid('Patch contains duplicate or overlapping paths.');
|
|
240
|
+
claimed.add(key);
|
|
241
|
+
};
|
|
242
|
+
while (index < lines.length) {
|
|
243
|
+
const header = lines[index++];
|
|
244
|
+
if (header.startsWith('*** Add File: ')) {
|
|
245
|
+
const path = header.slice('*** Add File: '.length);
|
|
246
|
+
claim(path);
|
|
247
|
+
const content = [];
|
|
248
|
+
while (index < lines.length && !lines[index].startsWith('*** ')) {
|
|
249
|
+
const line = lines[index++];
|
|
250
|
+
if (!line.startsWith('+'))
|
|
251
|
+
invalid('Added file lines must start with +.');
|
|
252
|
+
content.push(line.slice(1));
|
|
253
|
+
}
|
|
254
|
+
if (!content.length)
|
|
255
|
+
invalid('Added files must contain at least one patch line.');
|
|
256
|
+
changes.push({ kind: 'add', path, lines: content });
|
|
257
|
+
}
|
|
258
|
+
else if (header.startsWith('*** Delete File: ')) {
|
|
259
|
+
const path = header.slice('*** Delete File: '.length);
|
|
260
|
+
claim(path);
|
|
261
|
+
changes.push({ kind: 'delete', path });
|
|
262
|
+
}
|
|
263
|
+
else if (header.startsWith('*** Update File: ')) {
|
|
264
|
+
const path = header.slice('*** Update File: '.length);
|
|
265
|
+
claim(path);
|
|
266
|
+
let targetPath;
|
|
267
|
+
if (lines[index]?.startsWith('*** Move to: ')) {
|
|
268
|
+
targetPath = lines[index++].slice('*** Move to: '.length);
|
|
269
|
+
claim(targetPath);
|
|
270
|
+
}
|
|
271
|
+
const hunks = [];
|
|
272
|
+
while (index < lines.length && lines[index].startsWith('@@')) {
|
|
273
|
+
const hunkHeader = lines[index++];
|
|
274
|
+
let changeContext;
|
|
275
|
+
if (hunkHeader !== '@@') {
|
|
276
|
+
if (!hunkHeader.startsWith('@@ '))
|
|
277
|
+
invalid('Patch change context headers must use @@ or @@ <context>.');
|
|
278
|
+
changeContext = hunkHeader.slice(3);
|
|
279
|
+
if (!changeContext.trim())
|
|
280
|
+
invalid('Patch change context must be non-empty.');
|
|
281
|
+
}
|
|
282
|
+
const oldLines = [];
|
|
283
|
+
const patchLines = [];
|
|
284
|
+
let addedLines = 0;
|
|
285
|
+
let deletedLines = 0;
|
|
286
|
+
while (index < lines.length &&
|
|
287
|
+
!lines[index].startsWith('@@') &&
|
|
288
|
+
!lines[index].startsWith('*** ')) {
|
|
289
|
+
const line = lines[index++];
|
|
290
|
+
if (line.startsWith(' ')) {
|
|
291
|
+
const value = line.slice(1);
|
|
292
|
+
oldLines.push(value);
|
|
293
|
+
patchLines.push({ kind: 'context', value });
|
|
294
|
+
}
|
|
295
|
+
else if (line.startsWith('+')) {
|
|
296
|
+
const value = line.slice(1);
|
|
297
|
+
patchLines.push({ kind: 'addition', value });
|
|
298
|
+
addedLines++;
|
|
299
|
+
}
|
|
300
|
+
else if (line.startsWith('-')) {
|
|
301
|
+
const value = line.slice(1);
|
|
302
|
+
oldLines.push(value);
|
|
303
|
+
patchLines.push({ kind: 'deletion', value });
|
|
304
|
+
deletedLines++;
|
|
305
|
+
}
|
|
306
|
+
else
|
|
307
|
+
invalid('Patch hunk lines must start with space, +, or -.');
|
|
308
|
+
}
|
|
309
|
+
if (!oldLines.length)
|
|
310
|
+
invalid('Insertion hunks require context.');
|
|
311
|
+
hunks.push({
|
|
312
|
+
...(changeContext === undefined ? {} : { changeContext }),
|
|
313
|
+
oldLines,
|
|
314
|
+
patchLines,
|
|
315
|
+
addedLines,
|
|
316
|
+
deletedLines,
|
|
317
|
+
anchor: !addedLines && !deletedLines
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
let hasFollowingChange = false;
|
|
321
|
+
for (let hunkIndex = hunks.length - 1; hunkIndex >= 0; hunkIndex--) {
|
|
322
|
+
const hunk = hunks[hunkIndex];
|
|
323
|
+
if (hunk.anchor) {
|
|
324
|
+
if (!hasFollowingChange)
|
|
325
|
+
invalid('Context-only patch hunks must precede a changing hunk.');
|
|
326
|
+
}
|
|
327
|
+
else
|
|
328
|
+
hasFollowingChange = true;
|
|
329
|
+
}
|
|
330
|
+
if (!hasFollowingChange && !targetPath)
|
|
331
|
+
invalid('Update operations must change or move a file.');
|
|
332
|
+
changes.push({ kind: 'update', path, ...(targetPath ? { targetPath } : {}), hunks });
|
|
333
|
+
}
|
|
334
|
+
else
|
|
335
|
+
invalid('Patch operation is unsupported.');
|
|
336
|
+
if (changes.length > TOOL_EXECUTION_LIMITS.patchMaxFiles)
|
|
337
|
+
throw new MarAgentError('MAR_AGENT_TOOL_OUTPUT_LIMIT', 'Patch exceeds the file limit.');
|
|
338
|
+
}
|
|
339
|
+
return changes;
|
|
340
|
+
}
|
|
341
|
+
function applyHunks(original, hunks, path) {
|
|
342
|
+
let text;
|
|
343
|
+
try {
|
|
344
|
+
text = utf8Decoder.decode(original);
|
|
345
|
+
}
|
|
346
|
+
catch (cause) {
|
|
347
|
+
throw new MarAgentError('MAR_AGENT_PATCH_CONFLICT', 'Patched files must be valid UTF-8.', {
|
|
348
|
+
cause
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
const newline = text.includes('\r\n') ? '\r\n' : '\n';
|
|
352
|
+
if (text.replaceAll('\r\n', '').includes('\r'))
|
|
353
|
+
invalid('Patched files contain mixed carriage returns.');
|
|
354
|
+
const normalized = text.replaceAll('\r\n', '\n');
|
|
355
|
+
const finalNewline = normalized.endsWith('\n');
|
|
356
|
+
let lines = (finalNewline ? normalized.slice(0, -1) : normalized).split('\n');
|
|
357
|
+
if (lines.length === 1 && lines[0] === '')
|
|
358
|
+
lines = [];
|
|
359
|
+
let searchStart = 0;
|
|
360
|
+
for (const [hunkIndex, hunk] of hunks.entries()) {
|
|
361
|
+
if (hunk.changeContext !== undefined) {
|
|
362
|
+
const contextPosition = seekSequence(lines, [hunk.changeContext], searchStart);
|
|
363
|
+
if (contextPosition === undefined)
|
|
364
|
+
throw new MarAgentError('MAR_AGENT_PATCH_CONFLICT', `Patch hunk ${hunkIndex + 1} could not find change context in ${path}:\n${summarizePatchLines([hunk.changeContext])}`);
|
|
365
|
+
searchStart = contextPosition + 1;
|
|
366
|
+
}
|
|
367
|
+
const position = seekSequence(lines, hunk.oldLines, searchStart);
|
|
368
|
+
if (position === undefined)
|
|
369
|
+
throw new MarAgentError('MAR_AGENT_PATCH_CONFLICT', `Patch hunk ${hunkIndex + 1} could not find expected lines in ${path}:\n${summarizePatchLines(hunk.oldLines)}`);
|
|
370
|
+
if (hunk.anchor) {
|
|
371
|
+
searchStart = position + hunk.oldLines.length;
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
const replacement = materializeHunk(lines, position, hunk.patchLines);
|
|
375
|
+
lines.splice(position, hunk.oldLines.length, ...replacement);
|
|
376
|
+
searchStart = position + replacement.length;
|
|
377
|
+
}
|
|
378
|
+
return Buffer.from(`${lines.join(newline)}${finalNewline ? newline : ''}`);
|
|
379
|
+
}
|
|
380
|
+
function materializeHunk(source, position, patchLines) {
|
|
381
|
+
const replacement = [];
|
|
382
|
+
let sourceOffset = 0;
|
|
383
|
+
for (const line of patchLines) {
|
|
384
|
+
if (line.kind === 'addition') {
|
|
385
|
+
replacement.push(line.value);
|
|
386
|
+
continue;
|
|
387
|
+
}
|
|
388
|
+
if (line.kind === 'context')
|
|
389
|
+
replacement.push(source[position + sourceOffset]);
|
|
390
|
+
sourceOffset++;
|
|
391
|
+
}
|
|
392
|
+
return replacement;
|
|
393
|
+
}
|
|
394
|
+
function seekSequence(lines, pattern, start) {
|
|
395
|
+
if (!pattern.length)
|
|
396
|
+
return start;
|
|
397
|
+
if (pattern.length > lines.length)
|
|
398
|
+
return undefined;
|
|
399
|
+
const comparators = [
|
|
400
|
+
(actual, expected) => actual === expected,
|
|
401
|
+
(actual, expected) => actual.trimEnd() === expected.trimEnd(),
|
|
402
|
+
(actual, expected) => actual.trim() === expected.trim(),
|
|
403
|
+
(actual, expected) => normalizePatchContext(actual) === normalizePatchContext(expected)
|
|
404
|
+
];
|
|
405
|
+
for (const matches of comparators)
|
|
406
|
+
for (let position = start; position <= lines.length - pattern.length; position++)
|
|
407
|
+
if (pattern.every((line, offset) => matches(lines[position + offset], line)))
|
|
408
|
+
return position;
|
|
409
|
+
return undefined;
|
|
410
|
+
}
|
|
411
|
+
function normalizePatchContext(value) {
|
|
412
|
+
return [...value.trim()]
|
|
413
|
+
.map((character) => {
|
|
414
|
+
if ('‐‑‒–—―−'.includes(character))
|
|
415
|
+
return '-';
|
|
416
|
+
if ('‘’‚‛'.includes(character))
|
|
417
|
+
return "'";
|
|
418
|
+
if ('“”„‟'.includes(character))
|
|
419
|
+
return '"';
|
|
420
|
+
if (' '.includes(character))
|
|
421
|
+
return ' ';
|
|
422
|
+
return character;
|
|
423
|
+
})
|
|
424
|
+
.join('');
|
|
425
|
+
}
|
|
426
|
+
function summarizePatchLines(lines) {
|
|
427
|
+
const value = lines.join('\n');
|
|
428
|
+
return value.length <= 1_000 ? value : `${[...value].slice(0, 1_000).join('')}…`;
|
|
429
|
+
}
|
|
430
|
+
function logicalLineCount(content) {
|
|
431
|
+
if (!content.length)
|
|
432
|
+
return 0;
|
|
433
|
+
const text = content.toString('utf8').replaceAll('\r\n', '\n');
|
|
434
|
+
return text.endsWith('\n') ? text.slice(0, -1).split('\n').length : text.split('\n').length;
|
|
435
|
+
}
|
|
436
|
+
async function readBoundedFile(path) {
|
|
437
|
+
const info = await lstat(path);
|
|
438
|
+
if (info.size > TOOL_EXECUTION_LIMITS.patchMaxFileBytes)
|
|
439
|
+
throw new MarAgentError('MAR_AGENT_TOOL_OUTPUT_LIMIT', 'Patched file exceeds the byte limit.');
|
|
440
|
+
return readFile(path);
|
|
441
|
+
}
|
|
442
|
+
function enforceContentLimits(content, current) {
|
|
443
|
+
if (content.byteLength > TOOL_EXECUTION_LIMITS.patchMaxFileBytes)
|
|
444
|
+
throw new MarAgentError('MAR_AGENT_TOOL_OUTPUT_LIMIT', 'Patched file exceeds the byte limit.');
|
|
445
|
+
const total = current + content.byteLength;
|
|
446
|
+
if (total > TOOL_EXECUTION_LIMITS.patchMaxTotalBytes)
|
|
447
|
+
throw new MarAgentError('MAR_AGENT_TOOL_OUTPUT_LIMIT', 'Patch working set exceeds the byte limit.');
|
|
448
|
+
return total;
|
|
449
|
+
}
|
|
450
|
+
async function assertMissing(path) {
|
|
451
|
+
if (await exists(path))
|
|
452
|
+
throw new MarAgentError('MAR_AGENT_PATCH_CONFLICT', 'Patch target already exists.');
|
|
453
|
+
}
|
|
454
|
+
async function ensureDirectory(path, created) {
|
|
455
|
+
const missing = [];
|
|
456
|
+
let current = path;
|
|
457
|
+
const root = parse(path).root;
|
|
458
|
+
while (current !== root && !(await exists(current))) {
|
|
459
|
+
missing.push(current);
|
|
460
|
+
current = dirname(current);
|
|
461
|
+
}
|
|
462
|
+
await mkdir(path, { recursive: true });
|
|
463
|
+
created.push(...missing.reverse());
|
|
464
|
+
}
|
|
465
|
+
async function exists(path) {
|
|
466
|
+
try {
|
|
467
|
+
await lstat(path);
|
|
468
|
+
return true;
|
|
469
|
+
}
|
|
470
|
+
catch (error) {
|
|
471
|
+
if (error.code === 'ENOENT')
|
|
472
|
+
return false;
|
|
473
|
+
throw error;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
function invalid(message) {
|
|
477
|
+
throw new MarAgentError('MAR_AGENT_TOOL_INPUT_INVALID', message);
|
|
478
|
+
}
|