Check the text of structural edits and refuse stale instruments

AddRegion, RemoveRegion and MoveRegion now refuse an edit whose written text
would read differently from the nodes, as when a comment that never ends
takes in the text after it, and an instrument whose regions were changed
other than by Apply. Build refuses an unknown trigger and an empty random
range. Add tests for edits across files, defines, skipped regions and edge
cases, a random-sequence test and FuzzRegionEdits.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 01:26:36 +00:00
co-authored by Claude Sonnet 5.5
parent 49fd867283
commit 8db34cc16c
4 changed files with 395 additions and 17 deletions
+3 -3
View File
@@ -45,16 +45,16 @@ The model is read with each setting that a controller moves taken at the control
- `Instrument.Apply(i, region)` writes a change to a typed region back to the document that the instrument was read from, as changes to the region's own opcodes. It touches only the settings that changed, leaves unknown opcodes, comments and the other regions as they were, and puts every change in the region's own header, over what the region inherits. `Instrument.Patches` returns the changes without making them.
- It returns `ErrNoSource` if the region has no header to edit: a variable made the header, or an include or a variable brings in opcodes after it.
- It returns `ErrUnpatchable`, and changes nothing, for a change the header can't hold: removing an opcode the region inherits, or a sample outside `default_path`.
- `Instrument.AddRegion(region)`, `RemoveRegion(i)` and `MoveRegion(from, to)` change the document's structure and read the instrument again, so it is as the document reads. A removed or moved region takes the comments and spacing that follow its header. A new region goes at the end of the main file. Each changes nothing, and returns an error, if the result wouldn't read as intended.
- `Instrument.AddRegion(region)`, `RemoveRegion(i)` and `MoveRegion(from, to)` change the document's structure and read the instrument again, so it is as the document reads. A removed or moved region takes the comments and spacing that follow its header, so a comment on the lines above a `<region>` stays with the header before it. They read the document again, so they refuse an instrument whose regions were changed other than by `Apply`, or whose document was edited since it was read. A new region goes at the end of the main file. Each changes nothing, and returns an error, if the result wouldn't read as intended.
- `ErrNoSource` is for a region that has no header to move or remove, or an instrument that wasn't read from a document.
- `ErrUnpatchable` is for a region that would read differently where it ends up: a new region that a `default_path` changes, or a region moved under another `<group>`.
- `ErrUnpatchable` is for a region that would read differently where it ends up: a new region that a `default_path` changes, or a region moved under another `<group>` or over a `#define` that changes it, or text that wouldn't read back as the edited nodes, such as a comment that never ends.
- `Node` positions are worked out when asked for, with `File.Position`, so edits stay cheap.
## Writing and building
- `Document.Write` writes one file, and `WriteFiles` writes the main file and each file it includes, through a `FileWriter`. They take `WriteOptions`: `InlineIncludes` puts included files into the main file, `ExpandVariables` replaces variables with their values, and `LineEnding` sets the line break. The zero value writes each file as it was read.
- `Write` and `WriteTo` return an error for a document with includes unless `InlineIncludes` is set, as the files they hold would be lost. `WriteFiles` also returns an error for an include whose path is absolute or leaves the folder (`AllowParentPaths` opts in to `..` paths, not absolute ones), and for a file included more than once that would be written differently each time.
- `Build` makes a new document from an `Instrument`, with `BuildOptions`: `SplitFiles` puts regions in included files, `Variables` hoists repeated values into `#define`s and the samples' folder into `default_path`, and `Indent` sets the indent. Reading the result gives the instrument back, except for the opcodes and headers the typed model doesn't hold. It returns an error wrapping `ErrInvalid` for a value that can't be written, and for a region that wouldn't read back as it is, such as a zero `Region` with an empty `Trigger`. `NewRegion(sample)` makes a region with the format's defaults.
- `Build` makes a new document from an `Instrument`, with `BuildOptions`: `SplitFiles` puts regions in included files, `Variables` hoists repeated values into `#define`s and the samples' folder into `default_path`, and `Indent` sets the indent. Reading the result gives the instrument back, except for the opcodes and headers the typed model doesn't hold. It returns an error wrapping `ErrInvalid` for a value that can't be written, and for a region that wouldn't read back as it is, such as a zero `Region` with an empty `Trigger`, a `Trigger` the format doesn't have, or a random range that is empty. `NewRegion(sample)` makes a region with the format's defaults.
- A `Region.Sample` is written as one opcode, so `Build` and `Apply` refuse one with a line break, `//`, `/*`, `<`, or a space followed by `=` (as in `a b=c.wav`). A backslash reads back as `/`. A host that has such file names needs to map them to safe ones.
## Includes