This repository was archived by the owner on May 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 199
feat(wren): add LanceDB-backed memory layer for schema and query retrieval #1494
Merged
Merged
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3083763
feat(wren): add LanceDB-backed memory module for schema and query memory
goldmedal 91c5502
feat(wren): add CLI commands for memory module
goldmedal ac1585f
docs(wren): document memory CLI commands and hybrid strategy
goldmedal f35ff7d
feat(skills): add wren-memory skill for agent memory guidance
goldmedal 21ec216
feat(skills): add cli-skills/ for standalone wren CLI agent workflows
goldmedal bd4c108
refactor(skills): consolidate cli-skills into single wren-usage skill
goldmedal 51a035f
refactor(wren): merge memory search into context command
goldmedal 8c1d867
refactor(wren): rename memory context command to fetch
goldmedal d5f80e7
chore(skills): remove wren-memory from MCP skills
goldmedal 34bcaa7
fix(wren): remove tableReference from schema describe output
goldmedal 71e5428
update lock
goldmedal c8522a5
fix(wren): catch ImportError at MemoryStore construction in tests
goldmedal 273f42e
chore(wren): add just install-memory and test-memory scripts
goldmedal ebd5699
fix(wren): address CodeRabbit review feedback on memory module
goldmedal 5da354c
fix(wren): address remaining CodeRabbit review feedback
goldmedal 886aa7e
fix(wren): address third round of CodeRabbit review feedback
goldmedal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| --- | ||
| name: wren-usage | ||
| description: "Wren Engine CLI workflow guide for AI agents. Answer data questions end-to-end using the wren CLI: gather schema context, recall past queries, write SQL through the MDL semantic layer, execute, and learn from confirmed results. Use when: agent needs to query data, connect a data source, handle errors, or manage MDL changes via the wren CLI." | ||
| license: Apache-2.0 | ||
| metadata: | ||
| author: wren-engine | ||
| version: "1.0" | ||
| --- | ||
|
|
||
| # Wren Engine CLI — Agent Workflow Guide | ||
|
|
||
| The `wren` CLI queries databases through an MDL (Model Definition Language) semantic layer. You write SQL against model names, not raw tables. The engine translates to the target dialect. | ||
|
|
||
| Two files drive everything (auto-discovered from `~/.wren/`): | ||
| - `mdl.json` — the semantic model | ||
| - `connection_info.json` — database credentials | ||
|
|
||
| For memory-specific decisions, see [references/memory.md](references/memory.md). | ||
|
|
||
| --- | ||
|
|
||
| ## Workflow 1: Answering a data question | ||
|
|
||
| ### Step 1 — Gather context | ||
|
|
||
| | Situation | Command | | ||
| |-----------|---------| | ||
| | Default | `wren memory fetch -q "<question>"` | | ||
| | Need specific model's columns | `wren memory fetch -q "..." --model <name> --threshold 0` | | ||
| | Memory not installed | Read `~/.wren/mdl.json` directly | | ||
|
|
||
| ### Step 2 — Recall past queries | ||
|
|
||
| ```bash | ||
| wren memory recall -q "<question>" --limit 3 | ||
| ``` | ||
|
|
||
| Use results as few-shot examples. Skip if empty. | ||
|
|
||
| ### Step 3 — Write and execute SQL | ||
|
|
||
| ```bash | ||
| wren --sql 'SELECT c_name, SUM(o_totalprice) FROM orders | ||
| JOIN customer ON orders.o_custkey = customer.c_custkey | ||
| GROUP BY 1 ORDER BY 2 DESC LIMIT 5' | ||
| ``` | ||
|
|
||
| **SQL rules:** | ||
| - Target MDL model names, not database tables | ||
| - Use `CAST(x AS type)`, not `::type` | ||
| - Avoid correlated subqueries — use JOINs or CTEs | ||
| - Write dialect-neutral SQL — the engine translates | ||
|
|
||
| ### Step 4 — Handle the result | ||
|
|
||
| | Outcome | Action | | ||
| |---------|--------| | ||
| | User confirms correct | `wren memory store --nl "..." --sql "..."` | | ||
| | User continues with follow-up | Store, then handle follow-up | | ||
| | User says nothing | Do NOT store | | ||
| | User says wrong | Do NOT store — fix the SQL | | ||
| | Query error | See Error recovery below | | ||
|
|
||
| --- | ||
|
|
||
| ## Workflow 2: Error recovery | ||
|
|
||
| ### "table not found" | ||
|
|
||
| 1. Verify model name: `wren memory fetch -q "<name>" --type model --threshold 0` | ||
| 2. Check MDL exists: `ls ~/.wren/mdl.json` | ||
| 3. Verify column: `wren memory fetch -q "<column>" --model <name> --threshold 0` | ||
|
|
||
| ### Connection error | ||
|
|
||
| 1. Check: `cat ~/.wren/connection_info.json` | ||
| 2. Test: `wren --sql "SELECT 1"` | ||
| 3. Valid datasource values: `postgres`, `mysql`, `bigquery`, `snowflake`, `clickhouse`, `trino`, `mssql`, `databricks`, `redshift`, `spark`, `athena`, `oracle`, `duckdb` | ||
|
|
||
| ### SQL syntax / planning error | ||
|
|
||
| 1. Isolate the layer: | ||
| - `wren dry-plan --sql "..."` — if this fails, it is an MDL-level issue | ||
| - If dry-plan succeeds but execution fails, the DB rejects the translated SQL | ||
| 2. Common fixes: replace `::` with `CAST()`, replace correlated subqueries with JOINs | ||
|
|
||
| --- | ||
|
|
||
| ## Workflow 3: Connecting a new data source | ||
|
|
||
| 1. Create `~/.wren/connection_info.json` — see [wren/docs/connections.md](../../wren/docs/connections.md) for per-connector formats | ||
| 2. Test: `wren --sql "SELECT 1"` | ||
| 3. Place or create `~/.wren/mdl.json` | ||
| 4. Index: `wren memory index` | ||
| 5. Verify: `wren --sql "SELECT * FROM <model> LIMIT 5"` | ||
|
|
||
| --- | ||
|
|
||
| ## Workflow 4: After MDL changes | ||
|
|
||
| When the MDL is updated, downstream state goes stale: | ||
|
|
||
| ```bash | ||
| # 1. Deploy updated MDL | ||
| cp updated-mdl.json ~/.wren/mdl.json | ||
|
|
||
| # 2. Re-index schema memory | ||
| wren memory index | ||
|
|
||
| # 3. Verify | ||
| wren --sql "SELECT * FROM <changed_model> LIMIT 1" | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Command decision tree | ||
|
|
||
| ``` | ||
| Get data back → wren --sql "..." | ||
| See translated SQL only → wren dry-plan --sql "..." | ||
| Validate against DB → wren dry-run --sql "..." | ||
| Schema context → wren memory fetch -q "..." | ||
| Filter by type/model → wren memory fetch -q "..." --type T --model M --threshold 0 | ||
| Store confirmed query → wren memory store --nl "..." --sql "..." | ||
| Few-shot examples → wren memory recall -q "..." | ||
| Index stats → wren memory status | ||
| Re-index after MDL change → wren memory index | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Things to avoid | ||
|
|
||
| - Do not guess model or column names — check context first | ||
| - Do not store queries the user has not confirmed — success != correctness | ||
| - Do not re-index before every query — once per MDL change | ||
| - Do not use database-specific syntax — write ANSI SQL | ||
| - Do not pass passwords via `--connection-info` if shell history is shared — use `--connection-file` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| # Wren Memory — When to index, context, store, and recall | ||
|
|
||
| This reference covers the decision logic for each memory command. The main workflow is in the parent SKILL.md. | ||
|
|
||
| --- | ||
|
|
||
| ## Schema context: `context` and `describe` | ||
|
|
||
| | Command | When to use | | ||
| |---------|-------------| | ||
| | `wren memory fetch -q "..."` | Default. Auto-selects full text (small schema) or embedding search (large schema) based on a 30K-char threshold. | | ||
| | `wren memory fetch -q "..." --type T --model M` | When you need filtering (forces search strategy on large schemas). | | ||
| | `wren memory describe` | When you want the full schema text and know it is small. | | ||
|
|
||
| The hybrid strategy works like this: | ||
| - Below 30K characters (~8K tokens): returns the entire schema as structured plain text — the LLM sees complete model-to-column relationships, join paths, and primary keys | ||
| - Above 30K characters: returns embedding search results — only the most relevant fragments | ||
|
|
||
| CJK-heavy schemas switch to search sooner (~1.5 chars per token vs 4 for English), which is the safe direction. | ||
|
|
||
| Override with `--threshold`: | ||
| ```bash | ||
| wren memory fetch -q "revenue" --threshold 50000 # raise for larger context windows | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Indexing: `wren memory index` | ||
|
|
||
| **When to index:** | ||
| - After deploying a new or updated MDL | ||
| - When `wren memory status` shows `schema_items: 0 rows` | ||
| - When `wren memory fetch` returns stale results (references deleted models) | ||
|
|
||
| **When NOT to index:** | ||
| - Before every query — indexing is expensive, do it once per MDL change | ||
| - When only using `describe` or `context` with full strategy — those read the MDL directly | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| ```bash | ||
| wren memory index --mdl ~/.wren/mdl.json | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Storing queries: `wren memory store` | ||
|
|
||
| **Store when ALL of these are true:** | ||
| 1. The SQL query executed successfully | ||
| 2. The user confirmed the result is correct, OR continued working with it (follow-up question, exported data, etc.) | ||
| 3. There is a clear natural language question that the SQL answers | ||
|
|
||
| **Do NOT store when:** | ||
| - The query failed or returned an error | ||
| - The user said the result is wrong or asked to fix it | ||
| - The query is exploratory / throwaway (`SELECT * FROM orders LIMIT 5`) | ||
| - There is no natural language question — just raw SQL | ||
| - The user explicitly asked not to store it | ||
|
|
||
| ```bash | ||
| wren memory store \ | ||
| --nl "top 5 customers by revenue last quarter" \ | ||
| --sql "SELECT c_name, SUM(o_totalprice) AS revenue ..." \ | ||
| --datasource postgres | ||
| ``` | ||
|
|
||
| The `--nl` value should be the user's original question, not a paraphrase. | ||
|
|
||
| --- | ||
|
|
||
| ## Recalling queries: `wren memory recall` | ||
|
|
||
| **When to recall:** | ||
| - Before writing SQL for a new question, especially complex ones | ||
| - When the user asks something similar to a past question | ||
|
|
||
| ```bash | ||
| wren memory recall -q "monthly revenue by category" --limit 3 | ||
| ``` | ||
|
|
||
| Use results as few-shot examples: adapt the SQL pattern to the current question. | ||
|
|
||
| --- | ||
|
|
||
| ## Full lifecycle example | ||
|
|
||
| ``` | ||
| Session start: | ||
| 1. wren memory status → if schema_items is 0: wren memory index | ||
|
|
||
| User asks a question: | ||
| 2. wren memory recall -q "<question>" --limit 3 | ||
| 3. wren memory fetch -q "<question>" | ||
| 4. Write SQL using recalled examples + schema context | ||
| 5. wren --sql "..." | ||
|
|
||
| After execution: | ||
| 6. Show results to user | ||
| 7. User confirms → wren memory store --nl "..." --sql "..." | ||
| User says wrong → fix SQL, do NOT store | ||
| User silent → do NOT store | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Housekeeping | ||
|
|
||
| ```bash | ||
| wren memory status # path, table names, row counts | ||
| wren memory reset --force # drop everything, start fresh | ||
| ``` | ||
|
|
||
| All memory commands accept `--path DIR` to override `~/.wren/memory/`. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.