From 1c808470aea023a437ecd827a15082091d8e717e Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 16:42:36 +0530 Subject: [PATCH 01/18] test: cost and runs tests pass with both features on 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 --- .../hooks/features/cost.test.tsx | 2 ++ .../hooks/features/runs.test.tsx | 24 ++++++++++--------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/plugins/altimate-code/hooks/features/cost.test.tsx b/plugins/altimate-code/hooks/features/cost.test.tsx index 38f7d808..62804e70 100644 --- a/plugins/altimate-code/hooks/features/cost.test.tsx +++ b/plugins/altimate-code/hooks/features/cost.test.tsx @@ -75,6 +75,8 @@ async function start($: any, on: any, options: Options = {}) { on('tool.call', async (_: unknown, e: { tool: string; command?: string }) => { if (e.tool === 'Bash' && options.holdBash && options.holdBash.command === e.command) await options.holdBash.until if (e.tool === 'Bash') ran.push(String(e.command)) + // A call takes time: the runs feature beside this one reads one dbt call ending at the very moment another began as an overlap. + await clock.advance(100) return { result: { text: 'ok', isError: false } } }) await $.session.start({ cwd: ROOT, surface: 'terminal', isInteractive: true }) diff --git a/plugins/altimate-code/hooks/features/runs.test.tsx b/plugins/altimate-code/hooks/features/runs.test.tsx index 825511a0..a71b0753 100644 --- a/plugins/altimate-code/hooks/features/runs.test.tsx +++ b/plugins/altimate-code/hooks/features/runs.test.tsx @@ -228,7 +228,7 @@ test('of several dbt commands in one call, the one that wrote the file is record test('two dbt calls in different folders that ran together are both left out: any overlap in time is not told apart', async ($, on) => { const { stored } = await start($, on, args => view(args.project_dir === '/repo/a' ? 'inv-a' : 'inv-b', undefined, 'build', AFTER, String(args.project_dir))) - const both: any[] = await Promise.all([$.tool.call({ tool: 'Bash', command: 'cd a && dbt build', tool_use_id: 'one' }), $.tool.call({ tool: 'Bash', command: 'cd b && dbt build', tool_use_id: 'two' })]) + const both: any[] = await Promise.all([$.tool.call({ tool: 'Bash', command: 'cd a && dbt test', tool_use_id: 'one' }), $.tool.call({ tool: 'Bash', command: 'cd b && dbt test', tool_use_id: 'two' })]) expect(stored.has('runs')).toBe(false) expect(both.map(one => own(one.context))).toEqual([[NOT_RECORDED], [NOT_RECORDED]]) }) @@ -334,13 +334,15 @@ test('a call takes time and is still recorded: the guard against overlapping cal expect(kept(stored)).toHaveLength(1) }) +// The calls that overlap are `dbt test` ones: the cost gate, on beside runs, estimates a run or build, and it refuses a second +// concurrent one to ask again, so that call would never run and nothing would overlap. const NOT_RECORDED = 'run not recorded: overlapping dbt commands' test('two dbt calls that overlap are not told apart: neither is recorded, and each is told so', async ($, on) => { const { stored } = await start($, on, () => view('inv-second', undefined, 'build', AFTER)) 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' }), + $.tool.call({ tool: 'Bash', command: 'dbt test -s second', tool_use_id: 'two' }), ]) expect(stored.has('runs')).toBe(false) expect(both.map(one => own(one.context))).toEqual([[NOT_RECORDED], [NOT_RECORDED]]) @@ -350,8 +352,8 @@ test('an older call still running in the same folder holds back the later call t // The older call is the slow one: when the later one ends, the file on disk may be the older call's. const { stored } = await start($, on, () => view('inv-older', undefined, 'build', AFTER), { slow: 'older' }) const both: any[] = await Promise.all([ - $.tool.call({ tool: 'Bash', command: 'dbt build -s older', tool_use_id: 'older' }), - $.tool.call({ tool: 'Bash', command: 'dbt build -s later', tool_use_id: 'later' }), + $.tool.call({ tool: 'Bash', command: 'dbt test -s older', tool_use_id: 'older' }), + $.tool.call({ tool: 'Bash', command: 'dbt test -s later', tool_use_id: 'later' }), ]) expect(stored.has('runs')).toBe(false) expect(both.map(one => own(one.context))).toEqual([[NOT_RECORDED], [NOT_RECORDED]]) @@ -712,8 +714,8 @@ test('a URL with a secret-named query parameter is not recorded', async ($, on) test('two spellings of one project folder overlap: /repo and /repo/models share one target', async ($, on) => { const { stored } = await start($, on, () => view('inv-x', undefined, 'build', AFTER)) const both: any[] = await Promise.all([ - $.tool.call({ tool: 'Bash', command: 'dbt build -s a', tool_use_id: 'one' }), - $.tool.call({ tool: 'Bash', command: 'dbt build -s a --project-dir models', tool_use_id: 'two' }), + $.tool.call({ tool: 'Bash', command: 'dbt test -s a', tool_use_id: 'one' }), + $.tool.call({ tool: 'Bash', command: 'dbt test -s a --project-dir models', tool_use_id: 'two' }), ]) expect(stored.has('runs')).toBe(false) expect(both.map(one => own(one.context))).toEqual([[NOT_RECORDED], [NOT_RECORDED]]) @@ -740,8 +742,8 @@ test('a call whose result is a refusal never ran: a later run is recorded, not t test('a call whose folder cannot be told still counts as a dbt call that overlaps', async ($, on) => { const { stored, cli } = await start($, on, () => view('inv-x', undefined, 'build', AFTER)) const both: any[] = await Promise.all([ - $.tool.call({ tool: 'Bash', command: 'dbt build -s known', tool_use_id: 'one' }), - $.tool.call({ tool: 'Bash', command: 'dbt build --project-dir $X', tool_use_id: 'two' }), + $.tool.call({ tool: 'Bash', command: 'dbt test -s known', tool_use_id: 'one' }), + $.tool.call({ tool: 'Bash', command: 'dbt test --project-dir $X', tool_use_id: 'two' }), ]) expect(stored.has('runs')).toBe(false) expect(cli).toEqual([]) @@ -773,8 +775,8 @@ test('dbt calls that follow one another do not overlap: each is recorded', async test('calls that end in the same moment are both left out, even when the host answers slowly', async ($, on) => { const { stored } = await start($, on, () => view('inv-x', undefined, 'build', AFTER), { rootDelay: 50 }) const both: any[] = await Promise.all([ - $.tool.call({ tool: 'Bash', command: 'dbt build -s a', tool_use_id: 'one' }), - $.tool.call({ tool: 'Bash', command: 'dbt build -s b', tool_use_id: 'two' }), + $.tool.call({ tool: 'Bash', command: 'dbt test -s a', tool_use_id: 'one' }), + $.tool.call({ tool: 'Bash', command: 'dbt test -s b', tool_use_id: 'two' }), ]) expect(stored.has('runs')).toBe(false) expect(both.map(one => own(one.context))).toEqual([[NOT_RECORDED], [NOT_RECORDED]]) From e06c39b40e5118eec79fd474e9e28c0e27622790 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 17:19:06 +0530 Subject: [PATCH 02/18] fix: a session folder that is never answered no longer uses up the budget 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 --- .../hooks/features/cost.test.tsx | 31 ++++++++++++++++++- plugins/altimate-code/hooks/features/cost.tsx | 18 ++++++++--- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/plugins/altimate-code/hooks/features/cost.test.tsx b/plugins/altimate-code/hooks/features/cost.test.tsx index 62804e70..14d87d0f 100644 --- a/plugins/altimate-code/hooks/features/cost.test.tsx +++ b/plugins/altimate-code/hooks/features/cost.test.tsx @@ -20,6 +20,8 @@ type Options = { holds?: Record> /** The session's directory; changed mid-test to stand for a /cd. */ cwd?: string + /** Set after start: the session's directory is not answered until this resolves. */ + holdCwd?: Promise /** Drawing the status line throws. */ statusFails?: boolean /** The Bash tool runs this command only once this resolves. */ @@ -40,7 +42,10 @@ async function start($: any, on: any, options: Options = {}) { on('turn.complete', (_: unknown, e: { answer: string }) => ({ text: e.answer })) on('session.start', (_: unknown, e: { cwd: string }) => ({ cwd: e.cwd })) on('session.root', () => ({ value: ROOT })) - on('session.cwd', () => ({ value: options.cwd ?? ROOT })) + on('session.cwd', async () => { + await options.holdCwd + return { value: options.cwd ?? ROOT } + }) on('ui.log', () => ({ value: undefined })) on('ui.status', (_: unknown, e: { text?: string }) => { if (options.statusFails) throw new Error('no status line') @@ -179,6 +184,30 @@ test('a selection that is not one plain model passes, saying there is no estimat expect(ran).toHaveLength(4) }) +test('a session folder that is never answered does not hold a command that scans nothing for more than 80 ms', async ($, on) => { + const options: Options = {} + const { clock, ran } = await start($, on, options) + options.holdCwd = new Promise(() => {}) + // Without the bound this call never finishes: the gate waits for the folder, and a gate after it would never run. + const pending = $.tool.call(bash('git push')) + for (let i = 0; i < 80; i++) await clock.advance(100) + const result: any = await pending + expect(result.deny).toBeUndefined() + expect(ran).toEqual(['git push']) +}) + +test('a session folder that is never answered holds a dbt run for 80 ms at most, then the estimate goes on', async ($, on) => { + const options: Options = { answers: { orders: UNDER } } + const { clock, ran } = await start($, on, options) + options.holdCwd = new Promise(() => {}) + const pending = $.tool.call(bash('dbt run -s orders')) + for (let i = 0; i < 80; i++) await clock.advance(100) + await clock.settle() + const result: any = await pending + expect(result.deny).toBeUndefined() + expect(ran).toEqual(['dbt run -s orders']) +}) + test('commands that scan nothing are not looked at', async ($, on) => { const { clock, estimates, ran } = await start($, on) for (const command of ['dbt test -s orders', 'dbt compile -s orders', 'ls', 'git status']) await $.tool.call(bash(command)) diff --git a/plugins/altimate-code/hooks/features/cost.tsx b/plugins/altimate-code/hooks/features/cost.tsx index f968d4a3..825c9c8a 100644 --- a/plugins/altimate-code/hooks/features/cost.tsx +++ b/plugins/altimate-code/hooks/features/cost.tsx @@ -97,6 +97,14 @@ async function afterShellChange($: EngineInterface, command: string, ran = false /** An answer nobody took within this long is dropped: the model may have changed since. */ 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 + +/** The session's folder, or `fallback` when it cannot be read within CWD_WAIT_MS. */ +async function sessionDir($: EngineInterface, fallback: string | null): Promise { + return (await within($, CWD_WAIT_MS, $.session.cwd())) ?? fallback +} + /** Adds this feature's own hooks. */ export function register(on: On, context: FeatureContext) { const configured = Number((context.options as Record | undefined)?.maxScanBytes) @@ -203,7 +211,7 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P // A command refused for its estimate has not run: its retry is not a change, and keeps the answers it waited for. 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) if (seenDir !== null && dir !== seenDir) { // What is still running belongs to the project left behind: it holds nothing back here, and still holds that project back on return. await leaveProject($) @@ -241,7 +249,7 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P 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. - const now = (await $.session.cwd().catch(() => dir)) ?? null + const now = (await sessionDir($, dir)) ?? null if (now !== (dir ?? null)) { await leaveProject($) seenDir = now @@ -419,7 +427,7 @@ async function startWhenIdle( // 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 for (;;) { - dir = (await $.session.cwd().catch(() => seenDir)) ?? null + dir = (await sessionDir($, seenDir)) ?? null const running = [...active].filter(([, d]) => d === dir).map(([p]) => p) if (running.length === 0) break const left = waitUntil - (await $.clock.now()) @@ -628,7 +636,7 @@ async function estimateWithin($: EngineInterface, args: { model?: string; sql?: if (had) return had // The look-up and the insert below are one step: nothing is awaited between them, or two passes would each start one. const now = await $.clock.now() - const here = (await $.session.cwd().catch(() => seenDir)) ?? null + const here = (await sessionDir($, 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 && (running.generation !== generation || running.dir !== here)) running = undefined @@ -662,7 +670,7 @@ async function estimateWithin($: EngineInterface, args: { model?: string; sql?: 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.generation !== generation || running.dir !== ((await $.session.cwd().catch(() => seenDir)) ?? null)) { + if (running.generation !== generation || running.dir !== ((await sessionDir($, seenDir)) ?? null)) { if (inflight.get(key) === running) inflight.delete(key) return 'pending' } From eb451aa1317ce6601d4dc8f43f15ad24846d6a5b Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 17:31:58 +0530 Subject: [PATCH 03/18] test: the cost gate waits at most 80 ms for a session folder that is never answered Co-Authored-By: Claude Sonnet 5.5 --- .../hooks/features/cost.test.tsx | 25 ++++++------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/plugins/altimate-code/hooks/features/cost.test.tsx b/plugins/altimate-code/hooks/features/cost.test.tsx index 14d87d0f..156acbb2 100644 --- a/plugins/altimate-code/hooks/features/cost.test.tsx +++ b/plugins/altimate-code/hooks/features/cost.test.tsx @@ -184,28 +184,19 @@ test('a selection that is not one plain model passes, saying there is no estimat expect(ran).toHaveLength(4) }) -test('a session folder that is never answered does not hold a command that scans nothing for more than 80 ms', async ($, on) => { +test('a session folder that is never answered holds the gate for 80 ms at most: a command that scans nothing runs without waiting for the phase deadline', async ($, on) => { const options: Options = {} const { clock, ran } = await start($, on, options) - options.holdCwd = new Promise(() => {}) - // Without the bound this call never finishes: the gate waits for the folder, and a gate after it would never run. - const pending = $.tool.call(bash('git push')) - for (let i = 0; i < 80; i++) await clock.advance(100) - const result: any = await pending - expect(result.deny).toBeUndefined() - expect(ran).toEqual(['git push']) -}) - -test('a session folder that is never answered holds a dbt run for 80 ms at most, then the estimate goes on', async ($, on) => { - const options: Options = { answers: { orders: UNDER } } - const { clock, ran } = await start($, on, options) - options.holdCwd = new Promise(() => {}) - const pending = $.tool.call(bash('dbt run -s orders')) - for (let i = 0; i < 80; i++) await clock.advance(100) + let release: () => void = () => {} + options.holdCwd = new Promise(resolve => (release = resolve)) + const pending = $.tool.call(bash('git status')) + await clock.advance(200) + // A gate that waited for the folder would hold the call for the 5 s a phase has, and every gate after it with it. + expect(ran).toEqual(['git status']) + release() await clock.settle() const result: any = await pending expect(result.deny).toBeUndefined() - expect(ran).toEqual(['dbt run -s orders']) }) test('commands that scan nothing are not looked at', async ($, on) => { From 5ee89b353d00ece7208030255ff94b342fee9e61 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 17:38:06 +0530 Subject: [PATCH 04/18] fix: a session folder that cannot be read in time is no known project: the gate reuses none of what it learned Co-Authored-By: Claude Sonnet 5.5 --- .../hooks/features/cost.test.tsx | 12 ++++++++++++ plugins/altimate-code/hooks/features/cost.tsx | 19 +++++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/plugins/altimate-code/hooks/features/cost.test.tsx b/plugins/altimate-code/hooks/features/cost.test.tsx index 156acbb2..521832a0 100644 --- a/plugins/altimate-code/hooks/features/cost.test.tsx +++ b/plugins/altimate-code/hooks/features/cost.test.tsx @@ -199,6 +199,18 @@ test('a session folder that is never answered holds the gate for 80 ms at most: expect(result.deny).toBeUndefined() }) +test('a session folder that could not be read is no project the gate knows: an earlier verdict is not reused for it', async ($, on) => { + const options: Options = { answers: { orders: NO_DRY_RUN } } + const { clock } = await start($, on, options) + expect(((await $.tool.call(bash('dbt run -s orders'))) as any).deny).toBeUndefined() + options.answers = { orders: OVER } + options.holdCwd = new Promise(() => {}) + const pending = $.tool.call(bash('dbt run -s orders')) + for (let i = 0; i < 70; i++) await clock.advance(100) + const result: any = await pending + expect(result.deny).toContain('20.0 GiB') +}) + test('commands that scan nothing are not looked at', async ($, on) => { const { clock, estimates, ran } = await start($, on) for (const command of ['dbt test -s orders', 'dbt compile -s orders', 'ls', 'git status']) await $.tool.call(bash(command)) diff --git a/plugins/altimate-code/hooks/features/cost.tsx b/plugins/altimate-code/hooks/features/cost.tsx index 825c9c8a..0bab1bc5 100644 --- a/plugins/altimate-code/hooks/features/cost.tsx +++ b/plugins/altimate-code/hooks/features/cost.tsx @@ -100,9 +100,12 @@ 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 -/** The session's folder, or `fallback` when it cannot be read within CWD_WAIT_MS. */ -async function sessionDir($: EngineInterface, fallback: string | null): Promise { - return (await within($, CWD_WAIT_MS, $.session.cwd())) ?? fallback +/** What the folder is taken for when it could not be read in time: no project the gate has seen, so nothing it learned is reused. */ +const UNKNOWN_DIR = '\u0000unknown' + +/** The session's folder, or UNKNOWN_DIR when it cannot be read within CWD_WAIT_MS. */ +async function sessionDir($: EngineInterface): Promise { + return (await within($, CWD_WAIT_MS, $.session.cwd())) ?? UNKNOWN_DIR } /** Adds this feature's own hooks. */ @@ -211,7 +214,7 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P // A command refused for its estimate has not run: its retry is not a change, and keeps the answers it waited for. 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 sessionDir($, seenDir) + const dir = await sessionDir($) if (seenDir !== null && dir !== seenDir) { // What is still running belongs to the project left behind: it holds nothing back here, and still holds that project back on return. await leaveProject($) @@ -249,7 +252,7 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P 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. - const now = (await sessionDir($, dir)) ?? null + const now = (await sessionDir($)) ?? null if (now !== (dir ?? null)) { await leaveProject($) seenDir = now @@ -427,7 +430,7 @@ async function startWhenIdle( // 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 for (;;) { - dir = (await sessionDir($, seenDir)) ?? null + dir = (await sessionDir($)) ?? null const running = [...active].filter(([, d]) => d === dir).map(([p]) => p) if (running.length === 0) break const left = waitUntil - (await $.clock.now()) @@ -636,7 +639,7 @@ async function estimateWithin($: EngineInterface, args: { model?: string; sql?: if (had) return had // The look-up and the insert below are one step: nothing is awaited between them, or two passes would each start one. const now = await $.clock.now() - const here = (await sessionDir($, seenDir)) ?? null + const here = (await sessionDir($)) ?? null let running = inflight.get(key) // An answer made in another project is of that project's models, not this one's. if (running && (running.generation !== generation || running.dir !== here)) running = undefined @@ -670,7 +673,7 @@ async function estimateWithin($: EngineInterface, args: { model?: string; sql?: 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.generation !== generation || running.dir !== ((await sessionDir($, seenDir)) ?? null)) { + if (running.generation !== generation || running.dir !== ((await sessionDir($)) ?? null)) { if (inflight.get(key) === running) inflight.delete(key) return 'pending' } From 52baaa5e2ade538658d3dd858dd8c092d0ff1f5f Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 17:52:14 +0530 Subject: [PATCH 05/18] fix: a session folder that stays unreadable is a different unknown project each time, so no verdict is carried from one command to the next Co-Authored-By: Claude Sonnet 5.5 --- .../hooks/features/cost.test.tsx | 20 ++++++++++++++++++- plugins/altimate-code/hooks/features/cost.tsx | 10 ++++++---- .../altimate-code/hooks/tool-chain.test.ts | 1 + 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/plugins/altimate-code/hooks/features/cost.test.tsx b/plugins/altimate-code/hooks/features/cost.test.tsx index 521832a0..9e926d5a 100644 --- a/plugins/altimate-code/hooks/features/cost.test.tsx +++ b/plugins/altimate-code/hooks/features/cost.test.tsx @@ -208,7 +208,25 @@ test('a session folder that could not be read is no project the gate knows: an e const pending = $.tool.call(bash('dbt run -s orders')) for (let i = 0; i < 70; i++) await clock.advance(100) const result: any = await pending - expect(result.deny).toContain('20.0 GiB') + // Not allowed on the strength of the earlier verdict: the command is refused, to be asked again once the folder can be read. + expect(result.deny).toBeDefined() +}) + +test('a session folder that stays unreadable is never the same project twice: no verdict is carried from one command to the next', async ($, on) => { + const options: Options = { answers: { orders: NO_DRY_RUN } } + const { clock, started } = await start($, on, options) + options.holdCwd = new Promise(() => {}) + const run = async () => { + const pending = $.tool.call(bash('dbt run -s orders')) as Promise + for (let i = 0; i < 70; i++) await clock.advance(100) + return pending + } + await run() + options.answers = { orders: OVER } + const second = await run() + // Each command asks for its own estimate: the earlier "no dry run here" is not taken for this project. + expect(started.filter(key => key === 'orders').length).toBeGreaterThanOrEqual(2) + expect(second.deny).toBeDefined() }) test('commands that scan nothing are not looked at', async ($, on) => { diff --git a/plugins/altimate-code/hooks/features/cost.tsx b/plugins/altimate-code/hooks/features/cost.tsx index 0bab1bc5..c555d5f6 100644 --- a/plugins/altimate-code/hooks/features/cost.tsx +++ b/plugins/altimate-code/hooks/features/cost.tsx @@ -100,12 +100,14 @@ 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 -/** What the folder is taken for when it could not be read in time: no project the gate has seen, so nothing it learned is reused. */ -const UNKNOWN_DIR = '\u0000unknown' +let unreadable = 0 -/** The session's folder, or UNKNOWN_DIR when it cannot be read within CWD_WAIT_MS. */ +/** + * The session's folder. One that cannot be read within CWD_WAIT_MS is a different, unknown project each time it is asked: + * 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 { - return (await within($, CWD_WAIT_MS, $.session.cwd())) ?? UNKNOWN_DIR + return (await within($, CWD_WAIT_MS, $.session.cwd())) ?? `\u0000unknown-${++unreadable}` } /** Adds this feature's own hooks. */ diff --git a/plugins/altimate-code/hooks/tool-chain.test.ts b/plugins/altimate-code/hooks/tool-chain.test.ts index c3ff93f7..df1e098b 100644 --- a/plugins/altimate-code/hooks/tool-chain.test.ts +++ b/plugins/altimate-code/hooks/tool-chain.test.ts @@ -91,6 +91,7 @@ test('through the plugin, a Bash call reaches the tool unchanged and its result // The session beneath has nothing to report, so no feature that is built holds a call or adds to one it has no reason to. mock.clock(on) on('ui.status', () => ({ value: undefined })) + on('session.cwd', () => ({ value: '/repo' })) on('session.root', () => ({ value: '/repo' })) on('fs.exists', () => ({ value: false })) on('process.run', (_: unknown, e: { argv: readonly string[] }) => ({ value: { isStdoutTruncated: false, isStderrTruncated: false, ...(nothingToReport(e.argv) ?? { exitCode: 1, stdout: '', stderr: 'no CLI here' }) } })) From 64afffa4515353423300cfd0d002a33dc4c7b3b1 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 18:00:56 +0530 Subject: [PATCH 06/18] fix: an unreadable session folder starts no estimate and refuses the scan, and a failed lookup keeps the last folder as before Co-Authored-By: Claude Sonnet 5.5 --- .../hooks/features/cost.test.tsx | 16 ++++++------- plugins/altimate-code/hooks/features/cost.tsx | 23 +++++++++++++------ 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/plugins/altimate-code/hooks/features/cost.test.tsx b/plugins/altimate-code/hooks/features/cost.test.tsx index 9e926d5a..dbcff3d5 100644 --- a/plugins/altimate-code/hooks/features/cost.test.tsx +++ b/plugins/altimate-code/hooks/features/cost.test.tsx @@ -212,21 +212,19 @@ test('a session folder that could not be read is no project the gate knows: an e expect(result.deny).toBeDefined() }) -test('a session folder that stays unreadable is never the same project twice: no verdict is carried from one command to the next', async ($, on) => { +test('a session folder that cannot be read is an unknown project: no verdict is reused for it, and no estimate starts', async ($, on) => { const options: Options = { answers: { orders: NO_DRY_RUN } } const { clock, started } = await start($, on, options) + expect(((await $.tool.call(bash('dbt run -s orders'))) as any).deny).toBeUndefined() + options.answers = { orders: OVER } options.holdCwd = new Promise(() => {}) - const run = async () => { + for (let again = 0; again < 2; again++) { const pending = $.tool.call(bash('dbt run -s orders')) as Promise for (let i = 0; i < 70; i++) await clock.advance(100) - return pending + // Refused, though the earlier answer was "no dry run here"; and nothing asks the adapter, which could start dbt in a project one is already running in. + expect((await pending).deny).toContain('could not be read in time') } - await run() - options.answers = { orders: OVER } - const second = await run() - // Each command asks for its own estimate: the earlier "no dry run here" is not taken for this project. - expect(started.filter(key => key === 'orders').length).toBeGreaterThanOrEqual(2) - expect(second.deny).toBeDefined() + expect(started).toEqual(['orders']) }) test('commands that scan nothing are not looked at', async ($, on) => { diff --git a/plugins/altimate-code/hooks/features/cost.tsx b/plugins/altimate-code/hooks/features/cost.tsx index c555d5f6..a77d23f6 100644 --- a/plugins/altimate-code/hooks/features/cost.tsx +++ b/plugins/altimate-code/hooks/features/cost.tsx @@ -106,10 +106,15 @@ let unreadable = 0 * The session's folder. One that cannot be read within CWD_WAIT_MS is a different, unknown project each time it is asked: * 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 { - return (await within($, CWD_WAIT_MS, $.session.cwd())) ?? `\u0000unknown-${++unreadable}` +async function sessionDir($: EngineInterface, fallback: string | null): Promise { + // A lookup that fails keeps the folder last seen, as before; one that does not answer in time is an unknown project. + const read = await within($, CWD_WAIT_MS, $.session.cwd().then(dir => ({ dir: dir as string | null }), () => ({ dir: fallback }))) + return read ? read.dir : `${UNKNOWN}${++unreadable}` } +const UNKNOWN = '\u0000unknown-' +const isUnknown = (dir: string | null) => dir?.startsWith(UNKNOWN) === true + /** Adds this feature's own hooks. */ export function register(on: On, context: FeatureContext) { const configured = Number((context.options as Record | undefined)?.maxScanBytes) @@ -216,7 +221,7 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P // A command refused for its estimate has not run: its retry is not a change, and keeps the answers it waited for. 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 sessionDir($) + const dir = await sessionDir($, seenDir) if (seenDir !== null && dir !== seenDir) { // What is still running belongs to the project left behind: it holds nothing back here, and still holds that project back on return. await leaveProject($) @@ -225,6 +230,8 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P const all = parseDbtCommand(e.command) const invocations = all.filter(i => i.subcommand !== null && SCANNING.has(i.subcommand)) if (invocations.length === 0) return null + // Which project this is cannot be told, so nothing is estimated for it (two estimates could start dbt in one project together) and nothing is let through. + if (isUnknown(dir)) return { deny: 'The project folder could not be read in time, so the scan cannot be estimated. Run the same command again.' } // An edit or a command that changed the project while the answers were coming in makes them void. const startedAt = generation const state = (await read($, costAtom)) ?? FRESH @@ -254,7 +261,7 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P 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. - const now = (await sessionDir($)) ?? null + const now = (await sessionDir($, dir)) ?? null if (now !== (dir ?? null)) { await leaveProject($) seenDir = now @@ -432,7 +439,9 @@ async function startWhenIdle( // 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 for (;;) { - dir = (await sessionDir($)) ?? 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 const running = [...active].filter(([, d]) => d === dir).map(([p]) => p) if (running.length === 0) break const left = waitUntil - (await $.clock.now()) @@ -641,7 +650,7 @@ async function estimateWithin($: EngineInterface, args: { model?: string; sql?: if (had) return had // The look-up and the insert below are one step: nothing is awaited between them, or two passes would each start one. const now = await $.clock.now() - const here = (await sessionDir($)) ?? null + const here = (await sessionDir($, 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 && (running.generation !== generation || running.dir !== here)) running = undefined @@ -675,7 +684,7 @@ async function estimateWithin($: EngineInterface, args: { model?: string; sql?: 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.generation !== generation || running.dir !== ((await sessionDir($)) ?? null)) { + if (running.generation !== generation || running.dir !== ((await sessionDir($, seenDir)) ?? null)) { if (inflight.get(key) === running) inflight.delete(key) return 'pending' } From 482069527024e7c316c7ed7aa070897ba0f5bb92 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 18:12:06 +0530 Subject: [PATCH 07/18] fix: a lookup of the session folder that is not answered while an estimate is asked for refuses the scan Co-Authored-By: Claude Sonnet 5.5 --- .../altimate-code/hooks/features/cost.test.tsx | 15 +++++++++++++++ plugins/altimate-code/hooks/features/cost.tsx | 2 ++ 2 files changed, 17 insertions(+) diff --git a/plugins/altimate-code/hooks/features/cost.test.tsx b/plugins/altimate-code/hooks/features/cost.test.tsx index dbcff3d5..49057e4d 100644 --- a/plugins/altimate-code/hooks/features/cost.test.tsx +++ b/plugins/altimate-code/hooks/features/cost.test.tsx @@ -22,6 +22,8 @@ type Options = { cwd?: string /** Set after start: the session's directory is not answered until this resolves. */ holdCwd?: Promise + /** Set after start: only this lookup of the session's directory (counted from 1, after it is set) is never answered. */ + stallNth?: { n: number; seen: number } /** Drawing the status line throws. */ statusFails?: boolean /** The Bash tool runs this command only once this resolves. */ @@ -44,6 +46,7 @@ async function start($: any, on: any, options: Options = {}) { on('session.root', () => ({ value: ROOT })) on('session.cwd', async () => { await options.holdCwd + if (options.stallNth && ++options.stallNth.seen === options.stallNth.n) await new Promise(() => {}) return { value: options.cwd ?? ROOT } }) on('ui.log', () => ({ value: undefined })) @@ -227,6 +230,18 @@ test('a session folder that cannot be read is an unknown project: no verdict is expect(started).toEqual(['orders']) }) +// 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]) { + test(`a scan is not let through on a project that cannot be told: the gate's lookup number ${n} of the session folder is not answered`, async ($, on) => { + const options: Options = { answers: { orders: UNDER } } + const { clock } = await start($, on, options) + options.stallNth = { n, seen: 0 } + const pending = $.tool.call(bash('dbt run -s orders')) as Promise + for (let i = 0; i < 70; i++) await clock.advance(100) + expect(((await pending) as any).deny).toBeDefined() + }) +} + test('commands that scan nothing are not looked at', async ($, on) => { const { clock, estimates, ran } = await start($, on) for (const command of ['dbt test -s orders', 'dbt compile -s orders', 'ls', 'git status']) await $.tool.call(bash(command)) diff --git a/plugins/altimate-code/hooks/features/cost.tsx b/plugins/altimate-code/hooks/features/cost.tsx index a77d23f6..9383a46b 100644 --- a/plugins/altimate-code/hooks/features/cost.tsx +++ b/plugins/altimate-code/hooks/features/cost.tsx @@ -651,6 +651,8 @@ async function estimateWithin($: EngineInterface, args: { model?: string; sql?: // The look-up and the insert below are one step: nothing is awaited between them, or two passes would each start one. const now = await $.clock.now() 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' let running = inflight.get(key) // An answer made in another project is of that project's models, not this one's. if (running && (running.generation !== generation || running.dir !== here)) running = undefined From 897f91708e4c6d1e87bd4f061101964a5b7bbac1 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 18:19:48 +0530 Subject: [PATCH 08/18] fix: a lookup of the session folder that goes unanswered refuses the whole pass, whatever later answers say Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/hooks/features/cost.test.tsx | 9 +++++++++ plugins/altimate-code/hooks/features/cost.tsx | 6 +++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/plugins/altimate-code/hooks/features/cost.test.tsx b/plugins/altimate-code/hooks/features/cost.test.tsx index 49057e4d..bb41602a 100644 --- a/plugins/altimate-code/hooks/features/cost.test.tsx +++ b/plugins/altimate-code/hooks/features/cost.test.tsx @@ -242,6 +242,15 @@ for (const n of [2, 3, 4, 5, 6]) { }) } +test('a lookup of the session folder lost on one model of a line is not made good by a later model that has no dry run', async ($, on) => { + const options: Options = { answers: { first: UNDER, second: NO_DRY_RUN } } + const { clock } = await start($, on, options) + options.stallNth = { n: 3, seen: 0 } + const pending = $.tool.call(bash('dbt run -s first && dbt run -s second')) as Promise + for (let i = 0; i < 70; i++) await clock.advance(100) + expect(((await pending) as any).deny).toBeDefined() +}) + test('commands that scan nothing are not looked at', async ($, on) => { const { clock, estimates, ran } = await start($, on) for (const command of ['dbt test -s orders', 'dbt compile -s orders', 'ls', 'git status']) await $.tool.call(bash(command)) diff --git a/plugins/altimate-code/hooks/features/cost.tsx b/plugins/altimate-code/hooks/features/cost.tsx index 9383a46b..853a4a84 100644 --- a/plugins/altimate-code/hooks/features/cost.tsx +++ b/plugins/altimate-code/hooks/features/cost.tsx @@ -112,6 +112,7 @@ async function sessionDir($: EngineInterface, fallback: string | null): Promise< return read ? read.dir : `${UNKNOWN}${++unreadable}` } +const UNREADABLE_DENY = 'The project folder could not be read in time, so the scan cannot be estimated. Run the same command again.' const UNKNOWN = '\u0000unknown-' const isUnknown = (dir: string | null) => dir?.startsWith(UNKNOWN) === true @@ -231,7 +232,7 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P const invocations = all.filter(i => i.subcommand !== null && SCANNING.has(i.subcommand)) if (invocations.length === 0) return null // Which project this is cannot be told, so nothing is estimated for it (two estimates could start dbt in one project together) and nothing is let through. - if (isUnknown(dir)) return { deny: 'The project folder could not be read in time, so the scan cannot be estimated. Run the same command again.' } + if (isUnknown(dir)) return { deny: UNREADABLE_DENY } // An edit or a command that changed the project while the answers were coming in makes them void. const startedAt = generation const state = (await read($, costAtom)) ?? FRESH @@ -249,6 +250,8 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P // One wait for the whole command, however many queries it names. const waitUntil = (await $.clock.now()) + GATE_WAIT_MS const pending: string[] = [] + // Any lookup of the folder that goes unanswered during this pass refuses it whole, whatever the later answers say. + const lostBefore = unreadable // What this pass already has, so the same query named twice on the line is asked for once. const taken = new Map() let failed = false @@ -303,6 +306,7 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P // 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 } // A finished command's answers are taken: a later one asks again. A pending one keeps them for its retry. if (pending.length === 0) for (const key of taken.keys()) inflight.delete(key) From e1876202403220cc41f285fcb0b35df123cfc8a8 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 18:26:33 +0530 Subject: [PATCH 09/18] fix: a lookup of the session folder that goes unanswered refuses at once, before the rest of the line is worked through Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/hooks/features/cost.test.tsx | 13 +++++++++++++ plugins/altimate-code/hooks/features/cost.tsx | 2 ++ 2 files changed, 15 insertions(+) diff --git a/plugins/altimate-code/hooks/features/cost.test.tsx b/plugins/altimate-code/hooks/features/cost.test.tsx index bb41602a..fbb2f2e6 100644 --- a/plugins/altimate-code/hooks/features/cost.test.tsx +++ b/plugins/altimate-code/hooks/features/cost.test.tsx @@ -251,6 +251,19 @@ test('a lookup of the session folder lost on one model of a line is not made goo expect(((await pending) as any).deny).toBeDefined() }) +test('a lookup lost on the first of many models refuses at once: the later models are not worked through first', async ($, on) => { + const answers: Record = {} + const line = Array.from({ length: 20 }, (_, i) => `dbt run -s m${i}`).join(' && ') + for (let i = 0; i < 20; i++) answers[`m${i}`] = UNDER + const options: Options = { answers } + const { clock, started } = await start($, on, options) + options.stallNth = { n: 3, seen: 0 } + const pending = $.tool.call(bash(line)) as Promise + for (let i = 0; i < 70; i++) await clock.advance(100) + expect(((await pending) as any).deny).toBeDefined() + expect(started.length).toBeLessThan(3) +}) + test('commands that scan nothing are not looked at', async ($, on) => { const { clock, estimates, ran } = await start($, on) for (const command of ['dbt test -s orders', 'dbt compile -s orders', 'ls', 'git status']) await $.tool.call(bash(command)) diff --git a/plugins/altimate-code/hooks/features/cost.tsx b/plugins/altimate-code/hooks/features/cost.tsx index 853a4a84..363966f5 100644 --- a/plugins/altimate-code/hooks/features/cost.tsx +++ b/plugins/altimate-code/hooks/features/cost.tsx @@ -263,6 +263,8 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P } 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)) + // The folder could not be read in time: the rest of the line is not looked at, or the chain's own deadline would let the whole command through first. + if (unreadable !== lostBefore) return { deny: UNREADABLE_DENY } // A /cd while it was being asked: the answer is of the project left, and nothing here is allowed on it. const now = (await sessionDir($, dir)) ?? null if (now !== (dir ?? null)) { From 056d44388fd98d827d918e31eb8b3591c602bd31 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 19:05:40 +0530 Subject: [PATCH 10/18] fix: an unreadable session folder is no project change; one lookup budget per pass, said as such to the user Co-Authored-By: Claude Sonnet 5.5 --- .../hooks/features/cost.test.tsx | 84 ++++++++++++++++++- plugins/altimate-code/hooks/features/cost.tsx | 79 ++++++++++------- .../hooks/features/runs.test.tsx | 12 +-- 3 files changed, 136 insertions(+), 39 deletions(-) diff --git a/plugins/altimate-code/hooks/features/cost.test.tsx b/plugins/altimate-code/hooks/features/cost.test.tsx index fbb2f2e6..f399ade4 100644 --- a/plugins/altimate-code/hooks/features/cost.test.tsx +++ b/plugins/altimate-code/hooks/features/cost.test.tsx @@ -24,6 +24,8 @@ type Options = { holdCwd?: Promise /** Set after start: only this lookup of the session's directory (counted from 1, after it is set) is never answered. */ stallNth?: { n: number; seen: number } + /** Set after start: every lookup of the session's directory takes this long to answer. */ + cwdDelay?: number /** Drawing the status line throws. */ statusFails?: boolean /** The Bash tool runs this command only once this resolves. */ @@ -44,8 +46,9 @@ async function start($: any, on: any, options: Options = {}) { on('turn.complete', (_: unknown, e: { answer: string }) => ({ text: e.answer })) on('session.start', (_: unknown, e: { cwd: string }) => ({ cwd: e.cwd })) on('session.root', () => ({ value: ROOT })) - on('session.cwd', async () => { + on('session.cwd', async ($$: any) => { await options.holdCwd + if (options.cwdDelay) await new Promise(resolve => setTimeout(resolve, options.cwdDelay)) if (options.stallNth && ++options.stallNth.seen === options.stallNth.n) await new Promise(() => {}) return { value: options.cwd ?? ROOT } }) @@ -238,7 +241,8 @@ for (const n of [2, 3, 4, 5, 6]) { options.stallNth = { n, seen: 0 } const pending = $.tool.call(bash('dbt run -s orders')) as Promise for (let i = 0; i < 70; i++) await clock.advance(100) - expect(((await pending) as any).deny).toBeDefined() + // Said as an unreadable folder, whichever lookup it was: not as an estimate still running. + expect(((await pending) as any).deny).toContain('could not be read in time') }) } @@ -261,7 +265,81 @@ test('a lookup lost on the first of many models refuses at once: the later model const pending = $.tool.call(bash(line)) as Promise for (let i = 0; i < 70; i++) await clock.advance(100) expect(((await pending) as any).deny).toBeDefined() - expect(started.length).toBeLessThan(3) + expect(started).toEqual([]) +}) + +test('a folder that cannot be read is not a change of project: the allowance of the project the session is in survives a stalled pass', async ($, on) => { + const options: Options = { answers: { orders: OVER } } + const { clock, ran } = await start($, on, options) + expect(((await $.tool.call(bash('dbt run -s orders'))) as any).deny).toContain('over the') + await $.command.run({ command: 'allow-scan', args: '' } as any) + // Any Bash call whose lookup stalls: not a dbt one even. + options.stallNth = { n: 1, seen: 0 } + const stalled = $.tool.call(bash('git status')) as Promise + for (let i = 0; i < 10; i++) await clock.advance(100) + expect((await stalled).deny).toBeUndefined() + options.stallNth = undefined + // The hold and its allowance are still the session's: the held command runs once. + expect(((await $.tool.call(bash('dbt run -s orders'))) as any).deny).toBeUndefined() + expect(ran).toContain('dbt run -s orders') +}) + +test('a stalled pass is not remembered as a project: the next pass, that reads the folder, finds the project it left', async ($, on) => { + const options: Options = { answers: { orders: OVER } } + const { clock, ran } = await start($, on, options) + expect(((await $.tool.call(bash('dbt run -s orders'))) as any).deny).toContain('over the') + for (let again = 0; again < 2; again++) { + options.stallNth = { n: 1, seen: 0 } + const stalled = $.tool.call(bash('git status')) as Promise + for (let i = 0; i < 10; i++) await clock.advance(100) + await stalled + } + options.stallNth = undefined + await $.command.run({ command: 'allow-scan', args: '' } as any) + expect(((await $.tool.call(bash('dbt run -s orders'))) as any).deny).toBeUndefined() + expect(ran).toContain('dbt run -s orders') +}) + +test('a lookup lost in one pass does not refuse another pass whose own lookups all answered', async ($, on) => { + let release: () => void = () => {} + const options: Options = { answers: { slow: UNDER }, holds: { slow: new Promise(resolve => (release = resolve)) } } + const { clock } = await start($, on, options) + const other = $.tool.call(bash('dbt run -s slow')) as Promise + await clock.advance(10) + options.stallNth = { n: 1, seen: 0 } + const stalled = $.tool.call(bash('git status')) as Promise + await clock.advance(200) + await stalled + release() + await clock.advance(100) + expect((await other).deny).toBeUndefined() +}) + +test('the folder lookups of one command share one 80 ms budget: answers that each come in time but add up past it refuse the pass', async ($, on) => { + const options: Options = { answers: { first: UNDER, second: UNDER } } + const { started } = await start($, on, options) + options.cwdDelay = 50 + const pending = $.tool.call(bash('dbt run -s first && dbt run -s second')) as Promise + // Real time, as the lookups' delays are: the fake clock is not moved, so only the budget can end the pass. + expect((await pending).deny).toContain('could not be read in time') + expect(started).toEqual([]) +}) + +test('a folder that cannot be read is said so, not reported as an estimate still running', async ($, on) => { + const options: Options = { answers: { orders: UNDER } } + const { clock } = await start($, on, options) + options.stallNth = { n: 3, seen: 0 } + const pending = $.tool.call(bash('dbt run -s orders')) as Promise + for (let i = 0; i < 70; i++) await clock.advance(100) + const deny = (await pending).deny + expect(deny).toContain('could not be read in time') + expect(deny).not.toContain('still being estimated') + options.stallNth = { n: 1, seen: 0 } + const run = $.command.run({ command: 'scan-cost', args: 'orders' } as any) as Promise + for (let i = 0; i < 10; i++) await clock.advance(100) + const text = (await run).text + expect(text).toContain('could not be read in time') + expect(text).not.toContain('another estimate') }) test('commands that scan nothing are not looked at', async ($, on) => { diff --git a/plugins/altimate-code/hooks/features/cost.tsx b/plugins/altimate-code/hooks/features/cost.tsx index 363966f5..c88566dd 100644 --- a/plugins/altimate-code/hooks/features/cost.tsx +++ b/plugins/altimate-code/hooks/features/cost.tsx @@ -100,21 +100,31 @@ 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 -let unreadable = 0 +/** One pass's reading of the session folder: every lookup of the pass spends one budget, and the first that goes unanswered ends the pass's knowledge of it. */ +type Look = { spent: number; lost: boolean } +const newLook = (): Look => ({ spent: 0, lost: false }) /** - * The session's folder. One that cannot be read within CWD_WAIT_MS is a different, unknown project each time it is asked: - * 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. + * The session's folder. A folder that cannot be read is no project and no change of project: it answers `fallback`, the folder + * last known, so nothing the gate holds for that project is touched, and the pass is marked lost: its caller refuses what needs the folder. */ -async function sessionDir($: EngineInterface, fallback: string | null): Promise { - // A lookup that fails keeps the folder last seen, as before; one that does not answer in time is an unknown project. - const read = await within($, CWD_WAIT_MS, $.session.cwd().then(dir => ({ dir: dir as string | null }), () => ({ dir: fallback }))) - return read ? read.dir : `${UNKNOWN}${++unreadable}` +async function sessionDir($: EngineInterface, look: Look, fallback: string | null): Promise { + if (look.lost) return fallback + const left = CWD_WAIT_MS - look.spent + // 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 keeps the folder last seen, as before; one that does not answer in time loses the pass. + const read = left > 0 ? await within($, left, $.session.cwd().then(dir => ({ dir: dir as string | null }), () => ({ dir: fallback }))) : null + look.spent += Date.now() - began + if (!read) { + look.lost = true + return fallback + } + return read.dir } const UNREADABLE_DENY = 'The project folder could not be read in time, so the scan cannot be estimated. Run the same command again.' -const UNKNOWN = '\u0000unknown-' -const isUnknown = (dir: string | null) => dir?.startsWith(UNKNOWN) === true +const UNREADABLE_TEXT = 'No estimate: the project folder could not be read in time. Try again in a moment.' /** Adds this feature's own hooks. */ export function register(on: On, context: FeatureContext) { @@ -143,7 +153,9 @@ export function register(on: On, context: FeatureContext) { } 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) + const look = newLook() + const started = await startWhenIdle($, look, { model: target }, began + GATE_WAIT_MS, () => undefined, one => one) + if (!started && look.lost) return { text: UNREADABLE_TEXT } if (!started) return { text: 'No estimate: another estimate is still running in this project. Try again in a moment.' } const answer = await started.answer if (answer.kind === 'bytes') { @@ -222,17 +234,21 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P // A command refused for its estimate has not run: its retry is not a change, and keeps the answers it waited for. 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 sessionDir($, seenDir) - if (seenDir !== null && dir !== seenDir) { - // What is still running belongs to the project left behind: it holds nothing back here, and still holds that project back on return. - await leaveProject($) + const look = newLook() + const dir = await sessionDir($, look, seenDir) + // A folder that could not be read is not a change: the project last known keeps everything the gate holds for it. + if (!look.lost) { + if (seenDir !== null && dir !== seenDir) { + // What is still running belongs to the project left behind: it holds nothing back here, and still holds that project back on return. + await leaveProject($) + } + seenDir = dir } - seenDir = dir ?? null const all = parseDbtCommand(e.command) const invocations = all.filter(i => i.subcommand !== null && SCANNING.has(i.subcommand)) if (invocations.length === 0) return null // Which project this is cannot be told, so nothing is estimated for it (two estimates could start dbt in one project together) and nothing is let through. - if (isUnknown(dir)) return { deny: UNREADABLE_DENY } + if (look.lost) return { deny: UNREADABLE_DENY } // An edit or a command that changed the project while the answers were coming in makes them void. const startedAt = generation const state = (await read($, costAtom)) ?? FRESH @@ -250,8 +266,6 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P // One wait for the whole command, however many queries it names. const waitUntil = (await $.clock.now()) + GATE_WAIT_MS const pending: string[] = [] - // Any lookup of the folder that goes unanswered during this pass refuses it whole, whatever the later answers say. - const lostBefore = unreadable // What this pass already has, so the same query named twice on the line is asked for once. const taken = new Map() let failed = false @@ -262,12 +276,13 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P continue } 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)) + const answer = await estimateWithin($, target.model ? { model: target.model } : { sql: target.sql }, waitUntil, taken, refused.get(e.command), look) // The folder could not be read in time: the rest of the line is not looked at, or the chain's own deadline would let the whole command through first. - if (unreadable !== lostBefore) return { deny: UNREADABLE_DENY } + if (answer === 'unreadable') return { deny: UNREADABLE_DENY } // A /cd while it was being asked: the answer is of the project left, and nothing here is allowed on it. - const now = (await sessionDir($, dir)) ?? null - if (now !== (dir ?? null)) { + const now = await sessionDir($, look, dir) + if (look.lost) return { deny: UNREADABLE_DENY } + if (now !== dir) { await leaveProject($) seenDir = now } @@ -308,7 +323,7 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P // 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 } + if (look.lost) return { deny: UNREADABLE_DENY } // A finished command's answers are taken: a later one asks again. A pending one keeps them for its retry. if (pending.length === 0) for (const key of taken.keys()) inflight.delete(key) @@ -437,6 +452,7 @@ function startEstimate($: EngineInterface, args: { model?: string; sql?: string */ async function startWhenIdle( $: EngineInterface, + look: Look, args: { model?: string; sql?: string }, waitUntil: number, reuse: () => T | undefined, @@ -445,9 +461,9 @@ async function startWhenIdle( // 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 for (;;) { - dir = (await sessionDir($, seenDir)) ?? null + dir = await sessionDir($, look, seenDir) // An unreadable folder starts no estimate: it could be the project one is already running in. - if (isUnknown(dir)) return null + if (look.lost) return null const running = [...active].filter(([, d]) => d === dir).map(([p]) => p) if (running.length === 0) break const left = waitUntil - (await $.clock.now()) @@ -650,15 +666,15 @@ async function estimate($: EngineInterface, args: { model?: string; sql?: string } /** The estimate, started on the long budget if none is running for these arguments; `pending` when it is not done by `waitUntil`. */ -async function estimateWithin($: EngineInterface, args: { model?: string; sql?: string }, waitUntil: number, taken: Map, refusedAt: number | undefined): Promise { +async function estimateWithin($: EngineInterface, args: { model?: string; sql?: string }, waitUntil: number, taken: Map, refusedAt: number | undefined, look: Look): Promise { const key = JSON.stringify(args) const had = taken.get(key) if (had) return had // The look-up and the insert below are one step: nothing is awaited between them, or two passes would each start one. const now = await $.clock.now() - const here = (await sessionDir($, seenDir)) ?? null + const here = await sessionDir($, look, seenDir) // 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' + if (look.lost) return 'unreadable' let running = inflight.get(key) // An answer made in another project is of that project's models, not this one's. if (running && (running.generation !== generation || running.dir !== here)) running = undefined @@ -672,6 +688,7 @@ async function estimateWithin($: EngineInterface, args: { model?: string; sql?: // this one waits its turn, within the same bound. const entry = await startWhenIdle( $, + look, args, waitUntil, () => { @@ -685,14 +702,16 @@ async function estimateWithin($: EngineInterface, args: { model?: string; sql?: return made }, ) - if (!entry) return 'pending' + if (!entry) return look.lost ? 'unreadable' : 'pending' running = entry } const left = waitUntil - (await $.clock.now()) 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.generation !== generation || running.dir !== ((await sessionDir($, seenDir)) ?? null)) { + const after = await sessionDir($, look, seenDir) + if (look.lost) return 'unreadable' + if (running.generation !== generation || running.dir !== after) { if (inflight.get(key) === running) inflight.delete(key) return 'pending' } diff --git a/plugins/altimate-code/hooks/features/runs.test.tsx b/plugins/altimate-code/hooks/features/runs.test.tsx index a71b0753..84ce94db 100644 --- a/plugins/altimate-code/hooks/features/runs.test.tsx +++ b/plugins/altimate-code/hooks/features/runs.test.tsx @@ -227,7 +227,7 @@ test('of several dbt commands in one call, the one that wrote the file is record }) test('two dbt calls in different folders that ran together are both left out: any overlap in time is not told apart', async ($, on) => { - const { stored } = await start($, on, args => view(args.project_dir === '/repo/a' ? 'inv-a' : 'inv-b', undefined, 'build', AFTER, String(args.project_dir))) + const { stored } = await start($, on, args => view(args.project_dir === '/repo/a' ? 'inv-a' : 'inv-b', undefined, 'test', AFTER, String(args.project_dir))) const both: any[] = await Promise.all([$.tool.call({ tool: 'Bash', command: 'cd a && dbt test', tool_use_id: 'one' }), $.tool.call({ tool: 'Bash', command: 'cd b && dbt test', tool_use_id: 'two' })]) expect(stored.has('runs')).toBe(false) expect(both.map(one => own(one.context))).toEqual([[NOT_RECORDED], [NOT_RECORDED]]) @@ -339,7 +339,7 @@ test('a call takes time and is still recorded: the guard against overlapping cal const NOT_RECORDED = 'run not recorded: overlapping dbt commands' test('two dbt calls that overlap are not told apart: neither is recorded, and each is told so', async ($, on) => { - const { stored } = await start($, on, () => view('inv-second', undefined, 'build', AFTER)) + const { stored } = await start($, on, () => view('inv-second', undefined, 'test', AFTER)) const both: any[] = await Promise.all([ $.tool.call({ tool: 'Bash', command: 'dbt test -s first', tool_use_id: 'one' }), $.tool.call({ tool: 'Bash', command: 'dbt test -s second', tool_use_id: 'two' }), @@ -350,7 +350,7 @@ test('two dbt calls that overlap are not told apart: neither is recorded, and ea test('an older call still running in the same folder holds back the later call that ends first', async ($, on) => { // The older call is the slow one: when the later one ends, the file on disk may be the older call's. - const { stored } = await start($, on, () => view('inv-older', undefined, 'build', AFTER), { slow: 'older' }) + const { stored } = await start($, on, () => view('inv-older', undefined, 'test', AFTER), { slow: 'older' }) const both: any[] = await Promise.all([ $.tool.call({ tool: 'Bash', command: 'dbt test -s older', tool_use_id: 'older' }), $.tool.call({ tool: 'Bash', command: 'dbt test -s later', tool_use_id: 'later' }), @@ -712,7 +712,7 @@ test('a URL with a secret-named query parameter is not recorded', async ($, on) }) test('two spellings of one project folder overlap: /repo and /repo/models share one target', async ($, on) => { - const { stored } = await start($, on, () => view('inv-x', undefined, 'build', AFTER)) + const { stored } = await start($, on, () => view('inv-x', undefined, 'test', AFTER)) const both: any[] = await Promise.all([ $.tool.call({ tool: 'Bash', command: 'dbt test -s a', tool_use_id: 'one' }), $.tool.call({ tool: 'Bash', command: 'dbt test -s a --project-dir models', tool_use_id: 'two' }), @@ -740,7 +740,7 @@ test('a call whose result is a refusal never ran: a later run is recorded, not t }) test('a call whose folder cannot be told still counts as a dbt call that overlaps', async ($, on) => { - const { stored, cli } = await start($, on, () => view('inv-x', undefined, 'build', AFTER)) + const { stored, cli } = await start($, on, () => view('inv-x', undefined, 'test', AFTER)) const both: any[] = await Promise.all([ $.tool.call({ tool: 'Bash', command: 'dbt test -s known', tool_use_id: 'one' }), $.tool.call({ tool: 'Bash', command: 'dbt test --project-dir $X', tool_use_id: 'two' }), @@ -773,7 +773,7 @@ test('dbt calls that follow one another do not overlap: each is recorded', async }) test('calls that end in the same moment are both left out, even when the host answers slowly', async ($, on) => { - const { stored } = await start($, on, () => view('inv-x', undefined, 'build', AFTER), { rootDelay: 50 }) + const { stored } = await start($, on, () => view('inv-x', undefined, 'test', AFTER), { rootDelay: 50 }) const both: any[] = await Promise.all([ $.tool.call({ tool: 'Bash', command: 'dbt test -s a', tool_use_id: 'one' }), $.tool.call({ tool: 'Bash', command: 'dbt test -s b', tool_use_id: 'two' }), From 71844b9f356f42f14d29c8e6625db903357cdbb0 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 19:13:28 +0530 Subject: [PATCH 11/18] fix: a folder lookup that fails, once a folder has been seen, refuses the scan like one that times out Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/hooks/features/cost.test.tsx | 14 ++++++++++++++ plugins/altimate-code/hooks/features/cost.tsx | 5 +++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/plugins/altimate-code/hooks/features/cost.test.tsx b/plugins/altimate-code/hooks/features/cost.test.tsx index f399ade4..024e048e 100644 --- a/plugins/altimate-code/hooks/features/cost.test.tsx +++ b/plugins/altimate-code/hooks/features/cost.test.tsx @@ -24,6 +24,8 @@ type Options = { holdCwd?: Promise /** Set after start: only this lookup of the session's directory (counted from 1, after it is set) is never answered. */ stallNth?: { n: number; seen: number } + /** Set after start: every lookup of the session's directory fails. */ + cwdFails?: boolean /** Set after start: every lookup of the session's directory takes this long to answer. */ cwdDelay?: number /** Drawing the status line throws. */ @@ -48,6 +50,7 @@ async function start($: any, on: any, options: Options = {}) { on('session.root', () => ({ value: ROOT })) on('session.cwd', async ($$: any) => { await options.holdCwd + if (options.cwdFails) throw new Error('no folder') if (options.cwdDelay) await new Promise(resolve => setTimeout(resolve, options.cwdDelay)) if (options.stallNth && ++options.stallNth.seen === options.stallNth.n) await new Promise(() => {}) return { value: options.cwd ?? ROOT } @@ -300,6 +303,17 @@ test('a stalled pass is not remembered as a project: the next pass, that reads t expect(ran).toContain('dbt run -s orders') }) +test('a folder lookup that fails is no known project either: a verdict of the project left is not reused for a scan', async ($, on) => { + const options: Options = { answers: { orders: NO_DRY_RUN } } + const { clock } = await start($, on, options) + expect(((await $.tool.call(bash('dbt run -s orders'))) as any).deny).toBeUndefined() + options.answers = { orders: OVER } + options.cwdFails = true + const pending = $.tool.call(bash('dbt run -s orders')) as Promise + for (let i = 0; i < 10; i++) await clock.advance(100) + expect((await pending).deny).toContain('could not be read in time') +}) + test('a lookup lost in one pass does not refuse another pass whose own lookups all answered', async ($, on) => { let release: () => void = () => {} const options: Options = { answers: { slow: UNDER }, holds: { slow: new Promise(resolve => (release = resolve)) } } diff --git a/plugins/altimate-code/hooks/features/cost.tsx b/plugins/altimate-code/hooks/features/cost.tsx index c88566dd..a251158e 100644 --- a/plugins/altimate-code/hooks/features/cost.tsx +++ b/plugins/altimate-code/hooks/features/cost.tsx @@ -113,8 +113,9 @@ async function sessionDir($: EngineInterface, look: Look, fallback: string | nul const left = CWD_WAIT_MS - look.spent // 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 keeps the folder last seen, as before; one that does not answer in time loses the pass. - const read = left > 0 ? await within($, left, $.session.cwd().then(dir => ({ dir: dir as string | null }), () => ({ dir: fallback }))) : null + // A lookup that does not answer in time loses the pass. One that fails does too once a folder has been seen: the session may + // have moved, and the last one is not assumed. With none seen, there is no project to confuse it with, as before. + const read = left > 0 ? await within($, left, $.session.cwd().then(dir => ({ dir: dir as string | null }), () => (fallback === null ? { dir: null } : null))) : null look.spent += Date.now() - began if (!read) { look.lost = true From 381e9a90a18c2a2d3fcd2a7a81df5b322711cb31 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 19:19:44 +0530 Subject: [PATCH 12/18] fix: every failed or unanswered folder lookup refuses the scan; the defer tests' fake host answers the folder Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/hooks/features/cost.test.tsx | 12 ++++++++++++ plugins/altimate-code/hooks/features/cost.tsx | 5 ++--- plugins/altimate-code/hooks/features/defer.test.ts | 7 +++++-- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/plugins/altimate-code/hooks/features/cost.test.tsx b/plugins/altimate-code/hooks/features/cost.test.tsx index 024e048e..03dadcad 100644 --- a/plugins/altimate-code/hooks/features/cost.test.tsx +++ b/plugins/altimate-code/hooks/features/cost.test.tsx @@ -314,6 +314,18 @@ test('a folder lookup that fails is no known project either: a verdict of the pr expect((await pending).deny).toContain('could not be read in time') }) +test('a folder lookup that fails before any folder was seen refuses the scan too, and starts no estimate', async ($, on) => { + const options: Options = { answers: { orders: UNDER } } + const { clock, started } = await start($, on, options) + options.cwdFails = true + // A fresh session: no folder has been read yet. + await $.session.start({ cwd: ROOT, surface: 'terminal', isInteractive: true }) + const pending = $.tool.call(bash('dbt run -s orders')) as Promise + for (let i = 0; i < 10; i++) await clock.advance(100) + expect((await pending).deny).toContain('could not be read in time') + expect(started).toEqual([]) +}) + test('a lookup lost in one pass does not refuse another pass whose own lookups all answered', async ($, on) => { let release: () => void = () => {} const options: Options = { answers: { slow: UNDER }, holds: { slow: new Promise(resolve => (release = resolve)) } } diff --git a/plugins/altimate-code/hooks/features/cost.tsx b/plugins/altimate-code/hooks/features/cost.tsx index a251158e..c46abb20 100644 --- a/plugins/altimate-code/hooks/features/cost.tsx +++ b/plugins/altimate-code/hooks/features/cost.tsx @@ -113,9 +113,8 @@ async function sessionDir($: EngineInterface, look: Look, fallback: string | nul const left = CWD_WAIT_MS - look.spent // 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 does not answer in time loses the pass. One that fails does too once a folder has been seen: the session may - // have moved, and the last one is not assumed. With none seen, there is no project to confuse it with, as before. - const read = left > 0 ? await within($, left, $.session.cwd().then(dir => ({ dir: dir as string | null }), () => (fallback === null ? { dir: null } : null))) : null + // 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 look.spent += Date.now() - began if (!read) { look.lost = true diff --git a/plugins/altimate-code/hooks/features/defer.test.ts b/plugins/altimate-code/hooks/features/defer.test.ts index 336cde1f..3d065df9 100644 --- a/plugins/altimate-code/hooks/features/defer.test.ts +++ b/plugins/altimate-code/hooks/features/defer.test.ts @@ -222,6 +222,8 @@ test('the override decides, and the status text shows target and defer', () => { }) const ROOT = '/repo' +/** The session's folder, answered by the fake host. */ +let cwd = ROOT /** A dbt project at /repo whose defer setting the CLI reports as `answer`; Bash commands are recorded. */ function start($: any, on: any, answer: Record, fail: { times: number } = { times: 0 }, projects: string[] = [ROOT]) { @@ -232,6 +234,8 @@ function start($: any, on: any, answer: Record, fail: { times: on('command.register', (_: unknown, e: any) => ({ value: { command: e.name } })) on('session.start', (_: unknown, e: { cwd: string }) => ({ cwd: e.cwd })) on('session.root', () => ({ value: ROOT })) + cwd = ROOT + on('session.cwd', () => ({ value: cwd })) on('fs.exists', (_: unknown, e: { path: string }) => ({ value: projects.some(dir => e.path === `${dir}/dbt_project.yml`) })) on('process.run', (_: unknown, e: { readonly argv: readonly string[] }) => { if (!e.argv.includes('dbt_defer_state')) return { value: { exitCode: 0, stdout: '{}', stderr: '' } } @@ -265,8 +269,7 @@ test('through the plugin, Claude’s dbt commands run with --defer --state when test('a dbt project below the project, even under dbt_packages, keeps its own settings; the shell folder from an earlier cd counts', async ($, on) => { const seen = start($, on, DEFER, { times: 0 }, [ROOT, `${ROOT}/dbt_packages/pkg`, `${ROOT}/nested`]) - let cwd = ROOT - on('session.cwd', () => ({ value: cwd })) + cwd = ROOT on('fs.list', (_: unknown, e: { path: string }) => ({ value: e.path === ROOT ? [{ name: 'nested', kind: 'dir' as const, size: 0, mtimeMs: 0, isLink: false }, { name: 'dbt_packages', kind: 'dir' as const, size: 0, mtimeMs: 0, isLink: false }] : e.path === `${ROOT}/dbt_packages` ? [{ name: 'pkg', kind: 'dir' as const, size: 0, mtimeMs: 0, isLink: false }] : [] })) await $.session.start({ cwd: ROOT, surface: 'terminal', isInteractive: true }) await $.tool.call({ tool: 'Bash', command: 'cd nested && dbt build' }) From 961577c17bebb0bc168d6cd38c7c26392cec3b89 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 19:28:22 +0530 Subject: [PATCH 13/18] fix: a line refused for its unreadable folder keeps the over-limit answer already in hand Co-Authored-By: Claude Sonnet 5.5 --- .../hooks/features/cost.test.tsx | 15 +++++++++++++ plugins/altimate-code/hooks/features/cost.tsx | 21 ++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/plugins/altimate-code/hooks/features/cost.test.tsx b/plugins/altimate-code/hooks/features/cost.test.tsx index 03dadcad..3f6a365c 100644 --- a/plugins/altimate-code/hooks/features/cost.test.tsx +++ b/plugins/altimate-code/hooks/features/cost.test.tsx @@ -326,6 +326,21 @@ test('a folder lookup that fails before any folder was seen refuses the scan too expect(started).toEqual([]) }) +test('a line refused for its unreadable folder keeps the over-limit answer already in hand: its retry is still held when that estimate then fails', async ($, on) => { + const options: Options = { answers: { b: UNDER }, sequence: { a: [OVER, { success: false, error: 'Access Denied' }] } } + const { clock, ran } = await start($, on, options) + const command = 'dbt run -s a && dbt run -s b > run.log' + options.stallNth = { n: 7, seen: 0 } + const first = $.tool.call(bash(command)) as Promise + for (let i = 0; i < 70; i++) await clock.advance(100) + expect((await first).deny).toContain('could not be read in time') + options.stallNth = undefined + const retry = $.tool.call(bash(command)) as Promise + for (let i = 0; i < 70; i++) await clock.advance(100) + expect((await retry).deny).toBeDefined() + expect(ran).toEqual([]) +}) + test('a lookup lost in one pass does not refuse another pass whose own lookups all answered', async ($, on) => { let release: () => void = () => {} const options: Options = { answers: { slow: UNDER }, holds: { slow: new Promise(resolve => (release = resolve)) } } diff --git a/plugins/altimate-code/hooks/features/cost.tsx b/plugins/altimate-code/hooks/features/cost.tsx index c46abb20..d915edf4 100644 --- a/plugins/altimate-code/hooks/features/cost.tsx +++ b/plugins/altimate-code/hooks/features/cost.tsx @@ -266,6 +266,21 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P // One wait for the whole command, however many queries it names. const waitUntil = (await $.clock.now()) + GATE_WAIT_MS 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 () => { + if (over.length > 0) { + const worst = over.reduce((x, y) => (y.bytes > x.bytes ? y : x)) + await update($, costAtom, prev => { + const current = prev ?? FRESH + const same = current.held?.command === e.command + return { ...current, allowOnce: same && current.allowOnce, held: { command: e.command, target: worst.label, bytes: worst.bytes, display: worst.display } } + }) + } + notes.delete(e.command) + refused.set(e.command, await $.clock.now()) + return { deny: UNREADABLE_DENY } + } // What this pass already has, so the same query named twice on the line is asked for once. const taken = new Map() let failed = false @@ -278,10 +293,10 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P 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), look) // The folder could not be read in time: the rest of the line is not looked at, or the chain's own deadline would let the whole command through first. - if (answer === 'unreadable') return { deny: UNREADABLE_DENY } + if (answer === 'unreadable') return unreadable() // A /cd while it was being asked: the answer is of the project left, and nothing here is allowed on it. const now = await sessionDir($, look, dir) - if (look.lost) return { deny: UNREADABLE_DENY } + if (look.lost) return unreadable() if (now !== dir) { await leaveProject($) seenDir = now @@ -323,7 +338,7 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P // 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 } + if (look.lost) return unreadable() // A finished command's answers are taken: a later one asks again. A pending one keeps them for its retry. if (pending.length === 0) for (const key of taken.keys()) inflight.delete(key) From 702c5d556de2076ab9a61822cd79ba12556584c9 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 19:36:00 +0530 Subject: [PATCH 14/18] fix: the unreadable-folder refusal records nothing for a project that changed meanwhile, and stands if the hold cannot be recorded Co-Authored-By: Claude Sonnet 5.5 --- .../altimate-code/hooks/features/cost.test.tsx | 5 ++++- plugins/altimate-code/hooks/features/cost.tsx | 16 +++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/plugins/altimate-code/hooks/features/cost.test.tsx b/plugins/altimate-code/hooks/features/cost.test.tsx index 3f6a365c..70bfa44d 100644 --- a/plugins/altimate-code/hooks/features/cost.test.tsx +++ b/plugins/altimate-code/hooks/features/cost.test.tsx @@ -328,16 +328,19 @@ test('a folder lookup that fails before any folder was seen refuses the scan too test('a line refused for its unreadable folder keeps the over-limit answer already in hand: its retry is still held when that estimate then fails', async ($, on) => { const options: Options = { answers: { b: UNDER }, sequence: { a: [OVER, { success: false, error: 'Access Denied' }] } } - const { clock, ran } = await start($, on, options) + const { clock, ran, started } = await start($, on, options) const command = 'dbt run -s a && dbt run -s b > run.log' options.stallNth = { n: 7, seen: 0 } const first = $.tool.call(bash(command)) as Promise for (let i = 0; i < 70; i++) await clock.advance(100) expect((await first).deny).toContain('could not be read in time') options.stallNth = undefined + // Long enough that the answers kept for the retry are gone: the estimate of `a` is asked for again, and fails. + await clock.advance(70_000) const retry = $.tool.call(bash(command)) as Promise for (let i = 0; i < 70; i++) await clock.advance(100) expect((await retry).deny).toBeDefined() + expect(started.filter(one => one === 'a')).toHaveLength(2) expect(ran).toEqual([]) }) diff --git a/plugins/altimate-code/hooks/features/cost.tsx b/plugins/altimate-code/hooks/features/cost.tsx index d915edf4..ce8704a6 100644 --- a/plugins/altimate-code/hooks/features/cost.tsx +++ b/plugins/altimate-code/hooks/features/cost.tsx @@ -269,13 +269,19 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P // 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 () => { + // What a project changed meanwhile left is not this pass's to record. + if (generation !== startedAt) return changedDeny(e.command) if (over.length > 0) { const worst = over.reduce((x, y) => (y.bytes > x.bytes ? y : x)) - await update($, costAtom, prev => { - const current = prev ?? FRESH - const same = current.held?.command === e.command - return { ...current, allowOnce: same && current.allowOnce, held: { command: e.command, target: worst.label, bytes: worst.bytes, display: worst.display } } - }) + try { + await update($, costAtom, prev => { + const current = prev ?? FRESH + const same = current.held?.command === e.command + return { ...current, allowOnce: same && current.allowOnce, held: { command: e.command, target: worst.label, bytes: worst.bytes, display: worst.display } } + }) + } catch { + // The refusal stands whether or not the hold could be recorded: a gate that throws lets the call run. + } } notes.delete(e.command) refused.set(e.command, await $.clock.now()) From 2bb03ed350357f468c57cf1187be1af490d7fbf2 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 19:41:19 +0530 Subject: [PATCH 15/18] fix: an over-limit estimate in hand when the folder is lost still holds the command Co-Authored-By: Claude Sonnet 5.5 --- .../altimate-code/hooks/features/cost.test.tsx | 16 ++++++++++++++++ plugins/altimate-code/hooks/features/cost.tsx | 8 ++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/plugins/altimate-code/hooks/features/cost.test.tsx b/plugins/altimate-code/hooks/features/cost.test.tsx index 70bfa44d..9c711788 100644 --- a/plugins/altimate-code/hooks/features/cost.test.tsx +++ b/plugins/altimate-code/hooks/features/cost.test.tsx @@ -344,6 +344,22 @@ test('a line refused for its unreadable folder keeps the over-limit answer alrea expect(ran).toEqual([]) }) +for (const n of [5, 6]) test(`a folder lost just after an estimate came in over the limit still holds the command for its retry: lookup ${n}`, async ($, on) => { + const options: Options = { sequence: { a: [OVER, { success: false, error: 'Access Denied' }] } } + const { clock, ran, started } = await start($, on, options) + options.stallNth = { n, seen: 0 } + const first = $.tool.call(bash('dbt run -s a')) as Promise + for (let i = 0; i < 70; i++) await clock.advance(100) + expect((await first).deny).toContain('could not be read in time') + options.stallNth = undefined + await clock.advance(70_000) + const retry = $.tool.call(bash('dbt run -s a')) as Promise + for (let i = 0; i < 70; i++) await clock.advance(100) + expect((await retry).deny).toBeDefined() + expect(started).toEqual(['a', 'a']) + expect(ran).toEqual([]) +}) + test('a lookup lost in one pass does not refuse another pass whose own lookups all answered', async ($, on) => { let release: () => void = () => {} const options: Options = { answers: { slow: UNDER }, holds: { slow: new Promise(resolve => (release = resolve)) } } diff --git a/plugins/altimate-code/hooks/features/cost.tsx b/plugins/altimate-code/hooks/features/cost.tsx index ce8704a6..9d630571 100644 --- a/plugins/altimate-code/hooks/features/cost.tsx +++ b/plugins/altimate-code/hooks/features/cost.tsx @@ -302,7 +302,11 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P if (answer === 'unreadable') return unreadable() // A /cd while it was being asked: the answer is of the project left, and nothing here is allowed on it. const now = await sessionDir($, look, dir) - if (look.lost) return unreadable() + 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 }) + return unreadable() + } if (now !== dir) { await leaveProject($) seenDir = now @@ -730,8 +734,8 @@ async function estimateWithin($: EngineInterface, args: { model?: string; sql?: 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. + // A folder lost here is the gate's to refuse: it keeps the answer in hand if it is over the limit. const after = await sessionDir($, look, seenDir) - if (look.lost) return 'unreadable' if (running.generation !== generation || running.dir !== after) { if (inflight.get(key) === running) inflight.delete(key) return 'pending' From 1c1b129702baf8c95affb065e4c17ccdd6e49669 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 19:47:09 +0530 Subject: [PATCH 16/18] refactor: one refusal helper that records the hold, drops the unreachable post-loop guard, and tighter retry assertions Co-Authored-By: Claude Sonnet 5.5 --- .../hooks/features/cost.test.tsx | 4 ++-- plugins/altimate-code/hooks/features/cost.tsx | 23 +++++-------------- .../hooks/features/defer.test.ts | 1 - 3 files changed, 8 insertions(+), 20 deletions(-) diff --git a/plugins/altimate-code/hooks/features/cost.test.tsx b/plugins/altimate-code/hooks/features/cost.test.tsx index 9c711788..ac72c894 100644 --- a/plugins/altimate-code/hooks/features/cost.test.tsx +++ b/plugins/altimate-code/hooks/features/cost.test.tsx @@ -339,7 +339,7 @@ test('a line refused for its unreadable folder keeps the over-limit answer alrea await clock.advance(70_000) const retry = $.tool.call(bash(command)) as Promise for (let i = 0; i < 70; i++) await clock.advance(100) - expect((await retry).deny).toBeDefined() + expect((await retry).deny).toContain('over the') expect(started.filter(one => one === 'a')).toHaveLength(2) expect(ran).toEqual([]) }) @@ -355,7 +355,7 @@ for (const n of [5, 6]) test(`a folder lost just after an estimate came in over await clock.advance(70_000) const retry = $.tool.call(bash('dbt run -s a')) as Promise for (let i = 0; i < 70; i++) await clock.advance(100) - expect((await retry).deny).toBeDefined() + expect((await retry).deny).toContain('over the') expect(started).toEqual(['a', 'a']) expect(ran).toEqual([]) }) diff --git a/plugins/altimate-code/hooks/features/cost.tsx b/plugins/altimate-code/hooks/features/cost.tsx index 9d630571..dca6f513 100644 --- a/plugins/altimate-code/hooks/features/cost.tsx +++ b/plugins/altimate-code/hooks/features/cost.tsx @@ -266,9 +266,9 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P // One wait for the whole command, however many queries it names. const waitUntil = (await $.clock.now()) + GATE_WAIT_MS 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 () => { + // A refusal that 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 refuse = async (deny: string) => { // What a project changed meanwhile left is not this pass's to record. if (generation !== startedAt) return changedDeny(e.command) if (over.length > 0) { @@ -285,8 +285,9 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P } notes.delete(e.command) refused.set(e.command, await $.clock.now()) - return { deny: UNREADABLE_DENY } + return { deny } } + const unreadable = () => refuse(UNREADABLE_DENY) // What this pass already has, so the same query named twice on the line is asked for once. const taken = new Map() let failed = false @@ -348,7 +349,6 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P // 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() // A finished command's answers are taken: a later one asks again. A pending one keeps them for its retry. if (pending.length === 0) for (const key of taken.keys()) inflight.delete(key) @@ -356,18 +356,7 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P // An estimate still running decides nothing yet, and spends no allowance: the command is refused until it is in. if (pending.length > 0) { // What is already known to be over the limit stays held, so a failed estimate on the retry cannot free it. - if (over.length > 0) { - const worst = over.reduce((a, b) => (b.bytes > a.bytes ? b : a)) - await update($, costAtom, prev => { - const current = prev ?? FRESH - // An allowance belongs to the command it was given for: another command's hold does not inherit it. - const same = current.held?.command === e.command - return { ...current, allowOnce: same && current.allowOnce, held: { command: e.command, target: worst.label, bytes: worst.bytes, display: worst.display } } - }) - } - notes.delete(e.command) - refused.set(e.command, await $.clock.now()) - return { deny: `The scan of ${pending.join(', ')} is still being estimated (the dbt adapter may be starting). Run the same command again in a moment.` } + return refuse(`The scan of ${pending.join(', ')} is still being estimated (the dbt adapter may be starting). Run the same command again in a moment.`) } // A command held earlier stays held while its estimate cannot be made again: only /allow-scan or an estimate under the limit frees it. diff --git a/plugins/altimate-code/hooks/features/defer.test.ts b/plugins/altimate-code/hooks/features/defer.test.ts index 3d065df9..e48bab2f 100644 --- a/plugins/altimate-code/hooks/features/defer.test.ts +++ b/plugins/altimate-code/hooks/features/defer.test.ts @@ -269,7 +269,6 @@ test('through the plugin, Claude’s dbt commands run with --defer --state when test('a dbt project below the project, even under dbt_packages, keeps its own settings; the shell folder from an earlier cd counts', async ($, on) => { const seen = start($, on, DEFER, { times: 0 }, [ROOT, `${ROOT}/dbt_packages/pkg`, `${ROOT}/nested`]) - cwd = ROOT on('fs.list', (_: unknown, e: { path: string }) => ({ value: e.path === ROOT ? [{ name: 'nested', kind: 'dir' as const, size: 0, mtimeMs: 0, isLink: false }, { name: 'dbt_packages', kind: 'dir' as const, size: 0, mtimeMs: 0, isLink: false }] : e.path === `${ROOT}/dbt_packages` ? [{ name: 'pkg', kind: 'dir' as const, size: 0, mtimeMs: 0, isLink: false }] : [] })) await $.session.start({ cwd: ROOT, surface: 'terminal', isInteractive: true }) await $.tool.call({ tool: 'Bash', command: 'cd nested && dbt build' }) From ff811aecc2458585cb63c009eb7f81d868838a86 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 20:12:51 +0530 Subject: [PATCH 17/18] fix: an over-limit answer kept for an unreadable-folder refusal is visible to a parallel pass at once Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/hooks/features/cost.tsx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/plugins/altimate-code/hooks/features/cost.tsx b/plugins/altimate-code/hooks/features/cost.tsx index dca6f513..05e3aa1a 100644 --- a/plugins/altimate-code/hooks/features/cost.tsx +++ b/plugins/altimate-code/hooks/features/cost.tsx @@ -305,7 +305,12 @@ async function gate($: EngineInterface, e: BashCall, mine: { note?: object }): P 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 }) + 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 }) + over.push({ label, bytes: answer.maxBytes, point: answer.bytes, display: answer.display }) + } return unreadable() } if (now !== dir) { From 60db6849ef1ab9c914bb797c84d540d7477b5087 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 20:58:07 +0530 Subject: [PATCH 18/18] fix: a folder lookup that fails outright falls back to the session root; only one that times out refuses the scan Co-Authored-By: Claude Sonnet 5.5 --- .../hooks/features/cost.test.tsx | 32 +++++++++---------- plugins/altimate-code/hooks/features/cost.tsx | 8 +++-- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/plugins/altimate-code/hooks/features/cost.test.tsx b/plugins/altimate-code/hooks/features/cost.test.tsx index ac72c894..36f48a43 100644 --- a/plugins/altimate-code/hooks/features/cost.test.tsx +++ b/plugins/altimate-code/hooks/features/cost.test.tsx @@ -303,27 +303,25 @@ test('a stalled pass is not remembered as a project: the next pass, that reads t expect(ran).toContain('dbt run -s orders') }) -test('a folder lookup that fails is no known project either: a verdict of the project left is not reused for a scan', async ($, on) => { - const options: Options = { answers: { orders: NO_DRY_RUN } } - const { clock } = await start($, on, options) - expect(((await $.tool.call(bash('dbt run -s orders'))) as any).deny).toBeUndefined() - options.answers = { orders: OVER } - options.cwdFails = true - const pending = $.tool.call(bash('dbt run -s orders')) as Promise +test('a host whose session folder lookup always fails: the project is the session root and the gate works as usual', async ($, on) => { + const options: Options = { answers: { fine: UNDER, big: OVER }, cwdFails: true } + const { clock, ran, started } = await start($, on, options) + const under = $.tool.call(bash('dbt run -s fine')) as Promise for (let i = 0; i < 10; i++) await clock.advance(100) - expect((await pending).deny).toContain('could not be read in time') + expect((await under).deny).toBeUndefined() + const over = $.tool.call(bash('dbt run -s big')) as Promise + for (let i = 0; i < 10; i++) await clock.advance(100) + expect((await over).deny).toContain('over the') + expect(started).toEqual(['fine', 'big']) + expect(ran).toEqual(['dbt run -s fine']) }) -test('a folder lookup that fails before any folder was seen refuses the scan too, and starts no estimate', async ($, on) => { - const options: Options = { answers: { orders: UNDER } } - const { clock, started } = await start($, on, options) - options.cwdFails = true - // A fresh session: no folder has been read yet. - await $.session.start({ cwd: ROOT, surface: 'terminal', isInteractive: true }) - const pending = $.tool.call(bash('dbt run -s orders')) as Promise +test('a host whose session folder lookup always fails still answers /scan-cost', async ($, on) => { + const options: Options = { answers: { orders: OVER }, cwdFails: true } + const { clock } = await start($, on, options) + const run = $.command.run({ command: 'scan-cost', args: 'orders' } as any) as Promise for (let i = 0; i < 10; i++) await clock.advance(100) - expect((await pending).deny).toContain('could not be read in time') - expect(started).toEqual([]) + expect((await run).text).toContain('would scan 20.0 GiB') }) test('a line refused for its unreadable folder keeps the over-limit answer already in hand: its retry is still held when that estimate then fails', async ($, on) => { diff --git a/plugins/altimate-code/hooks/features/cost.tsx b/plugins/altimate-code/hooks/features/cost.tsx index 05e3aa1a..f36ff11d 100644 --- a/plugins/altimate-code/hooks/features/cost.tsx +++ b/plugins/altimate-code/hooks/features/cost.tsx @@ -105,7 +105,7 @@ type Look = { spent: number; lost: boolean } const newLook = (): Look => ({ spent: 0, lost: false }) /** - * The session's folder. A folder that cannot be read is no project and no change of project: it answers `fallback`, the folder + * The session's folder. A folder that times out is no project and no change of project: it answers `fallback`, the folder * last known, so nothing the gate holds for that project is touched, and the pass is marked lost: its caller refuses what needs the folder. */ async function sessionDir($: EngineInterface, look: Look, fallback: string | null): Promise { @@ -113,8 +113,10 @@ async function sessionDir($: EngineInterface, look: Look, fallback: string | nul const left = CWD_WAIT_MS - look.spent // 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 + // 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 }) + const read = left > 0 ? await within($, left, $.session.cwd().then(dir => ({ dir: dir as string | null }), failed)) : null look.spent += Date.now() - began if (!read) { look.lost = true