fix(memory): rotate a full section's oldest records to history

This commit is contained in:
sam committed 2026-10-25 23:10:15 +11:00
1 parent e6019c9f29
commit b30cb0b1db
4 files changed
+89 -7

No files matched your search

@@ -3,6 +3,7 @@
package builtin
import (
"errors"
"fmt"
"os"
"path/filepath"
@@ -119,6 +120,10 @@ func (s *Store) Size() int {
return len(renderSections(s.preamble, s.sections))
}
// ErrTooLarge rejects a write that would take the document past its size
// bound.
var ErrTooLarge = errors.New("would exceed max file size")
// AddResult describes the outcome of a successful Add.
type AddResult struct {
Section string
@@ -150,7 +155,7 @@ func (s *Store) Add(sectionName, content string) (AddResult, error) {
rendered := renderSections(s.preamble, candidate)
if len(rendered) > s.maxBytes {
return AddResult{}, fmt.Errorf("builtin: add to section %q would exceed max file size (%d > %d bytes)", sectionName, len(rendered), s.maxBytes)
return AddResult{}, fmt.Errorf("builtin: add to section %q: %w (%d > %d bytes)", sectionName, ErrTooLarge, len(rendered), s.maxBytes)
}
if err := writeFileAtomic(s.path, rendered); err != nil {
return AddResult{}, err
@@ -247,10 +247,34 @@ func (e *BuiltinEngine) Create(_ context.Context, in domainmemory.NewRecord) (do
if err != nil {
return domainmemory.Record{}, err
}
if _, err := stores.live.Add(section, block); err != nil {
return domainmemory.Record{}, err
for {
_, err := stores.live.Add(section, block)
if !errors.Is(err, builtin.ErrTooLarge) {
if err != nil {
return domainmemory.Record{}, err
}
return recordOf(marker, in.Scope, content), nil
}
if err := e.retireOldest(stores, section); err != nil {
return domainmemory.Record{}, err
}
}
return recordOf(marker, in.Scope, content), nil
}
// retireOldest moves the oldest record of a full section to history, so a
// section at the size cap keeps recording instead of refusing every write.
func (e *BuiltinEngine) retireOldest(stores *scopeStores, section string) error {
for _, block := range parseBlocks(stores.live.Render()) {
if block.section != section || block.marker.ID == "" {
continue
}
if err := appendHistory(stores.history, block.marker, block.content(), e.now(), true); err != nil {
return err
}
_, err := stores.live.Remove(section, block.text, true)
return err
}
return fmt.Errorf("memory: builtin engine: record larger than section %q can hold: %w", section, builtin.ErrTooLarge)
}
func (e *BuiltinEngine) Get(_ context.Context, scope domainmemory.Scope, id domainmemory.RecordID) (domainmemory.Record, error) {
@@ -0,0 +1,38 @@
package memory
import (
"context"
"fmt"
"strings"
"testing"
domainmemory "github.com/samcharles93/archie-core/internal/domain/memory"
)
// A section at the size cap must keep accepting records by moving its oldest
// ones to history, not fail the write and stop recording for good.
func TestBuiltinEngineCreateRotatesFullSection(t *testing.T) {
engine := NewBuiltinEngine(t.TempDir(), 4096)
scope := domainmemory.Scope{Kind: domainmemory.ScopeAgentUser, Agent: "a", User: "u"}
ctx := context.Background()
var ids []domainmemory.RecordID
for i := range 60 {
rec, err := engine.Create(ctx, domainmemory.NewRecord{
Scope: scope, Kind: "note", Author: "test",
Content: fmt.Sprintf("observation %02d %s", i, strings.Repeat("x", 80)),
})
if err != nil {
t.Fatalf("create %d: %v", i, err)
}
ids = append(ids, rec.ID)
}
if _, err := engine.Get(ctx, scope, ids[len(ids)-1]); err != nil {
t.Fatalf("newest record not live: %v", err)
}
revisions, err := engine.Revisions(ctx, scope, ids[0])
if err != nil || len(revisions) == 0 {
t.Fatalf("oldest record not retained in history: %v %v", revisions, err)
}
}
+18 -3
View File
@@ -1,6 +1,7 @@
package memory
import (
"errors"
"fmt"
"time"
@@ -32,10 +33,24 @@ func appendHistory(h *builtin.Store, m markerData, content string, at time.Time,
if err != nil {
return err
}
if _, err := h.Add(historySectionName, block); err != nil {
return fmt.Errorf("memory: builtin engine: retaining the superseded state of record %q: %w", m.ID, err)
for {
_, err := h.Add(historySectionName, block)
if !errors.Is(err, builtin.ErrTooLarge) {
if err != nil {
return fmt.Errorf("memory: builtin engine: retaining the superseded state of record %q: %w", m.ID, err)
}
return nil
}
// A full history drops its oldest state: provenance is bounded, and
// refusing here would stop the live document from rotating too.
oldest := parseBlocks(h.Render())
if len(oldest) == 0 {
return fmt.Errorf("memory: builtin engine: retaining the superseded state of record %q: %w", m.ID, err)
}
if _, err := h.Remove(historySectionName, oldest[0].text, true); err != nil {
return err
}
}
return nil
}
// revisionsOf returns one record's retained states, oldest first.