Repository navigation
Expand file tree
/
Copy pathmemory_telemetry.go
More file actions
66 lines (63 loc) · 2.51 KB
/
Copy pathmemory_telemetry.go
File metadata and controls
66 lines (63 loc) · 2.51 KB
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
63
64
65
66
package store
import (
"encoding/json"
"log"
)
// PopulateMemoryTelemetry counts the admitted prompt lines in the same rune
// units as the budget. The source label remains intact; derived semantics
// decide the breakdown, with declared type as the compatibility fallback.
func PopulateMemoryTelemetry(block *MemoryBlock) {
block.TotalRunes = 0
block.PinnedRunes, block.BudgetedRunes = 0, 0
block.RunesBySemanticType = make(map[string]int)
block.BudgetedRunesBySemanticType = make(map[string]int)
for _, memory := range block.Memories {
kind := memory.SemanticType
if kind == "" {
kind = memory.Type
}
switch kind {
case MemoryFact, MemoryPreference, MemoryCorrection, MemoryDecision, MemoryProjectState:
default:
kind = "unclassified"
}
runes := MemoryLineCost(memory.Text)
block.TotalRunes += runes
if memory.Rule || memory.Origin == OriginPerson {
block.PinnedRunes += runes
} else {
block.BudgetedRunes += runes
block.BudgetedRunesBySemanticType[kind] += runes
}
block.RunesBySemanticType[kind] += runes
}
}
// LogMemoryAssembly records composition without memory text, IDs, context or
// owner names. The optional stage distinguishes a store assembly from the
// conversation's final set after dwell has been applied.
func LogMemoryAssembly(block MemoryBlock, stages ...string) {
stage := "assembly"
if len(stages) > 0 && stages[0] != "" {
stage = stages[0]
}
shares := make(map[string]float64, len(block.RunesBySemanticType))
for kind, runes := range block.RunesBySemanticType {
if block.TotalRunes > 0 {
shares[kind] = float64(runes) / float64(block.TotalRunes)
}
}
data, err := json.Marshal(struct {
Event string `json:"event"`
Stage string `json:"stage"`
TotalRunes int `json:"total_runes"`
PinnedRunes int `json:"pinned_runes"`
BudgetedRunes int `json:"budgeted_runes"`
BudgetRunes int `json:"budget_runes"`
RunesBySemanticType map[string]int `json:"runes_by_semantic_type"`
ShareBySemanticType map[string]float64 `json:"share_by_semantic_type"`
BudgetedRunesBySemanticType map[string]int `json:"budgeted_runes_by_semantic_type"`
}{"memory_admission", stage, block.TotalRunes, block.PinnedRunes, block.BudgetedRunes, MemoryBlockRunes, block.RunesBySemanticType, shares, block.BudgetedRunesBySemanticType})
if err == nil {
log.Print(string(data))
}
}