devsql 0.7.0

Code Mode across AI coding history, shell history, Git, and source code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
//! Answers the questions agents ask when they do not know the schema.
//!
//! Two shapes recur in query history. Agents ask for the schema the way they
//! would in another SQL shell (`SHOW TABLES`, `.schema commits`, `DESCRIBE
//! history`), and they guess column names from other tools (`commits.hash`,
//! `history.message`, `diff_files.file_path`). Both used to end in a bare
//! SQLite error. This module recognizes the first and explains the second.

use rusqlite::Connection;

/// One queryable table, as listed by `devsql schema`.
pub struct TableInfo {
    pub name: &'static str,
    pub source: &'static str,
    pub about: &'static str,
}

/// Every table a query can load. A name here must load through
/// `tools::load_query_tables`; the `catalog_tables_are_detected` test pins that
/// each one is at least recognized by table detection.
pub const TABLES: &[TableInfo] = &[
    TableInfo {
        name: "history",
        source: "Claude Code",
        about: "prompts you typed (display, timestamp, project)",
    },
    TableInfo {
        name: "sessions",
        source: "Claude Code",
        about: "one row per session with title, cwd, and token totals",
    },
    TableInfo {
        name: "transcripts",
        source: "Claude Code",
        about: "every conversation record (large; filter early)",
    },
    TableInfo {
        name: "tool_calls",
        source: "Claude Code",
        about: "tool invocations from transcripts",
    },
    TableInfo {
        name: "todos",
        source: "Claude Code",
        about: "todo lists",
    },
    TableInfo {
        name: "jhistory",
        source: "Codex CLI",
        about: "Codex prompts (alias: codex_history)",
    },
    TableInfo {
        name: "codex_threads",
        source: "Codex CLI",
        about: "one row per Codex conversation",
    },
    TableInfo {
        name: "codex_messages",
        source: "Codex CLI",
        about: "Codex conversation messages",
    },
    TableInfo {
        name: "codex_events",
        source: "Codex CLI",
        about: "raw Codex events",
    },
    TableInfo {
        name: "codex_tool_executions",
        source: "Codex CLI",
        about: "Codex commands with their output",
    },
    TableInfo {
        name: "codex_tool_calls",
        source: "Codex CLI",
        about: "Codex tool calls",
    },
    TableInfo {
        name: "codex_compactions",
        source: "Codex CLI",
        about: "Codex context compactions",
    },
    TableInfo {
        name: "codex_ingest_errors",
        source: "Codex CLI",
        about: "Codex records that failed to parse",
    },
    TableInfo {
        name: "commits",
        source: "Git",
        about: "commits in --repo (id, summary, message, authored_at)",
    },
    TableInfo {
        name: "diffs",
        source: "Git",
        about: "per-commit totals (files_changed, insertions, deletions)",
    },
    TableInfo {
        name: "diff_files",
        source: "Git",
        about: "per-file changes (commit_id, path, insertions, deletions)",
    },
    TableInfo {
        name: "branches",
        source: "Git",
        about: "branches in --repo",
    },
    TableInfo {
        name: "commit_parents",
        source: "Git",
        about: "commit to parent edges",
    },
    TableInfo {
        name: "tags",
        source: "Git",
        about: "tags and what they point at",
    },
    TableInfo {
        name: "refs",
        source: "Git",
        about: "every ref",
    },
    TableInfo {
        name: "stashes",
        source: "Git",
        about: "stash entries",
    },
    TableInfo {
        name: "reflog",
        source: "Git",
        about: "HEAD and branch reflog entries",
    },
    TableInfo {
        name: "blame",
        source: "Git",
        about: "line authorship for every tracked file (slow on large repos)",
    },
    TableInfo {
        name: "config",
        source: "Git",
        about: "Git config entries",
    },
    TableInfo {
        name: "remotes",
        source: "Git",
        about: "remotes and their URLs",
    },
    TableInfo {
        name: "submodules",
        source: "Git",
        about: "submodules",
    },
    TableInfo {
        name: "status",
        source: "Git",
        about: "working tree status per file",
    },
    TableInfo {
        name: "worktrees",
        source: "Git",
        about: "linked worktrees",
    },
    TableInfo {
        name: "hooks",
        source: "Git",
        about: "installed hooks",
    },
    TableInfo {
        name: "notes",
        source: "Git",
        about: "Git notes",
    },
    TableInfo {
        name: "source_files",
        source: "Code",
        about: "files in --repo",
    },
    TableInfo {
        name: "source_lines",
        source: "Code",
        about: "lines of files in --repo",
    },
    TableInfo {
        name: "symbols",
        source: "Code",
        about: "functions, types, and other symbols in --repo",
    },
    TableInfo {
        name: "shell_history",
        source: "Shell",
        about: "Atuin, zsh, and bash history",
    },
    TableInfo {
        name: "command_events",
        source: "Shell",
        about: "shell and agent commands in one stream",
    },
    TableInfo {
        name: "grok_bots",
        source: "Grok Bots",
        about: "Grok bots",
    },
    TableInfo {
        name: "grok_entries",
        source: "Grok Bots",
        about: "Grok bot entries",
    },
    TableInfo {
        name: "grok_messages",
        source: "Grok Bots",
        about: "Grok bot messages",
    },
    TableInfo {
        name: "grok_ingest_errors",
        source: "Grok Bots",
        about: "Grok records that failed to parse",
    },
    TableInfo {
        name: "macos_logs",
        source: "macOS",
        about: "Unified Log records (bounded by --log-* options)",
    },
];

/// A schema question phrased as a query.
#[derive(Debug, PartialEq, Eq)]
pub enum SchemaRequest {
    ListTables,
    Describe(String),
}

/// Recognize schema questions in the forms agents have typed: `tables`,
/// `schema`, `SHOW TABLES`, `.tables`, `.schema t`, `DESCRIBE t`, `\d t`,
/// and `information_schema.tables`.
pub fn schema_request(input: &str) -> Option<SchemaRequest> {
    let text = input.trim().trim_end_matches(';').trim();
    let lower = text.to_ascii_lowercase();
    if lower.contains("information_schema.tables") {
        return Some(SchemaRequest::ListTables);
    }
    let words: Vec<&str> = lower.split_whitespace().collect();
    let table = |t: &str| {
        Some(SchemaRequest::Describe(
            t.trim_matches(['`', '"', '\'']).to_string(),
        ))
    };
    match words.as_slice() {
        ["tables"] | [".tables"] | ["schema"] | [".schema"] | ["show", "tables"] | ["\\dt"] => {
            Some(SchemaRequest::ListTables)
        }
        [".schema", t] | ["schema", t] | ["describe", t] | ["desc", t] | ["\\d", t] => table(t),
        ["show", "columns", "from", t] => table(t),
        _ => None,
    }
}

/// Column names of a loaded table, in declaration order. Empty when the table
/// is not loaded.
pub fn table_columns(conn: &Connection, table: &str) -> Vec<(String, String)> {
    let Ok(mut stmt) = conn.prepare(&format!(
        "PRAGMA table_info(\"{}\")",
        table.replace('"', "")
    )) else {
        return Vec::new();
    };
    stmt.query_map([], |row| {
        Ok((
            row.get::<_, String>(1)?,
            row.get::<_, String>(2).unwrap_or_default(),
        ))
    })
    .map(|rows| rows.filter_map(|r| r.ok()).collect())
    .unwrap_or_default()
}

/// Names people reach for that mean the same thing. A guess in one group
/// suggests any real column in the same group.
const SYNONYMS: &[&[&str]] = &[
    &[
        "id",
        "short_id",
        "hash",
        "sha",
        "commit_hash",
        "commit_sha",
        "oid",
        "commit",
        "commit_id",
    ],
    &["summary", "subject", "title"],
    &[
        "display",
        "message",
        "prompt",
        "text",
        "content",
        "body",
        "first_user_text",
    ],
    &[
        "project",
        "cwd",
        "repo",
        "repo_path",
        "directory",
        "dir",
        "workdir",
    ],
    &["path", "file_path", "file", "filename", "file_name"],
    &[
        "insertions",
        "additions",
        "added",
        "lines_added",
        "added_lines",
    ],
    &[
        "deletions",
        "removed",
        "lines_removed",
        "deleted_lines",
        "deleted",
    ],
    &[
        "first_timestamp",
        "started_at",
        "created_at",
        "start_time",
        "first_event_at",
    ],
    &[
        "last_timestamp",
        "last_event_at",
        "ended_at",
        "updated_at",
        "end_time",
        "finished_at",
    ],
    &[
        "timestamp",
        "ts",
        "time",
        "date",
        "authored_at",
        "committed_at",
    ],
    &["author_name", "author"],
    &["thread_id", "session_id", "conversation_id"],
];

const TABLE_SYNONYMS: &[(&str, &str)] = &[
    ("prompts", "history"),
    ("messages", "codex_messages"),
    ("conversations", "sessions"),
    ("threads", "codex_threads"),
    ("commands", "command_events"),
    ("shell", "shell_history"),
    ("files", "diff_files"),
    ("logs", "macos_logs"),
];

fn edit_distance(a: &str, b: &str) -> usize {
    let b: Vec<char> = b.chars().collect();
    let mut prev: Vec<usize> = (0..=b.len()).collect();
    for (i, ca) in a.chars().enumerate() {
        let mut cur = vec![i + 1];
        for (j, cb) in b.iter().enumerate() {
            let cost = usize::from(ca != *cb);
            cur.push((prev[j] + cost).min(prev[j + 1] + 1).min(cur[j] + 1));
        }
        prev = cur;
    }
    prev[b.len()]
}

fn resembles(guess: &str, real: &str) -> bool {
    if SYNONYMS
        .iter()
        .any(|group| group.contains(&guess) && group.contains(&real))
    {
        return true;
    }
    let close = guess.len() > 3 && edit_distance(guess, real) <= 2;
    let nested =
        guess.len() >= 4 && real.len() >= 4 && (real.contains(guess) || guess.contains(real));
    close || nested
}

fn mentioned_tables(sql: &str) -> Vec<&'static str> {
    let lower = sql.to_ascii_lowercase();
    let words: Vec<&str> = lower
        .split(|c: char| !(c.is_ascii_alphanumeric() || c == '_'))
        .collect();
    let mut tables: Vec<&'static str> = TABLES
        .iter()
        .map(|t| t.name)
        .filter(|name| words.contains(name))
        .collect();
    if words.contains(&"codex_history") && !tables.contains(&"jhistory") {
        tables.push("jhistory");
    }
    tables
}

fn captured<'a>(message: &'a str, marker: &str) -> Option<&'a str> {
    let rest = &message[message.find(marker)? + marker.len()..];
    let end = rest
        .find(|c: char| !(c.is_ascii_alphanumeric() || c == '_' || c == '.'))
        .unwrap_or(rest.len());
    Some(&rest[..end]).filter(|name| !name.is_empty())
}

/// Explain a failed query in terms of the real schema: the columns of each
/// table the query names, and the closest real name to what was guessed.
/// Returns `None` when the error is not about a name.
pub fn explain_error(conn: &Connection, sql: &str, message: &str) -> Option<String> {
    if let Some(name) = captured(message, "no such column: ") {
        let guess = name.rsplit('.').next().unwrap_or(name).to_ascii_lowercase();
        let mut suggestions = Vec::new();
        let mut listings = Vec::new();
        for table in mentioned_tables(sql) {
            let columns = table_columns(conn, table);
            if columns.is_empty() {
                continue;
            }
            for (column, _) in &columns {
                if resembles(&guess, column) {
                    suggestions.push(format!("{table}.{column}"));
                }
            }
            let names: Vec<&str> = columns.iter().map(|(c, _)| c.as_str()).collect();
            listings.push(format!("{table}({})", names.join(", ")));
        }
        if listings.is_empty() {
            return None;
        }
        let mut text = format!("No column named `{name}`.");
        if !suggestions.is_empty() {
            text.push_str(&format!(" Did you mean {}?", suggestions.join(" or ")));
        } else {
            let mentioned = mentioned_tables(sql);
            let elsewhere: Vec<&str> = TABLES
                .iter()
                .filter(|t| !mentioned.contains(&t.name) && guess.len() >= 4)
                .filter(|t| {
                    t.about
                        .split(|c: char| !c.is_ascii_alphanumeric() && c != '_')
                        .any(|w| w == guess)
                })
                .map(|t| t.name)
                .collect();
            if !elsewhere.is_empty() {
                text.push_str(&format!(" `{guess}` is in {}.", elsewhere.join(", ")));
            }
        }
        text.push_str(&format!(" Columns: {}.", listings.join("; ")));
        return Some(text);
    }
    if let Some(name) = captured(message, "no such table: ") {
        let guess = name.to_ascii_lowercase();
        let mut suggestions: Vec<&str> = TABLE_SYNONYMS
            .iter()
            .filter(|(alias, _)| *alias == guess)
            .map(|(_, table)| *table)
            .collect();
        for table in TABLES.iter().map(|t| t.name) {
            if !suggestions.contains(&table) && resembles(&guess, table) {
                suggestions.push(table);
            }
        }
        let mut text = format!("No table named `{name}`.");
        if !suggestions.is_empty() {
            text.push_str(&format!(" Did you mean {}?", suggestions.join(" or ")));
        }
        let names: Vec<&str> = TABLES.iter().map(|t| t.name).collect();
        text.push_str(&format!(" Tables: {}.", names.join(", ")));
        return Some(text);
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;

    fn conn_with(ddl: &str) -> Connection {
        let conn = Connection::open_in_memory().unwrap();
        conn.execute_batch(ddl).unwrap();
        conn
    }

    #[test]
    fn recognizes_schema_questions_from_other_sql_shells() {
        for text in [
            "tables",
            "SHOW TABLES;",
            ".tables",
            "schema",
            "\\dt",
            "SELECT table_name FROM information_schema.tables",
        ] {
            assert_eq!(
                schema_request(text),
                Some(SchemaRequest::ListTables),
                "{text}"
            );
        }
        for text in [
            ".schema commits",
            "DESCRIBE commits",
            "desc `commits`;",
            "schema commits",
            "SHOW COLUMNS FROM commits",
        ] {
            assert_eq!(
                schema_request(text),
                Some(SchemaRequest::Describe("commits".into())),
                "{text}"
            );
        }
        assert_eq!(schema_request("SELECT * FROM commits"), None);
        assert_eq!(schema_request("SELECT tables FROM x"), None);
    }

    #[test]
    fn suggests_the_real_column_for_a_guessed_one() {
        let conn = conn_with("CREATE TABLE commits (id TEXT, short_id TEXT, summary TEXT, message TEXT, authored_at TEXT);");
        let text = explain_error(
            &conn,
            "SELECT hash FROM commits",
            "SQL error: no such column: hash in SELECT",
        )
        .unwrap();
        assert!(
            text.contains("Did you mean commits.id or commits.short_id?"),
            "{text}"
        );
        assert!(
            text.contains("commits(id, short_id, summary, message, authored_at)"),
            "{text}"
        );

        let conn = conn_with(
            "CREATE TABLE history (rowid INTEGER, display TEXT, timestamp TEXT, project TEXT);",
        );
        let text = explain_error(&conn, "SELECT cwd FROM history", "no such column: cwd").unwrap();
        assert!(text.contains("Did you mean history.project?"), "{text}");
        let text = explain_error(
            &conn,
            "SELECT h.message FROM history h",
            "no such column: h.message",
        )
        .unwrap();
        assert!(text.contains("history.display"), "{text}");
    }

    #[test]
    fn names_the_table_that_has_a_missing_column() {
        let conn = conn_with("CREATE TABLE commits (id TEXT, summary TEXT);");
        let text = explain_error(
            &conn,
            "SELECT SUM(insertions) FROM commits",
            "no such column: insertions",
        )
        .unwrap();
        assert!(
            text.contains("`insertions` is in diffs, diff_files."),
            "{text}"
        );
    }

    #[test]
    fn lists_columns_even_without_a_close_match() {
        let conn =
            conn_with("CREATE TABLE diff_files (commit_id TEXT, path TEXT, insertions INTEGER);");
        let text =
            explain_error(&conn, "SELECT zzz FROM diff_files", "no such column: zzz").unwrap();
        assert!(!text.contains("Did you mean"), "{text}");
        assert!(
            text.contains("diff_files(commit_id, path, insertions)"),
            "{text}"
        );
    }

    #[test]
    fn suggests_the_real_table_for_a_guessed_one() {
        let conn = conn_with("");
        let text = explain_error(&conn, "SELECT * FROM comits", "no such table: comits").unwrap();
        assert!(text.contains("Did you mean commits"), "{text}");
    }

    #[test]
    fn leaves_other_errors_alone() {
        let conn = conn_with("");
        assert_eq!(
            explain_error(&conn, "SELEC 1", "near \"SELEC\": syntax error"),
            None
        );
    }

    #[test]
    fn catalog_tables_are_detected() {
        for table in TABLES {
            let needed = crate::engine::detect_tables(&format!("SELECT * FROM {}", table.name));
            let found = [
                needed.claude,
                needed.git,
                needed.code,
                needed.shell,
                needed.system,
                needed.grok,
            ]
            .concat();
            assert!(
                found.iter().any(|t| t == table.name),
                "{} is listed but never loaded",
                table.name
            );
        }
    }
}