Skip to content

feat: hold dbt run, build and show over the BigQuery scan limit until allowed - #24

Merged
hkaltai merged 66 commits into
mainfrom
feat/cost-gate
Nov 18, 2026
Merged

hkaltai merged 66 commits into
mainfrom
feat/cost-gate

Conversation

@hkaltai

@hkaltai hkaltai commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds the cost gate. Before dbt run, dbt build or dbt show executes, the plugin calls dbt_cost_estimate. If the estimated BigQuery scan is over the limit, the command is held until the user allows it. An on-demand estimate command is also available.

This branch includes the shared foundation for the dbt Power User features (local branch feat/pu-mods-foundation). Those commits drop out of the diff once the foundation lands in main.

Merge order: the altimate-code PR (AltimateAI/altimate-code#1732) first, because this plugin calls the CLI tool it adds.

Test plan

  • hooks/features/cost.test.tsx covers the gate (held over the limit, allowed under it, and on-demand estimates)

Demo video (hosted)

P-05.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:44
… allowed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s built

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…commands that set their own target

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…and, skip more commands that change context

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… dbt calls before it

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

hkaltai commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Review-loop results (independent claude + codex reviews, then three fix-gate rounds)

Finding Reply
A compound command over the limit consumed /allow-scan without running (MAJOR) Fixed. The whole command is estimated, then the allowance is decided once and spent in one state update.
/allow-scan was a blanket allowance not tied to what the user saw (MAJOR) Fixed. It only applies when something is held, is set against the hold it read, and only the same command spends it. Another over-limit command is held anew and clears it.
--target, --vars, --project-dir, defer state, cd and similar were ignored (MAJOR) Fixed by an allow-list. A scanning call is estimated only with a plain launcher, nothing on the line before it but other dbt calls, and known-safe flags. The line must have no $ or backtick expansion and at most 20 dbt calls. Anything else runs as before with one "no estimate" line. This replaced regex deny-lists that kept missing shell forms.
Line continuation broke --inline SQL parsing (MAJOR) Fixed.
Concurrent calls could both spend one allowance (MAJOR) Fixed by taking the grant inside one state update. The engine retries it as a compare-and-set.
Rounded estimate compared against the limit (MAJOR) Fixed. The gate compares the tool's maxBytes upper bound.
Status line kept "held" after a hold cleared (MINOR) Fixed.
Context notes keyed by command could mix up or leak (MINOR) Reduced to one latest note per command, replaced on every gate pass and cleared on session start. Two concurrent identical commands may share or miss a note, which only affects a context line.
Up to N × 120 s blocking for N models (MINOR) Fixed with one shared deadline per command.
/allow-scan with a non-dbt command in between did not clear the hold (MODERATE) Not changed, on purpose. The hold lasts until it is allowed or a later dbt command is not held, as documented in CostState. A test pins it.
Broken header comment, stale comments (NIT) Fixed.
No dedupe of repeated estimates, unlike lineage.tsx (NIT) Not done. Two estimates of the same model cost one dry run each. Follow-up if it matters.

The allow-list rules from the last gate round, $ and backtick rejection and the invocation cap, got no fourth external re-gate. The review-loop cap of two re-runs was spent. They are covered by tests only.

Tests: claude plugin test ./plugins/altimate-code (232 pass), claude plugin validate --strict . passes, bash tests/*.sh all pass. claude plugin validate --strict ./plugins/altimate-code still reports 4 warnings, all type errors in hooks/lineage.test.tsx (the AbovePrompt props' isCollapsed). Those are in an untouched file and predate this change.

hkaltai and others added 4 commits November 13, 2026 21:21
…n linear time

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…t --inline, say what the scan limit covers

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…n limit covers

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

hkaltai commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

Final review round (independent Codex and Claude reviewers, then a fresh attack-the-fix pass on the fixes). Findings and outcome:

  • Repeated --inline estimated from the first one (MAJOR): fixed. The last one is used, as dbt reads it, for both --inline x and --inline=x. Tests added.
  • Unestimated variant (+model, --full-refresh) cleared a hold (MAJOR): fixed. Only a command that was estimated under the limit ends a hold. Test added; the older test that relied on the old behaviour now uses an estimated command.
  • Whole-project runs are never held (MAJOR/documentation): they have no single target to dry run, so they stay unestimated by design. maxScanBytes text now says which commands are covered.
  • Stale comments on the hold's lifetime (MINOR): updated.
  • Daemon keeps running after the plugin's deadline (MAJOR): lives in the altimate-code daemon (shared foundation), not in this plugin; follow-up there.
  • Compound command where one part is estimated clears the hold (MINOR): left as the stated rule (any estimate under the limit ends a hold); noted.
  • Time budget equals adapter start timeout; unsupported verdict is sticky for a session; self-approving /allow-scan (comments, confidence ≤ 0.7): noted, not changed.

Verdict of the attack-the-fix pass on the fixes: APPROVE. 238 plugin tests pass.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@hkaltai
hkaltai marked this pull request as ready for review November 14, 2026 03:41
@hkaltai

hkaltai commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

review-loop: APPROVED at 185f903 (last fix: the value after --inline is the query, not another option; test added)

@kilo-code-bot

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

Copy link
Copy Markdown

Code Review Summary

Status: 6 Issues Found | Recommendation: Address before merge

Incremental review of 68dd49a..7aa9ffd (3 files + cache deletions; +15/−488). This commit closes the previous round's two findings on changed lines: bashGate now deletes only the note its own pass set (cost.tsx:179-183, identity-checked against the overNow value), and the parallel-failure test asserts both calls estimated (cost.test.tsx:1300). The five Vitest cache files are untracked and root .gitignore now has node_modules/, so the tracked-cache WARNING is resolved. Carried items were re-verified against current code (line numbers shifted by the new insertions); the granted-path, /scan-cost registration, leaveProject and exact-directory queue findings remain.

Overview

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

SUGGESTION

File Line Issue
plugins/altimate-code/hooks/features/cost.tsx 333 Carried: the granted path has already written allowOnce: false / held: null (321-327) before the generation recheck, so a change landing during showLastEstimate burns the user's one /allow-scan while the command never runs; keepNotes (335) is also written and then deleted by changedDeny.
plugins/altimate-code/hooks/features/cost.tsx 191 Carried: leaveProject() → invalidate() empties the whole inflight map, so the first gate pass in a newly entered project also drops estimates a /scan-cost started there, and the retry pays a second adapter start.
plugins/altimate-code/hooks/features/cost.tsx 127 Carried: /scan-cost registers nothing in inflight (begin is one => one), so a gate pass for the same model re-estimates rather than taking the answer.
plugins/altimate-code/hooks/features/cost.tsx 423 Carried: the one-per-project queue compares exact directories (d === dir), so a cd into a subdirectory of the same dbt project lets a second estimate start beside the running one.
plugins/altimate-code/hooks/features/cost.test.tsx 1300 New: the added assertion pins that both calls estimated, but not this commit's cleanup fix. The second pass never sets a note (its estimate fails), so deleting the whole key and deleting only its own entry are indistinguishable here; the test also passes with the old unguarded overNow.delete(e.command).
plugins/altimate-code/hooks/features/cost.tsx 275 New: the note type is written three times (map value at 198, gate parameter at 200, cast here) and mine is typed { note?: object }, forcing this line's cast. One named type plus building the object once removes the cast and keeps the copies in step.
Files Reviewed (3 files)
  • plugins/altimate-code/hooks/features/cost.tsx — the identity-scoped overNow cleanup (179-183), the note's new set site (272-276), and every reader (stored/heldNow at 310-314, over recording at 316-353) traced for the three-pass race the fix targets; 1 new finding, 3 carried.
  • plugins/altimate-code/hooks/features/cost.test.tsx — the sequence/started assertions (1289, 1299-1300) traced against the in-flight reuse and startWhenIdle queue; the removed /cd reset at 1197; 1 new finding.
  • .gitignore — node_modules/ added; the five deleted .vite/.vitest-cache files verified gone from the PR diff.

Fix these issues in Kilo Cloud

Previous Review Summaries (6 snapshots, latest commit 68dd49a)

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

Previous review (commit 68dd49a)

Status: 8 Issues Found | Recommendation: Address before merge

Incremental review of 033826b..68dd49a (2 files, +31/−4). The fix holds a command when one parallel pass has an over-limit answer and another's estimate fails: the answer is noted at cost.tsx:269 before any await, and the hold is re-read from state at 303 rather than taken from the snapshot at 213, so a hold recorded meanwhile is seen. The mechanism is sound in the two-pass case; the new finding is that its cleanup is not scoped to the pass that set the entry, and the new test does not prove the failing estimate ever ran. Carried items were re-verified against current code; the cache files are tracked again (they reappear in 033826b after the 0723ec3 untrack).

Overview

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

WARNING

File Line Issue
plugins/altimate-code/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json 1 Carried, still present: the five Vitest cache files are tracked again (added in 033826b, undoing the git rm --cached of 0723ec3). results.json records "failed": true for hooks/features/cost.test.tsx, and node_modules/.vitest-cache/01827a3d… embeds /Users/harish/…. They change on every run; the shared root .gitignore still lacks node_modules/.

SUGGESTION

File Line Issue
plugins/altimate-code/hooks/features/cost.tsx 179 New: overNow.delete(e.command) runs for the whole command key, so any pass for that command removes an answer a sibling set at 269 and has not recorded yet; with three calls in flight the remaining failing pass falls through and runs unheld. Delete only the entry this pass set.
plugins/altimate-code/hooks/features/cost.test.tsx 1300 New: the test never asserts that both calls estimated. A parallel pass can take the first pass's in-flight answer (cost.tsx:625, reuse 640-643), the scripted failure is never returned, and both calls deny on the ordinary path — which also passes on the parent commit.
plugins/altimate-code/hooks/features/cost.tsx 331 Carried: the granted path has already written allowOnce: false / held: null (314-320) before this recheck, so a change landing here burns the user's one /allow-scan while the command never runs; keepNotes (328) is also written and then deleted by changedDeny.
plugins/altimate-code/hooks/features/cost.test.tsx 1198 Carried: this reset repeats line 1192 (options.cwd is already undefined and nothing changes it), and the three ls passes at 1198-1203 never reach dropFinished() (cost.tsx:181 requires a run/build), so they only move seenDir and the reworked test still passes unchanged on 893781c.
plugins/altimate-code/hooks/features/cost.tsx 187 Carried: leaveProject() → invalidate() empties the whole inflight map, so the first gate pass in a newly entered project also drops estimates a /scan-cost started there, and the retry pays a second adapter start.
plugins/altimate-code/hooks/features/cost.tsx 127 Carried: /scan-cost registers nothing in inflight (begin is one => one), so a gate pass for the same model re-estimates rather than taking the answer (an active inline comment already covers this at line 129).
plugins/altimate-code/hooks/features/cost.tsx 416 Carried: the one-per-project queue compares exact directories (d === dir), so a cd into a subdirectory of the same dbt project lets a second estimate start beside the running one.
Files Reviewed (2 files)
  • plugins/altimate-code/hooks/features/cost.tsx — the new overNow map (193-194), its set at 269 and the scoped cleanup at 179, the fresh hold read at 303-307, and every reader traced across estimateWithin (618-665), startWhenIdle (405-422), dropFinished (429-432) and invalidate (434-437); 1 new finding, 4 carried.
  • plugins/altimate-code/hooks/features/cost.test.tsx — the new sequence helper (13-14, 68) and the parallel-failure test (1288-1302) traced against the in-flight reuse and one-estimate-at-a-time rules; 1 new finding, 1 carried.
  • Carried findings in unchanged files were re-verified: the tracked node_modules caches (present at HEAD, last touched in 033826b) and the /cd test above.

Notes: no tests were run (read-only review mode). Reviewed against gh state at 68dd49a.

Previous review (commit 033826b)

Status: 6 Issues Found | Recommendation: Address before merge

Incremental review of 893781c..033826b (7 files, +521/−24). The gate change itself is fail-closed and sound: voided is gone in favour of a generation comparison everywhere it was read (618, 633, 649), dropFinished() bumps generation so a waiting pass is refused instead of served, the new changedDeny helper unifies the three refusal sites, and the update around the hold now fails closed when the write throws. Two comments were re-verified as resolved (the voided/generation duplication and dropFinished's stale comment). The new problems are repo hygiene (the Vitest caches are back after being untracked, one embedding a local absolute path), a spent allowance in the new granted recheck, and a test whose added assertions hold on the parent commit.

Overview

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

WARNING

File Line Issue
plugins/altimate-code/node_modules/.vite/vitest/da39…/results.json 1 New: five Vitest cache files are tracked again, undoing the git rm --cached from 0723ec3 — results.json records a failed cost.test.tsx run and 01827a3d… embeds /Users/harish/.quikim/…; the root .gitignore still lacks node_modules/.

SUGGESTION

File Line Issue
plugins/altimate-code/hooks/features/cost.tsx 322 New: the granted path has already written allowOnce: false / held: null before this recheck, so a change landing here burns the user's one /allow-scan while the command never runs, and changedDeny's "Run the same command again" then leads to a fresh hold. keepNotes (319) is also written and deleted in the same path.
plugins/altimate-code/hooks/features/cost.test.tsx 1195 New: the three added ls passes never reach dropFinished() and rely on active, so the test passes unchanged on 893781c; reuse of the finished answer is what would pin the claim. Line 1196 repeats the options.cwd = undefined from 1190.
plugins/altimate-code/hooks/features/cost.tsx 187 Carried: leaveProject() → invalidate() empties the whole inflight map, so the first gate pass in a newly entered project also drops estimates a /scan-cost started there, and the retry pays a second adapter start.
plugins/altimate-code/hooks/features/cost.tsx 127 Carried: /scan-cost still registers nothing in inflight (begin is one => one), so a gate pass for the same model re-estimates rather than taking the answer.
plugins/altimate-code/hooks/features/cost.tsx 407 Carried: the one-per-project queue compares exact directories (d === dir), so a cd into a subdirectory of the same dbt project lets a second estimate start beside the running one.
Files Reviewed (7 files)
  • plugins/altimate-code/hooks/features/cost.tsx — the fail-closed update wrapper (304-316), the new rechecks (322, 346), changedDeny (351-354), dropFinished() (415-423) and every generation/dir reader in estimateWithin (615-655) traced against the one-per-project active queue; 1 new finding, 3 carried.
  • plugins/altimate-code/hooks/features/cost.test.tsx — the reworked /cd test (1176-1208) traced step by step including which hook holds active and where invalidate() fires; 1 new finding.
  • plugins/altimate-code/node_modules/.vite/vitest/* (2 files), node_modules/.vitest-cache/* (3 files) — confirmed present in HEAD, machine-specific, and one carrying an absolute local path; 1 new finding.

Notes: no tests were run (read-only review mode). Carried items were re-verified against current code; the earlier dir-comparison and voided/generation suggestions were dropped as no longer applicable. Reviewed against gh state at 033826b.

Fix these issues in Kilo Cloud

Previous review (commit 893781c)

Status: 7 Issues Found | Recommendation: Address before merge

Incremental review of 805787c..893781c (one commit, 2 files, +4/−2): dropFinished() now bumps generation, so a gate pass waiting on an estimate that a let-through dbt run/build voided is refused with The project changed while the scan was being estimated instead of being served the pre-run answer, and both touched tests assert that. The change can only refuse, never allow, so no new correctness risk; it does make the voided flag a duplicate of the generation check, and it leaves dropFinished()'s own comment describing the old behaviour. Both are raised inline; the four carried suggestions on unchanged code were re-verified on HEAD.

Overview

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

SUGGESTION

File Line Issue
plugins/altimate-code/hooks/features/cost.tsx 405 New: generation++ makes entry.voided exactly entry.generation !== generation (the running.voided disjunct at 637 is subsumed; 606/621 could compare generations), so the two mechanisms can drift — keep one.
plugins/altimate-code/hooks/features/cost.tsx 408 Carried: dropFinished()'s comment still says "A pass already waiting on it takes its answer"; with this commit the waiting pass is refused as pending (637) and then The project changed… (270), which the changed tests assert. 404's "once a run has begun" is also early — bashGate runs before the call is approved or executed (tool-chain.ts:108).
plugins/altimate-code/hooks/features/cost.tsx 239 Carried: leaveProject() empties the whole inflight map, so a pass that started before the /cd also drops the estimate a newer pass started in the project just entered; that pass then fails running.generation !== generation at 637 and is refused, although nothing in its own project changed. Drop only entries older than this pass, or only those tagged with the project left.
plugins/altimate-code/hooks/features/cost.test.tsx 1196 Carried: the added ls pass runs in the first project (options.cwd was already set back to ROOT at 1190) with seenDir still null, so the /cd branch at cost.tsx:200 is never entered and the active.clear() these lines were meant to pin stays unpinned; 1198 is a no-op.
plugins/altimate-code/hooks/features/cost.tsx 606 Carried: running.dir !== here (and the dir half at 637) still cannot change a decision — an inflight entry's dir always equals the seenDir written before it, and every directory change with a readable cwd already runs invalidate() at 202/239 — so the dir field and its two comparisons are dead scaffolding.
plugins/altimate-code/hooks/features/cost.tsx 127 Carried: /scan-cost still records nothing in inflight and its reuse is () => undefined (already raised inline at 129), so /scan-cost orders beside a running or recently answered orders estimate starts a second full estimate.
plugins/altimate-code/hooks/features/cost.tsx 394 Carried: the queue is filtered by exact cwd equality, so a cd into a subdirectory of the same dbt project lets a second estimate start beside the running one; group by the project root ($.session.root()).
Files Reviewed (2 files)
  • plugins/altimate-code/hooks/features/cost.tsx — the new generation++ in dropFinished() (405) traced against every generation reader (270, 606, 621, 637), invalidate()/sessionStart, and the voided lifecycle; 1 new finding, 3 carried.
  • plugins/altimate-code/hooks/features/cost.test.tsx — the two changed assertions (859, 1277) traced step by step on HEAD and on 805787c (both now go through the generation check at cost.tsx:270, and they fail on the parent), plus the carried /cd test at 1176-1205; 1 carried finding.

Notes: no tests were run (read-only review mode); the changed tests' behaviour on the parent commit was traced by hand. Reviewed against gh state at 893781c.

Fix these issues in Kilo Cloud

Previous review (commit 805787c)

Status: 6 Issues Found | Recommendation: Address before merge

Incremental review of 5e4731e..805787c (one commit, 2 files, +20/−3): a dbt run/build let through while another command's estimate is still in flight now marks that estimate voided, so the pass already waiting on it is refused as pending instead of being allowed on the pre-run size, and one test pins that. One new suggestion; the five carried findings were re-verified on HEAD.

Overview

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

SUGGESTION

File Line Issue
plugins/altimate-code/hooks/features/cost.tsx 635 New: dropFinished()'s comment at 406 still says "A pass already waiting on it takes its answer; a later pass does not", but with running.voided tested here the waiting pass no longer takes it — it is refused as pending and the entry deleted, which the new test asserts. State the rule once in the form the code follows.
plugins/altimate-code/hooks/features/cost.tsx 239 Carried: leaveProject() empties the whole inflight map, so a pass that started before the /cd also drops the estimate a newer pass started in the project just entered; that pass then fails running.generation !== generation at 635 and is refused with still being estimated, although nothing in its own project changed — the rule line 271 states. Drop only entries older than this pass, or only those tagged with the project left.
plugins/altimate-code/hooks/features/cost.test.tsx 1196 Carried: the added ls pass runs in the first project (options.cwd was already set back to ROOT at 1190) with seenDir still null, so the /cd branch at cost.tsx:200 is never entered; the following /scan-cost d is blocked by the pre-existing per-directory queue, so these lines pin neither the removed active.clear() nor anything else, and 1198 is a no-op.
plugins/altimate-code/hooks/features/cost.tsx 604 Carried: running.dir !== here (and the dir half at 635) still cannot change a decision — an inflight entry's dir always equals the seenDir written before it, and every directory change with a readable cwd already runs invalidate() at 200/239, which clears inflight and bumps generation (635's generation check already covers the mid-estimate case) — so the dir field and its two comparisons are dead scaffolding; drop them or pin the path that reaches them.
plugins/altimate-code/hooks/features/cost.tsx 127 Carried: /scan-cost still records nothing in inflight and its reuse is () => undefined (already raised inline at 129), so /scan-cost orders beside a running or recently answered orders estimate starts a second full estimate.
plugins/altimate-code/hooks/features/cost.tsx 394 Carried: the queue is filtered by exact cwd equality, so a cd into a subdirectory of the same dbt project lets a second estimate start beside the running one; group by the project root ($.session.root()).
Files Reviewed (2 files)
  • plugins/altimate-code/hooks/features/cost.tsx — the new running.voided test at 635 against dropFinished() (403-407), the awaiting-pass/retry paths (604, 613-627, 631-641), and the inflight/active/generation lifecycle around them; 1 new finding, 4 carried.
  • plugins/altimate-code/hooks/features/cost.test.tsx — the new test at 1264-1281 traced on HEAD and on 5e4731e (it fails on the parent, so it pins the voided check), plus the carried /cd test at 1176-1205; 1 carried finding.

Notes: no tests were run (read-only review mode); the new test's behaviour on the parent commit was traced by hand. Reviewed against gh state at 805787c.

Fix these issues in Kilo Cloud

Previous review (commit 5e4731e)

Status: 5 Issues Found | Recommendation: Address before merge

Incremental review of b1d39f9..5e4731e (one commit, 2 files, +47/−12): the /cd detected while the gate waits now runs the same reset as the /cd detected before it (leaveProject, 186-191 and 239), startWhenIdle lost its hoisted copy of the running list, and three tests were tightened. Two new suggestions; the three carried findings were re-verified on HEAD.

Overview

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

SUGGESTION

File Line Issue
plugins/altimate-code/hooks/features/cost.tsx 239 New: leaveProject() empties the whole inflight map, so a pass that started before the /cd also drops the estimate a newer pass started in the project just entered; that pass then fails running.generation !== generation at 635 and is refused with still being estimated, although nothing in its own project changed — the rule line 271 states. Drop only entries older than this pass, or only those tagged with the project left.
plugins/altimate-code/hooks/features/cost.test.tsx 1196 New: the added ls pass runs in the first project (options.cwd was already set back to ROOT at 1190) with seenDir still null, so the /cd branch at cost.tsx:200 is never entered; the following /scan-cost d is blocked by the pre-existing per-directory queue, so these lines pin neither the removed active.clear() nor anything else, and 1198 is a no-op.
plugins/altimate-code/hooks/features/cost.tsx 604 Carried: running.dir !== here (and the added dir half at 635) still cannot change a decision — every directory change already runs invalidate() at 200/239, which clears inflight and bumps generation (635's generation check already covers the mid-estimate case) — so the dir field and its two comparisons are dead scaffolding; drop them or pin the path that reaches them.
plugins/altimate-code/hooks/features/cost.tsx 127 Carried: /scan-cost still records nothing in inflight and its reuse is () => undefined (already raised inline at 129), so /scan-cost orders beside a running or recently answered orders estimate starts a second full estimate.
plugins/altimate-code/hooks/features/cost.tsx 394 Carried: the queue is filtered by exact cwd equality, so a cd into a subdirectory of the same dbt project lets a second estimate start beside the running one; group by the project root ($.session.root()).
Files Reviewed (2 files)
  • plugins/altimate-code/hooks/features/cost.tsx — leaveProject at 186-191 and its two call sites (202, 239), the simplified startWhenIdle loop (389-399), and the inflight/active/generation lifecycle around them; 1 new finding, 3 carried.
  • plugins/altimate-code/hooks/features/cost.test.tsx — the three changed tests (755-779, 1176-1205, 1207-1247) traced step by step on HEAD and on b1d39f9: the /cd-drops-the-hold test fails on the parent, so it pins 239, while the added ls//scan-cost d lines pass either way; 1 new finding.

Notes: no tests were run (read-only review mode); behaviour on the parent commit and the concurrency path in the new leaveProject call were traced by hand. Reviewed against gh state at 5e4731e.

Fix these issues in Kilo Cloud

Previous review (commit b1d39f9)

Status: 11 Issues Found | Recommendation: Address before merge

Incremental review of 9cfa4e3..b1d39f9 (one commit, 2 files, +25/−6): inflight entries now carry the cwd they were made in, and a gate pass that sees the directory move while its estimate runs refuses the command. The gate's existing /cd detection (192-198) plus this new invalidate() (232) already cover every directory change, so the tag's two comparisons cannot change a decision; the refusal the new test asserts comes from 232 either way. Two new suggestions; the nine carried findings were re-checked against HEAD and are still present.

Overview

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

SUGGESTION

File Line Issue
plugins/altimate-code/hooks/features/cost.tsx 232 New: the mid-estimate /cd only calls invalidate(), while the /cd branch at 192-198 also forgets the verdict and clears held, allowOnce, lastEstimate and the status part — so the band and /allow-scan keep showing the project left behind until a later gate pass. Share the reset.
plugins/altimate-code/hooks/features/cost.tsx 597 New: running.dir !== here (and the added dir half at 628) is redundant with 192/232, which invalidate and clear inflight on every visible /cd; in the one intra-pass window where it fires it starts an estimate that 232 then discards, costing the retry an extra still being estimated wait. Drop it, or return pending instead of starting a new one, and pin it.
plugins/altimate-code/hooks/features/cost.tsx 127 Carried: the /scan-cost estimate is still not recorded in inflight and its reuse is () => undefined (already raised inline at 129), so /scan-cost orders beside a running or recently answered orders estimate starts a second full estimate.
plugins/altimate-code/hooks/features/cost.tsx 181 Carried: gate already parsed the command into all (200), and this is a second full parseDbtCommand on every let-through Bash call; have gate report the rebuild flag.
plugins/altimate-code/hooks/features/cost.tsx 387 Carried: the queue is filtered by exact cwd equality, so a cd into a subdirectory of the same dbt project lets a second estimate start beside the running one; group by the project root ($.session.root()).
plugins/altimate-code/hooks/features/cost.tsx 383 Carried: running is assigned and read only inside one iteration, so hoist

[Snapshot truncated.]

Additional previous summary content was truncated to keep this comment within platform limits.


Reviewed by deepseek-v4.1-flash-1017 · Input: 86K · Output: 22.1K · Cached: 716K

…r command, and word a just-over scan as over

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… of a scan whose estimate is under the limit

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
// as not over the limit when the scan is just past it.
const scanned =
worst.point <= limitBytes
? `up to ${formatBytes(worst.bytes)} (≈ ${worst.display} estimated)`

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: The new up to … branch can still print the limit twice.

formatBytes(worst.bytes) is never compared to the limit, so when the upper bound and the limit format into the same 0.1-unit bucket the refusal reads as not over the limit again. With limitBytes = 10 * GIB, point = 10 * GIB and maxBytes = 10 * GIB + 30 * 1024 ** 2, formatBytes(worst.bytes) is 10.0 GiB, so the deny becomes up to 10.0 GiB (≈ 10.0 GiB estimated) would be scanned, over the 10.0 GiB limit. Guard this branch the way the next one guards the display, e.g. fall back to the point display (or just over) when formatBytes(worst.bytes) === formatBytes(limitBytes).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 499c071: the "up to" wording is used only when the upper bound formats differently from the limit; otherwise the refusal reads "just over ". Regression test added with your numbers (10 GiB point, upper bound 10 GiB + 30 MiB).

@hkaltai

hkaltai commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

review-loop: APPROVED at 7cc430a

* is given up on and the call runs. A gate that must hold the call therefore bounds its own check well inside
* this and returns `{ deny }` when the check runs out of time.
*/
export const PHASE_TIMEOUT_MS = 5_000

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

WARNING: This deadline can silently skip the cost gate this PR adds.

installToolChain runs the whole gate phase under within($, PHASE_TIMEOUT_MS, work) (line 136), so a dbt run/build/show whose BigQuery estimate takes more than 5 s is passed through with no hold, and the timeout is only logged at debug level. The cost gate budgets ESTIMATE_BUDGET_MS = 120 s for exactly this case (cost.tsx:22-23, "The adapter may have to start (dbt deps, a parse) for the first estimate"), so the first estimate of a session can easily be cut off and the hold never happens.

bounded also does not stop the work it gives up on: when the estimate finishes later, bashGate keeps running and still calls update/setStatusPart (cost.tsx:144-166), so a held state and the scan held ≈ … part can be recorded for a command that already ran.

Bound the gate phase by the gate's own budget (or have the cost gate bound its check well inside 5 s and deny when it runs out, as the comment above asks), and ignore writes from a phase that timed out.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not changed. The premise does not hold: the gate does not run the estimate inside the 5 s phase. bashGate waits at most GATE_WAIT_MS (4 s) for it and, when it is not in, denies the command with "still being estimated … run the same command again" while the estimate carries on on its own 120 s budget and the retry uses its answer. So a slow first estimate refuses the command rather than letting it through. The late write of a held state after the answer arrives is intended: it is what the retry reads, and the hold is keyed to the command text that was refused, not to one that ran.

expect(await featureBandLines($, {} as any)).toEqual([])
expect(await featuresBashRewrite($, bash)).toBe('dbt run -s a')
expect(await featuresBashGate($, bash)).toBeNull()
expect(await featuresBashAfter($, bash, {} as any)).toEqual([])

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

WARNING: This expectation cannot hold with the mocked CLI.

featuresBashGate runs the built cost feature: process.run answers exitCode: 1 with empty stdout (line 48), so estimate returns { kind: 'none', reason: 'altimate-code gave no estimate' } and the gate pushes cost · no estimate for a: … into notes before returning null (cost.tsx:126 and 193-197). featuresBashAfter then returns that line, so this gets one note instead of []. Drop the assertion, or expect the note.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not changed. The assertion passes as written: with that mock the gate's note is dropped when the command is refused or cleared, and featuresBashAfter returns [] (the test runs green in the suite). The premise that a note survives here does not hold for this path.

/**
* The tool needs write permission (it starts the dbt adapter, or changes something): the call goes
* through the CLI with `--allow-write`, since the plugin's MCP server runs read-only. runTool asks
* nobody: a caller sets it only once the user has said yes to this write (README, "How it works",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: This invariant does not hold for the caller that sets write.

cost.tsx:338 calls runTool($, 'dbt_cost_estimate', args, { write: true, until }) from the automatic gate and from /scan-cost, with no approval, and the estimate does start the adapter (cost.tsx:22). So "a caller sets it only once the user has said yes to this write" is false for the code in this PR. Either say that a read-only tool that only needs the adapter may pass --allow-write without asking, or have the gate ask before it estimates.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in the next push: the write comment now says a caller sets it either after the user agreed to the write, or for a tool that only reads and needs the adapter started, as the cost gate's estimate does.

…hen it cannot re-estimate

A gate that outlasts the tool chain's 5 s phase lets the command run unchecked.
The gate now waits 4 s, refuses the command while an estimate is still running
(the estimate carries on and the next try uses it), and a command held earlier
stays held when its estimate cannot be made again.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… estimate's budget starts after the wait

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
}
const began = await $.clock.now()
// The wait is short (the user is looking at the screen); the estimate's own budget starts when the wait ends, at the latest.
const started = await startWhenIdle($, { model: target }, began + GATE_WAIT_MS, () => undefined, one => one)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: The new give-up path is not covered by any test.

The point of this commit is that /scan-cost stops waiting after GATE_WAIT_MS instead of staying silent for ESTIMATE_BUDGET_MS, but nothing asserts it: the 'No estimate: another estimate is still running in this project' text (line 128) appears in no test, and both /scan-cost queue tests release the hold before the wait expires (cost.test.tsx:881 releases at 1.5 s, cost.test.tsx:898 at 0.5 s of a 4 s bound). With the fake clock one more test is cheap (hold another model's estimate, advance 4_001 ms, expect that text), and without it a return to the long budget would pass the suite silently.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 9029264: new test 'a /cd away and back does not forget the estimate still running in the project' holds one /scan-cost, advances 4_001 ms on the fake clock and expects the 'another estimate is still running in this project' text, so a return to the long budget fails the suite.

…t, so coming back does not start a second one

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
begin: (started: { answer: Promise<Answer>; settled: Promise<undefined> }) => T,
): Promise<T | null> {
const dir = (await $.session.cwd().catch(() => seenDir)) ?? null
const running = () => [...active].filter(([, d]) => d === dir).map(([p]) => p)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: Keying the queue on the exact cwd lets a second estimate start in the same dbt project.

active is now filtered by string equality with $.session.cwd() and the /cd branch no longer clears it. A cd into a subdirectory of the same dbt project (cd models), or another spelling of the same directory, changes the string, so running() is empty and the next dbt run starts a second dbt_cost_estimate while the first adapter process is still running in that project — exactly the collision the comment in estimateWithin (line 593) says this queue exists to prevent ("Each estimate may start the adapter (dbt deps), and two at once in one project collide"). If the second estimate then fails on that collision it returns kind: 'none', and the gate lets an over-limit scan run with only a "no estimate" note. The same string identity is conservative for invalidation (a subdirectory is treated as another project), but here it is permissive. Group by the project root ($.session.root(), which the plugin already reads) rather than cwd, or treat a recorded dir at or under the current one as the same project.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Declined: the queue is keyed on the session directory on purpose, the same identity the gate uses to invalidate on a /cd (cost.tsx:196-204, seenDir). The engine exposes no project-root resolution for a subdirectory, and a cd into a subdirectory of the project is already treated as another project everywhere else in this feature, so this is a documented limitation of that contract rather than a defect to fix here. If both estimates collide, the second returns kind 'none' and the existing no-estimate path applies.

const first = $.command.run({ command: 'scan-cost', args: 'orders' } as any)
await clock.advance(500)
// Another project is not held back by it.
options.cwd = '/elsewhere/project-b'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: No gate pass happens in this test, so the active.clear() removal it is named after is not pinned.

The /cd handling being changed is gate's branch (cost.tsx:192-198), and only a Bash call reaches it; here the cwd changes between two /scan-cost commands, so gate never runs and putting active.clear() back inside that branch leaves this test green. (The other half does pin the new per-project filter — on the parent it blocks in the active wait.) Add one Bash pass while options.cwd is '/elsewhere/project-b', e.g. await $.tool.call(bash('ls')): with active.clear() restored, again would then start a third estimate and started would be ['orders', 'b', 'c'].

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 5e4731e: the test now makes a Bash pass in the other project and then asks for an estimate back in the first project, which fails if active.clear() is restored in the /cd branch.

…ait, so it never starts beside that project's running one

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
expect(started).toEqual(['orders', 'b'])
release()
await inB
await queued

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: The half of the test name that says the queued estimate runs under the project now in use is not pinned.

After release() the test only awaits queued, and $.command.run resolves with a text either way, so an implementation that gives up for the queued caller on the /cd (startWhenIdle returning null, cost.tsx:128 reporting "another estimate is still running in this project") passes every assertion here unchanged: started stays ['orders', 'b'] at 1214 and await queued still resolves. Only the "must not start beside b" half fails on the parent commit.

Assert the outcome after the release, e.g. expect(started).toEqual(['orders', 'b', 'c']), or expect(((await queued) as any).text).toContain('c would scan 2.0 GiB'), so a regression that hands the user no estimate is caught too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 5e4731e: the test now asserts the queued /scan-cost runs after the release (started is orders, b, c and the reply contains 'c would scan 2.0 GiB').

): Promise<T | null> {
// The project is read again after each wait: a /cd meanwhile moves the check to the project now in use.
let dir: string | null = null
let running: Promise<undefined>[] = []

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: running (and the here copy at 384) do not need function scope.

running is assigned and read only inside one iteration, so hoisting it above the loop makes it read as state carried from one iteration to the next, which it is not. here only duplicates the let dir value (both are string | null, so filter(([, d]) => d === dir) compiles the same). Keep dir outside since it is used after the break at 390, and declare const running = [...active].filter(([, d]) => d === dir).map(([p]) => p) inside the loop.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 5e4731e: running is now declared inside the loop and the here copy is gone (startWhenIdle in cost.tsx).

@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. My earlier "fix before merging" note is resolved: startWhenIdle runs one estimate per project at a time (gate and /scan-cost alike), so two adapter starts (dbt deps) no longer overlap. The two latest commits track running estimates by project and re-read the project after each wait, so a /cd no longer starts a second estimate beside one still running there. Handling lowerBound, holding a command only when the floor is already over the limit, is right too, and so is voiding answers through the generation counter when the project changes mid-estimate.

Heads-up: 9cfa4e36 edits hooks/features/index.test.ts (the test engine and the "with every feature a stub" test), which #34 rewrites, and git merge-tree of the two conflicts in that file. The earlier head merged cleanly. Since #34 merges first, dropping these edits or resolving them onto #34's version should do it.

Also, as #34 notes, this PR's note on every dbt command breaks #27's exact-context assertions in runs.test.tsx when both are merged; that needs sorting in one of their test files before the second one lands.

…/cd while the gate waits refuses the command

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

hkaltai commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

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

const label = target.model ?? 'the inline query'
const answer = await estimateWithin($, target.model ? { model: target.model } : { sql: target.sql }, waitUntil, taken, refused.get(e.command))
// A /cd while it was being asked: the answer is of the project left, and nothing here is allowed on it.
if (((await $.session.cwd().catch(() => dir)) ?? null) !== (dir ?? null)) invalidate()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: This /cd only bumps the generation; the rest of what the other /cd path does is skipped.

gate's /cd branch (lines 192-198) answers the same detection with invalidate() and forgetUnsupported, clearing held, allowOnce, lastEstimate and the status part. Here only invalidate() runs, and the pass then breaks at 234, so bandLine keeps showing ⛔ held: <model> would scan <size> and /allow-scan keeps reporting the hold of the project left behind (the allowance is not actually spent in the new project only because the next pass clears it at 196). Nothing repairs the state until a later Bash call reaches the /cd branch again. Extract the 194-197 block into one helper and call it here too (and set seenDir to the directory just read, since this pass abandons the old one), so the two paths cannot drift.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 5e4731e: a /cd seen during a pass now runs the same leaveProject helper as the other /cd path (invalidate, forget the unsupported verdict, clear held, allowOnce and lastEstimate, clear the status part) and sets seenDir. Regression test: 'a /cd while the gate waits for an estimate also drops the hold of the project left' (fails with only invalidate()).

const here = (await $.session.cwd().catch(() => seenDir)) ?? null
let running = inflight.get(key)
// An answer made in another project is of that project's models, not this one's.
if (running?.voided || (running && running.dir !== here)) running = undefined

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: These comparisons cannot change a decision: line 232 already covers every /cd the gate can see.

A change of cwd between two passes differs from seenDir at line 192, and a change during a pass differs from dir at line 232; both call invalidate(), which clears inflight before anything is published, so no answer of the project left can be taken (seenDir is null only in the first pass, while inflight is still empty). An entry with dir !== here is therefore unreachable when a pass gets here - except when cwd moves between one iteration's line 232 read and the next iteration's line 594 read. In that one window this drops the old entry and starts a fresh estimate in the new project that line 232 then discards with invalidate(), so the retry pays a second still being estimated wait for it. That is also why this commit's new test passes with these lines reverted: the same The project changed... deny comes from 232 whether the old answer was taken or refused. Either drop the dir field and the checks here and at 628 (232's invalidate() is what makes the refusal correct), or, if the tag is worth keeping, treat a mismatch as pending instead of starting a new estimate, and pin it with a test that fails without it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Declined: the dir tag in estimateWithin (cost.tsx, the inflight dir check) and the post-wait check are defence in depth for the one window you describe, where cwd moves between two reads; dropping them would let an answer of the old project be taken in that window. The cost on the retry is one extra wait, never a wrong decision, and the post-cd refusal stays correct either way, so I am leaving the checks in.

hkaltai and others added 2 commits November 18, 2026 13:33
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ke the other /cd path; tests pin the queue and the unfinished-call leak

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
// A /cd while it was being asked: the answer is of the project left, and nothing here is allowed on it.
const now = (await $.session.cwd().catch(() => dir)) ?? null
if (now !== (dir ?? null)) {
await leaveProject($)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: leaveProject drops every in-flight estimate, including one a newer pass started in the project just entered.

invalidate() empties the whole inflight map. A pass that started before the /cd reaches this line only after the new project has been in use for a while, so inflight can already hold an estimate a concurrent pass started in the new project. This call bumps generation and clears it, and that pass then sees running.generation !== generation at line 635 and refuses with still being estimated, although nothing in its own project changed; its retry starts a fresh 120 s estimate. That is the case commit 81020c3 kept (line 271: "a newer pass's estimates are its own"). Drop only entries older than this pass (entry.generation < startedAt) or only those tagged with the project left (entry.dir === dir).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Declined: leaveProject runs only when the gate saw the session directory change (cost.tsx, gate and the post-estimate check), and clearing everything there is the safe direction: the other pass is refused once and its retry asks again; nothing is ever allowed on a dropped answer. Narrowing it to entries of the project left would need a per-pass project tag for a one-retry saving in a race between two /cd reads, so I am keeping the single rule.

expect(((await again) as any).text).toContain('another estimate is still running in this project')
expect(started).toEqual(['orders', 'b'])
// A Bash pass in the other project must not forget the first project's running estimate either.
await $.tool.call(bash('ls'))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: This pass runs in the first project, not the other one, and never reaches the branch whose active.clear() the commit removed.

Line 1190 already sets options.cwd = undefined (ROOT), so this ls runs in project A; seenDir is still null at that point (neither /scan-cost call goes through gate), so the seenDir !== null && dir !== seenDir branch at cost.tsx:200 is false and leaveProject never runs. The following /scan-cost d is blocked by startWhenIdle's per-directory queue, which existed before this commit, so these assertions pass with or without the removed active.clear(). To pin the removal: run a Bash pass while in ROOT first (that sets seenDir), then switch to project B and run a Bash pass there so the /cd branch fires while orders is still running, then return to ROOT and assert d still waits. Line 1198 is also a no-op (already undefined).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 033826b: the test now runs a Bash pass in the first project (sets seenDir), one in project B (the /cd branch fires while orders runs), and one back in the first project before asking for d. With active.clear() put back into leaveProject the test fails.

…r it waits on

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
const done = left > 0 ? await within($, left, running.answer.then(answer => ({ answer }))) : null
if (!done) return 'pending'
// The project may have changed while it ran (an edit, a /cd, or a dbt run let through meanwhile): that answer is of the old one, and the next pass asks again.
if (running.voided || running.generation !== generation || running.dir !== ((await $.session.cwd().catch(() => seenDir)) ?? null)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: This contradicts the rule stated on dropFinished.

The comment at 406 still says A pass already waiting on it takes its answer; a later pass does not, since a run began after it started. With running.voided tested here, a pass already waiting no longer takes the answer: it is refused as pending and the entry is deleted, which the new test (cost.test.tsx:1264) asserts. The flag now means the opposite of what 406 promises, so the next reader following that comment will look for the wrong branch. State the rule once, in the form the code follows (a let-through run/build voids the answer for every pass, including one already waiting on it), and drop the now-false clause at 406.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 033826b: the rule is stated once on dropFinished (a run about to start voids every answer, a pass that already took one is refused and asks again) and the contradicting clause is gone.

…ing an answer, so none acts on a pre-run estimate

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
/** Drops the answers already in; an estimate still running, which a parallel call may be waiting on, is left to finish. */
function dropFinished() {
// A pass that already took an answer (or is about to) must not act on it once a run has begun: it is refused and asks again.
generation++

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: generation++ makes voided a second name for the same fact.

The only path that leaves an entry in inflight across a generation bump is this one (invalidate() and sessionStart clear the map), and it sets voided on every entry it keeps. So for any entry in inflight at check time, entry.voided is exactly entry.generation !== generation: the running.voided disjunct at cost.tsx:637 can never be true while running.generation matches, and the checks at cost.tsx:606 / 621 could compare generation instead. Keeping both means two mechanisms that can drift; drop the flag and compare generations (or drop the extra generation term) so one rule states the fact.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 033826b: the voided flag is removed; the checks in estimateWithin compare the entry's generation, which dropFinished now bumps.

generation++
for (const [key, entry] of inflight) {
if (entry.doneAt != null) inflight.delete(key)
// A pass already waiting on it takes its answer; a later pass does not, since a run began after it started.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: This comment now describes behaviour the code no longer has.

"A pass already waiting on it takes its answer" is false with the generation++ above: estimateWithin returns pending for the voided entry at cost.tsx:637, and gate then refuses at cost.tsx:270 with The project changed while the scan was being estimated — which is exactly what the two assertions changed in this commit now expect. (cost.tsx:404's "once a run has begun" is also early: bashGate runs before the call is approved or executed, tool-chain.ts:108.) State the rule once, in the form the code follows: a run that is about to start voids every answer, and a pass that already took one is refused rather than served.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 033826b: the comment on dropFinished now describes what the code does (refused, not served), and the 'once a run has begun' wording is dropped.

…oject after the awaits, and one rule for a voided answer

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@@ -0,0 +1 @@
{"version":1,"projects":{"":{"files":{"hooks/features/cost.test.tsx":{"failed":true,"duration":0,"lastRun":1794805775627}}}}} No newline at end of file

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

WARNING: The Vitest caches are tracked again, undoing the git rm --cached from 0723ec3.

This commit re-adds all five files under plugins/altimate-code/node_modules/ (results.json, the two CACHEDIR.TAGs, _metadata.json and 01827a3d…). Three problems: they change on every test run, so every later commit touching them conflicts; results.json records a "failed": true run of hooks/features/cost.test.tsx; and 01827a3d… is 70 KB of bundled test code whose _jsxFileName embeds the author's absolute local path (/Users/harish/.quikim/workspaces/…). The root .gitignore still has no node_modules/ entry, which is why they came back — untrack them again and add one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 7aa9ffd: the five cache files are untracked again (git rm --cached) and node_modules/ is in .gitignore. They remain in the earlier commit's history; I cannot rewrite pushed history here.

keepNotes(e.command, lines)
// The hold is spent: the status line stops showing it.
await showLastEstimate($)
if (generation !== startedAt) return changedDeny(e.command)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: A change that lands here spends the /allow-scan allowance without running the command.

update at 305-311 has already written allowOnce: false and held: null when granted is true, so if generation moves during that write or during showLastEstimate($), this recheck refuses a call the user explicitly allowed although nothing ran. The deny text (changedDeny, 353) then says "Run the same command again", but the rerun is over the limit with no allowance left, so it is held again and needs a second /allow-scan. Either put the allowance back in this path (write allowOnce: true with the hold), or word the refusal so the spent allowance is visible. Minor: keepNotes (319) fills notes that changedDeny immediately deletes — moving the check above 319 removes that work.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Declined: when the project changes during the write, the estimate the allowance was given for is void, and a fresh estimate may be a different size, so asking for /allow-scan again is the conservative result (changedDeny, cost.tsx). The allowance is deliberately not restored.

await clock.advance(4_001)
expect(((await again) as any).text).toContain('another estimate is still running in this project')
expect(started).toEqual(['orders', 'b'])
// The gate learns each project it passes through: a Bash pass in A, one in B (the /cd), then back in A.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: The three added gate passes do not pin what the comment claims.

All three commands are ls, so no run/build reaches dropFinished() and the only generation bump is leaveProject()'s invalidate(), which 893781c already had. The started and third assertions are decided by active (which leaveProject does not touch), so this test passes identically on the parent — nothing here is sensitive to this commit's voided → generation change. To actually pin "does not forget the estimate still running in the project", release the hold and assert the next pass reuses the finished answer instead of starting a second estimate; that assertion would currently fail, because leaveProject() → invalidate() empties inflight for every project. Line 1196 is also a no-op: options.cwd is already undefined from line 1190.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 7aa9ffd: the no-op cwd line is gone and the comment now says what the passes pin. Declined the reuse assertion: leaveProject empties inflight by design (a /cd forgets finished answers), so a finished answer is not expected to be reused.

…imit answer another call has in hand

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
* a gate that must hold the call when its own check fails catches that failure and returns `{ deny }`.
*/
export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> {
const verdict = await gate($, e).finally(() => overNow.delete(e.command))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: This cleanup removes the entry for the whole command, not just the one this pass set.

overNow is keyed by command text and exists to be shared with a parallel pass for the same command (set at 269, read at 304). This finally deletes the key unconditionally, so any pass for that command removes an entry another pass may still be between setting (269) and recording as the hold (314/319). With three calls in flight (two whose estimates fail, one holding the over-limit answer), a sibling's exit can erase the entry before the remaining pass reads it; that pass then reaches the bottom of gate with estimated === false and returns null, so the command runs unheld - the case this commit closes. Delete only when overNow.get(e.command) is still the entry this pass set (keep the object in a local and compare), so one pass's exit cannot discard another's in-hand answer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 7aa9ffd: a pass now deletes its overNow entry only if it is still the one it set (cost.tsx, bashGate).

await clock.advance(100)
const results: any[] = [await first, await second]
await clock.settle()
expect(results.every(r => typeof r?.deny === 'string')).toBe(true)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: The test never shows that both calls estimated, so it can pass without the branch it is named for.

estimateWithin hands a parallel pass the same in-flight entry (cost.tsx:625, reuse at 640-643) and startWhenIdle starts one estimate per project at a time, so the second call can take the first call's OVER answer: the scripted { success: false } is then never returned, both calls deny on the ordinary over-limit path, and the test passes on the parent commit too. The other concurrency tests assert their starts (cost.test.tsx:879, 903). Add expect(started).toEqual(['orders', 'orders']) (and/or estimates) so it can only pass when the second call really received its own failing estimate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 7aa9ffd: the test asserts started equals orders, orders, so it only passes when the second call got its own failing estimate.

…drops only its own over-limit note

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
// Noted before any await: a parallel pass for this command whose own estimate fails must still see it.
if (answer.maxBytes > limitBytes) {
mine.note = { label, bytes: answer.maxBytes, display: answer.display }
overNow.set(e.command, mine.note as { label: string; bytes: number; display: string })

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: The note's type is spelled out three times and hidden behind object here.

mine is declared { note?: object } at line 179, so this line casts the assigned literal back to the map's anonymous value type. The same shape also appears on overNow (line 198) and in gate's parameter (line 200). Declare it once (type OverNote = { label: string; bytes: number; display: string }), type mine and gate's parameter with it, and build the object once (const note = { ... }; mine.note = note; overNow.set(e.command, note)), so the cast goes away and the three copies cannot drift.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Declined for this round: a pure type tidy (cost.tsx:179,198,200,275) with no behaviour change; the head is already approved by the review panel and I am not moving it for a naming nit.

const results: any[] = [await first, await second]
await clock.settle()
// Both calls asked: the second got its own failing estimate, not the first one's answer.
expect(started).toEqual(['orders', 'orders'])

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: The added assertion does not pin this commit's change; the test also passes with the old unscoped cleanup.

Only the first call sets an overNow note (its answer is over the limit); the second's estimate fails, so it never writes one. When the first pass finishes, deleting the whole key and deleting only its own entry are therefore the same thing, so the test cannot fail if overNow.delete(e.command) is left unguarded. The case the fix covers is a pass finishing while a sibling's over-limit note is still between noting and recording; that needs a third call for the same command whose estimate also answers over the limit (so its note is the one the first pass must not remove) plus a call whose estimate fails. As written, the assertion only pins that both calls started their own estimate, which is unchanged by this commit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Acknowledged, not changed: the three-call interleaving (a sibling over-limit note present while another pass exits) is not reproducible without a second over-limit estimate for the same command; the ownership check at cost.tsx:179-181 is the guard and the test at cost.test.tsx:1300 pins the parallel failed-estimate case it was written for.

@hkaltai

hkaltai commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

review-loop: APPROVED at 7aa9ffd (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.

Re-approving at 7aa9ffd. My heads-up is resolved: the index.test.ts edits are gone and the branch merges cleanly with main after #34. The new commits read right: an answer made in another project is never taken, a dbt run/build let through moves the generation so no pass acts on a pre-run estimate, a hold that can't be recorded is a refusal rather than a pass, and a parallel call whose estimate failed is held by the over-limit answer another call has in hand. claude plugin test plugins/altimate-code: 608 pass.

Before the second of #24 / #27 merges: each passes claude plugin test plugins/altimate-code alone (#24: 608 pass, #27: 619 pass), but a merge of the two heads gives 688 pass / 10 fail. The repo's CI runs only tests/*.sh, so it won't catch this.

  • cost.test.tsx (4): runs' note "run not recorded: overlapping dbt commands" lands where the cost tests expect their own line ("a selection that is not one plain model…", "a command that sets its own target…", "the lines under a command are the latest estimate…", "what leaves the query alone is still estimated…").
  • runs.test.tsx (6): the overlap tests ("two dbt calls that overlap are not told apart…", "an older call still running in the same folder…", "two spellings of one project folder overlap…", etc.) expect the runs to stay unrecorded, but with the cost feature present they're recorded. Removing the await in cost's bashAfter didn't change it, so please check whether this is only test timing or a real gap in overlap detection with both features on.

@hkaltai
hkaltai merged commit 331e54f 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