Skip to content

Commit d49a2c3

Browse files
committed
fix: total_conversation_tokens now includes DAG summary tokens
Previously only counted tokens from the messages table, missing DAG summary nodes (level > 0) that also consume context window. This caused the compaction planner to underestimate total token pressure when summaries existed without corresponding messages.
1 parent 5a68095 commit d49a2c3

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

src/db.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2633,7 +2633,11 @@ impl Database {
26332633
pub fn total_conversation_tokens(&self, conv_id: i64) -> anyhow::Result<i64> {
26342634
let conn = self.read_conn();
26352635
let total: i64 = conn.query_row(
2636-
"SELECT COALESCE(SUM(token_count), 0) FROM messages WHERE conversation_id = ?1",
2636+
"SELECT COALESCE(SUM(token_count), 0) FROM (
2637+
SELECT token_count FROM messages WHERE conversation_id = ?1
2638+
UNION ALL
2639+
SELECT token_count FROM dag_nodes WHERE conversation_id = ?1 AND level > 0 AND deleted = 0
2640+
)",
26372641
rusqlite::params![conv_id],
26382642
|row| row.get(0),
26392643
)?;

0 commit comments

Comments
 (0)