feat(advisor): read-only EXPLAIN-based query cost estimator (plan 49) by duyet · Pull Request #2233 · chmonitor/chmonitor · GitHub
Skip to content

feat(advisor): read-only EXPLAIN-based query cost estimator (plan 49) - #2233

Merged
duyet merged 6 commits into
mainfrom
advisor/49-query-cost-estimator
Jul 3, 2026
Merged

feat(advisor): read-only EXPLAIN-based query cost estimator (plan 49)#2233
duyet merged 6 commits into
mainfrom
advisor/49-query-cost-estimator

Conversation

@duyet

@duyet duyet commented Jul 3, 2026

Copy link
Copy Markdown
Member

What (plan 49 — query cost estimator)

Read-only estimateQueryCost(sql, hostId) that runs EXPLAIN json=1, header=1, indexes=1 (never the raw SQL) and returns { estRows, estBytesRead, estPeakMemoryBytes, estWallMs, confidence, warnings[] }, exposed as the agent tool estimate_query_cost.

  • New lib/ai/advisor/cost-estimator.ts + 17 tests; parser grounded against ClickHouse's own EXPLAIN fixtures.
  • Recommend/estimate-only, never executes: the real test asserts a mutating SQL (ALTER/DROP/INSERT) throws before fetchData is called, and the EXPLAIN call sets readonly=1.
  • bun test src/lib/ai/advisor src/lib/ai/agent/tools --isolate → 148 pass, 0 fail. bun run check / depcruise clean.

Reviewer note — parallel tool-count coordination

Bumps tool-docs-sync.test.ts (24→25) and updates ai-agent.mdx. Held plans 46 (advisor engine) and 47 (MV designer) also add agent tools; whichever merges after this must reconcile the tool count. This PR is safe to land first (read-only, self-contained).

Risk

Read-only; no billing/security/destructive surface.

Co-Authored-By: duyetbot bot@duyet.net

Add estimate_query_cost, a pre-flight agent tool that estimates rows
scanned, bytes read, peak memory, and wall time for a query from
EXPLAIN alone. It never executes or mutates the analyzed query - only
ever runs EXPLAIN json=1,header=1,indexes=1 through the existing
readonly transport, gated by the same SQL validator as the rest of
the agent tools.

Parses the plan's ReadFromMergeTree/Join/Aggregating nodes (shape
verified against ClickHouse's own EXPLAIN json test fixtures) and
combines granule-pruning counts with system.columns sizing to derive
the estimate; peak memory follows max(scan floor, join build side,
aggregation state) rather than total bytes read, since ClickHouse
streams a plain scan rather than materializing it.

Co-Authored-By: duyetbot <bot@duyet.net>
@duyet
duyet enabled auto-merge (squash) July 3, 2026 13:05
@github-actions github-actions Bot added documentation Improvements or additions to documentation app: dashboard Changes to the apps/dashboard TanStack Start app labels Jul 3, 2026
@codecov

codecov Bot commented Jul 3, 2026

Copy link
Copy Markdown

duyet and others added 5 commits July 3, 2026 20:27
Co-Authored-By: duyetbot <bot@duyet.net>
…stimator

tsc flagged `t` as possibly null in the read-table filter — `Boolean(t)`
doesn't narrow for TypeScript. Use optional chaining instead.

Co-Authored-By: duyetbot <bot@duyet.net>
@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

☁️ Cloudflare Preview Deployment

App Preview URL
Dashboard https://preview.dash.chmonitor.dev
MCP https://preview.dash.chmonitor.dev/api/mcp
Landing https://preview.chmonitor.dev
Docs https://preview.docs.chmonitor.dev
Property Value
Commit 743a762
Deployed at 2026-07-03T16:17:36.874Z

Previews are automatically updated on every push to this PR.
Unchanged apps keep their previous preview (path-filtered deploys).

@duyet
duyet merged commit a3acf68 into main Jul 3, 2026
19 of 20 checks passed
@duyet
duyet deleted the advisor/49-query-cost-estimator branch July 3, 2026 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app: dashboard Changes to the apps/dashboard TanStack Start app documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant