Engine
engine
Batch file editing engine.
Implements the validate-then-apply strategy from the axm-edit spec:
- Read all affected files once (snapshot)
- Validate every operation against the snapshot
- Resolve line positions (fuzzy search for
oldcontent) - Sort replace edits bottom-to-top to avoid line-shift
- Apply all operations atomically (or fail with 0 files touched)
ResolvedEdit
dataclass
An edit whose line position has been resolved against the file.
Source code in packages/axm-edit/src/axm_edit/core/engine.py
batch_apply(root, operations)
Validate and apply a batch of file operations.
Atomicity contract:
- Validation is the gate. All operations are validated first; if any
fails the batch is rejected wholesale (
success=False) before a single byte is written — a true all-or-nothing guarantee. - Apply is best-effort with automatic rollback. Once validation
passes, a targeted checkpoint of every touched path is captured and the
operations are applied. If any exception occurs mid-apply (anchor
drift, a
write_text/unlink/mkdirfailure, a permission error, …) the partial work is rolled back to that checkpoint — touched files are restored to their pre-batch bytes and batch-created files (and the empty directories created for them) are removed — and a failingBatchResultis returned. The filesystem is not made transactional at the OS level; the rollback restores only the snapshotted paths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
Path
|
Project root directory (all paths are relative to this). |
required |
operations
|
Sequence[Operation]
|
List of replace, create, rewrite and delete operations. |
required |
Returns:
| Type | Description |
|---|---|
BatchResult
|
BatchResult with success status, a targeted-path snapshot |
BatchResult
|
( |
Source code in packages/axm-edit/src/axm_edit/core/engine.py
| Python | |
|---|---|
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 | |