diff --git a/cmd/mavend/dialogue_contract_test.go b/cmd/mavend/dialogue_contract_test.go index 1eac958..ba62cf3 100644 --- a/cmd/mavend/dialogue_contract_test.go +++ b/cmd/mavend/dialogue_contract_test.go @@ -1,10 +1,18 @@ package main import ( + "bytes" + "context" + "log" + "os" "strings" + "testing" "time" "github.com/kami/maven/internal/dialogue" + "github.com/kami/maven/internal/memory" + "github.com/kami/maven/internal/router" + "github.com/kami/maven/internal/store" ) // Dialogue contract tests (V-563, child of V-558). @@ -141,3 +149,293 @@ type trace struct { turns []turn end endState } + +// newDialogueHandler — the offline floor with the real cascade and a movable +// clock: newClarifyHandler's wiring (stub date parser, real fact parser, tool +// matcher) plus the router newRoutingClarifyHandler builds, and the `now` +// pointer so a turn can carry a wait. +func newDialogueHandler(t *testing.T) (*reactiveHandler, *store.Store, *time.Time) { + t.Helper() + h, st, now := newClarifyHandler(t) + h.router = buildRouter(router.NewHashEmbedder(1024), h.matcher, 0.55, nil) + h.recall = recallWiring{embedder: router.NewHashEmbedder(1024), memStore: memory.NewInMemoryStore()} + return h, st, now +} + +// runTrace drives one trace through handleText and checks every turn, then the +// end state. Every failure carries the decision trace so far, so a wrong +// claimant reads differently from wrong copy. +func runTrace(t *testing.T, tr trace) { + t.Helper() + // MAVEN_DIALOGUE_NO_SKIP=1 runs the rows that fail today. That is how + // whoever lands V-560, V-561 or V-562 sees their row go green before + // deleting its skip, and it is also the check that a skip is still earned: + // a row that passes with the skip in place is a fix nobody noticed. + if tr.skip != "" && os.Getenv("MAVEN_DIALOGUE_NO_SKIP") == "" { + t.Skip(tr.skip) + } + ctx := context.Background() + h, st, now := newDialogueHandler(t) + const conversation = "web" + id := dialogueIDFor(sourceText, conversation) + + var claims []claim + fail := func(turnIdx int, format string, args ...any) { + t.Helper() + lines := make([]string, 0, len(claims)) + for _, c := range claims { + lines = append(lines, " "+c.String()) + } + t.Fatalf("turn %d: "+format+"\n who claimed what:\n%s", + append([]any{turnIdx}, append(args, strings.Join(lines, "\n"))...)...) + } + + for i, tn := range tr.turns { + if tn.wait > 0 { + *now = now.Add(tn.wait) + } + var logged bytes.Buffer + prev := log.Writer() + log.SetOutput(&logged) + reply := h.handleText(ctx, conversation, tn.say) + log.SetOutput(prev) + claims = append(claims, claimsOf(tn.say, logged.String())) + + body := reply + if tn.expired { + if !isClarifyExpired(reply) { + fail(i, "reply %q must open with the expiry notice", reply) + } + body = trimClarifyExpired(reply) + // The notice is glued in front of this turn's reply, and both halves + // have to survive: the words he just said are routed fresh, and + // answering only "I let the old one go" drops them. + if body == "" { + fail(i, "the notice was the whole reply; the fresh words were never answered") + } + } else if isClarifyExpired(reply) { + fail(i, "reply %q announced an expiry nothing asked for", reply) + } + if tn.question != "" { + want, ok := clarifyQuestionFor(tn.question, tn.attempt) + if !ok { + fail(i, "no question exists for slot %s attempt %d", tn.question, tn.attempt) + } + if body != want { + fail(i, "reply %q, want the %s question worded for attempt %d, %q", body, tn.question, tn.attempt, want) + } + } + if tn.noQuestion && isAnyClarifyQuestion(body) { + fail(i, "reply %q is another question; this turn is not something to ask about", body) + } + for _, want := range tn.contains { + if !strings.Contains(body, want) { + fail(i, "reply %q does not carry %q", body, want) + } + } + for _, unwanted := range tn.notContain { + if strings.Contains(body, unwanted) { + fail(i, "reply %q carries %q and must not", body, unwanted) + } + } + checkParked(t, fail, i, h.clarifyStore.Get(id, h.now()), tn.parked) + } + checkEnd(t, ctx, st, h, tr.end, claims) +} + +// isAnyClarifyQuestion — is this reply one of her clarify questions, at any +// attempt wording? Reads the templates rather than a list of its own. +func isAnyClarifyQuestion(reply string) bool { + for _, variants := range clarifyQuestionVariants { + for _, v := range variants { + if reply == v { + return true + } + } + } + return false +} + +func checkParked(t *testing.T, fail func(int, string, ...any), i int, got *dialogue.PendingQuestion, want *parkedWant) { + t.Helper() + if want == nil { + if got != nil { + fail(i, "a question about %v is still armed and nothing should be: %+v", got.Missing, got.Slots) + } + return + } + if got == nil { + fail(i, "nothing is armed, want a question about %s (attempt %d)", want.slot, want.attempt) + return + } + if len(got.Missing) != 1 || got.Missing[0] != want.slot { + fail(i, "armed question is about %v, want %s", got.Missing, want.slot) + } + if got.Attempts != want.attempt { + fail(i, "armed question is on attempt %d, want %d — a retry spent on something that was never an answer is the V-554 shape", got.Attempts, want.attempt) + } + if want.carries != "" && !strings.Contains(got.Utterance, want.carries) { + fail(i, "the parked request no longer carries %q: %q", want.carries, got.Utterance) + } +} + +func checkEnd(t *testing.T, ctx context.Context, st *store.Store, h *reactiveHandler, want endState, claims []claim) { + t.Helper() + lines := make([]string, 0, len(claims)) + for _, c := range claims { + lines = append(lines, " "+c.String()) + } + trace := "\n who claimed what:\n" + strings.Join(lines, "\n") + + reminders, err := st.DueReminders(ctx, h.now().Add(14*24*time.Hour)) + if err != nil { + t.Fatalf("DueReminders: %v", err) + } + if len(reminders) != len(want.reminders) { + t.Fatalf("end state: %d reminder(s), want %d: %+v%s", len(reminders), len(want.reminders), reminders, trace) + } + for i, w := range want.reminders { + if !strings.Contains(reminders[i].Payload, w.payload) { + t.Fatalf("end state: reminder %d payload %q does not carry %q%s", i, reminders[i].Payload, w.payload, trace) + } + if w.fireAt != "" { + if got := reminders[i].FireTs.UTC().Format("2006-01-02 15:04"); got != w.fireAt { + t.Fatalf("end state: reminder %d fires at %s, want %s%s", i, got, w.fireAt, trace) + } + } + } + + facts, err := st.RecentFacts(ctx, 20) + if err != nil { + t.Fatalf("RecentFacts: %v", err) + } + if len(facts) != len(want.factKeys) { + t.Fatalf("end state: %d fact(s), want %d: %+v%s", len(facts), len(want.factKeys), facts, trace) + } + for i, key := range want.factKeys { + if facts[i].Key != key { + t.Fatalf("end state: fact %d is %q, want %q%s", i, facts[i].Key, key, trace) + } + } + + notes, err := st.RecentNotes(ctx, 20) + if err != nil { + t.Fatalf("RecentNotes: %v", err) + } + if len(notes) != want.notes { + t.Fatalf("end state: %d note(s), want %d%s", len(notes), want.notes, trace) + } + + tasks, err := st.ListTasks(ctx, store.TaskOpen) + if err != nil { + t.Fatalf("ListTasks: %v", err) + } + if len(tasks) != len(want.tasks) { + t.Fatalf("end state: %d open task(s), want %d: %+v%s", len(tasks), len(want.tasks), tasks, trace) + } + for i, text := range want.tasks { + if !strings.Contains(tasks[i].Text, text) { + t.Fatalf("end state: task %d is %q, want it to carry %q%s", i, tasks[i].Text, text, trace) + } + } +} + +func TestDialogueTraces(t *testing.T) { + for _, tr := range dialogueTraces() { + tr := tr + t.Run(tr.name, func(t *testing.T) { runTrace(t, tr) }) + } +} + +// dialogueTraces — the fixture. Order is the order the shapes were found, not a +// dependency: each trace builds its own handler and store. +func dialogueTraces() []trace { + return []trace{ + // The plain two-turn shape, and the one every other row is a deviation + // from: she asks for the time, he gives it, the reminder lands with the + // subject he said in the FIRST turn. + { + name: "reminder completed over two turns", + turns: []turn{ + {say: "напомни позвонить маме", question: dialogue.SlotTime, attempt: 1, + parked: &parkedWant{slot: dialogue.SlotTime, attempt: 1, carries: "маме"}}, + {say: "в 11:00", contains: []string{"11:00"}, notContain: []string{"?"}}, + }, + end: endState{reminders: []reminderWant{{payload: "позвонить маме", fireAt: "2026-07-31 11:00"}}}, + }, + // The same shape on the fact path, where the answer carries both halves + // of what was missing — the key and the value — in one breath. + { + name: "fact completed over two turns", + turns: []turn{ + {say: "запиши", question: dialogue.SlotKey, attempt: 1, + parked: &parkedWant{slot: dialogue.SlotKey, attempt: 1}}, + {say: "пил воду", contains: []string{"water"}}, + }, + end: endState{factKeys: []string{"water"}}, + }, + // An answer past the TTL is a new request, not an answer (V-385). She + // says the old one is gone and routes the words fresh. A bare time on + // its own carries no request, so the fresh routing lands on the canned + // reply — the point of the row is that NOTHING is created: a reminder + // here would fire with the subject of a request she had already let go. + { + name: "answer arrives after the TTL", + turns: []turn{ + {say: "напомни позвонить маме", question: dialogue.SlotTime, attempt: 1, + parked: &parkedWant{slot: dialogue.SlotTime, attempt: 1}}, + {say: "в 11:00", wait: clarifyTTL + time.Second, expired: true}, + }, + end: endState{}, + }, + // Three questions is the budget, and running out is SPOKEN: a mute + // give-up reads as "done" and he would wait for a reminder that was + // never set. The wording changes with the attempt (V-457). + { + name: "three unclear answers then the give-up line", + turns: []turn{ + {say: "напомни позвонить маме", question: dialogue.SlotTime, attempt: 1, + parked: &parkedWant{slot: dialogue.SlotTime, attempt: 1}}, + {say: "ну не знаю", question: dialogue.SlotTime, attempt: 2, + parked: &parkedWant{slot: dialogue.SlotTime, attempt: 2}}, + {say: "ну не знаю", question: dialogue.SlotTime, attempt: 3, + parked: &parkedWant{slot: dialogue.SlotTime, attempt: 3}}, + {say: "ну не знаю", contains: []string{clarifyGaveUp}, noQuestion: true}, + }, + end: endState{}, + }, + // A correction points at the previous ACTED turn (repair.go): she redoes + // it under the intent he names and says so out loud, because a + // correction he cannot see is indistinguishable from one that was + // dropped. The task she filed first stays filed — repair redoes, it does + // not retract, and V-455 decided that deliberately. + // + // The corrected-to intent has to differ from the one she used, or repair + // declines: teaching the classifier the label it already produced is + // worse than doing nothing. + { + name: "correction of the previous turn", + turns: []turn{ + {say: "добавь в задачи купить молоко", contains: []string{"купить молоко"}}, + {say: "нет, это был вопрос", contains: []string{"поняла, это вопрос"}}, + }, + end: endState{tasks: []string{"купить молоко"}}, + }, + // He walks away from his own request: a question is parked, the next + // utterance is an unrelated request of its own, and nothing follows. + // V-554's fix is what makes this row pass — the question steps aside + // rather than scoring "добавь в задачи" as the time. The reminder is + // dropped in silence and that is the decision: if he meant it he says it + // again, and a question left armed eats the turn after next. + { + name: "abandoned flow: parked, then an unrelated request", + turns: []turn{ + {say: "напомни позвонить маме", question: dialogue.SlotTime, attempt: 1, + parked: &parkedWant{slot: dialogue.SlotTime, attempt: 1}}, + {say: "добавь в задачи купить молоко", contains: []string{"купить молоко"}}, + {say: "спасибо"}, + }, + end: endState{tasks: []string{"купить молоко"}}, + }, + } +}