| 1 | package memoria |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/binary" |
| 6 | "strconv" |
| 7 | "sync" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/suite" |
| 11 | |
| 12 | "amadan.net/oficina/memoria/internal/memoria/provider" |
| 13 | ) |
| 14 | |
| 15 | // §8.11's cache (Paul, 2026-10-27: "Cache briefly"). An answer is kept in |
| 16 | // memory only, never on disk, for AnswerCacheTTL, keyed by the asker, the |
| 17 | // query and the exact passages sent. The ruled search runs on every |
| 18 | // request regardless — §4.1 is asked every time — and a hit skips only the |
| 19 | // model call, so a changed passage, a different ruling or a different |
| 20 | // person is a different key rather than an invalidation to remember. |
| 21 | |
| 22 | // AnswerCacheTTL is how long a cached answer is reused. |
| 23 | const AnswerCacheTTL = 10 * time.Minute |
| 24 | |
| 25 | // AnswerCacheMax bounds the cache; past it, the oldest entry goes. |
| 26 | const AnswerCacheMax = 256 |
| 27 | |
| 28 | type answerCache struct { |
| 29 | mu sync.Mutex |
| 30 | now func() time.Time |
| 31 | entries map[[32]byte]cachedAnswer |
| 32 | } |
| 33 | |
| 34 | type cachedAnswer struct { |
| 35 | blocks []provider.Sentence |
| 36 | at time.Time |
| 37 | } |
| 38 | |
| 39 | func newAnswerCache() *answerCache { |
| 40 | return &answerCache{now: time.Now, entries: map[[32]byte]cachedAnswer{}} |
| 41 | } |
| 42 | |
| 43 | // answerKey hashes everything the model's reply depends on: the asker, |
| 44 | // the question exactly as sent to the model, and the passages sent, in |
| 45 | // order, by chunk id. |
| 46 | // |
| 47 | // **A chunk id is the passage's identity, its text and its revision at |
| 48 | // once.** Chunks are only ever inserted and deleted, never updated — a |
| 49 | // changed document gets new chunks — and chunks.id is AUTOINCREMENT, so |
| 50 | // SQLite never hands an id out twice. So the same words in another |
| 51 | // document, or in a later revision, are different ids (Astra, |
| 52 | // 2026-10-27). Text, document id and revision were in this key too, and |
| 53 | // removing each left every test that goes through the store green: with |
| 54 | // the id there, nothing could reach them. |
| 55 | // |
| 56 | // Every field is length-prefixed, so no two inputs concatenate alike: an |
| 57 | // asker "a" asking "bc" is not an asker "ab" asking "c". |
| 58 | func answerKey(asker suite.Subject, q string, chunkIDs []int64) [32]byte { |
| 59 | h := sha256.New() |
| 60 | field := func(s string) { |
| 61 | var n [8]byte |
| 62 | binary.BigEndian.PutUint64(n[:], uint64(len(s))) |
| 63 | h.Write(n[:]) |
| 64 | h.Write([]byte(s)) |
| 65 | } |
| 66 | field(string(asker.Kind)) |
| 67 | field(asker.Value) |
| 68 | field(q) |
| 69 | for _, id := range chunkIDs { |
| 70 | field(strconv.FormatInt(id, 10)) |
| 71 | } |
| 72 | var k [32]byte |
| 73 | copy(k[:], h.Sum(nil)) |
| 74 | return k |
| 75 | } |
| 76 | |
| 77 | func (c *answerCache) get(k [32]byte) ([]provider.Sentence, bool) { |
| 78 | c.mu.Lock() |
| 79 | defer c.mu.Unlock() |
| 80 | e, ok := c.entries[k] |
| 81 | if !ok { |
| 82 | return nil, false |
| 83 | } |
| 84 | if c.now().Sub(e.at) >= AnswerCacheTTL { |
| 85 | delete(c.entries, k) |
| 86 | return nil, false |
| 87 | } |
| 88 | return cloneBlocks(e.blocks), true |
| 89 | } |
| 90 | |
| 91 | func (c *answerCache) put(k [32]byte, blocks []provider.Sentence) { |
| 92 | c.mu.Lock() |
| 93 | defer c.mu.Unlock() |
| 94 | now := c.now() |
| 95 | if _, ok := c.entries[k]; !ok && len(c.entries) >= AnswerCacheMax { |
| 96 | var oldest [32]byte |
| 97 | first := true |
| 98 | for key, e := range c.entries { |
| 99 | if first || e.at.Before(c.entries[oldest].at) { |
| 100 | oldest, first = key, false |
| 101 | } |
| 102 | } |
| 103 | delete(c.entries, oldest) |
| 104 | } |
| 105 | c.entries[k] = cachedAnswer{blocks: cloneBlocks(blocks), at: now} |
| 106 | } |
| 107 | |
| 108 | // cloneBlocks copies a reply, so neither the caller nor the cache can |
| 109 | // change the other's copy. |
| 110 | func cloneBlocks(in []provider.Sentence) []provider.Sentence { |
| 111 | out := make([]provider.Sentence, len(in)) |
| 112 | for i, b := range in { |
| 113 | out[i] = provider.Sentence{Text: b.Text, Cite: append([]int(nil), b.Cite...)} |
| 114 | } |
| 115 | return out |
| 116 | } |
| 117 | |