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
+37 -6
View File
@@ -30,7 +30,9 @@ func (inst *Instrument) Source(i int) (*FlatRegion, bool) {
// a document.
//
// Afterwards the instrument is as the document reads, with Skipped, Unread and
// Controls too. Use [Instrument.Source] to set opcodes the typed model doesn't
// Controls too. It returns an error wrapping [ErrNoSource], and changes nothing,
// if the document was edited since the instrument was read, or the instrument's
// regions were changed other than with [Instrument.Apply]. Use [Instrument.Source] to set opcodes the typed model doesn't
// hold on the new region's header.
func (inst *Instrument) AddRegion(r Region) error {
if err := checkRegion(r); err != nil {
@@ -73,7 +75,9 @@ func (inst *Instrument) writeRegion(r Region, all bool) error {
}
// RemoveRegion removes region i from the document that the instrument was read
// from, with the comments and spacing that follow its header. It changes
// from, with the comments and spacing that follow its header. A comment on the
// lines above a <region> follows the header before it, so it isn't removed
// with the region it is above. It changes
// nothing, and returns an error, if the document can't be edited there: an
// error wrapping [ErrNoSource] if there is no region i or it has no header of
// its own to remove. Afterwards the instrument is as the document reads.
@@ -95,7 +99,8 @@ func (inst *Instrument) RemoveRegion(i int) error {
// MoveRegion moves region from in the document that the instrument was read
// from so that it is region to, with the comments and spacing that follow its
// header. The regions between keep their order, and so do the regions the typed
// header, as for [Instrument.RemoveRegion]. The regions between keep their
// order, and so do the regions the typed
// model has no region for. It changes nothing, and returns an error, if the
// document can't be edited there: an error wrapping [ErrNoSource] if either
// region has no header of its own to move, and [ErrUnpatchable] if the headers
@@ -157,7 +162,7 @@ func (inst *Instrument) restructure(change func() error, want func(old []Region)
}
before := inst.doc.Flatten().Instrument()
if !inst.matches(before) {
return fmt.Errorf("%w: the document has changed since the instrument was read", ErrNoSource)
return fmt.Errorf("%w: the document or the instrument has changed since the instrument was read, so read it again", ErrNoSource)
}
saved := map[*File][]*Node{}
for _, f := range inst.doc.Files() {
@@ -173,6 +178,15 @@ func (inst *Instrument) restructure(change func() error, want func(old []Region)
undo()
return err
}
// The text that is written has to read back as the nodes, which it doesn't
// when a comment that never ends, or a line comment, would take in the text
// that comes after it.
for _, f := range inst.doc.Files() {
if !slices.Equal(significantNodes(lex(f.String())), significantNodes(f.Nodes)) {
undo()
return fmt.Errorf("%w: the text of %q would read differently from the nodes", ErrUnpatchable, f.Path)
}
}
after := inst.doc.Flatten().Instrument()
expected := want(before.Regions)
if len(after.Regions) != len(expected) {
@@ -190,10 +204,24 @@ func (inst *Instrument) restructure(change func() error, want func(old []Region)
return nil
}
// significantNodes returns the kind and text of the nodes that aren't
// whitespace.
func significantNodes(nodes []*Node) []string {
var out []string
for _, n := range nodes {
if n.Kind != KindSpace {
out = append(out, n.Kind.String()+":"+n.Raw)
}
}
return out
}
// matches reports whether other, read from the instrument's document, has the
// regions that the instrument was read with, from the same headers.
// regions that the instrument holds, from the same headers. An instrument whose
// regions were changed other than by [Instrument.Apply] doesn't match, as
// reading it again would lose the change.
func (inst *Instrument) matches(other *Instrument) bool {
if len(inst.sources) != len(other.sources) {
if len(inst.sources) != len(other.sources) || len(inst.Regions) != len(other.Regions) {
return false
}
for i, s := range inst.sources {
@@ -201,6 +229,9 @@ func (inst *Instrument) matches(other *Instrument) bool {
if (s.Header == nil) != (o.Header == nil) || s.Header != nil && s.Header.Node != o.Header.Node {
return false
}
if len(differing(inst.Regions[i], other.Regions[i])) > 0 {
return false
}
}
return true
}