fix: read tool calls and prose from the v2 session store (#30)
Snippet extraction and claim lint were still on the frozen v1 part table. opencode 2.0.24 moved tool calls into session_message.data $.content and stopped writing part at 2026-11-01, so every later session produced no snippet candidates and no lint evidence: 1,475 completed write/edit/shell calls are in v2 that v1 never saw. Both readers now prefer v2 and fall back to v1 only where v2 has nothing, so a migrated session is never counted twice. Shape differences handled: the tool name moved from $.tool to $.name, write/edit input carries 'path' as well as 'filePath', completed output is $.state.content[] rather than a flat $.state.output, and the shell tool is emitted as both 'bash' and 'shell'. v2 has no patch parts, so PatchHash stays empty for v2 candidates; supersession already derives from the last full write, so nothing depends on it.
This commit is contained in:
+92
-22
@@ -88,54 +88,110 @@ func LatestSession(db *sql.DB) (string, error) {
|
||||
}
|
||||
|
||||
// LoadSession reads a session's messages and their parts in order.
|
||||
//
|
||||
// opencode 2.0.24 keeps a message's prose and tool calls inside
|
||||
// session_message.data ($.content for assistant turns, $.text for user turns)
|
||||
// and stopped writing the v1 part table. v2 is read first; part rows are used
|
||||
// only for messages v2 does not describe, so a migrated session is not read
|
||||
// twice.
|
||||
func LoadSession(db *sql.DB, sessionID string) (*Session, error) {
|
||||
s := &Session{ID: sessionID}
|
||||
_ = db.QueryRow(`SELECT COALESCE(title,'') FROM session_v2 WHERE id=?`, sessionID).Scan(&s.Title)
|
||||
|
||||
rows, err := db.Query(
|
||||
`SELECT m.id, m.seq, m.type, p.data
|
||||
FROM session_message m
|
||||
JOIN part p ON p.message_id = m.id
|
||||
WHERE m.session_id = ?
|
||||
ORDER BY m.seq, p.time_created, p.id`, sessionID)
|
||||
`SELECT id, seq, type, data FROM session_message WHERE session_id=? ORDER BY seq`, sessionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
byID := make(map[string]*Message)
|
||||
var order []string
|
||||
for rows.Next() {
|
||||
var id, typ, data string
|
||||
var seq int64
|
||||
if err := rows.Scan(&id, &seq, &typ, &data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m, ok := byID[id]
|
||||
if !ok {
|
||||
m = &Message{ID: id, Seq: seq, Role: typ}
|
||||
byID[id] = m
|
||||
order = append(order, id)
|
||||
}
|
||||
appendPart(m, data)
|
||||
m := &Message{ID: id, Seq: seq, Role: typ}
|
||||
appendMessage(m, data)
|
||||
s.Messages = append(s.Messages, *m)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, id := range order {
|
||||
s.Messages = append(s.Messages, *byID[id])
|
||||
if err := appendLegacyParts(db, sessionID, s); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// appendPart decodes one part row, keeping prose and tool output apart. The
|
||||
// decode is map-based because part payloads are heterogeneous: a tool part
|
||||
// carries a typed state object, a text part carries a string.
|
||||
func appendPart(m *Message, data string) {
|
||||
var p map[string]any
|
||||
if err := json.Unmarshal([]byte(data), &p); err != nil {
|
||||
// appendMessage decodes one session_message row. Assistant turns carry a typed
|
||||
// part list at $.content; user turns carry their prose at $.text.
|
||||
func appendMessage(m *Message, data string) {
|
||||
var msg map[string]any
|
||||
if err := json.Unmarshal([]byte(data), &msg); err != nil {
|
||||
return
|
||||
}
|
||||
if parts, ok := msg["content"].([]any); ok {
|
||||
for _, p := range parts {
|
||||
if pm, ok := p.(map[string]any); ok {
|
||||
appendPart(m, pm)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(m.Text) == 0 && len(m.Outputs) == 0 {
|
||||
if t := asString(msg["text"]); t != "" {
|
||||
m.Text = append(m.Text, t)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// appendLegacyParts fills in messages that v2 left empty from the pre-migration
|
||||
// part table, when that table still exists.
|
||||
func appendLegacyParts(db *sql.DB, sessionID string, s *Session) error {
|
||||
need := make(map[string]*Message)
|
||||
for i := range s.Messages {
|
||||
if len(s.Messages[i].Text) == 0 && len(s.Messages[i].Outputs) == 0 {
|
||||
need[s.Messages[i].ID] = &s.Messages[i]
|
||||
}
|
||||
}
|
||||
if len(need) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var n int
|
||||
if err := db.QueryRow(
|
||||
`SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='part'`).Scan(&n); err != nil || n == 0 {
|
||||
return nil // no v1 table: nothing to fall back to
|
||||
}
|
||||
|
||||
rows, err := db.Query(
|
||||
`SELECT message_id, data FROM part WHERE session_id=? ORDER BY time_created, id`, sessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var id, data string
|
||||
if err := rows.Scan(&id, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
m, ok := need[id]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
var p map[string]any
|
||||
if err := json.Unmarshal([]byte(data), &p); err != nil {
|
||||
continue
|
||||
}
|
||||
appendPart(m, p)
|
||||
}
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
// appendPart decodes one part, keeping prose and tool output apart. The decode
|
||||
// is map-based because part payloads are heterogeneous: a tool part carries a
|
||||
// typed state object, a text part carries a string.
|
||||
func appendPart(m *Message, p map[string]any) {
|
||||
switch asString(p["type"]) {
|
||||
case "text":
|
||||
if t := asString(p["text"]); t != "" {
|
||||
@@ -151,9 +207,23 @@ func appendPart(m *Message, data string) {
|
||||
if status := asString(state["status"]); status != "" && status != "completed" {
|
||||
return
|
||||
}
|
||||
// v1 stored a flat output string; v2 keeps a {type,text} content list.
|
||||
if out := asString(state["output"]); out != "" {
|
||||
m.Outputs = append(m.Outputs, out)
|
||||
}
|
||||
if parts, ok := state["content"].([]any); ok {
|
||||
var b strings.Builder
|
||||
for _, raw := range parts {
|
||||
pm, ok := raw.(map[string]any)
|
||||
if !ok || asString(pm["type"]) != "text" {
|
||||
continue
|
||||
}
|
||||
b.WriteString(asString(pm["text"]))
|
||||
}
|
||||
if b.Len() > 0 {
|
||||
m.Outputs = append(m.Outputs, b.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user