c07722266a
Four reminders in a row on the box all landed at the first one's hour, each confirmed as if it had been read from the sentence: "напомни без четверти восемь выходить" fired at 07:30. followUpMerge inherits a missing slot from the previous same-intent turn, and a reminder time is one of those slots. It also filled the slot before actionReminder's own fallback parse could run, so inheriting hid a time that did parse. router.MentionsTime tells the two cases apart. A sentence that names no time still inherits, which is the follow-up the seam exists for. A sentence that names one the parser missed keeps an empty slot, so she asks. Missing the hour he said costs a question; borrowing one costs an alarm he stops thinking about. Signals are lexicon classes and digits only: the day qualifiers, parts of day, day offsets, weekdays by lemma through morph, the half-past and quarter-to markers, and a written clock whose minutes are two digits so a score does not pass for one. Fact keys and act fns inherit through the same call and are left alone: a borrowed key answers about the wrong thing out loud, which he hears, while a borrowed hour is silent until it fires.
273 lines
9.2 KiB
Go
273 lines
9.2 KiB
Go
package main
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/kami/maven/internal/dialogue"
|
|
"github.com/kami/maven/internal/router"
|
|
)
|
|
|
|
func TestFollowUpMerge(t *testing.T) {
|
|
base := time.Date(2026, 7, 6, 12, 0, 0, 0, time.UTC)
|
|
fireAt := base.Add(24 * time.Hour)
|
|
|
|
// prior turn: a reminder that resolved a fire time.
|
|
prev := &dialogue.Session{
|
|
Intent: dialogue.IntentReminder,
|
|
Slots: dialogue.Slots{Time: fireAt, HasTime: true, Text: "старый текст"},
|
|
Timestamp: base,
|
|
TTL: 2 * time.Minute,
|
|
}
|
|
|
|
t.Run("same intent inherits the missing time", func(t *testing.T) {
|
|
// follow-up reminder with text but no parsed time.
|
|
cur := router.Decision{
|
|
Intent: router.IntentReminder,
|
|
Slots: router.Slots{Text: "позвонить маме"},
|
|
}
|
|
got := followUpMerge(prev, cur, base.Add(30*time.Second))
|
|
if !got.Slots.HasTime || !got.Slots.Time.Equal(fireAt) {
|
|
t.Errorf("time not inherited: HasTime=%v Time=%v", got.Slots.HasTime, got.Slots.Time)
|
|
}
|
|
if got.Slots.Text != "позвонить маме" {
|
|
t.Errorf("current text was overwritten: %q", got.Slots.Text)
|
|
}
|
|
})
|
|
|
|
// V-543, measured on the box: four reminders in a row all landed at the
|
|
// first one's hour, each confirmed as if it had been read from the sentence.
|
|
// A sentence that names a time and fails to parse must ask, not borrow.
|
|
t.Run("a named time that did not parse is not inherited", func(t *testing.T) {
|
|
for _, utt := range []string{
|
|
"напомни без четверти восемь выходить",
|
|
"напомни в половине первого пообедать",
|
|
"напомни завтра принять лекарство",
|
|
"remind me at noon to stretch",
|
|
} {
|
|
cur := router.Decision{
|
|
Intent: router.IntentReminder,
|
|
Utterance: utt,
|
|
Slots: router.Slots{Text: utt},
|
|
}
|
|
got := followUpMerge(prev, cur, base.Add(30*time.Second))
|
|
if got.Slots.HasTime {
|
|
t.Errorf("%q borrowed the previous hour %v", utt, got.Slots.Time)
|
|
}
|
|
}
|
|
})
|
|
|
|
// The follow-up this seam exists for still works: the sentence names no
|
|
// time, so the previous one is the only one it could mean.
|
|
t.Run("a follow-up naming no time still inherits", func(t *testing.T) {
|
|
cur := router.Decision{
|
|
Intent: router.IntentReminder,
|
|
Utterance: "и ещё полить цветы",
|
|
Slots: router.Slots{Text: "полить цветы"},
|
|
}
|
|
got := followUpMerge(prev, cur, base.Add(30*time.Second))
|
|
if !got.Slots.HasTime || !got.Slots.Time.Equal(fireAt) {
|
|
t.Errorf("time not inherited: HasTime=%v Time=%v", got.Slots.HasTime, got.Slots.Time)
|
|
}
|
|
})
|
|
|
|
t.Run("current slot wins over prior (gaps only)", func(t *testing.T) {
|
|
own := base.Add(48 * time.Hour)
|
|
cur := router.Decision{
|
|
Intent: router.IntentReminder,
|
|
Slots: router.Slots{Time: own, HasTime: true},
|
|
}
|
|
got := followUpMerge(prev, cur, base.Add(30*time.Second))
|
|
if !got.Slots.Time.Equal(own) {
|
|
t.Errorf("current time clobbered by prior: %v", got.Slots.Time)
|
|
}
|
|
})
|
|
|
|
t.Run("different intent does not inherit", func(t *testing.T) {
|
|
cur := router.Decision{Intent: router.IntentFact, Slots: router.Slots{Key: "water", HasKey: true}}
|
|
got := followUpMerge(prev, cur, base.Add(30*time.Second))
|
|
if got.Slots.HasTime {
|
|
t.Error("time bled across a different intent")
|
|
}
|
|
})
|
|
|
|
t.Run("clarify turn does not inherit", func(t *testing.T) {
|
|
cur := router.Decision{Intent: router.IntentReminder, Clarify: true}
|
|
got := followUpMerge(prev, cur, base.Add(30*time.Second))
|
|
if got.Slots.HasTime {
|
|
t.Error("clarify turn inherited slots")
|
|
}
|
|
})
|
|
|
|
t.Run("expired prior does not inherit", func(t *testing.T) {
|
|
cur := router.Decision{Intent: router.IntentReminder, Slots: router.Slots{Text: "x"}}
|
|
got := followUpMerge(prev, cur, base.Add(3*time.Minute)) // past the 2-min TTL
|
|
if got.Slots.HasTime {
|
|
t.Error("expired session still inherited")
|
|
}
|
|
})
|
|
|
|
t.Run("nil prior is a no-op", func(t *testing.T) {
|
|
cur := router.Decision{Intent: router.IntentReminder, Slots: router.Slots{Text: "x"}}
|
|
got := followUpMerge(nil, cur, base)
|
|
if got.Slots.HasTime {
|
|
t.Error("nil prior produced inheritance")
|
|
}
|
|
})
|
|
|
|
t.Run("router-only Value survives the round-trip", func(t *testing.T) {
|
|
cur := router.Decision{
|
|
Intent: router.IntentFact,
|
|
Slots: router.Slots{Key: "sleep", HasKey: true, Value: "6h"},
|
|
}
|
|
factPrev := &dialogue.Session{
|
|
Intent: dialogue.IntentFact,
|
|
Slots: dialogue.Slots{Text: "спал"},
|
|
Timestamp: base,
|
|
TTL: 2 * time.Minute,
|
|
}
|
|
got := followUpMerge(factPrev, cur, base.Add(10*time.Second))
|
|
if got.Slots.Value != "6h" {
|
|
t.Errorf("Value lost through dialogue conversion: %q", got.Slots.Value)
|
|
}
|
|
})
|
|
|
|
// --- Cross-intent + anaphora tests (P3.2) ---
|
|
|
|
t.Run("query after fact inherits key via anaphora", func(t *testing.T) {
|
|
prior := &dialogue.Session{
|
|
Intent: dialogue.IntentFact,
|
|
Slots: dialogue.Slots{Key: "water", HasKey: true},
|
|
Timestamp: base,
|
|
TTL: 2 * time.Minute,
|
|
}
|
|
cur := router.Decision{
|
|
Intent: router.IntentQuery,
|
|
Utterance: "когда я это сделал?",
|
|
}
|
|
got := followUpMerge(prior, cur, base.Add(30*time.Second))
|
|
if !got.Slots.HasKey {
|
|
t.Error("query after fact with anaphora: key not inherited")
|
|
}
|
|
if got.Slots.Key != "water" {
|
|
t.Errorf("query after fact: got key=%q, want water", got.Slots.Key)
|
|
}
|
|
})
|
|
|
|
t.Run("query after fact without anaphora does not inherit", func(t *testing.T) {
|
|
prior := &dialogue.Session{
|
|
Intent: dialogue.IntentFact,
|
|
Slots: dialogue.Slots{Key: "water", HasKey: true},
|
|
Timestamp: base,
|
|
TTL: 2 * time.Minute,
|
|
}
|
|
cur := router.Decision{
|
|
Intent: router.IntentQuery,
|
|
Utterance: "какая погода в москве?",
|
|
}
|
|
got := followUpMerge(prior, cur, base.Add(30*time.Second))
|
|
if got.Slots.HasKey {
|
|
t.Error("query without anaphora inherited key when it shouldn't")
|
|
}
|
|
})
|
|
|
|
t.Run("three-turn breaks context correctly", func(t *testing.T) {
|
|
// Simulate: turn 1 (fact: water), turn 2 (weather query — break),
|
|
// turn 3 (query referring to turn 1 should NOT inherit from turn 2).
|
|
turn2 := &dialogue.Session{
|
|
Intent: dialogue.IntentQuery,
|
|
Slots: dialogue.Slots{Text: "какая погода в москве?"},
|
|
Timestamp: base.Add(30 * time.Second),
|
|
TTL: 2 * time.Minute,
|
|
}
|
|
cur := router.Decision{
|
|
Intent: router.IntentQuery,
|
|
Utterance: "когда я это сделал?",
|
|
}
|
|
// turn2 is the "prior" but has no key — anaphora should not resolve.
|
|
got := followUpMerge(turn2, cur, base.Add(60*time.Second))
|
|
if got.Slots.HasKey {
|
|
t.Error("key inherited across a weather break that had no key")
|
|
}
|
|
})
|
|
|
|
t.Run("anaphora in reminder inherits key", func(t *testing.T) {
|
|
prior := &dialogue.Session{
|
|
Intent: dialogue.IntentFact,
|
|
Slots: dialogue.Slots{Key: "water", HasKey: true},
|
|
Timestamp: base,
|
|
TTL: 2 * time.Minute,
|
|
}
|
|
cur := router.Decision{
|
|
Intent: router.IntentReminder,
|
|
Utterance: "напомни про это завтра",
|
|
}
|
|
got := followUpMerge(prior, cur, base.Add(30*time.Second))
|
|
if !got.Slots.HasKey {
|
|
t.Error("reminder with anaphora: key not inherited")
|
|
}
|
|
if got.Slots.Key != "water" {
|
|
t.Errorf("reminder anaphora: got key=%q, want water", got.Slots.Key)
|
|
}
|
|
})
|
|
|
|
t.Run("anaphora in new fact inherits key", func(t *testing.T) {
|
|
prior := &dialogue.Session{
|
|
Intent: dialogue.IntentFact,
|
|
Slots: dialogue.Slots{Key: "water", HasKey: true},
|
|
Timestamp: base,
|
|
TTL: 2 * time.Minute,
|
|
}
|
|
cur := router.Decision{
|
|
Intent: router.IntentFact,
|
|
Slots: router.Slots{Value: "2 литра"},
|
|
Utterance: "я выпил это",
|
|
}
|
|
got := followUpMerge(prior, cur, base.Add(30*time.Second))
|
|
if !got.Slots.HasKey {
|
|
t.Error("fact with anaphora: key not inherited")
|
|
}
|
|
if got.Slots.Key != "water" {
|
|
t.Errorf("fact anaphora: got key=%q, want water", got.Slots.Key)
|
|
}
|
|
})
|
|
|
|
t.Run("explicit key wins over anaphora", func(t *testing.T) {
|
|
prior := &dialogue.Session{
|
|
Intent: dialogue.IntentFact,
|
|
Slots: dialogue.Slots{Key: "water", HasKey: true},
|
|
Timestamp: base,
|
|
TTL: 2 * time.Minute,
|
|
}
|
|
cur := router.Decision{
|
|
Intent: router.IntentFact,
|
|
Slots: router.Slots{Key: "sleep", HasKey: true, Value: "6h"},
|
|
Utterance: "я спал 6 часов",
|
|
}
|
|
// Even though the utterance doesn't have anaphora, the explicit key
|
|
// from the fact parser should win — same intent, same merge as before.
|
|
got := followUpMerge(prior, cur, base.Add(30*time.Second))
|
|
if !got.Slots.HasKey || got.Slots.Key != "sleep" {
|
|
t.Errorf("explicit key overwritten by prior: got key=%q", got.Slots.Key)
|
|
}
|
|
})
|
|
|
|
t.Run("query after fact inherits time too", func(t *testing.T) {
|
|
factTime := base.Add(-2 * time.Hour)
|
|
prior := &dialogue.Session{
|
|
Intent: dialogue.IntentFact,
|
|
Slots: dialogue.Slots{Key: "water", HasKey: true, Time: factTime, HasTime: true},
|
|
Timestamp: base,
|
|
TTL: 2 * time.Minute,
|
|
}
|
|
cur := router.Decision{
|
|
Intent: router.IntentQuery,
|
|
Utterance: "когда я это сделал?",
|
|
}
|
|
got := followUpMerge(prior, cur, base.Add(30*time.Second))
|
|
if !got.Slots.HasTime || !got.Slots.Time.Equal(factTime) {
|
|
t.Errorf("query after fact did not inherit time: HasTime=%v, Time=%v", got.Slots.HasTime, got.Slots.Time)
|
|
}
|
|
})
|
|
}
|