Skip to content

feat: /profile-ctes times each CTE of a dbt model, slowest first - #31

Open
hkaltai wants to merge 10 commits into
mainfrom
feat/profile-ctes
Open

hkaltai wants to merge 10 commits into
mainfrom
feat/profile-ctes

Conversation

@hkaltai

@hkaltai hkaltai commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

What this does

/profile-ctes [model] lists a dbt model's CTEs with altimate-code's dbt_cte_profile tool, then times them one tool call at a time, so the pane fills live. Each CTE is one warehouse query that holds the CTEs before it (a warehouse may skip those the CTE does not use); nothing runs on edit.

  • Pane altimate-cte-profile: rows slowest first, each with its own added time (marginal), total query time, row count and a hot/warm/cool tier. Hot is half the largest marginal time or more, warm a fifth, as in dbt Power User.
  • Stop ends the run between queries (the pane and the band both have it); a query already running is waited for. A query that times out or gets no usable answer ends the run too, since it may still be running on the warehouse.
  • A bare /profile-ctes profiles the .sql model last read or edited by the main conversation (not a subagent's read, not a file under target/ or dbt_packages/).
  • The listing goes through the CLI read-only; each timing goes through the CLI with --allow-write (it starts the dbt adapter), so it never uses the read-only MCP server.
  • When the profile finishes, a "Tell Claude" button sends it, and the next tool result also carries it once as context.
  • With an altimate-code that lacks the tool (or has only the placeholder), one line names what is needed and no query is sent.
  • The optional cost pre-check is skipped: dbt_cost_estimate is still a placeholder on this base.
  • types/index.d.ts: CteProfileState filled in (cteProfile was already declared by the foundation).

This needs the altimate-code PR for dbt_cte_profile (AltimateAI/altimate-code#1731) merged and released first. The branch contains the foundation commit, which drops out of the diff when the foundation merges.

Checks

claude plugin test ./plugins/altimate-code: 233 pass (new: tiers, marginal times, summary, live progress, Stop, a timed-out query, missing tool, no model, a subagent's read). bash tests/*.sh pass. claude plugin validate --strict on the marketplace passes; on the plugin it reports warnings from hooks/lineage.test.tsx (not touched here) and none from cte-profile.

Review: two independent reviewers (Claude and Codex). Fixed after review: a timed-out query kept the run going, Stop or a /clear arriving just before a query still let it start, a finishing run could write its result back after a /clear, a subagent's .sql read changed the bare-command target, one failed CTE hid the own times of all later ones, Tell Claude shown while stopping. Left as is: the result reaches Claude with the next main-conversation tool result (also offered by the Tell Claude button); a window of one clock read inside runTool, a shared file, between the last Stop check and the process start; a project that sits inside a folder named target is not remembered for the bare command.

Changelog

  • New /profile-ctes command: times each CTE of a dbt model and lists them slowest first.

Demo video (hosted)

P-03.mp4 — watch on Notion (Altimate workspace; recorded from the approved head of this PR).

🤖 Generated with Claude Code

hkaltai and others added 8 commits November 13, 2026 16:31
Each feature gets its own file under hooks/features/, called by name from
features/index.ts in a fixed order: its own hooks, the session and turn
events, its band line and its tool-call phases. One tool.call hook
(tool-chain.ts) runs Bash and file handling in fixed phases: bashRewrite,
bashGate, the call, bashAfter, editAfter, readOrEditAfter. The status line
is built from parts (status-line.ts). runTool moves to altimate-cli.ts with
a `write` option that goes through the CLI with --allow-write.
dbt-command.ts finds the dbt invocations in a shell command line. New
userConfig fields: maxScanBytes, healthBlocksPush, healthConfigPath,
commentsPollSeconds, commentsInContext.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Lists the model's CTEs through altimate-code's dbt_cte_profile, then times
them one call at a time so the pane fills live and Stop works between
queries. Marginal times and hot/warm/cool tiers are worked out in the mod.
Runs only on request, and the finished profile reaches Claude with its next
tool result.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Subshells after keywords, case patterns, process substitution, arrays,
arithmetic and function definitions parse again; quotes in a heredoc inside
a substitution are text; a function or case pattern named dbt is not an
invocation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…laude a bounded, quoted summary

Stop pressed while the list loads ends the run before any query; a run ends
with its session (a /clear or reload) and hands nothing to the next one; a
second command while one runs starts nothing; a refusal ends the run at the
first CTE; a failed CTE leaves the next one with its total but no own part;
and the note for Claude is quoted data under a size cap, delivered once with
the main conversation's next tool result (a read too, never a subagent's).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…he profile to Claude only once

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ight before each query, and ignores a subagent's reads

A call with no usable answer ends the run instead of starting the next prefix query while the last may still be running. Finishing is bound to the run's session, a failed CTE costs only the next row its own part, and the Tell Claude button waits for the run to stop.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@hkaltai

hkaltai commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Review of this PR by two independent reviewers (Claude and Codex), then a fix gate (also both) on the fixes. Every finding and what happened to it (fixes are in c31bf14):

Fixed

  • A timed-out CTE (or a call with no usable answer) did not end the run, so the next, slower prefix query was sent while the last might still run: it ends the run with a message. A tool answer with success:false (a CTE's own query error) still lets the run continue.
  • Stop or a session end arriving just before a query started did not stop it: status and session are checked after the last read, before the call.
  • A finishing run could write its state and context back after a /clear: the state update and the context hand-over are guarded by the run's session.
  • A subagent's .sql read changed what a bare /profile-ctes profiles; files under target/, dbt_packages/, dbt_modules/ were remembered too: not any more.
  • One failed CTE removed the own time of every later CTE in its clause: only the next row loses it.
  • A refusal ended the run without saying so (the result read as a user stop): a message says it ended early. "Tell Claude" is hidden while stopping. Doc text claiming each query recomputes all earlier CTEs now says the query holds them. A test name that claimed more than it checked was fixed, and tests were added for each item above.

Left as is

  • The profile reaches Claude with the next main-conversation tool result (design; "Tell Claude" sends it at once).
  • write: true is needed: the tool starts the dbt adapter (--allow-write).
  • One clock read inside runTool (a shared file this PR does not own) sits between the last Stop check and the process start.
  • A project that itself sits in a folder named target is not remembered for the bare command (naming the model works).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@hkaltai

hkaltai commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

Final-gate round. Claude: the two "Ended early" message writes could land after the session ended or a new run started, overwriting the clean-up state. Fixed in 21dd352: each write now checks the live run inside the state update. Its success question holds: the tool's failure results set success: false, so an ordinary failed CTE does not end the run. Codex: approved. 233 plugin tests pass. The fix was not given a further review pass, so this PR stays a draft.

@hkaltai
hkaltai marked this pull request as ready for review November 14, 2026 03:38
@hkaltai

hkaltai commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

review-loop: APPROVED at 21dd352

@kilo-code-bot

kilo-code-bot Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Verification of previous findings

Previous finding Current state
active claim released before finish() awaits (cte-profile.tsx) Fixed: finish() now wraps settle() and releases the claim in a finally only after clean-up, guarded by current(); generation changes on session start/end also clear it, so the claim cannot leak.
MAX_NOTE truncation could drop every failed CTE Fixed: the Failed: block now precedes the measured rows, and a new test with 80 measured CTEs proves the failure survives the cut.
pendingContext cleared before $.prompt.submit Fixed: the note is cleared only after the submit resolves, so a rejected submit leaves it for the next tool result.
target / dbt_packages exclusion missed relative and differently-cased paths Fixed: `/(^
Files Reviewed (2 files)
  • plugins/altimate-code/hooks/features/cte-profile.tsx
  • plugins/altimate-code/hooks/features/cte-profile.test.tsx

Notes: incremental review of 21dd352..84f8a64 only (2 files, 24 insertions). Tests were not executed (read-only review). Merge remains dependent on the altimate-code release shipping dbt_cte_profile, as stated in the PR description.

Previous Review Summary (commit 21dd352)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit 21dd352)

Status: 4 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 1
SUGGESTION 3
Issue Details (click to expand)

WARNING

File Line Issue
plugins/altimate-code/hooks/features/cte-profile.tsx 235 finish() clears the module-level active claim before its awaits; a /profile-ctes started in that window is overwritten by the old run's finished state (and the vacuous rows.every(...) at line 241 relabels it failed). Clear the claim in a finally at the end.

SUGGESTION

File Line Issue
plugins/altimate-code/hooks/features/cte-profile.tsx 112 Failed: rows are appended last, so MAX_NOTE truncation on ~15+ CTEs can drop every failure from the note handed to Claude; put the failed block before the measured rows.
plugins/altimate-code/hooks/features/cte-profile.tsx 318 pendingContext is cleared before $.prompt.submit; if the submit rejects, the profile is neither sent nor handed over. Clear it after the submit resolves.
plugins/altimate-code/hooks/features/cte-profile.tsx 402 The target/dbt_packages/dbt_modules exclusion misses relative target/... paths and differently-cased Windows folders; use `/(^
Files Reviewed (21 files)
  • plugins/altimate-code/.claude-plugin/plugin.json
  • plugins/altimate-code/hooks/altimate-cli.ts
  • plugins/altimate-code/hooks/dbt-command.test.ts
  • plugins/altimate-code/hooks/dbt-command.ts
  • plugins/altimate-code/hooks/features/comments.tsx
  • plugins/altimate-code/hooks/features/cost.tsx
  • plugins/altimate-code/hooks/features/cte-profile.test.tsx
  • plugins/altimate-code/hooks/features/cte-profile.tsx - 4 issues
  • plugins/altimate-code/hooks/features/defer.tsx
  • plugins/altimate-code/hooks/features/docs.tsx
  • plugins/altimate-code/hooks/features/health.tsx
  • plugins/altimate-code/hooks/features/index.ts
  • plugins/altimate-code/hooks/features/runs.tsx
  • plugins/altimate-code/hooks/features/sql-graph.tsx
  • plugins/altimate-code/hooks/features/to-model.tsx
  • plugins/altimate-code/hooks/integrations.tsx
  • plugins/altimate-code/hooks/lineage.tsx
  • plugins/altimate-code/hooks/status-line.ts
  • plugins/altimate-code/hooks/tool-chain.test.ts
  • plugins/altimate-code/hooks/tool-chain.ts
  • plugins/altimate-code/types/index.d.ts

Notes: the current diff still contains the feat/pu-mods-foundation commit (dbt-command, tool-chain, feature stubs, status line, runTool move); helpers moved verbatim from lineage.tsx were treated as pre-existing. Tests were not run (read-only review).

Fix these issues in Kilo Cloud


Reviewed by deepseek-v4.1-flash-1017 · Input: 49.7K · Output: 9.3K · Cached: 311.6K

…ailed CTEs in a long note, and drops the note only after it is sent

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@hkaltai

hkaltai commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

review-loop: APPROVED at 84f8a64 (follow-up diff 21dd352..84f8a64; Codex approved; Claude first asked for changes, both points were refuted with evidence, then approved)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant