Skip to content

Commit dbfbb13

Browse files
authored
fix(core): authorize patch move destinations (anomalyco#38133)
1 parent 99b2e78 commit dbfbb13

4 files changed

Lines changed: 58 additions & 4 deletions

File tree

‎packages/core/src/tool/patch.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ export const Plugin = {
189189
catch: (error) => new ToolFailure({ message: `patch verification failed: ${String(error)}` }),
190190
})
191191
const moveTarget = hunk.movePath ? resolveTarget(location, hunk.movePath) : undefined
192+
if (moveTarget) targets.push(moveTarget)
192193
if (moveTarget?.externalDirectory) {
193194
yield* permission.assert({
194195
action: "external_directory",

‎packages/core/test/patch.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ describe("Patch", () => {
4040
expect(() => parse("*** Begin Patch\n*** Add File: add.txt\n+added")).toThrow(
4141
"The last line of the patch must be '*** End Patch'",
4242
)
43+
expect(() => parse("extra\n*** Begin Patch\n*** End Patch")).toThrow(
44+
"The first line of the patch must be '*** Begin Patch'",
45+
)
46+
expect(() => parse("*** Begin Patch\n*** Add File: add.txt\n+added\n*** End Patch\nextra")).toThrow(
47+
"The last line of the patch must be '*** End Patch'",
48+
)
49+
})
50+
51+
test("allows whitespace after the end marker", () => {
52+
expect(parse("*** Begin Patch\n*** Add File: add.txt\n+added\n*** End Patch\n \t\n")).toEqual([
53+
{ type: "add", path: "add.txt", contents: "added" },
54+
])
4355
})
4456

4557
test("strips a heredoc wrapper", () => {
@@ -169,6 +181,25 @@ describe("Patch", () => {
169181
).toBe('He said "hi"\n')
170182
})
171183

184+
test("matches Unicode minus signs and spaces", () => {
185+
expect(
186+
Patch.derive("minus.txt", [{ oldLines: ["value - 1"], newLines: ["value - 2"] }], "value − 1\n")
187+
.content,
188+
).toBe("value - 2\n")
189+
const spaces = ["\u00A0", "\u2002", "\u2003", "\u2004", "\u2005", "\u2006", "\u2007", "\u2008", "\u2009", "\u200A", "\u202F", "\u205F", "\u3000"]
190+
spaces.forEach(
191+
(space) => {
192+
expect(
193+
Patch.derive(
194+
"spaces.txt",
195+
[{ oldLines: ["hello world"], newLines: ["hello there"] }],
196+
`hello${space}world\n`,
197+
).content,
198+
).toBe("hello there\n")
199+
},
200+
)
201+
})
202+
172203
test("matches EOF-anchored chunks from the end", () => {
173204
expect(
174205
Patch.derive(

‎packages/core/test/tool-patch.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,28 @@ describe("PatchTool", () => {
351351
),
352352
)
353353

354+
it.live("includes the move destination in edit permission resources", () =>
355+
withTempTool((directory, registry) =>
356+
Effect.gen(function* () {
357+
const source = path.join(directory, "old", "name.txt")
358+
yield* Effect.promise(() => fs.mkdir(path.dirname(source), { recursive: true }))
359+
yield* Effect.promise(() => fs.writeFile(source, "old content\n"))
360+
yield* executeTool(
361+
registry,
362+
call(
363+
"*** Begin Patch\n*** Update File: old/name.txt\n*** Move to: renamed/dir/name.txt\n@@\n-old content\n+new content\n*** End Patch",
364+
),
365+
)
366+
expect(assertions).toMatchObject([
367+
{
368+
action: "edit",
369+
resources: ["old/name.txt", "renamed/dir/name.txt"],
370+
},
371+
])
372+
}),
373+
),
374+
)
375+
354376
it.live("inserts lines with an insert-only hunk", () =>
355377
withTempTool((directory, registry) =>
356378
Effect.gen(function* () {

‎packages/util/src/patch.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ export function parse(patchText: string): Result.Result<ReadonlyArray<Hunk>, Par
4747
const lines = stripHeredoc(patchText.trim())
4848
.split("\n")
4949
.map((line) => (line.endsWith("\r") ? line.slice(0, -1) : line))
50-
const begin = lines.findIndex((line) => line.trim() === "*** Begin Patch")
51-
const end = lines.findIndex((line) => line.trim() === "*** End Patch")
50+
const begin = lines[0]?.trim() === "*** Begin Patch" ? 0 : -1
51+
const end = lines.at(-1)?.trim() === "*** End Patch" ? lines.length - 1 : -1
5252
if (begin === -1) return Result.fail(new BoundaryError({ boundary: "first" }))
5353
if (end === -1 || begin >= end) return Result.fail(new BoundaryError({ boundary: "last" }))
5454

@@ -227,9 +227,9 @@ const normalize = (value: string) =>
227227
value
228228
.replace(/[‘’‚‛]/g, "'")
229229
.replace(/[“”„‟]/g, '"')
230-
.replace(/[‐‑‒–—―]/g, "-")
230+
.replace(/[‐‑‒–—―−]/g, "-")
231231
.replace(/…/g, "...")
232-
.replace(/ /g, " ")
232+
.replace(/[\u00A0\u2002-\u200A\u202F\u205F\u3000]/g, " ")
233233
const splitBom = (text: string) =>
234234
text.startsWith("\uFEFF") ? { bom: true, text: text.slice(1) } : { bom: false, text }
235235
const stripHeredoc = (input: string) =>

0 commit comments

Comments
 (0)