From b30cb0b1dbc1a034caedc23949f63bf97d948849 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 25 Oct 2026 23:10:15 +1100 Subject: [PATCH] fix(memory): rotate a full section's oldest records to history --- .../infrastructure/memory/builtin/store.go | 7 +++- .../infrastructure/memory/builtin_engine.go | 30 +++++++++++++-- .../memory/builtin_engine_test.go | 38 +++++++++++++++++++ internal/infrastructure/memory/history.go | 21 ++++++++-- 4 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 internal/infrastructure/memory/builtin_engine_test.go diff --git a/internal/infrastructure/memory/builtin/store.go b/internal/infrastructure/memory/builtin/store.go index 224f1676c..1e303e47c 100644 --- a/internal/infrastructure/memory/builtin/store.go +++ b/internal/infrastructure/memory/builtin/store.go @@ -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 diff --git a/internal/infrastructure/memory/builtin_engine.go b/internal/infrastructure/memory/builtin_engine.go index c19a77adf..1f1eafb60 100644 --- a/internal/infrastructure/memory/builtin_engine.go +++ b/internal/infrastructure/memory/builtin_engine.go @@ -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) { diff --git a/internal/infrastructure/memory/builtin_engine_test.go b/internal/infrastructure/memory/builtin_engine_test.go new file mode 100644 index 000000000..e2d7121fa --- /dev/null +++ b/internal/infrastructure/memory/builtin_engine_test.go @@ -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) + } +} diff --git a/internal/infrastructure/memory/history.go b/internal/infrastructure/memory/history.go index b690dd6ab..11e3c26a3 100644 --- a/internal/infrastructure/memory/history.go +++ b/internal/infrastructure/memory/history.go @@ -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.