rea
morluto
Reverse engineer anything with agents, from app behavior down to native binaries.
PROJECT TOPICS
INSTALL REFERENCE
dsh plugin --profile web add github:CC19990113/dsh-plugin-dbgraph
该命令指向仓库当前默认分支;尚无绑定当前 commit 的完整验证结果。
PROJECT README
Database structure the agent already knows, so it answers with one tool call instead of five.
A DeepSeek Harness plugin that indexes a remote
database's catalog — every table, column, comment, key, index, and a bounded sample of enum
literals — into a local SQLite index under ~/.dsh/dbgraph. The model reads that index (and a digest
of it placed directly in the system prompt) instead of spending round-trips on SHOW TABLES and
DESCRIBE, then fetches the actual values with exactly one network call.
Without an index, answering "what is 张三's password?" looks like this:
1. SHOW TABLES ← network round-trip
2. DESCRIBE sys_user ← network round-trip
3. SELECT ... WHERE status = 1 ← wrong: status is a varchar
4. SELECT ... WHERE status = 'paid' ← guess corrected
5. 2,000 rows into the transcript ← context burned
Four model round-trips, four network round-trips, two wrong guesses. The expensive part is not the database — it is the model's turns. Each one costs seconds; a 100 ms query does not.
With dbgraph:
1. dbgraph find {entity:'用户', fields:['密码'], where:{name:'张三'}}
← one row, and the answer says which table and column it used
| Where | Why | |
|---|---|---|
| Tables, columns, comments, keys, indexes, enum samples, row estimates | Local index | Zero network, usable offline, no credential needed to ask what a column means |
| Actual row values | Always the server | A cached password is not a fast password, it is a wrong one |
Nothing derived from business data is persisted. The one data-shaped field is enum_values_json, a
SELECT DISTINCT … LIMIT n sample that teaches the model whether status holds 'paid' or 1.
packages/
bundle cordis.patch.yml — mounts everything as one profile layer
service ctx.dbgraph: the vocabulary, the dialect registry, the index protocol
catalog one DatabaseDialect per engine (SQLite, MySQL; PostgreSQL/MongoDB/Redis to follow)
store ~/.dsh/dbgraph: connections.json, the index writer, the reader, the resolver
tool the two model tools, /dbgraph, and the prompt digest
crates/
dbgraph-indexer the index write path in Rust (normalize + terms + SQLite)
node:sqlite is a synchronous API. Writing ten thousand columns through it blocks the event loop for
as long as the write takes — this is the same wall dsh-plugin-codegraph and
dsh-plugin-sessiongraph hit, and both moved their kernels behind napi AsyncTask to get past it.
crates/dbgraph-indexer does the same: normalization, term generation, and the SQLite write run on
libuv's thread pool, so a rebuild never stalls the host.
Measured on one machine, same catalog, same fixed clock — 500 tables / 10,000 columns:
| writer | write | index file | longest event-loop block |
|---|---|---|---|
| JavaScript | 1992 ms | 8.46 MiB | 1992 ms (the whole write) |
Rust (AsyncTask) |
218 ms | 8.41 MiB | 33.6 ms |
9.1× faster, and the blocking cost drops 59× — which is the number that matters, because a two-second stall is two seconds of the host doing nothing else.
The two writers are checked against each other rather than trusted: given the same catalog and the
same fixed now, objects, columns, edges, terms, and search (rowids included) must match
row for row, and only meta.writer may differ. Fixture expectations are generated by running the
JavaScript writer, never hand-written, so a change on either side that is not mirrored fails the
build.
The JavaScript writer is therefore not an optimization fallback — it is the parity reference and the degradation path (no Rust toolchain, no addon, a throwing call, an unparsable answer all fall back to it, once and quietly).
node:sqlite contentless FTS5 stores nothing you can read back. Every column — UNINDEXED
ones included — returns NULL; only rowid and rank survive. The index therefore uses a
contentful table, where object_id UNINDEXED reads back verbatim. (The FTS5 column grammar is
also column-name [UNINDEXED]: a type name in front is a parse error.)unicode61 treats a run of Chinese as one token. Store 广告点击 verbatim and MATCH '广告'
finds nothing. So text holds a pre-tokenized stream — camel/digit boundaries for ASCII, bigrams
for CJK — and queries go through the same tokenizer, joined with OR (a space means phrase, not
any of these).dbgraph_index takes writer: 'auto' | 'js' | 'rust':
js — the JavaScript writer, always. Never touches the addon.rust — the native writer only; a missing artifact or a throwing call fails the run rather than
quietly producing a slower index.auto (default) — native when available; a missing artifact, a rejected call, or an unreadable
report logs once per process and falls back to JavaScript.DSH_DBGRAPH_RUST_ADDON points at an artifact explicitly: a test hook for the fallback path, and an
escape hatch for a deployment that keeps the addon outside crates/. Resolution order is: that
environment variable, then the platform package (dsh-plugin-dbgraph-indexer-<platform>, an
optionalDependency that npm installs on the matching OS), then
crates/dbgraph-indexer/artifacts/dbgraph-indexer.<platform>-<arch>.node for a working copy.
A platform package without its .node inside falls through to the next candidate rather than failing:
a checkout that was never built and a published package always behave the same way to the caller.
scripts/build-native.mjs builds the current platform into both places, and the five platform
packages are workspace members until they are published.
One acknowledged difference: the JavaScript writer counts dangling edges (an edge naming a table the
snapshot does not contain) into report.warnings; the native writer drops them silently. The stored
data is identical — only that one warning line differs.
One SQLite file per connection, replaced wholesale on every run (write to a staging file named
<index>.<pid>-<nonce>.tmp, then rename). The nonce is per run, so two runs never share the staging
file. That single decision removes incremental diffing, deletes, and half-updated indexes.
meta key/value: engine, database, schemaVersion, l1IndexedAt, l2IndexedAt
objects one row per table / view / collection / key pattern
columns ordinal, type, type_class, nullability, default, comment, pk/unique, enum sample
edges fk | index | view_ref | code_ref
terms normalized words + Chinese bigrams + alias expansion, for semantic lookup
search FTS5 (contentful — identity columns must read back) over names and comments
status.json beside the indexes carries a digest the system-prompt section reads synchronously —
a section is assembled on every model request, so opening SQLite there would be a per-step cost the
plugin refuses to pay.
| Level | Content | Where |
|---|---|---|
| L1 | object and column names, declared types | inline, seconds — usable immediately |
| L2 | comments, foreign keys, indexes, row estimates, enum samples | background job |
An L1 failure to enrich never discards the structural snapshot: an index that answers "which columns exist" is most of the value.
A run moves to a background job when the previous index covered at least 500 objects or 2,000
columns. Both numbers matter because they measure different costs: objects drive the catalog queries,
while columns drive the enum sampling — one SELECT DISTINCT … LIMIT n per string column, run with a
bounded concurrency (default 2). A 61-table warehouse with 2,300 columns is small by object count and
minutes by sampling, which is exactly the case the column threshold exists for. sampleConcurrency
retunes the parallelism and enumSampleLimit: 0 skips sampling entirely for one connection.
An index that has outlived indexStaleHours (default 6) is refreshed once per process per
connection on agent/created, silently: no host job, no conversation message, and a failure lands
in <root>/last-error.json and console.error. autoRefresh: false turns it off.
| Tool | Timeout | What it does |
|---|---|---|
dbgraph |
30 s | connections schema objects columns find sql impact refs |
dbgraph_index |
300 s | Builds or refreshes one connection (l1 / l2 / full); hands a large run to a background job |
find is the one that matters: it resolves words to a table and columns offline, then runs one
parameterized SELECT. It answers ambiguous with ranked candidates rather than guessing, and
missing_predicate rather than scanning a table because the user said "this user" without saying
which.
A where value is either a scalar (equality) or an operator object — {eq, ne, gt, gte, lt, lte, in, like, is_null} — rendered as one parameterized clause per operator, and order adds up to three
ORDER BY terms. The vocabulary is closed: a model names a comparison and can never supply a
fragment, so a range query or an IN list no longer forces a hand-written statement. Anything beyond
that (joins, grouping, window functions) is sql, on purpose.
sql takes statements: string[] — up to six — and answers them in one round-trip. Each
statement gets its own results[i], a failing statement does not cancel the rest, and one that never
ran says so with a skipped: <reason> token instead of a server message. That is what turns "the
same measure by three dimensions" from three model turns into one.
objects carries columns_preview (first eight columns, a primary key marked with *), so naming a
table and learning its shape is one call rather than two.
Measured against the production Doris host (read-only account, 2026-11-11), the same filter on a 27-column hourly table:
| Shape | Wall time |
|---|---|
local resolution + SQLite (find's offline half) |
0.3 ms |
network round trip alone (SELECT 1, warm pool) |
32 ms |
find with three named columns |
35 ms |
find reading all 27 columns |
126–902 ms (cache-dependent) |
count(*) over one partition (29.2 M rows) |
1039 ms |
The plugin's own work is free and a named-column read is already at the network floor; what costs time is width and aggregation. Each of those has a mechanism rather than a hope:
find that reads every column of a table ten columns or wider says so in its own answer, with the
measured multiplier, so the next question narrows instead of repeating the cost (readAllColumns on
the seam; WIDE_TABLE_COLUMNS sets the boundary).SELECT 1 (warmConnections, default on),
moving the ~110 ms of TCP and authentication off the first real question.row_estimate when it only needs a scale, instead of paying a
second for a count(*).sql statements run in order, one at a time, on purpose: the only cheaper option is
parallelism, and the target is a production warehouse.Sensitive columns (password, passwd, pwd, secret, token, salt, id_card, …) come back as
[REDACTED] by default. Without that, one careless question puts a password into a transcript — and
this harness persists transcripts and indexes them. sensitiveColumns: 'show' per connection turns it
off.
Credentials never live in connections.json: a connection stores a credential reference, and the
password is resolved through the host's ctx.credentials seam on each operation.
pnpm install
pnpm run build # tsc -b across the four TS packages
pnpm test # vitest run
pnpm run test:coverage # vitest run --coverage (report only — see Coverage below)
pnpm run rust:build # cargo build --release for crates/dbgraph-indexer
Tests resolve to live src/, never to built lib/ (tsconfig.base.json's paths map is the
facade), so a stale build can never load a second copy of a module singleton.
test:coverage enforces per-file 100% statements/branches/functions/lines: a new branch without a
test fails CI rather than drifting quietly.
The MySQL dialect reaches 100% without a live server, and that is not a technicality. The dialect
takes its driver as a constructor argument (createMysqlDialect(driver)), so driver-contract tests
cover the states a conforming server cannot produce on demand — an empty SELECT VERSION(), a table
that reports no columns, a driver error partway through a catalog. mysql.spec.ts still runs against
a real server whenever a DSN is set, and its job is to prove those stubs match mysql2's QueryResult
shapes rather than an invented palette.
packages/catalog/tests/mysql.spec.ts runs only when DSH_DBGRAPH_TEST_MYSQL_DSN is set, and skips
itself otherwise — it never fakes a connection. Without a server the MySQL dialect drops to ~13% in
the coverage report, which is why .github/workflows/ci.yml starts a throwaway mysql:8 service and
points the variable at it. Locally, the same thing by hand:
docker run -d --name dg-mysql -p 3306:3306 \
-e MYSQL_ALLOW_EMPTY_PASSWORD=yes -e MYSQL_DATABASE=dbgraph_test mysql:8
DSH_DBGRAPH_TEST_MYSQL_DSN=mysql://root@127.0.0.1:3306/dbgraph_test pnpm run test:coverage
pnpm run release:prepare 0.1.1
git commit -am "chore(release): 0.1.1"
git tag v0.1.1 && git push --tags # publish.yml publishes the five packages
pnpm run release:finish # regenerate the lockfile, then re-run the version gate
pnpm run check:versions is that gate. This package has no native addons, so the addon checks it
performs for the other two plugins have nothing to compare against here; the package versions, the
lockfile and the root private: true rule still apply. Both ci.yml and publish.yml run it first.
MIT
CLASSIFICATION EVIDENCE
系统优先读取 GitHub Topics,再与站内分类词典和词根规则比对。当前命中: llm-tools、deepseek-harness。