~bigbes/sr-ht-dolt

ref: 231b7764d90a18c5f1bc6608fff9bd0534bb603d sr-ht-dolt/web/memory.go -rw-r--r-- 2.8 KiB
231b7764 — Eugene Blikh web: give bd memories their own view 5 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package web

import (
	"context"
	"net/url"

	"sourcecraft.dev/bigbes/sr-ht-dolt/beads"
	"sourcecraft.dev/bigbes/sr-ht-dolt/browse"
	"sourcecraft.dev/bigbes/sr-ht-dolt/core"
)

// memoryView renders the memories `bd remember` writes: config rows keyed
// kv.memory.<slug>, each with the revision its value was last written at. The
// row itself is (key, value) and carries no timestamp, so the date comes out of
// the history — see beads.BuildMemories for the walk that finds it.
//
// The reading is not here. It lives in the beads package, which the MCP
// surface's list_memories shares; this type is only the View adapter — slug,
// label, template, and the hand-off of the request's ref and query.
type memoryView struct{}

// init registers the Memory view, and registers it *after* Milestones, which is
// where the design puts the tab: registration order is tab order (views.go).
//
// Getting that order is not free here, and the extra line is the whole reason
// this comment exists. A package's init functions run in lexical file-name
// order, and "memory.go" sorts before "milestones.go" — so registering only
// memoryView would put this tab between Beads and Milestones. Registering
// milestonesView first claims the slot ahead of it; milestones.go's own init
// then re-registers the same view, which RegisterView resolves in place (same
// Name), leaving the order alone. Both calls name real views, so a rename or a
// removal on either side is a compile error rather than a silently reordered
// tab bar.
func init() {
	RegisterView(&milestonesView{})
	RegisterView(&memoryView{})
}

func (*memoryView) Name() string     { return "memory" }
func (*memoryView) Label() string    { return "Memory" }
func (*memoryView) Template() string { return "memory.html" }

// Applies is the beads fingerprint plus a config table carrying key and value;
// see beads.AppliesMemories. Applies sees table shapes and never rows, so a
// tracker that has never had a memory written into it still gets the tab and
// renders an empty state — the same contract Milestones has.
func (*memoryView) Applies(tables []browse.TableInfo) bool { return beads.AppliesMemories(tables) }

// Build reads the memories and dates them from the history. The result is a
// *beads.MemoryView, handed to memory.html as its .Data.
//
// The clock is passed in rather than read inside the projection: staleness is
// the one thing on this page that depends on when it was rendered, and timeNow
// is the same swappable clock the freshness line uses, so a test pins both at
// once.
func (*memoryView) Build(ctx context.Context, sess BrowseSession, _ *core.Repo, ref string, query url.Values) (any, error) {
	data, err := beads.BuildMemories(ctx, sess, ref, query, timeNow())
	if err != nil {
		return nil, err
	}
	return data, nil
}