rtk-ai/rtk

feat: add sqlite3 filter — compress query output, .schema dumps, and .explain plans

Open

#1,253 opened on 2026年4月12日

GitHub で見る
 (1 comment) (0 reactions) (0 assignees)Rust (2,914 forks)batch import
area:clieffort-largeenhancementhelp wantedpriority:medium

Repository metrics

Stars
 (48,085 stars)
PR merge metrics
 (平均マージ 11d 1h) (30d で 45 merged PRs)

説明

Summary

Add a rtk sqlite3 filter analogous to the existing rtk psql — strip column-padding, borders, repeated headers, and sqlite system tables from sqlite3 CLI output so query results, schema dumps, and query plans consume less LLM context.

Use case

sqlite3 is the de-facto system database on macOS (TCC, Quicklook, Launchservices, Chrome/Firefox/Slack state, Apple Mail index, many Electron apps) and the most common embedded DB for CLI/desktop tools. It's a frequent target in Claude Code sessions that need to inspect app state without installing a server DB. It is also a gap in the current registry: rtk psql exists, but rtk sqlite3 does not.

rtk discover on a 30-day window of my Claude Code history lists sqlite3 under "TOP UNHANDLED COMMANDS":

sqlite3    277    sqlite3 "/Library/Application Support/..."

277 invocations in 30 days is comparable to the volume of rtk prisma (138) and higher than many existing filters — a first-class handler is justified.

What is wasteful in raw sqlite3 output

  1. .mode column + .headers on — fixed-width padding inflates every cell; a 10-column result with long VARCHARs can multiply row size 3–5×.
  2. Repeated column headers when the caller runs multiple queries in one invocation with .headers on.
  3. .schema output — trailing commas, indentation, blank lines, and system tables (sqlite_sequence, sqlite_stat1..4, sqlite_master) that are almost never what the user asked for.
  4. .dumpBEGIN TRANSACTION; / COMMIT; boilerplate and leading PRAGMA foreign_keys=OFF; / PRAGMA defer_foreign_keys=ON; lines.
  5. .explain query plan — the "detail" column is whitespace-heavy and the "id/parent/notused" columns are rarely useful to an LLM.

Proposed behavior

Mirror rtk psql: force a compact output mode, strip borders, cap row count with a truncation notice on large result sets.

Query output

# Before (native sqlite3 with .mode column, .headers on)
$ sqlite3 db.sqlite \".headers on\" \".mode column\" \"SELECT id, name, email FROM users LIMIT 3\"
id    name           email
----  -------------  --------------------
1     Alice Johnson  alice@example.com
2     Bob Miller     bob@example.com
3     Carol Chen     carol@example.com

# After (rtk sqlite3)
$ rtk sqlite3 db.sqlite \"SELECT id, name, email FROM users LIMIT 3\"
id|name|email
1|Alice Johnson|alice@example.com
2|Bob Miller|bob@example.com
3|Carol Chen|carol@example.com

Implementation note: force .mode tabs or .mode list -separator '|' internally; then the filter is trivial because native sqlite3 already has a non-padded mode.

Schema

# Before
$ sqlite3 db.sqlite .schema
CREATE TABLE users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    email TEXT UNIQUE,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE sqlite_sequence(name,seq);
CREATE TABLE orders (
    id INTEGER PRIMARY KEY,
    user_id INTEGER REFERENCES users(id),
    total REAL
);

# After (rtk sqlite3 --schema, or auto-detect .schema subcommand)
users(id PK, name NN, email UNIQ, created_at DATETIME)
orders(id PK, user_id FK→users(id), total REAL)

Drop sqlite_sequence / sqlite_stat* by default (opt-in flag to include).

Query plan

# Before
$ sqlite3 db.sqlite \"EXPLAIN QUERY PLAN SELECT * FROM orders WHERE user_id = 42\"
id  parent  notused  detail
--  ------  -------  ------------------------------------------------
3   0       0        SEARCH orders USING INDEX idx_orders_user_id (user_id=?)

# After
SEARCH orders USING INDEX idx_orders_user_id (user_id=?)

Row cap

Like other rtk filters: on result sets > N rows (configurable via [limits] in config.toml, default 100), print the first N, drop the rest, append … (1247 more rows truncated).

Registry entry

Add to the rewrite registry so that bare sqlite3 ... rewrites to rtk sqlite3 .... Preserve shell-sensitive arguments (\"SELECT ...\" with spaces and operators) through the rewrite unchanged.

Related

  • rtk psql — exists, can serve as the template for query-output compression
  • Similar gap: mysql/mariadb CLIs are also unhandled and could share this code path (column-mode stripping is identical in spirit)
  • rtk discover currently surfaces sqlite3 under unhandled commands with 277 count in my 30-day window

コントリビューターガイド