@carllee1983/dbcli 1.50.0 → 1.51.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dbcli-agent",
3
- "version": "1.50.0",
3
+ "version": "1.51.1",
4
4
  "description": "Database CLI skill and command reference for AI agents.",
5
5
  "author": {
6
6
  "name": "Carl Lee",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dbcli-agent",
3
- "version": "1.50.0",
3
+ "version": "1.51.1",
4
4
  "description": "Database CLI skill and command reference for AI agents",
5
5
  "author": {
6
6
  "name": "Carl Lee",
@@ -82,6 +82,16 @@ warnings) go to stderr so stdout stays parseable — when piping JSON into a par
82
82
  use `2>/dev/null` or leave stderr alone. **Never `2>&1`**: it merges those lines back
83
83
  into stdout and the parse fails.
84
84
 
85
+ **Business-language discovery:** When a user uses a business alias, metric, recurring
86
+ term, or relationship/join intent instead of a physical table or field name, first run
87
+ `dbcli skill context --format json`. If it includes `semantic`, treat that reviewed
88
+ section as the governed vocabulary; use `dbcli semantic search <terms> --format json`
89
+ to look up a specific term. If no semantic section exists or search returns no result,
90
+ fall back to `blacklist` → `schema` mapping and tell the user that optional
91
+ `dbcli.semantic.json` can make future requests consistent. Never create, update, or
92
+ migrate that file without an explicit human request; semantic vocabulary never replaces
93
+ schema confirmation or the normal query/write safety gates.
94
+
85
95
  ## Agent Task Packs
86
96
 
87
97
  When the user asks for a database workflow ("diagnose this slow query", "audit
@@ -375,6 +385,7 @@ Full flags and edge cases: see [reference.md](reference.md) `init` section.
375
385
  | `upgrade` | n/a | Self-update from npm; 24h-cached version hints on every command. |
376
386
  | `shell` | (same as query+) | Interactive REPL. SQL engines, MongoDB, and Redis (single-line; `.no-limit on/off`). **(v1.22)** Elasticsearch opens a Kibana Dev Tools-style REPL (`<METHOD> /<path>` + optional JSON body, blank line submits). |
377
387
  | `skill` | n/a | Generate / install AI skill docs (`--install <claude\|gemini\|antigravity\|copilot\|cursor\|codex\|windsurf>`); `skill tasks list/show/plan` for Agent Task Packs; `skill context` for an LLM prompt-context payload (for injecting into another LLM, not needed for normal operation). |
388
+ | `semantic` | n/a | Validate, search, inspect drift, migrate to v2, or print the optional project-root `dbcli.semantic.json`. Give its reviewed context to an external agent, but keep provider credentials, prompts, and agent context outside dbcli. `semantic draft validate --input <file|-> [--format text\|json]` validates only the explicit untrusted `QueryDraft` offline against local semantic/schema/saved-query metadata; it returns safe hashes/references/violation codes, never executes or echoes candidate SQL. Review the original draft, then invoke `explain` or `query` separately if intended. |
378
389
  | `migrate` | admin | SQL only. **DDL; dry-run by default** — needs `--execute`. |
379
390
 
380
391
  Use root-level `dbcli --use <name> <command>` for any command; `query`, `schema`, `list`,
@@ -551,7 +562,7 @@ schema. Raw `query` / `export` invocations render a sortable table only.
551
562
  - **Health / growth:** `check --all` (huge tables skipped unless `--include-large`); consult schema `sizeCategory` before ad-hoc queries.
552
563
  - **Codegen from live DB:** `schema --format json` to drive an ORM; cross-check once with `dbcli query`.
553
564
  - **Integration truth:** `query` before → run app → `query` after. Unit-test mocks are not a substitute.
554
- - **Natural language requests** (e.g. "update order to shipped"): pick `query` vs DML, map terms → columns via `schema` (and enum values in data), respect blacklist and `sizeCategory`, **always `--dry-run` writes first**.
565
+ - **Natural language requests** (e.g. "update order to shipped"): follow **Business-language discovery** first when the request uses business terminology; then pick `query` vs DML, map terms → columns via `schema` (and enum values in data), respect blacklist and `sizeCategory`, **always `--dry-run` writes first**.
555
566
 
556
567
  ## Notes
557
568
 
@@ -2190,6 +2190,132 @@ dbcli migrate drop-enum status --execute --force
2190
2190
 
2191
2191
  **AI agent note:** Always use dry-run first (no `--execute`) to preview generated SQL. Only add `--execute` after confirming the SQL is correct. For DROP operations, both `--execute` and `--force` are required.
2192
2192
 
2193
+ ### semantic
2194
+
2195
+ Validate or print the optional, version-controlled `dbcli.semantic.json` in the
2196
+ project root. It supplies business names and descriptions to an agent, but is
2197
+ not a query language: these commands are offline, read-only, and never execute
2198
+ SQL or contact an LLM.
2199
+
2200
+ When an agent receives a business term, metric, alias, or relationship/join intent,
2201
+ it should first inspect `dbcli skill context --format json`. If that output includes
2202
+ `semantic`, use the reviewed vocabulary and use `semantic search <terms>` for a
2203
+ specific lookup. When the section is absent or search has no result, fall back to the
2204
+ blacklist-filtered schema and tell the user that `dbcli.semantic.json` is an optional
2205
+ way to make future terminology consistent. Do not create, update, or migrate the file
2206
+ without an explicit human request.
2207
+
2208
+ ```bash
2209
+ dbcli semantic validate
2210
+ dbcli semantic validate --format json
2211
+ dbcli semantic context
2212
+ dbcli semantic context --format markdown
2213
+ dbcli semantic context --file ./analytics.semantic.json
2214
+ dbcli semantic drift --format json
2215
+ dbcli semantic migrate --to 2 --format json
2216
+ dbcli semantic search purchases --kind model --format json
2217
+ dbcli semantic draft validate --input ./draft.json --format json
2218
+ external-agent | dbcli semantic draft validate --input - --format json
2219
+ ```
2220
+
2221
+ The default file has this compact contract:
2222
+
2223
+ ```json
2224
+ {
2225
+ "version": 2,
2226
+ "models": [
2227
+ {
2228
+ "name": "orders",
2229
+ "table": "orders",
2230
+ "description": "Completed purchases.",
2231
+ "aliases": ["purchases"],
2232
+ "fields": [
2233
+ { "column": "created_at", "aliases": ["order date"] },
2234
+ { "column": "customer_id" }
2235
+ ]
2236
+ },
2237
+ {
2238
+ "name": "customers",
2239
+ "table": "customers",
2240
+ "fields": [{ "column": "id" }]
2241
+ }
2242
+ ],
2243
+ "relationships": [
2244
+ {
2245
+ "name": "order-customer",
2246
+ "from": { "model": "orders", "field": "customer_id" },
2247
+ "to": { "model": "customers", "field": "id" },
2248
+ "cardinality": "many-to-one",
2249
+ "description": "Each order belongs to one customer."
2250
+ }
2251
+ ],
2252
+ "metrics": [
2253
+ { "name": "daily-revenue", "query": "@analytics/revenue" }
2254
+ ]
2255
+ }
2256
+ ```
2257
+
2258
+ Version 1 remains supported and is normalized with `relationships: []`.
2259
+ Version 2 relationships must reference a declared model and a declared field on
2260
+ that model; the field must also be visible in cached schema. Their `cardinality`
2261
+ is one of `one-to-one`, `one-to-many`, `many-to-one`, or `many-to-many`.
2262
+ `models[].table` and `models[].fields[].column` must name a visible cached
2263
+ schema object. Blacklisted tables and columns are not visible and are rejected.
2264
+ Each metric `query` must name an available saved query. Validation parses its
2265
+ local file through the normal saved-query safety checks, but never executes or
2266
+ emits SQL. The semantic file cannot contain SQL, connection data, or
2267
+ credentials. `semantic validate` reports a
2268
+ deterministic success summary (`--format text|json`); `semantic context` prints
2269
+ the validated context (`--format json|markdown`). `semantic drift` returns a
2270
+ stable `valid`, `stale`, `invalid`, or `unavailable` report and exits non-zero
2271
+ for every status except `valid`; it never connects to a database. `semantic
2272
+ migrate --to 2` prints a deterministic v2 JSON document to stdout and never
2273
+ writes the input file. An absent default file is allowed for `skill context`
2274
+ and simply omits the semantic section; a stale or invalid present file fails
2275
+ closed rather than being silently ignored.
2276
+
2277
+ `semantic search <terms...>` performs deterministic, case-insensitive matching
2278
+ over canonical names, aliases, descriptions, and governed model paths. Results
2279
+ are ranked by exact canonical name, exact alias, prefix, then description token;
2280
+ kind/name breaks ties. Use `--kind model|field|relationship|metric` and
2281
+ `--limit <1-100>` (default `20`). It returns only canonical references, matched
2282
+ terms, aliases, descriptions, and necessary model paths—never SQL bodies,
2283
+ connection data, or blacklist names. No result is an empty array / text notice
2284
+ with exit 0.
2285
+
2286
+ `semantic draft validate --input <file|-> [--format text|json]` accepts only an
2287
+ explicit untrusted `QueryDraft` JSON document from a file or stdin. First give
2288
+ the external agent reviewed `dbcli semantic context --format json` output; its
2289
+ provider credentials, prompt, and other agent context remain outside dbcli. The
2290
+ agent returns a draft using only the declared models and fields, for example:
2291
+
2292
+ ```json
2293
+ {
2294
+ "version": 1,
2295
+ "questionHash": "<sha256-of-the-original-question>",
2296
+ "candidate": { "kind": "sql", "sql": "<reviewed-read-only-sql>" },
2297
+ "semanticReferences": ["model:<model>", "field:<model>.<field>"]
2298
+ }
2299
+ ```
2300
+
2301
+ The command validates the contract, read-only single-statement SQL, canonical
2302
+ semantic references, saved-query names, and local filtered schema/blacklist
2303
+ compatibility without connecting to a database, reading query results, storing
2304
+ the draft, or calling a provider. JSON reports contain only status, hashes,
2305
+ canonical references, and safe violation codes; they never echo candidate SQL
2306
+ or protected names. Exit `0` means valid, `1` means rejected, and `2` means
2307
+ required local semantic evidence is unavailable. A valid report is not execution
2308
+ authorization: review the original draft, then separately and explicitly invoke
2309
+ `dbcli explain "<reviewed-read-only-sql>"` or `dbcli query
2310
+ "<reviewed-read-only-sql>"` when appropriate.
2311
+
2312
+ When valid, `dbcli skill context` includes the same bounded data in its JSON,
2313
+ XML, and Markdown output. To run a metric, an agent must still invoke the named
2314
+ saved query through `dbcli q`; all ordinary permissions, blacklist masking,
2315
+ limits, audit, and recovery safeguards remain in force.
2316
+
2317
+ **Permission:** n/a (local files only; no database connection).
2318
+
2193
2319
  ### skill
2194
2320
 
2195
2321
  Emit `SKILL.md` (and the companion `reference.md`) to stdout, a file, or an
@@ -2,7 +2,7 @@
2
2
  "name": "dbcli-agent",
3
3
  "displayName": "dbcli Agent",
4
4
  "description": "Database CLI skill and command reference for AI agents.",
5
- "version": "1.50.0",
5
+ "version": "1.51.1",
6
6
  "author": {
7
7
  "name": "Carl Lee",
8
8
  "url": "https://github.com/CarlLee1983"
@@ -82,6 +82,16 @@ warnings) go to stderr so stdout stays parseable — when piping JSON into a par
82
82
  use `2>/dev/null` or leave stderr alone. **Never `2>&1`**: it merges those lines back
83
83
  into stdout and the parse fails.
84
84
 
85
+ **Business-language discovery:** When a user uses a business alias, metric, recurring
86
+ term, or relationship/join intent instead of a physical table or field name, first run
87
+ `dbcli skill context --format json`. If it includes `semantic`, treat that reviewed
88
+ section as the governed vocabulary; use `dbcli semantic search <terms> --format json`
89
+ to look up a specific term. If no semantic section exists or search returns no result,
90
+ fall back to `blacklist` → `schema` mapping and tell the user that optional
91
+ `dbcli.semantic.json` can make future requests consistent. Never create, update, or
92
+ migrate that file without an explicit human request; semantic vocabulary never replaces
93
+ schema confirmation or the normal query/write safety gates.
94
+
85
95
  ## Agent Task Packs
86
96
 
87
97
  When the user asks for a database workflow ("diagnose this slow query", "audit
@@ -375,6 +385,7 @@ Full flags and edge cases: see [reference.md](reference.md) `init` section.
375
385
  | `upgrade` | n/a | Self-update from npm; 24h-cached version hints on every command. |
376
386
  | `shell` | (same as query+) | Interactive REPL. SQL engines, MongoDB, and Redis (single-line; `.no-limit on/off`). **(v1.22)** Elasticsearch opens a Kibana Dev Tools-style REPL (`<METHOD> /<path>` + optional JSON body, blank line submits). |
377
387
  | `skill` | n/a | Generate / install AI skill docs (`--install <claude\|gemini\|antigravity\|copilot\|cursor\|codex\|windsurf>`); `skill tasks list/show/plan` for Agent Task Packs; `skill context` for an LLM prompt-context payload (for injecting into another LLM, not needed for normal operation). |
388
+ | `semantic` | n/a | Validate, search, inspect drift, migrate to v2, or print the optional project-root `dbcli.semantic.json`. Give its reviewed context to an external agent, but keep provider credentials, prompts, and agent context outside dbcli. `semantic draft validate --input <file|-> [--format text\|json]` validates only the explicit untrusted `QueryDraft` offline against local semantic/schema/saved-query metadata; it returns safe hashes/references/violation codes, never executes or echoes candidate SQL. Review the original draft, then invoke `explain` or `query` separately if intended. |
378
389
  | `migrate` | admin | SQL only. **DDL; dry-run by default** — needs `--execute`. |
379
390
 
380
391
  Use root-level `dbcli --use <name> <command>` for any command; `query`, `schema`, `list`,
@@ -551,7 +562,7 @@ schema. Raw `query` / `export` invocations render a sortable table only.
551
562
  - **Health / growth:** `check --all` (huge tables skipped unless `--include-large`); consult schema `sizeCategory` before ad-hoc queries.
552
563
  - **Codegen from live DB:** `schema --format json` to drive an ORM; cross-check once with `dbcli query`.
553
564
  - **Integration truth:** `query` before → run app → `query` after. Unit-test mocks are not a substitute.
554
- - **Natural language requests** (e.g. "update order to shipped"): pick `query` vs DML, map terms → columns via `schema` (and enum values in data), respect blacklist and `sizeCategory`, **always `--dry-run` writes first**.
565
+ - **Natural language requests** (e.g. "update order to shipped"): follow **Business-language discovery** first when the request uses business terminology; then pick `query` vs DML, map terms → columns via `schema` (and enum values in data), respect blacklist and `sizeCategory`, **always `--dry-run` writes first**.
555
566
 
556
567
  ## Notes
557
568
 
@@ -2190,6 +2190,132 @@ dbcli migrate drop-enum status --execute --force
2190
2190
 
2191
2191
  **AI agent note:** Always use dry-run first (no `--execute`) to preview generated SQL. Only add `--execute` after confirming the SQL is correct. For DROP operations, both `--execute` and `--force` are required.
2192
2192
 
2193
+ ### semantic
2194
+
2195
+ Validate or print the optional, version-controlled `dbcli.semantic.json` in the
2196
+ project root. It supplies business names and descriptions to an agent, but is
2197
+ not a query language: these commands are offline, read-only, and never execute
2198
+ SQL or contact an LLM.
2199
+
2200
+ When an agent receives a business term, metric, alias, or relationship/join intent,
2201
+ it should first inspect `dbcli skill context --format json`. If that output includes
2202
+ `semantic`, use the reviewed vocabulary and use `semantic search <terms>` for a
2203
+ specific lookup. When the section is absent or search has no result, fall back to the
2204
+ blacklist-filtered schema and tell the user that `dbcli.semantic.json` is an optional
2205
+ way to make future terminology consistent. Do not create, update, or migrate the file
2206
+ without an explicit human request.
2207
+
2208
+ ```bash
2209
+ dbcli semantic validate
2210
+ dbcli semantic validate --format json
2211
+ dbcli semantic context
2212
+ dbcli semantic context --format markdown
2213
+ dbcli semantic context --file ./analytics.semantic.json
2214
+ dbcli semantic drift --format json
2215
+ dbcli semantic migrate --to 2 --format json
2216
+ dbcli semantic search purchases --kind model --format json
2217
+ dbcli semantic draft validate --input ./draft.json --format json
2218
+ external-agent | dbcli semantic draft validate --input - --format json
2219
+ ```
2220
+
2221
+ The default file has this compact contract:
2222
+
2223
+ ```json
2224
+ {
2225
+ "version": 2,
2226
+ "models": [
2227
+ {
2228
+ "name": "orders",
2229
+ "table": "orders",
2230
+ "description": "Completed purchases.",
2231
+ "aliases": ["purchases"],
2232
+ "fields": [
2233
+ { "column": "created_at", "aliases": ["order date"] },
2234
+ { "column": "customer_id" }
2235
+ ]
2236
+ },
2237
+ {
2238
+ "name": "customers",
2239
+ "table": "customers",
2240
+ "fields": [{ "column": "id" }]
2241
+ }
2242
+ ],
2243
+ "relationships": [
2244
+ {
2245
+ "name": "order-customer",
2246
+ "from": { "model": "orders", "field": "customer_id" },
2247
+ "to": { "model": "customers", "field": "id" },
2248
+ "cardinality": "many-to-one",
2249
+ "description": "Each order belongs to one customer."
2250
+ }
2251
+ ],
2252
+ "metrics": [
2253
+ { "name": "daily-revenue", "query": "@analytics/revenue" }
2254
+ ]
2255
+ }
2256
+ ```
2257
+
2258
+ Version 1 remains supported and is normalized with `relationships: []`.
2259
+ Version 2 relationships must reference a declared model and a declared field on
2260
+ that model; the field must also be visible in cached schema. Their `cardinality`
2261
+ is one of `one-to-one`, `one-to-many`, `many-to-one`, or `many-to-many`.
2262
+ `models[].table` and `models[].fields[].column` must name a visible cached
2263
+ schema object. Blacklisted tables and columns are not visible and are rejected.
2264
+ Each metric `query` must name an available saved query. Validation parses its
2265
+ local file through the normal saved-query safety checks, but never executes or
2266
+ emits SQL. The semantic file cannot contain SQL, connection data, or
2267
+ credentials. `semantic validate` reports a
2268
+ deterministic success summary (`--format text|json`); `semantic context` prints
2269
+ the validated context (`--format json|markdown`). `semantic drift` returns a
2270
+ stable `valid`, `stale`, `invalid`, or `unavailable` report and exits non-zero
2271
+ for every status except `valid`; it never connects to a database. `semantic
2272
+ migrate --to 2` prints a deterministic v2 JSON document to stdout and never
2273
+ writes the input file. An absent default file is allowed for `skill context`
2274
+ and simply omits the semantic section; a stale or invalid present file fails
2275
+ closed rather than being silently ignored.
2276
+
2277
+ `semantic search <terms...>` performs deterministic, case-insensitive matching
2278
+ over canonical names, aliases, descriptions, and governed model paths. Results
2279
+ are ranked by exact canonical name, exact alias, prefix, then description token;
2280
+ kind/name breaks ties. Use `--kind model|field|relationship|metric` and
2281
+ `--limit <1-100>` (default `20`). It returns only canonical references, matched
2282
+ terms, aliases, descriptions, and necessary model paths—never SQL bodies,
2283
+ connection data, or blacklist names. No result is an empty array / text notice
2284
+ with exit 0.
2285
+
2286
+ `semantic draft validate --input <file|-> [--format text|json]` accepts only an
2287
+ explicit untrusted `QueryDraft` JSON document from a file or stdin. First give
2288
+ the external agent reviewed `dbcli semantic context --format json` output; its
2289
+ provider credentials, prompt, and other agent context remain outside dbcli. The
2290
+ agent returns a draft using only the declared models and fields, for example:
2291
+
2292
+ ```json
2293
+ {
2294
+ "version": 1,
2295
+ "questionHash": "<sha256-of-the-original-question>",
2296
+ "candidate": { "kind": "sql", "sql": "<reviewed-read-only-sql>" },
2297
+ "semanticReferences": ["model:<model>", "field:<model>.<field>"]
2298
+ }
2299
+ ```
2300
+
2301
+ The command validates the contract, read-only single-statement SQL, canonical
2302
+ semantic references, saved-query names, and local filtered schema/blacklist
2303
+ compatibility without connecting to a database, reading query results, storing
2304
+ the draft, or calling a provider. JSON reports contain only status, hashes,
2305
+ canonical references, and safe violation codes; they never echo candidate SQL
2306
+ or protected names. Exit `0` means valid, `1` means rejected, and `2` means
2307
+ required local semantic evidence is unavailable. A valid report is not execution
2308
+ authorization: review the original draft, then separately and explicitly invoke
2309
+ `dbcli explain "<reviewed-read-only-sql>"` or `dbcli query
2310
+ "<reviewed-read-only-sql>"` when appropriate.
2311
+
2312
+ When valid, `dbcli skill context` includes the same bounded data in its JSON,
2313
+ XML, and Markdown output. To run a metric, an agent must still invoke the named
2314
+ saved query through `dbcli q`; all ordinary permissions, blacklist masking,
2315
+ limits, audit, and recovery safeguards remain in force.
2316
+
2317
+ **Permission:** n/a (local files only; no database connection).
2318
+
2193
2319
  ### skill
2194
2320
 
2195
2321
  Emit `SKILL.md` (and the companion `reference.md`) to stdout, a file, or an
package/CHANGELOG.md CHANGED
@@ -5,6 +5,25 @@ All notable changes to dbcli are documented here.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [1.51.1] - 2026-08-07 - Proactive semantic context discovery
9
+
10
+ ### Changed
11
+
12
+ - **Agent skills now surface semantic context before users have to discover it.** When a request uses business aliases, metrics, recurring terminology, or relationship/join intent, installed dbcli skills first check `skill context`; they use validated semantic vocabulary when available, otherwise fall back to the blacklist-filtered schema and explain that `dbcli.semantic.json` is an optional way to keep future requests consistent. Skills never create, update, or migrate the file without explicit human instruction.
13
+
14
+ ### Fixed
15
+
16
+ - **Release formatting gate.** Remove an extra closing brace from the static guides stylesheet so Prettier can parse the repository and the release gate can run.
17
+
18
+ ## [1.51.0] - 2026-08-07 - Local semantic context and offline query-draft validation
19
+
20
+ ### Added
21
+
22
+ - **Business semantic context commands.** Add the offline, read-only `dbcli semantic validate`, `context`, `search`, `drift`, and `migrate` commands for a reviewable `dbcli.semantic.json`. Semantic models, metrics, aliases, and v2 relationships are checked against the cached visible schema and saved-query names; v1 files remain supported and `migrate --to 2` writes only to stdout.
23
+ - **Deterministic governed semantic search and relationship drift checks.** `semantic search` returns only reviewed metadata, removes blacklist names from free-text results, and supports bounded result counts. `semantic drift` identifies stale, invalid, or unavailable local semantic evidence, including relationship references that no longer match declared visible fields.
24
+ - **Offline validation boundary for agent query drafts.** `dbcli semantic draft validate --input <file|->` accepts an explicit untrusted JSON draft and validates its references and read-only SQL without executing it, persisting it, or calling a provider. Reports contain hashes, canonical references, and safe violation codes rather than candidate SQL; a successful validation is explicitly not permission to execute.
25
+ - **Semantic context in agent-facing skill context.** `dbcli skill context` includes the validated semantic context when present, after blacklist filtering, so agents receive only governed schema and semantic metadata.
26
+
8
27
  ## [1.50.0] - 2026-08-06 - QueryLens proxy query analysis
9
28
 
10
29
  ### Added
package/assets/SKILL.md CHANGED
@@ -82,6 +82,16 @@ warnings) go to stderr so stdout stays parseable — when piping JSON into a par
82
82
  use `2>/dev/null` or leave stderr alone. **Never `2>&1`**: it merges those lines back
83
83
  into stdout and the parse fails.
84
84
 
85
+ **Business-language discovery:** When a user uses a business alias, metric, recurring
86
+ term, or relationship/join intent instead of a physical table or field name, first run
87
+ `dbcli skill context --format json`. If it includes `semantic`, treat that reviewed
88
+ section as the governed vocabulary; use `dbcli semantic search <terms> --format json`
89
+ to look up a specific term. If no semantic section exists or search returns no result,
90
+ fall back to `blacklist` → `schema` mapping and tell the user that optional
91
+ `dbcli.semantic.json` can make future requests consistent. Never create, update, or
92
+ migrate that file without an explicit human request; semantic vocabulary never replaces
93
+ schema confirmation or the normal query/write safety gates.
94
+
85
95
  ## Agent Task Packs
86
96
 
87
97
  When the user asks for a database workflow ("diagnose this slow query", "audit
@@ -375,6 +385,7 @@ Full flags and edge cases: see [reference.md](reference.md) `init` section.
375
385
  | `upgrade` | n/a | Self-update from npm; 24h-cached version hints on every command. |
376
386
  | `shell` | (same as query+) | Interactive REPL. SQL engines, MongoDB, and Redis (single-line; `.no-limit on/off`). **(v1.22)** Elasticsearch opens a Kibana Dev Tools-style REPL (`<METHOD> /<path>` + optional JSON body, blank line submits). |
377
387
  | `skill` | n/a | Generate / install AI skill docs (`--install <claude\|gemini\|antigravity\|copilot\|cursor\|codex\|windsurf>`); `skill tasks list/show/plan` for Agent Task Packs; `skill context` for an LLM prompt-context payload (for injecting into another LLM, not needed for normal operation). |
388
+ | `semantic` | n/a | Validate, search, inspect drift, migrate to v2, or print the optional project-root `dbcli.semantic.json`. Give its reviewed context to an external agent, but keep provider credentials, prompts, and agent context outside dbcli. `semantic draft validate --input <file|-> [--format text\|json]` validates only the explicit untrusted `QueryDraft` offline against local semantic/schema/saved-query metadata; it returns safe hashes/references/violation codes, never executes or echoes candidate SQL. Review the original draft, then invoke `explain` or `query` separately if intended. |
378
389
  | `migrate` | admin | SQL only. **DDL; dry-run by default** — needs `--execute`. |
379
390
 
380
391
  Use root-level `dbcli --use <name> <command>` for any command; `query`, `schema`, `list`,
@@ -551,7 +562,7 @@ schema. Raw `query` / `export` invocations render a sortable table only.
551
562
  - **Health / growth:** `check --all` (huge tables skipped unless `--include-large`); consult schema `sizeCategory` before ad-hoc queries.
552
563
  - **Codegen from live DB:** `schema --format json` to drive an ORM; cross-check once with `dbcli query`.
553
564
  - **Integration truth:** `query` before → run app → `query` after. Unit-test mocks are not a substitute.
554
- - **Natural language requests** (e.g. "update order to shipped"): pick `query` vs DML, map terms → columns via `schema` (and enum values in data), respect blacklist and `sizeCategory`, **always `--dry-run` writes first**.
565
+ - **Natural language requests** (e.g. "update order to shipped"): follow **Business-language discovery** first when the request uses business terminology; then pick `query` vs DML, map terms → columns via `schema` (and enum values in data), respect blacklist and `sizeCategory`, **always `--dry-run` writes first**.
555
566
 
556
567
  ## Notes
557
568
 
@@ -64,6 +64,14 @@ legacy 單檔 `.dbcli`。若要防護同一 OS 使用者的惡意 process,host
64
64
  stdout 保持可解析——把 JSON 導進 parser 時請用 `2>/dev/null` 或不要動 stderr。
65
65
  **絕對不要用 `2>&1`**:那會把那些訊息併回 stdout,解析必定失敗。
66
66
 
67
+ **業務語言探索:** 當使用者以業務別名、metric、反覆出現的術語或 relationship/join 意圖,
68
+ 而非實體 table 或 field 名稱提出需求時,先執行 `dbcli skill context --format json`。若輸出含有
69
+ `semantic`,將該已檢閱的區塊視為受治理詞彙;需查找特定術語時,用
70
+ `dbcli semantic search <terms> --format json`。若沒有 semantic 區塊,或搜尋沒有結果,就退回
71
+ `blacklist` → `schema` 對照,並告知使用者可選用的 `dbcli.semantic.json` 能讓後續需求保持一致。
72
+ 除非人類明確要求,絕不可建立、更新或 migrate 此檔案;語意詞彙不能取代 schema 確認或正常的
73
+ query/write 安全閘門。
74
+
67
75
  ## Agent Task Packs
68
76
 
69
77
  當使用者要求一個資料庫工作流(例如「診斷這個慢查詢」、「審計權限」、「審視長時間執行的操作」),**優先選用已發布的任務模板,而非憑記憶自行組合步驟。**
@@ -288,6 +296,7 @@ dbcli init --conn-name prod --env-file .env.production --use-env-refs --skip-tes
288
296
  | `upgrade` | n/a | 從 npm 自我更新;每個指令都帶 24h 快取的版本提示。 |
289
297
  | `shell` | (與 query 同) | 互動式 REPL。SQL 引擎、MongoDB 與 Redis(單行;`.no-limit on/off`)。**(v1.22)** Elasticsearch 開啟 Kibana Dev Tools 風格的 REPL(`<METHOD> /<path>` + 可選 JSON body,空白行送出)。 |
290
298
  | `skill` | n/a | 產出 / 安裝 AI skill 文件(`--install <claude\|gemini\|antigravity\|copilot\|cursor\|codex\|windsurf>`);`skill tasks list/show/plan` 提供 Agent Task Packs;`skill context` 提供 LLM 提示詞脈絡載荷(用於注入其他 LLM,正常操作不需要)。 |
299
+ | `semantic` | n/a | 驗證、搜尋、檢查漂移、遷移至 v2 或輸出可選的專案根目錄 `dbcli.semantic.json`。把已檢閱的 context 交給外部 agent,但 provider 憑證、prompt 與 agent context 都留在 dbcli 外。`semantic draft validate --input <file|-> [--format text\|json]` 只會以本機 semantic/schema/saved-query metadata 離線驗證明確提交、不受信任的 `QueryDraft`;只回傳安全的 hash/reference/violation code,絕不執行或回顯 candidate SQL。先檢閱原始 draft,若要執行再另行呼叫 `explain` 或 `query`。 |
291
300
  | `migrate` | admin | 僅 SQL。**DDL;預設 dry-run** — 需 `--execute`。 |
292
301
 
293
302
  任何指令都可使用 root 層級的 `dbcli --use <name> <command>`;`query`、`schema`、`list`、`export`、`check` 也接受指令層級的 `--use`。兩種寫法都只把本次目標切到 v2 連線,不改變預設值。`--recovery` 被 `query`、`q`、`insert`、`update`、`delete`、`export`、`schema`、`inspect`、`lint` 與 `diff --against-orm` 支援(見上方**失敗時**)。
@@ -420,7 +429,7 @@ dbcli export "SELECT * FROM orders" --format html --output orders.html
420
429
  - **健康 / 成長:** `check --all`(除非加 `--include-large`,否則略過巨大表);做 ad-hoc query 前先看 schema 的 `sizeCategory`。
421
430
  - **從活線 DB 產生程式:** `schema --format json` 餵給 ORM;再用 `dbcli query` 交叉驗證一次。
422
431
  - **整合事實:** 前 `query` → 跑應用 → 後 `query`。單元測試 mock 不能替代。
423
- - **自然語言請求**(如「把訂單更新為 shipped」):先決定要 `query` 還是 DML,透過 `schema` 把詞彙映到欄位(值用 enum 資料),尊重 blacklist 與 `sizeCategory`,**寫入永遠先 `--dry-run`**。
432
+ - **自然語言請求**(如「把訂單更新為 shipped」):若需求使用業務術語,先遵循**業務語言探索**;接著決定要 `query` 還是 DML,透過 `schema` 把詞彙映到欄位(值用 enum 資料),尊重 blacklist 與 `sizeCategory`,**寫入永遠先 `--dry-run`**。
424
433
 
425
434
  ## 備註
426
435
 
@@ -2190,6 +2190,132 @@ dbcli migrate drop-enum status --execute --force
2190
2190
 
2191
2191
  **AI agent note:** Always use dry-run first (no `--execute`) to preview generated SQL. Only add `--execute` after confirming the SQL is correct. For DROP operations, both `--execute` and `--force` are required.
2192
2192
 
2193
+ ### semantic
2194
+
2195
+ Validate or print the optional, version-controlled `dbcli.semantic.json` in the
2196
+ project root. It supplies business names and descriptions to an agent, but is
2197
+ not a query language: these commands are offline, read-only, and never execute
2198
+ SQL or contact an LLM.
2199
+
2200
+ When an agent receives a business term, metric, alias, or relationship/join intent,
2201
+ it should first inspect `dbcli skill context --format json`. If that output includes
2202
+ `semantic`, use the reviewed vocabulary and use `semantic search <terms>` for a
2203
+ specific lookup. When the section is absent or search has no result, fall back to the
2204
+ blacklist-filtered schema and tell the user that `dbcli.semantic.json` is an optional
2205
+ way to make future terminology consistent. Do not create, update, or migrate the file
2206
+ without an explicit human request.
2207
+
2208
+ ```bash
2209
+ dbcli semantic validate
2210
+ dbcli semantic validate --format json
2211
+ dbcli semantic context
2212
+ dbcli semantic context --format markdown
2213
+ dbcli semantic context --file ./analytics.semantic.json
2214
+ dbcli semantic drift --format json
2215
+ dbcli semantic migrate --to 2 --format json
2216
+ dbcli semantic search purchases --kind model --format json
2217
+ dbcli semantic draft validate --input ./draft.json --format json
2218
+ external-agent | dbcli semantic draft validate --input - --format json
2219
+ ```
2220
+
2221
+ The default file has this compact contract:
2222
+
2223
+ ```json
2224
+ {
2225
+ "version": 2,
2226
+ "models": [
2227
+ {
2228
+ "name": "orders",
2229
+ "table": "orders",
2230
+ "description": "Completed purchases.",
2231
+ "aliases": ["purchases"],
2232
+ "fields": [
2233
+ { "column": "created_at", "aliases": ["order date"] },
2234
+ { "column": "customer_id" }
2235
+ ]
2236
+ },
2237
+ {
2238
+ "name": "customers",
2239
+ "table": "customers",
2240
+ "fields": [{ "column": "id" }]
2241
+ }
2242
+ ],
2243
+ "relationships": [
2244
+ {
2245
+ "name": "order-customer",
2246
+ "from": { "model": "orders", "field": "customer_id" },
2247
+ "to": { "model": "customers", "field": "id" },
2248
+ "cardinality": "many-to-one",
2249
+ "description": "Each order belongs to one customer."
2250
+ }
2251
+ ],
2252
+ "metrics": [
2253
+ { "name": "daily-revenue", "query": "@analytics/revenue" }
2254
+ ]
2255
+ }
2256
+ ```
2257
+
2258
+ Version 1 remains supported and is normalized with `relationships: []`.
2259
+ Version 2 relationships must reference a declared model and a declared field on
2260
+ that model; the field must also be visible in cached schema. Their `cardinality`
2261
+ is one of `one-to-one`, `one-to-many`, `many-to-one`, or `many-to-many`.
2262
+ `models[].table` and `models[].fields[].column` must name a visible cached
2263
+ schema object. Blacklisted tables and columns are not visible and are rejected.
2264
+ Each metric `query` must name an available saved query. Validation parses its
2265
+ local file through the normal saved-query safety checks, but never executes or
2266
+ emits SQL. The semantic file cannot contain SQL, connection data, or
2267
+ credentials. `semantic validate` reports a
2268
+ deterministic success summary (`--format text|json`); `semantic context` prints
2269
+ the validated context (`--format json|markdown`). `semantic drift` returns a
2270
+ stable `valid`, `stale`, `invalid`, or `unavailable` report and exits non-zero
2271
+ for every status except `valid`; it never connects to a database. `semantic
2272
+ migrate --to 2` prints a deterministic v2 JSON document to stdout and never
2273
+ writes the input file. An absent default file is allowed for `skill context`
2274
+ and simply omits the semantic section; a stale or invalid present file fails
2275
+ closed rather than being silently ignored.
2276
+
2277
+ `semantic search <terms...>` performs deterministic, case-insensitive matching
2278
+ over canonical names, aliases, descriptions, and governed model paths. Results
2279
+ are ranked by exact canonical name, exact alias, prefix, then description token;
2280
+ kind/name breaks ties. Use `--kind model|field|relationship|metric` and
2281
+ `--limit <1-100>` (default `20`). It returns only canonical references, matched
2282
+ terms, aliases, descriptions, and necessary model paths—never SQL bodies,
2283
+ connection data, or blacklist names. No result is an empty array / text notice
2284
+ with exit 0.
2285
+
2286
+ `semantic draft validate --input <file|-> [--format text|json]` accepts only an
2287
+ explicit untrusted `QueryDraft` JSON document from a file or stdin. First give
2288
+ the external agent reviewed `dbcli semantic context --format json` output; its
2289
+ provider credentials, prompt, and other agent context remain outside dbcli. The
2290
+ agent returns a draft using only the declared models and fields, for example:
2291
+
2292
+ ```json
2293
+ {
2294
+ "version": 1,
2295
+ "questionHash": "<sha256-of-the-original-question>",
2296
+ "candidate": { "kind": "sql", "sql": "<reviewed-read-only-sql>" },
2297
+ "semanticReferences": ["model:<model>", "field:<model>.<field>"]
2298
+ }
2299
+ ```
2300
+
2301
+ The command validates the contract, read-only single-statement SQL, canonical
2302
+ semantic references, saved-query names, and local filtered schema/blacklist
2303
+ compatibility without connecting to a database, reading query results, storing
2304
+ the draft, or calling a provider. JSON reports contain only status, hashes,
2305
+ canonical references, and safe violation codes; they never echo candidate SQL
2306
+ or protected names. Exit `0` means valid, `1` means rejected, and `2` means
2307
+ required local semantic evidence is unavailable. A valid report is not execution
2308
+ authorization: review the original draft, then separately and explicitly invoke
2309
+ `dbcli explain "<reviewed-read-only-sql>"` or `dbcli query
2310
+ "<reviewed-read-only-sql>"` when appropriate.
2311
+
2312
+ When valid, `dbcli skill context` includes the same bounded data in its JSON,
2313
+ XML, and Markdown output. To run a metric, an agent must still invoke the named
2314
+ saved query through `dbcli q`; all ordinary permissions, blacklist masking,
2315
+ limits, audit, and recovery safeguards remain in force.
2316
+
2317
+ **Permission:** n/a (local files only; no database connection).
2318
+
2193
2319
  ### skill
2194
2320
 
2195
2321
  Emit `SKILL.md` (and the companion `reference.md`) to stdout, a file, or an