Skip to content

test: cost and runs tests pass with both features on - #37

Merged
hkaltai merged 19 commits into
mainfrom
fix/cost-runs-together
Nov 19, 2026
Merged

hkaltai merged 19 commits into
mainfrom
fix/cost-runs-together

Conversation

@hkaltai

@hkaltai hkaltai commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

What

With the cost gate (#24) and the runs pane (#27) both on main, claude plugin test plugins/altimate-code gave 743 pass and 10 fail, though each feature passed alone. Found by Sarav (saravmajestic) in review of the two PRs.

The cause is the tests' fake engines, not runs' overlap detection:

  • runs.test.tsx (6 tests): the overlap tests ran two concurrent dbt build calls. The cost gate, now on beside runs, refuses a second concurrent run or build ("the project changed while the scan was being estimated, run again"), so that call never ran and only one was recorded. A call that never ran is no overlap. The overlap tests now use dbt test, which the cost gate leaves alone and which runs still records.
  • cost.test.tsx (4 tests): the fake Bash call took no time on a frozen clock, so a call ending in the very instant the next began was read by runs as an overlap (run not recorded: overlapping dbt commands in place of cost's own line). The fake call now takes 100 ms, as the runs tests' does.

One real defect, found by merging the project health check on top of main: the cost gate (cost.tsx) waited for $.session.cwd() with no bound on every Bash call, and it runs before the health gate. A lookup that stalls used the whole 5 s phase budget, so the git push was let through before health could hold it. The gate now bounds the folder lookups of one pass by a single 80 ms budget. A folder lookup that times out is not a project and not a change of project: the last known project keeps its hold, allowance and estimates, no estimate starts, and a scan is refused at once with "the project folder could not be read in time" (/scan-cost says the same), so the chain's own 5 s deadline cannot let it through first. The state is per pass, not module-wide, so a stall in one pass refuses no other. A line refused this way keeps any over-limit answer already in hand as the command's hold. Regression tests in cost.test.tsx stall each of the gate's lookups in turn, a line of 20 models, a /cd-free stalled pass, concurrent passes, the shared budget and a host whose lookup always fails (it falls back to the session root and the gate works as usual). tool-chain.test.ts and defer.test.ts now answer session.cwd in their fake hosts.

Verification

claude plugin test plugins/altimate-code: 832 pass, 0 fail (main: 743 pass, 10 fail). With feat/implement-items-7-8-9-12-15-with-e2e-demo merged on top: 1128 pass, 0 fail. claude plugin validate --strict . passes; tests/*.sh pass.

🤖 Generated with Claude Code

The runs overlap tests use dbt test: the cost gate refuses a second concurrent run or build to ask
again, so that call never ran and nothing overlapped. The cost tests' fake Bash call takes time:
a call ending in the very moment another began is an overlap to runs.

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

hkaltai commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

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

const both: any[] = await Promise.all([
$.tool.call({ tool: 'Bash', command: 'dbt build -s first', tool_use_id: 'one' }),
$.tool.call({ tool: 'Bash', command: 'dbt build -s second', tool_use_id: 'two' }),
$.tool.call({ tool: 'Bash', command: 'dbt test -s first', tool_use_id: '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 fake artifact still says build while the call is now dbt test

The view(...) call above (line 342) answers with command: 'build', but the invocations are now dbt test. writer(invocations, 'build') finds neither a build nor a retry invocation and returns null, so nothing is recorded here even when the overlap check does not fire: expect(stored.has('runs')).toBe(false) on line 347 passes for two independent reasons. If a future change recorded a run and still emitted the overlap line, this test (named neither is recorded) would not catch it.

Pass the artifact's command as 'test' (e.g. view('inv-second', undefined, 'test', AFTER)) here and in the other rewritten overlap tests (lines 231, 355, 717, 745, 778), so that the "not recorded" assertion depends on the overlap rule alone.

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 056d443: the fake artifact of the six overlap tests in runs.test.tsx now answers test, matching the dbt test calls, so the only reason nothing is recorded is the overlap guard.

@kilo-code-bot

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

Copy link
Copy Markdown

Code Review Summary

Status: 1 Issue Found | Recommendation: No merge blockers; the single item is a corner of the new failure fallback

Overview

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

SUGGESTION

File Line Issue
plugins/altimate-code/hooks/features/cost.tsx 118 (New) A failed lookup answers $.session.root(), but callers cannot tell that from a folder they read, so a lookup that answered and then rejects reads as a project change when the session is in a subfolder: gate (243) runs leaveProject() and clears held/allowOnce/lastEstimate (even for a git status), and estimateWithin (701, 735) / startWhenIdle (488) discard the running estimate and start a second one, the adapter collision lines 48-49 forbid. Marking the fallback answer and keeping seenDir while it is used avoids both
Files Reviewed (2 files)
  • plugins/altimate-code/hooks/features/cost.tsx - 1 new suggestion; the new fallback reaches gate and /scan-cost as intended and the timeout path still loses the pass (116-124); the previous warning that a rejected lookup permanently refused every dbt run/build/show is resolved
  • plugins/altimate-code/hooks/features/cost.test.tsx - the two rewritten tests cover a permanently failing host (under-limit runs, over-limit is held, /scan-cost answers); no test covers a lookup that answers and then rejects, which is where the finding above applies

Notes: this pass reviewed 60db684 against ff811ae (3 added / 2 removed lines in cost.tsx plus two rewritten tests). The two author-declined items carried in the previous summary (the single 80 ms pool at 113 and the duplicated note recording at 310-313) are unchanged and not re-raised. No security issues found. Tests were not executed (read-only review); the suite result in the PR description was taken as given.

Previous Review Summaries (6 snapshots, latest commit ff811ae)

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

Previous review (commit ff811ae)

Status: 4 Issues Found | Recommendation: No merge blockers; the line-307 race is fixed, and the remaining items are optional (two carried, author-declined design choices)

Overview

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

WARNING

File Line Issue
plugins/altimate-code/hooks/features/cost.tsx 117 (Carried, author declined as fail-closed by design) within maps a rejection of $.session.cwd() to a timeout, so on a host that cannot answer the hook every dbt run/build/show is refused with could not be read in time ... Run the same command again: a retry that can never succeed, and /allow-scan cannot lift it because the deny at 251 precedes any hold. defer.tsx:439 and health.tsx:739 fall back to $.session.root()

SUGGESTION

File Line Issue
plugins/altimate-code/hooks/features/cost.tsx 310 (New) The new note-recording pairs at 310-311 and 345-346 are identical, including the inline as { label; bytes; display } cast; one OverNote type and a noteOver(label, maxBytes, display) helper would drop the duplicate cast and keep both over-limit paths recording the same shape that 371 reads back
plugins/altimate-code/hooks/features/cost.tsx 311 (New) No test reaches the interleaving this commit fixes (lost folder after an over-limit answer in one pass, failing estimate in a parallel pass for the same command, so overNow at 371 rather than stored at 370 decides); cost.test.tsx:329/:347 reach the hold through refuse's write and :1512 through an already-recorded hold
plugins/altimate-code/hooks/features/cost.tsx 113 (Carried, author declined as intended single-budget design) look.spent charges the elapsed time of every answered lookup against the one 80 ms, so a multi-invocation line (up to MAX_INVOCATIONS = 20, several lookups each) can be refused as unreadable although every lookup answered; cost.test.tsx:378 pins this for two 50 ms lookups
Files Reviewed (2 files)
  • plugins/altimate-code/hooks/features/cost.tsx - 1 warning + 1 suggestion carried (both declined by the author) and 2 new suggestions; the new lost-folder branch is correct: mine.note/overNow are set before unreadable()'s first await, refuse writes the same hold for the pass's own retry, and bashGate's finally (211-214) drops the note only after refuse has finished its hold write, so a parallel pass either sees the note or the recorded hold; the generation-changed case returns changedDeny without a hold exactly as the normal path at 344-347 does
  • plugins/altimate-code/hooks/features/cost.test.tsx - targeted check only (not in this increment): the retry tests at 329/347/1512 still pass their assertions through refuse's write or a recorded hold, which is why the new visibility is untested

Notes: this pass reviewed ff811ae against 1c1b129 (6 added lines in cost.tsx). Resolved since the previous review: the line-307 warning that an over-limit answer kept for an unreadable-folder refusal was invisible to a parallel pass until the hold write landed. No security issues found. Tests were not executed (read-only review); the suite result in the PR description was taken as given.

Fix these issues in Kilo Cloud

Previous review (commit 1c1b129)

Status: 3 Issues Found | Recommendation: No merge blockers; the two warnings are about the fail-closed unreadable-folder path

Overview

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

WARNING

File Line Issue
plugins/altimate-code/hooks/features/cost.tsx 308 (Carried) The over-limit answer is pushed into over before any await, but mine.note/overNow is not set there (unlike 339-341), so a parallel pass for the same command whose own estimate fails sees stored and overNow empty at 365-366 and lets a command this pass knows is over the limit run before refuse's hold write lands
plugins/altimate-code/hooks/features/cost.tsx 117 (Carried, author declined as fail-closed by design) within maps a rejection of $.session.cwd() to a timeout, so on a host that cannot answer the hook every dbt run/build/show is refused with could not be read in time ... Run the same command again: a retry that can never succeed, and /allow-scan cannot lift it because the deny at 251 precedes any hold. defer.tsx:439 and health.tsx:739 fall back to $.session.root()

SUGGESTION

File Line Issue
plugins/altimate-code/hooks/features/cost.tsx 113 (Carried, author declined as intended single-budget design) look.spent charges the elapsed time of every answered lookup against the one 80 ms, so a multi-invocation line (up to MAX_INVOCATIONS = 20, several lookups each) can be refused as unreadable although every lookup answered; cost.test.tsx:378 pins this for two 50 ms lookups
Files Reviewed (3 files)
  • plugins/altimate-code/hooks/features/cost.tsx - 2 warnings + 1 suggestion carried; the new refuse(deny) helper is correct: it merges the pending branch (359) and the unreadable refusal (303/309) into one hold-recording path, puts the pending branch's hold write under the same try/catch (282-284) that the unreadable path had, and its generation check is a no-op at 359 because 348 already returned. Removing the post-loop look.lost guard is safe: every in-loop lookup is followed by a lost check (303, 306), estimateWithin answers 'unreadable' whenever the pass is lost (691, 719) and the 727 after-check either returns at 306 or an answer the branch keeps, and sessionDir short-circuits once lost (112) - so a pass leaving the loop always has look.lost === false
  • plugins/altimate-code/hooks/features/cost.test.tsx - the two toBeDefined() assertions are now toContain('over the') and both retry paths do reach the over-limit deny (the hold recorded by refuse at 274-285 is read back at 365-368, and the deny at 407 always ends over the <limit> limit); traced for the stalled-lookup cases 5 (727) and 6 (305)
  • plugins/altimate-code/hooks/features/defer.test.ts - only a targeted check: the removed cwd = ROOT is redundant because start() assigns it at 237 before registering the session.cwd stub (238)

Notes: this pass reviewed 1c1b129 against 2bb03ed (2 files of test assertions, 1 test line, and a refactor in cost.tsx). Resolved since the previous review: the duplicated hold-recording block, the unreachable post-loop guard, the unconstrained retry assertions, and the redundant cwd = ROOT. No security issues found. Tests were not executed (read-only review); the suite result in the PR description was taken as given.

Previous review (commit 2bb03ed)

Status: 7 Issues Found | Recommendation: Address the two warnings before merge; the suggestions are optional

Overview

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

WARNING

File Line Issue
plugins/altimate-code/hooks/features/cost.tsx 307 (New) The over-limit answer is pushed into over before any await, but mine.note/overNow is not set (unlike 338-341), so a parallel identical call whose own estimate fails reads stored as null at 376 and lets the command known to be over the limit run before unreadable()'s hold write lands
plugins/altimate-code/hooks/features/cost.tsx 117 (Carried) A rejected $.session.cwd() is look.lost (within maps rejection to null), so a host that cannot answer the hook refuses every dbt run/build/show with could not be read in time ... Run the same command again — a retry that can never succeed, with no /allow-scan escape (the deny at 251 precedes any hold). defer.tsx:439 and health.tsx:739 fall back to $.session.root()

SUGGESTION

File Line Issue
plugins/altimate-code/hooks/features/cost.tsx 113 (Carried) look.spent charges every lookup of the pass against the one 80 ms, so a pass that reads the folder ~4 times per dbt invocation (up to the 20-invocation cap) is refused as unreadable although each lookup answered; cost.test.tsx:378 pins this for two 50 ms lookups
plugins/altimate-code/hooks/features/cost.tsx 271 (Carried) The hold-recording block repeats the pending branch (359-367) line for line, and the try/catch exists only in this copy: one shared refuse(deny) helper would also put the pending branch's write under the guard (a failed write there still lets a known-over-limit command run, since bounded passes a throwing gate)
plugins/altimate-code/hooks/features/cost.tsx 351 (Carried) Unreachable guard: estimateWithin answers 'unreadable' (702/730) handled at 302, the new lost branch at 305-309 returns, the loop's own read is checked at 305, and sessionDir short-circuits once lost (112), so a pass leaving the loop always has look.lost === false
plugins/altimate-code/hooks/features/cost.test.tsx 342, 358 (Carried) expect((await retry).deny).toBeDefined() accepts any refusal; the test's subject is that the retry is still held, so it should assert the hold message ('20.0 GiB' / 'over the'). The new loop test at 358 repeats the same unconstrained assertion
plugins/altimate-code/hooks/features/defer.test.ts 272 (Carried) cwd = ROOT repeats the assignment start() makes at line 237; it is the leftover of moving the session.cwd stub into the helper
Files Reviewed (3 files)
  • plugins/altimate-code/hooks/features/cost.tsx - 1 new warning + 1 warning and 3 suggestions carried; the new lost-folder branch keeps an over-limit answer in hand and records the hold correctly for a single pass (verified for lookups 5 and 6), and dropping the look.lost return in estimateWithin (738) is safe because every lost result still ends at 305 or 302
  • plugins/altimate-code/hooks/features/cost.test.tsx - 1 carried suggestion (now also at 358); the new [5, 6] loop was traced through its lookups, retained inflight answer, 70 s expiry and re-ask
  • plugins/altimate-code/hooks/features/defer.test.ts - targeted check only; 1 carried suggestion still present

Notes: this pass reviewed 2bb03ed against 702c5d5 (2 files, 22 added lines). The change is correct for the sequential case it targets: with the estimate's own after-check lost, estimateWithin now returns the answer instead of 'unreadable', and the gate's second read short-circuits to the new branch, which pushes the over-limit answer before unreadable() records the hold; an under-limit or pending answer still refuses without a hold, and taken/inflight carry no state across passes. No security issues found. Tests were not executed (read-only review); the suite result in the PR description was taken as given.

Fix these issues in Kilo Cloud

Previous review (commit 702c5d5)

Status: 6 Issues Found | Recommendation: Address the remaining warning before merge; the suggestions are optional

Overview

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

WARNING

File Line Issue
plugins/altimate-code/hooks/features/cost.tsx 117 (Carried) A rejected $.session.cwd() is look.lost (within maps rejection to null), so a host that cannot answer the hook refuses every dbt run/build/show with could not be read in time ... Run the same command again — a retry that can never succeed, with no /allow-scan escape (the deny at 251 precedes any hold). defer.tsx:439 and health.tsx:739 fall back to $.session.root()

SUGGESTION

File Line Issue
plugins/altimate-code/hooks/features/cost.tsx 113 (Carried) look.spent charges every lookup of the pass against the one 80 ms, so a pass that reads the folder ~4 times per dbt invocation (up to the 20-invocation cap) is refused as unreadable although each lookup answered; cost.test.tsx:362 pins this for two 50 ms lookups
plugins/altimate-code/hooks/features/cost.tsx 271 (Carried) The hold-recording block repeats the pending branch (353-362) line for line, and the new try/catch exists only in this copy: one shared refuse(deny) helper would also put the pending branch's write under the guard (a failed write there still lets a known-over-limit command run, since bounded passes a throwing gate)
plugins/altimate-code/hooks/features/cost.tsx 347 (Carried) Unreachable guard: estimateWithin answers 'unreadable' (698/726/734) handled at 302, every loop read is checked at 305, and sessionDir short-circuits once lost (112), so a pass leaving the loop always has look.lost === false
plugins/altimate-code/hooks/features/cost.test.tsx 342 (Carried) expect((await retry).deny).toBeDefined() accepts any refusal; the test's subject is that the retry is still held, so it should assert the hold message ('20.0 GiB' / 'over the'). started now pins the re-ask, but the deny reason is still unconstrained
plugins/altimate-code/hooks/features/defer.test.ts 272 (Carried) cwd = ROOT repeats the assignment start() makes at line 237; it is the leftover of moving the session.cwd stub into the helper
Files Reviewed (3 files)
  • plugins/altimate-code/hooks/features/cost.tsx - 1 warning + 3 suggestions carried; the incremental unreadable() change re-verified and the previous failed-hold-write warning is resolved
  • plugins/altimate-code/hooks/features/cost.test.tsx - 1 carried suggestion; the new retry test was traced through its lookups and estimate starts
  • plugins/altimate-code/hooks/features/defer.test.ts - targeted check only; 1 carried suggestion still present

Notes: this pass reviewed 702c5d5 against 961577c (2 files, 15 added lines). The new generation guard in unreadable() (cost.tsx:273) is correct: a change detected during the pass returns changedDeny before any hold is recorded, matching the other generation checks, and leaving refused unset lets the retry re-run afterShellChange as the voided answers require. The try/catch around the hold write resolves the earlier warning — the refusal is returned even when the atom write fails. The test's 70 s advance is enough to expire the 60 s answer keep and retrying window, so a is asked again and fails while the recorded hold keeps the retry refused; the started count of 2 confirms that path rather than the reused answer. Tests were not executed (read-only review); the suite result in the PR description was taken as given. No security issues found.

Fix these issues in Kilo Cloud

Previous review (commit 961577c)

Status: 6 Issues Found | Recommendation: Address the new warning before merge; the suggestions are optional

Overview

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

WARNING

File Line Issue
plugins/altimate-code/hooks/features/cost.tsx 117 (Carried) A rejected $.session.cwd() is look.lost, so a host that cannot answer the hook refuses every dbt run/build/show with could not be read in time ... Run the same command again — a retry that can never succeed, with no /allow-scan escape (the deny at 251 precedes any hold). defer.tsx:439 and health.tsx:739 fall back to $.session.root()
plugins/altimate-code/hooks/features/cost.tsx 274 (New) unreadable() records the hold with a bare await update(...); a gate that rejects is let through by bounded (tool-chain.ts:111), so a failed atom write runs the command the pass already knows is over the limit and returns no deny. The over-limit branch guards the same write (376-388)

SUGGESTION

File Line Issue
plugins/altimate-code/hooks/features/cost.tsx 113 (Carried) look.spent charges every lookup of the pass against the one 80 ms, so a pass that reads the folder ~4 times per dbt invocation (up to the 20-invocation cap) is refused as unreadable although each lookup answered; cost.test.tsx:359 pins this for two 50 ms lookups
plugins/altimate-code/hooks/features/cost.tsx 271 (New) The new helper repeats the pending branch's hold-recording block (349-359) line for line, deny text aside; one shared refuse(deny) helper would keep a single place that holds a refused command
plugins/altimate-code/hooks/features/cost.tsx 341 (Carried) Unreachable guard: estimateWithin answers 'unreadable' (692/720/728) handled at 296, the loop's own read is checked at 299, and sessionDir short-circuits once lost (112), so a pass leaving the loop always has look.lost === false
plugins/altimate-code/hooks/features/cost.test.tsx 340 (New) expect((await retry).deny).toBeDefined() accepts any refusal; the test's subject is that the retry is still held, so it should assert the hold message ('20.0 GiB' / 'over the')
plugins/altimate-code/hooks/features/defer.test.ts 272 (Carried) cwd = ROOT repeats the assignment start() makes at line 237; it is the leftover of moving the session.cwd stub into the helper
Files Reviewed (5 files)
  • plugins/altimate-code/hooks/features/cost.tsx - 1 new warning + 1 new suggestion; 2 carried findings re-verified (the look.spent budget and the unreachable post-loop guard)
  • plugins/altimate-code/hooks/features/cost.test.tsx - 1 new suggestion; the new two-model retry test was traced through its seven folder lookups and does exercise the new helper (without it the retry runs)
  • plugins/altimate-code/hooks/features/defer.test.ts - targeted check only; 1 carried suggestion still present
  • plugins/altimate-code/hooks/features/runs.test.tsx - targeted check only; the six overlap fixtures answer test, matching their dbt test calls
  • plugins/altimate-code/hooks/tool-chain.test.ts - targeted check only; session.cwd stub unchanged and correct

Notes: this pass reviewed 961577c against 381e9a9 (2 files, 33 added lines). The new unreadable() helper is correct in what it fixes: an over-limit answer in hand survives the refusal, and the retry's failed estimate can no longer free the command (refused is refreshed so the retry skips afterShellChange, matching the pending branch). The generation ordering is safe — a detected /cd bumps the generation, so no answer from the project left is pushed into over. Tests were not executed (read-only review); the suite result in the PR description was taken as given. No security issues found.

Fix these issues in Kilo Cloud

Previous review (commit 381e9a9)

Status: 4 Issues Found | Recommendation: Address the warning before merge; the three suggestions are optional

Overview

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

WARNING

File Line Issue
plugins/altimate-code/hooks/features/cost.tsx 117 (New) A rejected $.session.cwd() is now look.lost, so a host that cannot answer the hook refuses every dbt run/build/show with could not be read in time ... Run the same command again — a retry that can never succeed, with no /allow-scan escape (the deny at 251 precedes any hold). Base main and 71844b9 let the pass go on; defer.tsx:439 and health.tsx:739 fall back to $.session.root()

SUGGESTION

File Line Issue
plugins/altimate-code/hooks/features/cost.tsx 113 (Carried, still present) look.spent charges every lookup of the pass against the one 80 ms, so a pass that reads the folder ~4 times per dbt invocation (up to the 20-invocation cap) is refused as unreadable although each lookup answered; the test at cost.test.tsx:344 pins this for two 50 ms lookups
plugins/altimate-code/hooks/features/cost.tsx 326 (Carried, still present) Unreachable guard: estimateWithin answers 'unreadable' (denied at 281) and the loop's own read is checked at 284, while the continue (277) and both breaks (291, 305) make no lookup, so a pass leaving the loop always has look.lost === false
plugins/altimate-code/hooks/features/defer.test.ts 272 (New) cwd = ROOT repeats the assignment start() makes at line 237; it is the leftover of moving the session.cwd stub into the helper
Files Reviewed (5 files)
  • plugins/altimate-code/hooks/features/cost.tsx - 1 new warning; 2 carried suggestions re-verified against current code
  • plugins/altimate-code/hooks/features/cost.test.tsx - no new issues; the failed-lookup and shared-budget tests were checked (the extra $.session.start in the fresh-session test is harmless, since nothing read the folder before it)
  • plugins/altimate-code/hooks/features/defer.test.ts - 1 new suggestion (redundant cwd = ROOT)
  • plugins/altimate-code/hooks/features/runs.test.tsx - no new issues; the six overlap tests now answer test, matching their dbt test calls
  • plugins/altimate-code/hooks/tool-chain.test.ts - no new issues; the session.cwd stub is correct

Notes: this pass reviewed 381e9a9 against 71844b9 (the merge of main in 9d7bf77 is not part of this PR). All earlier warnings are resolved: the module-global unreadable counter is gone, a stalled pass leaves seenDir and the hold/allowance alone, and the refusal is reported as an unreadable folder rather than an estimate still running. The only behavioral change since the last review is that a failed lookup now loses the pass even in a fresh session, which is the warning above. Tests were not executed (read-only review); the suite result in the PR description was taken as given.

Fix these issues in Kilo Cloud

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


Reviewed by deepseek-v4.1-flash-1017 · Input: 98K · Output: 21.9K · Cached: 1.3M

hkaltai and others added 3 commits November 18, 2026 17:19
…dget of the gates after the cost gate

The cost gate waited for the session's folder with no bound, so a stalled lookup spent the whole
phase budget and a later gate (the health push gate) never ran. It now waits 80 ms.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…never answered

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…: the gate reuses none of what it learned

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

/** The session's folder, or `fallback` when it cannot be read within CWD_WAIT_MS. */
async function sessionDir($: EngineInterface, fallback: string | null): Promise<string | null> {
return (await within($, CWD_WAIT_MS, $.session.cwd())) ?? fallback

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 timed-out lookup is treated as the same project as last time, so a /cd slower than 80 ms is missed. within(...) ?? fallback collapses two different answers into one: the folder is X, and I could not find out. Callers then treat the fallback as knowledge. At cost.tsx:214 a stalled lookup yields dir === seenDir, so the /cd branch at 215 never runs and leaveProject is skipped: the previous project's held, allowOnce and estimates survive into the new project. At cost.tsx:673 the fallback is the module-global seenDir, so running.dir !== here is false and an answer made in the old project is accepted for the new one, which is exactly the reuse that line's comment says it prevents. This needs only $.session.cwd() to take over 80 ms on a busy host or right after a /cd, and seenDir is shared by concurrent Bash passes, so one pass can fall back to another pass's folder. Consider keeping unknown distinct from the previous folder: cache the folder once per pass and, when it cannot be read, skip the equality checks or invalidate rather than reuse the global seenDir.

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 056d443: a lookup that misses the budget no longer answers a value the callers read as knowledge. sessionDir (cost.tsx:105-120) marks the pass lost (look.lost); the gate (cost.tsx:228-240) then refuses any scanning command and touches no project state. A /cd missed during a stall is seen by the next pass that can read the folder, which runs leaveProject then. Regression: 'a folder that cannot be read is not a change of project...' in cost.test.tsx.

const ANSWER_KEEP_MS = 60_000

/** The folder the session is in is asked for this long: a lookup that stalls must not use up the budget of the gates after this one (health's wait is 4 s of the 5 s a phase has). */
const CWD_WAIT_MS = 80

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 80 ms bound is per lookup, not per command, so a multi-invocation line can still spend the phase budget. gate waits once at cost.tsx:214, then for each dbt invocation on the line estimateWithin waits at 639 and 673, startWhenIdle at 430, and gate checks again at 252: up to four more 80 ms waits per invocation when the lookup stalls. A chain such as dbt build -s a && dbt build -s b && dbt build -s c therefore burns roughly 3 x 320 ms plus 80 ms, which is the whole ~1 s this gate should leave out of the 5 s phase for health's 4 s wait (comment at line 100, GATE_WAIT_MS at line 28); a longer chain (MAX_INVOCATIONS is 20) can again consume the phase, the failure this change fixes. Reading the folder once per pass, or memoizing a timeout for the rest of the pass, would keep the bound independent of how many invocations the line names.

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 056d443: the lookups of one command pass share one 80 ms budget (Look.spent, cost.tsx:105-120), threaded through gate, estimateWithin and startWhenIdle, so a multi-invocation line cannot spend more than 80 ms on the folder in total. Regression: 'the folder lookups of one command share one 80 ms budget' in cost.test.tsx (fails on the previous head).

if (!refused.has(e.command)) await afterShellChange($, e.command)
// A session moved to another project (a /cd) keeps nothing the gate learned in the last one.
const dir = await $.session.cwd().catch(() => seenDir)
const dir = await sessionDir($)

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: a folder that could not be read is stored as the project, so the next pass drops the state of a project the session never left

sessionDir now answers UNKNOWN_DIR when $.session.cwd() misses the 80 ms, and line 222 stores that sentinel in seenDir. An unknown is not a folder, so two things follow. (a) On the stall itself, line 218 sees dir !== seenDir and calls leaveProject, clearing the hold, allowOnce and lastEstimate. (b) Worse, seenDir is left as UNKNOWN_DIR, so the next pass whose lookup succeeds compares the real folder with the sentinel, calls leaveProject again and clears whatever the stalled pass recorded - including an /allow-scan the user granted between the two passes. That retry is then refused again and the allowance is silently gone, although no /cd ever happened. Lines 255-258 do the same for a stall mid-command.

When the folder cannot be read, leaving seenDir as it is (if (dir !== UNKNOWN_DIR) seenDir = dir, and nothing to compare against while it is unknown) keeps the no-reuse property without discarding the state of the project the session is still in.

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 056d443: the unknown sentinel is gone. An unreadable folder answers the last known folder (seenDir) and the pass is lost; seenDir is only written when the lookup was read (cost.tsx:228-238), so the next pass does not see a project change and the hold, allowance and estimate survive. Regressions: 'the allowance of the project the session is in survives a stalled pass' and 'a stalled pass is not remembered as a project' (both fail on the previous head).

let dir: string | null = null
for (;;) {
dir = (await $.session.cwd().catch(() => seenDir)) ?? null
dir = (await sessionDir($)) ?? 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: an unread folder makes the running-estimate check miss the estimate of the same project

active records the folder each estimate was started in, and this filter only waits for entries whose folder equals dir. When the lookup stalls, dir is UNKNOWN_DIR while an estimate started while the folder was readable carries the real path, so nothing matches and begin starts a second dbt_cost_estimate in the same project - the collision the one-estimate-per-project rule (line 49, comment at line 650) exists to prevent. The same mismatch at line 642 drops the inflight entry, so a concurrent pass can start it too. Treating a stalled lookup as "matches whatever is running" for this wait, rather than as a different project, keeps the serialization while the folder is unknown.

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 056d443 by the same change: a lost pass starts no estimate (startWhenIdle returns null on look.lost before the running-estimate filter, cost.tsx:~463) and is refused as unreadable, so no second dbt_cost_estimate can start in the project; the folder compared is the last known one, not a sentinel.

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.
const now = (await $.session.cwd().catch(() => dir)) ?? null
const now = (await sessionDir($)) ?? 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: the ?? null is dead now that sessionDir returns string

sessionDir never returns null any more (it answers UNKNOWN_DIR), so ?? null here and at lines 433, 642 and 676 (and dir ?? null on the next line) can never apply, and they read as if a null folder were still possible. Dropping them - const now = await sessionDir($) and if (now !== dir) - shows the unknown case instead of hiding it behind a null that no longer exists.

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 056d443: every ?? null on sessionDir is removed (it returns string | null and the lost state is carried by look.lost).

…oject each time, so no verdict is carried from one command to the next

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
* it never equals one the gate has seen, so no verdict, estimate or allowance is reused for it, and a pass that asks twice is refused as changed.
*/
async function sessionDir($: EngineInterface): Promise<string> {
return (await within($, CWD_WAIT_MS, $.session.cwd())) ?? `\u0000unknown-${++unreadable}`

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: a fresh unknown folder on every timeout makes a stalled lookup look like a project change on every pass

sessionDir now answers a new sentinel each time $.session.cwd() misses the 80 ms, so every place that uses the folder as the project's identity is unequal for as long as the lookup stalls: (a) seenDir (lines 219-224) differs on every pass, so leaveProject runs, and then the re-check at lines 257-261 gets yet another fresh sentinel and runs it again. leaveProject calls invalidate() (bumping generation and clearing inflight) and drops held, allowOnce and lastEstimate for the project the session is still in - even for a Bash call that scans nothing - and the pass then ends in changedDeny (lines 295-298) with "The project changed..." although nothing changed. (b) startWhenIdle (line 436) can never match a running estimate, and estimateWithin (lines 647 and 678) always discards inflight, so every retry of the same stalled command starts another dbt_cost_estimate beside one still running - the adapter collision the one-estimate-per-project rule (line 49, comment at 652) exists to prevent - and a finished answer is never taken.

Keeping the folder unknown without treating it as a change (skip the seenDir comparison while the lookup fails, and let a running estimate match for the wait) keeps the no-reuse property without wiping the real project's state or spawning an estimate per pass. A test that one stalled command asks for exactly one estimate would cover (b).

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 056d443: no sentinel is minted any more, so a stall is never an unequal project. leaveProject runs only on a read folder that differs from seenDir, and the re-check inside the loop bails out when lost instead of running it again (cost.tsx:~284).

…scan, and a failed lookup keeps the last folder as before

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
if (!refused.has(e.command)) await afterShellChange($, e.command)
// A session moved to another project (a /cd) keeps nothing the gate learned in the last one.
const dir = await $.session.cwd().catch(() => seenDir)
const dir = await sessionDir($, seenDir)

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: a folder that cannot be read still counts as a project change, so the hold and the allowance of the project the session is in are dropped

sessionDir answers a fresh \u0000unknown-N on every lookup that misses the 80 ms, so dir never equals seenDir here: line 225 runs leaveProject(), which invalidates the generation and clears held, allowOnce and lastEstimate for the project the session never left. This happens for any Bash call, not only a dbt one - a git status whose lookup stalls returns at line 232, after the wipe. Concretely, a user's /allow-scan is silently lost if the next Bash call's $.session.cwd() misses the 80 ms, and the retried dbt run is held again although the allowed command never ran. Line 229 then stores the sentinel in seenDir, so the next pass with a readable folder compares a real path with it, runs leaveProject a second time, and a stall in the middle of a pass (line 264) ends in changedDeny "The project changed while the scan was being estimated" although nothing changed.

Skipping the seenDir comparison while the folder is unknown (leaving seenDir as it was, as the earlier single-sentinel fix did) keeps the no-reuse property without discarding the real project's state: the deny at line 234 already stops any verdict or estimate for an unknown folder. A test that runs /allow-scan, then a stalled git status, then the held command would cover 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.

Fixed in 056d443: a lookup that cannot be read is not a project change; leaveProject is skipped for a lost pass (cost.tsx:228-238), for any Bash call. Regression: 'the allowance of the project the session is in survives a stalled pass' runs a stalled git status and checks the held command still runs once.

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.
const now = (await $.session.cwd().catch(() => dir)) ?? null
const now = (await sessionDir($, dir)) ?? 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: the ?? null can no longer apply, and hides the unknown folder behind a null that is never used

sessionDir now returns string | null on every path (line 112), so (await sessionDir($, dir)) ?? null maps null to null. The same dead ?? null is at lines 442, 653 and 687. const now = await sessionDir($, dir) shows the unknown case directly, so a stalled lookup is compared as the sentinel it is rather than through a nullable wrapper.

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 056d443: dead ?? null removed at all four sites.

dir = (await $.session.cwd().catch(() => seenDir)) ?? null
dir = (await sessionDir($, seenDir)) ?? null
// An unreadable folder starts no estimate: it could be the project one is already running in.
if (isUnknown(dir)) return 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: an unreadable folder is reported to the user as "another estimate is still running"

startWhenIdle already returns null when the wait runs out with an estimate still running, and /scan-cost prints that reason for it: "No estimate: another estimate is still running in this project. Try again in a moment." (line 146). A folder that misses the 80 ms now returns null from the same place, so /scan-cost tells the user an estimate is running when none is. Returning a distinct value, or checking isUnknown in the caller, lets /scan-cost say that the folder could not be read.

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 056d443: a lost lookup is told apart from a running estimate. /scan-cost answers 'No estimate: the project folder could not be read in time. Try again in a moment.' (UNREADABLE_TEXT, cost.tsx:~110, ~157). Regression: 'a folder that cannot be read is said so, not reported as an estimate still running'.

…imate is asked for refuses the scan

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
const here = (await $.session.cwd().catch(() => seenDir)) ?? null
const here = (await sessionDir($, seenDir)) ?? null
// Which project this is cannot be told: nothing is reused, nothing starts, and the command is refused until it can be.
if (isUnknown(here)) return 'pending'

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 stalled lookup is reported as an estimate still running

'pending' is the value estimateWithin uses for "an estimate is running and not done by waitUntil" (lines 664, 682, 687), and the gate's pending branch (line 324) tells Claude "The scan of is still being estimated (the dbt adapter may be starting). Run the same command again in a moment." On this path no estimate was started and none is running: only the folder lookup missed the 80 ms. The new test's stall at lookup 3 receives exactly that message and its deny assertion accepts it. Returning a distinct value (or checking isUnknown in gate) would let the deny reuse line 234's "The project folder could not be read in time..." reason, so the user is not told to wait for an estimate that does not exist.

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 056d443: estimateWithin now answers 'unreadable' for a lost folder, distinct from 'pending'; the gate turns it into the unreadable refusal, not 'still being estimated' (cost.tsx:~280). Same regression test as above (gate deny asserted not to contain 'still being estimated').

})

// The lookups the gate makes, one after another: whichever one is not answered, the project is not known and the command is refused.
for (const n of [2, 3, 4, 5, 6]) {

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: only one of the five lookups in this loop reaches the guard added in this PR

For dbt run -s orders the session-folder lookups are: runs' bashRewrite (1), cost's gate at 224 (2), estimateWithin at 653 (3), startWhenIdle at 442 (4), the re-check at 689 (5), and the gate's second read at 264 (6). On the pre-change code a stall at 2 is denied at cost.tsx:234, at 4 by startWhenIdle's existing isUnknown check (444), at 5 by the running.dir comparison at 689, and at 6 by changedDeny (302), so cases 2, 4, 5 and 6 deny without this change, and case 2 repeats the test at line 218. Only case 3 covers the new if (isUnknown(here)) return 'pending'. Because the assertion is just deny toBeDefined, it cannot tell those denial reasons apart - and case 3 currently accepts the "still being estimated" message for a command whose estimate never started. Assert the exact message per case (or that estimates/started stay empty) and drop the cases that duplicate existing coverage; the numbering also silently depends on how many features call $.session.cwd().

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.

Addressed in 056d443: the loop over lookups 2 to 6 now asserts the refusal says the folder could not be read, so each stalled lookup is pinned to the unreadable guard and not to the 'still being estimated' path. (Lookup 1 is not cost's: a stall there does not reach this feature.) The remaining guards for each lookup are the look.lost checks in gate, estimateWithin and startWhenIdle.

…whole pass, whatever later answers say

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
// Nothing is cleared here: the change already cleared what was older, and a newer pass's estimates are its own.
return changedDeny(e.command)
}
if (unreadable !== lostBefore) return { deny: UNREADABLE_DENY }

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 "lost folder" test is a module-wide counter, so a stall in one pass refuses unrelated passes

unreadable (line 103) is module-global: every stalled lookup in any pass increments it, while lostBefore (line 254) is captured per pass. With two Bash calls in flight - the case the tests themselves exercise with Promise.all - a stall at line 267, 446, 657 or 693 in pass A bumps the counter, and pass B, whose own $.session.cwd() answered every time and which captured lostBefore before A's stall, reaches this line, sees unreadable !== lostBefore, and returns UNREADABLE_DENY. B is then refused with "The project folder could not be read in time" although it read the folder fine, and an under-limit estimate it already had in hand is discarded; the dbt run does not happen and must be retried. Tracking the loss per pass (a pass-local flag set wherever isUnknown(...) is observed, or returning { dir, lost } from sessionDir) would keep this guard to the pass's own lookups.

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 056d443: the module-global counter is removed; the lost state is a per-pass Look. Regression: 'a lookup lost in one pass does not refuse another pass whose own lookups all answered' (fails on the previous head).

…nce, before the rest of the line is worked through

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
const pending = $.tool.call(bash(line)) as Promise<any>
for (let i = 0; i < 70; i++) await clock.advance(100)
expect(((await pending) as any).deny).toBeDefined()
expect(started.length).toBeLessThan(3)

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: toBeLessThan(3) accepts the regression this test is named for

The line names 20 models, so "the later models are not worked through first" means no estimate after the stalled lookup starts. estimateWithin answers pending for a lost lookup before startWhenIdle (cost.tsx:661), so with the new guard at cost.tsx:267 started stays empty; but <3 also passes if the pass works through one or two more models, which is exactly the behavior the test exists to pin. Assert the empty array instead.

Suggested change
expect(started.length).toBeLessThan(3)
expect(started).toEqual([])

Related: n: 3 counts every $.session.cwd() on the line - runs' bashRewrite (runs.tsx:684) is lookup 1, cost's gate (cost.tsx:225) is 2 - so if that count changes, lookup 3 becomes the gate's own read, the pass is refused at cost.tsx:235, and this test passes without ever reaching the new guard.

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 056d443: the 20-model test now asserts started equals [].

@hkaltai

hkaltai commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

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

@hkaltai

hkaltai commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

@saravmajestic re-requesting your review: the PR also bounds the cost gate's folder lookup now (details in the description).

hkaltai and others added 4 commits November 18, 2026 19:05
…dget per pass, said as such to the user

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… the scan like one that times out

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…efer tests' fake host answers the folder

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
*/
async function sessionDir($: EngineInterface, look: Look, fallback: string | null): Promise<string | null> {
if (look.lost) return fallback
const left = CWD_WAIT_MS - look.spent

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 one 80 ms pool is spent by lookups that answered, so a multi-invocation line is refused as unreadable

look.spent accumulates the real time of every $.session.cwd() in the pass, and a pass reads the folder several times per dbt invocation: gate at 239 and 284, estimateWithin at 676 and 713, startWhenIdle at 465. Once that sum reaches CWD_WAIT_MS, left is 0 or less, read is null, look.lost is set, and a command whose folder was read successfully on every lookup is refused with The project folder could not be read in time (252, 282, 285). At four lookups per invocation that leaves roughly 6 ms per lookup for a three-invocation line and under 2 ms at the MAX_INVOCATIONS cap of 20; the test at cost.test.tsx:332 pins the same outcome for two 50 ms lookups. Charging only the lookup that goes unanswered, and keeping the phase-budget property from the thread on line 101 with a separate bound, avoids reporting a host whose lookups work but cost a few ms as unreadable.

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 single pool is the requested design (bound the whole command's lookups, cost.tsx:105-120), and a lookup that answers takes well under a millisecond on a live host, so a command's lookups reach 80 ms in total only when the host is stalling; refusing then is the intended fail-closed outcome. The 50 ms test pins exactly that.

// Nothing is cleared here: the change already cleared what was older, and a newer pass's estimates are its own.
return changedDeny(e.command)
}
if (look.lost) return { deny: UNREADABLE_DENY }

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 guard can no longer be reached

Since look is per pass now, every lookup inside the loop already ends the pass when it is lost: estimateWithin answers 'unreadable' (678, 706, 714) and that is turned into this deny at 282, and the loop's own sessionDir (284) is followed by the check at 285. The continue at 277 makes no lookup, and both breaks (291, 305) come after the 285 check, so a pass that leaves the loop always has look.lost === false and this line never runs. The initial-lookup guard at 252 and the in-loop ones already cover every read; dropping this one leaves a single place per lookup that decides the refusal.

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 1c1b129: the post-loop guard is removed; every lookup is already checked in the loop.

// Read from the process clock, not the engine's: no await is added between the gate's steps that must stay one.
const began = Date.now()
// A lookup that fails or does not answer in time loses the pass: the folder is not known, and the last one is not assumed.
const read = left > 0 ? await within($, left, $.session.cwd().then(dir => ({ dir: dir as string | null }))) : 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.

WARNING: a rejected lookup is a permanent block, and "Run the same command again" cannot help

within turns both a timeout and a rejection of $.session.cwd() into null, so every rejection now sets look.lost and every dbt run/build/show is refused with The project folder could not be read in time, so the scan cannot be estimated. Run the same command again. (line 251). A timeout is transient, but a host that does not answer this hook, or a session whose folder cannot be resolved, rejects on every call: the retry the message asks for can never succeed, and /allow-scan cannot help either, because the deny at 251 happens before any hold or allowance is read. Nothing in the feature escapes this: the limit defaults to 10 GiB, so the cost gate is on by default.

Base main answered .catch(() => seenDir) and let the pass go on, and the commit before this one (71844b9) still went on in a fresh session, answering { dir: null } when no folder had been seen yet. The sibling features also treat this hook as fallible: defer.tsx:439 and health.tsx:739 both .catch() it and fall back to $.session.root(). Falling back to $.session.root() for a rejection (while still refusing on a real stall) would keep the block for an unknown project without making every dbt scan impossible on a host where session.cwd fails; at minimum the message should not promise that retrying will 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: a rejected folder lookup leaves the project unknown, and the gate is a safety gate that must fail closed. Letting a rejection fall back to the last folder let a verdict of the project left (or an estimate under another folder) authorise a scan, which the review panel reproduced as MAJOR twice on earlier heads (cost.tsx:105-120, test 'a folder lookup that fails ...' in cost.test.tsx). A live host always answers session.cwd; the unanswerable case is a stalled or broken host, where refusing is correct.

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 60db684 per the owner's decision: a session.cwd() lookup that rejects outright (a host without it) falls back to the session root folder, as before, and the command goes through the gate normally; only a lookup that times out loses the pass and refuses the scan (cost.tsx:105-122). Regressions: 'a host whose session folder lookup always fails ...' and the /scan-cost variant in cost.test.tsx.

const seen = start($, on, DEFER, { times: 0 }, [ROOT, `${ROOT}/dbt_packages/pkg`, `${ROOT}/nested`])
let cwd = ROOT
on('session.cwd', () => ({ value: cwd }))
cwd = ROOT

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: cwd = ROOT is redundant, start() already sets it

start() assigns the module-level cwd to ROOT (line 237) before registering the session.cwd stub, so this line repeats what the call on the line above has just done. It is the leftover of moving the stub out of this test into the helper; dropping it leaves the test's own folder setup as just the cwd = ${ROOT}/dbt_packages/pkg and cwd = ${ROOT}/models changes below.

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 1c1b129: the redundant cwd = ROOT is removed from defer.test.ts.

…swer already in hand

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
const unreadable = async () => {
if (over.length > 0) {
const worst = over.reduce((x, y) => (y.bytes > x.bytes ? y : x))
await update($, costAtom, prev => {

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: a hold write that fails lets the over-limit command run

unreadable() records the hold with a bare await update(...). The chain runs each gate through bounded (tool-chain.ts:111) and lets the call go unchanged when the gate rejects, so if this atom write fails the command the pass already knows is over the limit runs unchecked and no deny is returned at all. The file states that contract at cost.tsx:206-208, and the over-limit branch guards exactly this write for that reason (try { await update(...) } catch { ... return { deny: ... } }, 376-388); rememberEstimate wraps its write too (739-743). The deny is already in hand here, so catching the failure and still returning { deny: UNREADABLE_DENY } keeps the hold-and-refuse property.

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 702c5d5 (kept in 1c1b129): the hold write in the refusal helper is in a try/catch, so the unreadable-folder deny is returned even if the atom write fails.

const pending: string[] = []
// The refusal for a folder that could not be read keeps what the pass already found over the limit, and marks the command refused:
// its retry is no change, and a failed estimate then cannot free a command already known to be expensive.
const unreadable = async () => {

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 repeats the pending branch's hold-recording block

The body of this helper is cost.tsx:349-359 a second time: the same over.reduce over bytes, the same update with allowOnce: same && current.allowOnce and the same held shape, then the same notes.delete(e.command) and refused.set(e.command, await $.clock.now()). Only the deny text differs. One helper - const refuse = async (deny: string) => { /* record the hold, drop the note, mark refused */ return { deny } } - used here and by the pending.length > 0 branch would leave a single place that decides how a refused command is held, and would put the hold write under one try/catch (see the next comment).

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 1c1b129: one refuse(deny) helper (cost.tsx:~269-290) records the hold, drops the note and marks the command refused; the pending branch and the unreadable refusal both use it.

// Nothing is cleared here: the change already cleared what was older, and a newer pass's estimates are its own.
return changedDeny(e.command)
}
if (look.lost) return unreadable()

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 guard cannot run

Every lookup of the pass already ends it when lost: estimateWithin answers 'unreadable' (692, 720, 728) which returns at 296, the loop's own sessionDir is checked at 299, and sessionDir returns early once look.lost is set (112), so no later lookup can lose a pass whose loss was not already handled. The continue at 291 makes no lookup, and both breaks (305, 319) come after the check at 299, so a pass that leaves the loop always has look.lost === false and this line never executes. The check at 251 and the in-loop ones cover every read; dropping this one leaves one place per lookup that decides the refusal.

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 1c1b129: the unreachable guard is removed.

options.stallNth = undefined
const retry = $.tool.call(bash(command)) as Promise<any>
for (let i = 0; i < 70; i++) await clock.advance(100)
expect((await retry).deny).toBeDefined()

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: toBeDefined() cannot tell the hold from any other refusal

The point of this test is that the retry is still held for the over-limit answer the first pass had in hand, but every denial passes here - still being estimated, the project changed, or the hold. ran does catch the unfixed behaviour (the command would run), yet the message is the behaviour under test: assert it, e.g. expect((await retry).deny).toContain('20.0 GiB') or 'over the', so a retry that loses the hold and is refused for another reason fails here.

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 1c1b129: both retry tests now assert the hold's own message (over the), so another kind of refusal no longer passes.

hkaltai and others added 2 commits November 18, 2026 19:36
… changed meanwhile, and stands if the hold cannot be recorded

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ds 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 2bb03ed (reviewers: gpt-6-astra high, gpt-6.1-sol high)

const now = await sessionDir($, look, dir)
if (look.lost) {
// The answer in hand is still the project's: one over the limit is kept, so the refusal holds the command.
if (answer !== 'pending' && answer.kind === 'bytes' && answer.maxBytes > limitBytes) over.push({ label, bytes: answer.maxBytes, point: answer.bytes, display: answer.display })

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 over-limit answer is pushed here but not noted for a parallel pass, so the hold is invisible until unreadable()'s write lands

The push happens before any await, yet the hold only becomes visible to another pass once unreadable() finishes its update (line 277). The normal over-limit path notes the answer first (mine.note / overNow.set(e.command, ...), 338-341), whose comment says a parallel pass for this command whose own estimate fails must still see it. A second concurrent call for the same command can start its own estimate and get a failing one (see the test at cost.test.tsx:1512, where both calls ask), so at line 376 stored is still null and overNow holds no note for this command: it falls through to line 430 and the command this pass knows is over the limit runs. Setting mine.note and overNow before over.push, as the normal path does, keeps this branch as safe as the other 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 ff811ae: the over-limit answer kept for the unreadable-folder refusal is noted in mine.note / overNow before the push into over, as on the normal path, so a parallel pass for the same command sees it before the hold write lands.

…able post-loop guard, and tighter retry assertions

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 1c1b129 (reviewers: gpt-6-astra high, gpt-6.1-sol high)

…sible to a parallel pass at once

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
// The answer in hand is still the project's: one over the limit is kept, so the refusal holds the command.
if (answer !== 'pending' && answer.kind === 'bytes' && answer.maxBytes > limitBytes) {
// Noted before any await, as on the normal path: a parallel pass for this command whose own estimate fails must still see it.
mine.note = { label, bytes: answer.maxBytes, display: answer.display }

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 repeats the normal path's note-recording block verbatim

Lines 310-311 are the same two statements as cost.tsx:345-346 (mine.note = { label, bytes: answer.maxBytes, display: answer.display }, then overNow.set(e.command, mine.note as { label: string; bytes: number; display: string })), including the inline cast. Naming the shape once (type OverNote = { label: string; bytes: number; display: string }, new Map<string, OverNote>()) and moving the two statements into a small noteOver(label, maxBytes, display) used here and at 345-346 would remove the duplicate cast and keep both over-limit paths recording the same fields, which matters because the note is read back at 371 and its bytes/display become the recorded hold.

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: two statements, in two branches that must stay separately readable (the normal path also records the estimate afterwards and clears the note differently); a shared helper would add indirection for no behaviour change. Not worth a type alias in this fix.

if (answer !== 'pending' && answer.kind === 'bytes' && answer.maxBytes > limitBytes) {
// Noted before any await, as on the normal path: a parallel pass for this command whose own estimate fails must still see it.
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: no test pins the note-before-write visibility this commit adds

One interleaving now decides whether an over-limit command runs: this pass loses the folder after its over-limit answer, and a parallel pass for the same command gets a failing estimate and reads overNow (371) because stored (370) is still null. Nothing in cost.test.tsx reaches it: :329 and :347 reach the hold through refuse's own write, and :1512 succeeds because the hold was already recorded. A test that stalls the lookup after the over-limit answer in one pass while the second pass's estimate fails would fail on the previous head (the second pass runs the command there) and pass here, pinning exactly the change in 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 added: the interleaving (one pass loses the folder after its over-limit answer while a parallel pass for the same command gets a failing estimate within the hold write's await) cannot be timed deterministically with the fake host, and the fix is the same two statements the normal path already pins ('a parallel call whose estimate fails is held by the hold another call recorded meanwhile', cost.test.tsx:~1512). The panel approved at ff811ae.

@hkaltai

hkaltai commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

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

…ot; only one that times out refuses the scan

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
const began = Date.now()
// A lookup that does not answer in time loses the pass: the folder is not known, and the last one is not assumed.
// One that fails outright (a host with no session folder) is the session's root, as before: the gate goes on with it.
const failed = async () => ({ dir: (await $.session.root().catch(() => fallback)) as string | 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: a failed lookup answers the root as if it were the folder read, so a host whose lookup starts failing reads as a project change

sessionDir now answers $.session.root() when $.session.cwd() rejects, and callers cannot tell that answer from a folder they actually read. When the session is in a subfolder - defer.tsx:438 notes that cwd moves with a cd while root does not - the two differ, and every identity decision in this file compares the root with the real path:

  • gate (243) sees dir !== seenDir and calls leaveProject(), clearing held, allowOnce and lastEstimate and bumping generation for the project the session never left, even for a git status. A user's /allow-scan is lost, and a held command whose next estimate fails (372-376) then runs.
  • estimateWithin (701, 735) finds running.dir !== here for an estimate started while the lookup still worked, so a finished answer is discarded, and startWhenIdle (488) does not match it in active either: a second dbt_cost_estimate starts beside the running one, the adapter collision the one-per-project rule (48-49) exists to prevent.

A permanently failing host is unaffected (the root is the same every pass); the harm needs a lookup that answered, then rejects. Returning whether the answer came from the fallback (e.g. { dir, fellBack }) and, while it did, keeping seenDir and skipping the equality decisions above would keep the scan unblocked without discarding the state of the project the session is still in - it would also drop the redundant as string | null cast at 118. The new tests only exercise cwdFails from the start, so this interleaving is not covered.

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, by the owner's decision: a lookup that rejects outright (a host without session.cwd) falls back to the session root, the old behaviour, so the gate keeps working on such hosts; a host that fails every call fails the same way every time, so root is consistently the project there. A host that answers cwd and then starts rejecting is not a supported case. Only a timeout loses the pass (cost.tsx:105-122).

@hkaltai

hkaltai commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

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

LGTM. Locally claude plugin test plugins/altimate-code gives 832 pass, 0 fail on this head, and 1128 pass, 0 fail merged with current main (health check included). The test fixes explain the 10 failures from the #24/#27 overlap, and the bounded folder lookup no longer lets a stalled lookup spend health's budget.

Non-blocking, on Kilo's open note at cost.tsx:113: the 80 ms budget is also spent by lookups that answered. With a probe test (each session.cwd answering after a real delay, fake clock not advanced, as in the shared-budget test), cost does about 4 lookups per dbt invocation. At 3 ms per lookup a line of 6 chained dbt runs is refused as "could not be read in time", and at 8 ms 3 are. The refusal repeats on every retry and comes before the /allow-scan check, so the only way through is splitting the line. Whether it matters depends on the real $.session.cwd() latency; if that's sub-millisecond it would take a line of about 20 invocations. If it's worth handling, one option is to bound each lookup by 80 ms (or reuse a successful read within the pass) and keep the total cap for lookups that actually stall.

@hkaltai
hkaltai merged commit e014863 into main Nov 19, 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