Skip to content

Commit 8ca7e83

Browse files
committed
Make File.Patch throw on refusal by default
1 parent 4a0dee1 commit 8ca7e83

9 files changed

Lines changed: 85 additions & 31 deletions

File tree

‎packages/cli/src/documents/sidekick/environment.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ answer.
3939
before each diff row. Keep the row's diff marker and original spaces.
4040
A missing, misindented, or premature delimiter produces a source diagnostic
4141
before the entry runs. Use a longer outer fence when the diff contains a
42-
fence of the same length. Success emits no text. Read the source again
42+
fence of the same length. Success emits no text. A refusal throws and stops
43+
later work by default. Use `<PrintErrors>` around an invocation when the
44+
entry should print the diagnostic and continue. Read the source again
4345
after a stale-patch error.
4446

4547
````xmd

‎packages/cli/tests/retained-repository-local.test.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,17 @@ describe("one entry's local file work", () => {
485485
new InMemoryStream(events.slice(0, writeIndex)),
486486
counters(),
487487
);
488-
expect(resumed.refusal).toBe(undefined);
489-
expect(resumed.output).toContain("existing bytes no longer match");
488+
expect(resumed.refusal).toContain("existing bytes no longer match");
489+
expect(localRecords(resumed.events).at(-1)?.outcome.ok).toBe(false);
490490
expect(yield* readTextFile(target)).toBe("external\n");
491+
492+
yield* writeTextFile(target, "seed\n");
493+
const refusedReplay = counters();
494+
const refusedAgain = yield* run(fixture, source, unfinished(resumed.events), refusedReplay);
495+
expect(refusedAgain.refusal).toBe(resumed.refusal);
496+
expect(refusedReplay.reads).toEqual([]);
497+
expect(refusedReplay.writes).toEqual([]);
498+
expect(yield* readTextFile(target)).toBe("seed\n");
491499
});
492500

493501
it("975: workspace acknowledgements restore before live I/O and only the suffix runs", function* () {

‎packages/core/src/components/FilePatch.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { cwd } from "@executablemd/runtime";
22
import type { Operation } from "effection";
3-
import { printErrors } from "../component-failures.ts";
43
import { writeFileText } from "../files.ts";
54
import type { FormDeclaration } from "../invocation-identity.ts";
65
import type { Json, Segment } from "../types.ts";
@@ -43,8 +42,8 @@ export function* patchBody(
4342

4443
export const form: FormDeclaration = {
4544
forms: "paired",
46-
fn: printErrors(() => {
45+
fn: () => {
4746
throw new FileAccessError(FENCE);
48-
}),
47+
},
4948
refuse: () => new FileAccessError(FENCE),
5049
};

‎packages/core/src/components/components.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ the directories above it as needed.
176176

177177
## File.Patch
178178

179-
Applies a unified diff to one existing UTF-8 file. Inspect the current source
179+
Apply a unified diff to a file. Inspect the current source
180180
with `<File path="notes.md" />`, then submit a paired `<File.Patch>`.
181181

182182
````mdx
@@ -213,9 +213,9 @@ spaces from the original line. Use a longer outer fence when a diff row
213213
contains a fence of the same length. A missing, misindented, or premature
214214
delimiter reports a source diagnostic before the document runs.
215215

216-
Success renders nothing. Echoed `<File.Patch>` source means the patch did
217-
not execute. A stale or malformed diff inside valid delimiters produces a
218-
file error. Read the source again before editing a stale patch.
216+
Success renders nothing. A refused patch throws and stops later work by
217+
default. Wrap the invocation in `<PrintErrors>` to render the diagnostic and
218+
continue in a text document. Read the source again before retrying a stale patch.
219219

220220
## File.Copy
221221

‎packages/core/src/components/fixed-filesystem.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ const DEFINITIONS: readonly FunctionComponentDefinition[] = Object.freeze([
172172
parseJsonObject(fileProps),
173173
{
174174
description:
175-
'Apply an exact unified diff to an existing file. Read its current source with `<File path="notes.md" />` before writing `<File.Patch path="notes.md">…</File.Patch>`. A refused patch prints a file error.',
175+
"Apply a unified diff to a file. Provide one fenced diff. A refusal throws by default.",
176176
as: "Optional. Captures the empty string on success.",
177177
context:
178178
"Exactly one passive fenced unified diff for the relative path. Indent both fence markers to the structural body prefix, two spaces per enclosing block.",

‎packages/core/tests/file-delete-component.test.ts‎

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,12 @@ function patchDocument(path: string, diff: string): string {
577577
].join("\n");
578578
}
579579

580+
function printPatchRefusal(patch: string): string {
581+
return ["<PrintErrors>", ...patch.split("\n").map((line) => " " + line), "</PrintErrors>"].join(
582+
"\n",
583+
);
584+
}
585+
580586
const SMALL_PATCH = [
581587
"```diff",
582588
"--- a/target.txt",
@@ -722,6 +728,30 @@ describe("File.Patch", () => {
722728
}
723729
});
724730

731+
it("stops later effects on refusal unless PrintErrors encloses the patch", function* () {
732+
const fixture = yield* useFixture();
733+
const target = join(fixture.workspace, "target.txt");
734+
const later = join(fixture.workspace, "later.txt");
735+
const before = new TextEncoder().encode("first\nchanged\r\nlast");
736+
yield* until(writeFile(target, before));
737+
const patch = patchDocument("target.txt", SMALL_PATCH);
738+
const following = '\n<File path="later.txt">continued</File>';
739+
let refusal = "";
740+
try {
741+
yield* run(fixture, patch + following);
742+
} catch (error) {
743+
refusal = String(error);
744+
}
745+
expect(refusal).toContain("the diff no longer matches");
746+
expect(yield* exists(later)).toBe(false);
747+
expect(new Uint8Array(yield* until(readFile(target)))).toEqual(before);
748+
749+
const output = yield* run(fixture, printPatchRefusal(patch + following));
750+
expect(output).toContain("the diff no longer matches");
751+
expect(yield* readTextFile(later)).toBe("continued");
752+
expect(new Uint8Array(yield* until(readFile(target)))).toEqual(before);
753+
});
754+
725755
it("refuses the entire edit for stale, malformed or wrong-target diffs", function* () {
726756
const fixture = yield* useFixture();
727757
const target = join(fixture.workspace, "target.txt");
@@ -735,7 +765,9 @@ describe("File.Patch", () => {
735765
[SMALL_PATCH.replace("-2 +2", "-2,2 +2"), "exact, single-file unified diff"],
736766
[SMALL_PATCH.replaceAll("target.txt", "other.txt"), "exact, single-file unified diff"],
737767
]) {
738-
expect(yield* run(fixture, patchDocument("target.txt", diff))).toContain(message);
768+
expect(yield* run(fixture, printPatchRefusal(patchDocument("target.txt", diff)))).toContain(
769+
message,
770+
);
739771
expect(new Uint8Array(yield* until(readFile(target)))).toEqual(before);
740772
expect(new Uint8Array(yield* until(readFile(other)))).toEqual(before);
741773
}
@@ -847,9 +879,9 @@ describe("File.Patch", () => {
847879
const target = join(fixture.workspace, "target.txt");
848880
const invalid = new Uint8Array([0xff, ...new TextEncoder().encode("\nold\n")]);
849881
yield* until(writeFile(target, invalid));
850-
expect(yield* run(fixture, patchDocument("target.txt", SMALL_PATCH))).toContain(
851-
"existing bytes no longer match",
852-
);
882+
expect(
883+
yield* run(fixture, printPatchRefusal(patchDocument("target.txt", SMALL_PATCH))),
884+
).toContain("existing bytes no longer match");
853885
expect(new Uint8Array(yield* until(readFile(target)))).toEqual(invalid);
854886

855887
yield* writeTextFile(target, "first\r\nold");
@@ -868,13 +900,19 @@ describe("File.Patch", () => {
868900
yield* writeTextFile(other, before);
869901
const outside = "../outside/other.txt";
870902
const outsidePatch = SMALL_PATCH.replaceAll("target.txt", outside);
871-
expect(yield* run(fixture, patchDocument(outside, outsidePatch))).toContain("outside");
903+
expect(yield* run(fixture, printPatchRefusal(patchDocument(outside, outsidePatch)))).toContain(
904+
"outside",
905+
);
872906
expect(
873-
yield* run(fixture, patchDocument("target.txt", SMALL_PATCH), function* () {
874-
yield* Permissions.around({
875-
files: ([operation], next) => (operation === "write" ? false : next(operation)),
876-
});
877-
}),
907+
yield* run(
908+
fixture,
909+
printPatchRefusal(patchDocument("target.txt", SMALL_PATCH)),
910+
function* () {
911+
yield* Permissions.around({
912+
files: ([operation], next) => (operation === "write" ? false : next(operation)),
913+
});
914+
},
915+
),
878916
).toContain("permission denied");
879917
expect(yield* readTextFile(target)).toBe(before);
880918
expect(yield* readTextFile(other)).toBe(before);
@@ -885,6 +923,7 @@ describe("File.Patch", () => {
885923
const output = yield* run(fixture, '<Syntax names={["File.Patch"]} />');
886924
expect(output).toContain('<File.Patch path="notes.md">');
887925
expect(output).toContain("Exactly one passive fenced unified diff");
888-
expect(output).toContain("refused patch");
926+
expect(output).toContain("Apply a unified diff to a file.");
927+
expect(output).toContain("A refusal throws by default.");
889928
});
890929
});

‎packages/core/tests/fixtures/syntax-generated-base.json‎

Lines changed: 5 additions & 5 deletions
Large diffs are not rendered by default.

‎packages/workflow/tests/workspace-files.test.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -728,8 +728,11 @@ describe("WF workflow document filesystem", () => {
728728
" ```",
729729
"",
730730
"</File.Patch>",
731-
].join("\n");
732-
const run = yield* runDocument(database, source);
731+
];
732+
const run = yield* runDocument(
733+
database,
734+
["<PrintErrors>", ...source.map((line) => " " + line), "</PrintErrors>"].join("\n"),
735+
);
733736
expect(String(run.output)).toContain(
734737
'cannot write "seed.txt": the existing bytes no longer match',
735738
);

‎specs/executable-mdx-spec.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10030,8 +10030,11 @@ unterminated last line.
1003010030

1003110031
The replacement carries the read text as a precondition. A live provider
1003210032
checks it against the existing bytes before replacement. The host applies the
10033-
same filesystem stability boundary as a File write. A refusal renders through
10034-
File's error boundary. Success renders the empty string. Retaining hosts
10033+
same filesystem stability boundary as a File write. An invocation refusal
10034+
throws by default and stops later document effects. In a text root, an
10035+
explicit `<PrintErrors>` region renders the refusal and continues under §6.9.
10036+
Source diagnostics still stop the document before expansion. Success renders
10037+
the empty string. Retaining hosts
1003510038
restore the acknowledged read and write through the same records as File.
1003610039
`File.Patch` is available in authored XMD.
1003710040

0 commit comments

Comments
 (0)