18 // nodeBytes is the size limit of a node text in UTF-8 bytes. A model can go a
19 // few bytes over it, because the view measures the real sizes.
22 // treeRecordBytes is the size of a record of index/tree.idx: the position of
23 // the node line, the node index, and the node level.
24 const treeRecordBytes = 32
26 // Tree holds the built nodes of one chat. It writes one JSON line for each node
27 // to tree/YYYY-MM-DD.jsonl, named for the local day of the build. Each node is
28 // built once and never again. A node of a higher level is built only after both
29 // of its children. After a failed write, the tree refuses all writes until it
30 // is opened again. The caller holds the lock of the chat and serializes all
33 // The tree keeps no node in memory. The index index/tree.idx lists the node
34 // lines in file order, and index/tree-L.idx holds the position of node (L, i)
35 // in record i, with a zero record for a node that is not built. At open the
36 // tree indexes the lines after the last indexed one, so a missing index is
37 // built again from the day files.
39 clock func() time.Time
43 levels map[int]*Records
47 // Node is one line of the summary tree. Node (l, i) covers the 2^l messages
48 // from i*2^l on. A node of level 0 holds one message. A node of a higher level
49 // merges its two children, (l-1, 2i) and (l-1, 2i+1). Size is the UTF-8 byte
54 Text string `json:"text"`
55 Size int `json:"size"`
58 // NodeKey names the node (l, i).
64 // openTree opens the tree of the chat in root and indexes its new lines. A chat
65 // of total messages cannot have a node that covers a later message.
66 func openTree(ctx context.Context, root string, total int, clock func() time.Time) (*Tree, error) {
67 for _, dir := range []string{"tree", "index"} {
68 err := os.MkdirAll(filepath.Join(root, dir), 0o700)
70 return nil, fmt.Errorf("create %s directory: %w", dir, err)
77 dir: filepath.Join(root, "tree"),
79 dir: filepath.Join(root, "index"),
80 levels: make(map[int]*Records),
83 err := t.open(ctx, total)
85 return nil, errors.Join(fmt.Errorf("index tree: %w", err), t.Close())
91 func (t *Tree) open(ctx context.Context, total int) error {
94 t.order, err = openRecords(filepath.Join(t.dir, "tree.idx"), treeRecordBytes)
96 return fmt.Errorf("open node order index: %w", err)
100 if t.order.count == 0 {
101 err = t.removeLevels()
103 return fmt.Errorf("remove empty level indexes: %w", err)
106 record, _, err := t.order.Read(t.order.count - 1)
108 return fmt.Errorf("read last node order: %w", err)
111 // A crash can stop an add between the two index writes, so the
112 // last node gets its level record again.
113 node, last := treeDecodeRecord(record)
114 err = t.writeLevel(node, last)
116 return fmt.Errorf("restore last node level: %w", err)
121 offset: last.offset + uint64(last.length),
125 err = t.journal.Scan(ctx, start, func(line []byte, place Position) error {
128 err := json.Unmarshal(line, &node)
130 return fmt.Errorf("decode tree node: %w", err)
135 return fmt.Errorf("validate scanned node: %w", err)
138 if nodeEnd(node.Level, node.Index) > total {
139 return fmt.Errorf("node (%d, %d) covers messages after the log", node.Level, node.Index)
142 err = t.addIndex(NodeKey{node.Level, node.Index}, place)
144 return fmt.Errorf("index scanned node: %w", err)
150 return fmt.Errorf("scan node journal: %w", err)
156 // Close closes the files of the tree.
157 func (t *Tree) Close() error {
158 err := t.journal.Close()
160 err = errors.Join(err, t.order.Close())
164 for level, index := range t.levels {
165 err = errors.Join(err, index.Close())
167 delete(t.levels, level)
173 // removeLevels removes the level indexes, which are not valid without the order
175 func (t *Tree) removeLevels() error {
176 paths, err := filepath.Glob(filepath.Join(t.dir, "tree-*.idx"))
178 return fmt.Errorf("list level indexes: %w", err)
181 for _, path := range paths {
182 err = os.Remove(path)
184 return fmt.Errorf("remove level index: %w", err)
191 // writeLevel writes the position of node to its level index.
192 func (t *Tree) writeLevel(node NodeKey, place Position) error {
193 level, err := t.level(node.level)
195 return fmt.Errorf("open node level index: %w", err)
198 record := make([]byte, positionBytes)
199 encodePosition(record, place)
201 err = level.Write(node.index, record)
203 return fmt.Errorf("write node level record: %w", err)
209 // check makes sure that node is valid and can join the tree now.
210 func (t *Tree) check(node Node) error {
211 if node.Level < 0 || node.Level >= bits.UintSize-2 || node.Index < 0 {
212 return fmt.Errorf("node (%d, %d) is out of range", node.Level, node.Index)
215 if node.Text == "" || !utf8.ValidString(node.Text) || node.Size != len(node.Text) {
216 return fmt.Errorf("node (%d, %d) has no valid text of its size", node.Level, node.Index)
219 built, err := t.Built(node.Level, node.Index)
221 return fmt.Errorf("check target node: %w", err)
225 return fmt.Errorf("node (%d, %d) is already built", node.Level, node.Index)
232 for _, index := range []int{2 * node.Index, 2*node.Index + 1} {
233 built, err = t.Built(node.Level-1, index)
235 return fmt.Errorf("check child node: %w", err)
239 return fmt.Errorf("node (%d, %d) has a child that is not built", node.Level, node.Index)
246 // addIndex adds the node line at place to both indexes.
247 func (t *Tree) addIndex(node NodeKey, place Position) error {
248 record := make([]byte, treeRecordBytes)
249 encodePosition(record, place)
250 binary.LittleEndian.PutUint64(record[positionBytes:], uint64(node.index))
251 binary.LittleEndian.PutUint32(record[positionBytes+8:], uint32(node.level))
253 err := t.order.Append(record)
255 return fmt.Errorf("append node order: %w", err)
258 err = t.writeLevel(node, place)
260 return fmt.Errorf("write indexed node level: %w", err)
266 // level returns the index of level, and opens it at first use.
267 func (t *Tree) level(level int) (*Records, error) {
268 index, open := t.levels[level]
273 index, err := openRecords(filepath.Join(t.dir, fmt.Sprintf("tree-%d.idx", level)), positionBytes)
278 t.levels[level] = index
283 // Built tells if the node (l, i) is built.
284 func (t *Tree) Built(level, index int) (bool, error) {
285 _, built, err := t.place(level, index)
289 // place returns the position of the node (l, i) if it is built.
290 func (t *Tree) place(level, index int) (Position, bool, error) {
291 if level < 0 || level >= bits.UintSize-2 || index < 0 {
292 return Position{}, false, nil
295 records, err := t.level(level)
297 return Position{}, false, fmt.Errorf("open node level index: %w", err)
300 record, found, err := records.Read(index)
301 if err != nil || !found {
303 return Position{}, false, fmt.Errorf("read node level record: %w", err)
306 return Position{}, false, nil
309 place := decodePosition(record)
311 return place, place.length > 0, nil
314 // Add writes a new node to the tree.
315 func (t *Tree) Add(node Node) error {
316 if t.failure != nil {
317 return fmt.Errorf("tree needs a reopen after a failed write: %w", t.failure)
322 return fmt.Errorf("validate node: %w", err)
325 line, err := json.Marshal(node)
327 return fmt.Errorf("encode node: %w", err)
330 place, err := t.journal.Write(t.clock().Format(time.DateOnly), append(line, '\n'))
332 err = t.addIndex(NodeKey{node.Level, node.Index}, place)
337 return fmt.Errorf("write node (%d, %d): %w", node.Level, node.Index, err)
343 // Node reads the node (l, i) if it is built.
344 func (t *Tree) Node(level, index int) (Node, bool, error) {
345 place, built, err := t.place(level, index)
346 if err != nil || !built {
348 return Node{}, false, fmt.Errorf("find node position: %w", err)
351 return Node{}, false, nil
354 line, err := t.journal.Read(place)
356 return Node{}, false, fmt.Errorf("read node (%d, %d): %w", level, index, err)
361 err = json.Unmarshal(line, &node)
363 return Node{}, false, fmt.Errorf("decode node (%d, %d): %w", level, index, err)
366 if node.Level != level || node.Index != index {
367 return Node{}, false, fmt.Errorf("index of node (%d, %d) points to node (%d, %d)", level, index, node.Level, node.Index)
370 return node, true, nil
373 // Keys returns the keys of the built nodes, ordered by level and then by index.
374 func (t *Tree) Keys() ([]NodeKey, error) {
375 keys := make([]NodeKey, 0, t.order.count)
376 for n := range t.order.count {
377 record, _, err := t.order.Read(n)
382 key, _ := treeDecodeRecord(record)
383 keys = append(keys, key)
386 slices.SortFunc(keys, func(a, b NodeKey) int {
387 return cmp.Or(cmp.Compare(a.level, b.level), cmp.Compare(a.index, b.index))
393 // Count returns the number of built nodes.
394 func (t *Tree) Count() int {
398 // treeDecodeRecord reads the key and the position of a record of
400 func treeDecodeRecord(record []byte) (NodeKey, Position) {
402 level: int(binary.LittleEndian.Uint32(record[positionBytes+8:])),
403 index: int(binary.LittleEndian.Uint64(record[positionBytes:])),
406 return key, decodePosition(record)
409 // nodeEnd returns the ID after the last message of node (l, i).
410 func nodeEnd(level, index int) int {
411 return (index + 1) << level
414 // freeLeaf returns a text-only leaf when its source fits in a node. Images need
415 // a model description even when their text fits.
416 func freeLeaf(message Message) (Node, bool) {
417 text := leafSource(message)
418 if len(text) > nodeBytes || len(imageFiles(message.Text)) > 0 {
430 // leafSource returns the text that the leaf of message holds or summarizes.
431 func leafSource(message Message) string {
432 return message.Kind + ": " + message.Text
435 // freeMerge returns the parent of two sibling nodes when both texts, joined by
436 // a newline, fit in a node. Such a parent needs no model call.
437 func freeMerge(left, right Node) (Node, bool) {
438 if left.Size+1+right.Size > nodeBytes {
442 text := left.Text + "\n" + right.Text
445 Level: left.Level + 1,
446 Index: left.Index / 2,