Skip to content

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

Merged
hkaltai merged 17 commits into
mainfrom
feat/profile-ctes
Nov 18, 2026
Merged

hkaltai merged 17 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 5 commits November 13, 2026 17:08
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>
…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

Files Reviewed (3 files)
  • plugins/altimate-code/hooks/features/cte-profile.tsx
  • plugins/altimate-code/hooks/features/cte-profile.test.tsx
  • plugins/altimate-code/types/index.d.ts

Notes: incremental review of the PR as it now stands against main (e10448c), re-verified at 9b24733. The earlier foundation-file findings (tsconfig.json, dbt-command.ts) are no longer part of this PR's diff and are not carried forward; the four inline findings on cte-profile.tsx were re-checked at HEAD and are fixed (held run claim via try/finally in finish, failed CTEs moved ahead of the measured rows so truncation cannot drop them, note cleared only after prompt.submit resolves, case-insensitive target|dbt_packages|dbt_modules exclusion). The run lifecycle (Stop during listing, /clear and reload generations, second command while claimed, failure/refusal/timeout early ends, subagent hand-over) was traced case by case against tool-chain.ts, altimate-cli.ts and status-line.ts; no new defects found. Tests were not executed (read-only review); their new expectations were re-derived by hand against the implementation.

Previous Review Summaries (6 snapshots, latest commit c79111f)

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

Previous review (commit c79111f)

Status: 2 Issues Found | Recommendation: Address before merge

Overview

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

SUGGESTION

File Line Issue
plugins/altimate-code/tsconfig.json 2 extends points at ./.claude-plugin/types/tsconfig.json, which does not exist (.claude-plugin/ holds only plugin.json; the declarations live at types/index.d.ts and no tsconfig*.json exists under types/), so any tsc or editor run using this config fails with TS5083. Carried forward: the file is unchanged by this round, re-verified at HEAD.
plugins/altimate-code/hooks/dbt-command.ts 683 The new firstAssignment = null reset in the -- branch is dead code: optionsOpen is true only while firstAssignment is already null (every assignment sets optionsOpen = false; the only branch that sets it true clears firstAssignment).
Files Reviewed (8 files)
  • plugins/altimate-code/hooks/dbt-command.ts - 1 issue (new); the per-prefix PREFIX_OPTIONS/ENDS_AT_DASHES tables, the per-launcher RUNNER_OPTIONS tables and the assignmentDynamic change were checked case by case: unknown option words, --, exec -a/-l, time -p, env with assignments, keyword-as-program-name, missing option values, and joined/repeated short flags all fail closed or report the right binary; no case found where a launcher option value is misread as the dbt binary.
  • plugins/altimate-code/hooks/dbt-command.test.ts - no new issues (new expectations re-derived by hand against the implementation; they match, e.g. -qq via the repeated-flag rule, uvx --from, env -- dbt run, the trailing-backslash rule and the \-newline continuation).
  • plugins/altimate-code/hooks/altimate-cli.ts - no new issues (attaching work.then(settle, …) before clock.after means a throwing clock can no longer leave the work's rejection unhandled; callers either catch the rejection (runTool) or are bounded (tool-chain)).
  • plugins/altimate-code/hooks/altimate-cli.test.ts - no new issues (the 10-minute now still defeats the CONNECT_RETRY_MS cache left by the previous test).
  • plugins/altimate-code/hooks/tool-chain.ts - no new issues (a throwing deadline or logger now drops only its phase, so the gate still runs; the production limit never throws synchronously, so no rejection is left unattached).
  • plugins/altimate-code/hooks/tool-chain.test.ts - no new issues.
  • plugins/altimate-code/.claude-plugin/plugin.json - no new issues (valid JSON; all five settings now carry the "not used yet" caveat consistently).
  • plugins/altimate-code/README.md - no new issues (the earlier read-only/--allow-write contradiction is now stated where the promise is made, and the settings section says none is read yet).

Notes: incremental review of 5cd97c3..c79111f. The two README suggestions from the previous review are resolved and are not repeated; the newer code comment on README.md:113 is likewise satisfied by the new exception at line 55. dbt-command.ts has no production consumer yet (only its test imports it), so the parser findings are correctness-of-intent, not live behaviour. Tests were not executed (read-only review).

Previous review (commit 5cd97c3)

Status: 3 Issues Found | Recommendation: Address before merge

Overview

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

SUGGESTION

File Line Issue
plugins/altimate-code/README.md 113 The /profile-ctes bullet says the timing queries always run with --allow-write, which contradicts the read-only promise in step 4 (line 55) and runTool's "only once the user has said yes" contract (hooks/altimate-cli.ts:120); hooks/features/cte-profile.tsx:196 passes write: true with no approval step. State the exception where the promise is made, or make the grant an explicit opt-in.
plugins/altimate-code/README.md 138 The five settings are described as active ("What it controls", lines 142-146, and the cleaned-up plugin.json descriptions), but maxScanBytes, healthBlocksPush, healthConfigPath, commentsPollSeconds and commentsInContext are read nowhere and their features (features/cost.tsx, health.tsx, comments.tsx) are still no-op stubs. Restore the inactive caveat or land the implementing features.
plugins/altimate-code/tsconfig.json 2 extends points at ./.claude-plugin/types/tsconfig.json, which does not exist (.claude-plugin/ holds only plugin.json, and no other tsconfig*.json exists in the repo), so any tsc or editor run using this config fails with TS5083.
Files Reviewed (6 files)
  • plugins/altimate-code/README.md - 2 issues (carried, re-verified at HEAD)
  • plugins/altimate-code/tsconfig.json - 1 issue (carried, re-verified at HEAD)
  • plugins/altimate-code/hooks/integrations.tsx - no new issues
  • plugins/altimate-code/hooks/integrations.test.tsx - no new issues
  • plugins/altimate-code/.claude-plugin/plugin.json - no new issues
  • .claude-plugin/marketplace.json - no new issues
  • plugins/altimate-code/CHANGELOG.md - no new issues

Notes: incremental review of 1565683..5cd97c3. That range is the merge of main (PR #16, v1.6.1) into the feature branch; the form/discard/failureText code it brings in is already on the base and outside this PR's diff, so it is not commented on. I checked the merge for lost or duplicated work: main's reasonOf/joinNotes/discardEntries/outcome changes are present exactly once, the PR's status-line tests exist once each with unique titles, hooks are registered once (clearStatusParts, registerFeatures, installToolChain), and the version is coherent (marketplace.json 1.6.1, plugin.json 1.6.1, top CHANGELOG entry 1.6.1). The previous CRITICAL (CteProfileState unclosed) is fixed — types/index.d.ts:290 closes it and the declaration file is balanced. No new inline comments: the three remaining suggestions already have active inline comments, so none were duplicated. Tests were not executed (read-only review).

Fix these issues in Kilo Cloud

Previous review (commit 1565683)

Status: 3 Issues Found | Recommendation: Address before merge

Overview

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

SUGGESTION

File Line Issue
plugins/altimate-code/README.md 113 The new /profile-ctes bullet says the timing queries always run with --allow-write, which contradicts the README's read-only promise (step 4, line 55) and runTool's "only once the user has said yes" contract (hooks/altimate-cli.ts:120).
plugins/altimate-code/README.md 138 The five settings are described as active ("What it controls"), but their features (features/cost.tsx, health.tsx, comments.tsx) are still no-op stubs and nothing reads maxScanBytes, healthBlocksPush, healthConfigPath, commentsPollSeconds or commentsInContext.
plugins/altimate-code/tsconfig.json 2 extends points at ./.claude-plugin/types/tsconfig.json, which does not exist (.claude-plugin/ holds only plugin.json), so any tsc or editor run using this config cannot resolve its base.
Files Reviewed (3 files)
  • plugins/altimate-code/README.md - 2 issues
  • plugins/altimate-code/tsconfig.json - 1 issue
  • plugins/altimate-code/types/index.d.ts - no new issues
  • plugins/altimate-code/hooks/features/cte-profile.test.tsx - no new issues

Notes: incremental review of 93ee73a..1565683. The previous CRITICAL (unclosed CteProfileState) is fixed and the declaration file is balanced again; the test rename is backed by the standalone pane-failure test at line 313. Both remaining suggestions were re-verified against current HEAD. Files outside the incremental diff were checked only for the carried findings. Tests were not executed (read-only review).

Fix these issues in Kilo Cloud

Previous review (commit 93ee73a)

Status: 3 Issues Found | Recommendation: Address before merge

Overview

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

CRITICAL

File Line Issue
plugins/altimate-code/types/index.d.ts 289 CteProfileState is added without its closing }, so declare module 'claude-code' nests inside the interface and the declaration file no longer parses.

SUGGESTION

File Line Issue
plugins/altimate-code/README.md 137 The settings table describes five settings as active, but their feature files are still no-op stubs and nothing reads the keys.
plugins/altimate-code/tsconfig.json 2 extends points at ./.claude-plugin/types/tsconfig.json, which does not exist in the repository.
Files Reviewed (27 files)
  • plugins/altimate-code/types/index.d.ts - 1 issue
  • plugins/altimate-code/README.md - 1 issue
  • plugins/altimate-code/tsconfig.json - 1 issue
  • plugins/altimate-code/hooks/features/cte-profile.tsx - no new issues
  • plugins/altimate-code/hooks/features/cte-profile.test.tsx - no new issues
  • plugins/altimate-code/hooks/dbt-command.ts - no new issues
  • plugins/altimate-code/hooks/dbt-command.test.ts - no new issues
  • plugins/altimate-code/hooks/altimate-cli.ts - no new issues
  • plugins/altimate-code/hooks/altimate-cli.test.ts - no new issues
  • plugins/altimate-code/hooks/tool-chain.ts - no new issues
  • plugins/altimate-code/hooks/tool-chain.test.ts - no new issues
  • plugins/altimate-code/hooks/features/index.ts - no new issues
  • plugins/altimate-code/hooks/features/index.test.ts - no new issues
  • plugins/altimate-code/hooks/integrations.tsx - no new issues
  • plugins/altimate-code/hooks/integrations.test.tsx - no new issues
  • plugins/altimate-code/hooks/lineage.tsx - no new issues
  • plugins/altimate-code/hooks/status-line.ts - no new issues
  • plugins/altimate-code/hooks/task-impact.tsx - no new issues
  • feature stubs (comments.tsx, cost.tsx, defer.tsx, docs.tsx, health.tsx, runs.tsx, sql-graph.tsx, to-model.tsx) - no new issues

Notes: incremental review of 84f8a64..93ee73a; the previous findings are verified fixed. Tests were not executed (read-only review).

Fix these issues in Kilo Cloud

Previous review (commit 84f8a64)

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 (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: 118.1K · Output: 33.9K · Cached: 1.5M

…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)

Comment thread plugins/altimate-code/types/index.d.ts Resolved
Comment thread plugins/altimate-code/README.md Resolved
Comment thread plugins/altimate-code/tsconfig.json Resolved
Comment thread plugins/altimate-code/README.md Resolved
@hkaltai

hkaltai commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

review-loop: APPROVED at 5cd97c3 (reviewers: gpt-6-astra high, gpt-6.1-sol high)

@saravmajestic saravmajestic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving (reviewed as the delta over #30). The plugin reads exactly the metadata dbt_cte_profile returns, and passing both name and position works with the tool's matching. Stop is checked again right before each query. A refusal, a timeout or an unusable answer ends the run, so nothing piles onto a query still running, and the session generation keeps a stale run's result out after /clear. The summary for Claude is quoted data, capped and handed over once. One small note inline.

@hkaltai

hkaltai commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

review-loop: APPROVED at c79111f (reviewers: gpt-6-astra high, gpt-6.1-sol high)

# Conflicts:
#	plugins/altimate-code/hooks/integrations.tsx
@hkaltai

hkaltai commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

review-loop: APPROVED at b2798b9 (reviewers: gpt-6-astra high, gpt-6.1-sol high)

… main

The CteTier, CteRow and CteProfileState declarations were lost when the
foundation's empty block was kept, so the feature's imports from types did
not resolve and strict plugin validation failed. Only the feature's own block
is restored.

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

hkaltai commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

review-loop: APPROVED at 9b24733 (reviewers: gpt-6-astra high, gpt-6.1-sol high)

@hkaltai

hkaltai commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in 9b24733: the merge with main had kept the foundation's empty block, so CteTier, CteRow and CteProfileState were no longer declared and strict plugin validation failed; the feature's own block in types/index.d.ts is restored (nothing else in that file changes).

@saravmajestic saravmajestic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approving after the conflict fix. The only change since my last approval is in the shared types, which came in from main with #29.

@hkaltai
hkaltai merged commit daf6d27 into main Nov 18, 2026
4 checks passed
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.

2 participants