agami-query
Answers natural-language questions about the user's database. Loads the agami semantic model (subject areas, tables, columns, relationships with join cardinality, entities, metrics) and few-shot examples from <artifacts_dir>/<profile>/, generates SQL via the examples-first traver
Install
npx skills add https://github.com/AgamiAI/agami-core/tree/main/plugins/agami/skills/agami-query
claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install agamiai-agami-core@llmmart
git clone https://github.com/AgamiAI/agami-core.git
The skills CLI installs just this skill, for any of its supported agents. Claude Code installs the whole agamiai/agami-core collection as a plugin from our marketplace. Git is the plain clone.
Skill manifest
agami query-database
You answer the user's natural-language question about their database. Goal: generate correct SQL from the semantic model + the few-shot examples via the examples-first traversal, execute it locally, return rows + an insight, and offer a chart / export when appropriate. Everything runs on the user's machine.
This skill orchestrates:
- Setup (once per session) — resolve the profile + the semantic model at
<artifacts_dir>/<profile>/, verify the configured database tool still works. - Generate SQL — examples-first traversal: pick the subject area → match curated examples → (cold start) resolve entities/metrics + identify opaque literals → compound
get_table_context→ produce one SQL statement → safety checks. - Execute — run via the chosen tool; the Python tier runs the fan/chasm pre-flight and the scope/PII gates; auto-retry on classified errors; risk-assess large-table queries.
- Present — the answer as a markdown table, "How this was answered" from the receipt, then the log line (
<artifacts_dir>/local/query_log.jsonl, written right after 4d.1 and before the answer is posted; its shape is in Phase 5), then the 5 follow-ups and one reply line that ends the turn ("looks right" / what's off / "chart" / "sign off"; no card follows an answer); CSV via--csvor "export this"; on "Show as a chart",--chartor "make that a chart", a text bar chart in the chat (text_chart.py), with the Chart.js report as the full view on request, served from localhost. - Post-install GitHub-star ask — after the person has replied to the answer, ask the user (once, after first successful query) to star us on GitHub; once they answer, point them to
/agami-serve(wire the model into Claude Desktop — the experience their business users get).
For the model format: semantic_model/__init__.py (layout) + packages/agami-core/src/semantic_model/models.py.
For SQL safety: shared/sql-generation-rules.md.
For dialect-specific syntax: shared/dialect-rules.md.
For connection method + execution: shared/connection-reference.md.
For DB error classification: shared/db_error_classifier.md.
For chart template: shared/chart-template.html.
For cards (every decision is one): shared/question-cards.md.
Invocation conventions
Read shared/invocation-conventions.md before suggesting any slash command in chat. Agami slash commands: /agami-connect, /agami-query, /agami-model, /agami-save-correction, /agami-reconcile. (/agami-model's Review tab absorbed the former /agami-review.) Never write the un-prefixed forms (/init, /connect, /query-database, etc.) or colon-namespaced forms (/agami:init, etc.) — those don't exist. /agami-init was folded into /agami-connect Phase 0a — credential setup now lives there.
For chat replies, prefer natural language over slash commands — it reads better and the skill's when_to_use matcher routes correctly:
- Re-introspect the schema → "say 'reload the schema'" or "say 'reintrospect my database'"
- Save a correction → "say 'save this as a correction'" or "say 'remember this'"
- Ask a data question → just type the question
- Set up agami / switch profiles →
/agami-connect(the one place the slash form is genuinely cleaner than natural language — agami-connect handles credentials too via Phase 0a)
Conversation style
- One question per turn unless they're truly bundled.
- A decision is a card (
shared/question-cards.md): every answer ends its turn on a typed-reply line, never a card (Phase 4d.2); large-table HIGH-risk approval, the post-install GitHub-star ask. Do NOT use AskUserQuestion for follow-up suggestions — those are 5 plain numbered bullets per Phase 4f. - No page opens by default. The chart page is the answer's full view: rendered only when the person asks for a chart (Phase 4e), and served from localhost, never opened as a
file://page. - Insights, not narration — lead with the answer ("Carol Chen has the highest spend at $148.95"), not the SQL or the process.
- Round numbers in prose, exact in the table.
- Don't echo the SQL in chat prose — that's enforced as a hard rule in Phase 2. Don't paste the raw Bash CSV — Phase 3.
Phase −1: Plan-mode check
Run the detection + ask logic from shared/plan-mode-check.md. agami-query needs Bash (SQL execution) and Write (chart HTML) — both are blocked in plan mode.
If plan mode is active and the user picks Stay in plan mode:
Reopen-last-chart intent (Phase 2a.1 below) — re-displaying an existing HTML chart only needs
Readplus serving the file withserve_page.py(run withrun_in_background). Run that flow if matched.Anything else — refuse and end the turn. DO NOT write a plan file. DO NOT call
ExitPlanMode. Refusal text (verbatim):I can't run SQL in plan mode. Switch to Auto or Edit Automatically mode (Shift+Tab to cycle) and re-invoke me.
If plan mode is not active, skip this phase silently and go to Phase 1.
Phase 1: Setup (once per session)
HARD RULES — connection rules
These are non-negotiable.
- Connect ONLY to the host/port/database/user/password in
<artifacts_dir>/local/credentials— the sole credential source (no env-var bypass). Never substitutelocalhostor any other host as a fallback. Never connect to anywhere not in the credentials. - Never ask the user for connection details in chat. If credentials are missing, stop and invoke
/agami-connect— its Phase 0a runs the DB-type picker, writes<artifacts_dir>/local/credentials.example, and ends the turn for the user to fill it in. - Never scan or guess. No
pgrep, nops, nofind /, nols /Applications/Postgres.app, no listing port-listeners. The only Bash probes allowed during setup arewhich <tool>for a database tool onPATHandbash "${CLAUDE_PLUGIN_ROOT}/scripts/py" -c 'import <module>'for a Python driver. - NEVER put the password (or any credential field) in a Bash command line. That includes
export PGPASSWORD='<value>',export MYSQL_PWD='<value>',psql -W <password>,mysql -p<password>, or any heredoc / stdin form that interpolates the password. Hosts render Bash tool calls as collapsibles in their UI — anything in the command becomes visible in the chat. Use the auth files generated byscripts/setup_pgauth.py(seeshared/connection-reference.md → HARD RULES). For native CLI queries the visible Bash command isPGPASSFILE=<artifacts_dir>/local/.pgpass psql -h ... -U ... -d ... -c "$SQL" --csv. For the Python driver path usebash "${CLAUDE_PLUGIN_ROOT}/scripts/py" -m execute_sql --sql-file ....
These rules apply to every phase of this skill, not just Phase 1.
1a — credentials check (binding)
Read <artifacts_dir>/local/credentials. If the file (or the active profile's section) is missing, invoke /agami-connect (its Phase 0a handles first-time credential setup) and stop this skill. Do not continue to load the semantic model. Do not run any other Bash commands.
1b — load the semantic model
Resolve <profile>: AGAMI_PROFILE → active_profile in <artifacts_dir>/local/.config → "main".
Resolve <artifacts_dir> per shared/file-layout.md: AGAMI_ARTIFACTS_DIR → .config.artifacts_dir → $HOME/agami-artifacts.
Then create the scratch folder every later step writes to: mkdir -p "<artifacts_dir>/local/tmp" (Claude's Write tool and a > redirect both need it to exist).
The model is the semantic-model tree at <artifacts_dir>/<profile>/ (datasource.yaml + subject_areas/<area>/…). There is no legacy-layout fallback — the model is the only format.
Never hand-read OR hand-roll the model. Don't cat/Read datasource.yaml, subject_areas/**, tables/*.yaml, or relationships.yaml, and never write a python -c / ad-hoc script to load, dump, or walk the model tree — that guesses the schema, breaks on a wrong key, and can leak a traceback to the user. The CLI returns the same data structured, and the layout is already known (relationships + entities + metrics live at the area level, not inside a table file). sm areas "$ROOT" is the one-call model map: per area it returns table_count, entity_count, metric_count, relationship_count + description — the whole shape in a single call. (Column-level detail → sm context; browsable table/column tree → sm model-tree.) When the user asks "what does the model look like" / "show me the model," run sm model-tree (or open /agami-model) — don't improvise Python.
Don't run a separate existence probe either (no ls datasource.yaml, and never probe for the plugin's own scripts — sm, execute_sql.py, semantic_model/ always ship with the plugin). That same first sm areas call doubles as the check: model present → you get the map; absent → the CLI returns {"error":"no_model"} with exit code 3 → invoke agami-connect and stop, unless AGAMI_PROFILE names a database with no model (shared/file-layout.md): then say so in one line and stop.
If an sm call exits 12, agami's library is older than the plugin: run bash "${CLAUDE_PLUGIN_ROOT}/scripts/sm" install (the person approves it), then repeat the call. It is not "no model" (exit 3): never send the person to agami-connect for it.
Drive everything through the CLI — the sm wrapper resolves the interpreter + deps. (These granular steps are CLI operations; on the MCP surface they're folded into the smart get_datasource_schema, which advertises the 4 product tools — so don't invoke the steps below as MCP tools.)
ROOT="<artifacts_dir>/<profile>"
bash "${CLAUDE_PLUGIN_ROOT}/scripts/sm" areas "$ROOT"; rc=$? # subject-area index; rc 3 = no model → agami-connect
bash "${CLAUDE_PLUGIN_ROOT}/scripts/sm" context "$ROOT" --area A --tables t1 t2 # compound table context
bash "${CLAUDE_PLUGIN_ROOT}/scripts/sm" examples "$ROOT" --area A --query "…" # examples-first ranking
The model loader already drops review_state: rejected entries from what it serves and applies the area's expose_column_groups scoping, so you never see excluded tables/columns/relationships. (Rejections are the curator's choice via /agami-model — surfaced nowhere.) When a query would touch a stale entry, warn once. A stale metric, entity or join was approved and then edited, so it is not signed off: "This uses <entity>, which changed since it was approved and needs re-approving in /agami-model." A stale table or column is schema drift: "This would use <entity>, marked stale (schema drift). Run /agami-connect to re-introspect, then /agami-model to reconcile."
1c — what the model gives you
You don't build hand-rolled indexes — the loader returns structured objects. The pieces you'll use during SQL generation:
- Subject areas — the primary scoping unit (replaces "load every table"). Each has a description, a table list, entities, metrics, and an intra-area relationship graph (each edge carries join cardinality + a trust block).
get_table_context(area, tables)— columns (scoped byexpose_column_groups),default_filters, relationships,caveats,value_transforms, metrics — in one call.- Entities — the vocabulary users say (name/plural/other_names →
maps_totable.column, with avalue_patternfor opaque IDs). Useresolve_entities/identify_entity. - Metrics — reusable aggregations with prose
calculation+ per-dialectbindings. Use the binding SQL VERBATIM when the user asks for a metric by name or synonym; don't hand-roll the aggregate. A metric withjoinssays how its tables join: bring each named table in with that kind (left→LEFT JOIN,inner→JOIN) on the declared relationship's key, or the receipt cannot credit the metric. - Cross-subject-area relationships (org level) — for joins that span two areas.
1d — load the examples library
Examples live per subject area at <artifacts_dir>/<profile>/prompt_examples/<area>/examples.yaml. Use cli examples "$ROOT" --area <area> --query "<question>" to rank them (the examples-first signal — step 2a). If a high-confidence match returns, mirror its tagged tables/columns/SQL shape and skip cold-start resolution.
If there are no examples for the relevant area → warn: "I don't have few-shot examples for this database yet — answers may be lower quality. Say 'introspect the schema' to seed them." (Slash form /agami-connect only if the user asks "what do I type?".)
1d.1 — load USER_MEMORY.md
Read <artifacts_dir>/USER_MEMORY.md (if present). Strip HTML comments (<!--...-->), then keep the rest. If the file is missing, treat it as empty — never error. See shared/user-memory-format.md for what's in it.
This file holds free-form user preferences across every database (default filters, display preferences). Inject it into the SQL-generation prompt in Phase 2b under a labeled ## User memory (preferences and policies) section — the LLM uses it as steering context.
1d.2 — load domain context
Run cli org-context "$ROOT" — it returns the full domain context for this database in one block: the human's datasource.md narrative (HTML comments stripped) plus the model-derived summary that the file does NOT contain — subject areas, conventions, and the decoded glossary (key_terminology + enum legends), assembled fresh from the structured model. Don't Read datasource.md by hand: the file holds only the human narrative; the glossary and summary live in the model, and this command is the one that combines them. If there's no model, treat as empty — never error. See shared/organization-context-format.md.
Inject the result into the SQL-generation prompt in Phase 2b under ## Datasource context, before the ## User memory section — domain knowledge precedes display preferences in the LLM's reading order.
Order in Phase 2b prompt:
- Schema context (tables / columns / relationships / metrics from the semantic model)
## Datasource context← fromcli org-context(narrative + derived summary + glossary)## User memory (preferences and policies)← from USER_MEMORY.md- Few-shot examples
- The user's question
1e — the connection invocation pattern (do NOT run a standalone probe)
Look up the cached connection method from <artifacts_dir>/local/.config. Do NOT run a separate SELECT 1 connectivity probe — it's a wasted round-trip (and pointless for a local SQLite/DuckDB file). The user's actual query is the connectivity check: run it directly, and if it fails, classify the error via db_error_classifier.md. The table below is the exact invocation pattern per tier for running that query — don't guess flags (execute_sql.py does NOT accept positional SQL, a --format flag, or any flag not listed; guessing produces "unrecognized arguments" errors that waste turns). The SELECT 1 in each row is only a placeholder for your SQL.
| tier | invocation pattern (substitute your SQL for SELECT 1) |
|---|---|
cli (postgres) |
PGPASSFILE="<artifacts_dir>/local/.pgpass" psql -h <host> -U <user> -d <db> -c 'SELECT 1' --csv |
cli (mysql) |
mysql --defaults-file="<artifacts_dir>/local/.mysql.cnf" --defaults-group-suffix="_<profile>" -e 'SELECT 1' --batch |
cli (snowflake) |
snowsql --config "<artifacts_dir>/local/.snowsql.cnf" -c "<profile>" -q 'SELECT 1' -o exit_on_error=true -o output_format=csv -o friendly=false |
cli (sqlite) |
sqlite3 -header -csv "<path>" 'SELECT 1' — always -header, or result CSVs lose column names and format-table treats the first data row as the header (a wasteful re-export). |
duckdb (any) |
duckdb -init "$init_file" -c 'SELECT 1' --csv (see build_duckdb_attach.py for $init_file) |
python (all DBs) |
AGAMI_PROFILE="<profile>" bash "${CLAUDE_PLUGIN_ROOT}/scripts/py" -m execute_sql --sql 'SELECT 1' |
A profile whose password is a keyring: reference always runs on the python tier (python -m execute_sql), never psql / mysql / snowsql: setup_pgauth.py writes no auth file for it, because those files are plain text.
When such a profile's password has changed, or the error says to reconnect with --replace, change the saved password with bash "${CLAUDE_PLUGIN_ROOT}/scripts/py" connect_helper.py --spec <spec.json> --replace: the spec is {"profile", "db_type", "fields"} taken from the profile's current section (every key except type and password), and the user types the new password on the page the helper's first JSON line points to, never in chat. If the new password does not connect, the helper puts the old one back.
The Python tier's CLI is --sql <string> or --sql-file <path> — those are the only two ways to pass SQL. Optional flag: --profile <profile> (overrides AGAMI_PROFILE env). Output is RFC-4180 CSV on stdout, always — no --format flag exists. If you need JSON, post-process the CSV.
Route any error through shared/db_error_classifier.md. Common cases:
auth/dsn→ credentials may have rotated; point at<artifacts_dir>/local/credentials.network→ check VPN / DB endpoint reachability.sign_in_required→ the person's own sign-in expired; relay the message (sign in again, new conversation).driver_missing→ fall through to the next available method.
If the cached method doesn't work, re-run tool detection per agami-connect/SKILL.md → Phase 0a.5.
Phase 2: Generate SQL
HARD RULE — never echo SQL in chat prose
The generated SQL belongs in two places only: (1) the Bash invocation that executes it (which the host shows as a collapsible tool call — outside our control), and (2) the collapsible "SQL" section of the chart page, when the person asks for one (Phase 4e). Never paste, quote, or summarize the SQL in the assistant's narrated text. No SELECT ... lines, no fenced ```sql blocks, no "I'm running this query: ..." prose. Users who want the SQL see it in the SQL details element of the chart page.
This rule applies to every retry, every fallback, every regenerate. The chat prose stays focused on approach, fetching, and insight.
2a — classify the input
Check intents in this order. The first match wins; only that branch runs.
Reopen-last-chart intent (handled in 2a.1 below). Triggered by short messages that ask to re-display the most recent chart without re-running SQL. Trigger phrases:
- "reopen", "reopen the chart", "reopen that"
- "open the last chart", "open that again", "open my last report"
- "show me that chart again", "show me the last chart", "show that"
- "open the previous chart", "show that report"
- Any message ≤ 8 words that combines an open-verb (open / show / see / view / display) with a chart-noun (chart / report / it / that / last / again).
If matched → jump to 2a.1 and skip Phases 2b–4.
A question (contains
?or starts with how/what/show/list/which/count/give/get/find/total/average/top/which AND isn't matched by the reopen intent above) → save it as the user's data question. Continue to 2b.Empty → ask the user; suggest 2-3 questions from the model's
ai_context.examplesif present, or inferred fromdatasets[].description.Flag-only (
--csv/--chart bar) → re-run the previous query with the flag applied.Follow-up like "make that a chart" → the person asked for a chart: Phase 4e on the last answer.
2a.1 — Reopen-last-chart flow (no new SQL)
If the user's intent is to re-display the most recent chart:
- Read
<artifacts_dir>/local/query_log.jsonl(each line is a JSON object) and take the last non-empty line whosestatusis"ok"or absent — the log also records refused and failed executions, and neither of those rendered a chart. Entries written before the log carried astatusfield have none; treat those asok. - Look at the
chart_pathfield. Possible cases:chart_pathset AND the file exists on disk → serve it from localhost,bash "${CLAUDE_PLUGIN_ROOT}/scripts/py" serve_page.py "<path>" --minutes 30withrun_in_background, and read its first line,{"url": …}. Surface a one-liner in chat:
Done. Skip every other phase. Don't re-execute SQL. Don't re-render. Don't add 5 follow-ups (this is a UI action, not a fresh answer).Reopened: http://localhost:<port>/<name> (open for 30 minutes)chart_pathis null (the last answer was never shown as a chart) → render it now: Phase 4e on the last answer, the same as "Show as a chart". A 1×1 scalar has nothing to chart: surface "The last answer was a single number, so there's no chart to show."chart_pathset but the file is missing (user deleted<artifacts_dir>/local/charts/<profile>/) → surface: "The chart file is gone —<path>no longer exists. Ask me the question again and I'll regenerate it."- Query log empty or missing → surface: "I don't have any prior queries to reopen. Ask me a question first."
This phase neither logs anything new to query_log.jsonl nor sends telemetry — re-opening an existing artifact isn't a query event.
2b — assemble the prompt via the examples-first traversal
For a single profile, follow the examples-first canonical loop — the subject area is the scoping unit, so you never dump the whole schema. (Cross-profile federation is 2b.federation below; it's orthogonal to this loop.)
Step 1 — pick the subject area(s). cli areas "$ROOT" → choose the area(s) whose description matches the question's intent. Most questions touch one area; cross-area ones (a join spanning two areas) select both, and the org's cross_subject_area_relationships supply the join.
Step 2 — examples first (strongest signal). cli examples "$ROOT" --area <area> --query "<question>", adding --metric "<name>" for each model metric the question names, so the examples tagged with it rank first. --metric alone, without --query, lists only the examples tagged with that metric. If high_confidence is true, mirror the top match's tagged tables / columns / metrics / SQL shape and jump to step 5 — skip cold-start resolution.
Step 3 (cold start only) — resolve entities + metrics + opaque literals. Match the question's terms to the area's entities (and metrics). For any opaque literal in the question (an ID-looking token), the CLI entity matching recognizes its type via value_pattern (folded into get_datasource_schema on the MCP surface, not a separate tool); if it returns clarify, ask the user one targeted question rather than guessing.
Step 4 (cold start only) — choose tables + columns from what resolved (entity maps_to, metric source_tables).
Step 5 — compound context fetch. cli context "$ROOT" --area <area> --tables … [--columns …] returns columns (scoped by the area's expose_column_groups — wide tables disclose only their exposed groups), default_filters, relationships (with cardinality + review state), caveats, value_transforms, and metrics, in one round-trip.
Step 6 — assemble the generator prompt in this order, then produce ONE SQL statement (first statement only if several are emitted):
System — "Write one valid SQL statement for
<DB_TYPE>(ANSI_SQL +<DB_TYPE>tweaks per dialect-rules.md). Output ONLY SQL. Prefer indexed/recommended_filterscolumns on large tables. Apply each column'svalue_transformwhen selecting/filtering it. Asensitivecolumn is the model author asking for care, not a locked door. Prefer using it inCOUNT/COUNT(DISTINCT …),GROUP BY,WHERE, andJOINover projecting its raw per-row values. So: (a) 'how many unique customer emails?' →SELECT COUNT(DISTINCT email)and report the count. (b) To disambiguate identical display labels (two customers with the same name), put the entity's non-sensitive key (id) in the output rather than the raw email/phone. (c) When the question genuinely needs the values — 'I need the mailing list' — project them and say in the answer that you did; the receipt records it too. Nothing refuses this, so the care is yours to exercise and yours to be transparent about. A column that must never be readable is not in the model at all, and any statement naming it is refused as out of scope. Use a metric'sbindingsSQL VERBATIM when the question names that metric (or a synonym)."Schema context — the
get_table_contextoutput for the chosen tables (columns + types + caveats + value_transforms), the area's relationships (rendered asfrom.col → to.col [cardinality]), and the area's metrics (<name>: <binding> -- <calculation>+ synonyms).default_filtersARE yours to apply. Nothing applies them for you — but the receipt DOES report whether you did, per table reference, so an omission is visible to the user in the report beside the answer. If a table declares one and the question does not deliberately ask about the rows it excludes, write it into theWHEREclause. Two things to watch, becauseget_table_contexthas already rewritten them: the{alias}placeholder is gone — each filter comes back qualified with the bare table name, so re-qualify it to whatever alias you actually used, or the database rejects the statement. And a filter that still carries a:parammarker (e.g.orders.tenant_id = :tenant_id) has no value to bind — leave it OUT and say so in the answer rather than emitting SQL that won't parse. Also DO honor any caveats, and each column'splaceholders: a value a person said meansunknown,currentornot_set, written as sql-generation-rules.md § 11 says (nothing filters it for you).Unreviewed metrics are USED, not refused. When the question names a metric whose
review_state ≠ approved, still use its binding and answer — do NOT block or refuse on it. The trust layer surfaces it on the receipt, not as a hard gate: the metric rides on thereceipt.columns.itemsentry for the output column that computes it (kind: "output",status: "matched") carrying its ownreview_state, and "How this was answered" (and the chart page's approve/change banner) is driven off exactly that field (Phase 4d.1). The loader already drops onlyrejectedmetrics; anunreviewed/proposedone is yours to use, with the surfaced review state carrying the honesty. (Same for unreviewed joins/entities andstaleentries — surface, never refuse.)Datasource context —
datasource.md(step 1d.2), heading## Datasource context. Binding domain context.User memory —
USER_MEMORY.md(step 1d.1), heading## User memory (preferences and policies).Few-shot examples — the ranked matches from step 2.
User question.
The fan/chasm safety pass runs as a pre-execution step, on every tier. Before you execute the generated SQL, pass it through sm prepare "$ROOT" --area <area> --sql-file <path> (Phase 3a). It runs the fan-trap / chasm-trap pre-flight and the aggregation-semantics checks, and returns the SQL to actually run, which is always the SQL you gave it: this command never rewrites your statement and never refuses it. What it returns alongside is findings — see Phase 3a for how to act on them. Tier-independent — works whether you execute via psql, the Python driver, or DuckDB — so the checks never depend on the execution path. It does not apply the area's default_filters, and nothing else does either: those are declarative only, so put any you need into the statement yourself at step 6, and sm prepare emits no applied_filters key of its own (an always-empty list would read as "we checked, none applied"). Which ones your finished statement satisfied is settled later, by sm receipt, on tables.items[].filters (Phase 4d.1). The findings DO ride on the receipt too, in its aggregates section, so the panel draws them beside the answer.
2b.federation — cross-database queries
When the question references datasets from ≥ 2 different profiles (e.g., ITSM in Redshift × finance in MySQL), the skill routes the SQL through DuckDB, which ATTACHes both databases in one session and runs a native federated JOIN.
Detecting federation. Extend Pass 1 of the two-pass retrieval to pick (profile, schema, table) tuples instead of just (schema, table). If the picked set spans len({tuple.profile}) > 1, federation mode is on.
For small databases (under 50 tables), build the union of every profile's index up front and run Pass 1 against that combined index — the picker then chooses across profiles automatically. For larger setups Pass 1 already runs; just include profile in each entry.
The Pass 1 prompt loads <artifacts_dir>/local/cross_profile_relationships.yaml (if present) so the picker knows about declared cross-profile JOIN paths. If the file is missing, the picker falls back to inferring relationships from column-name/type matching across profile indexes — best-effort, with a warning to the user that confidence is lower.
<artifacts_dir>/local/cross_profile_relationships.yaml (optional) — declares known JOIN paths across profiles:
version: "0.1.1"
relationships:
- name: itsm_assets_to_finance_cost_centers
from_profile: itsm
from_dataset: public.assets
from_columns: [department_id]
to_profile: finance
to_dataset: dbo.cost_centers
to_columns: [dept_id]
description: ITSM assets carry the same dept_id as finance cost centers.
Loaded at session start the same way per-profile indexes are loaded.
Building the federated SQL. When federation mode is active, the schema-context section of the prompt uses three-part dataset names matching the DuckDB ATTACH alias: <profile>.<schema>.<table> (e.g., itsm.public.assets, finance.dbo.cost_centers). Cross-profile relationships from cross_profile_relationships.yaml are rendered alongside per-profile relationships. The model produces SQL using these three-part names.
Verifying DuckDB is available. Look up tool_paths.duckdb from <artifacts_dir>/local/.config. If missing, surface:
Cross-database queries need DuckDB. Install it with `brew install duckdb`
(or apt / download) and re-run.
…and stop.
Verifying credentials are set up for every profile. For each profile in the picked set, check that the corresponding auth file exists (<artifacts_dir>/local/.pgpass, <artifacts_dir>/local/.mysql.cnf). Missing → run bash "${CLAUDE_PLUGIN_ROOT}/scripts/py" setup_pgauth.py --profile <profile> for each gap, then re-check.
Generating the temp init file.
init_file=$(bash "${CLAUDE_PLUGIN_ROOT}/scripts/py" build_duckdb_attach.py \
--profiles "$P1" "$P2" | tr -d '\r') # Windows Python ends lines \r\n
# init_file is the path of a chmod-600 file in <artifacts_dir>/local/.duckdb_init_*.sql.
# Credentials are inside that file, NOT on the command line.
Running the SQL.
duckdb -init "$init_file" -c "$FEDERATED_SQL" --csv
The visible Bash command shows only the path — DuckDB reads the ATTACH credentials silently from the init file.
Tear-down. After the query completes (success or failure), delete the init file:
rm -f "$init_file"
The next invocation also self-cleans any .duckdb_init_*.sql older than 1 hour in case a prior run crashed:
find "<artifacts_dir>/local" -maxdepth 1 -name '.duckdb_init_*.sql' -mmin +60 -delete 2>/dev/null
Performance warning. Federated joins through DuckDB scanners are bounded by network round-trips. If both sides of the join estimate to > 100k rows, surface a one-liner before running:
This federated query may take 30–120s (network round-trips for
<P1>×<P2>). Want to tighten the filter first?
Options: Run anyway (Recommended for one-off) / Let me add a filter / Cancel.
Type alignment. Postgres numeric(10,2) joined with MySQL decimal(10,2) works. Mismatched types (date vs string, integer vs uuid) need an explicit CAST in the generated SQL. The Phase 2b prompt instructs the LLM about this:
When joining across profiles, prefer explicit
CAST(<col> AS <type>)for any pair where the types might differ (e.g., timestamps stored as strings on one side, dates on the other).
No Snowflake federation. DuckDB's snowflake_scanner is experimental and not packaged with the standard binary. If a profile in the picked set has db_type=snowflake, build_duckdb_attach.py exits with a clear error: surface it to the user and suggest pre-aggregating one side as a CSV.
Round what a person reads, in the SQL. A rate, a percentage or an average comes out of the database with a dozen decimals (79.6501901140684411), and the table formatter shows numbers exactly as they arrive, on purpose. So wrap a rate or percentage in ROUND(…, 1) and an average or money value in ROUND(…, 2) in the statement itself. Never round a count or a sum of whole things. A metric wrapped in ROUND still matches its signed-off definition on the receipt.
2c — safety checks
Apply shared/sql-generation-rules.md:
- No DDL/DML. Refuse on
DROP,DELETE,INSERT,UPDATE,ALTER,TRUNCATE,CREATE,GRANT,REVOKE. Regenerate with explicit "SELECT only" framing. - No system tables. Refuse on
pg_catalog,information_schema,mysql.*,sys.*unless the user is explicitly asking about schema metadata. - NULL-safe division via
NULLIF(denominator, 0). agami.typeconsistency — if the SQL applies a numeric aggregate (SUM,AVG) to a field whoseagami.typeisstringorboolean, refuse and regenerate. Type info exists for a reason.
2d — risk assessment + time estimate for large tables
For each dataset touched by the SQL, look up its agami.performance_hints:
recommended_filters is a list of column names (introspection seeds it with a large table's date/time columns — the columns worth filtering on to avoid a full scan). Check whether the generated SQL's WHERE filters on any of them.
estimated_row_count > 1_000_000AND the WHERE filters on none of the table'srecommended_filters: → HIGH risk. Surface a banner before executing — name a suggested column when one exists: "This query scans the table (~<row_count> rows) with no filter on <recommended_filters[0] in words>. Estimated time:Largest scanned dataset With indexed filter ( WHEREmatchesagami.performance_hints.indexes)Without indexed filter (full scan) < 100k rows < 1s < 2s 100k–1M 1–5s 5–30s 1M–10M 5–15s 30–120s 10M–100M 15–60s 2–10 min — ALWAYS warn even if filter is present > 100M 30–120s > 10 min — block as HIGH risk; offer to add filter or sample Snowflake-specific: add 5–30s on top of any estimate for warehouse spin-up if the warehouse has been idle (the query log can detect "first query in this session" → assume cold). Federation (Phase 2b.federation) doubles or triples estimates due to network round-trips — surface "this federated query may take 30–120s" before running, regardless of estimated_row_count.
If the estimate exceeds 30s, also surface: "Cancel anytime — Ctrl+C in CLI, or just send another message." The user should know they're not trapped waiting.
Phase 3: Execute
HARD RULE — never paste raw output in chat
The Bash result (CSV stdout, stderr, exit code) is for the skill to parse, not for the user to read. Never paste the raw CSV / TSV from the Bash result into the assistant's response text. No "Here's what came back: …", no markdown code-fence dumps of the result. Parse internally, then surface the polished output per Phase 4. The host shows the Bash tool call as a collapsible — that's enough provenance for users who want to dig.
3a — safety pass, then run the SQL
Step 1 — prepare (every tier). Write the generated SQL to a temp file, then run the tier-independent safety pass:
bash "${CLAUDE_PLUGIN_ROOT}/scripts/sm" prepare "$ROOT" --area <area> --sql-file "<artifacts_dir>/local/tmp/agami-q.sql"It returns JSON:
{sql, findings, units}, and it always exits 0. The returnedsqlis the statement you handed in, byte for byte — this command never rewrites and never refuses. (unitsis the{output_column: unit}map traced through that statement — keep it for the table render in 4d.)findingsis a list, usually empty. Each entry is{risk, reason, triggering_joins}and each one is a fact about the statement, not a verdict on it:fan_trap/chasm_trap— a join multiplies the rows an aggregate is computed from.triggering_joinsnames which join does it.fan_out_invariant— the same multiplication, on an aggregate it cannot move (MIN,MAX, aDISTINCTone,BOOL_AND/BOOL_OR). The rows were duplicated; the number is the same either way.triggering_joinsnames the join, exactly as above.bad_aggregation— aSUMof a rate or an identifier, or anAVGof one.semi_additive— aSUMof a balance across a time grain, which multiplies a stock.
A finding is not a refusal, and it is not automatically a bug. Whether a multiplied total is wrong depends on what was asked: the same statement is wrong for order revenue and right for line-item exposure. The pre-flight does not have the question. You do. So this is the point where the judgement gets made, out loud, by you:
- The finding is not what the user meant — restructure and re-prepare. Typically: pre-aggregate each measure in its own CTE and outer-join them (chasm), move the aggregate into a window function to keep raw rows, or drop a join the query does not otherwise use (fan, where the many side is touched nowhere but the
ONclause). Say in the answer that you restructured to avoid a<risk>. The restructured statement carries no finding, so the receipt says nothing about it — that sentence is yours to write, and it is what separates this from a guard silently swapping the statement underneath the user. - The finding IS what the user meant — run it and say so. "This counts each order once per line item, which is what 'line-item exposure' asks for." A correct answer to the question asked is not a defect, and refusing it would have been the wrong call.
- You cannot tell which — the fan-out join also filters or groups, so the candidate readings return different numbers (e.g. "loans with ≥1 payment since January" versus a payment-weighted total). Do not pick one. Ask: a short "Did you mean…?" with 2–3 concrete interpretations, one plain-language sentence each, no SQL. Generate for the one they choose.
Either way the finding rides on the answer's receipt, in the
aggregatessection, so a user reading the result can see what was found without taking your word for it.An empty
findingsis not by itself a clean bill of health. Checkuncheckedalongside it: it isnullwhen the checks ran, and a sentence when they could not — sqlglot missing, the statement unparseable, or no SELECT in it. All three yield the same empty list a genuinely clean statement does, so a non-nulluncheckedmeans you have learned nothing about this statement's aggregates and should say so rather than implying it passed.Even when
uncheckedis null, "found nothing" is bounded. The receipt'saggregatesmarker states what the checks do not reach — an aggregate inside a CTE or a subquery, one inHAVINGorORDER BY— and you should surface that marker rather than presenting an empty section as clean.Step 2 — execute the returned
sqlvia the tier's tool fromshared/connection-reference.md → CLI Connection Commands— psql / mysql / snowsql / sqlite3 / DuckDB, or the Python driver. (If you usepython -m execute_sql, do NOT pass--no-safety.sm prepareruns the fan/chasm and aggregation checks, which report; it does not run the table-scope,SELECT *or column-scope gates, which refuse. Those are the enforcement, they live only inexecute_sql, and skipping them lets a hallucinated table or column reach the warehouse. The checks that would be doubled are cheap; the gates that would be skipped are not optional.) Wrap in a high-resolution timer; capture stdout (CSV rows), stderr (errors), exit code. Route a non-zero exit through the error classifier (Phase 3b).3b — error handling + auto-retry
Route any non-zero exit through
shared/db_error_classifier.md. Behavior per kind:error_kindBehavior auth,dsn,networkStop. Surface the one-line remediation. No retry. driver_missingFall through to the next available method (native CLI → DuckDB → Python driver). permissionStop. DB user lacks SELECT on the touched dataset. sign_in_requiredStop. The person's own sign-in expired or is missing — relay the message (sign in again, then start a new conversation). Nothing is broken; don't retry. column_not_found,table_not_found,syntaxAuto-retry up to 2 times. Pass the error back to the SQL generator: "The previous SQL failed with <one-line classifier message>. Regenerate using only table / column names from the schema context above."otherStop. Surface raw error truncated to 200 chars. A statement stopped for taking too long no longer arrives here as an error. A per-statement deadline is a
resource_limitrefusal, which carries its own remediation because it is a decision the server made — handle it as a refusal, not as a classified failure. Thetimeoutfailure kind now has one producer, the supervisor stopping an executor that never responded, and there is nothing query-specific to suggest for it.After 2 retries with no success, stop. Don't loop.
3c — parse rows
Parse the CSV stdout. Header row = column names. Body rows = data.
Sanitize column headers before display. SQL aliasing slips happen — bare
n,cnt, single-letter columns,?column?(Postgres unaliased), and shouty all-uppercase Snowflake names are common. Apply this header transform once before any rendering surface (4d markdown, the 4e chart page, chart axis labels):Raw header Display header Why n,N,cnt,CNTCountBare counts are unreadable; "Count" is universal ?column?(Postgres unaliased),_col0,_col1Drop the column entirely from the visible output (parse warning to log: "unaliased column in result; SQL generator should always alias") Single uppercase word (Snowflake default — STATUS,AMOUNT)Status,Amount(title-case the word, lowercase trailing letters)Snake_case ( order_count,customer_name)Title-case with spaces ( Order Count,Customer Name) — except when ending in a unit (_amount→_amountraw is OK if unit shows in the header parens, likeAvg Outstanding (INR))Already title-case or sentence-case Leave as-is The header transform is purely cosmetic — the underlying alias stays in the SQL and the
tables_usedlog. Don't rename the column in the result data; only its rendered label.Format every cell per its column's
typeandunit(both come fromget_table_context—unitis the structured currency/unit set during onboarding's currency ask or by a correction). For a metric result column, use the metric'sunit. The canonical mapping issemantic_model/units.py(format_value) — the table below mirrors it; when in doubt, match it. The same formatting applies to every downstream surface — chat markdown table (4d), HTMLtable_rows(4e.iii), AND chartlabels. Also pass the value column'sunitinto each chart section's"unit"field so the chart's y-axis + tooltips format the symbol + grouping deterministically (the chart template applies it; you don't hand-format chart axes — and thedatasetsdata stays RAW numbers).Numbers are formatted by code, not by you. The chat markdown table (4d) is rendered by
sm format-tableand the chart by the template — both viaunits.py, in full and exact (a verification surface; never abbreviate or round). Your job in 3c is the non-numeric display (header sanitization, dates →MMM D, YYYY, booleans → Yes/No,choice_field→ label) and supplying each column'sunit; the numeric cells are then emitted deterministically downstream. The type/unit table below is the referenceunits.pyimplements.typeunitFormat decimal/integerusd,dollars$148.95(always show$, 2 decimals, locale grouping)decimal/integereur€148.95decimal/integergbp£148.95decimal/integerjpy¥148(no decimals — JPY has no minor unit)decimal/integerinr₹2,162,087(Indian Rupee symbol; for amounts > 100k optionally use2.16 Cr/21.6 Lif USER_MEMORY says "use Indian numbering")decimal/integercadCA$148.95(or$148.95if locale is Canadian)decimal/integeraudA$148.95decimal/integerchfCHF 148.95(no symbol convention; show code + space)decimal/integerother ISO 4217 code <UPPERCASE_CODE> <number>— show the code as a prefix (SEK 148.95,BRL 148.95). Never strip the unit silently.decimalpercent12.4%(1 decimal)decimal(other / none) 1,234.5(commas, the value's own decimals with trailing zeros dropped: a NUMERIC0.12500000shows0.125)integer(no currency unit) 1,234(commas, no decimals; a negative is-719)integer/string— (a year, a ZIP/postal code, an id, a key, a choice field) as written — 2011,07094, never2,011or7,094.sm preparereturns these columns aslabels.date— May 6, 2026(MMM D, YYYY) — never raw ISO2026-05-06, never epoch numberstimestamp— May 6, 2026 3:14 PM(MMM D, YYYY h:mm A) — never raw ISO withTseparator, never microsecond precisioninteger/stringepoch_s/epoch_ms/epoch_us/epoch_ns,yyyymmdda date encoding — format-tablerenders it human-readably (epoch →YYYY-MM-DD HH:MM:SS UTC) deterministically; don't hand-format. In the SQL, convert it (to_timestamp(created_ts)for epoch_s,to_timestamp(created_ts/1000)for ms, etc.) so filters/grouping work and the result is a real date.boolean— Yes/No(ortrue/falseif the user prefers — read USER_MEMORY)string(withchoice_field)— the choice's display label, not the stored value string(other)— as-is Timezone: when a date/timestamp result column has a
timezoneinget_table_context(e.g.UTCfor epoch columns,offset-awarefor a TZ-aware timestamp), state it once in the prose insight ("times shown in UTC") so the reader isn't guessing — and especially flag it when the stored value is a naive/offset-awaretimestamp whose zone differs from the user's. Epoch columns are UTC by definition;format-tablealready labels each rendered cell… UTC.Hard rule for currency: if the field has a currency
agami.unit, show the symbol or code in every cell value — never omit it because the column header already mentions the currency. The user's screenshot showed bare2,162,087in an "Avg Outstanding (INR)" column; the cell should read₹2,162,087. Headers can drop redundant unit (the column headerAvg Outstandingis enough when every cell is₹...), but cells should always carry the symbol so a screenshot or copy-paste of a single cell stays unambiguous.Hard rule for dates: never display ISO timestamps like
2026-05-07T15:14:00.000Z, epoch seconds, or any other machine-format string. Cell values OR chart-axis labels — both must be human-readable. If the column shows months only (typical for time-series charts grouped by month), useMMM YYYY(e.g.May 2026); for daily granularity useMMM Dif all data is in the current year, elseMMM D, YYYY. The skill is responsible for inferring the right grain from the data.If the user has stated a date-format preference in
<artifacts_dir>/USER_MEMORY.md(e.g. "use ISO dates" or "use DD/MM/YYYY"), respect that. The defaults above apply only when USER_MEMORY is silent.If row count > 15:
- The chat preview shows the first 15 rows only (
shared/question-cards.mdrule 3) — longer markdown tables scroll and bury the insight. - The chart page (Phase 4e, when the person asks for it) holds the full set in a paginated table.
- For row counts > 15, auto-write the CSV to
<artifacts_dir>/local/exports/<profile>/<ts>.csvwithout waiting for the user to ask. Surface its path in Phase 4d's footer. - Footer line under the table:
Showing 15 of <N> rows; <N−15> more · full set in CSV: <csv-path>. Use thousands separators on<N>(e.g.4,213). - If
<N>is huge (> 100k), additionally suggest tightening the filter inline: "If you want to slice by region or date, say so and I'll re-run."
CSVs open natively in Excel / Numbers / Google Sheets, so "export to Excel" routes to this same CSV path — no separate
.xlsxflow in v1.If row count == 0:
- "No rows matched. The query was: …" (show SQL).
- Suggest a relaxation if applicable.
Phase 4: Present
The chat reply follows this strict order, with NO other content interleaved (no SQL, no Bash output, no "let me know if…" filler):
4a Approach → 4b Fetching → 4c Insight → 4d Table → 4d.1 How this was answered → (log the query, Phase 5, silently) → 4f Numbered follow-ups → the reply line (4d.2), always the last line, all in the turn's last message, which ends the turn (4d.2: no card after an answer) → (optional 4g CSV path); the person's reply to the answer, then (4e.5 star ask, once)
Each step is short. The whole reply should fit in a typical chat viewport without scrolling. No page is rendered or opened on this path: the chart page (4e) runs only when the person replies Show as a chart to the answer (4d.2), passes
--chart, or asks for a chart ("make that a chart"). Cards followshared/question-cards.md.4a — Approach (one sentence, plain English)
Open with a single sentence describing how you'll answer, in plain English. No SQL keywords (
SELECT,JOIN,GROUP BY). No table or column names — describe the dimensions in user-language. Examples:- "I'll group orders by status across the last 30 days and rank by count."
- "I'll pull total spend per customer and sort to find the top 5."
- "I'll compare this month's revenue to last month's, broken down by region."
For multi-section reports (broad questions), the approach sentence describes the narrative across all sections: "I'll cover four angles — revenue trend, top customers, order status, and region split."
4b — Fetching (one sentence, counts only)
Right after the approach, one sentence about the data we just pulled, in counts and dimensions. Examples:
- "Pulled 6 rows across 4 statuses."
- "Pulled 247 orders spanning the last 30 days."
- "Pulled 12 monthly buckets with revenue totals."
For multi-section: "Pulled data for 4 sections (12 + 5 + 6 + 3 rows)."
4c — Insight first
One sentence stating the answer. Lead with the most surprising or actionable finding. Examples:
- "Carol Chen is the top spender at $148.95 — about 3x the next customer."
- "Revenue is up 12% MoM; the surge came from EU customers."
- "60% of orders shipped on time this month, up from 48% last month."
For multi-section: a 1–3 sentence executive summary across all sections (the same summary passed as
--summaryif the chart page is shown).4d — Markdown table
Render the table deterministically — do NOT hand-type the number cells. This is a verification surface; an exact number is mandatory (no rounding, no
1.2L/2.16Crabbreviation, no dropped decimals). Pass the result CSV (first 15 rows, headers sanitized per 3c) and theunitsmap thatsm preparealready returned in 3a through the packaged formatter, then embed its output verbatim:bash "${CLAUDE_PLUGIN_ROOT}/scripts/sm" format-table --csv-file "<artifacts_dir>/local/tmp/agami-result.csv" --units "$PREPARE_UNITS" --labels "$PREPARE_LABELS"prepare'sunitsis keyed by output column and traced through the SQL — soSUM(amount) AS total_outstandingcorrectly carries amount's currency (a bare name match would miss the summed total), and only a column that has a unit gets one. Itslabelslists the output columns that hold a year, a code or an id, whichformat-tableshows as written instead of grouping.format-tablethen formats every numeric cell in full viaunits.py(symbol + Indian/western grouping, exact decimals) and passes non-numbers through. Same map + formatter the MCP uses, so verification numbers are identical regardless of host/LLM. (If a header was prettified in 3c, key the units to the prettified name.) Wide tables (> 8 cols) → vertical layout + "wide table — the CSV (or Show as a chart) has the full grid".Cap the chat preview at 15 rows per Phase 3c — even when the user asked for "all leads with credit rating > 700" and the result is 4,213 rows, the chat shows the first 15 and points them at the CSV. The full set lives on disk. The footer line ("Showing 15 of 4,213 rows; 4,198 more · full set in CSV: …") is the contract that tells the user where to find everything.
Multi-section answers show one short table per section, each under its section title and each rendered by
sm format-table(at most 15 rows, with the same footer when there are more). Keep the section count to what the question needs (4e.i); the chart page, when asked for, holds them all with their charts.4d.1 — How this was answered (the trust receipt via
sm receipt; do NOT hand-build it)The receipt documents provenance: the tables the statement touched, the joins and metrics it used, the columns it referenced, the assumptions it leaned on, and the model-version pin. It is assembled deterministically from the SQL + the model by
runtime.assemble_receipt— the SAME builder the MCP server uses — so it never depends on you extracting fields by hand:bash "${CLAUDE_PLUGIN_ROOT}/scripts/sm" receipt "$ROOT" \ --sql-file "<artifacts_dir>/local/tmp/agami-q-<n>.sql" \ > "<artifacts_dir>/local/tmp/agami-receipt-<ts>.json"It emits
model_versionplus five sections —columns,tables,joins,aggregates,assumptions— each an object of the form{"items": [...], "undetermined": "<sentence>" | null}. It parses the FROM/JOIN scope and matches model entries itself:section what items[]holdscolumnsevery column the statement referenced, as {"column": "<schema>.<table>.<col>", "metric": null}— plus each model metric whose binding SQL appears in the statement, as{"column": null, "metric": {...}}. A metric match is a statement-level fact today, so it has no owning column. The metric object carriesname,area,definition_prose,expression,confidence,review_state,origin— whether it is approved, never who approved ittablesone entry per REFERENCE, not per table — a table read twice appears twice. ref(the name as the statement wrote it),alias,qname(what the model resolved it to),declared,rows,rows_text(the size in words, "at least 5,000,000 rows" for one that reached the cap; quote this, neverrows),rows_as_of,freshness,scope,filters,scan_risk.scan_riskis a sentence, or null, whe
Files (agami-core)
-
SKILL.md 112.6 KB
--- name: agami-query description: "Answers natural-language questions about the user's database. Loads the agami semantic model (subject areas, tables, columns, relationships with join cardinality, entities, metrics) and few-shot examples from <artifacts_dir>/<profile>/, generates SQL via the examples-first traversal (pick subject area → match examples → resolve entities/metrics → compound table context), executes it locally via the user's chosen tool (psql / mysql / snowsql / sqlite3 native CLI, DuckDB binary, or the Python driver `execute_sql.py` — which runs the scope gates and reports fan-trap/chasm-trap and aggregation findings on the receipt), returns results as a markdown table with optional CSV export, and renders Chart.js HTML charts on request. All execution is local — no data leaves the machine." when_to_use: "Use when the user asks 'how many', 'show me', 'top N', 'trend over time', 'compare', 'breakdown by', 'group by', 'average', or any other data question against their configured database. Also use for CSV export ('export this'), chart rendering ('make that a bar chart'), or to follow up on a previous result ('drill into the EU region')." argument-hint: "[question] [--csv] [--chart bar|line|pie|doughnut|scatter]" --- # agami query-database You answer the user's natural-language question about their database. Goal: generate correct SQL from the semantic model + the few-shot examples via the examples-first traversal, execute it locally, return rows + an insight, and offer a chart / export when appropriate. Everything runs on the user's machine. This skill orchestrates: 1. **Setup** (once per session) — resolve the profile + the semantic model at `<artifacts_dir>/<profile>/`, verify the configured database tool still works. 2. **Generate SQL** — examples-first traversal: pick the subject area → match curated examples → (cold start) resolve entities/metrics + identify opaque literals → compound `get_table_context` → produce one SQL statement → safety checks. 3. **Execute** — run via the chosen tool; the Python tier runs the fan/chasm pre-flight and the scope/PII gates; auto-retry on classified errors; risk-assess large-table queries. 4. **Present** — the answer as a markdown table, "How this was answered" from the receipt, then the log line (`<artifacts_dir>/local/query_log.jsonl`, written right after 4d.1 and before the answer is posted; its shape is in Phase 5), then the 5 follow-ups and one reply line that ends the turn ("looks right" / what's off / "chart" / "sign off"; no card follows an answer); CSV via `--csv` or "export this"; on "Show as a chart", `--chart` or "make that a chart", a text bar chart in the chat (`text_chart.py`), with the Chart.js report as the full view on request, served from localhost. 5. **Post-install GitHub-star ask** — after the person has replied to the answer, ask the user (once, after first successful query) to star us on GitHub; once they answer, point them to `/agami-serve` (wire the model into Claude Desktop — the experience their business users get). For the model format: [`semantic_model/__init__.py`](../../../../packages/agami-core/src/semantic_model/__init__.py) (layout) + `packages/agami-core/src/semantic_model/models.py`. For SQL safety: [`shared/sql-generation-rules.md`](../../shared/sql-generation-rules.md). For dialect-specific syntax: [`shared/dialect-rules.md`](../../shared/dialect-rules.md). For connection method + execution: [`shared/connection-reference.md`](../../shared/connection-reference.md). For DB error classification: [`shared/db_error_classifier.md`](../../shared/db_error_classifier.md). For chart template: [`shared/chart-template.html`](../../shared/chart-template.html). For cards (every decision is one): [`shared/question-cards.md`](../../shared/question-cards.md). ## Invocation conventions **Read [`shared/invocation-conventions.md`](../../shared/invocation-conventions.md) before suggesting any slash command in chat.** Agami slash commands: `/agami-connect`, `/agami-query`, `/agami-model`, `/agami-save-correction`, `/agami-reconcile`. (`/agami-model`'s Review tab absorbed the former `/agami-review`.) Never write the un-prefixed forms (`/init`, `/connect`, `/query-database`, etc.) or colon-namespaced forms (`/agami:init`, etc.) — those don't exist. **`/agami-init` was folded into `/agami-connect` Phase 0a** — credential setup now lives there. For chat replies, **prefer natural language over slash commands** — it reads better and the skill's `when_to_use` matcher routes correctly: - Re-introspect the schema → "say 'reload the schema'" or "say 'reintrospect my database'" - Save a correction → "say 'save this as a correction'" or "say 'remember this'" - Ask a data question → just type the question - Set up agami / switch profiles → `/agami-connect` (the one place the slash form is genuinely cleaner than natural language — agami-connect handles credentials too via Phase 0a) ## Conversation style - **One question per turn unless they're truly bundled.** - **A decision is a card** ([`shared/question-cards.md`](../../shared/question-cards.md)): every answer ends its turn on a typed-reply line, never a card (Phase 4d.2); large-table HIGH-risk approval, the post-install GitHub-star ask. **Do NOT use AskUserQuestion for follow-up suggestions** — those are 5 plain numbered bullets per Phase 4f. - **No page opens by default.** The chart page is the answer's full view: rendered only when the person asks for a chart (Phase 4e), and served from localhost, never opened as a `file://` page. - **Insights, not narration** — lead with the answer ("Carol Chen has the highest spend at $148.95"), not the SQL or the process. - **Round numbers in prose**, exact in the table. - **Don't echo the SQL in chat prose** — that's enforced as a hard rule in Phase 2. Don't paste the raw Bash CSV — Phase 3. --- ## Phase −1: Plan-mode check Run the detection + ask logic from [`shared/plan-mode-check.md`](../../shared/plan-mode-check.md). agami-query needs Bash (SQL execution) and Write (chart HTML) — both are blocked in plan mode. **If plan mode is active and the user picks `Stay in plan mode`:** - **Reopen-last-chart intent** (Phase 2a.1 below) — re-displaying an existing HTML chart only needs `Read` plus serving the file with `serve_page.py` (run with `run_in_background`). Run that flow if matched. - **Anything else** — refuse and end the turn. **DO NOT write a plan file. DO NOT call `ExitPlanMode`.** Refusal text (verbatim): > I can't run SQL in plan mode. Switch to **Auto** or **Edit Automatically** mode (Shift+Tab to cycle) and re-invoke me. If plan mode is not active, skip this phase silently and go to Phase 1. --- ## Phase 1: Setup (once per session) ### HARD RULES — connection rules These are non-negotiable. 1. **Connect ONLY to the host/port/database/user/password in `<artifacts_dir>/local/credentials`** — the sole credential source (no env-var bypass). Never substitute `localhost` or any other host as a fallback. Never connect to anywhere not in the credentials. 2. **Never ask the user for connection details in chat.** If credentials are missing, stop and invoke `/agami-connect` — its Phase 0a runs the DB-type picker, writes `<artifacts_dir>/local/credentials.example`, and ends the turn for the user to fill it in. 3. **Never scan or guess.** No `pgrep`, no `ps`, no `find /`, no `ls /Applications/Postgres.app`, no listing port-listeners. The only Bash probes allowed during setup are `which <tool>` for a database tool on `PATH` and `bash "${CLAUDE_PLUGIN_ROOT}/scripts/py" -c 'import <module>'` for a Python driver. 4. **NEVER put the password (or any credential field) in a Bash command line.** That includes `export PGPASSWORD='<value>'`, `export MYSQL_PWD='<value>'`, `psql -W <password>`, `mysql -p<password>`, or any heredoc / stdin form that interpolates the password. Hosts render Bash tool calls as collapsibles in their UI — anything in the command becomes visible in the chat. Use the auth files generated by `scripts/setup_pgauth.py` (see [`shared/connection-reference.md → HARD RULES`](../../shared/connection-reference.md)). For native CLI queries the visible Bash command is `PGPASSFILE=<artifacts_dir>/local/.pgpass psql -h ... -U ... -d ... -c "$SQL" --csv`. For the Python driver path use `bash "${CLAUDE_PLUGIN_ROOT}/scripts/py" -m execute_sql --sql-file ...`. These rules apply to every phase of this skill, not just Phase 1. ### 1a — credentials check (binding) Read `<artifacts_dir>/local/credentials`. If the file (or the active profile's section) is missing, invoke `/agami-connect` (its Phase 0a handles first-time credential setup) and **stop this skill**. Do not continue to load the semantic model. Do not run any other Bash commands. ### 1b — load the semantic model Resolve `<profile>`: `AGAMI_PROFILE` → `active_profile` in `<artifacts_dir>/local/.config` → `"main"`. Resolve `<artifacts_dir>` per [`shared/file-layout.md`](../../shared/file-layout.md#configuring-artifacts_dir): `AGAMI_ARTIFACTS_DIR` → `.config.artifacts_dir` → `$HOME/agami-artifacts`. Then create the scratch folder every later step writes to: `mkdir -p "<artifacts_dir>/local/tmp"` (Claude's Write tool and a `>` redirect both need it to exist). The model is the semantic-model tree at `<artifacts_dir>/<profile>/` (`datasource.yaml` + `subject_areas/<area>/…`). There is no legacy-layout fallback — the model is the only format. **Never hand-read OR hand-roll the model.** Don't `cat`/`Read` `datasource.yaml`, `subject_areas/**`, `tables/*.yaml`, or `relationships.yaml`, and **never write a `python -c` / ad-hoc script to load, dump, or walk the model tree** — that guesses the schema, breaks on a wrong key, and can leak a traceback to the user. The CLI returns the same data structured, and the layout is already known (relationships + entities + metrics live at the **area** level, not inside a table file). `sm areas "$ROOT"` is the **one-call model map**: per area it returns `table_count`, `entity_count`, `metric_count`, `relationship_count` + description — the whole shape in a single call. (Column-level detail → `sm context`; browsable table/column tree → `sm model-tree`.) **When the user asks "what does the model look like" / "show me the model," run `sm model-tree` (or open `/agami-model`) — don't improvise Python.** **Don't run a separate existence probe either** (no `ls datasource.yaml`, and never probe for the plugin's own scripts — `sm`, `execute_sql.py`, `semantic_model/` always ship with the plugin). That same first `sm areas` call doubles as the check: model present → you get the map; absent → the CLI returns `{"error":"no_model"}` with **exit code 3** → invoke `agami-connect` and stop, unless `AGAMI_PROFILE` names a database with no model ([`shared/file-layout.md`](../../shared/file-layout.md#when-agami_profile-names-a-database-with-no-model)): then say so in one line and stop. **If an `sm` call exits 12, agami's library is older than the plugin:** run `bash "${CLAUDE_PLUGIN_ROOT}/scripts/sm" install` (the person approves it), then repeat the call. It is not "no model" (exit 3): never send the person to agami-connect for it. Drive everything through the CLI — the `sm` wrapper resolves the interpreter + deps. (These granular steps are CLI operations; on the MCP surface they're folded into the smart `get_datasource_schema`, which advertises the 4 product tools — so don't invoke the steps below as MCP tools.) ```bash ROOT="<artifacts_dir>/<profile>" bash "${CLAUDE_PLUGIN_ROOT}/scripts/sm" areas "$ROOT"; rc=$? # subject-area index; rc 3 = no model → agami-connect bash "${CLAUDE_PLUGIN_ROOT}/scripts/sm" context "$ROOT" --area A --tables t1 t2 # compound table context bash "${CLAUDE_PLUGIN_ROOT}/scripts/sm" examples "$ROOT" --area A --query "…" # examples-first ranking ``` The model loader already drops `review_state: rejected` entries from what it serves and applies the area's `expose_column_groups` scoping, so you never see excluded tables/columns/relationships. (Rejections are the curator's choice via `/agami-model` — surfaced nowhere.) When a query would touch a `stale` entry, warn once. A stale metric, entity or join was approved and then edited, so it is not signed off: *"This uses `<entity>`, which changed since it was approved and needs re-approving in /agami-model."* A stale table or column is schema drift: *"This would use `<entity>`, marked stale (schema drift). Run /agami-connect to re-introspect, then /agami-model to reconcile."* ### 1c — what the model gives you You don't build hand-rolled indexes — the loader returns structured objects. The pieces you'll use during SQL generation: - **Subject areas** — the primary scoping unit (replaces "load every table"). Each has a description, a table list, entities, metrics, and an intra-area relationship graph (each edge carries join **cardinality** + a trust block). - **`get_table_context(area, tables)`** — columns (scoped by `expose_column_groups`), `default_filters`, relationships, `caveats`, `value_transforms`, metrics — in one call. - **Entities** — the vocabulary users say (name/plural/other_names → `maps_to` table.column, with a `value_pattern` for opaque IDs). Use `resolve_entities` / `identify_entity`. - **Metrics** — reusable aggregations with prose `calculation` + per-dialect `bindings`. **Use the binding SQL VERBATIM** when the user asks for a metric by name or synonym; don't hand-roll the aggregate. A metric with `joins` says how its tables join: bring each named table in with that kind (`left` → `LEFT JOIN`, `inner` → `JOIN`) on the declared relationship's key, or the receipt cannot credit the metric. - **Cross-subject-area relationships** (org level) — for joins that span two areas. ### 1d — load the examples library Examples live per subject area at `<artifacts_dir>/<profile>/prompt_examples/<area>/examples.yaml`. Use `cli examples "$ROOT" --area <area> --query "<question>"` to rank them (the **examples-first** signal — step 2a). If a high-confidence match returns, mirror its tagged tables/columns/SQL shape and skip cold-start resolution. If there are no examples for the relevant area → warn: "I don't have few-shot examples for this database yet — answers may be lower quality. Say 'introspect the schema' to seed them." (Slash form `/agami-connect` only if the user asks "what do I type?".) ### 1d.1 — load USER_MEMORY.md Read `<artifacts_dir>/USER_MEMORY.md` (if present). Strip HTML comments (`<!--...-->`), then keep the rest. If the file is missing, treat it as empty — never error. See [`shared/user-memory-format.md`](../../shared/user-memory-format.md) for what's in it. This file holds free-form **user preferences across every database** (default filters, display preferences). Inject it into the SQL-generation prompt in Phase 2b under a labeled `## User memory (preferences and policies)` section — the LLM uses it as steering context. ### 1d.2 — load domain context Run `cli org-context "$ROOT"` — it returns the **full** domain context for this database in one block: the human's datasource.md narrative (HTML comments stripped) **plus** the model-derived summary that the file does NOT contain — subject areas, conventions, and the **decoded glossary** (`key_terminology` + enum legends), assembled fresh from the structured model. Don't `Read` datasource.md by hand: the file holds only the human narrative; the glossary and summary live in the model, and this command is the one that combines them. If there's no model, treat as empty — never error. See [`shared/organization-context-format.md`](../../shared/organization-context-format.md). Inject the result into the SQL-generation prompt in Phase 2b under `## Datasource context`, **before** the `## User memory` section — domain knowledge precedes display preferences in the LLM's reading order. Order in Phase 2b prompt: 1. Schema context (tables / columns / relationships / metrics from the semantic model) 2. `## Datasource context` ← from `cli org-context` (narrative + derived summary + glossary) 3. `## User memory (preferences and policies)` ← from USER_MEMORY.md 4. Few-shot examples 5. The user's question ### 1e — the connection invocation pattern (do NOT run a standalone probe) Look up the cached connection method from `<artifacts_dir>/local/.config`. **Do NOT run a separate `SELECT 1` connectivity probe** — it's a wasted round-trip (and pointless for a local SQLite/DuckDB file). The user's *actual* query is the connectivity check: run it directly, and if it fails, classify the error via [`db_error_classifier.md`](../../shared/db_error_classifier.md). The table below is the **exact invocation pattern per tier** for running that query — **don't guess flags** (`execute_sql.py` does NOT accept positional SQL, a `--format` flag, or any flag not listed; guessing produces "unrecognized arguments" errors that waste turns). The `SELECT 1` in each row is only a placeholder for *your* SQL. | tier | invocation pattern (substitute your SQL for `SELECT 1`) | |---|---| | `cli` (postgres) | `PGPASSFILE="<artifacts_dir>/local/.pgpass" psql -h <host> -U <user> -d <db> -c 'SELECT 1' --csv` | | `cli` (mysql) | `mysql --defaults-file="<artifacts_dir>/local/.mysql.cnf" --defaults-group-suffix="_<profile>" -e 'SELECT 1' --batch` | | `cli` (snowflake) | `snowsql --config "<artifacts_dir>/local/.snowsql.cnf" -c "<profile>" -q 'SELECT 1' -o exit_on_error=true -o output_format=csv -o friendly=false` | | `cli` (sqlite) | `sqlite3 -header -csv "<path>" 'SELECT 1'` — **always `-header`**, or result CSVs lose column names and `format-table` treats the first data row as the header (a wasteful re-export). | | `duckdb` (any) | `duckdb -init "$init_file" -c 'SELECT 1' --csv` (see `build_duckdb_attach.py` for `$init_file`) | | `python` (all DBs) | `AGAMI_PROFILE="<profile>" bash "${CLAUDE_PLUGIN_ROOT}/scripts/py" -m execute_sql --sql 'SELECT 1'` | A profile whose `password` is a `keyring:` reference always runs on the `python` tier (`python -m execute_sql`), never psql / mysql / snowsql: `setup_pgauth.py` writes no auth file for it, because those files are plain text. When such a profile's password has changed, or the error says to reconnect with `--replace`, change the saved password with `bash "${CLAUDE_PLUGIN_ROOT}/scripts/py" connect_helper.py --spec <spec.json> --replace`: the spec is `{"profile", "db_type", "fields"}` taken from the profile's current section (every key except `type` and `password`), and the user types the new password on the page the helper's first JSON line points to, never in chat. If the new password does not connect, the helper puts the old one back. The Python tier's CLI is **`--sql <string>`** or **`--sql-file <path>`** — those are the only two ways to pass SQL. Optional flag: `--profile <profile>` (overrides `AGAMI_PROFILE` env). **Output is RFC-4180 CSV on stdout**, always — no `--format` flag exists. If you need JSON, post-process the CSV. Route any error through [`shared/db_error_classifier.md`](../../shared/db_error_classifier.md). Common cases: - `auth` / `dsn` → credentials may have rotated; point at `<artifacts_dir>/local/credentials`. - `network` → check VPN / DB endpoint reachability. - `sign_in_required` → the person's own sign-in expired; relay the message (sign in again, new conversation). - `driver_missing` → fall through to the next available method. If the cached method doesn't work, re-run tool detection per [`agami-connect/SKILL.md → Phase 0a.5`](../agami-connect/SKILL.md#0a5--tool-detection). --- ## Phase 2: Generate SQL ### HARD RULE — never echo SQL in chat prose The generated SQL belongs in two places only: (1) the Bash invocation that executes it (which the host shows as a collapsible tool call — outside our control), and (2) the collapsible "SQL" section of the chart page, when the person asks for one (Phase 4e). **Never paste, quote, or summarize the SQL in the assistant's narrated text.** No `SELECT ...` lines, no fenced ```sql blocks, no "I'm running this query: ..." prose. Users who want the SQL see it in the SQL details element of the chart page. This rule applies to every retry, every fallback, every regenerate. The chat prose stays focused on approach, fetching, and insight. ### 2a — classify the input Check intents in this order. The first match wins; only that branch runs. 1. **Reopen-last-chart intent** (handled in 2a.1 below). Triggered by short messages that ask to re-display the most recent chart without re-running SQL. Trigger phrases: - "reopen", "reopen the chart", "reopen that" - "open the last chart", "open that again", "open my last report" - "show me that chart again", "show me the last chart", "show that" - "open the previous chart", "show that report" - Any message ≤ 8 words that combines an open-verb (open / show / see / view / display) with a chart-noun (chart / report / it / that / last / again). If matched → jump to **2a.1** and skip Phases 2b–4. 2. **A question** (contains `?` or starts with how/what/show/list/which/count/give/get/find/total/average/top/which AND isn't matched by the reopen intent above) → save it as the user's data question. Continue to 2b. 3. **Empty** → ask the user; suggest 2-3 questions from the model's `ai_context.examples` if present, or inferred from `datasets[].description`. 4. **Flag-only** (`--csv` / `--chart bar`) → re-run the previous query with the flag applied. 5. **Follow-up like "make that a chart"** → the person asked for a chart: Phase 4e on the last answer. ### 2a.1 — Reopen-last-chart flow (no new SQL) If the user's intent is to re-display the most recent chart: 1. Read `<artifacts_dir>/local/query_log.jsonl` (each line is a JSON object) and take the last non-empty line **whose `status` is `"ok"` or absent** — the log also records refused and failed executions, and neither of those rendered a chart. Entries written before the log carried a `status` field have none; treat those as `ok`. 2. Look at the `chart_path` field. Possible cases: - **`chart_path` set AND the file exists on disk** → serve it from localhost, `bash "${CLAUDE_PLUGIN_ROOT}/scripts/py" serve_page.py "<path>" --minutes 30` with `run_in_background`, and read its first line, `{"url": …}`. Surface a one-liner in chat: ``` Reopened: http://localhost:<port>/<name> (open for 30 minutes) ``` Done. Skip every other phase. Don't re-execute SQL. Don't re-render. Don't add 5 follow-ups (this is a UI action, not a fresh answer). - **`chart_path` is null** (the last answer was never shown as a chart) → render it now: Phase 4e on the last answer, the same as "Show as a chart". A 1×1 scalar has nothing to chart: surface "The last answer was a single number, so there's no chart to show." - **`chart_path` set but the file is missing** (user deleted `<artifacts_dir>/local/charts/<profile>/`) → surface: "The chart file is gone — `<path>` no longer exists. Ask me the question again and I'll regenerate it." - **Query log empty or missing** → surface: "I don't have any prior queries to reopen. Ask me a question first." This phase neither logs anything new to `query_log.jsonl` nor sends telemetry — re-opening an existing artifact isn't a query event. ### 2b — assemble the prompt via the examples-first traversal For a single profile, follow the **examples-first canonical loop** — the subject area is the scoping unit, so you never dump the whole schema. (Cross-profile federation is **2b.federation** below; it's orthogonal to this loop.) **Step 1 — pick the subject area(s).** `cli areas "$ROOT"` → choose the area(s) whose description matches the question's intent. Most questions touch one area; cross-area ones (a join spanning two areas) select both, and the org's `cross_subject_area_relationships` supply the join. **Step 2 — examples first (strongest signal).** `cli examples "$ROOT" --area <area> --query "<question>"`, adding `--metric "<name>"` for each model metric the question names, so the examples tagged with it rank first. `--metric` alone, without `--query`, lists only the examples tagged with that metric. If `high_confidence` is true, mirror the top match's tagged tables / columns / metrics / SQL shape and jump to step 5 — skip cold-start resolution. **Step 3 *(cold start only)* — resolve entities + metrics + opaque literals.** Match the question's terms to the area's entities (and `metrics`). For any opaque literal in the question (an ID-looking token), the CLI entity matching recognizes its type via `value_pattern` (folded into `get_datasource_schema` on the MCP surface, not a separate tool); if it returns `clarify`, ask the user one targeted question rather than guessing. **Step 4 *(cold start only)* — choose tables + columns** from what resolved (entity `maps_to`, metric `source_tables`). **Step 5 — compound context fetch.** `cli context "$ROOT" --area <area> --tables … [--columns …]` returns columns (scoped by the area's `expose_column_groups` — wide tables disclose only their exposed groups), `default_filters`, relationships (with cardinality + review state), `caveats`, `value_transforms`, and metrics, in one round-trip. **Step 6 — assemble the generator prompt** in this order, then produce ONE SQL statement (first statement only if several are emitted): 1. **System** — "Write one valid SQL statement for `<DB_TYPE>` (ANSI_SQL + `<DB_TYPE>` tweaks per dialect-rules.md). Output ONLY SQL. Prefer indexed/`recommended_filters` columns on large tables. Apply each column's `value_transform` when selecting/filtering it. **A `sensitive` column is the model author asking for care, not a locked door.** Prefer using it in `COUNT`/`COUNT(DISTINCT …)`, `GROUP BY`, `WHERE`, and `JOIN` over projecting its raw per-row values. So: (a) 'how many unique customer emails?' → `SELECT COUNT(DISTINCT email)` and report the count. (b) To disambiguate identical display labels (two customers with the same name), put the entity's **non-sensitive key (`id`)** in the output rather than the raw email/phone. (c) When the question genuinely needs the values — 'I need the mailing list' — project them and **say in the answer that you did**; the receipt records it too. Nothing refuses this, so the care is yours to exercise and yours to be transparent about. A column that must never be readable is not in the model at all, and any statement naming it is refused as out of scope. Use a metric's `bindings` SQL VERBATIM when the question names that metric (or a synonym)." 2. **Schema context** — the `get_table_context` output for the chosen tables (columns + types + caveats + value_transforms), the area's relationships (rendered as `from.col → to.col [cardinality]`), and the area's metrics (`<name>: <binding> -- <calculation>` + synonyms). **`default_filters` ARE yours to apply.** Nothing applies them for you — but the receipt DOES report whether you did, per table reference, so an omission is visible to the user in the report beside the answer. If a table declares one and the question does not deliberately ask about the rows it excludes, write it into the `WHERE` clause. Two things to watch, because `get_table_context` has already rewritten them: the `{alias}` placeholder is gone — each filter comes back qualified with the **bare table name**, so re-qualify it to whatever alias you actually used, or the database rejects the statement. And a filter that still carries a `:param` marker (e.g. `orders.tenant_id = :tenant_id`) has no value to bind — leave it OUT and say so in the answer rather than emitting SQL that won't parse. Also DO honor any caveats, and each column's `placeholders`: a value a person said means `unknown`, `current` or `not_set`, written as [sql-generation-rules.md § 11](../../shared/sql-generation-rules.md#11-placeholder-values) says (nothing filters it for you). **Unreviewed metrics are USED, not refused.** When the question names a metric whose `review_state ≠ approved`, still use its binding and answer — do NOT block or refuse on it. The trust layer surfaces it **on the receipt**, not as a hard gate: the metric rides on the `receipt.columns.items` entry for the output column that computes it (`kind: "output"`, `status: "matched"`) carrying its own `review_state`, and "How this was answered" (and the chart page's approve/change banner) is driven off exactly that field (Phase 4d.1). The loader already drops only `rejected` metrics; an `unreviewed`/`proposed` one is yours to use, with the surfaced review state carrying the honesty. (Same for unreviewed joins/entities and `stale` entries — surface, never refuse.) 3. **Datasource context** — `datasource.md` (step 1d.2), heading `## Datasource context`. Binding domain context. 4. **User memory** — `USER_MEMORY.md` (step 1d.1), heading `## User memory (preferences and policies)`. 5. **Few-shot examples** — the ranked matches from step 2. 6. **User question.** **The fan/chasm safety pass runs as a pre-execution step, on every tier.** Before you execute the generated SQL, pass it through `sm prepare "$ROOT" --area <area> --sql-file <path>` (Phase 3a). It runs the fan-trap / chasm-trap pre-flight and the aggregation-semantics checks, and returns the SQL to actually run, which is always the SQL you gave it: this command never rewrites your statement and never refuses it. What it returns alongside is `findings` — see Phase 3a for how to act on them. Tier-independent — works whether you execute via psql, the Python driver, or DuckDB — so the checks never depend on the execution path. It does **not** apply the area's `default_filters`, and nothing else does either: those are declarative only, so put any you need into the statement yourself at step 6, and `sm prepare` emits no `applied_filters` key of its own (an always-empty list would read as "we checked, none applied"). Which ones your finished statement satisfied is settled later, by `sm receipt`, on `tables.items[].filters` (Phase 4d.1). The findings DO ride on the receipt too, in its `aggregates` section, so the panel draws them beside the answer. #### 2b.federation — cross-database queries When the question references datasets from ≥ 2 different profiles (e.g., ITSM in Redshift × finance in MySQL), the skill routes the SQL through DuckDB, which ATTACHes both databases in one session and runs a native federated JOIN. **Detecting federation.** Extend Pass 1 of the two-pass retrieval to pick `(profile, schema, table)` tuples instead of just `(schema, table)`. If the picked set spans `len({tuple.profile}) > 1`, federation mode is on. For small databases (under 50 tables), build the union of every profile's index up front and run Pass 1 against that combined index — the picker then chooses across profiles automatically. For larger setups Pass 1 already runs; just include `profile` in each entry. The Pass 1 prompt loads `<artifacts_dir>/local/cross_profile_relationships.yaml` (if present) so the picker knows about declared cross-profile JOIN paths. If the file is missing, the picker falls back to inferring relationships from column-name/type matching across profile indexes — best-effort, with a warning to the user that confidence is lower. **`<artifacts_dir>/local/cross_profile_relationships.yaml`** (optional) — declares known JOIN paths across profiles: ```yaml version: "0.1.1" relationships: - name: itsm_assets_to_finance_cost_centers from_profile: itsm from_dataset: public.assets from_columns: [department_id] to_profile: finance to_dataset: dbo.cost_centers to_columns: [dept_id] description: ITSM assets carry the same dept_id as finance cost centers. ``` Loaded at session start the same way per-profile indexes are loaded. **Building the federated SQL.** When federation mode is active, the schema-context section of the prompt uses **three-part dataset names** matching the DuckDB ATTACH alias: `<profile>.<schema>.<table>` (e.g., `itsm.public.assets`, `finance.dbo.cost_centers`). Cross-profile relationships from `cross_profile_relationships.yaml` are rendered alongside per-profile relationships. The model produces SQL using these three-part names. **Verifying DuckDB is available.** Look up `tool_paths.duckdb` from `<artifacts_dir>/local/.config`. If missing, surface: ``` Cross-database queries need DuckDB. Install it with `brew install duckdb` (or apt / download) and re-run. ``` …and stop. **Verifying credentials are set up for every profile.** For each profile in the picked set, check that the corresponding auth file exists (`<artifacts_dir>/local/.pgpass`, `<artifacts_dir>/local/.mysql.cnf`). Missing → run `bash "${CLAUDE_PLUGIN_ROOT}/scripts/py" setup_pgauth.py --profile <profile>` for each gap, then re-check. **Generating the temp init file.** ```bash init_file=$(bash "${CLAUDE_PLUGIN_ROOT}/scripts/py" build_duckdb_attach.py \ --profiles "$P1" "$P2" | tr -d '\r') # Windows Python ends lines \r\n # init_file is the path of a chmod-600 file in <artifacts_dir>/local/.duckdb_init_*.sql. # Credentials are inside that file, NOT on the command line. ``` **Running the SQL.** ```bash duckdb -init "$init_file" -c "$FEDERATED_SQL" --csv ``` The visible Bash command shows only the path — DuckDB reads the ATTACH credentials silently from the init file. **Tear-down.** After the query completes (success or failure), delete the init file: ```bash rm -f "$init_file" ``` The next invocation also self-cleans any `.duckdb_init_*.sql` older than 1 hour in case a prior run crashed: ```bash find "<artifacts_dir>/local" -maxdepth 1 -name '.duckdb_init_*.sql' -mmin +60 -delete 2>/dev/null ``` **Performance warning.** Federated joins through DuckDB scanners are bounded by network round-trips. If both sides of the join estimate to > 100k rows, surface a one-liner before running: > This federated query may take 30–120s (network round-trips for `<P1>` × `<P2>`). Want to tighten the filter first? Options: `Run anyway (Recommended for one-off)` / `Let me add a filter` / `Cancel`. **Type alignment.** Postgres `numeric(10,2)` joined with MySQL `decimal(10,2)` works. Mismatched types (date vs string, integer vs uuid) need an explicit `CAST` in the generated SQL. The Phase 2b prompt instructs the LLM about this: > When joining across profiles, prefer explicit `CAST(<col> AS <type>)` for any pair where the types might differ (e.g., timestamps stored as strings on one side, dates on the other). **No Snowflake federation.** DuckDB's `snowflake_scanner` is experimental and not packaged with the standard binary. If a profile in the picked set has `db_type=snowflake`, `build_duckdb_attach.py` exits with a clear error: surface it to the user and suggest pre-aggregating one side as a CSV. **Round what a person reads, in the SQL.** A rate, a percentage or an average comes out of the database with a dozen decimals (`79.6501901140684411`), and the table formatter shows numbers exactly as they arrive, on purpose. So wrap a rate or percentage in `ROUND(…, 1)` and an average or money value in `ROUND(…, 2)` in the statement itself. Never round a count or a sum of whole things. A metric wrapped in `ROUND` still matches its signed-off definition on the receipt. ### 2c — safety checks Apply [`shared/sql-generation-rules.md`](../../shared/sql-generation-rules.md): - **No DDL/DML.** Refuse on `DROP`, `DELETE`, `INSERT`, `UPDATE`, `ALTER`, `TRUNCATE`, `CREATE`, `GRANT`, `REVOKE`. Regenerate with explicit "SELECT only" framing. - **No system tables.** Refuse on `pg_catalog`, `information_schema`, `mysql.*`, `sys.*` unless the user is explicitly asking about schema metadata. - **NULL-safe division** via `NULLIF(denominator, 0)`. - **`agami.type` consistency** — if the SQL applies a numeric aggregate (`SUM`, `AVG`) to a field whose `agami.type` is `string` or `boolean`, refuse and regenerate. Type info exists for a reason. ### 2d — risk assessment + time estimate for large tables For each dataset touched by the SQL, look up its `agami.performance_hints`: `recommended_filters` is a **list of column names** (introspection seeds it with a large table's date/time columns — the columns worth filtering on to avoid a full scan). Check whether the generated SQL's WHERE filters on **any** of them. - `estimated_row_count > 1_000_000` AND the WHERE filters on **none** of the table's `recommended_filters`: → **HIGH risk**. Surface a banner before executing — **name a suggested column** when one exists: "This query scans the <table in words> table (~<row_count> rows) with no filter on <recommended_filters[0] in words>. Estimated time: <est>. Narrow it — e.g. a date range on <recommended_filters[0] in words> — or proceed anyway?" (*"…scans the Order items table (~4.2M rows) with no filter on Created at…"*: names in words, never `order_items.created_at`, per [question-cards rule 3b](../../shared/question-cards.md#3b-things-are-named-in-plain-words)). AskUserQuestion: `Add a filter` / `Proceed anyway` / `Cancel`. If `recommended_filters` is **empty** (no known good filter for this table), use the generic "…without a filter. Add one, or proceed?" wording. - `100k–1M` rows with no filter on a `recommended_filters` column → **MEDIUM**. Note in response footer; proceed. - A query that **does** filter on a `recommended_filters` column → treat as narrowed: drop a risk tier (don't HIGH-warn just because the table is big). Otherwise → **LOW**. Proceed silently. **Time estimate (announced BEFORE Phase 3 execution).** Long-running queries kill the user's confidence — they don't know if the skill is hung or actually working. Before running any non-LOW query, surface a one-liner with the rough wall-clock estimate so they can wait without anxiety: ``` Running this against ~12M rows in <dataset> — estimated 30–90s. I'll narrate when results land. ``` Estimation table (rough, calibrated to common Postgres / Snowflake shapes — adjust as needed from the latency log over time): | Largest scanned dataset | With indexed filter (`WHERE` matches `agami.performance_hints.indexes`) | Without indexed filter (full scan) | |---|---|---| | < 100k rows | < 1s | < 2s | | 100k–1M | 1–5s | 5–30s | | 1M–10M | 5–15s | 30–120s | | 10M–100M | 15–60s | **2–10 min** — ALWAYS warn even if filter is present | | > 100M | 30–120s | **> 10 min** — block as HIGH risk; offer to add filter or sample | Snowflake-specific: add 5–30s on top of any estimate for warehouse spin-up if the warehouse has been idle (the query log can detect "first query in this session" → assume cold). Federation (Phase 2b.federation) doubles or triples estimates due to network round-trips — surface "this federated query may take 30–120s" before running, regardless of estimated_row_count. If the estimate exceeds 30s, also surface: "Cancel anytime — Ctrl+C in CLI, or just send another message." The user should know they're not trapped waiting. --- ## Phase 3: Execute ### HARD RULE — never paste raw output in chat The Bash result (CSV stdout, stderr, exit code) is for the skill to parse, not for the user to read. **Never paste the raw CSV / TSV from the Bash result into the assistant's response text.** No "Here's what came back: …", no markdown code-fence dumps of the result. Parse internally, then surface the polished output per Phase 4. The host shows the Bash tool call as a collapsible — that's enough provenance for users who want to dig. ### 3a — safety pass, then run the SQL **Step 1 — prepare (every tier).** Write the generated SQL to a temp file, then run the tier-independent safety pass: ```bash bash "${CLAUDE_PLUGIN_ROOT}/scripts/sm" prepare "$ROOT" --area <area> --sql-file "<artifacts_dir>/local/tmp/agami-q.sql" ``` It returns JSON: `{sql, findings, units}`, and it **always exits 0**. The returned `sql` is the statement you handed in, byte for byte — this command never rewrites and never refuses. (`units` is the `{output_column: unit}` map traced through that statement — keep it for the table render in 4d.) `findings` is a list, usually empty. Each entry is `{risk, reason, triggering_joins}` and each one is a **fact about the statement**, not a verdict on it: - `fan_trap` / `chasm_trap` — a join multiplies the rows an aggregate is computed from. `triggering_joins` names which join does it. - `fan_out_invariant` — the same multiplication, on an aggregate it cannot move (`MIN`, `MAX`, a `DISTINCT` one, `BOOL_AND` / `BOOL_OR`). The rows were duplicated; the number is the same either way. `triggering_joins` names the join, exactly as above. - `bad_aggregation` — a `SUM` of a rate or an identifier, or an `AVG` of one. - `semi_additive` — a `SUM` of a balance across a time grain, which multiplies a stock. **A finding is not a refusal, and it is not automatically a bug.** Whether a multiplied total is wrong depends on what was asked: the same statement is wrong for order revenue and right for line-item exposure. The pre-flight does not have the question. You do. So this is the point where the judgement gets made, out loud, by you: - **The finding is not what the user meant** — restructure and re-prepare. Typically: pre-aggregate each measure in its own CTE and outer-join them (chasm), move the aggregate into a window function to keep raw rows, or drop a join the query does not otherwise use (fan, where the many side is touched nowhere but the `ON` clause). **Say in the answer** that you restructured to avoid a `<risk>`. The restructured statement carries no finding, so the receipt says nothing about it — that sentence is yours to write, and it is what separates this from a guard silently swapping the statement underneath the user. - **The finding IS what the user meant** — run it and say so. "This counts each order once per line item, which is what 'line-item exposure' asks for." A correct answer to the question asked is not a defect, and refusing it would have been the wrong call. - **You cannot tell which** — the fan-out join also filters or groups, so the candidate readings return *different numbers* (e.g. "loans with ≥1 payment since January" versus a payment-weighted total). Do **not** pick one. Ask: a short "Did you mean…?" with 2–3 concrete interpretations, one plain-language sentence each, no SQL. Generate for the one they choose. Either way the finding rides on the answer's receipt, in the `aggregates` section, so a user reading the result can see what was found without taking your word for it. **An empty `findings` is not by itself a clean bill of health.** Check `unchecked` alongside it: it is `null` when the checks ran, and a sentence when they could not — sqlglot missing, the statement unparseable, or no SELECT in it. All three yield the same empty list a genuinely clean statement does, so a non-null `unchecked` means you have learned nothing about this statement's aggregates and should say so rather than implying it passed. Even when `unchecked` is null, "found nothing" is bounded. The receipt's `aggregates` marker states what the checks do not reach — an aggregate inside a CTE or a subquery, one in `HAVING` or `ORDER BY` — and you should surface that marker rather than presenting an empty section as clean. **Step 2 — execute the returned `sql`** via the tier's tool from [`shared/connection-reference.md → CLI Connection Commands`](../../shared/connection-reference.md#cli-connection-commands) — psql / mysql / snowsql / sqlite3 / DuckDB, or the Python driver. (If you use `python -m execute_sql`, do **NOT** pass `--no-safety`. `sm prepare` runs the fan/chasm and aggregation checks, which report; it does **not** run the table-scope, `SELECT *` or column-scope gates, which refuse. Those are the enforcement, they live only in `execute_sql`, and skipping them lets a hallucinated table or column reach the warehouse. The checks that would be doubled are cheap; the gates that would be skipped are not optional.) Wrap in a high-resolution timer; capture stdout (CSV rows), stderr (errors), exit code. Route a non-zero exit through the error classifier (Phase 3b). ### 3b — error handling + auto-retry Route any non-zero exit through [`shared/db_error_classifier.md`](../../shared/db_error_classifier.md). Behavior per kind: | `error_kind` | Behavior | |---|---| | `auth`, `dsn`, `network` | Stop. Surface the one-line remediation. No retry. | | `driver_missing` | Fall through to the next available method (native CLI → DuckDB → Python driver). | | `permission` | Stop. DB user lacks SELECT on the touched dataset. | | `sign_in_required` | Stop. The person's own sign-in expired or is missing — relay the message (sign in again, then start a new conversation). Nothing is broken; don't retry. | | `column_not_found`, `table_not_found`, `syntax` | Auto-retry up to **2** times. Pass the error back to the SQL generator: "The previous SQL failed with `<one-line classifier message>`. Regenerate using only table / column names from the schema context above." | | `other` | Stop. Surface raw error truncated to 200 chars. | A statement stopped for taking too long no longer arrives here as an error. A per-statement deadline is a **`resource_limit` refusal**, which carries its own remediation because it is a decision the server made — handle it as a refusal, not as a classified failure. The `timeout` failure kind now has one producer, the supervisor stopping an executor that never responded, and there is nothing query-specific to suggest for it. After 2 retries with no success, stop. Don't loop. ### 3c — parse rows Parse the CSV stdout. Header row = column names. Body rows = data. **Sanitize column headers before display.** SQL aliasing slips happen — bare `n`, `cnt`, single-letter columns, `?column?` (Postgres unaliased), and shouty all-uppercase Snowflake names are common. Apply this header transform **once** before any rendering surface (4d markdown, the 4e chart page, chart axis labels): | Raw header | Display header | Why | |---|---|---| | `n`, `N`, `cnt`, `CNT` | `Count` | Bare counts are unreadable; "Count" is universal | | `?column?` (Postgres unaliased), `_col0`, `_col1` | Drop the column entirely from the visible output (parse warning to log: "unaliased column in result; SQL generator should always alias") | | Single uppercase word (Snowflake default — `STATUS`, `AMOUNT`) | `Status`, `Amount` (title-case the word, lowercase trailing letters) | | Snake_case (`order_count`, `customer_name`) | Title-case with spaces (`Order Count`, `Customer Name`) — except when ending in a unit (`_amount` → `_amount` raw is OK if unit shows in the header parens, like `Avg Outstanding (INR)`) | | Already title-case or sentence-case | Leave as-is | The header transform is purely cosmetic — the underlying alias stays in the SQL and the `tables_used` log. Don't rename the column in the result data; only its rendered label. Format every cell per its column's `type` and **`unit`** (both come from `get_table_context` — `unit` is the structured currency/unit set during onboarding's currency ask or by a correction). For a metric result column, use the **metric's** `unit`. The canonical mapping is `semantic_model/units.py` (`format_value`) — the table below mirrors it; when in doubt, match it. The same formatting applies to **every** downstream surface — chat markdown table (4d), HTML `table_rows` (4e.iii), AND chart `labels`. **Also pass the value column's `unit` into each chart section's `"unit"` field** so the chart's y-axis + tooltips format the symbol + grouping deterministically (the chart template applies it; you don't hand-format chart axes — and the `datasets` data stays RAW numbers). **Numbers are formatted by code, not by you.** The chat markdown table (4d) is rendered by `sm format-table` and the chart by the template — both via `units.py`, in full and exact (a verification surface; never abbreviate or round). Your job in 3c is the **non-numeric** display (header sanitization, dates → `MMM D, YYYY`, booleans → Yes/No, `choice_field` → label) and supplying each column's **`unit`**; the numeric cells are then emitted deterministically downstream. The type/unit table below is the reference `units.py` implements. | `type` | `unit` | Format | |---|---|---| | `decimal` / `integer` | `usd`, `dollars` | `$148.95` (always show `$`, 2 decimals, locale grouping) | | `decimal` / `integer` | `eur` | `€148.95` | | `decimal` / `integer` | `gbp` | `£148.95` | | `decimal` / `integer` | `jpy` | `¥148` (no decimals — JPY has no minor unit) | | `decimal` / `integer` | `inr` | `₹2,162,087` (Indian Rupee symbol; for amounts > 100k optionally use `2.16 Cr` / `21.6 L` if USER_MEMORY says "use Indian numbering") | | `decimal` / `integer` | `cad` | `CA$148.95` (or `$148.95` if locale is Canadian) | | `decimal` / `integer` | `aud` | `A$148.95` | | `decimal` / `integer` | `chf` | `CHF 148.95` (no symbol convention; show code + space) | | `decimal` / `integer` | other ISO 4217 code | `<UPPERCASE_CODE> <number>` — show the code as a prefix (`SEK 148.95`, `BRL 148.95`). Never strip the unit silently. | | `decimal` | `percent` | `12.4%` (1 decimal) | | `decimal` | (other / none) | `1,234.5` (commas, the value's own decimals with trailing zeros dropped: a NUMERIC `0.12500000` shows `0.125`) | | `integer` | (no currency unit) | `1,234` (commas, no decimals; a negative is `-719`) | | `integer` / `string` | — (a year, a ZIP/postal code, an id, a key, a choice field) | as written — `2011`, `07094`, never `2,011` or `7,094`. `sm prepare` returns these columns as `labels`. | | `date` | — | **`May 6, 2026`** (`MMM D, YYYY`) — never raw ISO `2026-05-06`, never epoch numbers | | `timestamp` | — | **`May 6, 2026 3:14 PM`** (`MMM D, YYYY h:mm A`) — never raw ISO with `T` separator, never microsecond precision | | `integer` / `string` | `epoch_s`/`epoch_ms`/`epoch_us`/`epoch_ns`, `yyyymmdd` | a **date encoding** — `format-table` renders it human-readably (epoch → `YYYY-MM-DD HH:MM:SS UTC`) deterministically; don't hand-format. **In the SQL, convert it** (`to_timestamp(created_ts)` for epoch_s, `to_timestamp(created_ts/1000)` for ms, etc.) so filters/grouping work and the result is a real date. | | `boolean` | — | `Yes` / `No` (or `true` / `false` if the user prefers — read USER_MEMORY) | | `string` (with `choice_field`) | — | the choice's display label, not the stored value | | `string` (other) | — | as-is | **Timezone:** when a date/timestamp result column has a `timezone` in `get_table_context` (e.g. `UTC` for epoch columns, `offset-aware` for a TZ-aware timestamp), **state it once in the prose insight** ("times shown in UTC") so the reader isn't guessing — and especially flag it when the stored value is a naive/`offset-aware` timestamp whose zone differs from the user's. Epoch columns are UTC by definition; `format-table` already labels each rendered cell `… UTC`. **Hard rule for currency:** if the field has a currency `agami.unit`, show the symbol or code in **every** cell value — never omit it because the column header already mentions the currency. The user's screenshot showed bare `2,162,087` in an "Avg Outstanding (INR)" column; the cell should read `₹2,162,087`. Headers can drop redundant unit (the column header `Avg Outstanding` is enough when every cell is `₹...`), but cells should always carry the symbol so a screenshot or copy-paste of a single cell stays unambiguous. **Hard rule for dates: never display ISO timestamps like `2026-05-07T15:14:00.000Z`, epoch seconds, or any other machine-format string.** Cell values OR chart-axis labels — both must be human-readable. If the column shows months only (typical for time-series charts grouped by month), use `MMM YYYY` (e.g. `May 2026`); for daily granularity use `MMM D` if all data is in the current year, else `MMM D, YYYY`. The skill is responsible for inferring the right grain from the data. If the user has stated a date-format preference in `<artifacts_dir>/USER_MEMORY.md` (e.g. "use ISO dates" or "use DD/MM/YYYY"), respect that. The defaults above apply only when USER_MEMORY is silent. If row count > 15: - The chat preview shows the first **15** rows only ([`shared/question-cards.md`](../../shared/question-cards.md) rule 3) — longer markdown tables scroll and bury the insight. - The chart page (Phase 4e, when the person asks for it) holds the full set in a paginated table. - For row counts > 15, **auto-write the CSV to `<artifacts_dir>/local/exports/<profile>/<ts>.csv`** without waiting for the user to ask. Surface its path in Phase 4d's footer. - Footer line under the table: `Showing 15 of <N> rows; <N−15> more · full set in CSV: <csv-path>`. Use thousands separators on `<N>` (e.g. `4,213`). - If `<N>` is huge (> 100k), additionally suggest tightening the filter inline: "If you want to slice by region or date, say so and I'll re-run." CSVs open natively in Excel / Numbers / Google Sheets, so "export to Excel" routes to this same CSV path — no separate `.xlsx` flow in v1. If row count == 0: - "No rows matched. The query was: …" (show SQL). - Suggest a relaxation if applicable. --- ## Phase 4: Present The chat reply follows this **strict order**, with NO other content interleaved (no SQL, no Bash output, no "let me know if…" filler): 4a Approach → 4b Fetching → 4c Insight → 4d Table → 4d.1 How this was answered → (log the query, Phase 5, silently) → 4f Numbered follow-ups → the reply line (4d.2), always the last line, all in the turn's last message, which ends the turn (4d.2: no card after an answer) → (optional 4g CSV path); the person's reply to the answer, then (4e.5 star ask, once) Each step is short. The whole reply should fit in a typical chat viewport without scrolling. **No page is rendered or opened on this path:** the chart page (4e) runs only when the person replies **Show as a chart** to the answer (4d.2), passes `--chart`, or asks for a chart ("make that a chart"). Cards follow [`shared/question-cards.md`](../../shared/question-cards.md). ### 4a — Approach (one sentence, plain English) Open with a single sentence describing **how** you'll answer, in plain English. No SQL keywords (`SELECT`, `JOIN`, `GROUP BY`). No table or column names — describe the dimensions in user-language. Examples: - "I'll group orders by status across the last 30 days and rank by count." - "I'll pull total spend per customer and sort to find the top 5." - "I'll compare this month's revenue to last month's, broken down by region." For multi-section reports (broad questions), the approach sentence describes the narrative across all sections: "I'll cover four angles — revenue trend, top customers, order status, and region split." ### 4b — Fetching (one sentence, counts only) Right after the approach, one sentence about the data we just pulled, in counts and dimensions. Examples: - "Pulled 6 rows across 4 statuses." - "Pulled 247 orders spanning the last 30 days." - "Pulled 12 monthly buckets with revenue totals." For multi-section: "Pulled data for 4 sections (12 + 5 + 6 + 3 rows)." ### 4c — Insight first One sentence stating the answer. Lead with the most surprising or actionable finding. Examples: - "**Carol Chen** is the top spender at **$148.95** — about 3x the next customer." - "Revenue is up **12% MoM**; the surge came from EU customers." - "60% of orders shipped on time this month, up from 48% last month." For multi-section: a 1–3 sentence executive summary across all sections (the same summary passed as `--summary` if the chart page is shown). ### 4d — Markdown table **Render the table deterministically — do NOT hand-type the number cells.** This is a verification surface; an exact number is mandatory (no rounding, no `1.2L`/`2.16Cr` abbreviation, no dropped decimals). Pass the result CSV (first 15 rows, headers sanitized per 3c) and the **`units` map that `sm prepare` already returned in 3a** through the packaged formatter, then **embed its output verbatim**: ```bash bash "${CLAUDE_PLUGIN_ROOT}/scripts/sm" format-table --csv-file "<artifacts_dir>/local/tmp/agami-result.csv" --units "$PREPARE_UNITS" --labels "$PREPARE_LABELS" ``` `prepare`'s `units` is keyed by output column and **traced through the SQL** — so `SUM(amount) AS total_outstanding` correctly carries amount's currency (a bare name match would miss the summed total), and only a column that has a unit gets one. Its `labels` lists the output columns that hold a year, a code or an id, which `format-table` shows as written instead of grouping. `format-table` then formats every numeric cell in full via `units.py` (symbol + Indian/western grouping, exact decimals) and passes non-numbers through. Same map + formatter the **MCP** uses, so verification numbers are identical regardless of host/LLM. (If a header was prettified in 3c, key the units to the prettified name.) Wide tables (> 8 cols) → vertical layout + "wide table — the CSV (or Show as a chart) has the full grid". **Cap the chat preview at 15 rows** per Phase 3c — even when the user asked for "all leads with credit rating > 700" and the result is 4,213 rows, the chat shows the first 15 and points them at the CSV. The full set lives on disk. The footer line ("Showing 15 of 4,213 rows; 4,198 more · full set in CSV: …") is the contract that tells the user where to find everything. **Multi-section answers show one short table per section**, each under its section title and each rendered by `sm format-table` (at most 15 rows, with the same footer when there are more). Keep the section count to what the question needs (4e.i); the chart page, when asked for, holds them all with their charts. ### 4d.1 — How this was answered (the trust receipt via `sm receipt`; do NOT hand-build it) The receipt documents provenance: the tables the statement touched, the joins and metrics it used, the columns it referenced, the assumptions it leaned on, and the model-version pin. **It is assembled deterministically from the SQL + the model** by `runtime.assemble_receipt` — the SAME builder the MCP server uses — so it never depends on you extracting fields by hand: ```bash bash "${CLAUDE_PLUGIN_ROOT}/scripts/sm" receipt "$ROOT" \ --sql-file "<artifacts_dir>/local/tmp/agami-q-<n>.sql" \ > "<artifacts_dir>/local/tmp/agami-receipt-<ts>.json" ``` It emits `model_version` plus **five sections** — `columns`, `tables`, `joins`, `aggregates`, `assumptions` — each an object of the form `{"items": [...], "undetermined": "<sentence>" | null}`. It parses the FROM/JOIN scope and matches model entries itself: | section | what `items[]` holds | | --- | --- | | `columns` | every column the statement referenced, as `{"column": "<schema>.<table>.<col>", "metric": null}` — **plus** each model metric whose binding SQL appears in the statement, as `{"column": null, "metric": {...}}`. A metric match is a statement-level fact today, so it has no owning column. The metric object carries `name`, `area`, `definition_prose`, `expression`, `confidence`, `review_state`, `origin` — whether it is approved, never who approved it | | `tables` | **one entry per REFERENCE, not per table** — a table read twice appears twice. `ref` (the name as the statement wrote it), `alias`, `qname` (what the model resolved it to), `declared`, `rows`, `rows_text` (the size in words, "at least 5,000,000 rows" for one that reached the cap; quote this, never `rows`), `rows_as_of`, `freshness`, `scope`, `filters`, `scan_risk`. `scan_risk` is a sentence, or null, whe
Comments (0)
Sign in to join the conversation.
Reviews (0)
No reviews yet.
No comments yet.