Skip to content

Commit

Permalink
brain/sqlbrain: preserve iteration across context drops
Browse files Browse the repository at this point in the history
When we drop context, we model the result as appending the new search
to the existing sequence, including preserving the skip sequence.
This way, we remain uniform across the entire chain space.

This change also fixes a bug where we were using the same skip sequence
after dropping context, which would lead to almost always not selecting
a term and thereby terminating generation early.
  • Loading branch information
zephyrtronium committed Nov 19, 2024
1 parent e92f7de commit 7d89fd8
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions brain/sqlbrain/speak.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ func next(conn *sqlite.Conn, tag string, b []byte, prompt []string) ([]byte, str
}
st.SetText(":tag", tag)
w := make([]byte, 0, 32)
var d []byte
var skip brain.Skip
var (
d []byte
skip brain.Skip
t uint64
)
for {
var seen uint64
b = prefix(b[:0], prompt)
Expand All @@ -67,6 +70,17 @@ func next(conn *sqlite.Conn, tag string, b []byte, prompt []string) ([]byte, str
st.SetBytes(":upper", d)
sel:
for {
for t > 0 {
ok, err := st.Step()
if err != nil {
return b[:0], "", len(prompt), fmt.Errorf("couldn't step term selection: %w", err)
}
if !ok {
break sel
}
seen++
t--
}
ok, err := st.Step()
if err != nil {
return b[:0], "", len(prompt), fmt.Errorf("couldn't step term selection: %w", err)
Expand All @@ -80,16 +94,7 @@ func next(conn *sqlite.Conn, tag string, b []byte, prompt []string) ([]byte, str
w = make([]byte, n)
}
w = w[:st.ColumnBytes(1, w[:n])]
for range skip.N(rand.Uint64(), rand.Uint64()) {
ok, err := st.Step()
if err != nil {
return b[:0], "", len(prompt), fmt.Errorf("couldn't step term selection: %w", err)
}
if !ok {
break sel
}
seen++
}
t = skip.N(rand.Uint64(), rand.Uint64())
}
// Try to lose context.
// We want to do so when we have a long context and almost no options,
Expand Down

0 comments on commit 7d89fd8

Please sign in to comment.