memory: canonicalise habit keys, take the median on a clock, name the period

Three defects in how the counted profile is read back.

The counting unit was the key the LLM invented. There is no allowlist and no
normalization behind it, so "я выпил воду" and "попил воды" landed as different
keys, split one habit into two, and dropped both below the two-day threshold.
Keys now go through an alias table in behavior_ru.json before they are counted.
An unglossed key is quoted rather than recited as a verb, because "обычно ты
выпил_воды около 09:00" is not a sentence.

The typical time was a median of minutes since midnight, which is wrong for
anything that straddles midnight. Bedtimes of 23:40, 23:50, 00:10 and 00:20
gave 12:00, on the one activity most likely to cross the boundary. It is now a
circular median, and when the observations span more than half the clock she
names the habit without a time instead of inventing one.

The rest is wording. Profile.Since was computed and never spoken, so "обычно"
was an unfalsifiable claim; the overall read-back now says over how many days
of records it holds. The no-data weekday answer said "у меня пока нет ничего
постоянного" about a question concerning him. And "quiet" was a bare prefix in
the non-behavioural list, so any future self-fact key starting with those five
letters would have been dropped.

Found in review of #59.
This commit is contained in:
kami
2026-08-01 14:04:24 +04:00
parent 012bdcc1ae
commit ba33a677f8
4 changed files with 317 additions and 20 deletions
+143
View File
@@ -207,3 +207,146 @@ func TestWeekdayProfileExcludesEverydayHabits(t *testing.T) {
t.Fatalf("plain weekday readout should say the day is unremarkable and name the daily habits: %q", wed)
}
}
// TestTypicalTimeIsCircular — the median is over a clock, not a number line.
// Bedtimes either side of midnight used to average to midday, which is the
// exact error the median was chosen to avoid, on the one activity most likely
// to cross the boundary.
func TestTypicalTimeIsCircular(t *testing.T) {
now := behaviorNow()
var obs []Observation
for i, mins := range []int{23*60 + 40, 23*60 + 50, 10, 20} {
day := now.AddDate(0, 0, -(i + 1))
obs = append(obs, Observation{
At: time.Date(day.Year(), day.Month(), day.Day(), 0, mins, 0, 0, now.Location()),
Key: "sleep",
Kind: "self",
})
}
p := BuildProfile(obs, now)
if len(p.All) != 1 {
t.Fatalf("got %+v, want one activity", p.All)
}
got := p.All[0].TypicalAt
if !p.All[0].HasTypical {
t.Fatal("a four-observation cluster has a typical time")
}
if got < 23*time.Hour+55*time.Minute && got > 5*time.Minute {
t.Errorf("typical bedtime = %v, want just either side of midnight", got)
}
if s := p.FormatOverallRU(); strings.Contains(s, "около 12:00") {
t.Errorf("read back as %q", s)
}
}
// Times spread across the whole clock have no typical value, and she must not
// name one.
func TestNoTypicalTimeWhenSpreadWide(t *testing.T) {
now := behaviorNow()
var obs []Observation
for i, hh := range []int{2, 9, 16, 21} {
day := now.AddDate(0, 0, -(i + 1))
obs = append(obs, Observation{
At: time.Date(day.Year(), day.Month(), day.Day(), hh, 0, 0, 0, now.Location()),
Key: "water",
Kind: "self",
})
}
p := BuildProfile(obs, now)
if len(p.All) != 1 {
t.Fatalf("got %+v, want one activity", p.All)
}
if p.All[0].HasTypical {
t.Errorf("times spanning %v were given a typical value", p.All[0].TypicalAt)
}
if s := p.FormatOverallRU(); strings.Contains(s, "около") {
t.Errorf("read back with a time she cannot support: %q", s)
}
}
// TestKeysAreCanonicalisedBeforeCounting — the fact key comes out of the LLM
// with no allowlist behind it, so the same habit arrives spelled several ways.
// Counted separately, each spelling sits below MinHabitDays and the habit
// vanishes.
func TestKeysAreCanonicalisedBeforeCounting(t *testing.T) {
now := behaviorNow()
var obs []Observation
for i, key := range []string{"water", "Воду", "выпил воды", "попил_воды"} {
day := now.AddDate(0, 0, -(i + 1))
obs = append(obs, Observation{
At: time.Date(day.Year(), day.Month(), day.Day(), 9, 0, 0, 0, now.Location()),
Key: key,
Kind: "self",
})
}
p := BuildProfile(obs, now)
if len(p.All) != 1 {
t.Fatalf("got %+v, want one activity — the spellings are one habit", p.All)
}
if p.All[0].Key != "water" || p.All[0].Days != 4 {
t.Errorf("got %+v, want water on 4 days", p.All[0])
}
if s := p.FormatOverallRU(); !strings.Contains(s, "пьёшь воду") {
t.Errorf("read back as %q, want the glossed canonical key", s)
}
}
// An unglossed key is quoted, not read as a verb. "обычно ты выпил_воды около
// 09:00" is what reciting the raw key produced.
func TestUnglossedKeyIsQuoted(t *testing.T) {
now := behaviorNow()
p := BuildProfile(habitHistory("починил_кран", time.Tuesday, 12, 0, 2, now), now)
got := p.FormatOverallRU()
if !strings.Contains(got, "отмечаешь «починил кран»") {
t.Errorf("got %q", got)
}
}
// She says over what stretch of records "обычно" is claimed. Without it the
// same sentence comes out of three days and out of a year.
func TestOverallNamesThePeriod(t *testing.T) {
now := behaviorNow()
p := BuildProfile(habitHistory("water", time.Tuesday, 9, 0, 3, now), now)
got := p.FormatOverallRU()
if !strings.Contains(got, "по записям за последние 21 день") {
t.Errorf("got %q, want the period spoken", got)
}
}
// The no-data weekday answer is about him, not about her. "у меня пока нет
// ничего постоянного" answers a question nobody asked.
func TestEmptyWeekdayAnswerIsAboutHim(t *testing.T) {
p := BuildProfile(nil, behaviorNow())
got := p.FormatWeekdayRU(time.Wednesday)
if strings.Contains(got, "у меня") {
t.Errorf("got %q", got)
}
if !strings.Contains(got, "у тебя") {
t.Errorf("got %q, want an answer about him", got)
}
}
// "quiet" is machinery, but only as a whole key. As a bare five-letter prefix
// it silently swallowed any future self-fact key starting with those letters.
func TestQuietPrefixDoesNotSwallowRealKeys(t *testing.T) {
now := behaviorNow()
p := BuildProfile(habitHistory("quietude", time.Tuesday, 8, 0, 3, now), now)
if len(p.All) != 1 {
t.Fatalf("got %+v, want the key counted", p.All)
}
p = BuildProfile(habitHistory("quiet_hours", time.Tuesday, 8, 0, 3, now), now)
if len(p.All) != 0 {
t.Fatalf("got %+v, want maven's own tuning state dropped", p.All)
}
}
func TestPluralDaysRU(t *testing.T) {
for _, c := range []struct {
n int
want string
}{{1, "день"}, {2, "дня"}, {5, "дней"}, {11, "дней"}, {21, "день"}, {22, "дня"}, {114, "дней"}} {
if got := pluralDaysRU(c.n); got != c.want {
t.Errorf("pluralDaysRU(%d) = %q, want %q", c.n, got, c.want)
}
}
}