@aria-cli/tools 1.0.9 → 1.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (241) hide show
  1. package/package.json +9 -5
  2. package/src/__tests__/web-fetch-download.test.ts +0 -433
  3. package/src/__tests__/web-tools.test.ts +0 -619
  4. package/src/ask-user-interaction.ts +0 -33
  5. package/src/cache/web-cache.ts +0 -110
  6. package/src/definitions/arion.ts +0 -118
  7. package/src/definitions/browser/browser.ts +0 -502
  8. package/src/definitions/browser/index.ts +0 -5
  9. package/src/definitions/browser/pw-downloads.ts +0 -142
  10. package/src/definitions/browser/pw-interactions.ts +0 -282
  11. package/src/definitions/browser/pw-responses.ts +0 -98
  12. package/src/definitions/browser/pw-session.ts +0 -405
  13. package/src/definitions/browser/pw-shared.ts +0 -85
  14. package/src/definitions/browser/pw-snapshot.ts +0 -383
  15. package/src/definitions/browser/pw-state.ts +0 -101
  16. package/src/definitions/browser/types.ts +0 -203
  17. package/src/definitions/code-intelligence.ts +0 -526
  18. package/src/definitions/core.ts +0 -118
  19. package/src/definitions/delegation.ts +0 -567
  20. package/src/definitions/deploy.ts +0 -73
  21. package/src/definitions/filesystem.ts +0 -217
  22. package/src/definitions/frg.ts +0 -67
  23. package/src/definitions/index.ts +0 -28
  24. package/src/definitions/memory.ts +0 -150
  25. package/src/definitions/messaging.ts +0 -734
  26. package/src/definitions/meta.ts +0 -392
  27. package/src/definitions/network.ts +0 -179
  28. package/src/definitions/outlook.ts +0 -318
  29. package/src/definitions/patch/apply-patch.ts +0 -235
  30. package/src/definitions/patch/fuzzy-match.ts +0 -217
  31. package/src/definitions/patch/index.ts +0 -1
  32. package/src/definitions/patch/patch-parser.ts +0 -297
  33. package/src/definitions/patch/sandbox-paths.ts +0 -129
  34. package/src/definitions/process/index.ts +0 -5
  35. package/src/definitions/process/process-registry.ts +0 -303
  36. package/src/definitions/process/process.ts +0 -456
  37. package/src/definitions/process/pty-keys.ts +0 -298
  38. package/src/definitions/process/session-slug.ts +0 -147
  39. package/src/definitions/quip.ts +0 -225
  40. package/src/definitions/search.ts +0 -67
  41. package/src/definitions/session-history.ts +0 -79
  42. package/src/definitions/shell.ts +0 -202
  43. package/src/definitions/slack.ts +0 -211
  44. package/src/definitions/web.ts +0 -119
  45. package/src/executors/apply-patch.ts +0 -1035
  46. package/src/executors/arion.ts +0 -199
  47. package/src/executors/code-intelligence.ts +0 -1179
  48. package/src/executors/deploy.ts +0 -1066
  49. package/src/executors/filesystem.ts +0 -1428
  50. package/src/executors/frg-freshness.ts +0 -743
  51. package/src/executors/frg.ts +0 -394
  52. package/src/executors/index.ts +0 -280
  53. package/src/executors/learning-meta.ts +0 -1367
  54. package/src/executors/lsp-client.ts +0 -355
  55. package/src/executors/memory.ts +0 -978
  56. package/src/executors/meta.ts +0 -293
  57. package/src/executors/process-registry.ts +0 -570
  58. package/src/executors/pty-session-store.ts +0 -43
  59. package/src/executors/pty.ts +0 -342
  60. package/src/executors/restart.ts +0 -133
  61. package/src/executors/search-freshness.ts +0 -249
  62. package/src/executors/search-types.ts +0 -98
  63. package/src/executors/search.ts +0 -89
  64. package/src/executors/self-diagnose.ts +0 -552
  65. package/src/executors/session-history.ts +0 -435
  66. package/src/executors/shell-safety.ts +0 -519
  67. package/src/executors/shell.ts +0 -1243
  68. package/src/executors/utils.ts +0 -40
  69. package/src/executors/web.ts +0 -786
  70. package/src/extraction/content-extraction.ts +0 -281
  71. package/src/extraction/index.ts +0 -5
  72. package/src/headless-control-contract.ts +0 -1149
  73. package/src/index.ts +0 -788
  74. package/src/local-control-http-auth.ts +0 -2
  75. package/src/mcp/client.ts +0 -218
  76. package/src/mcp/connection.ts +0 -568
  77. package/src/mcp/index.ts +0 -11
  78. package/src/mcp/jsonrpc.ts +0 -195
  79. package/src/mcp/types.ts +0 -199
  80. package/src/network-control-adapter.ts +0 -88
  81. package/src/network-runtime/address-types.ts +0 -218
  82. package/src/network-runtime/db-owner-fencing.ts +0 -91
  83. package/src/network-runtime/delivery-receipts.ts +0 -372
  84. package/src/network-runtime/direct-endpoint-authority.ts +0 -35
  85. package/src/network-runtime/index.ts +0 -316
  86. package/src/network-runtime/local-control-contract.ts +0 -784
  87. package/src/network-runtime/node-store-contract.ts +0 -46
  88. package/src/network-runtime/pair-route-contract.ts +0 -97
  89. package/src/network-runtime/peer-capabilities.ts +0 -48
  90. package/src/network-runtime/peer-principal-ref.ts +0 -20
  91. package/src/network-runtime/peer-state-machine.ts +0 -160
  92. package/src/network-runtime/protocol-schemas.ts +0 -265
  93. package/src/network-runtime/runtime-bootstrap-contract.ts +0 -83
  94. package/src/outlook/desktop-session.ts +0 -409
  95. package/src/policy.ts +0 -171
  96. package/src/providers/brave.ts +0 -80
  97. package/src/providers/duckduckgo.ts +0 -199
  98. package/src/providers/exa.ts +0 -85
  99. package/src/providers/firecrawl.ts +0 -77
  100. package/src/providers/index.ts +0 -8
  101. package/src/providers/jina.ts +0 -70
  102. package/src/providers/router.ts +0 -121
  103. package/src/providers/search-provider.ts +0 -74
  104. package/src/providers/tavily.ts +0 -74
  105. package/src/quip/desktop-session.ts +0 -435
  106. package/src/registry/index.ts +0 -1
  107. package/src/registry/registry.ts +0 -905
  108. package/src/runtime-socket-local-control-client.ts +0 -632
  109. package/src/security/dns-normalization.ts +0 -34
  110. package/src/security/dns-pinning.ts +0 -138
  111. package/src/security/external-content.ts +0 -129
  112. package/src/security/ssrf.ts +0 -207
  113. package/src/slack/desktop-session.ts +0 -493
  114. package/src/tool-factory.ts +0 -91
  115. package/src/types.ts +0 -1341
  116. package/src/utils/retry.ts +0 -163
  117. package/src/utils/safe-parse-json.ts +0 -176
  118. package/src/utils/url.ts +0 -20
  119. package/tests/benchmarks/registry.bench.ts +0 -57
  120. package/tests/cache/web-cache.test.ts +0 -147
  121. package/tests/critical-integration.test.ts +0 -1465
  122. package/tests/definitions/apply-patch.test.ts +0 -586
  123. package/tests/definitions/browser.test.ts +0 -495
  124. package/tests/definitions/delegation-pause-resume.test.ts +0 -758
  125. package/tests/definitions/execution.test.ts +0 -671
  126. package/tests/definitions/messaging-inbox-scope.test.ts +0 -229
  127. package/tests/definitions/messaging.test.ts +0 -1468
  128. package/tests/definitions/outlook.test.ts +0 -30
  129. package/tests/definitions/process.test.ts +0 -469
  130. package/tests/definitions/slack.test.ts +0 -28
  131. package/tests/definitions/tool-inventory.test.ts +0 -218
  132. package/tests/e2e/delegation-quest-orchestration.e2e.test.ts +0 -433
  133. package/tests/e2e/memory-tool-discovery-contract.e2e.test.ts +0 -81
  134. package/tests/executors/apply-patch.test.ts +0 -538
  135. package/tests/executors/arion.test.ts +0 -309
  136. package/tests/executors/conversation-primitives.test.ts +0 -250
  137. package/tests/executors/deploy.test.ts +0 -746
  138. package/tests/executors/filesystem-tools.test.ts +0 -357
  139. package/tests/executors/filesystem.test.ts +0 -959
  140. package/tests/executors/frg-freshness.test.ts +0 -136
  141. package/tests/executors/frg-merge.test.ts +0 -70
  142. package/tests/executors/frg-session-content.test.ts +0 -40
  143. package/tests/executors/frg.test.ts +0 -56
  144. package/tests/executors/memory-bugfixes.test.ts +0 -257
  145. package/tests/executors/memory-real-memoria.integration.test.ts +0 -316
  146. package/tests/executors/memory.test.ts +0 -853
  147. package/tests/executors/meta-tools.test.ts +0 -411
  148. package/tests/executors/meta.test.ts +0 -683
  149. package/tests/executors/path-containment.test.ts +0 -51
  150. package/tests/executors/process-registry.test.ts +0 -505
  151. package/tests/executors/pty.test.ts +0 -664
  152. package/tests/executors/quest-security.test.ts +0 -249
  153. package/tests/executors/read-file-media.test.ts +0 -230
  154. package/tests/executors/recall-knowledge-schema.test.ts +0 -209
  155. package/tests/executors/recall-tags.test.ts +0 -278
  156. package/tests/executors/remember-null-safety.contract.test.ts +0 -41
  157. package/tests/executors/restart.test.ts +0 -67
  158. package/tests/executors/search-unified.test.ts +0 -381
  159. package/tests/executors/session-history.test.ts +0 -340
  160. package/tests/executors/session-transcript.test.ts +0 -561
  161. package/tests/executors/shell-abort.test.ts +0 -416
  162. package/tests/executors/shell-env-blocklist.test.ts +0 -648
  163. package/tests/executors/shell-env-process.test.ts +0 -245
  164. package/tests/executors/shell-process-registry.test.ts +0 -334
  165. package/tests/executors/shell-tools.test.ts +0 -393
  166. package/tests/executors/shell.test.ts +0 -690
  167. package/tests/executors/web-abort-vs-timeout.test.ts +0 -213
  168. package/tests/executors/web-integration.test.ts +0 -633
  169. package/tests/executors/web-symlink.test.ts +0 -18
  170. package/tests/executors/web.test.ts +0 -1400
  171. package/tests/executors/write-stdin.test.ts +0 -145
  172. package/tests/extraction/content-extraction.test.ts +0 -153
  173. package/tests/guards/tools-default-test-lane.integration.test.ts +0 -21
  174. package/tests/guards/tools-package-test-commands.e2e.test.ts +0 -43
  175. package/tests/guards/tools-test-lane-manifest.contract.test.ts +0 -76
  176. package/tests/guards/tools-vitest-workspace-alias.contract.test.ts +0 -63
  177. package/tests/helpers/async-waits.ts +0 -53
  178. package/tests/integration/headless-control-contract.integration.test.ts +0 -153
  179. package/tests/integration/memory-tool-schema-parity.integration.test.ts +0 -67
  180. package/tests/integration/meta-tools-round-trip.integration.test.ts +0 -506
  181. package/tests/integration/quest-round-trip.test.ts +0 -303
  182. package/tests/integration/registry-executor-flow.test.ts +0 -85
  183. package/tests/integration.test.ts +0 -177
  184. package/tests/loading-tier.test.ts +0 -126
  185. package/tests/mcp/client-reconnect.test.ts +0 -267
  186. package/tests/mcp/connection.test.ts +0 -846
  187. package/tests/mcp/injectable-logger.test.ts +0 -83
  188. package/tests/mcp/jsonrpc.test.ts +0 -109
  189. package/tests/mcp/lifecycle.test.ts +0 -879
  190. package/tests/network-runtime/address-types.contract.test.ts +0 -143
  191. package/tests/network-runtime/continuity-bind-schema.contract.test.ts +0 -203
  192. package/tests/network-runtime/local-control-contract.test.ts +0 -869
  193. package/tests/network-runtime/local-control-invite-token.contract.test.ts +0 -146
  194. package/tests/network-runtime/node-store-contract.test.ts +0 -11
  195. package/tests/network-runtime/pair-protocol-nodeid.contract.test.ts +0 -15
  196. package/tests/network-runtime/peer-state-machine.contract.test.ts +0 -148
  197. package/tests/network-runtime/protocol-schemas.contract.test.ts +0 -512
  198. package/tests/network-runtime/relay-pending-nodeid.contract.test.ts +0 -62
  199. package/tests/network-runtime/runtime-bootstrap-contract.test.ts +0 -227
  200. package/tests/network-runtime/runtime-socket-local-control-client.test.ts +0 -621
  201. package/tests/network-runtime/wait-for-message-script.test.ts +0 -288
  202. package/tests/parallel.test.ts +0 -71
  203. package/tests/policy.test.ts +0 -184
  204. package/tests/print-default-test-lane.ts +0 -14
  205. package/tests/print-test-lane-manifest.ts +0 -22
  206. package/tests/providers/brave.test.ts +0 -159
  207. package/tests/providers/duckduckgo.test.ts +0 -207
  208. package/tests/providers/exa.test.ts +0 -175
  209. package/tests/providers/firecrawl.test.ts +0 -168
  210. package/tests/providers/jina.test.ts +0 -144
  211. package/tests/providers/router.test.ts +0 -328
  212. package/tests/providers/tavily.test.ts +0 -165
  213. package/tests/registry/discovery.test.ts +0 -154
  214. package/tests/registry/injectable-logger.test.ts +0 -230
  215. package/tests/registry/input-validation.test.ts +0 -361
  216. package/tests/registry/interface-completeness.test.ts +0 -85
  217. package/tests/registry/mcp-integration.test.ts +0 -103
  218. package/tests/registry/mcp-read-only-hint.test.ts +0 -60
  219. package/tests/registry/memoria-discovery.test.ts +0 -390
  220. package/tests/registry/nested-validation.test.ts +0 -283
  221. package/tests/registry/pseudo-tool-filtering.test.ts +0 -258
  222. package/tests/registry/registration-lifecycle.test.ts +0 -133
  223. package/tests/registry-validation.test.ts +0 -424
  224. package/tests/registry.test.ts +0 -460
  225. package/tests/security/dns-pinning.test.ts +0 -162
  226. package/tests/security/external-content.test.ts +0 -144
  227. package/tests/security/ssrf.test.ts +0 -118
  228. package/tests/shell-safety-integration.test.ts +0 -32
  229. package/tests/shell-safety.test.ts +0 -365
  230. package/tests/slack/desktop-session.test.ts +0 -50
  231. package/tests/test-lane-manifest.ts +0 -440
  232. package/tests/test-utils.ts +0 -27
  233. package/tests/tool-factory.test.ts +0 -188
  234. package/tests/utils/retry.test.ts +0 -231
  235. package/tests/utils/url.test.ts +0 -63
  236. package/tsconfig.cjs.json +0 -24
  237. package/tsconfig.json +0 -12
  238. package/vitest.config.ts +0 -55
  239. package/vitest.e2e.config.ts +0 -24
  240. package/vitest.integration.config.ts +0 -24
  241. package/vitest.native.config.ts +0 -24
@@ -1,1035 +0,0 @@
1
- /**
2
- * @aria/tools - Apply Patch executor
3
- *
4
- * Parses and applies unified diff patches with:
5
- * - Path traversal protection (SECURITY-CRITICAL)
6
- * - Atomic writes (all-or-nothing application)
7
- * - Fuzzy hunk matching (offset tolerance)
8
- * - CRLF normalization
9
- */
10
-
11
- import * as crypto from "node:crypto";
12
- import * as fs from "node:fs/promises";
13
- import * as fsSync from "node:fs";
14
- import * as nodePath from "node:path";
15
- import type { ToolContext, ToolResult } from "../types.js";
16
- import { success, fail, getErrorMessage, isPathWithinBase } from "./utils.js";
17
- import { recordFrgMutation } from "./frg-freshness.js";
18
- import { recordSearchMutation } from "./search-freshness.js";
19
-
20
- // ============================================================================
21
- // Types
22
- // ============================================================================
23
-
24
- export interface ApplyPatchInput {
25
- /** Unified diff format patch content */
26
- patch: string;
27
- /** Working directory for relative paths in the patch */
28
- cwd?: string;
29
- }
30
-
31
- /** A single hunk within a file diff */
32
- interface Hunk {
33
- /** Original file start line (1-based) */
34
- oldStart: number;
35
- /** Number of lines in original */
36
- oldCount: number;
37
- /** Modified file start line (1-based) */
38
- newStart: number;
39
- /** Number of lines in modified */
40
- newCount: number;
41
- /** Lines in the hunk: each prefixed with ' ', '+', or '-' */
42
- lines: string[];
43
- }
44
-
45
- /** A diff for a single file */
46
- interface FileDiff {
47
- /** Original file path (null for new files) */
48
- oldPath: string | null;
49
- /** Modified file path (null for deleted files) */
50
- newPath: string | null;
51
- /** Hunks to apply */
52
- hunks: Hunk[];
53
- }
54
-
55
- /** What to do with a file after applying hunks */
56
- interface FileAction {
57
- /** Source path for move operations */
58
- sourcePath?: string;
59
- /** Target absolute path */
60
- resolvedPath: string;
61
- /** Action type */
62
- type: "create" | "modify" | "delete" | "move";
63
- /** New content (for create/modify) */
64
- content?: string;
65
- }
66
-
67
- function getErrnoCode(err: unknown): string | undefined {
68
- if (typeof err !== "object" || err === null) {
69
- return undefined;
70
- }
71
- const withCode = err as { code?: unknown };
72
- return typeof withCode.code === "string" ? withCode.code : undefined;
73
- }
74
-
75
- // Maximum fuzzy offset when searching for hunk context
76
- const MAX_FUZZ_OFFSET = 3;
77
- const BEGIN_PATCH = "*** Begin Patch";
78
- const END_PATCH = "*** End Patch";
79
- const ADD_FILE = "*** Add File: ";
80
- const DELETE_FILE = "*** Delete File: ";
81
- const UPDATE_FILE = "*** Update File: ";
82
- const MOVE_TO = "*** Move to: ";
83
- const END_OF_FILE = "*** End of File";
84
-
85
- // ============================================================================
86
- // Unified Diff Parser
87
- // ============================================================================
88
-
89
- /**
90
- * Parse a unified diff string into an array of file diffs.
91
- * Handles standard unified diff and git-style diffs.
92
- */
93
- export function parseUnifiedDiff(patch: string): FileDiff[] {
94
- // Normalize CRLF to LF
95
- const normalized = patch.replace(/\r\n/g, "\n");
96
- const lines = normalized.split("\n");
97
- const diffs: FileDiff[] = [];
98
- let i = 0;
99
-
100
- while (i < lines.length) {
101
- const line = lines[i]!;
102
-
103
- // Look for --- header
104
- if (line.startsWith("--- ")) {
105
- // Check for binary diff markers before this point
106
- // (we check later too, but early check is good)
107
- const nextLine = lines[i + 1];
108
- if (!nextLine || !nextLine.startsWith("+++ ")) {
109
- i++;
110
- continue;
111
- }
112
-
113
- const oldPath = parseDiffPath(line.slice(4));
114
- const newPath = parseDiffPath(nextLine.slice(4));
115
- i += 2;
116
-
117
- // Parse hunks for this file
118
- const hunks: Hunk[] = [];
119
- while (i < lines.length) {
120
- const hunkLine = lines[i]!;
121
- if (hunkLine.startsWith("@@ ")) {
122
- const hunk = parseHunkHeader(hunkLine);
123
- if (!hunk) {
124
- throw new Error(`Invalid hunk header: ${hunkLine}`);
125
- }
126
- i++;
127
-
128
- // Collect hunk lines
129
- const hunkLines: string[] = [];
130
- let oldSeen = 0;
131
- let newSeen = 0;
132
- while (i < lines.length) {
133
- const l = lines[i]!;
134
- const prefix = l[0];
135
- if (prefix === " " || prefix === "+" || prefix === "-") {
136
- hunkLines.push(l);
137
- if (prefix === " " || prefix === "-") oldSeen++;
138
- if (prefix === " " || prefix === "+") newSeen++;
139
- i++;
140
- // Unified diff hunks define old/new line counts explicitly.
141
- // Stop when we consumed the declared line counts so a following
142
- // file header ("--- ...") is not misread as hunk content.
143
- if (oldSeen >= hunk.oldCount && newSeen >= hunk.newCount) {
144
- break;
145
- }
146
- } else if (l === "\") {
147
- // Skip this marker — we handle trailing newlines via content
148
- i++;
149
- } else {
150
- break;
151
- }
152
- }
153
-
154
- if (i < lines.length) {
155
- const next = lines[i]!;
156
- const nextPrefix = next[0];
157
- const looksLikeHunkLine =
158
- nextPrefix === " " ||
159
- (nextPrefix === "+" && !next.startsWith("+++ ")) ||
160
- (nextPrefix === "-" && !next.startsWith("--- "));
161
- if (looksLikeHunkLine) {
162
- throw new Error(
163
- `Malformed hunk: expected old/new counts ${hunk.oldCount}/${hunk.newCount} but found extra hunk lines`,
164
- );
165
- }
166
- }
167
-
168
- if (oldSeen !== hunk.oldCount || newSeen !== hunk.newCount) {
169
- throw new Error(
170
- `Malformed hunk: expected old/new counts ${hunk.oldCount}/${hunk.newCount} but found ${oldSeen}/${newSeen}`,
171
- );
172
- }
173
-
174
- hunks.push({ ...hunk, lines: hunkLines });
175
- } else if (hunkLine.startsWith("--- ") || hunkLine.startsWith("diff ") || hunkLine === "") {
176
- // Start of next file diff or blank line separator
177
- break;
178
- } else {
179
- // Skip git diff metadata lines (index, mode, etc.)
180
- i++;
181
- }
182
- }
183
-
184
- if (hunks.length > 0 || oldPath === null || newPath === null) {
185
- diffs.push({ oldPath, newPath, hunks });
186
- }
187
- } else if (line.startsWith("Binary files") || line.startsWith("GIT binary patch")) {
188
- throw new Error(`Binary diffs are not supported: ${line}`);
189
- } else {
190
- i++;
191
- }
192
- }
193
-
194
- return diffs;
195
- }
196
-
197
- function buildCountedHunk(lines: string[]): Hunk {
198
- let oldCount = 0;
199
- let newCount = 0;
200
- for (const line of lines) {
201
- if (line.startsWith(" ") || line.startsWith("-")) oldCount++;
202
- if (line.startsWith(" ") || line.startsWith("+")) newCount++;
203
- }
204
- return {
205
- oldStart: 1,
206
- oldCount,
207
- newStart: 1,
208
- newCount,
209
- lines,
210
- };
211
- }
212
-
213
- function parseHeaderPath(line: string, prefix: string): string {
214
- const raw = line.slice(prefix.length).trim();
215
- if (!raw) {
216
- throw new Error(`Missing file path after header: ${prefix.trim()}`);
217
- }
218
- return raw;
219
- }
220
-
221
- function parseBeginPatchFormat(patch: string): FileDiff[] {
222
- const lines = normalizePatchText(patch).split("\n");
223
- if ((lines[0] ?? "").trim() !== BEGIN_PATCH) {
224
- throw new Error("Invalid apply_patch envelope: missing *** Begin Patch");
225
- }
226
-
227
- const diffs: FileDiff[] = [];
228
- let i = 1;
229
-
230
- while (i < lines.length) {
231
- const line = lines[i] ?? "";
232
-
233
- if (line === END_PATCH) {
234
- return diffs;
235
- }
236
- if (line.trim() === "") {
237
- i++;
238
- continue;
239
- }
240
-
241
- if (line.startsWith(ADD_FILE)) {
242
- const newPath = parseHeaderPath(line, ADD_FILE);
243
- i++;
244
- const addLines: string[] = [];
245
- while (i < lines.length) {
246
- const current = lines[i] ?? "";
247
- if (current.startsWith("*** ")) break;
248
- if (!current.startsWith("+")) {
249
- throw new Error(`Invalid add-file line (must start with '+'): ${current}`);
250
- }
251
- addLines.push(current);
252
- i++;
253
- }
254
- diffs.push({
255
- oldPath: null,
256
- newPath,
257
- hunks: [buildCountedHunk(addLines)],
258
- });
259
- continue;
260
- }
261
-
262
- if (line.startsWith(DELETE_FILE)) {
263
- const oldPath = parseHeaderPath(line, DELETE_FILE);
264
- diffs.push({ oldPath, newPath: null, hunks: [] });
265
- i++;
266
- continue;
267
- }
268
-
269
- if (line.startsWith(UPDATE_FILE)) {
270
- const oldPath = parseHeaderPath(line, UPDATE_FILE);
271
- i++;
272
-
273
- let newPath = oldPath;
274
- if ((lines[i] ?? "").startsWith(MOVE_TO)) {
275
- newPath = parseHeaderPath(lines[i]!, MOVE_TO);
276
- i++;
277
- }
278
-
279
- const hunks: Hunk[] = [];
280
- let currentHunkLines: string[] = [];
281
- while (i < lines.length) {
282
- const current = lines[i] ?? "";
283
- if (current.startsWith("*** ")) break;
284
-
285
- if (current.startsWith("@@")) {
286
- if (currentHunkLines.length > 0) {
287
- hunks.push(buildCountedHunk(currentHunkLines));
288
- currentHunkLines = [];
289
- }
290
- i++;
291
- continue;
292
- }
293
-
294
- if (current === END_OF_FILE) {
295
- i++;
296
- continue;
297
- }
298
-
299
- if (current.startsWith(" ") || current.startsWith("+") || current.startsWith("-")) {
300
- currentHunkLines.push(current);
301
- i++;
302
- continue;
303
- }
304
-
305
- throw new Error(`Invalid update-file line: ${current}`);
306
- }
307
-
308
- if (currentHunkLines.length > 0) {
309
- hunks.push(buildCountedHunk(currentHunkLines));
310
- }
311
-
312
- if (hunks.length === 0 && oldPath === newPath) {
313
- throw new Error(`Update section has no hunks: ${oldPath}`);
314
- }
315
-
316
- diffs.push({ oldPath, newPath, hunks });
317
- continue;
318
- }
319
-
320
- throw new Error(`Invalid apply_patch section header: ${line}`);
321
- }
322
-
323
- throw new Error("Invalid apply_patch envelope: missing *** End Patch");
324
- }
325
-
326
- function normalizePatchText(patch: string): string {
327
- return patch.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
328
- }
329
-
330
- function parsePatchInput(patch: string): FileDiff[] {
331
- const normalized = normalizePatchText(patch).trimStart();
332
- if (normalized.startsWith(BEGIN_PATCH)) {
333
- return parseBeginPatchFormat(normalized);
334
- }
335
- return parseUnifiedDiff(normalized);
336
- }
337
-
338
- /**
339
- * Parse a file path from a --- or +++ line.
340
- * Strips a/ or b/ git-style prefixes.
341
- * Returns null for /dev/null (new file or deleted file).
342
- */
343
- function parseDiffPath(raw: string): string | null {
344
- // Remove trailing timestamp (e.g., "2024-01-01 00:00:00.000000000 +0000")
345
- const path = raw.replace(/\t.*$/, "").trim();
346
-
347
- if (path === "/dev/null") {
348
- return null;
349
- }
350
-
351
- // Strip git-style a/ or b/ prefix
352
- if (path.startsWith("a/") || path.startsWith("b/")) {
353
- return path.slice(2);
354
- }
355
-
356
- return path;
357
- }
358
-
359
- /**
360
- * Parse a hunk header line: @@ -oldStart,oldCount +newStart,newCount @@
361
- */
362
- function parseHunkHeader(line: string): Omit<Hunk, "lines"> | null {
363
- const match = line.match(/^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/);
364
- if (!match) return null;
365
-
366
- return {
367
- oldStart: parseInt(match[1]!, 10),
368
- oldCount: match[2] !== undefined ? parseInt(match[2], 10) : 1,
369
- newStart: parseInt(match[3]!, 10),
370
- newCount: match[4] !== undefined ? parseInt(match[4], 10) : 1,
371
- };
372
- }
373
-
374
- // ============================================================================
375
- // Path Validation (SECURITY-CRITICAL)
376
- // ============================================================================
377
-
378
- /**
379
- * Validate all paths in a parsed diff against path traversal attacks.
380
- *
381
- * SECURITY: This is the primary defense against malicious patches that
382
- * attempt to write outside the working directory.
383
- */
384
- function validatePatchPaths(
385
- diffs: FileDiff[],
386
- cwd: string,
387
- ): { valid: true; resolved: Map<string, string> } | { valid: false; error: string } {
388
- const resolved = new Map<string, string>();
389
-
390
- for (const diff of diffs) {
391
- const paths = [diff.oldPath, diff.newPath].filter((p): p is string => p !== null);
392
-
393
- for (const rawPath of paths) {
394
- if (resolved.has(rawPath)) continue;
395
-
396
- // 1. Reject absolute paths
397
- if (nodePath.isAbsolute(rawPath)) {
398
- return {
399
- valid: false,
400
- error: `Absolute path not allowed in patch: ${rawPath}`,
401
- };
402
- }
403
-
404
- // 2. Reject paths with .. components
405
- const segments = rawPath.split(/[/\\]/);
406
- if (segments.includes("..")) {
407
- return {
408
- valid: false,
409
- error: `Path traversal (..) not allowed in patch: ${rawPath}`,
410
- };
411
- }
412
-
413
- // 3. Reject system paths (even though relative shouldn't reach these,
414
- // defense-in-depth against creative path construction)
415
- const systemPrefixes = ["/dev/", "/proc/", "/sys/", "/etc/"];
416
- for (const prefix of systemPrefixes) {
417
- if (rawPath.startsWith(prefix) || rawPath === prefix.slice(0, -1)) {
418
- return {
419
- valid: false,
420
- error: `System path not allowed in patch: ${rawPath}`,
421
- };
422
- }
423
- }
424
-
425
- // 4. Resolve relative to cwd
426
- const fullPath = nodePath.resolve(cwd, rawPath);
427
-
428
- // 5. Verify the resolved path is within cwd
429
- // Resolve symlinks on cwd to handle platforms where /tmp -> /private/tmp
430
- let realCwd = cwd;
431
- try {
432
- realCwd = fsSync.realpathSync(cwd);
433
- } catch {
434
- // Fall back to original cwd if it doesn't exist yet
435
- }
436
-
437
- // Resolve symlinks on the target path (walk up to nearest existing ancestor)
438
- let realPath = fullPath;
439
- try {
440
- realPath = fsSync.realpathSync(fullPath);
441
- } catch {
442
- // Path may not exist yet — walk up to find nearest existing ancestor
443
- let current = fullPath;
444
- let suffix = "";
445
- while (current !== nodePath.dirname(current)) {
446
- const parent = nodePath.dirname(current);
447
- suffix = suffix
448
- ? nodePath.join(nodePath.basename(current), suffix)
449
- : nodePath.basename(current);
450
- try {
451
- const realAncestor = fsSync.realpathSync(parent);
452
- realPath = nodePath.join(realAncestor, suffix);
453
- break;
454
- } catch {
455
- current = parent;
456
- }
457
- }
458
- }
459
-
460
- if (!isPathWithinBase(realPath, realCwd)) {
461
- return {
462
- valid: false,
463
- error: `Resolved path escapes working directory: ${rawPath} -> ${realPath} (cwd: ${realCwd})`,
464
- };
465
- }
466
-
467
- resolved.set(rawPath, realPath);
468
- }
469
- }
470
-
471
- return { valid: true, resolved };
472
- }
473
-
474
- // ============================================================================
475
- // Windows Drive Letter Detection
476
- // ============================================================================
477
-
478
- /**
479
- * Check if a path looks like a Windows drive letter (e.g., C:\, D:/)
480
- */
481
- function isWindowsDrivePath(p: string): boolean {
482
- return /^[a-zA-Z]:[/\\]/.test(p);
483
- }
484
-
485
- // ============================================================================
486
- // Hunk Application
487
- // ============================================================================
488
-
489
- /**
490
- * Apply hunks to file content.
491
- * Uses fuzzy matching with a configurable offset tolerance.
492
- *
493
- * Returns the modified content or an error describing what went wrong.
494
- */
495
- function applyHunks(
496
- originalContent: string,
497
- hunks: Hunk[],
498
- filePath: string,
499
- ): { ok: true; content: string } | { ok: false; error: string } {
500
- // Normalize CRLF
501
- const normalized = originalContent.replace(/\r\n/g, "\n");
502
- let lines = normalized.split("\n");
503
-
504
- // Track cumulative offset from insertions/deletions
505
- let lineOffset = 0;
506
-
507
- for (let hunkIdx = 0; hunkIdx < hunks.length; hunkIdx++) {
508
- const hunk = hunks[hunkIdx]!;
509
-
510
- // Extract context and removal lines (lines that must exist in original)
511
- const expectedLines: string[] = [];
512
- const newLines: string[] = [];
513
-
514
- for (const line of hunk.lines) {
515
- const prefix = line[0];
516
- const content = line.slice(1);
517
- if (prefix === " ") {
518
- expectedLines.push(content);
519
- newLines.push(content);
520
- } else if (prefix === "-") {
521
- expectedLines.push(content);
522
- } else if (prefix === "+") {
523
- newLines.push(content);
524
- }
525
- }
526
-
527
- // Find the position where context matches
528
- const expectedStart = hunk.oldStart - 1 + lineOffset; // Convert 1-based to 0-based
529
- let matchPos = -1;
530
-
531
- // Try exact position first, then fuzzy within tolerance
532
- for (let offset = 0; offset <= MAX_FUZZ_OFFSET; offset++) {
533
- for (const dir of [0, 1, -1]) {
534
- const tryPos = expectedStart + offset * (dir === 0 ? 0 : dir);
535
- if (dir === 0 && offset > 0) continue; // Skip duplicate 0-offset
536
-
537
- if (tryPos < 0 || tryPos + expectedLines.length > lines.length) continue;
538
-
539
- let matches = true;
540
- for (let j = 0; j < expectedLines.length; j++) {
541
- if (lines[tryPos + j] !== expectedLines[j]) {
542
- matches = false;
543
- break;
544
- }
545
- }
546
-
547
- if (matches) {
548
- matchPos = tryPos;
549
- break;
550
- }
551
- }
552
- if (matchPos >= 0) break;
553
- }
554
-
555
- if (matchPos < 0) {
556
- // Fallback: global scan for context (used by relaxed patch formats where line numbers may be approximate)
557
- const candidates: number[] = [];
558
- for (let pos = 0; pos + expectedLines.length <= lines.length; pos++) {
559
- let matches = true;
560
- for (let j = 0; j < expectedLines.length; j++) {
561
- if (lines[pos + j] !== expectedLines[j]) {
562
- matches = false;
563
- break;
564
- }
565
- }
566
- if (matches) candidates.push(pos);
567
- }
568
-
569
- if (candidates.length === 1) {
570
- matchPos = candidates[0]!;
571
- } else {
572
- // Build a helpful error message
573
- const contextPreview = expectedLines.slice(0, 3).join("\n ");
574
- const ambiguity =
575
- candidates.length > 1
576
- ? `Context matched ${candidates.length} locations; provide more surrounding lines.`
577
- : "Context not found.";
578
- return {
579
- ok: false,
580
- error:
581
- `Hunk ${hunkIdx + 1} failed to apply to ${filePath} ` +
582
- `(expected at line ${hunk.oldStart}, searched ±${MAX_FUZZ_OFFSET} lines). ` +
583
- `${ambiguity}\n ${contextPreview}`,
584
- };
585
- }
586
- }
587
-
588
- // Apply: replace the matched range with the new lines
589
- lines = [
590
- ...lines.slice(0, matchPos),
591
- ...newLines,
592
- ...lines.slice(matchPos + expectedLines.length),
593
- ];
594
-
595
- // Update offset for subsequent hunks
596
- lineOffset += newLines.length - expectedLines.length;
597
- }
598
-
599
- return { ok: true, content: lines.join("\n") };
600
- }
601
-
602
- // ============================================================================
603
- // Atomic Patch Application
604
- // ============================================================================
605
-
606
- /**
607
- * Apply all file diffs atomically.
608
- * Writes to temp files first, then renames on success.
609
- * On any failure, cleans up all temp files.
610
- */
611
- async function applyPatchAtomic(
612
- diffs: FileDiff[],
613
- resolvedPaths: Map<string, string>,
614
- ): Promise<ToolResult> {
615
- const actions: FileAction[] = [];
616
- const tempFiles: string[] = [];
617
- let totalAdded = 0;
618
- let totalRemoved = 0;
619
-
620
- try {
621
- // Phase 1: Compute all file actions (read + apply hunks)
622
- for (const diff of diffs) {
623
- const isNewFile = diff.oldPath === null;
624
- const isDeleteFile = diff.newPath === null;
625
- const sourcePath = diff.oldPath ? resolvedPaths.get(diff.oldPath) : undefined;
626
- const targetPath = diff.newPath ? resolvedPaths.get(diff.newPath) : undefined;
627
-
628
- if (isDeleteFile) {
629
- // Deletion: mark for removal
630
- if (!sourcePath) {
631
- return fail(`Patch references missing delete source path: ${diff.oldPath}`);
632
- }
633
- let sourceStat: fsSync.Stats;
634
- try {
635
- sourceStat = await fs.stat(sourcePath);
636
- } catch (err) {
637
- return fail(`Cannot delete missing path: ${sourcePath}: ${getErrorMessage(err)}`);
638
- }
639
- if (sourceStat.isDirectory()) {
640
- return fail(`Patch delete targets a directory (unsupported): ${sourcePath}`);
641
- }
642
- actions.push({ resolvedPath: sourcePath, type: "delete" });
643
-
644
- // Count removed lines
645
- for (const hunk of diff.hunks) {
646
- for (const line of hunk.lines) {
647
- if (line.startsWith("-")) totalRemoved++;
648
- }
649
- }
650
- continue;
651
- }
652
-
653
- if (isNewFile) {
654
- if (!targetPath) {
655
- return fail(`Patch references missing create target path: ${diff.newPath}`);
656
- }
657
- // New file: build content from additions
658
- const contentLines: string[] = [];
659
- for (const hunk of diff.hunks) {
660
- for (const line of hunk.lines) {
661
- if (line.startsWith("+")) {
662
- contentLines.push(line.slice(1));
663
- totalAdded++;
664
- }
665
- }
666
- }
667
- const content = contentLines.join("\n") + "\n";
668
- actions.push({ resolvedPath: targetPath, type: "create", content });
669
- } else {
670
- if (!sourcePath || !targetPath) {
671
- return fail(
672
- `Patch references missing update source/target paths: ${diff.oldPath} -> ${diff.newPath}`,
673
- );
674
- }
675
- // Modify existing file
676
- let originalContent: string;
677
- try {
678
- originalContent = await fs.readFile(sourcePath, "utf-8");
679
- } catch (err) {
680
- return fail(`Cannot read file for patching: ${sourcePath}: ${getErrorMessage(err)}`);
681
- }
682
-
683
- const result = applyHunks(originalContent, diff.hunks, diff.newPath!);
684
- if (!result.ok) {
685
- return fail(result.error);
686
- }
687
-
688
- // Count additions and removals
689
- for (const hunk of diff.hunks) {
690
- for (const line of hunk.lines) {
691
- if (line.startsWith("+")) totalAdded++;
692
- else if (line.startsWith("-")) totalRemoved++;
693
- }
694
- }
695
-
696
- actions.push({
697
- sourcePath,
698
- resolvedPath: targetPath,
699
- type: sourcePath === targetPath ? "modify" : "move",
700
- content: result.content,
701
- });
702
- }
703
- }
704
-
705
- // Phase 2: Write all changes to temp files
706
- for (const action of actions) {
707
- if (action.type === "delete") continue;
708
-
709
- const dir = nodePath.dirname(action.resolvedPath);
710
- const base = nodePath.basename(action.resolvedPath);
711
- const suffix = crypto.randomBytes(6).toString("hex");
712
- const tempPath = nodePath.join(dir, `.${base}.patch-${suffix}`);
713
-
714
- // Ensure parent directory exists (needed for new files)
715
- await fs.mkdir(dir, { recursive: true });
716
-
717
- await fs.writeFile(tempPath, action.content!, "utf-8");
718
- tempFiles.push(tempPath);
719
- }
720
-
721
- // Phase 3: Atomic rename — all temp files to final destinations
722
- // Back up targets and deletion sources for rollback on failure
723
- const targetBackups = new Map<string, string>();
724
- const sourceRemovalBackups = new Map<string, string>();
725
- const appliedTargets: string[] = [];
726
- const removedSources: string[] = [];
727
-
728
- const backupTargetIfExists = async (targetPath: string): Promise<void> => {
729
- if (targetBackups.has(targetPath)) {
730
- return;
731
- }
732
- const backupSuffix = crypto.randomBytes(6).toString("hex");
733
- const backupPath = targetPath + `.patch-backup-${backupSuffix}`;
734
- try {
735
- await fs.copyFile(targetPath, backupPath);
736
- targetBackups.set(targetPath, backupPath);
737
- } catch (err) {
738
- const code = getErrnoCode(err);
739
- if (code === "ENOENT") {
740
- return;
741
- }
742
- throw err;
743
- }
744
- };
745
-
746
- const backupSourceRemovalPath = async (sourcePath: string): Promise<void> => {
747
- if (sourceRemovalBackups.has(sourcePath)) {
748
- return;
749
- }
750
- const backupSuffix = crypto.randomBytes(6).toString("hex");
751
- const backupPath = sourcePath + `.patch-source-backup-${backupSuffix}`;
752
- await fs.copyFile(sourcePath, backupPath);
753
- sourceRemovalBackups.set(sourcePath, backupPath);
754
- };
755
-
756
- const unlinkSourcePath = async (sourcePath: string): Promise<void> => {
757
- try {
758
- await fs.unlink(sourcePath);
759
- removedSources.push(sourcePath);
760
- } catch (err) {
761
- const code = getErrnoCode(err);
762
- // If the path is already gone, desired end-state (removed) is satisfied.
763
- if (code === "ENOENT") {
764
- return;
765
- }
766
- throw err;
767
- }
768
- };
769
-
770
- try {
771
- // Backup all existing target files that may be overwritten.
772
- for (const action of actions) {
773
- if (action.type === "delete") continue;
774
- await backupTargetIfExists(action.resolvedPath);
775
- }
776
-
777
- // Backup all files that will be removed (delete + move source path).
778
- for (const action of actions) {
779
- if (action.type === "delete") {
780
- await backupSourceRemovalPath(action.resolvedPath);
781
- continue;
782
- }
783
- if (
784
- action.type === "move" &&
785
- action.sourcePath &&
786
- action.sourcePath !== action.resolvedPath
787
- ) {
788
- await backupSourceRemovalPath(action.sourcePath);
789
- }
790
- }
791
-
792
- let tempIdx = 0;
793
- for (const action of actions) {
794
- if (action.type === "delete") continue;
795
-
796
- const tempPath = tempFiles[tempIdx]!;
797
- await fs.rename(tempPath, action.resolvedPath);
798
- appliedTargets.push(action.resolvedPath);
799
- tempIdx++;
800
- }
801
-
802
- // Apply delete/move source removals as part of the same transaction.
803
- for (const action of actions) {
804
- if (action.type === "delete") {
805
- await unlinkSourcePath(action.resolvedPath);
806
- } else if (
807
- action.type === "move" &&
808
- action.sourcePath &&
809
- action.sourcePath !== action.resolvedPath
810
- ) {
811
- await unlinkSourcePath(action.sourcePath);
812
- }
813
- }
814
- } catch (renameErr) {
815
- // Rollback: restore backup-backed targets, remove newly-created targets
816
- for (let idx = appliedTargets.length - 1; idx >= 0; idx--) {
817
- const target = appliedTargets[idx]!;
818
- const backup = targetBackups.get(target);
819
- try {
820
- if (backup) {
821
- await fs.rename(backup, target);
822
- } else {
823
- await fs.unlink(target);
824
- }
825
- } catch {
826
- /* best-effort */
827
- }
828
- }
829
-
830
- // Rollback removed source paths (delete/move) in reverse order.
831
- for (let idx = removedSources.length - 1; idx >= 0; idx--) {
832
- const sourcePath = removedSources[idx]!;
833
- const backup = sourceRemovalBackups.get(sourcePath);
834
- if (!backup) continue;
835
- try {
836
- await fs.rename(backup, sourcePath);
837
- sourceRemovalBackups.delete(sourcePath);
838
- } catch {
839
- /* best-effort */
840
- }
841
- }
842
-
843
- // Clean up any remaining backup files
844
- for (const backup of targetBackups.values()) {
845
- try {
846
- await fs.unlink(backup);
847
- } catch {
848
- /* best-effort */
849
- }
850
- }
851
- for (const backup of sourceRemovalBackups.values()) {
852
- try {
853
- await fs.unlink(backup);
854
- } catch {
855
- /* best-effort */
856
- }
857
- }
858
- // Clean up any remaining temp files
859
- for (const tempPath of tempFiles) {
860
- try {
861
- await fs.unlink(tempPath);
862
- } catch {
863
- /* best-effort */
864
- }
865
- }
866
- throw renameErr; // Will be caught by outer try/catch
867
- }
868
-
869
- // Success — clean up backups
870
- for (const backup of targetBackups.values()) {
871
- try {
872
- await fs.unlink(backup);
873
- } catch {
874
- /* best-effort */
875
- }
876
- }
877
- for (const backup of sourceRemovalBackups.values()) {
878
- try {
879
- await fs.unlink(backup);
880
- } catch {
881
- /* best-effort */
882
- }
883
- }
884
-
885
- // Build summary
886
- const filesChanged = actions.length;
887
- const created = actions.filter((a) => a.type === "create").length;
888
- const modified = actions.filter((a) => a.type === "modify").length;
889
- const deleted = actions.filter((a) => a.type === "delete").length;
890
- const moved = actions.filter((a) => a.type === "move").length;
891
-
892
- const parts: string[] = [];
893
- if (created > 0) parts.push(`${created} created`);
894
- if (modified > 0) parts.push(`${modified} modified`);
895
- if (moved > 0) parts.push(`${moved} moved`);
896
- if (deleted > 0) parts.push(`${deleted} deleted`);
897
-
898
- const summary =
899
- `Patch applied: ${filesChanged} file${filesChanged !== 1 ? "s" : ""} ` +
900
- `(${parts.join(", ")}), +${totalAdded}/-${totalRemoved} lines`;
901
-
902
- for (const action of actions) {
903
- if (action.type === "delete") {
904
- recordFrgMutation(action.resolvedPath, "delete");
905
- recordSearchMutation(action.resolvedPath, "delete");
906
- } else {
907
- recordFrgMutation(action.resolvedPath, "write", action.content);
908
- recordSearchMutation(action.resolvedPath, "write", action.content);
909
- if (
910
- action.type === "move" &&
911
- action.sourcePath &&
912
- action.sourcePath !== action.resolvedPath
913
- ) {
914
- recordFrgMutation(action.sourcePath, "delete");
915
- recordSearchMutation(action.sourcePath, "delete");
916
- }
917
- }
918
- }
919
-
920
- return success(summary, {
921
- filesChanged,
922
- created,
923
- modified,
924
- moved,
925
- deleted,
926
- linesAdded: totalAdded,
927
- linesRemoved: totalRemoved,
928
- files: actions.map((a) => ({
929
- path: a.type === "move" ? `${a.sourcePath} -> ${a.resolvedPath}` : a.resolvedPath,
930
- action: a.type,
931
- })),
932
- });
933
- } catch (err) {
934
- // Cleanup: remove all temp files on failure
935
- for (const tempPath of tempFiles) {
936
- try {
937
- await fs.unlink(tempPath);
938
- } catch {
939
- // Best-effort cleanup
940
- }
941
- }
942
- return fail(`Patch application failed: ${getErrorMessage(err)}`);
943
- }
944
- }
945
-
946
- // ============================================================================
947
- // Main Executor
948
- // ============================================================================
949
-
950
- /**
951
- * Execute the apply_patch tool.
952
- *
953
- * Parses a unified diff, validates all paths for security,
954
- * and applies changes atomically.
955
- */
956
- export async function executeApplyPatch(
957
- input: ApplyPatchInput,
958
- ctx: ToolContext,
959
- ): Promise<ToolResult> {
960
- try {
961
- if (!input.patch || input.patch.trim() === "") {
962
- return fail("Patch content is empty");
963
- }
964
-
965
- // Size guard: reject patches > 1MB to prevent memory exhaustion
966
- const MAX_PATCH_SIZE = 1_048_576; // 1MB
967
- if (input.patch.length > MAX_PATCH_SIZE) {
968
- return fail(`Patch too large: ${input.patch.length} bytes (max: ${MAX_PATCH_SIZE})`);
969
- }
970
-
971
- // Determine working directory
972
- const cwd = input.cwd ? nodePath.resolve(ctx.workingDir, input.cwd) : ctx.workingDir;
973
-
974
- // SECURITY: Validate cwd is within the working directory
975
- if (input.cwd) {
976
- let realCwd: string;
977
- try {
978
- realCwd = fsSync.realpathSync(cwd);
979
- } catch {
980
- return fail(`Working directory does not exist: ${cwd}`);
981
- }
982
- let realWorkingDir: string;
983
- try {
984
- realWorkingDir = fsSync.realpathSync(ctx.workingDir);
985
- } catch {
986
- return fail(`Base working directory does not exist: ${ctx.workingDir}`);
987
- }
988
- if (!isPathWithinBase(realCwd, realWorkingDir)) {
989
- return fail(`cwd must be within the working directory: ${input.cwd}`);
990
- }
991
- }
992
-
993
- // Verify cwd exists and is a directory
994
- try {
995
- const stat = await fs.stat(cwd);
996
- if (!stat.isDirectory()) {
997
- return fail(`Working directory is not a directory: ${cwd}`);
998
- }
999
- } catch {
1000
- return fail(`Working directory does not exist: ${cwd}`);
1001
- }
1002
-
1003
- // Step 1: Parse patch input (unified diff or apply_patch envelope)
1004
- let diffs: FileDiff[];
1005
- try {
1006
- diffs = parsePatchInput(input.patch);
1007
- } catch (err) {
1008
- return fail(`Failed to parse patch: ${getErrorMessage(err)}`);
1009
- }
1010
-
1011
- if (diffs.length === 0) {
1012
- return fail("No file diffs found in patch");
1013
- }
1014
-
1015
- // Step 2: Validate paths (SECURITY-CRITICAL)
1016
- // Check for Windows drive letters in paths (cross-platform safety)
1017
- for (const diff of diffs) {
1018
- for (const p of [diff.oldPath, diff.newPath]) {
1019
- if (p !== null && isWindowsDrivePath(p)) {
1020
- return fail(`Absolute path not allowed in patch: ${p}`);
1021
- }
1022
- }
1023
- }
1024
-
1025
- const validation = validatePatchPaths(diffs, cwd);
1026
- if (!validation.valid) {
1027
- return fail(validation.error);
1028
- }
1029
-
1030
- // Step 3: Apply atomically
1031
- return await applyPatchAtomic(diffs, validation.resolved);
1032
- } catch (err) {
1033
- return fail(`apply_patch failed: ${getErrorMessage(err)}`);
1034
- }
1035
- }