Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| aa8f5b2ee2 | |||
| d7cdcb63bd | |||
| c7dadc97d9 |
@@ -0,0 +1,54 @@
|
|||||||
|
package phraser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A reply that starts a JSON object and never finishes it is a failed
|
||||||
|
// generation, not a reply. Before this, the parser returned ("", "") for these
|
||||||
|
// and every caller then shipped the raw fragment as the thing Maven said. A
|
||||||
|
// real run produced replies of literally "{" and "{\n \"".
|
||||||
|
func TestParseResponseMoodRejectsUnfinishedJSON(t *testing.T) {
|
||||||
|
for _, raw := range []string{
|
||||||
|
`{`,
|
||||||
|
"{\n \"",
|
||||||
|
`{"response": "неполн`,
|
||||||
|
`{"response": "текст", "mood":`,
|
||||||
|
} {
|
||||||
|
text, mood, err := parseResponseMood(raw)
|
||||||
|
if !errors.Is(err, errBrokenJSON) {
|
||||||
|
t.Errorf("parseResponseMood(%q) err = %v, want errBrokenJSON", raw, err)
|
||||||
|
}
|
||||||
|
if text != "" || mood != "" {
|
||||||
|
t.Errorf("parseResponseMood(%q) leaked %q/%q — a fragment must never come back as a reply", raw, text, mood)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bare prose is still fine. Small models sometimes answer without any JSON at
|
||||||
|
// all, and that reply is usable — so the new error must not swallow it.
|
||||||
|
func TestParseResponseMoodAllowsBareProse(t *testing.T) {
|
||||||
|
for _, raw := range []string{
|
||||||
|
"норм, а ты как?",
|
||||||
|
"вот что я нашла: ключ у соседа",
|
||||||
|
} {
|
||||||
|
text, mood, err := parseResponseMood(raw)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("parseResponseMood(%q) err = %v, want nil", raw, err)
|
||||||
|
}
|
||||||
|
// No JSON means no fields; the caller ships raw as-is.
|
||||||
|
if text != "" || mood != "" {
|
||||||
|
t.Errorf("parseResponseMood(%q) = %q/%q, want empty", raw, text, mood)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The measured failure: the model wants more than 400 characters and the old
|
||||||
|
// grammar cut it off mid-word. Guards the bound against being tightened back.
|
||||||
|
func TestGrammarStringBoundHasRoomForARealAnswer(t *testing.T) {
|
||||||
|
if !strings.Contains(responseGrammar, "{0,1000}") {
|
||||||
|
t.Error("grammar string bound is not 1000; 400 truncated real replies mid-word (see the comment on responseGrammar)")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,3 +31,35 @@ func TestAddressDeduplicates(t *testing.T) {
|
|||||||
t.Errorf("detail repeats the same break %d times: %q", n, res.Detail)
|
t.Errorf("detail repeats the same break %d times: %q", n, res.Detail)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The fragments a real run produced. All of them scored as non-empty replies
|
||||||
|
// before checkNonEmpty looked for letters.
|
||||||
|
func TestNonEmptyNeedsLetters(t *testing.T) {
|
||||||
|
for _, body := range []string{
|
||||||
|
"{",
|
||||||
|
"{\n \"",
|
||||||
|
"15-16",
|
||||||
|
`{"`,
|
||||||
|
" ",
|
||||||
|
"...",
|
||||||
|
} {
|
||||||
|
if got := checkNonEmpty(body); got.Pass {
|
||||||
|
t.Errorf("checkNonEmpty(%q) passed — that is not a reply", body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// And it must not start failing real replies. Latin counts as well as Cyrillic:
|
||||||
|
// answers about ssd or vpn are legitimately part English.
|
||||||
|
func TestNonEmptyAcceptsRealReplies(t *testing.T) {
|
||||||
|
for _, body := range []string{
|
||||||
|
"норм, а ты как?",
|
||||||
|
"вот что я нашла: ключ у соседа",
|
||||||
|
"ssd быстрее hdd.",
|
||||||
|
"9 минут.",
|
||||||
|
} {
|
||||||
|
if got := checkNonEmpty(body); !got.Pass {
|
||||||
|
t.Errorf("checkNonEmpty(%q) failed: %s", body, got.Detail)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -623,11 +623,24 @@ const (
|
|||||||
CheckEllipsis = "ellipsis" // she finished the sentence
|
CheckEllipsis = "ellipsis" // she finished the sentence
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// A reply needs words in it, not just characters. This check used to test for a
|
||||||
|
// non-empty string, which scored 27/27 on a run where two replies were "{" and
|
||||||
|
// "{\n \"" — punctuation passed as content. Braces, quotes, digits and spaces
|
||||||
|
// are all empty in the only sense that matters.
|
||||||
|
//
|
||||||
|
// Digits alone fail too, and that is deliberate: the same run answered "сколько
|
||||||
|
// варить яйцо вкрутую?" with "15-16". No unit, no words, and it is also the
|
||||||
|
// wrong number. Whatever that is, it is not something she said.
|
||||||
func checkNonEmpty(body string) Result {
|
func checkNonEmpty(body string) Result {
|
||||||
if strings.TrimSpace(body) == "" {
|
if strings.TrimSpace(body) == "" {
|
||||||
return Result{CheckNonEmpty, false, "empty reply"}
|
return Result{CheckNonEmpty, false, "empty reply"}
|
||||||
}
|
}
|
||||||
return Result{CheckNonEmpty, true, ""}
|
for _, r := range body {
|
||||||
|
if unicode.IsLetter(r) {
|
||||||
|
return Result{CheckNonEmpty, true, ""}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Result{CheckNonEmpty, false, fmt.Sprintf("no letters in the reply %q — punctuation or digits only", strings.TrimSpace(body))}
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkEllipsis — a reply ending in "…" or "..." is a generation that ran out of
|
// checkEllipsis — a reply ending in "…" or "..." is a generation that ran out of
|
||||||
|
|||||||
@@ -97,7 +97,10 @@ func TestGrammarStringRuleIsNotASCIIOnly(t *testing.T) {
|
|||||||
// Russian body with an escaped quote inside, hand-built to test the contract.
|
// Russian body with an escaped quote inside, hand-built to test the contract.
|
||||||
func TestGrammarShapedJSONParses(t *testing.T) {
|
func TestGrammarShapedJSONParses(t *testing.T) {
|
||||||
raw := `{"response": "он сказал \"привет\" и ушёл.\nвот так.", "mood": "confused"}`
|
raw := `{"response": "он сказал \"привет\" и ушёл.\nвот так.", "mood": "confused"}`
|
||||||
text, mood := parseResponseMood(raw)
|
text, mood, err := parseResponseMood(raw)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("grammar-shaped JSON did not parse: %v", err)
|
||||||
|
}
|
||||||
if want := "он сказал \"привет\" и ушёл.\nвот так."; text != want {
|
if want := "он сказал \"привет\" и ушёл.\nвот так."; text != want {
|
||||||
t.Errorf("response = %q, want %q", text, want)
|
t.Errorf("response = %q, want %q", text, want)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -190,7 +190,12 @@ func (p *LLMPhraser) PhraseNudge(ctx context.Context, c loop.Candidate) (deliver
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return delivery.PhrasedNudge{}, err
|
return delivery.PhrasedNudge{}, err
|
||||||
}
|
}
|
||||||
body, mood := parseResponseMood(resp)
|
body, mood, perr := parseResponseMood(resp)
|
||||||
|
if perr != nil {
|
||||||
|
// Truncated JSON. Not a nudge — use the plain Russian fallback.
|
||||||
|
log.Printf("phraser: PhraseNudge: %v", perr)
|
||||||
|
body, mood = "", ""
|
||||||
|
}
|
||||||
if body == "" {
|
if body == "" {
|
||||||
// fallback: try old body/summary format
|
// fallback: try old body/summary format
|
||||||
body, _ = parsePhrase(resp)
|
body, _ = parsePhrase(resp)
|
||||||
@@ -216,11 +221,16 @@ func (p *LLMPhraser) PhraseQuery(ctx context.Context, utterance string, notes []
|
|||||||
// prompt is the single tested source in router.KnowledgePrompt.
|
// prompt is the single tested source in router.KnowledgePrompt.
|
||||||
sys := persona.Prepend(p.cfg.ContextBlock, router.KnowledgePrompt())
|
sys := persona.Prepend(p.cfg.ContextBlock, router.KnowledgePrompt())
|
||||||
prompt := fmt.Sprintf("Пользователь спрашивает: \"%s\".", utterance)
|
prompt := fmt.Sprintf("Пользователь спрашивает: \"%s\".", utterance)
|
||||||
resp, err := p.chatWithSystem(ctx, sys, prompt, 256)
|
resp, err := p.chatWithSystem(ctx, sys, prompt, 768)
|
||||||
if err != nil || resp == "" {
|
if err != nil || resp == "" {
|
||||||
return "не знаю.", nil
|
return "не знаю.", nil
|
||||||
}
|
}
|
||||||
if text, _ := parseResponseMood(resp); text != "" {
|
text, _, perr := parseResponseMood(resp)
|
||||||
|
if perr != nil {
|
||||||
|
log.Printf("phraser: PhraseQuery: %v", perr)
|
||||||
|
return "не знаю.", nil
|
||||||
|
}
|
||||||
|
if text != "" {
|
||||||
return text, nil
|
return text, nil
|
||||||
}
|
}
|
||||||
return resp, nil
|
return resp, nil
|
||||||
@@ -230,17 +240,22 @@ func (p *LLMPhraser) PhraseQuery(ctx context.Context, utterance string, notes []
|
|||||||
}
|
}
|
||||||
sys := p.querySystemPrompt()
|
sys := p.querySystemPrompt()
|
||||||
prompt := fmt.Sprintf(
|
prompt := fmt.Sprintf(
|
||||||
`The user asks: "%s". Your notes matching the query contain: "%s". Answer them naturally and briefly. If the notes don't answer the question, say so.`,
|
`Он спрашивает: "%s". В твоих заметках по этому вопросу написано: "%s". Ответь ему коротко и своими словами. Если в заметках ответа нет — так и скажи.`,
|
||||||
utterance, strings.Join(notes, `"; "`),
|
utterance, strings.Join(notes, `"; "`),
|
||||||
)
|
)
|
||||||
resp, err := p.chatWithSystem(ctx, sys, prompt, 256)
|
resp, err := p.chatWithSystem(ctx, sys, prompt, 768)
|
||||||
if err != nil {
|
text, _, perr := parseResponseMood(resp)
|
||||||
|
if err != nil || perr != nil {
|
||||||
|
// Read the notes out rather than ship a broken fragment.
|
||||||
|
if perr != nil {
|
||||||
|
log.Printf("phraser: PhraseQuery: %v", perr)
|
||||||
|
}
|
||||||
if len(notes) == 1 {
|
if len(notes) == 1 {
|
||||||
return "вот что я нашла: " + notes[0], nil
|
return "вот что я нашла: " + notes[0], nil
|
||||||
}
|
}
|
||||||
return "вот что я нашла: " + strings.Join(notes, "; "), nil
|
return "вот что я нашла: " + strings.Join(notes, "; "), nil
|
||||||
}
|
}
|
||||||
if text, _ := parseResponseMood(resp); text != "" {
|
if text != "" {
|
||||||
return text, nil
|
return text, nil
|
||||||
}
|
}
|
||||||
return resp, nil
|
return resp, nil
|
||||||
@@ -263,12 +278,17 @@ func (p *LLMPhraser) PhraseChat(ctx context.Context, utterance string, history [
|
|||||||
combined += utterance
|
combined += utterance
|
||||||
msgs = append(msgs, chatMsg{Role: "user", Content: strings.TrimSpace(combined)})
|
msgs = append(msgs, chatMsg{Role: "user", Content: strings.TrimSpace(combined)})
|
||||||
|
|
||||||
resp, err := p.chatWithMessages(ctx, msgs, 512)
|
resp, err := p.chatWithMessages(ctx, msgs, 768)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("phraser: PhraseChat: %v", err)
|
log.Printf("phraser: PhraseChat: %v", err)
|
||||||
return "поговорили.", nil
|
return "поговорили.", nil
|
||||||
}
|
}
|
||||||
if text, _ := parseResponseMood(resp); text != "" {
|
text, _, perr := parseResponseMood(resp)
|
||||||
|
if perr != nil {
|
||||||
|
log.Printf("phraser: PhraseChat: %v", perr)
|
||||||
|
return "поговорили.", nil
|
||||||
|
}
|
||||||
|
if text != "" {
|
||||||
return text, nil
|
return text, nil
|
||||||
}
|
}
|
||||||
// fallback: plain text without JSON
|
// fallback: plain text without JSON
|
||||||
@@ -281,11 +301,13 @@ func (p *LLMPhraser) PhraseChat(ctx context.Context, utterance string, history [
|
|||||||
// chatSystemPrompt returns the system prompt for conversational chat.
|
// chatSystemPrompt returns the system prompt for conversational chat.
|
||||||
// Prepends the shared context block when the phraser has one.
|
// Prepends the shared context block when the phraser has one.
|
||||||
func chatSystemPrompt(block func() string) string {
|
func chatSystemPrompt(block func() string) string {
|
||||||
base := `You are maven, a self-hosted personal assistant. You're talking with your owner.
|
// No self-introduction here: the persona block prepended one line above
|
||||||
Keep replies brief (1-3 sentences) and natural. You're helpful, curious, and a little warm.
|
// already says who she is, same as router.KnowledgePrompt.
|
||||||
Respond in the user's language (Russian or English, matching their last message).
|
base := `Ты разговариваешь с хозяином. О себе говоришь в женском роде ("я подумала", "я рада"). Он мужчина: обращайся к нему на "ты", в мужском роде ("ты сказал", "ты забыл"). Никогда не "вы"/"ваш" и никогда "он"/"его" — ты говоришь ему, а не о нём.
|
||||||
Never roleplay emotions you don't have, but stay friendly.
|
|
||||||
Respond ONLY with valid JSON: {"response": "...", "mood": "neutral"}. "response" is your reply text; "mood" reflects your tone (neutral/happy/thinking/tired/confused).`
|
Отвечай по-русски, коротко: одна-три фразы, живым языком. Ты доброжелательная, тебе интересно, но чувства не изображай.
|
||||||
|
|
||||||
|
Отвечай ТОЛЬКО одним объектом JSON: {"response": "...", "mood": "neutral"}. В "response" — твой ответ. В "mood" — ровно одно из: neutral, happy, thinking, tired, confused.`
|
||||||
return persona.Prepend(block, base)
|
return persona.Prepend(block, base)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -349,7 +371,12 @@ func (p *LLMPhraser) PhraseReminder(ctx context.Context, d loop.ReminderDecision
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return delivery.PhrasedReminder{}, err
|
return delivery.PhrasedReminder{}, err
|
||||||
}
|
}
|
||||||
body, mood := parseResponseMood(resp)
|
body, mood, perr := parseResponseMood(resp)
|
||||||
|
if perr != nil {
|
||||||
|
// Truncated JSON. Fall through to the reminder's own text.
|
||||||
|
log.Printf("phraser: PhraseReminder: %v", perr)
|
||||||
|
body, mood = "", ""
|
||||||
|
}
|
||||||
if body == "" {
|
if body == "" {
|
||||||
// fallback: try old body/summary format
|
// fallback: try old body/summary format
|
||||||
body, _ = parsePhrase(resp)
|
body, _ = parsePhrase(resp)
|
||||||
@@ -394,10 +421,16 @@ type chatReq struct {
|
|||||||
// Russian, so an ASCII-only rule would make every reply empty. The escape rule
|
// Russian, so an ASCII-only rule would make every reply empty. The escape rule
|
||||||
// is what lets the model close a string it opened with a quote inside. Length
|
// is what lets the model close a string it opened with a quote inside. Length
|
||||||
// is bounded so a repetition loop truncates the field, not the JSON object.
|
// is bounded so a repetition loop truncates the field, not the JSON object.
|
||||||
|
//
|
||||||
|
// That bound was 400 and 400 was too tight. Measured against Qwen3.5-0.8B: on
|
||||||
|
// "почему гром слышно позже молнии?" the reply came back exactly 400 characters
|
||||||
|
// long, cut mid-word ("Нужно записать и,"), at every token cap from 256 to 2048.
|
||||||
|
// So the token cap was never what stopped it — this rule was. 1000 characters is
|
||||||
|
// roughly six Russian sentences, still short enough to stop a repetition loop.
|
||||||
const responseGrammar = `
|
const responseGrammar = `
|
||||||
root ::= "{" ws "\"response\"" ws ":" ws string ws "," ws "\"mood\"" ws ":" ws mood ws "}"
|
root ::= "{" ws "\"response\"" ws ":" ws string ws "," ws "\"mood\"" ws ":" ws mood ws "}"
|
||||||
mood ::= "\"neutral\"" | "\"happy\"" | "\"thinking\"" | "\"tired\"" | "\"confused\""
|
mood ::= "\"neutral\"" | "\"happy\"" | "\"thinking\"" | "\"tired\"" | "\"confused\""
|
||||||
string ::= "\"" ([^"\\] | "\\" ["\\/bfnrt]){0,400} "\""
|
string ::= "\"" ([^"\\] | "\\" ["\\/bfnrt]){0,1000} "\""
|
||||||
ws ::= [ \t\n]*
|
ws ::= [ \t\n]*
|
||||||
`
|
`
|
||||||
|
|
||||||
@@ -507,7 +540,9 @@ func (p *LLMPhraser) systemPrompt() string {
|
|||||||
// querySystemPrompt returns the system prompt for PhraseQuery (notes + general
|
// querySystemPrompt returns the system prompt for PhraseQuery (notes + general
|
||||||
// knowledge). Prepends the configured persona when set.
|
// knowledge). Prepends the configured persona when set.
|
||||||
func (p *LLMPhraser) querySystemPrompt() string {
|
func (p *LLMPhraser) querySystemPrompt() string {
|
||||||
base := "You are maven, a self-hosted personal assistant answering from your notes. Answer briefly and naturally in Russian starting with \"вот что я нашла: \". Respond ONLY with valid JSON: {\"response\": \"...\", \"mood\": \"neutral\"}."
|
// No self-introduction here: the persona block prepended one line above
|
||||||
|
// already says who she is, same as router.KnowledgePrompt.
|
||||||
|
base := "Ты отвечаешь ему по своим заметкам. Отвечай по-русски, коротко и своими словами, начинай с \"вот что я нашла: \". О себе — в женском роде (\"нашла\", \"записала\"). Он мужчина, обращайся к нему на \"ты\". Respond ONLY with valid JSON: {\"response\": \"...\", \"mood\": \"neutral\"}."
|
||||||
return persona.Prepend(p.cfg.ContextBlock, base)
|
return persona.Prepend(p.cfg.ContextBlock, base)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -629,21 +664,39 @@ type responseMood struct {
|
|||||||
Mood string `json:"mood"`
|
Mood string `json:"mood"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// errBrokenJSON — the model started a JSON object and never finished it.
|
||||||
|
// That is a failed generation, not a reply. Callers must use their fallback.
|
||||||
|
var errBrokenJSON = fmt.Errorf("phraser: model output starts as JSON but does not parse")
|
||||||
|
|
||||||
// parseResponseMood extracts {"response","mood"} from LLM output, tolerant
|
// parseResponseMood extracts {"response","mood"} from LLM output, tolerant
|
||||||
// of thinking tokens and extra text before/after the JSON block. Returns
|
// of thinking tokens and extra text before/after the JSON block.
|
||||||
// ("", "") when no valid JSON is found.
|
//
|
||||||
func parseResponseMood(raw string) (response, mood string) {
|
// Three outcomes:
|
||||||
|
// - parsed fine → the fields, nil error.
|
||||||
|
// - output never looked like JSON → ("", "", nil). The caller may ship it
|
||||||
|
// as-is; small models sometimes answer in bare prose and that is fine.
|
||||||
|
// - output starts with "{" but does not parse → errBrokenJSON. The grammar
|
||||||
|
// guarantees a valid *prefix*, so a generation that hits the token cap
|
||||||
|
// mid-object comes back as a fragment like `{` or `{\n "`. Shipping that
|
||||||
|
// as a reply is the bug this error exists to stop.
|
||||||
|
func parseResponseMood(raw string) (response, mood string, err error) {
|
||||||
cleaned := strings.TrimSpace(raw)
|
cleaned := strings.TrimSpace(raw)
|
||||||
start := strings.Index(cleaned, "{")
|
start := strings.Index(cleaned, "{")
|
||||||
end := strings.LastIndex(cleaned, "}")
|
end := strings.LastIndex(cleaned, "}")
|
||||||
if start < 0 || end < 0 || end <= start {
|
if start < 0 || end < 0 || end <= start {
|
||||||
return "", ""
|
if strings.HasPrefix(cleaned, "{") {
|
||||||
|
return "", "", errBrokenJSON
|
||||||
|
}
|
||||||
|
return "", "", nil
|
||||||
}
|
}
|
||||||
var parsed responseMood
|
var parsed responseMood
|
||||||
if err := json.Unmarshal([]byte(cleaned[start:end+1]), &parsed); err != nil {
|
if e := json.Unmarshal([]byte(cleaned[start:end+1]), &parsed); e != nil {
|
||||||
return "", ""
|
if strings.HasPrefix(cleaned, "{") {
|
||||||
|
return "", "", errBrokenJSON
|
||||||
|
}
|
||||||
|
return "", "", nil
|
||||||
}
|
}
|
||||||
return parsed.Response, parsed.Mood
|
return parsed.Response, parsed.Mood, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parsePhrase(raw string) (body, summary string) {
|
func parsePhrase(raw string) (body, summary string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user