p2.3: pin chunk 0, hybrid score, keep question on retry

Question-mode selection always keeps the lead chunk, then fills
the budget by 0.7 cosine + 0.3 term overlap. prompt_version 2.
too-large embed retry no longer halves the question (index 0).
This commit is contained in:
2026-09-23 03:18:29 -07:00
parent 5dbc7e4094
commit 767c12c1af
3 changed files with 95 additions and 5 deletions
+46
View File
@@ -334,3 +334,49 @@ func TestEmbedTooLargeRetriesAndCompletes(t *testing.T) {
t.Fatalf("text %q", r.Text)
}
}
func TestHalveInputsKeepsQuestion(t *testing.T) {
in := []string{strings.Repeat("q", 100), strings.Repeat("c", 100)}
out := halveInputs(in)
if out[0] != in[0] {
t.Fatal("question halved")
}
if utf8.RuneCountInString(out[1]) != 50 {
t.Fatalf("chunk %d", utf8.RuneCountInString(out[1]))
}
}
func TestPinChunk0Infobox(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/embed", func(w http.ResponseWriter, r *http.Request) {
var body struct {
Input []string `json:"input"`
}
_ = json.NewDecoder(r.Body).Decode(&body)
var data []map[string]any
for i := range body.Input {
vec := []float64{1, 0}
if i == 1 {
vec = []float64{0, 1}
}
data = append(data, map[string]any{"index": i, "embedding": vec})
}
_ = json.NewEncoder(w).Encode(map[string]any{"data": data})
})
ts := httptest.NewServer(mux)
defer ts.Close()
e := &Engine{EmbedURL: ts.URL + "/embed", Client: ts.Client()}
lead := "Initial release: 2003\nDeveloper: Example\nLicense: MIT"
prose := strings.Repeat("The scheduler and memory manager dominate this chapter. ", 400)
cleaned := lead + "\n" + strings.Repeat(prose+"\n", 40)
if EstTokens(cleaned) <= SmallDoc {
t.Fatalf("fixture too small: %d", EstTokens(cleaned))
}
body, nUsed, nTotal, err := e.selectText(context.Background(), cleaned, "when was the initial release?")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(body, "Initial release: 2003") {
t.Fatalf("chunk 0 dropped used=%d total=%d body=%q", nUsed, nTotal, body[:min(200, len(body))])
}
}