Part of archie-core-1786637499480 (ship the file-writing store as the
default memory engine).
Records the missing design decision first (migration-decisions.md#5,
"authoritative memory record and revision model" was explicitly open):
internal/memory/builtin.Store addresses blocks by content substring with
no stable per-block ID, which the new Record{ID}/Forget(id) contract
needs. Decided and implemented: an ID assigned once at Write via a
leading `<!--mem:<uuid>-->` marker line, stripped back out of
Record.Content on read, located by that marker (not content) for
Forget -- so an unrelated edit can never orphan a Forget. Identity maps
to a directory named hex(sha256(identity)), not the identity string, so
an arbitrary identity can never traverse outside the configured root.
Kind maps to a builtin section name via the store's own
ValidateSectionName, so an invalid Kind fails the same way an invalid
section already does.
internal/infrastructure/memory.BuiltinEngine wraps one builtin.Store per
identity and implements domain/memory.MemoryEngine end to end
(Write/Query/List/Forget, Lifecycle, Manifest). Lives under
internal/infrastructure per organisation.md ("infrastructure implements
domain contracts"), not internal/domain/memory, which stays
contract-only.
internal/memory/builtin is untouched -- confirmed via git status and its
own test suite passing unmodified, per this issue's acceptance
criterion. This engine is not yet wired into daemon bootstrap or the
config surface (archie-core-1786637490043); it proves the contract
holds for the builtin store the same way curator's reference
implementations prove the curator contract, and registers/runs cleanly
through domain/memory.Registry (TestBuiltinEngineRegistersAndRuns
ThroughTheRegistry).
71 lines
2.0 KiB
Go
71 lines
2.0 KiB
Go
package memory
|
|
|
|
import (
|
|
"strings"
|
|
|
|
domainmemory "github.com/samcharles93/archie-core/internal/domain/memory"
|
|
)
|
|
|
|
// sectionHeaderPrefix matches internal/memory/builtin's own format (its
|
|
// sectionHeaderPrefix constant is unexported): a "## " line starts a named
|
|
// section, and blocks within it are separated by one blank line. This
|
|
// package only ever reads what it wrote through builtin.Store.Add, so it
|
|
// only needs to read that one documented format back, not the general
|
|
// case builtin.Store itself parses.
|
|
const sectionHeaderPrefix = "## "
|
|
|
|
// parseRecords reads every marked block out of rendered and returns it as
|
|
// a Record scoped to identity. A block with no marker was not written by
|
|
// this engine (e.g. hand-edited) and is skipped: this engine's contract is
|
|
// only ever asked about records it assigned an ID to.
|
|
func parseRecords(identity, rendered string) []domainmemory.Record {
|
|
var records []domainmemory.Record
|
|
section := ""
|
|
var blockLines []string
|
|
|
|
flush := func() {
|
|
if len(blockLines) == 0 {
|
|
return
|
|
}
|
|
if rec, ok := parseBlock(identity, section, strings.Join(blockLines, "\n")); ok {
|
|
records = append(records, rec)
|
|
}
|
|
blockLines = nil
|
|
}
|
|
|
|
for line := range strings.SplitSeq(rendered, "\n") {
|
|
if name, ok := strings.CutPrefix(line, sectionHeaderPrefix); ok {
|
|
flush()
|
|
section = strings.TrimSpace(name)
|
|
continue
|
|
}
|
|
if strings.TrimSpace(line) == "" {
|
|
flush()
|
|
continue
|
|
}
|
|
blockLines = append(blockLines, line)
|
|
}
|
|
flush()
|
|
return records
|
|
}
|
|
|
|
// parseBlock splits one block back into its marker-carried ID and content.
|
|
// ok is false for a block with no recognizable marker.
|
|
func parseBlock(identity, section, block string) (domainmemory.Record, bool) {
|
|
first, rest, _ := strings.Cut(block, "\n")
|
|
id, ok := strings.CutPrefix(first, markerPrefix)
|
|
if !ok {
|
|
return domainmemory.Record{}, false
|
|
}
|
|
id, ok = strings.CutSuffix(id, markerSuffix)
|
|
if !ok {
|
|
return domainmemory.Record{}, false
|
|
}
|
|
return domainmemory.Record{
|
|
ID: id,
|
|
Identity: identity,
|
|
Kind: section,
|
|
Content: rest,
|
|
}, true
|
|
}
|