Files
archie/internal/infrastructure/memory/history.go
T
sam 3c338a9722 refactor: strip doc and issue narration from code comments
Remove PRD/doc paths, bead IDs, phase notes and deleted-test references
from comments; drop test-only hooks left by the test deletion. Includes
in-progress archied bootstrap, gateway and query changes.
2026-10-03 21:47:46 +10:00

72 lines
2.8 KiB
Go

package memory
import (
"fmt"
"time"
domainmemory "github.com/samcharles93/archie-core/internal/domain/memory"
"github.com/samcharles93/archie-core/internal/infrastructure/memory/builtin"
)
// historyFileName is the second document in each scope's directory: every
// state the scope's records no longer live in -- superseded by an Update, or
// removed by a Forget -- so a record's provenance outlives its content
const historyFileName = "HISTORY.md"
// historySectionName is the single section every retained state is appended
// to. One section is enough: each block's marker carries its record id, so
// grouping by kind would be a second index over the same file.
const historySectionName = "history"
// historyMaxFileBytes bounds each scope's HISTORY.md, deliberately far above
// the live document's bound (builtin's 100KB default, which is what
// production passes: bootstrap builds the engine with a 0 maxFileBytes).
//
// It has its own bound because history grows on every Update and every Forget
// while the live document does not. That question stays open; this bound only
// makes reaching it a deliberate, enormous amount of history rather than an
// accident.
const historyMaxFileBytes = 4 * 1024 * 1024
// appendHistory appends one retained state -- the record as it was while
// live, plus when it stopped being live and whether it was deleted -- to the
// scope's history document. Content is passed beside m because the marker
// carries provenance, never the record's bytes.
func appendHistory(h *builtin.Store, m markerData, content string, at time.Time, deleted bool) error {
m.SupersededAt = &at
m.Deleted = deleted
block, err := renderBlock(m, content)
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)
}
return nil
}
// revisionsOf returns one record's retained states as HISTORY.md holds them,
// oldest first. Document order is append order, and a retained state is
// appended exactly when it stops being live, so no sort is needed -- or
// wanted: a crash between a history append and the live write leaves two
// entries for one revision, and sorting would not make
// that less true.
func revisionsOf(h *builtin.Store, scope domainmemory.Scope, id domainmemory.RecordID) []domainmemory.Revision {
var revisions []domainmemory.Revision
for _, block := range parseBlocks(h.Render()) {
if block.marker.ID != string(id) {
continue
}
var supersededAt time.Time
if block.marker.SupersededAt != nil {
supersededAt = *block.marker.SupersededAt
}
revisions = append(revisions, domainmemory.Revision{
Record: block.record(scope),
SupersededAt: supersededAt,
Deleted: block.marker.Deleted,
})
}
return revisions
}