X-Git-Url: http://git.giacomolli.uk/onechat/blobdiff_plain/feda522eba3e2a27c91741c77d0d01f1840c0004..c9183f90d757006e7221ecd0d4dc0db2a25f87e0:/memory.go?ds=sidebyside diff --git a/memory.go b/memory.go index 733673b..c3e9f45 100644 --- a/memory.go +++ b/memory.go @@ -272,15 +272,24 @@ func (m *Memory) Append(kind, text string) (Message, error) { } // addMessage builds the leaf of message when it needs no model call, or else -// adds the leaf to the queue. Then it appends the leaf to the views. +// adds the leaf to the queue. Then it appends the leaf to the views. A crash can +// stop an append after the leaf build and before the view save, so a leaf that +// is built already stays as it is, and restoreQueue settles its parent. func (m *Memory) addMessage(message Message) error { + built, err := m.tree.Built(0, message.ID) + if err != nil { + return err + } + leaf, free := freeLeaf(message) - if free { - err := m.build(leaf) + switch { + case built: + case free: + err = m.build(leaf) if err != nil { return err } - } else { + default: m.queue.AddLeaf(message.ID) } @@ -529,25 +538,3 @@ func (m *Memory) context(level, index int) string { return m.cview.Context(end) } - -// writeFileAtomic replaces the file at path with data through a temporary file -// and a rename. A reader or a crash of the process sees the old file or the new -// file, never a part of one. -func writeFileAtomic(path string, data []byte) error { - file, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".*") - if err != nil { - return fmt.Errorf("create temporary file: %w", err) - } - - _, err = file.Write(data) - err = errors.Join(err, file.Close()) - if err == nil { - err = os.Rename(file.Name(), path) - } - - if err != nil { - return errors.Join(fmt.Errorf("replace file: %w", err), os.Remove(file.Name())) - } - - return nil -}