Merge: a confirm answer is a whole word, not a substring (#213)

V-567, severe. classifyConfirm was strings.Contains over bare stems, so
"погода" contained "да" and "покажи" contained "ок". resolveConfirm runs
before routing, so asking the weather while a confirm was parked executed the
destructive tool or the bound Hexis capability. Measured on the box before the
fix: "какая погода" ran the parked act.

Second defect found while fixing: an unrecognised utterance also disarmed the
confirm, because claim() cleared the pending slot before the verdict was read.
An utterance that is not an answer is not a cancellation either.

The yes and no words are now two closed sets in internal/lexicon, matched as
whole tokens, phrases longest-first so "не надо" is read before "нет", and
negatives before positives. The whole utterance must be answer words plus
filler, so "давай посмотрим погоду" is unknown and leaves the confirm parked.
"хорошо", "ладно" and "точно" are deliberately absent: they open a sentence
about something else as often as they answer one.

Conflict in lexicon_ru_v1.json resolved by hand: V-560's slot_value_frame and
dialogue_cancel and V-567's confirm_yes and confirm_no all belong. 22 sets,
JSON validated, lexicon, mavend, router and router/eval green with -race.

--no-verify: the pre-commit hook refuses master, and the owner asked for
straight-to-master merges for this unattended run.
This commit is contained in:
2026-08-06 01:12:04 +04:00
4 changed files with 228 additions and 21 deletions
+98 -21
View File
@@ -3,9 +3,13 @@ package main
import (
"context"
"log"
"slices"
"sort"
"strings"
"time"
"unicode"
"github.com/kami/maven/internal/lexicon"
"github.com/kami/maven/internal/phraser"
"github.com/kami/maven/internal/router"
)
@@ -57,10 +61,18 @@ func (h *reactiveHandler) park(fn string, args []string, phrase string) {
// resolveConfirm interprets an utterance as the answer to a parked destructive
// act OR a parked routine proposal. Returns (reply, true) when it consumed the
// utterance as a y/n answer; ("", false) when there's nothing pending (or the
// parked act expired), so the caller routes the utterance normally. An
// unrecognised answer cancels the pending and routes normally — a confirm that
// can't be answered clearly is safer abandoned than left armed.
// parked act expired), so the caller routes the utterance normally.
//
// An utterance that is not clearly yes or no is not an answer at all, so it is
// handed straight back and the pending stays parked until it expires (V-567).
// This resolver runs before routing and holds the most dangerous trigger on the
// box; it may only claim a turn it is certain about.
func (h *reactiveHandler) resolveConfirm(ctx context.Context, text string) (string, bool) {
verdict := classifyConfirm(text)
if verdict == confirmUnknown {
return "", false
}
h.mu.Lock()
defer h.mu.Unlock()
@@ -68,16 +80,12 @@ func (h *reactiveHandler) resolveConfirm(ctx context.Context, text string) (stri
if !r.claim() {
continue
}
// The slot is already cleared by claim(): every branch below drops the
// pending, including the unclear one — a confirm that can't be
// answered clearly is safer abandoned than left armed.
switch classifyConfirm(text) {
// The slot is already cleared by claim().
switch verdict {
case confirmYes:
return r.yes(), true
case confirmNo:
return r.no(), true
default:
return "", false
return r.no(), true
}
}
return "", false
@@ -194,23 +202,92 @@ const (
confirmNo
)
// classifyConfirm reads a short ru/en yes-or-no answer. Substring match on the
// stems so inflections/fillers ("да, давай", "нет, отмени") still land.
// confirmWords are the two closed sets, tokenized once and ordered
// longest-first so "не надо" is read before "нет" could claim any of it.
var (
confirmYesPhrases = confirmPhrases(lexicon.ConfirmYes())
confirmNoPhrases = confirmPhrases(lexicon.ConfirmNo())
)
// confirmPhrases splits each lexicon member into tokens and sorts the result
// longest-first, so a walk that tries them in order matches the longest member
// that fits.
func confirmPhrases(words []string) [][]string {
out := make([][]string, 0, len(words))
for _, w := range words {
if toks := confirmTokens(w); len(toks) > 0 {
out = append(out, toks)
}
}
sort.SliceStable(out, func(i, j int) bool { return len(out[i]) > len(out[j]) })
return out
}
// confirmTokens splits an utterance into lowercase word tokens. Punctuation and
// spacing are separators; an apostrophe is not, because "don't" is one word.
func confirmTokens(text string) []string {
return strings.FieldsFunc(strings.ToLower(text), func(r rune) bool {
if r == '\'' || r == '' {
return false
}
return !unicode.IsLetter(r) && !unicode.IsDigit(r)
})
}
// classifyConfirm reads a short ru/en yes-or-no answer to a parked confirm.
//
// The whole utterance must consist of confirmation words and filler, matched as
// whole tokens against the closed lexicon sets. Anything else is
// confirmUnknown, which leaves the confirm parked and routes the turn — see
// resolveConfirm. Both halves of that are the fix for V-567: this used to be a
// substring test over bare stems, so "погода", "дальше", "надо" and "давление"
// all read as "да", and "покажи" and "около" read as "ок". A parked destructive
// act fired on a question about the weather.
//
// Requiring the WHOLE utterance is the second half. A leading confirm word does
// not make a sentence an answer: "давай посмотрим погоду" opens a request, and
// the only safe reading of a sentence that carries its own subject is that he
// moved on. Guessing wrong here executes something; guessing wrong the other way
// asks again.
func classifyConfirm(text string) confirmVerdict {
t := strings.ToLower(strings.TrimSpace(text))
// negatives first — "не надо" contains no "да", but check no-stems before
// yes so a leading "нет" isn't shadowed.
for _, no := range []string{"нет", "не надо", "отмен", "стоп", "no", "cancel", "stop", "don't"} {
if strings.Contains(t, no) {
tokens := confirmTokens(text)
if len(tokens) == 0 {
return confirmUnknown
}
verdict := confirmUnknown
for i := 0; i < len(tokens); {
// Negatives first: "не надо" and "не хочу" open with a token that is
// not itself an answer, and a yes hit must never shadow them.
if n := matchConfirm(confirmNoPhrases, tokens[i:]); n > 0 {
return confirmNo
}
if n := matchConfirm(confirmYesPhrases, tokens[i:]); n > 0 {
verdict, i = confirmYes, i+n
continue
}
if lexicon.IsFillerParticle(tokens[i]) {
i++
continue
}
// A word that is neither an answer nor filler carries a subject of its
// own, so this utterance is not an answer to her question.
return confirmUnknown
}
for _, yes := range []string{"да", "ага", "давай", "подтвер", "конечно", "yes", "yeah", "yep", "confirm", "ок", "okay", "ok"} {
if strings.Contains(t, yes) {
return confirmYes
return verdict
}
// matchConfirm reports the length of the longest phrase matching at the head of
// tokens, or 0.
func matchConfirm(phrases [][]string, tokens []string) int {
for _, p := range phrases {
if len(p) > len(tokens) {
continue
}
if slices.Equal(p, tokens[:len(p)]) {
return len(p)
}
}
return confirmUnknown
return 0
}
// actPhrase renders "fn arg1 arg2" for the confirm prompt.
+103
View File
@@ -0,0 +1,103 @@
package main
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/kami/maven/internal/ipc"
"github.com/kami/maven/internal/tool"
)
// TestClassifyConfirmRejectsSubstrings — V-567. The old matcher tested bare
// stems with strings.Contains, so every word below answered a question she had
// asked about something else: "погода", "дальше", "надо" and "давление" carry
// "да"; "покажи", "около" and "окно" carry "ок". A parked destructive act fired
// on a question about the weather.
func TestClassifyConfirmRejectsSubstrings(t *testing.T) {
for _, text := range []string{
"погода",
"какая погода",
"что дальше",
"надо ещё",
"покажи заметки",
"около окна",
"давление",
"давай посмотрим погоду",
"не забудь купить хлеб",
"окно открыто",
"стоит ли брать зонт",
"",
} {
if got := classifyConfirm(text); got != confirmUnknown {
t.Errorf("classifyConfirm(%q) = %v, want confirmUnknown", text, got)
}
}
}
// TestClassifyConfirmAcceptsAnswers keeps every genuine answer the substring
// matcher accepted, and pins the pair the fix could most easily get wrong:
// "надо" is not an answer and "не надо" is the opposite of one.
func TestClassifyConfirmAcceptsAnswers(t *testing.T) {
yes := []string{"да", "Да!", "ага", "давай", "да, давай", "конечно", "подтверждаю", "ну да", "yes", "yeah", "ok", "okay", "confirm"}
no := []string{"нет", "Нет.", "не надо", "не нужно", "не сейчас", "отмена", "отмени", "стоп", "нет, отмени", "no", "nope", "cancel", "stop", "don't"}
for _, text := range yes {
if got := classifyConfirm(text); got != confirmYes {
t.Errorf("classifyConfirm(%q) = %v, want confirmYes", text, got)
}
}
for _, text := range no {
if got := classifyConfirm(text); got != confirmNo {
t.Errorf("classifyConfirm(%q) = %v, want confirmNo", text, got)
}
}
}
// TestUnrelatedTurnLeavesConfirmParked — the whole point of V-567. An utterance
// that is not an answer must not execute the parked act, must not consume the
// turn, and must not disarm the confirm either: the answer he has not given yet
// is still answerable until it expires.
func TestUnrelatedTurnLeavesConfirmParked(t *testing.T) {
ctx := context.Background()
st := newTestStore(t)
api := ipc.NewStoreAPI(st)
now := time.Date(2026, 8, 6, 9, 0, 0, 0, time.UTC)
h := &reactiveHandler{
api: api,
dataStore: st,
now: func() time.Time { return now },
tools: tool.NewExecutor(api, time.Second),
}
marker := filepath.Join(t.TempDir(), "destructive-tool-ran")
if err := st.EnableTool(ctx, "delete_backups", []string{"touch", marker}, true, "test", h.now()); err != nil {
t.Fatal(err)
}
h.park("delete_backups", nil, "delete_backups")
if reply, handled := h.resolveConfirm(ctx, "какая погода"); handled {
t.Fatalf("the weather question was consumed as a confirm: %q", reply)
}
if _, err := os.Stat(marker); !os.IsNotExist(err) {
t.Fatalf("the parked destructive command ran on an unrelated turn: %v", err)
}
if h.pending == nil {
t.Fatal("the confirm was disarmed by a turn that did not answer it")
}
// It is still answerable, and answering it still runs the act.
reply, handled := h.resolveConfirm(ctx, "да")
if !handled || !strings.Contains(reply, "готово") {
t.Fatalf("the still-parked confirm did not resolve: handled=%v reply=%q", handled, reply)
}
if _, err := os.Stat(marker); err != nil {
t.Fatalf("confirmed destructive command did not run: %v", err)
}
if h.pending != nil {
t.Fatal("the confirm stayed parked after being answered")
}
}