From 51050d50c0bf6ad26944a65a0437107c7d23ab04 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 17:11:01 +0530 Subject: [PATCH 01/34] feat: project health: new DataPilot insights on the models Claude changed, in the band, with an optional git push gate Co-Authored-By: Claude Sonnet 5.5 --- .../altimate-code/hooks/features/health.tsx | 264 ++++++++++++++++-- plugins/altimate-code/hooks/health.test.tsx | 207 ++++++++++++++ plugins/altimate-code/hooks/lineage.test.tsx | 2 + plugins/altimate-code/types/index.d.ts | 14 +- 4 files changed, 468 insertions(+), 19 deletions(-) create mode 100644 plugins/altimate-code/hooks/health.test.tsx diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 80ef14e2..43a6a408 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -1,47 +1,275 @@ // Project health: new DataPilot insights on the models Claude changed. // -// The plugin calls each function below at its moment, in the order features/index.ts fixes; one with -// nothing to do stays a no-op. This feature's own hooks (its commands and panes, each under a matcher -// no other part uses) go in `register`. The events the whole plugin shares (session start and end, -// turns, Bash and file tool calls, the band) come through the other functions, since a plugin hooks -// each of those once. The status-line part is set with `setStatusPart($, 'health', text)`. -import type { Args, EngineInterface, EventResult, On, RenderElement, ToolCallResult } from 'claude-code' +// At session start a baseline run records every (model, insight) pair the project already has. When a +// turn changed model files (.sql, .yml), the check runs again and the pairs the baseline lacks, on the +// models changed so far this session, are the new issues: the band shows them, and with +// `healthBlocksPush` on, `git push` is refused while any remain. The check is altimate-code's +// `dbt_health` tool, which needs write permission (it starts the dbt adapter), so it goes through +// the CLI. +import { atom, read, update } from 'claude-code' +import type { Args, EngineInterface, EventResult, On, PluginOptions, RenderElement, ToolCallResult } from 'claude-code' +import type { HealthState } from '../../types' +import { later, runTool } from '../altimate-cli' +import { setStatusPart } from '../status-line' import type { BashCall, ContextLines, EditedFile, FileCall } from '../tool-chain' import type { BandUi, FeatureContext } from './index' +export type HealthInsight = { uniqueId: string; path: string; name: string; severity: string; message: string; recommendation: string } +type HealthRun = { insights: HealthInsight[] } | { problem: string } + +const COMMAND = 'health' +/** A check can take minutes: the dbt adapter starts, then DataPilot reads the manifest. */ +const CHECK_MS = 9 * 60_000 +/** The most issues the push refusal and the report list. */ +const LISTED = 10 +const NEEDS_CLI = 'Project health needs an altimate-code build newer than 0.12.10 (one with the dbt_health tool)' + +const healthAtom = atom( + { plugin: 'altimate-code', key: 'health' } as const, + null as HealthState | null, +) + +const fresh = (): HealthState => ({ baseline: null, pending: [], touched: [], issues: [], problem: null, running: false }) + +let options: PluginOptions = {} +/** Bumped at each session start: a run that started before a /clear or reload doesn't write its answer. */ +let generation = 0 + +export const pairKey = (i: Pick) => `${i.uniqueId}\u0000${i.name}` + +/** The model a file is, by its path: the project-relative path DataPilot reports ends the file's path. */ +export function modelsOfFile(file: string, insights: readonly HealthInsight[]): Set { + const ids = new Set() + const norm = file.replace(/\\/g, '/') + for (const i of insights) if (i.path && (norm === i.path || norm.endsWith(`/${i.path.replace(/\\/g, '/')}`))) ids.add(i.uniqueId) + return ids +} + +/** The model names a properties (.yml) file lists, as `unique_id` last segments. */ +export function modelNamesInYaml(text: string): string[] { + const names: string[] = [] + let inModels = false + for (const line of text.split(/\r?\n/)) { + if (/^\S/.test(line)) inModels = /^models\s*:/.test(line) + else if (inModels) { + const m = /^\s{0,4}-\s+name:\s*['"]?([\w.-]+)['"]?\s*$/.exec(line) + if (m?.[1]) names.push(m[1]) + } + } + return names +} + +/** The insights the baseline lacks, on the given models. */ +export function newIssues(insights: readonly HealthInsight[], baseline: readonly string[], models: ReadonlySet): HealthInsight[] { + const had = new Set(baseline) + return insights.filter(i => models.has(i.uniqueId) && !had.has(pairKey(i))) +} + +const shortName = (id: string) => id.split('.').pop() ?? id + +/** "2 new health issues · stg_orders: hard coded references · /health" */ +export function bandText(issues: readonly HealthInsight[]): string { + const first = issues[0] + if (!first) return '' + const more = new Set(issues.map(i => shortName(i.uniqueId))).size - 1 + return `${issues.length} new health issue${issues.length === 1 ? '' : 's'} · ${shortName(first.uniqueId)}: ${first.name.toLowerCase()}${more > 0 ? ` (+${more} more model${more === 1 ? '' : 's'})` : ''} · /${COMMAND}` +} + +export function reportText(issues: readonly HealthInsight[]): string { + const lines = issues.slice(0, LISTED).map(i => `- ${shortName(i.uniqueId)}: ${i.name.toLowerCase()} (${i.severity.toLowerCase()}) ${i.message}${i.recommendation ? ` → ${i.recommendation}` : ''}`) + if (issues.length > LISTED) lines.push(`- and ${issues.length - LISTED} more`) + return lines.join('\n') +} + +/** Whether a Bash command contains a `git push` (after `&&`, `||`, `;`, `|` or a newline, past env assignments and git's own options). */ +export function isGitPush(command: string): boolean { + return /(?:^|&&|\|\||[;|\n])\s*(?:\w+=\S*\s+)*git\s+(?:(?:-C|-c)\s+\S+\s+|--[\w-]+(?:=\S+)?\s+)*push(?:\s|$)/.test(command) +} + +/** The `dbt_health` arguments for the plugin's `healthConfigPath` setting. */ +export function toolArgs(configPath: unknown): Record { + const config = typeof configPath === 'string' ? configPath.trim() : '' + return config ? { config_path: config } : {} +} + +/** Asks altimate-code's dbt_health for the project's insights. */ +async function check($: EngineInterface): Promise { + const until = (await $.clock.now()) + CHECK_MS + let result: Awaited> + try { + result = await runTool($, 'dbt_health', toolArgs(options.healthConfigPath), { write: true, until }) + } catch (error) { + return { problem: `Project health could not run: ${String(error).split('\n')[0]}` } + } + const insights = result.metadata?.insights + if (result.ok && Array.isArray(insights)) return { insights: insights as HealthInsight[] } + if (/unknown tool/i.test(result.text)) return { problem: NEEDS_CLI } + return { problem: `Project health: ${(result.text || 'no answer').split('\n')[0]}` } +} + +/** Whether the status line carries the health part now. */ +let drawn = false + +async function drawStatus($: EngineInterface, state: HealthState | null) { + const n = state?.issues.length ?? 0 + if (n === 0 && !drawn) return + drawn = n > 0 + await setStatusPart($, 'health', n > 0 ? `${n} health issue${n === 1 ? '' : 's'}` : null) +} + +/** The baseline run: its pairs are what the project already had. */ +async function startBaseline($: EngineInterface, mine: number) { + const run = await check($) + if (mine !== generation) return + const state = await update($, healthAtom, prev => { + const base = prev ?? fresh() + return 'problem' in run ? { ...base, problem: run.problem } : { ...base, baseline: run.insights.map(pairKey), problem: null } + }) + void state +} + +/** Runs the check over the changes so far; returns the issues, or why it could not. */ +async function recheck($: EngineInterface, mine: number): Promise<{ issues: HealthInsight[] } | { problem: string } | null> { + const before = await read($, healthAtom) + if (!before?.baseline || before.running) return before?.problem ? { problem: before.problem } : null + await update($, healthAtom, prev => ({ ...(prev ?? fresh()), running: true })) + const run = await check($) + if (mine !== generation) return null + const touched = new Set() + let result: { issues: HealthInsight[] } | { problem: string } + if ('problem' in run) { + result = { problem: run.problem } + } else { + const current = await read($, healthAtom) + for (const file of current?.touched ?? []) { + for (const id of modelsOfFile(file, run.insights)) touched.add(id) + } + // A changed properties file names its models; one that no insight reports has no insights to match. + const names = new Set() + for (const file of current?.touched ?? []) { + if (/\.ya?ml$/i.test(file)) { + const text = await $.fs.read(file).catch(() => '') + for (const name of modelNamesInYaml(text)) names.add(name) + } + } + for (const i of run.insights) if (names.has(shortName(i.uniqueId))) touched.add(i.uniqueId) + result = { issues: newIssues(run.insights, current?.baseline ?? [], touched) } + } + const next = await update($, healthAtom, prev => { + const base = prev ?? fresh() + return 'problem' in result ? { ...base, running: false, problem: result.problem } : { ...base, running: false, problem: null, issues: result.issues } + }) + void next + await drawStatus($, await read($, healthAtom)) + return result +} + /** Adds this feature's own hooks. */ -export function register(on: On, context: FeatureContext) {} +export function register(on: On, context: FeatureContext) { + options = context.options + on('command.run', { command: COMMAND }, async $ => { + const state = await read($, healthAtom) + if (!state?.baseline && !state?.running) return { text: state?.problem ?? 'Project health has not finished its first check yet.' } + const mine = generation + const result = await recheck($, mine) + if (!result) return { text: 'Project health is still checking.' } + if ('problem' in result) return { text: result.problem } + return { text: result.issues.length ? `${bandText(result.issues)}\n${reportText(result.issues)}` : 'No new health issues in the models changed this session.' } + }) +} -/** After the session started: the place to register commands. */ -export async function sessionStart($: EngineInterface, e: Args<'session.start'>) {} +/** Whether the session's folder, or one above it, holds a dbt project. */ +async function inDbtProject($: EngineInterface): Promise { + let dir = await $.session.root().catch(() => '') + for (let i = 0; dir && i < 8; i++) { + if (await $.fs.exists(`${dir}/dbt_project.yml`).catch(() => false)) return true + const parent = dir.replace(/[\\/][^\\/]*$/, '') + if (parent === dir) break + dir = parent + } + return false +} + +export async function sessionStart($: EngineInterface, e: Args<'session.start'>) { + generation += 1 + await update($, healthAtom, () => fresh()) + if (drawn) await drawStatus($, null) + await $.command.register({ name: COMMAND, description: 'Show the project health issues Claude introduced' }) + // The baseline takes a while (the dbt adapter starts): nothing waits for it. A `-p` run or the SDK has + // nobody to show it to. + if (e.isInteractive && (await inDbtProject($))) { + const mine = generation + later($, () => startBaseline($, mine)) + } +} export async function turnStart($: EngineInterface, e: Args<'turn.start'>) {} -/** After the turn's answer; `out` is what the turn ended with. */ -export async function turnComplete($: EngineInterface, e: Args<'turn.complete'>, out: EventResult<'turn.complete'>) {} +/** After the turn's answer: when model files changed, check again. */ +export async function turnComplete($: EngineInterface, e: Args<'turn.complete'>, out: EventResult<'turn.complete'>) { + if ((e as { agentId?: string }).agentId) return + const state = await read($, healthAtom) + if (!state || state.pending.length === 0) return + await update($, healthAtom, prev => ({ ...(prev ?? fresh()), pending: [] })) + const mine = generation + later($, async () => { + const result = await recheck($, mine) + if (!result || mine !== generation) return + if ('problem' in result) { + // Said once, not at every turn. + if (result.problem !== state.problem) $.ui.toast(`◆ ${result.problem}`, { timeoutMs: 8000 }) + return + } + if (result.issues.length) $.ui.toast(`◆ ${bandText(result.issues)}`, { timeoutMs: 8000 }) + }) +} -export async function sessionEnd($: EngineInterface, e: Args<'session.end'>) {} +export async function sessionEnd($: EngineInterface, e: Args<'session.end'>) { + generation += 1 +} /** This feature's line in the band, under the lineage line; null when there is nothing to say. */ export async function bandLine($: EngineInterface, ui: BandUi): Promise { - return null + const state = await read($, healthAtom) + if (!state || state.issues.length === 0) return null + const { Text } = ui + return {bandText(state.issues)} } -/** The command to run in place of `e.command`; null keeps it. */ export async function bashRewrite($: EngineInterface, e: BashCall): Promise { return null } -/** `{ deny }` refuses the Bash call; null lets it run. */ +/** The refusal for `command` when the push gate is on and `issues` are open; null lets the command run. */ +export function pushRefusal(blocks: unknown, command: string, issues: readonly HealthInsight[]): { deny: string } | null { + if (!(blocks === true || blocks === 'true') || !isGitPush(command) || issues.length === 0) return null + return { + deny: `git push is held: the models changed this session have ${issues.length} new project health issue${issues.length === 1 ? '' : 's'}.\n${reportText(issues)}\nFix them, or turn off "Project health blocks git push" in the plugin's settings.`, + } +} + +/** With `healthBlocksPush` on, `git push` is refused while the models changed have new health issues. */ export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> { - return null + if (!isGitPush(e.command)) return null + const state = await read($, healthAtom) + return pushRefusal(options.healthBlocksPush, e.command, state?.issues ?? []) } export async function bashAfter($: EngineInterface, e: BashCall, result: ToolCallResult): Promise {} -/** `file.before` holds the text from before the edit only when `editReadsBefore` is true. */ -export async function editAfter($: EngineInterface, e: FileCall, result: ToolCallResult, file: EditedFile): Promise {} +/** A model file Claude changed is checked at the end of the turn. */ +export async function editAfter($: EngineInterface, e: FileCall, result: ToolCallResult, file: EditedFile): Promise { + if (!/\.(sql|ya?ml)$/i.test(file.path)) return + await update($, healthAtom, prev => { + const base = prev ?? fresh() + return { + ...base, + pending: base.pending.includes(file.path) ? base.pending : [...base.pending, file.path], + touched: base.touched.includes(file.path) ? base.touched : [...base.touched, file.path], + } + }) +} export async function readOrEditAfter($: EngineInterface, e: FileCall, result: ToolCallResult, file: { path: string }): Promise {} diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx new file mode 100644 index 00000000..d05d5021 --- /dev/null +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -0,0 +1,207 @@ +import { expect, mock, test } from 'claude-code/testing' + +import { bandText, isGitPush, modelNamesInYaml, modelsOfFile, newIssues, pairKey, pushRefusal, reportText, toolArgs } from './features/health' + +const ROOT = '/repo' +const STG = `${ROOT}/models/staging/stg_orders.sql` +const FCT = `${ROOT}/models/marts/fct_orders.sql` +const NOTES = `${ROOT}/README.md` + +const BAND = { + plugin: 'altimate-code', + component: 'AbovePrompt', + props: { hasSurvey: false, isCollapsed: false, isWorking: false, maxRows: 10, bodyColumns: 120, scroll: { offset: 0, bodyRows: 10 }, view: {} }, +} as const + +const insight = (id: string, path: string, name: string, message = `${name} found`) => ({ + uniqueId: `model.shop.${id}`, + path, + name, + severity: 'WARNING', + message, + recommendation: `Fix ${name}`, +}) + +// What the project already has before Claude changes anything. +const OLD_STG = insight('stg_orders', 'models/staging/stg_orders.sql', 'Missing documentation') +const OLD_FCT = insight('fct_orders', 'models/marts/fct_orders.sql', 'Missing tests') +// What a change adds. +const NEW_STG = insight('stg_orders', 'models/staging/stg_orders.sql', 'Hard coded references', 'stg_orders reads raw.orders directly') +const NEW_FCT = insight('fct_orders', 'models/marts/fct_orders.sql', 'Model fanout', 'fct_orders has 4 direct children') + +type Options = { + /** The tool's insights, per run (the last repeats). */ + runs: ReturnType[][] + /** What the CLI prints instead of an answer. */ + cliStdout?: string + yaml?: Record +} + +async function start($: any, on: any, options: Options) { + const clock = mock.clock(on) + const calls: { tool: string; args: Record; argv: readonly string[] }[] = [] + const toasts: string[] = [] + const statuses: string[] = [] + on('command.register', () => ({ value: { command: 'health' } })) + on('turn.start', (_: unknown, e: { turnId: string }) => ({ turnId: e.turnId })) + on('session.end', () => ({ sessionId: 'test' })) + 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('ui.toast', (_: unknown, e: { text: string }) => { + toasts.push(e.text) + return { value: undefined } + }) + on('ui.status', (_: unknown, e: { text?: string }) => { + statuses.push(String(e.text ?? '')) + return { value: undefined } + }) + on('ui.render', ($$: any, e: any) => { + const { Box } = $$.ui.resolve(e) + return + }) + on('fs.exists', (_: unknown, e: { path: string }) => ({ value: e.path === `${ROOT}/dbt_project.yml` })) + on('fs.read', (_: unknown, e: { path: string }) => ({ value: options.yaml?.[e.path] ?? '' })) + on('tool.list', () => ({ value: [] })) + on('mcp.connect', () => ({ value: { isConnected: false, reason: 'not_connected', message: 'off' } })) + on('tool.call', () => ({ result: { ok: true } })) + let run = 0 + on('process.run', (_: unknown, e: { argv: readonly string[] }) => { + const [, command, , tool = ''] = e.argv + if (command !== 'tool') return { value: { exitCode: 1, stdout: '', stderr: 'not this test' } } + const args = JSON.parse(String(e.argv[e.argv.indexOf('--args') + 1] ?? '{}')) + calls.push({ tool, args, argv: e.argv }) + if (options.cliStdout !== undefined) return { value: { exitCode: 0, stdout: options.cliStdout, stderr: '' } } + const insights = options.runs[Math.min(run++, options.runs.length - 1)] + return { value: { exitCode: 0, stdout: JSON.stringify({ status: 'ok', output: '', metadata: { success: true, insights } }), stderr: '' } } + }) + await $.session.start({ cwd: ROOT, surface: 'terminal', isInteractive: true }) + await clock.settle() + return { clock, calls, toasts, statuses } +} + +async function endTurn($: any, clock: any, id: string) { + await $.turn.complete({ answer: 'Done.', durationMs: 5, isAborted: false, reason: 'answer', turnId: id } as any) + await clock.settle() +} + +const edit = (file: string) => ({ tool: 'Edit' as const, file_path: file, old_string: 'a', new_string: 'b' }) + +test('a baseline run at session start; a changed model with a new insight shows in the band, not the ones the project already had', async ($, on) => { + const { clock, calls, toasts, statuses } = await start($, on, { runs: [[OLD_STG, OLD_FCT], [OLD_STG, OLD_FCT, NEW_STG, NEW_FCT]] }) + expect(calls.map(c => c.tool)).toEqual(['dbt_health']) + // The check starts the dbt adapter, so it asks for write permission. + expect(calls[0]!.argv).toContain('--allow-write') + + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + + expect(calls).toHaveLength(2) + // fct_orders has a new insight too, but Claude did not change it. + expect(toasts).toEqual(['◆ 1 new health issue · stg_orders: hard coded references · /health']) + expect(statuses.at(-1)).toContain('1 health issue') + const band = await $.ui.mount({ ...BAND, surface: 'terminal' }) + expect(await band.find({ type: 'Text', text: '1 new health issue · stg_orders: hard coded references · /health' })).toBeDefined() + await band.unmount() +}) + +test('two changed models give "2 new health issues" with the first model named', async ($, on) => { + const { clock } = await start($, on, { runs: [[OLD_STG], [OLD_STG, NEW_STG, NEW_FCT]] }) + await $.tool.call(edit(STG)) + await $.tool.call(edit(FCT)) + await endTurn($, clock, 't1') + const band = await $.ui.mount({ ...BAND, surface: 'terminal' }) + expect(await band.find({ type: 'Text', text: /2 new health issues · stg_orders: hard coded references \(\+1 more model\) · \/health/ })).toBeDefined() + await band.unmount() +}) + +test('a turn that changed no model file runs no check; other files are ignored', async ($, on) => { + const { clock, calls, toasts } = await start($, on, { runs: [[OLD_STG], [OLD_STG, NEW_STG]] }) + await $.tool.call(edit(NOTES)) + await endTurn($, clock, 't1') + expect(calls).toHaveLength(1) + expect(toasts).toEqual([]) + const band = await $.ui.mount({ ...BAND, surface: 'terminal' }) + expect(await band.find({ type: 'Text', text: /health issue/ })).toBeUndefined() + await band.unmount() +}) + +test('an insight the baseline already had is not new', async ($, on) => { + const { clock, toasts } = await start($, on, { runs: [[OLD_STG], [OLD_STG]] }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + expect(toasts).toEqual([]) +}) + +test('a changed properties file checks the models it lists', async ($, on) => { + const yml = `${ROOT}/models/staging/_staging.yml` + const { clock, toasts } = await start($, on, { + runs: [[], [NEW_STG]], + yaml: { [yml]: 'version: 2\nmodels:\n - name: stg_orders\n description: orders\n' }, + }) + await $.tool.call(edit(yml)) + await endTurn($, clock, 't1') + expect(toasts).toEqual(['◆ 1 new health issue · stg_orders: hard coded references · /health']) +}) + +test('git push goes through by default (the setting is off), with new issues open', async ($, on) => { + const { clock } = await start($, on, { runs: [[], [NEW_STG]] }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + const band = await $.ui.mount({ ...BAND, surface: 'terminal' }) + expect(await band.find({ type: 'Text', text: /1 new health issue/ })).toBeDefined() + await band.unmount() + const allowed: any = await $.tool.call({ tool: 'Bash', command: 'git push origin main' } as any) + expect(allowed?.deny).toBeUndefined() +}) + +test('with healthBlocksPush on, git push is refused while new issues exist and the refusal lists them; nothing else is', () => { + const denied = pushRefusal(true, 'git add -A && git push origin feat/x', [NEW_STG, NEW_FCT]) + expect(denied?.deny).toContain('git push is held: the models changed this session have 2 new project health issues.') + expect(denied?.deny).toContain('- stg_orders: hard coded references (warning) stg_orders reads raw.orders directly') + expect(denied?.deny).toContain('- fct_orders: model fanout (warning) fct_orders has 4 direct children') + expect(pushRefusal('true', 'git push', [NEW_STG])?.deny).toContain('1 new project health issue.') + // Setting off, no issues, or not a push. + expect(pushRefusal(false, 'git push', [NEW_STG])).toBeNull() + expect(pushRefusal(true, 'git push', [])).toBeNull() + expect(pushRefusal(true, 'git status', [NEW_STG])).toBeNull() +}) + +test('the configured config path is passed to the tool, empty means the defaults', () => { + expect(toolArgs('datapilot.yml')).toEqual({ config_path: 'datapilot.yml' }) + expect(toolArgs(' datapilot.yml ')).toEqual({ config_path: 'datapilot.yml' }) + expect(toolArgs('')).toEqual({}) + expect(toolArgs(undefined)).toEqual({}) +}) + +test('a CLI without the tool, or a tool that refuses, is said once and never blocks a push', async ($, on) => { + const { clock, toasts } = await start($, on, { + runs: [[]], + cliStdout: JSON.stringify({ status: 'error', output: 'DataPilot isn\'t installed in python3. Install the `altimate-datapilot-cli` package', metadata: { success: false, insights: [] } }), + }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + await $.tool.call(edit(STG)) + await endTurn($, clock, 't2') + expect(toasts).toEqual([]) + const push: any = await $.tool.call({ tool: 'Bash', command: 'git push' } as any) + expect(push?.deny).toBeUndefined() +}) + +test('the pure helpers', () => { + expect(isGitPush('git push')).toBe(true) + expect(isGitPush('git -C sub push origin x')).toBe(true) + expect(isGitPush('GIT_SSH=ssh git push --force')).toBe(true) + expect(isGitPush('git add . && git push')).toBe(true) + expect(isGitPush('git pushd')).toBe(false) + expect(isGitPush('echo git push')).toBe(false) + expect(isGitPush('git status')).toBe(false) + + expect(pairKey(NEW_STG)).toBe('model.shop.stg_orders\u0000Hard coded references') + expect([...modelsOfFile(STG, [OLD_STG, OLD_FCT])]).toEqual(['model.shop.stg_orders']) + expect([...modelsOfFile('/elsewhere/other.sql', [OLD_STG])]).toEqual([]) + expect(newIssues([OLD_STG, NEW_STG, NEW_FCT], [pairKey(OLD_STG)], new Set(['model.shop.stg_orders']))).toEqual([NEW_STG]) + expect(modelNamesInYaml('version: 2\nmodels:\n - name: a\n - name: "b"\nsources:\n - name: c\n')).toEqual(['a', 'b']) + expect(bandText([NEW_STG])).toBe('1 new health issue · stg_orders: hard coded references · /health') + expect(reportText([NEW_STG])).toBe('- stg_orders: hard coded references (warning) stg_orders reads raw.orders directly → Fix Hard coded references') +}) diff --git a/plugins/altimate-code/hooks/lineage.test.tsx b/plugins/altimate-code/hooks/lineage.test.tsx index 2d3012df..53e6674f 100644 --- a/plugins/altimate-code/hooks/lineage.test.tsx +++ b/plugins/altimate-code/hooks/lineage.test.tsx @@ -185,6 +185,8 @@ async function start($: any, on: any, options: Options = {}) { if (command === 'tool' && tool === 'dbt_lineage' && options.holdCliLineage) await options.holdCliLineage // The integrations half of the plugin asks the CLI for its status at session start. if (command !== 'tool') return { value: { exitCode: 1, stdout: '', stderr: 'not this test' } } + // The project health check the mod starts at session start is not part of these tests. + if (tool === 'dbt_health') return { value: { exitCode: 0, stdout: JSON.stringify({ status: 'ok', output: '', metadata: { success: true, insights: [] } }), stderr: '' } } const args = JSON.parse(String(e.argv[e.argv.indexOf('--args') + 1] ?? '{}')) calls.push(`cli:${tool}${args.include_graph ? ':graph' : ''}`) if (options.cliFailure) return { value: { exitCode: 1, stdout: JSON.stringify({ status: 'error', output: 'the dbt project could not start', metadata: {} }), stderr: '' } } diff --git a/plugins/altimate-code/types/index.d.ts b/plugins/altimate-code/types/index.d.ts index 93c272e6..fd241431 100644 --- a/plugins/altimate-code/types/index.d.ts +++ b/plugins/altimate-code/types/index.d.ts @@ -210,7 +210,19 @@ export interface CostState {} // Project health: the health check issues on the changed models. // Filled in by that feature alone (hooks/features/); the other features never read or write it. // --------------------------------------------------------------------------------------------------- -export interface HealthState {} +export interface HealthState { + /** `uniqueId\u0000insight name` of every insight the project had at session start; null until that run lands. */ + baseline: string[] | null + /** Model files changed since the last check. */ + pending: string[] + /** Every model file changed this session. */ + touched: string[] + /** What the last check found that the baseline lacked, on the touched models. */ + issues: Array<{ uniqueId: string; path: string; name: string; severity: string; message: string; recommendation: string }> + /** Why the check can't run, shown once per distinct reason. */ + problem: string | null + running: boolean +} // --------------------------------------------------------------------------------------------------- // SQL graph: the edited model's SQL graph. From 45c795f03e396caa7f6a3f0a5811a7eec753e2be Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 21:21:58 +0530 Subject: [PATCH 02/34] fix: project health band updates Co-Authored-By: Claude Sonnet 5.5 --- .../altimate-code/hooks/features/health.tsx | 459 +++++++++++++----- plugins/altimate-code/hooks/health.test.tsx | 306 +++++++++--- plugins/altimate-code/types/index.d.ts | 15 +- 3 files changed, 593 insertions(+), 187 deletions(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 43a6a408..82486f38 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -1,65 +1,69 @@ // Project health: new DataPilot insights on the models Claude changed. // -// At session start a baseline run records every (model, insight) pair the project already has. When a -// turn changed model files (.sql, .yml), the check runs again and the pairs the baseline lacks, on the -// models changed so far this session, are the new issues: the band shows them, and with -// `healthBlocksPush` on, `git push` is refused while any remain. The check is altimate-code's -// `dbt_health` tool, which needs write permission (it starts the dbt adapter), so it goes through -// the CLI. +// At session start a baseline run records every (model, insight) pair the project already has. A check +// runs again after the turn in which Claude changed model files (.sql, .yml), and the pairs the +// baseline lacks, on the models those files hold, are the new issues: the band shows them, and with +// `healthBlocksPush` on, `git push` is held until the check of the latest changes is in and refused +// while any issue remains. The check is altimate-code's `dbt_health` tool, which needs write +// permission (it starts the dbt adapter), so it goes through the CLI. +// +// One check runs at a time. Changes that land while one runs are checked by the next, which starts +// when that one ends. import { atom, read, update } from 'claude-code' import type { Args, EngineInterface, EventResult, On, PluginOptions, RenderElement, ToolCallResult } from 'claude-code' -import type { HealthState } from '../../types' -import { later, runTool } from '../altimate-cli' +import type { HealthInsight, HealthState } from '../../types' +import { later, runTool, within } from '../altimate-cli' import { setStatusPart } from '../status-line' import type { BashCall, ContextLines, EditedFile, FileCall } from '../tool-chain' import type { BandUi, FeatureContext } from './index' -export type HealthInsight = { uniqueId: string; path: string; name: string; severity: string; message: string; recommendation: string } +export type { HealthInsight } type HealthRun = { insights: HealthInsight[] } | { problem: string } const COMMAND = 'health' /** A check can take minutes: the dbt adapter starts, then DataPilot reads the manifest. */ const CHECK_MS = 9 * 60_000 +/** How long /health waits for a running check before it answers with what it has. */ +const COMMAND_WAIT_MS = 3_000 /** The most issues the push refusal and the report list. */ const LISTED = 10 const NEEDS_CLI = 'Project health needs an altimate-code build newer than 0.12.10 (one with the dbt_health tool)' +const ROOT_FILES = new Set(['dbt_project.yml', 'profiles.yml', 'packages.yml', 'dependencies.yml', 'selectors.yml']) +const SKIPPED_DIRS = /(?:^|\/)(?:target|dbt_packages|dbt_modules|node_modules|\.git|\.github|logs)\// -const healthAtom = atom( - { plugin: 'altimate-code', key: 'health' } as const, - null as HealthState | null, -) +const healthAtom = atom({ plugin: 'altimate-code', key: 'health' } as const, null as HealthState | null) const fresh = (): HealthState => ({ baseline: null, pending: [], touched: [], issues: [], problem: null, running: false }) let options: PluginOptions = {} -/** Bumped at each session start: a run that started before a /clear or reload doesn't write its answer. */ +/** Bumped at each session start and end: a check that began before does not write its answer. */ let generation = 0 +/** The check loop that is running, if any. */ +let running: { generation: number; done: Promise } | null = null +/** Whether this session checks at all: a dbt project was found, and somebody will see the answer or the push gate needs it. */ +let enabled = false +/** Whether the session may check once it finds a dbt project (see `enabled`). */ +let eligible = false +/** The problem told to the user last, so the same one is not told at every turn. */ +let toldProblem: string | null = null +/** Whether the status line carries the health part now. */ +let drawn = false +/** dbt project roots by folder, null for a folder in no project. */ +const projects = new Map() export const pairKey = (i: Pick) => `${i.uniqueId}\u0000${i.name}` -/** The model a file is, by its path: the project-relative path DataPilot reports ends the file's path. */ -export function modelsOfFile(file: string, insights: readonly HealthInsight[]): Set { +const norm = (p: string) => p.replace(/\\/g, '/') + +/** The models whose SQL file or properties file is one of `files`. */ +export function modelsOfFiles(files: readonly string[], insights: readonly HealthInsight[]): Set { + const changed = new Set(files.map(norm)) const ids = new Set() - const norm = file.replace(/\\/g, '/') - for (const i of insights) if (i.path && (norm === i.path || norm.endsWith(`/${i.path.replace(/\\/g, '/')}`))) ids.add(i.uniqueId) + for (const i of insights) if ((i.file && changed.has(norm(i.file))) || (i.properties && changed.has(norm(i.properties)))) ids.add(i.uniqueId) return ids } -/** The model names a properties (.yml) file lists, as `unique_id` last segments. */ -export function modelNamesInYaml(text: string): string[] { - const names: string[] = [] - let inModels = false - for (const line of text.split(/\r?\n/)) { - if (/^\S/.test(line)) inModels = /^models\s*:/.test(line) - else if (inModels) { - const m = /^\s{0,4}-\s+name:\s*['"]?([\w.-]+)['"]?\s*$/.exec(line) - if (m?.[1]) names.push(m[1]) - } - } - return names -} - /** The insights the baseline lacks, on the given models. */ export function newIssues(insights: readonly HealthInsight[], baseline: readonly string[], models: ReadonlySet): HealthInsight[] { const had = new Set(baseline) @@ -68,11 +72,11 @@ export function newIssues(insights: readonly HealthInsight[], baseline: readonly const shortName = (id: string) => id.split('.').pop() ?? id -/** "2 new health issues · stg_orders: hard coded references · /health" */ +/** "2 new health issues · stg_orders: hard coded references (+1 more model) · /health" */ export function bandText(issues: readonly HealthInsight[]): string { const first = issues[0] if (!first) return '' - const more = new Set(issues.map(i => shortName(i.uniqueId))).size - 1 + const more = new Set(issues.map(i => i.uniqueId)).size - 1 return `${issues.length} new health issue${issues.length === 1 ? '' : 's'} · ${shortName(first.uniqueId)}: ${first.name.toLowerCase()}${more > 0 ? ` (+${more} more model${more === 1 ? '' : 's'})` : ''} · /${COMMAND}` } @@ -82,9 +86,140 @@ export function reportText(issues: readonly HealthInsight[]): string { return lines.join('\n') } -/** Whether a Bash command contains a `git push` (after `&&`, `||`, `;`, `|` or a newline, past env assignments and git's own options). */ +const OPERATORS = new Set([';', '&', '|', '(', ')', '\n', '{', '}']) +/** Words that come before the command they apply to: `sudo git push`, `FOO=1 git push`, `! git push`. */ +const LEADERS = new Set(['sudo', 'command', 'env', 'time', 'nohup', 'exec', 'builtin', 'nice', 'doas', '!', 'if', 'then', 'else', 'elif', 'do', 'while', 'until']) +/** The options of those words that take their value as the next word. */ +const LEADER_VALUE_OPTIONS: Record = { + sudo: ['-u', '-g', '-h', '-p', '-C', '-D', '-R', '-T', '-U'], + doas: ['-u', '-C'], + env: ['-u', '-C', '-S'], + nice: ['-n'], +} +/** git's options that take their value as the next word. */ +const GIT_VALUE_OPTIONS = new Set(['-C', '-c', '--git-dir', '--work-tree', '--namespace', '--exec-path', '--super-prefix', '--config-env']) +const SHELLS = new Set(['bash', 'sh', 'zsh', 'dash', 'ksh']) + +/** The simple commands of a shell line, each as its words with quotes removed; a quoted word stays one word. */ +export function simpleCommands(line: string): string[][] { + const commands: string[][] = [] + const nested: string[][] = [] + let words: string[] = [] + let word = '' + let inWord = false + const endWord = () => { + if (inWord) words.push(word) + word = '' + inWord = false + } + const endCommand = () => { + endWord() + if (words.length) commands.push(words) + words = [] + } + for (let i = 0; i < line.length; i++) { + const c = line[i]! + if (c === "'") { + const close = line.indexOf("'", i + 1) + word += line.slice(i + 1, close < 0 ? line.length : close) + inWord = true + i = close < 0 ? line.length : close + } else if (c === '"') { + inWord = true + i++ + for (; i < line.length && line[i] !== '"'; i++) { + if (line[i] === '$' && line[i + 1] === '(') { + // A substitution inside quotes still runs: its commands are commands of the line. + let depth = 0 + let j = i + 1 + for (; j < line.length; j++) { + if (line[j] === '(') depth++ + else if (line[j] === ')' && --depth === 0) break + } + nested.push(...simpleCommands(line.slice(i + 2, j))) + word += line.slice(i, j + 1) + i = j + continue + } + if (line[i] === '\\' && i + 1 < line.length) i++ + word += line[i] + } + } else if (c === '\\' && line[i + 1] === '\n') { + i++ + } else if (c === '\\' && i + 1 < line.length) { + word += line[++i] + inWord = true + } else if (c === '$' && line[i + 1] === '(') { + endCommand() + i++ + } else if (c === '`' || OPERATORS.has(c)) { + endCommand() + } else if (c === ' ' || c === '\t' || c === '\r') { + endWord() + } else { + word += c + inWord = true + } + } + endCommand() + return [...commands, ...nested] +} + +/** Where the command itself starts in a simple command's words: past env assignments, `sudo -u x`, `nice -n 10`, and the like. */ +function leaderEnd(words: readonly string[]): number { + let i = 0 + let leader = '' + while (i < words.length) { + const w = words[i]! + if (/^[A-Za-z_]\w*=/.test(w)) i++ + else if (LEADERS.has(w)) leader = words[i++]! + else if (leader && w.startsWith('-')) i += LEADER_VALUE_OPTIONS[leader]?.includes(w) ? 2 : 1 + else break + } + return i +} + +function pushes(words: readonly string[]): boolean { + const start = leaderEnd(words) + let i = start + const bin = (words[i] ?? '').split('/').pop() ?? '' + if (SHELLS.has(bin)) { + const c = words.findIndex((w, j) => j > i && /^-[a-z]*c$/.test(w)) + return c >= 0 && c + 1 < words.length && isGitPush(words[c + 1]!) + } + if (bin !== 'git') return false + for (i++; i < words.length && words[i]!.startsWith('-'); i++) if (GIT_VALUE_OPTIONS.has(words[i]!)) i++ + return words[i] === 'push' +} + +/** Whether a Bash command line runs `git push`, in a pipeline, a list, a subshell or `bash -c`; not one that only names it in quotes. */ export function isGitPush(command: string): boolean { - return /(?:^|&&|\|\||[;|\n])\s*(?:\w+=\S*\s+)*git\s+(?:(?:-C|-c)\s+\S+\s+|--[\w-]+(?:=\S+)?\s+)*push(?:\s|$)/.test(command) + return simpleCommands(command).some(pushes) +} + +const MODEL_FILE = /\.(sql|ya?ml)$/i +/** Commands that change the files they are given. */ +const WRITERS = new Set(['mv', 'cp', 'rm', 'tee', 'patch', 'touch', 'truncate', 'install', 'ln']) +/** git subcommands that change files in the working tree. */ +const GIT_WRITERS = new Set(['checkout', 'restore', 'apply', 'stash', 'reset', 'revert', 'merge', 'pull', 'rebase', 'cherry-pick', 'switch', 'mv', 'rm', 'am']) + +/** The model-like file paths a Bash command line writes: redirects, `tee`, `sed -i`, `mv`, `git checkout -- file`. */ +export function filesWrittenBy(command: string): string[] { + const out: string[] = [] + for (const words of simpleCommands(command)) { + const models = (list: readonly string[]) => list.filter(w => MODEL_FILE.test(w) && !w.startsWith('-')) + for (let i = 0; i < words.length; i++) { + const redirect = /^\d*>>?(.*)$/.exec(words[i]!) + if (redirect) out.push(...models([redirect[1] || words[i + 1] || ''])) + } + const at = leaderEnd(words) + const bin = (words[at] ?? '').split('/').pop() ?? '' + const args = words.slice(at + 1) + if (WRITERS.has(bin)) out.push(...models(args)) + else if ((bin === 'sed' || bin === 'perl') && args.some(w => /^-[a-zA-Z]*i/.test(w) || w === '--in-place' || w.startsWith('--in-place='))) out.push(...models(args)) + else if (bin === 'git' && args.some(w => GIT_WRITERS.has(w))) out.push(...models(args)) + } + return out } /** The `dbt_health` arguments for the plugin's `healthConfigPath` setting. */ @@ -93,6 +228,36 @@ export function toolArgs(configPath: unknown): Record { return config ? { config_path: config } : {} } +/** The dbt project folder above `dir` (up to 40 levels), cached; null when there is none. */ +async function projectAbove($: EngineInterface, dir: string): Promise { + const seen: string[] = [] + let found: string | null = null + let at = dir + for (let i = 0; at && i < 40; i++) { + if (projects.has(at)) { + found = projects.get(at) ?? null + break + } + seen.push(at) + if (await $.fs.exists(`${at}/dbt_project.yml`).catch(() => false)) { + found = at + break + } + const parent = at.replace(/[\\/][^\\/]*$/, '') + if (parent === at) break + at = parent + } + for (const d of seen) projects.set(d, found) + return found +} + +/** Whether a changed file is one a model check can be about: a .sql or .yml in a dbt project, not dbt's own files. */ +async function isModelFile($: EngineInterface, file: string): Promise { + const path = norm(file) + if (!/\.(sql|ya?ml)$/i.test(path) || SKIPPED_DIRS.test(path) || ROOT_FILES.has(path.split('/').pop() ?? '')) return false + return (await projectAbove($, path.replace(/\/[^/]*$/, ''))) !== null +} + /** Asks altimate-code's dbt_health for the project's insights. */ async function check($: EngineInterface): Promise { const until = (await $.clock.now()) + CHECK_MS @@ -102,67 +267,95 @@ async function check($: EngineInterface): Promise { } catch (error) { return { problem: `Project health could not run: ${String(error).split('\n')[0]}` } } + // A refusal or failure comes back as success false with an empty list: only success true is an answer. const insights = result.metadata?.insights - if (result.ok && Array.isArray(insights)) return { insights: insights as HealthInsight[] } + if (result.ok && result.metadata?.success === true && Array.isArray(insights)) return { insights: insights as HealthInsight[] } if (/unknown tool/i.test(result.text)) return { problem: NEEDS_CLI } return { problem: `Project health: ${(result.text || 'no answer').split('\n')[0]}` } } -/** Whether the status line carries the health part now. */ -let drawn = false - -async function drawStatus($: EngineInterface, state: HealthState | null) { - const n = state?.issues.length ?? 0 +async function drawStatus($: EngineInterface, issues: readonly HealthInsight[]) { + const n = issues.length if (n === 0 && !drawn) return drawn = n > 0 await setStatusPart($, 'health', n > 0 ? `${n} health issue${n === 1 ? '' : 's'}` : null) } -/** The baseline run: its pairs are what the project already had. */ -async function startBaseline($: EngineInterface, mine: number) { - const run = await check($) - if (mine !== generation) return - const state = await update($, healthAtom, prev => { - const base = prev ?? fresh() - return 'problem' in run ? { ...base, problem: run.problem } : { ...base, baseline: run.insights.map(pairKey), problem: null } - }) - void state +/** Tells a problem once, until a different one comes. */ +function tell($: EngineInterface, problem: string) { + if (problem === toldProblem) return + toldProblem = problem + $.ui.toast(`◆ ${problem}`, { timeoutMs: 8000 }) } -/** Runs the check over the changes so far; returns the issues, or why it could not. */ -async function recheck($: EngineInterface, mine: number): Promise<{ issues: HealthInsight[] } | { problem: string } | null> { - const before = await read($, healthAtom) - if (!before?.baseline || before.running) return before?.problem ? { problem: before.problem } : null - await update($, healthAtom, prev => ({ ...(prev ?? fresh()), running: true })) - const run = await check($) - if (mine !== generation) return null - const touched = new Set() - let result: { issues: HealthInsight[] } | { problem: string } - if ('problem' in run) { - result = { problem: run.problem } - } else { - const current = await read($, healthAtom) - for (const file of current?.touched ?? []) { - for (const id of modelsOfFile(file, run.insights)) touched.add(id) - } - // A changed properties file names its models; one that no insight reports has no insights to match. - const names = new Set() - for (const file of current?.touched ?? []) { - if (/\.ya?ml$/i.test(file)) { - const text = await $.fs.read(file).catch(() => '') - for (const name of modelNamesInYaml(text)) names.add(name) - } +/** + * The checks the state asks for, one after another: the baseline when there is none, then the changed + * models. Edits that land during a check leave `pending` set, which starts the next. A session + * restart bumps `generation`, and an older loop stops without writing. + */ +async function loop($: EngineInterface, mine: number) { + for (;;) { + const before = await read($, healthAtom) + if (mine !== generation || !before) return + const needsBaseline = before.baseline === null + if (!needsBaseline && before.pending.length === 0) return + // The baseline run leaves `pending` alone: edits made while it ran are checked by the next run. + const batch = needsBaseline ? [] : before.pending + await update($, healthAtom, prev => ({ ...(prev ?? fresh()), pending: needsBaseline ? (prev?.pending ?? []) : (prev?.pending ?? []).filter(f => !batch.includes(f)), running: true })) + const run = await check($) + if (mine !== generation) return + if ('problem' in run) { + // The issues found so far stay until a check says otherwise, and the changes it did not cover are asked for again. + await update($, healthAtom, prev => { + const base = prev ?? fresh() + return { ...base, running: false, problem: run.problem, pending: [...new Set([...batch, ...base.pending])] } + }) + tell($, run.problem) + // Not asked for again at once: the next change, /health or push asks. + return } - for (const i of run.insights) if (names.has(shortName(i.uniqueId))) touched.add(i.uniqueId) - result = { issues: newIssues(run.insights, current?.baseline ?? [], touched) } + await update($, healthAtom, prev => { + const base = prev ?? fresh() + const baseline = base.baseline ?? run.insights.map(pairKey) + const issues = newIssues(run.insights, baseline, modelsOfFiles(base.touched, run.insights)) + return { ...base, baseline, issues, running: false, problem: null } + }) + toldProblem = null + const issues = (await read($, healthAtom))?.issues ?? [] + await drawStatus($, issues) + if (!needsBaseline && issues.length) $.ui.toast(`◆ ${bandText(issues)}`, { timeoutMs: 8000 }) } - const next = await update($, healthAtom, prev => { - const base = prev ?? fresh() - return 'problem' in result ? { ...base, running: false, problem: result.problem } : { ...base, running: false, problem: null, issues: result.issues } - }) - void next - await drawStatus($, await read($, healthAtom)) - return result +} + +/** Starts the check loop unless one runs; resolves when the loop that is running ends. */ +function kick($: EngineInterface): Promise { + if (running && running.generation === generation) return running.done + const mine = generation + const entry: { generation: number; done: Promise } = { + generation: mine, + done: loop($, mine) + .catch(error => { + try { + $.ui.log(`health check failed: ${String(error)}`, { to: 'debug' }) + } catch {} + }) + .then(async () => { + if (running === entry) running = null + if (mine === generation) await update($, healthAtom, prev => (prev?.running ? { ...prev, running: false } : prev)).catch(() => {}) + }), + } + running = entry + return entry.done +} + +/** + * Whether the state wants a check: changes not yet checked, or no baseline yet. After a failed check it is + * asked for again only by a new change or by `retry` (the person asking with /health), not at every turn. + */ +async function wantsCheck($: EngineInterface, retry = false): Promise { + const state = await read($, healthAtom) + if (!enabled || !state) return false + return (retry || !state.problem) && (state.pending.length > 0 || state.baseline === null) } /** Adds this feature's own hooks. */ @@ -170,38 +363,36 @@ export function register(on: On, context: FeatureContext) { options = context.options on('command.run', { command: COMMAND }, async $ => { const state = await read($, healthAtom) - if (!state?.baseline && !state?.running) return { text: state?.problem ?? 'Project health has not finished its first check yet.' } - const mine = generation - const result = await recheck($, mine) - if (!result) return { text: 'Project health is still checking.' } - if ('problem' in result) return { text: result.problem } - return { text: result.issues.length ? `${bandText(result.issues)}\n${reportText(result.issues)}` : 'No new health issues in the models changed this session.' } + if (!state || !enabled) return { text: 'Project health checks dbt projects: this session has none open yet.' } + // Asking again retries a first check that failed, and checks changes not yet checked. + if (await wantsCheck($, true)) { + const done = kick($) + await within($, COMMAND_WAIT_MS, done) + } + const now = await read($, healthAtom) + if (!now) return { text: 'Project health is not set up in this session yet.' } + if (now.problem) return { text: now.problem } + if (now.baseline === null || now.running) return { text: 'Project health is still checking: ask again in a minute.' } + return { text: now.issues.length ? `${bandText(now.issues)}\n${reportText(now.issues)}` : 'No new health issues in the models changed this session.' } }) } -/** Whether the session's folder, or one above it, holds a dbt project. */ -async function inDbtProject($: EngineInterface): Promise { - let dir = await $.session.root().catch(() => '') - for (let i = 0; dir && i < 8; i++) { - if (await $.fs.exists(`${dir}/dbt_project.yml`).catch(() => false)) return true - const parent = dir.replace(/[\\/][^\\/]*$/, '') - if (parent === dir) break - dir = parent - } - return false -} - export async function sessionStart($: EngineInterface, e: Args<'session.start'>) { generation += 1 + running = null + enabled = false + toldProblem = null + projects.clear() await update($, healthAtom, () => fresh()) - if (drawn) await drawStatus($, null) + if (drawn) await drawStatus($, []) await $.command.register({ name: COMMAND, description: 'Show the project health issues Claude introduced' }) - // The baseline takes a while (the dbt adapter starts): nothing waits for it. A `-p` run or the SDK has - // nobody to show it to. - if (e.isInteractive && (await inDbtProject($))) { - const mine = generation - later($, () => startBaseline($, mine)) - } + // The baseline takes a while (the dbt adapter starts): nothing waits for it. A session with nobody at + // the prompt checks only when the push gate needs it. + eligible = e.isInteractive || options.healthBlocksPush === true || options.healthBlocksPush === 'true' + const root = await $.session.root().catch(() => '') + if (!eligible || !root || !(await projectAbove($, root))) return + enabled = true + later($, () => kick($)) } export async function turnStart($: EngineInterface, e: Args<'turn.start'>) {} @@ -209,24 +400,13 @@ export async function turnStart($: EngineInterface, e: Args<'turn.start'>) {} /** After the turn's answer: when model files changed, check again. */ export async function turnComplete($: EngineInterface, e: Args<'turn.complete'>, out: EventResult<'turn.complete'>) { if ((e as { agentId?: string }).agentId) return - const state = await read($, healthAtom) - if (!state || state.pending.length === 0) return - await update($, healthAtom, prev => ({ ...(prev ?? fresh()), pending: [] })) - const mine = generation - later($, async () => { - const result = await recheck($, mine) - if (!result || mine !== generation) return - if ('problem' in result) { - // Said once, not at every turn. - if (result.problem !== state.problem) $.ui.toast(`◆ ${result.problem}`, { timeoutMs: 8000 }) - return - } - if (result.issues.length) $.ui.toast(`◆ ${bandText(result.issues)}`, { timeoutMs: 8000 }) - }) + if (await wantsCheck($)) later($, () => kick($)) } export async function sessionEnd($: EngineInterface, e: Args<'session.end'>) { generation += 1 + running = null + enabled = false } /** This feature's line in the band, under the lineage line; null when there is nothing to say. */ @@ -249,28 +429,47 @@ export function pushRefusal(blocks: unknown, command: string, issues: readonly H } } -/** With `healthBlocksPush` on, `git push` is refused while the models changed have new health issues. */ +/** + * With `healthBlocksPush` on, `git push` waits for the check of the changes so far and is refused while + * they have new health issues. A check that cannot run does not hold the push: it is told once, and + * /health shows it. + */ export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> { - if (!isGitPush(e.command)) return null + if (!(options.healthBlocksPush === true || options.healthBlocksPush === 'true') || !isGitPush(e.command)) return null + if ((await wantsCheck($)) || running?.generation === generation) await within($, CHECK_MS, kick($)) const state = await read($, healthAtom) return pushRefusal(options.healthBlocksPush, e.command, state?.issues ?? []) } -export async function bashAfter($: EngineInterface, e: BashCall, result: ToolCallResult): Promise {} +/** A model file Bash writes (a `sed -i`, a redirect, a `git checkout -- file`) is checked like one Claude edited. */ +export async function bashAfter($: EngineInterface, e: BashCall, result: ToolCallResult): Promise { + const root = await $.session.root().catch(() => '') + for (const name of filesWrittenBy(e.command)) await markChanged($, name.startsWith('/') || /^[A-Za-z]:/.test(name) ? name : `${root}/${name}`) +} -/** A model file Claude changed is checked at the end of the turn. */ -export async function editAfter($: EngineInterface, e: FileCall, result: ToolCallResult, file: EditedFile): Promise { - if (!/\.(sql|ya?ml)$/i.test(file.path)) return +async function markChanged($: EngineInterface, file: string) { + const path = norm(file) + if (!(await isModelFile($, path))) return + // A session that opened outside a dbt project checks from its first change to one. + if (eligible && !enabled) enabled = true await update($, healthAtom, prev => { const base = prev ?? fresh() return { ...base, - pending: base.pending.includes(file.path) ? base.pending : [...base.pending, file.path], - touched: base.touched.includes(file.path) ? base.touched : [...base.touched, file.path], + // A new change asks for a check again after a failed one. + problem: null, + pending: base.pending.includes(path) ? base.pending : [...base.pending, path], + touched: base.touched.includes(path) ? base.touched : [...base.touched, path], } }) } +/** A model file Claude changed is checked at the end of the turn. */ +export async function editAfter($: EngineInterface, e: FileCall, result: ToolCallResult, file: EditedFile): Promise { + const root = await $.session.root().catch(() => '') + await markChanged($, file.path.startsWith('/') || /^[A-Za-z]:/.test(file.path) ? file.path : `${root}/${file.path}`) +} + export async function readOrEditAfter($: EngineInterface, e: FileCall, result: ToolCallResult, file: { path: string }): Promise {} /** Whether `editAfter` needs the file's text from before the edit (one extra read per edit). */ diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index d05d5021..f3f48992 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -1,9 +1,10 @@ import { expect, mock, test } from 'claude-code/testing' -import { bandText, isGitPush, modelNamesInYaml, modelsOfFile, newIssues, pairKey, pushRefusal, reportText, toolArgs } from './features/health' +import { bandText, filesWrittenBy, isGitPush, modelsOfFiles, newIssues, pairKey, pushRefusal, reportText, simpleCommands, toolArgs } from './features/health' const ROOT = '/repo' const STG = `${ROOT}/models/staging/stg_orders.sql` +const STG_YML = `${ROOT}/models/staging/_staging.yml` const FCT = `${ROOT}/models/marts/fct_orders.sql` const NOTES = `${ROOT}/README.md` @@ -13,31 +14,36 @@ const BAND = { props: { hasSurvey: false, isCollapsed: false, isWorking: false, maxRows: 10, bodyColumns: 120, scroll: { offset: 0, bodyRows: 10 }, view: {} }, } as const -const insight = (id: string, path: string, name: string, message = `${name} found`) => ({ +const insight = (id: string, file: string, name: string, message = `${name} found`, properties = '') => ({ uniqueId: `model.shop.${id}`, - path, + path: file.replace(`${ROOT}/`, ''), name, severity: 'WARNING', message, recommendation: `Fix ${name}`, + file, + properties, }) // What the project already has before Claude changes anything. -const OLD_STG = insight('stg_orders', 'models/staging/stg_orders.sql', 'Missing documentation') -const OLD_FCT = insight('fct_orders', 'models/marts/fct_orders.sql', 'Missing tests') +const OLD_STG = insight('stg_orders', STG, 'Missing documentation', undefined, STG_YML) +const OLD_FCT = insight('fct_orders', FCT, 'Missing tests') // What a change adds. -const NEW_STG = insight('stg_orders', 'models/staging/stg_orders.sql', 'Hard coded references', 'stg_orders reads raw.orders directly') -const NEW_FCT = insight('fct_orders', 'models/marts/fct_orders.sql', 'Model fanout', 'fct_orders has 4 direct children') +const NEW_STG = insight('stg_orders', STG, 'Hard coded references', 'stg_orders reads raw.orders directly', STG_YML) +const NEW_FCT = insight('fct_orders', FCT, 'Model fanout', 'fct_orders has 4 direct children') +// A model of a dependency package: it has no files in the project. +const DEP = { ...insight('base', '', 'Missing tests'), uniqueId: 'model.dep.base', path: 'models/base.sql' } type Options = { - /** The tool's insights, per run (the last repeats). */ - runs: ReturnType[][] - /** What the CLI prints instead of an answer. */ - cliStdout?: string - yaml?: Record + /** The tool's answer per run (the last repeats). */ + runs: (ReturnType[] | { fail: string })[] + /** Holds the run with this index (from 0) until released. */ + hold?: { at: number; until: Promise } + /** Folders that hold a dbt_project.yml (default: the repo). */ + projects?: string[] } -async function start($: any, on: any, options: Options) { +async function start($: any, on: any, options: Options, session: { isInteractive?: boolean } = {}) { const clock = mock.clock(on) const calls: { tool: string; args: Record; argv: readonly string[] }[] = [] const toasts: string[] = [] @@ -60,22 +66,26 @@ async function start($: any, on: any, options: Options) { const { Box } = $$.ui.resolve(e) return }) - on('fs.exists', (_: unknown, e: { path: string }) => ({ value: e.path === `${ROOT}/dbt_project.yml` })) - on('fs.read', (_: unknown, e: { path: string }) => ({ value: options.yaml?.[e.path] ?? '' })) + const projects = options.projects ?? [ROOT] + on('fs.exists', (_: unknown, e: { path: string }) => ({ value: projects.some(p => e.path === `${p}/dbt_project.yml`) })) + on('fs.read', () => ({ value: '' })) on('tool.list', () => ({ value: [] })) on('mcp.connect', () => ({ value: { isConnected: false, reason: 'not_connected', message: 'off' } })) on('tool.call', () => ({ result: { ok: true } })) let run = 0 - on('process.run', (_: unknown, e: { argv: readonly string[] }) => { + on('process.run', async (_: unknown, e: { argv: readonly string[] }) => { const [, command, , tool = ''] = e.argv if (command !== 'tool') return { value: { exitCode: 1, stdout: '', stderr: 'not this test' } } const args = JSON.parse(String(e.argv[e.argv.indexOf('--args') + 1] ?? '{}')) + const index = run++ calls.push({ tool, args, argv: e.argv }) - if (options.cliStdout !== undefined) return { value: { exitCode: 0, stdout: options.cliStdout, stderr: '' } } - const insights = options.runs[Math.min(run++, options.runs.length - 1)] - return { value: { exitCode: 0, stdout: JSON.stringify({ status: 'ok', output: '', metadata: { success: true, insights } }), stderr: '' } } + if (options.hold?.at === index) await options.hold.until + const answer = options.runs[Math.min(index, options.runs.length - 1)]! + const metadata = 'fail' in answer ? { success: false, insights: [] } : { success: true, insights: answer } + const output = 'fail' in answer ? answer.fail : '' + return { value: { exitCode: 0, stdout: JSON.stringify({ status: 'ok', output, metadata }), stderr: '' } } }) - await $.session.start({ cwd: ROOT, surface: 'terminal', isInteractive: true }) + await $.session.start({ cwd: ROOT, surface: 'terminal', isInteractive: session.isInteractive ?? true }) await clock.settle() return { clock, calls, toasts, statuses } } @@ -86,6 +96,7 @@ async function endTurn($: any, clock: any, id: string) { } const edit = (file: string) => ({ tool: 'Edit' as const, file_path: file, old_string: 'a', new_string: 'b' }) +const command = (text: string) => ({ tool: 'Bash' as const, command: text }) as any test('a baseline run at session start; a changed model with a new insight shows in the band, not the ones the project already had', async ($, on) => { const { clock, calls, toasts, statuses } = await start($, on, { runs: [[OLD_STG, OLD_FCT], [OLD_STG, OLD_FCT, NEW_STG, NEW_FCT]] }) @@ -115,15 +126,20 @@ test('two changed models give "2 new health issues" with the first model named', await band.unmount() }) -test('a turn that changed no model file runs no check; other files are ignored', async ($, on) => { +test('README, dbt_project.yml, profiles, CI and dbt_packages files are not model files: no check runs', async ($, on) => { const { clock, calls, toasts } = await start($, on, { runs: [[OLD_STG], [OLD_STG, NEW_STG]] }) - await $.tool.call(edit(NOTES)) + for (const file of [NOTES, `${ROOT}/dbt_project.yml`, `${ROOT}/profiles.yml`, `${ROOT}/.github/workflows/ci.yml`, `${ROOT}/dbt_packages/dep/models/x.sql`, `${ROOT}/target/compiled/x.sql`]) + await $.tool.call(edit(file)) await endTurn($, clock, 't1') expect(calls).toHaveLength(1) expect(toasts).toEqual([]) - const band = await $.ui.mount({ ...BAND, surface: 'terminal' }) - expect(await band.find({ type: 'Text', text: /health issue/ })).toBeUndefined() - await band.unmount() +}) + +test('a .sql file outside any dbt project is not a model file', async ($, on) => { + const { clock, calls } = await start($, on, { runs: [[OLD_STG]] }) + await $.tool.call(edit('/elsewhere/query.sql')) + await endTurn($, clock, 't1') + expect(calls).toHaveLength(1) }) test('an insight the baseline already had is not new', async ($, on) => { @@ -133,17 +149,150 @@ test('an insight the baseline already had is not new', async ($, on) => { expect(toasts).toEqual([]) }) -test('a changed properties file checks the models it lists', async ($, on) => { - const yml = `${ROOT}/models/staging/_staging.yml` - const { clock, toasts } = await start($, on, { - runs: [[], [NEW_STG]], - yaml: { [yml]: 'version: 2\nmodels:\n - name: stg_orders\n description: orders\n' }, - }) - await $.tool.call(edit(yml)) +test('a changed properties file checks the models it documents, and a dependency package model is never a changed model', async ($, on) => { + const { clock, toasts } = await start($, on, { runs: [[], [NEW_STG, DEP]] }) + await $.tool.call(edit(STG_YML)) + await endTurn($, clock, 't1') + expect(toasts).toEqual(['◆ 1 new health issue · stg_orders: hard coded references · /health']) +}) + +test('a model file Bash writes counts like one Claude edited; a command naming no model file starts no check', async ($, on) => { + const { clock, calls, toasts } = await start($, on, { runs: [[], [NEW_STG]] }) + await $.tool.call(command('ls -la && git status')) + await endTurn($, clock, 't0') + expect(calls).toHaveLength(1) + await $.tool.call(command(`sed -i 's/a/b/' ${STG}`)) await endTurn($, clock, 't1') + expect(calls).toHaveLength(2) expect(toasts).toEqual(['◆ 1 new health issue · stg_orders: hard coded references · /health']) }) +test('edits made while a check runs are checked by the next one, not dropped', async ($, on) => { + let release!: () => void + const until = new Promise(resolve => (release = resolve)) + // Run 0 is the baseline; run 1 (turn 1's check) is held; run 2 follows for turn 2's edit. + const { clock, calls, toasts } = await start($, on, { runs: [[], [], [NEW_FCT]], hold: { at: 1, until } }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + expect(calls).toHaveLength(2) + + await $.tool.call(edit(FCT)) + await endTurn($, clock, 't2') + // Turn 1's check is still running: no second run starts beside it. + expect(calls).toHaveLength(2) + + release() + await clock.settle() + expect(calls).toHaveLength(3) + expect(toasts).toEqual(['◆ 1 new health issue · fct_orders: model fanout · /health']) +}) + +test('edits made before the baseline lands are checked once it has', async ($, on) => { + let release!: () => void + const until = new Promise(resolve => (release = resolve)) + const { clock, calls, toasts } = await start($, on, { runs: [[], [NEW_STG]], hold: { at: 0, until } }) + expect(calls).toHaveLength(1) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + // The baseline is still running: nothing else starts beside it. + expect(calls).toHaveLength(1) + release() + await clock.settle() + expect(calls).toHaveLength(2) + expect(toasts).toEqual(['◆ 1 new health issue · stg_orders: hard coded references · /health']) +}) + +test('a failed check is not read as a clean one: it is told, and the next change asks again', async ($, on) => { + const { clock, calls, toasts } = await start($, on, { + runs: [{ fail: "DataPilot isn't installed in python3. Install the `altimate-datapilot-cli` package" }, [OLD_STG], [OLD_STG, NEW_STG]], + }) + expect(toasts).toEqual(["◆ Project health: DataPilot isn't installed in python3. Install the `altimate-datapilot-cli` package"]) + + // The baseline failed: a change asks for it again, which now answers; the edit is checked after it. + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + expect(calls).toHaveLength(3) + expect(toasts).toHaveLength(2) + expect(toasts[1]).toBe('◆ 1 new health issue · stg_orders: hard coded references · /health') +}) + +test('the same problem at every turn is told once', async ($, on) => { + const { clock, toasts } = await start($, on, { runs: [{ fail: "the dbt adapter didn't start" }] }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + await $.tool.call(edit(FCT)) + await endTurn($, clock, 't2') + expect(toasts).toEqual(["◆ Project health: the dbt adapter didn't start"]) +}) + +test('/health shows the issues Claude introduced, and retries a first check that failed', async ($, on) => { + const { clock, calls } = await start($, on, { runs: [{ fail: "the dbt adapter didn't start" }, [OLD_STG], [OLD_STG, NEW_STG]] }) + const retried: any = await $.command.run({ command: 'health' } as any) + await clock.settle() + // Asking again ran the baseline again; it answered, and nothing has changed yet. + expect(calls).toHaveLength(2) + expect(String(retried?.text)).toContain('No new health issues') + + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + const shown: any = await $.command.run({ command: 'health' } as any) + expect(String(shown?.text)).toContain('1 new health issue · stg_orders: hard coded references') + expect(String(shown?.text)).toContain('stg_orders reads raw.orders directly') +}) + +test('a session in no dbt project starts no check at session start, at a turn, at /health or at a push', async ($, on) => { + const { clock, calls, toasts } = await start($, on, { runs: [[OLD_STG]], projects: [] }) + expect(calls).toHaveLength(0) + await endTurn($, clock, 't1') + const shown: any = await $.command.run({ command: 'health' } as any) + await $.tool.call(command('git push')) + await clock.settle() + expect(calls).toHaveLength(0) + expect(toasts).toEqual([]) + expect(String(shown?.text)).toContain('none open') +}) + +test('a session with nobody at the prompt starts no check while the push gate is off, at session start or at a turn', async ($, on) => { + const { clock, calls } = await start($, on, { runs: [[OLD_STG]] }, { isInteractive: false }) + await endTurn($, clock, 't1') + expect(calls).toHaveLength(0) +}) + +test('a baseline that failed is not asked for again at every turn, only by a change or /health', async ($, on) => { + const { clock, calls } = await start($, on, { runs: [{ fail: "the dbt adapter didn't start" }, [OLD_STG]] }) + expect(calls).toHaveLength(1) + await endTurn($, clock, 't1') + await endTurn($, clock, 't2') + expect(calls).toHaveLength(1) + await $.command.run({ command: 'health' } as any) + await clock.settle() + expect(calls).toHaveLength(2) +}) + +test('a recheck that fails keeps the issues found before, and the change it did not cover is asked for again by the next change', async ($, on) => { + const { clock, calls, toasts } = await start($, on, { runs: [[], [NEW_STG], { fail: 'dbt timed out' }, [NEW_STG]] }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + const band = await $.ui.mount({ ...BAND, surface: 'terminal' }) + expect(await band.find({ type: 'Text', text: /1 new health issue/ })).toBeDefined() + await band.unmount() + + await $.tool.call(edit(STG)) + await endTurn($, clock, 't2') + expect(calls).toHaveLength(3) + expect(toasts.at(-1)).toBe('◆ Project health: dbt timed out') + // Not asked for again at the next turn's end without a new change. + await endTurn($, clock, 't3') + expect(calls).toHaveLength(3) + const again = await $.ui.mount({ ...BAND, surface: 'terminal' }) + expect(await again.find({ type: 'Text', text: /1 new health issue/ })).toBeDefined() + await again.unmount() + + await $.tool.call(edit(STG)) + await endTurn($, clock, 't4') + expect(calls).toHaveLength(4) +}) + test('git push goes through by default (the setting is off), with new issues open', async ($, on) => { const { clock } = await start($, on, { runs: [[], [NEW_STG]] }) await $.tool.call(edit(STG)) @@ -151,7 +300,7 @@ test('git push goes through by default (the setting is off), with new issues ope const band = await $.ui.mount({ ...BAND, surface: 'terminal' }) expect(await band.find({ type: 'Text', text: /1 new health issue/ })).toBeDefined() await band.unmount() - const allowed: any = await $.tool.call({ tool: 'Bash', command: 'git push origin main' } as any) + const allowed: any = await $.tool.call(command('git push origin main')) expect(allowed?.deny).toBeUndefined() }) @@ -174,34 +323,79 @@ test('the configured config path is passed to the tool, empty means the defaults expect(toolArgs(undefined)).toEqual({}) }) -test('a CLI without the tool, or a tool that refuses, is said once and never blocks a push', async ($, on) => { - const { clock, toasts } = await start($, on, { - runs: [[]], - cliStdout: JSON.stringify({ status: 'error', output: 'DataPilot isn\'t installed in python3. Install the `altimate-datapilot-cli` package', metadata: { success: false, insights: [] } }), - }) - await $.tool.call(edit(STG)) - await endTurn($, clock, 't1') - await $.tool.call(edit(STG)) - await endTurn($, clock, 't2') - expect(toasts).toEqual([]) - const push: any = await $.tool.call({ tool: 'Bash', command: 'git push' } as any) - expect(push?.deny).toBeUndefined() +test('git push is found by its command words, not by its text', () => { + for (const line of [ + 'git push', + 'git push origin main', + 'git -C sub push origin x', + 'git -C "my project" push', + 'git --git-dir .git push', + 'git --git-dir=.git push', + 'GIT_SSH=ssh git push --force', + 'git add . && git push', + 'git add .; git push', + 'sudo git push', + 'command git push', + 'env X=1 git push', + 'time git push', + '/usr/bin/git push', + '(git push)', + '{ git push; }', + 'git push&', + 'bash -c "git push"', + "sh -c 'git add . && git push'", + 'echo $(git push)', + 'git commit -m x\ngit push', + 'git \\\npush', + 'bash -lc "git push"', + 'sudo -u bob git push', + 'nice -n 10 git push', + 'env -u X git push', + 'echo "$(git push)"', + ]) + expect([line, isGitPush(line)]).toEqual([line, true]) + for (const line of [ + 'git status', + 'git pushd', + 'git commit -m "fix; git push later"', + 'echo git push', + 'echo "example; git push origin main"', + "echo 'git push'", + 'git log --oneline -- push', + 'grep -r push .', + ]) + expect([line, isGitPush(line)]).toEqual([line, false]) }) -test('the pure helpers', () => { - expect(isGitPush('git push')).toBe(true) - expect(isGitPush('git -C sub push origin x')).toBe(true) - expect(isGitPush('GIT_SSH=ssh git push --force')).toBe(true) - expect(isGitPush('git add . && git push')).toBe(true) - expect(isGitPush('git pushd')).toBe(false) - expect(isGitPush('echo git push')).toBe(false) - expect(isGitPush('git status')).toBe(false) +test('simple commands split on operators outside quotes, and an unclosed quote still ends', () => { + expect(simpleCommands('a b && c "d e"; f | g')).toEqual([['a', 'b'], ['c', 'd e'], ['f'], ['g']]) + expect(simpleCommands("a 'b c")).toEqual([['a', 'b c']]) +}) +test('the model files a Bash command writes, not the ones it only reads', () => { + expect(filesWrittenBy(`sed -i 's/a/b/' models/x.sql && cat > models/y.yml`)).toEqual(['models/x.sql', 'models/y.yml']) + expect(filesWrittenBy('echo hi >models/z.sql')).toEqual(['models/z.sql']) + expect(filesWrittenBy('echo hi >> models/z.sql; mv models/a.sql models/b.sql')).toEqual(['models/z.sql', 'models/a.sql', 'models/b.sql']) + expect(filesWrittenBy('printf x | tee models/t.sql')).toEqual(['models/t.sql']) + expect(filesWrittenBy('git checkout -- models/c.sql')).toEqual(['models/c.sql']) + expect(filesWrittenBy('perl -pi -e s/a/b/ models/p.sql')).toEqual(['models/p.sql']) + for (const line of ['cat models/x.sql', 'git diff models/x.sql', 'grep foo models/x.yml', 'sed -n 1,5p models/x.sql', 'ls -la', 'git status']) + expect([line, filesWrittenBy(line)]).toEqual([line, []]) +}) + +test('models match by their own files, so a model of another package is never matched', () => { + expect([...modelsOfFiles([STG], [OLD_STG, OLD_FCT, DEP])]).toEqual(['model.shop.stg_orders']) + expect([...modelsOfFiles([STG_YML], [OLD_STG, OLD_FCT])]).toEqual(['model.shop.stg_orders']) + expect([...modelsOfFiles(['/elsewhere/other.sql'], [OLD_STG, DEP])]).toEqual([]) +}) + +test('new issues are the pairs the baseline lacks, on the given models', () => { expect(pairKey(NEW_STG)).toBe('model.shop.stg_orders\u0000Hard coded references') - expect([...modelsOfFile(STG, [OLD_STG, OLD_FCT])]).toEqual(['model.shop.stg_orders']) - expect([...modelsOfFile('/elsewhere/other.sql', [OLD_STG])]).toEqual([]) expect(newIssues([OLD_STG, NEW_STG, NEW_FCT], [pairKey(OLD_STG)], new Set(['model.shop.stg_orders']))).toEqual([NEW_STG]) - expect(modelNamesInYaml('version: 2\nmodels:\n - name: a\n - name: "b"\nsources:\n - name: c\n')).toEqual(['a', 'b']) +}) + +test('the band line and the report', () => { expect(bandText([NEW_STG])).toBe('1 new health issue · stg_orders: hard coded references · /health') + expect(bandText([])).toBe('') expect(reportText([NEW_STG])).toBe('- stg_orders: hard coded references (warning) stg_orders reads raw.orders directly → Fix Hard coded references') }) diff --git a/plugins/altimate-code/types/index.d.ts b/plugins/altimate-code/types/index.d.ts index fd241431..952beeb3 100644 --- a/plugins/altimate-code/types/index.d.ts +++ b/plugins/altimate-code/types/index.d.ts @@ -210,6 +210,19 @@ export interface CostState {} // Project health: the health check issues on the changed models. // Filled in by that feature alone (hooks/features/); the other features never read or write it. // --------------------------------------------------------------------------------------------------- +/** One DataPilot insight on a model, as altimate-code's dbt_health returns it. */ +export interface HealthInsight { + uniqueId: string + path: string + name: string + severity: string + message: string + recommendation: string + /** The model's SQL file and its properties file, absolute; empty when the model is not the project's own. */ + file: string + properties: string +} + export interface HealthState { /** `uniqueId\u0000insight name` of every insight the project had at session start; null until that run lands. */ baseline: string[] | null @@ -218,7 +231,7 @@ export interface HealthState { /** Every model file changed this session. */ touched: string[] /** What the last check found that the baseline lacked, on the touched models. */ - issues: Array<{ uniqueId: string; path: string; name: string; severity: string; message: string; recommendation: string }> + issues: HealthInsight[] /** Why the check can't run, shown once per distinct reason. */ problem: string | null running: boolean From fd340eb800a8c4ee48ec84b18a07128ceb4c1593 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 21:44:34 +0530 Subject: [PATCH 03/34] fix: project health push gate holds on an unfinished or failed check, reads edits spelled any way, and sees redirects, bash -c, cd and heredocs Co-Authored-By: Claude Sonnet 5.5 --- .../altimate-code/hooks/features/health.tsx | 149 +++++++++++++++--- plugins/altimate-code/hooks/health.test.tsx | 87 +++++++++- 2 files changed, 211 insertions(+), 25 deletions(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index bdbcb110..094e8e46 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -54,7 +54,17 @@ const projects = new Map() export const pairKey = (i: Pick) => `${i.uniqueId}\u0000${i.name}` -const norm = (p: string) => p.replace(/\\/g, '/') +/** A path with `/` separators and no `.`, `..` or empty parts, so two spellings of one file compare equal. */ +const norm = (p: string) => { + const absolute = p.startsWith('/') || p.startsWith('\\') + const parts: string[] = [] + for (const part of p.replace(/\\/g, '/').split('/')) { + if (part === '' || part === '.') continue + if (part === '..' && parts.length && parts[parts.length - 1] !== '..' && !/^[A-Za-z]:$/.test(parts[parts.length - 1]!)) parts.pop() + else if (part !== '..' || !absolute) parts.push(part) + } + return (absolute ? '/' : '') + parts.join('/') +} /** The models whose SQL file or properties file is one of `files`. */ export function modelsOfFiles(files: readonly string[], insights: readonly HealthInsight[]): Set { @@ -100,10 +110,34 @@ const LEADER_VALUE_OPTIONS: Record = { const GIT_VALUE_OPTIONS = new Set(['-C', '-c', '--git-dir', '--work-tree', '--namespace', '--exec-path', '--super-prefix', '--config-env']) const SHELLS = new Set(['bash', 'sh', 'zsh', 'dash', 'ksh']) -/** The simple commands of a shell line, each as its words with quotes removed; a quoted word stays one word. */ +/** The inner text of each `$(...)` and backtick substitution in `text`. */ +function substitutionsIn(text: string): string[] { + const out: string[] = [] + for (let i = 0; i < text.length; i++) { + if (text[i] === '\\') i++ + else if (text[i] === '$' && text[i + 1] === '(') { + let depth = 0 + let j = i + 1 + for (; j < text.length; j++) { + if (text[j] === '(') depth++ + else if (text[j] === ')' && --depth === 0) break + } + out.push(text.slice(i + 2, j)) + i = j + } else if (text[i] === '`') { + const close = text.indexOf('`', i + 1) + out.push(text.slice(i + 1, close < 0 ? text.length : close)) + i = close < 0 ? text.length : close + } + } + return out +} + +/** The simple commands of a shell line, each as its words with quotes removed; a quoted word stays one word. A redirect is its own word (`>`, `>>`, `2>`) before its target; a heredoc body is text, not commands. */ export function simpleCommands(line: string): string[][] { const commands: string[][] = [] const nested: string[][] = [] + const heredocs: { delimiter: string; quoted: boolean; strip: boolean }[] = [] let words: string[] = [] let word = '' let inWord = false @@ -128,15 +162,21 @@ export function simpleCommands(line: string): string[][] { inWord = true i++ for (; i < line.length && line[i] !== '"'; i++) { - if (line[i] === '$' && line[i + 1] === '(') { + if ((line[i] === '$' && line[i + 1] === '(') || line[i] === '`') { // A substitution inside quotes still runs: its commands are commands of the line. - let depth = 0 - let j = i + 1 - for (; j < line.length; j++) { - if (line[j] === '(') depth++ - else if (line[j] === ')' && --depth === 0) break + let j = i + if (line[i] === '`') { + j = line.indexOf('`', i + 1) + if (j < 0) j = line.length + } else { + let depth = 0 + j = i + 1 + for (; j < line.length; j++) { + if (line[j] === '(') depth++ + else if (line[j] === ')' && --depth === 0) break + } } - nested.push(...simpleCommands(line.slice(i + 2, j))) + nested.push(...substitutionsIn(line.slice(i, j + 1)).flatMap(simpleCommands)) word += line.slice(i, j + 1) i = j continue @@ -152,6 +192,48 @@ export function simpleCommands(line: string): string[][] { } else if (c === '$' && line[i + 1] === '(') { endCommand() i++ + } else if (c === '<' && line[i + 1] === '<' && line[i + 2] !== '<') { + let j = i + 2 + const strip = line[j] === '-' + if (strip) j++ + while (line[j] === ' ' || line[j] === '\t') j++ + let delimiter = '' + let quoted = false + for (; j < line.length && !/[\s;&|()<>]/.test(line[j]!); j++) { + if (line[j] === "'" || line[j] === '"') { + quoted = true + const close = line.indexOf(line[j]!, j + 1) + delimiter += line.slice(j + 1, close < 0 ? line.length : close) + j = close < 0 ? line.length : close + } else if (line[j] === '\\') { + quoted = true + delimiter += line[++j] ?? '' + } else delimiter += line[j] + } + heredocs.push({ delimiter, quoted, strip }) + i = j - 1 + } else if (c === '\n' && heredocs.length) { + endCommand() + let at = i + 1 + for (const doc of heredocs.splice(0)) { + let body = '' + while (at < line.length) { + const eol = line.indexOf('\n', at) + const text = line.slice(at, eol < 0 ? line.length : eol) + at = eol < 0 ? line.length : eol + 1 + if ((doc.strip ? text.replace(/^\t+/, '') : text) === doc.delimiter) break + body += text + '\n' + } + // Only an unquoted delimiter lets the body's substitutions run. + if (!doc.quoted) nested.push(...substitutionsIn(body).flatMap(simpleCommands)) + } + i = at - 1 + } else if (c === '>') { + if (!(inWord && /^\d+$/.test(word))) endWord() + word += '>' + if (line[i + 1] === '>' || line[i + 1] === '&' || line[i + 1] === '|') word += line[++i] + inWord = true + endWord() } else if (c === '`' || OPERATORS.has(c)) { endCommand() } else if (c === ' ' || c === '\t' || c === '\r') { @@ -203,19 +285,25 @@ const WRITERS = new Set(['mv', 'cp', 'rm', 'tee', 'patch', 'touch', 'truncate', /** git subcommands that change files in the working tree. */ const GIT_WRITERS = new Set(['checkout', 'restore', 'apply', 'stash', 'reset', 'revert', 'merge', 'pull', 'rebase', 'cherry-pick', 'switch', 'mv', 'rm', 'am']) -/** The model-like file paths a Bash command line writes: redirects, `tee`, `sed -i`, `mv`, `git checkout -- file`. */ +/** The model-like file paths a Bash command line writes: redirects, `tee`, `sed -i`, `mv`, `git checkout -- file`; a relative path is given from the folder a `cd` before it moved to. */ export function filesWrittenBy(command: string): string[] { const out: string[] = [] + let cwd = '' + const from = (w: string) => (w.startsWith('/') || /^[A-Za-z]:/.test(w) || !cwd ? w : `${cwd}/${w}`) for (const words of simpleCommands(command)) { - const models = (list: readonly string[]) => list.filter(w => MODEL_FILE.test(w) && !w.startsWith('-')) + const models = (list: readonly string[]) => list.filter(w => MODEL_FILE.test(w) && !w.startsWith('-')).map(from) for (let i = 0; i < words.length; i++) { - const redirect = /^\d*>>?(.*)$/.exec(words[i]!) - if (redirect) out.push(...models([redirect[1] || words[i + 1] || ''])) + const redirect = /^\d*>[>|]?$/.exec(words[i]!) + if (redirect) out.push(...models([words[i + 1] ?? ''])) } const at = leaderEnd(words) const bin = (words[at] ?? '').split('/').pop() ?? '' const args = words.slice(at + 1) - if (WRITERS.has(bin)) out.push(...models(args)) + if (bin === 'cd') cwd = args[0] && !args[0].startsWith('-') && args[0] !== '~' ? from(args[0]) : '' + else if (SHELLS.has(bin)) { + const c = args.findIndex(w => /^-[a-z]*c$/.test(w)) + if (c >= 0 && c + 1 < args.length) out.push(...filesWrittenBy(args[c + 1]!).map(from)) + } else if (WRITERS.has(bin)) out.push(...models(args)) else if ((bin === 'sed' || bin === 'perl') && args.some(w => /^-[a-zA-Z]*i/.test(w) || w === '--in-place' || w.startsWith('--in-place='))) out.push(...models(args)) else if (bin === 'git' && args.some(w => GIT_WRITERS.has(w))) out.push(...models(args)) } @@ -254,8 +342,11 @@ async function projectAbove($: EngineInterface, dir: string): Promise { const path = norm(file) - if (!/\.(sql|ya?ml)$/i.test(path) || SKIPPED_DIRS.test(path) || ROOT_FILES.has(path.split('/').pop() ?? '')) return false - return (await projectAbove($, path.replace(/\/[^/]*$/, ''))) !== null + if (!/\.(sql|ya?ml)$/i.test(path) || SKIPPED_DIRS.test(path)) return false + const dir = path.replace(/\/[^/]*$/, '') + const project = await projectAbove($, dir) + // dbt's own files are the ones at the project root; a `profiles.yml` in a model folder is a properties file. + return project !== null && !(ROOT_FILES.has(path.split('/').pop() ?? '') && norm(project) === dir) } /** Asks altimate-code's dbt_health for the project's insights. */ @@ -269,7 +360,12 @@ async function check($: EngineInterface): Promise { } // A refusal or failure comes back as success false with an empty list: only success true is an answer. const insights = result.metadata?.insights - if (result.ok && result.metadata?.success === true && Array.isArray(insights)) return { insights: insights as HealthInsight[] } + if (result.ok && result.metadata?.success === true && Array.isArray(insights)) { + // Insights read from the last good manifest are not a check of the changes just made. + const stale = result.metadata?.stale_manifest as { message?: unknown } | undefined + if (stale) return { problem: `Project health: ${typeof stale.message === 'string' ? stale.message : 'dbt could not parse the project, so the last good manifest was read.'}` } + return { insights: insights as HealthInsight[] } + } if (/unknown tool/i.test(result.text)) return { problem: NEEDS_CLI } return { problem: `Project health: ${(result.text || 'no answer').split('\n')[0]}` } } @@ -316,7 +412,10 @@ async function loop($: EngineInterface, mine: number) { } await update($, healthAtom, prev => { const base = prev ?? fresh() - const baseline = base.baseline ?? run.insights.map(pairKey) + // A baseline that lands after edits (the first run failed, or the project was opened above the dbt + // project) cannot tell the touched models' old issues from the new: all of theirs count as new. + const edited = modelsOfFiles(base.touched, run.insights) + const baseline = base.baseline ?? run.insights.filter(i => !edited.has(i.uniqueId)).map(pairKey) const issues = newIssues(run.insights, baseline, modelsOfFiles(base.touched, run.insights)) return { ...base, baseline, issues, running: false, problem: null } }) @@ -432,13 +531,21 @@ export function pushRefusal(blocks: unknown, command: string, issues: readonly H /** * With `healthBlocksPush` on, `git push` waits for the check of the changes so far and is refused while - * they have new health issues. A check that cannot run does not hold the push: it is told once, and - * /health shows it. + * they have new health issues. A check that cannot run or does not finish holds it too. */ export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> { if (!(options.healthBlocksPush === true || options.healthBlocksPush === 'true') || !isGitPush(e.command)) return null - if ((await wantsCheck($)) || running?.generation === generation) await within($, CHECK_MS, kick($)) + const hold = (why: string) => ({ + deny: `git push is held: ${why}\nRun /${COMMAND} to check again, or turn off "Project health blocks git push" in the plugin's settings.`, + }) + // An edit in the same command would run before any check of it. + const root = await $.session.root().catch(() => '') + for (const name of filesWrittenBy(e.command)) if (await isModelFile($, name.startsWith('/') || /^[A-Za-z]:/.test(name) ? name : `${root}/${name}`)) return hold(`this command changes the model file ${name} and pushes in one go, so the change is not checked first. Run them as separate commands.`) + if ((await wantsCheck($, true)) || running?.generation === generation) await within($, CHECK_MS, kick($)) const state = await read($, healthAtom) + // A check that failed or did not finish leaves the changes unchecked: a gate that fails open is no gate. + if (state && enabled && (state.running || state.pending.length > 0 || (state.baseline === null && state.touched.length > 0))) + return hold(`the check of the models changed this session ${state.running ? 'did not finish in time' : `could not run (${state.problem ?? 'no answer'})`}.`) return pushRefusal(options.healthBlocksPush, e.command, state?.issues ?? []) } diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index f3f48992..fc736c0c 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -36,7 +36,7 @@ const DEP = { ...insight('base', '', 'Missing tests'), uniqueId: 'model.dep.base type Options = { /** The tool's answer per run (the last repeats). */ - runs: (ReturnType[] | { fail: string })[] + runs: (ReturnType[] | { fail: string } | { stale: string })[] /** Holds the run with this index (from 0) until released. */ hold?: { at: number; until: Promise } /** Folders that hold a dbt_project.yml (default: the repo). */ @@ -81,7 +81,8 @@ async function start($: any, on: any, options: Options, session: { isInteractive calls.push({ tool, args, argv: e.argv }) if (options.hold?.at === index) await options.hold.until const answer = options.runs[Math.min(index, options.runs.length - 1)]! - const metadata = 'fail' in answer ? { success: false, insights: [] } : { success: true, insights: answer } + const metadata = + 'fail' in answer ? { success: false, insights: [] } : 'stale' in answer ? { success: true, insights: [OLD_STG], stale_manifest: { message: answer.stale } } : { success: true, insights: answer } const output = 'fail' in answer ? answer.fail : '' return { value: { exitCode: 0, stdout: JSON.stringify({ status: 'ok', output, metadata }), stderr: '' } } }) @@ -208,12 +209,32 @@ test('a failed check is not read as a clean one: it is told, and the next change }) expect(toasts).toEqual(["◆ Project health: DataPilot isn't installed in python3. Install the `altimate-datapilot-cli` package"]) - // The baseline failed: a change asks for it again, which now answers; the edit is checked after it. + // The baseline failed: a change asks for it again, which now answers after the edit; the old issues of + // the edited model cannot be told from the new, so the next check reports all of them. await $.tool.call(edit(STG)) await endTurn($, clock, 't1') expect(calls).toHaveLength(3) expect(toasts).toHaveLength(2) - expect(toasts[1]).toBe('◆ 1 new health issue · stg_orders: hard coded references · /health') + expect(toasts[1]).toBe('◆ 2 new health issues · stg_orders: missing documentation · /health') +}) + +test('a model folder file named like a dbt root file is a properties file; a path spelled with ./ or .. is the same file', async ($, on) => { + const { clock, calls } = await start($, on, { runs: [[OLD_STG], [OLD_STG, NEW_STG]] }) + await $.tool.call(edit(`${ROOT}/models/staging/../staging/./profiles.yml`)) + await endTurn($, clock, 't1') + expect(calls).toHaveLength(2) + await $.tool.call(edit(`${ROOT}/profiles.yml`)) + await endTurn($, clock, 't2') + expect(calls).toHaveLength(2) +}) + +test('a relative spelling of the model file still matches the model the tool names', async ($, on) => { + const { clock } = await start($, on, { runs: [[], [NEW_STG]] }) + await $.tool.call(edit('./models/./staging/stg_orders.sql')) + await endTurn($, clock, 't1') + const band = await $.ui.mount({ ...BAND, surface: 'terminal' }) + expect(await band.find({ type: 'Text', text: /1 new health issue/ })).toBeDefined() + await band.unmount() }) test('the same problem at every turn is told once', async ($, on) => { @@ -316,6 +337,48 @@ test('with healthBlocksPush on, git push is refused while new issues exist and t expect(pushRefusal(true, 'git status', [NEW_STG])).toBeNull() }) +test('with the push gate on, a check that failed holds git push, and the push asks for it again', { options: { healthBlocksPush: true } }, async ($, on) => { + const { clock, calls } = await start($, on, { runs: [[], { fail: 'dbt could not parse' }, [NEW_STG]] }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + expect(calls).toHaveLength(2) + // The retry at the push runs the check again and finds the issue. + const denied: any = await $.tool.call(command('git push')) + expect(calls).toHaveLength(3) + expect(denied?.deny).toContain('1 new project health issue') +}) + +test('with the push gate on, a check that cannot run keeps holding git push, and says why', { options: { healthBlocksPush: true } }, async ($, on) => { + const { clock } = await start($, on, { runs: [[], { fail: 'dbt could not parse' }] }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + const denied: any = await $.tool.call(command('git push')) + expect(denied?.deny).toContain('could not run (Project health: dbt could not parse)') +}) + +test('with the push gate on, a push with no model changed this session goes through', { options: { healthBlocksPush: true } }, async ($, on) => { + await start($, on, { runs: [[OLD_STG]] }) + const allowed: any = await $.tool.call(command('git push')) + expect(allowed?.deny).toBeUndefined() +}) + +test('with the push gate on, a command that edits a model and pushes is held: the edit is not checked before it runs', { options: { healthBlocksPush: true } }, async ($, on) => { + await start($, on, { runs: [[]] }) + const denied: any = await $.tool.call(command(`sed -i 's/a/b/' models/staging/stg_orders.sql && git push`)) + expect(denied?.deny).toContain('Run them as separate commands') + const unrelated: any = await $.tool.call(command(`sed -i 's/a/b/' README.md && git push`)) + expect(unrelated?.deny).toBeUndefined() +}) + +test('with the push gate on, stale insights (dbt could not parse) are a failed check, not a clean one', { options: { healthBlocksPush: true } }, async ($, on) => { + const { clock, toasts } = await start($, on, { runs: [[], { stale: 'dbt could not parse the project' }] }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + expect(toasts.at(-1)).toContain('dbt could not parse the project') + const denied: any = await $.tool.call(command('git push')) + expect(denied?.deny).toContain('dbt could not parse the project') +}) + test('the configured config path is passed to the tool, empty means the defaults', () => { expect(toolArgs('datapilot.yml')).toEqual({ config_path: 'datapilot.yml' }) expect(toolArgs(' datapilot.yml ')).toEqual({ config_path: 'datapilot.yml' }) @@ -383,6 +446,22 @@ test('the model files a Bash command writes, not the ones it only reads', () => expect([line, filesWrittenBy(line)]).toEqual([line, []]) }) +test('shell syntax: a redirect without spaces, bash -c, cd before the edit, backticks in quotes, heredoc bodies', () => { + expect(filesWrittenBy('echo x>models/x.sql')).toEqual(['models/x.sql']) + expect(filesWrittenBy('echo x 2>models/e.sql')).toEqual(['models/e.sql']) + expect(filesWrittenBy(`bash -c "sed -i s/a/b/ models/x.sql"`)).toEqual(['models/x.sql']) + expect(filesWrittenBy('cd models && sed -i s/a/b/ x.sql')).toEqual(['models/x.sql']) + expect(filesWrittenBy('cd /repo/models/staging; echo x > y.sql')).toEqual(['/repo/models/staging/y.sql']) + expect(filesWrittenBy('echo x > /dev/null 2>&1')).toEqual([]) + expect(isGitPush('echo "`git push`"')).toBe(true) + expect(isGitPush('echo "$(git push)"')).toBe(true) + expect(isGitPush("cat <<'EOF'\ngit push\nEOF")).toBe(false) + expect(isGitPush('cat < { expect([...modelsOfFiles([STG], [OLD_STG, OLD_FCT, DEP])]).toEqual(['model.shop.stg_orders']) expect([...modelsOfFiles([STG_YML], [OLD_STG, OLD_FCT])]).toEqual(['model.shop.stg_orders']) From a21c1c8eb28d81c3a3ddd6cf0c1be09a994f9f1d Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 21:51:25 +0530 Subject: [PATCH 04/34] fix: push gate reads heredocs, here-strings and unbalanced substitutions as the shell does, and waits only when a model changed Co-Authored-By: Claude Sonnet 5.5 --- .../altimate-code/hooks/features/health.tsx | 135 +++++++++++------- plugins/altimate-code/hooks/health.test.tsx | 16 +++ 2 files changed, 101 insertions(+), 50 deletions(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 094e8e46..6f967850 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -110,18 +110,77 @@ const LEADER_VALUE_OPTIONS: Record = { const GIT_VALUE_OPTIONS = new Set(['-C', '-c', '--git-dir', '--work-tree', '--namespace', '--exec-path', '--super-prefix', '--config-env']) const SHELLS = new Set(['bash', 'sh', 'zsh', 'dash', 'ksh']) +/** Where the heredoc started by `<<` at `i` ends: its delimiter, whether it was quoted, and `<<` at `i` is one at all (not `<<<`, not a shift in `$((x<<2))`). */ +function heredocAt(text: string, i: number): { delimiter: string; quoted: boolean; strip: boolean; end: number } | null { + if (text[i] !== '<' || text[i + 1] !== '<' || text[i + 2] === '<' || text[i - 1] === '<') return null + let j = i + 2 + const strip = text[j] === '-' + if (strip) j++ + while (text[j] === ' ' || text[j] === '\t') j++ + if (!/[A-Za-z_'"\\]/.test(text[j] ?? '')) return null + let delimiter = '' + let quoted = false + for (; j < text.length && !/[\s;&|()<>]/.test(text[j]!); j++) { + if (text[j] === "'" || text[j] === '"') { + quoted = true + const close = text.indexOf(text[j]!, j + 1) + delimiter += text.slice(j + 1, close < 0 ? text.length : close) + j = close < 0 ? text.length : close + } else if (text[j] === '\\') { + quoted = true + delimiter += text[++j] ?? '' + } else delimiter += text[j] + } + return { delimiter, quoted, strip, end: j - 1 } +} + +/** The body of a heredoc whose line ends at `eol`: its text, and where the line after its delimiter line starts. */ +function heredocBody(text: string, eol: number, doc: { delimiter: string; strip: boolean }): { body: string; next: number } { + let at = eol + 1 + let body = '' + while (at < text.length) { + const end = text.indexOf('\n', at) + const line = text.slice(at, end < 0 ? text.length : end) + at = end < 0 ? text.length : end + 1 + if ((doc.strip ? line.replace(/^\t+/, '') : line) === doc.delimiter) break + body += line + '\n' + } + return { body, next: at } +} + +/** The index of the `)` that closes the `$(` at `open`, skipping quotes and heredoc bodies; text.length when it never closes. */ +function substitutionEnd(text: string, open: number): number { + let depth = 1 + const pending: { delimiter: string; strip: boolean }[] = [] + for (let j = open + 1; j < text.length; j++) { + const c = text[j]! + if (c === '\\') j++ + else if (c === "'") { + const close = text.indexOf("'", j + 1) + j = close < 0 ? text.length : close + } else if (c === '"') { + for (j++; j < text.length && text[j] !== '"'; j++) if (text[j] === '\\') j++ + } else if (c === '<') { + const doc = heredocAt(text, j) + if (doc) { + pending.push(doc) + j = doc.end + } + } else if (c === '\n' && pending.length) { + for (const doc of pending.splice(0)) j = heredocBody(text, j, doc).next - 1 + } else if (c === '(') depth++ + else if (c === ')' && --depth === 0) return j + } + return text.length +} + /** The inner text of each `$(...)` and backtick substitution in `text`. */ function substitutionsIn(text: string): string[] { const out: string[] = [] for (let i = 0; i < text.length; i++) { if (text[i] === '\\') i++ else if (text[i] === '$' && text[i + 1] === '(') { - let depth = 0 - let j = i + 1 - for (; j < text.length; j++) { - if (text[j] === '(') depth++ - else if (text[j] === ')' && --depth === 0) break - } + const j = substitutionEnd(text, i + 1) out.push(text.slice(i + 2, j)) i = j } else if (text[i] === '`') { @@ -133,7 +192,7 @@ function substitutionsIn(text: string): string[] { return out } -/** The simple commands of a shell line, each as its words with quotes removed; a quoted word stays one word. A redirect is its own word (`>`, `>>`, `2>`) before its target; a heredoc body is text, not commands. */ +/** The simple commands of a shell line, each as its words with quotes removed; a quoted word stays one word. A redirect is its own word (`>`, `>>`, `2>`) before its target; a heredoc body is text, not commands, unless a shell reads it as its script or an unquoted body substitutes commands. */ export function simpleCommands(line: string): string[][] { const commands: string[][] = [] const nested: string[][] = [] @@ -168,14 +227,7 @@ export function simpleCommands(line: string): string[][] { if (line[i] === '`') { j = line.indexOf('`', i + 1) if (j < 0) j = line.length - } else { - let depth = 0 - j = i + 1 - for (; j < line.length; j++) { - if (line[j] === '(') depth++ - else if (line[j] === ')' && --depth === 0) break - } - } + } else j = substitutionEnd(line, i + 1) nested.push(...substitutionsIn(line.slice(i, j + 1)).flatMap(simpleCommands)) word += line.slice(i, j + 1) i = j @@ -192,42 +244,23 @@ export function simpleCommands(line: string): string[][] { } else if (c === '$' && line[i + 1] === '(') { endCommand() i++ - } else if (c === '<' && line[i + 1] === '<' && line[i + 2] !== '<') { - let j = i + 2 - const strip = line[j] === '-' - if (strip) j++ - while (line[j] === ' ' || line[j] === '\t') j++ - let delimiter = '' - let quoted = false - for (; j < line.length && !/[\s;&|()<>]/.test(line[j]!); j++) { - if (line[j] === "'" || line[j] === '"') { - quoted = true - const close = line.indexOf(line[j]!, j + 1) - delimiter += line.slice(j + 1, close < 0 ? line.length : close) - j = close < 0 ? line.length : close - } else if (line[j] === '\\') { - quoted = true - delimiter += line[++j] ?? '' - } else delimiter += line[j] - } - heredocs.push({ delimiter, quoted, strip }) - i = j - 1 + } else if (c === '<' && heredocAt(line, i)) { + const doc = heredocAt(line, i)! + heredocs.push(doc) + i = doc.end } else if (c === '\n' && heredocs.length) { endCommand() - let at = i + 1 + let at = i + // A heredoc fed to a shell is that shell's script; an unquoted body's substitutions run either way. + const target = commands[commands.length - 1] + const bin = target ? (target[leaderEnd(target)] ?? '').split('/').pop() ?? '' : '' for (const doc of heredocs.splice(0)) { - let body = '' - while (at < line.length) { - const eol = line.indexOf('\n', at) - const text = line.slice(at, eol < 0 ? line.length : eol) - at = eol < 0 ? line.length : eol + 1 - if ((doc.strip ? text.replace(/^\t+/, '') : text) === doc.delimiter) break - body += text + '\n' - } - // Only an unquoted delimiter lets the body's substitutions run. - if (!doc.quoted) nested.push(...substitutionsIn(body).flatMap(simpleCommands)) + const { body, next } = heredocBody(line, at, doc) + at = next - 1 + if (SHELLS.has(bin) && !target!.some(w => /^-[a-z]*c$/.test(w))) nested.push(...simpleCommands(body)) + else if (!doc.quoted) nested.push(...substitutionsIn(body).flatMap(simpleCommands)) } - i = at - 1 + i = at } else if (c === '>') { if (!(inWord && /^\d+$/.test(word))) endWord() word += '>' @@ -299,7 +332,7 @@ export function filesWrittenBy(command: string): string[] { const at = leaderEnd(words) const bin = (words[at] ?? '').split('/').pop() ?? '' const args = words.slice(at + 1) - if (bin === 'cd') cwd = args[0] && !args[0].startsWith('-') && args[0] !== '~' ? from(args[0]) : '' + if (bin === 'cd') cwd = args[0] && !args[0].startsWith('-') && !args[0].startsWith('~') ? from(args[0]) : '' else if (SHELLS.has(bin)) { const c = args.findIndex(w => /^-[a-z]*c$/.test(w)) if (c >= 0 && c + 1 < args.length) out.push(...filesWrittenBy(args[c + 1]!).map(from)) @@ -449,7 +482,7 @@ function kick($: EngineInterface): Promise { /** * Whether the state wants a check: changes not yet checked, or no baseline yet. After a failed check it is - * asked for again only by a new change or by `retry` (the person asking with /health), not at every turn. + * asked for again only by a new change or by `retry` (/health, or a `git push` held by the gate), not at every turn. */ async function wantsCheck($: EngineInterface, retry = false): Promise { const state = await read($, healthAtom) @@ -541,10 +574,12 @@ export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: // An edit in the same command would run before any check of it. const root = await $.session.root().catch(() => '') for (const name of filesWrittenBy(e.command)) if (await isModelFile($, name.startsWith('/') || /^[A-Za-z]:/.test(name) ? name : `${root}/${name}`)) return hold(`this command changes the model file ${name} and pushes in one go, so the change is not checked first. Run them as separate commands.`) + // With nothing changed this session there is nothing to wait for or hold. + if (((await read($, healthAtom))?.touched.length ?? 0) === 0) return null if ((await wantsCheck($, true)) || running?.generation === generation) await within($, CHECK_MS, kick($)) const state = await read($, healthAtom) // A check that failed or did not finish leaves the changes unchecked: a gate that fails open is no gate. - if (state && enabled && (state.running || state.pending.length > 0 || (state.baseline === null && state.touched.length > 0))) + if (state && enabled && (state.running || state.pending.length > 0 || state.baseline === null)) return hold(`the check of the models changed this session ${state.running ? 'did not finish in time' : `could not run (${state.problem ?? 'no answer'})`}.`) return pushRefusal(options.healthBlocksPush, e.command, state?.issues ?? []) } diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index fc736c0c..c172f4be 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -362,6 +362,13 @@ test('with the push gate on, a push with no model changed this session goes thro expect(allowed?.deny).toBeUndefined() }) +test('with the push gate on, a push before the baseline lands, with no model changed, is not held for it', { options: { healthBlocksPush: true } }, async ($, on) => { + const never = new Promise(() => {}) + await start($, on, { runs: [[OLD_STG]], hold: { at: 0, until: never } }) + const allowed: any = await $.tool.call(command('git push')) + expect(allowed?.deny).toBeUndefined() +}) + test('with the push gate on, a command that edits a model and pushes is held: the edit is not checked before it runs', { options: { healthBlocksPush: true } }, async ($, on) => { await start($, on, { runs: [[]] }) const denied: any = await $.tool.call(command(`sed -i 's/a/b/' models/staging/stg_orders.sql && git push`)) @@ -460,6 +467,15 @@ test('shell syntax: a redirect without spaces, bash -c, cd before the edit, back expect(isGitPush('cat < { From 53bea56723653be81537ffd663dd616006fa5bb5 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 21:56:31 +0530 Subject: [PATCH 05/34] fix: a heredoc belongs to the command it follows, and a shift is not a heredoc Co-Authored-By: Claude Sonnet 5.5 --- .../altimate-code/hooks/features/health.tsx | 18 +++++++++++------- plugins/altimate-code/hooks/health.test.tsx | 12 ++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 6f967850..bc369d5d 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -117,7 +117,6 @@ function heredocAt(text: string, i: number): { delimiter: string; quoted: boolea const strip = text[j] === '-' if (strip) j++ while (text[j] === ' ' || text[j] === '\t') j++ - if (!/[A-Za-z_'"\\]/.test(text[j] ?? '')) return null let delimiter = '' let quoted = false for (; j < text.length && !/[\s;&|()<>]/.test(text[j]!); j++) { @@ -131,6 +130,11 @@ function heredocAt(text: string, i: number): { delimiter: string; quoted: boolea delimiter += text[++j] ?? '' } else delimiter += text[j] } + // A `<<` with no closing line after it is a shift (`$((a< (strip ? line.replace(/^\t+/, '') : line).replace(/\r$/, '') === delimiter + const eol = text.indexOf('\n', j) + if (eol < 0 || !text.slice(eol + 1).split('\n').some(closes)) return null return { delimiter, quoted, strip, end: j - 1 } } @@ -142,7 +146,7 @@ function heredocBody(text: string, eol: number, doc: { delimiter: string; strip: const end = text.indexOf('\n', at) const line = text.slice(at, end < 0 ? text.length : end) at = end < 0 ? text.length : end + 1 - if ((doc.strip ? line.replace(/^\t+/, '') : line) === doc.delimiter) break + if ((doc.strip ? line.replace(/^\t+/, '') : line).replace(/\r$/, '') === doc.delimiter) break body += line + '\n' } return { body, next: at } @@ -196,7 +200,7 @@ function substitutionsIn(text: string): string[] { export function simpleCommands(line: string): string[][] { const commands: string[][] = [] const nested: string[][] = [] - const heredocs: { delimiter: string; quoted: boolean; strip: boolean }[] = [] + const heredocs: { delimiter: string; quoted: boolean; strip: boolean; owner: string[] }[] = [] let words: string[] = [] let word = '' let inWord = false @@ -246,18 +250,18 @@ export function simpleCommands(line: string): string[][] { i++ } else if (c === '<' && heredocAt(line, i)) { const doc = heredocAt(line, i)! - heredocs.push(doc) + // The command the heredoc feeds: the one being read now, which `words` goes on to hold. + heredocs.push({ ...doc, owner: words }) i = doc.end } else if (c === '\n' && heredocs.length) { endCommand() let at = i // A heredoc fed to a shell is that shell's script; an unquoted body's substitutions run either way. - const target = commands[commands.length - 1] - const bin = target ? (target[leaderEnd(target)] ?? '').split('/').pop() ?? '' : '' for (const doc of heredocs.splice(0)) { const { body, next } = heredocBody(line, at, doc) at = next - 1 - if (SHELLS.has(bin) && !target!.some(w => /^-[a-z]*c$/.test(w))) nested.push(...simpleCommands(body)) + const bin = (doc.owner[leaderEnd(doc.owner)] ?? '').split('/').pop() ?? '' + if (SHELLS.has(bin) && !doc.owner.some(w => /^-[a-z]*c$/.test(w))) nested.push(...simpleCommands(body)) else if (!doc.quoted) nested.push(...substitutionsIn(body).flatMap(simpleCommands)) } i = at diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index c172f4be..c0159a1e 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -476,6 +476,18 @@ test('shell syntax: a redirect without spaces, bash -c, cd before the edit, back // A shell reads its heredoc as its script. expect(isGitPush("bash <<'EOF'\ngit push\nEOF")).toBe(true) expect(filesWrittenBy("sh <<'EOF'\nsed -i s/a/b/ models/x.sql\nEOF")).toEqual(['models/x.sql']) + // The heredoc belongs to the command it follows, whatever comes after it on the line. + expect(isGitPush('bash < { From 40deecc1a9cd3bc0b25677ca9a4cef7f4f46f0c4 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Sat, 14 Nov 2026 08:00:43 +0530 Subject: [PATCH 06/34] fix: the health push gate sees a push behind a leading redirect, skips dbt folders by their place in the project, and matches model files through symlinked folders Co-Authored-By: Claude Sonnet 5.5 --- .../altimate-code/hooks/features/health.tsx | 21 ++++++++++++++++--- plugins/altimate-code/hooks/health.test.tsx | 20 ++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index bc369d5d..0677da46 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -9,6 +9,7 @@ // // One check runs at a time. Changes that land while one runs are checked by the next, which starts // when that one ends. +import { realpathSync } from 'node:fs' import { atom, read, update } from 'claude-code' import type { Args, EngineInterface, EventResult, On, PluginOptions, RenderElement, ToolCallResult } from 'claude-code' @@ -66,11 +67,21 @@ const norm = (p: string) => { return (absolute ? '/' : '') + parts.join('/') } +/** `norm`, with a symlinked folder resolved to the one the system names, as dbt_health names a model's files. */ +const canonical = (p: string) => { + const n = norm(p) + try { + return norm(realpathSync(n)) + } catch { + return n + } +} + /** The models whose SQL file or properties file is one of `files`. */ export function modelsOfFiles(files: readonly string[], insights: readonly HealthInsight[]): Set { - const changed = new Set(files.map(norm)) + const changed = new Set(files.map(canonical)) const ids = new Set() - for (const i of insights) if ((i.file && changed.has(norm(i.file))) || (i.properties && changed.has(norm(i.properties)))) ids.add(i.uniqueId) + for (const i of insights) if ((i.file && changed.has(canonical(i.file))) || (i.properties && changed.has(canonical(i.properties)))) ids.add(i.uniqueId) return ids } @@ -291,6 +302,7 @@ function leaderEnd(words: readonly string[]): number { while (i < words.length) { const w = words[i]! if (/^[A-Za-z_]\w*=/.test(w)) i++ + else if (/^\d*>[>|&]?$/.test(w)) i += 2 else if (LEADERS.has(w)) leader = words[i++]! else if (leader && w.startsWith('-')) i += LEADER_VALUE_OPTIONS[leader]?.includes(w) ? 2 : 1 else break @@ -379,9 +391,12 @@ async function projectAbove($: EngineInterface, dir: string): Promise { const path = norm(file) - if (!/\.(sql|ya?ml)$/i.test(path) || SKIPPED_DIRS.test(path)) return false + if (!/\.(sql|ya?ml)$/i.test(path)) return false const dir = path.replace(/\/[^/]*$/, '') const project = await projectAbove($, dir) + // dbt's own folders are skipped by their place in the project, not by where the project sits on disk. + const inside = project !== null && path.startsWith(`${norm(project)}/`) ? path.slice(norm(project).length + 1) : path + if (SKIPPED_DIRS.test(inside)) return false // dbt's own files are the ones at the project root; a `profiles.yml` in a model folder is a properties file. return project !== null && !(ROOT_FILES.has(path.split('/').pop() ?? '') && norm(project) === dir) } diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index c0159a1e..6ee90f28 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -1,3 +1,7 @@ +import { mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' + import { expect, mock, test } from 'claude-code/testing' import { bandText, filesWrittenBy, isGitPush, modelsOfFiles, newIssues, pairKey, pushRefusal, reportText, simpleCommands, toolArgs } from './features/health' @@ -460,6 +464,9 @@ test('shell syntax: a redirect without spaces, bash -c, cd before the edit, back expect(filesWrittenBy('cd models && sed -i s/a/b/ x.sql')).toEqual(['models/x.sql']) expect(filesWrittenBy('cd /repo/models/staging; echo x > y.sql')).toEqual(['/repo/models/staging/y.sql']) expect(filesWrittenBy('echo x > /dev/null 2>&1')).toEqual([]) + expect(isGitPush('2>/dev/null git push origin main')).toBe(true) + expect(isGitPush('>out.log git push')).toBe(true) + expect(isGitPush('2>/dev/null git status')).toBe(false) expect(isGitPush('echo "`git push`"')).toBe(true) expect(isGitPush('echo "$(git push)"')).toBe(true) expect(isGitPush("cat <<'EOF'\ngit push\nEOF")).toBe(false) @@ -490,6 +497,19 @@ test('shell syntax: a redirect without spaces, bash -c, cd before the edit, back expect(isGitPush('cat < { + const dir = mkdtempSync(join(tmpdir(), 'health-')) + try { + mkdirSync(join(dir, 'real/models'), { recursive: true }) + writeFileSync(join(dir, 'real/models/x.sql'), 'select 1') + symlinkSync(join(dir, 'real'), join(dir, 'alias')) + const insight = { ...OLD_STG, file: join(realpathSync(dir), 'real/models/x.sql'), properties: '' } + expect([...modelsOfFiles([join(dir, 'alias/models/x.sql')], [insight])]).toEqual([OLD_STG.uniqueId]) + } finally { + rmSync(dir, { recursive: true, force: true }) + } +}) + test('models match by their own files, so a model of another package is never matched', () => { expect([...modelsOfFiles([STG], [OLD_STG, OLD_FCT, DEP])]).toEqual(['model.shop.stg_orders']) expect([...modelsOfFiles([STG_YML], [OLD_STG, OLD_FCT])]).toEqual(['model.shop.stg_orders']) From beb058387b4879e10ae7c070efa094c64f0c9d05 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Sat, 14 Nov 2026 09:12:19 +0530 Subject: [PATCH 07/34] fix: the health check skips models inside an installed dbt package that has its own dbt_project.yml Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/hooks/features/health.tsx | 5 +++++ plugins/altimate-code/hooks/health.test.tsx | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 0677da46..9ae64ec5 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -397,6 +397,11 @@ async function isModelFile($: EngineInterface, file: string): Promise { // dbt's own folders are skipped by their place in the project, not by where the project sits on disk. const inside = project !== null && path.startsWith(`${norm(project)}/`) ? path.slice(norm(project).length + 1) : path if (SKIPPED_DIRS.test(inside)) return false + // An installed package carries its own dbt_project.yml: judge its place under the project that installed it. + if (project !== null) { + const outer = await projectAbove($, norm(project).replace(/\/[^/]*$/, '')) + if (outer !== null && norm(outer) !== norm(project) && path.startsWith(`${norm(outer)}/`) && SKIPPED_DIRS.test(path.slice(norm(outer).length + 1))) return false + } // dbt's own files are the ones at the project root; a `profiles.yml` in a model folder is a properties file. return project !== null && !(ROOT_FILES.has(path.split('/').pop() ?? '') && norm(project) === dir) } diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index 6ee90f28..d7d3c8ac 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -131,6 +131,13 @@ test('two changed models give "2 new health issues" with the first model named', await band.unmount() }) +test('a model inside an installed package with its own dbt_project.yml is not a model file: no check runs', async ($, on) => { + const { clock, calls } = await start($, on, { runs: [[OLD_STG], [OLD_STG, NEW_STG]], projects: [ROOT, `${ROOT}/dbt_packages/dep`] }) + await $.tool.call(edit(`${ROOT}/dbt_packages/dep/models/x.sql`)) + await endTurn($, clock, 't1') + expect(calls).toHaveLength(1) +}) + test('README, dbt_project.yml, profiles, CI and dbt_packages files are not model files: no check runs', async ($, on) => { const { clock, calls, toasts } = await start($, on, { runs: [[OLD_STG], [OLD_STG, NEW_STG]] }) for (const file of [NOTES, `${ROOT}/dbt_project.yml`, `${ROOT}/profiles.yml`, `${ROOT}/.github/workflows/ci.yml`, `${ROOT}/dbt_packages/dep/models/x.sql`, `${ROOT}/target/compiled/x.sql`]) From 353858369349054283c8275b090637c0f5839d6f Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Sat, 14 Nov 2026 11:12:34 +0530 Subject: [PATCH 08/34] fix: the health push gate sees a push behind a leading input redirect, and matches a deleted or renamed model reached through a symlinked folder Co-Authored-By: Claude Sonnet 5.5 --- .../altimate-code/hooks/features/health.tsx | 28 +++++++++++++++---- plugins/altimate-code/hooks/health.test.tsx | 18 ++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 9ae64ec5..89309bb9 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -70,10 +70,18 @@ const norm = (p: string) => { /** `norm`, with a symlinked folder resolved to the one the system names, as dbt_health names a model's files. */ const canonical = (p: string) => { const n = norm(p) - try { - return norm(realpathSync(n)) - } catch { - return n + // A deleted or renamed file has no real path of its own: resolve its nearest folder that exists and keep the rest. + let head = n + let tail = '' + for (;;) { + try { + return norm(realpathSync(head) + tail) + } catch { + const slash = head.lastIndexOf('/') + if (slash <= 0) return n + tail = head.slice(slash) + tail + head = head.slice(0, slash) + } } } @@ -276,6 +284,16 @@ export function simpleCommands(line: string): string[][] { else if (!doc.quoted) nested.push(...substitutionsIn(body).flatMap(simpleCommands)) } i = at + } else if (c === '<' && line[i + 1] !== '(') { + // An input redirect (`<`, `<<<`) is its own word before its target, as an output one is. + if (!(inWord && /^\d+$/.test(word))) endWord() + word += '<' + if (line.startsWith('<<', i + 1)) { + word += '<<' + i += 2 + } else if (line[i + 1] === '>') word += line[++i] + inWord = true + endWord() } else if (c === '>') { if (!(inWord && /^\d+$/.test(word))) endWord() word += '>' @@ -302,7 +320,7 @@ function leaderEnd(words: readonly string[]): number { while (i < words.length) { const w = words[i]! if (/^[A-Za-z_]\w*=/.test(w)) i++ - else if (/^\d*>[>|&]?$/.test(w)) i += 2 + else if (/^\d*(>[>|&]?|<(<<|>)?)$/.test(w)) i += 2 else if (LEADERS.has(w)) leader = words[i++]! else if (leader && w.startsWith('-')) i += LEADER_VALUE_OPTIONS[leader]?.includes(w) ? 2 : 1 else break diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index d7d3c8ac..30d3017b 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -517,6 +517,24 @@ test('a model edited through a symlinked folder matches the files dbt_health nam } }) +test('a deleted or renamed model reached through a symlinked folder still matches the real path', () => { + const dir = mkdtempSync(join(tmpdir(), 'health-')) + try { + mkdirSync(join(dir, 'real/models'), { recursive: true }) + symlinkSync(join(dir, 'real'), join(dir, 'alias')) + const insight = { ...OLD_STG, file: join(realpathSync(dir), 'real/models/gone.sql'), properties: '' } + expect([...modelsOfFiles([join(dir, 'alias/models/gone.sql')], [insight])]).toEqual([OLD_STG.uniqueId]) + } finally { + rmSync(dir, { recursive: true, force: true }) + } +}) + +test('a leading input redirect does not hide git push', () => { + for (const line of ['< in.txt git push', ' /dev/null git push', '0< in git push', '<> f git push']) + expect([line, isGitPush(line)]).toEqual([line, true]) + expect(isGitPush('< in.txt git status')).toBe(false) +}) + test('models match by their own files, so a model of another package is never matched', () => { expect([...modelsOfFiles([STG], [OLD_STG, OLD_FCT, DEP])]).toEqual(['model.shop.stg_orders']) expect([...modelsOfFiles([STG_YML], [OLD_STG, OLD_FCT])]).toEqual(['model.shop.stg_orders']) From d4020f939f62d65dfde8cbe31fe6f6093485bf2c Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Sat, 14 Nov 2026 11:23:14 +0530 Subject: [PATCH 09/34] fix: an input redirect's file is read, not written, and quoted digits before a redirect are an argument Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/hooks/features/health.tsx | 15 ++++++++++++--- plugins/altimate-code/hooks/health.test.tsx | 4 ++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 89309bb9..2ab83d29 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -223,11 +223,15 @@ export function simpleCommands(line: string): string[][] { let words: string[] = [] let word = '' let inWord = false + // Whether the word being read has quoted or escaped text: digits then are an argument, not a file descriptor. + let quoted = false const endWord = () => { if (inWord) words.push(word) word = '' inWord = false + quoted = false } + const bareDescriptor = () => inWord && !quoted && /^\d+$/.test(word) const endCommand = () => { endWord() if (words.length) commands.push(words) @@ -236,12 +240,14 @@ export function simpleCommands(line: string): string[][] { for (let i = 0; i < line.length; i++) { const c = line[i]! if (c === "'") { + quoted = true const close = line.indexOf("'", i + 1) word += line.slice(i + 1, close < 0 ? line.length : close) inWord = true i = close < 0 ? line.length : close } else if (c === '"') { inWord = true + quoted = true i++ for (; i < line.length && line[i] !== '"'; i++) { if ((line[i] === '$' && line[i + 1] === '(') || line[i] === '`') { @@ -264,6 +270,7 @@ export function simpleCommands(line: string): string[][] { } else if (c === '\\' && i + 1 < line.length) { word += line[++i] inWord = true + quoted = true } else if (c === '$' && line[i + 1] === '(') { endCommand() i++ @@ -286,7 +293,7 @@ export function simpleCommands(line: string): string[][] { i = at } else if (c === '<' && line[i + 1] !== '(') { // An input redirect (`<`, `<<<`) is its own word before its target, as an output one is. - if (!(inWord && /^\d+$/.test(word))) endWord() + if (!bareDescriptor()) endWord() word += '<' if (line.startsWith('<<', i + 1)) { word += '<<' @@ -295,7 +302,7 @@ export function simpleCommands(line: string): string[][] { inWord = true endWord() } else if (c === '>') { - if (!(inWord && /^\d+$/.test(word))) endWord() + if (!bareDescriptor()) endWord() word += '>' if (line[i + 1] === '>' || line[i + 1] === '&' || line[i + 1] === '|') word += line[++i] inWord = true @@ -357,7 +364,9 @@ export function filesWrittenBy(command: string): string[] { const out: string[] = [] let cwd = '' const from = (w: string) => (w.startsWith('/') || /^[A-Za-z]:/.test(w) || !cwd ? w : `${cwd}/${w}`) - for (const words of simpleCommands(command)) { + for (const all of simpleCommands(command)) { + // An input redirect's operand is read, not written. + const words = all.filter((_, j) => !/^\d*<(<<|>)?$/.test(all[j - 1] ?? '') && !/^\d*<(<<|>)?$/.test(all[j]!)) const models = (list: readonly string[]) => list.filter(w => MODEL_FILE.test(w) && !w.startsWith('-')).map(from) for (let i = 0; i < words.length; i++) { const redirect = /^\d*>[>|]?$/.exec(words[i]!) diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index 30d3017b..2f21bf6f 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -533,6 +533,10 @@ test('a leading input redirect does not hide git push', () => { for (const line of ['< in.txt git push', ' /dev/null git push', '0< in git push', '<> f git push']) expect([line, isGitPush(line)]).toEqual([line, true]) expect(isGitPush('< in.txt git status')).toBe(false) + expect(isGitPush('"0" { From 2b0ce1c3de44559bafe1655845b54bcb792b67ba Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Sun, 15 Nov 2026 21:38:17 +0530 Subject: [PATCH 10/34] fix: the health hooks resolve symlinked folders through the engine, so Claude Code loads the hooks module The module imported node:fs, which the hooks sandbox rejects, so no feature of the plugin loaded. Folders are now resolved with the engine's fs.stat (resolve: true), keeping the symlink and deleted-path matching, and tests prove the module loads and the symlink flow still raises the band. Co-Authored-By: Claude Sonnet 5.5 --- .../altimate-code/hooks/features/health.tsx | 65 ++++++++++------ plugins/altimate-code/hooks/health.test.tsx | 77 +++++++++++++------ 2 files changed, 95 insertions(+), 47 deletions(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 2ab83d29..5627062a 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -9,7 +9,6 @@ // // One check runs at a time. Changes that land while one runs are checked by the next, which starts // when that one ends. -import { realpathSync } from 'node:fs' import { atom, read, update } from 'claude-code' import type { Args, EngineInterface, EventResult, On, PluginOptions, RenderElement, ToolCallResult } from 'claude-code' @@ -67,29 +66,49 @@ const norm = (p: string) => { return (absolute ? '/' : '') + parts.join('/') } -/** `norm`, with a symlinked folder resolved to the one the system names, as dbt_health names a model's files. */ -const canonical = (p: string) => { - const n = norm(p) - // A deleted or renamed file has no real path of its own: resolve its nearest folder that exists and keep the rest. - let head = n - let tail = '' - for (;;) { - try { - return norm(realpathSync(head) + tail) - } catch { - const slash = head.lastIndexOf('/') - if (slash <= 0) return n - tail = head.slice(slash) + tail - head = head.slice(0, slash) - } +/** A path as the system names it: how `canonical` spells it, or `norm`'s spelling for a path it was not asked about. */ +export type RealPath = (path: string) => string + +/** The folder's real path; a folder that does not exist gets its nearest existing folder's real path and the rest. */ +async function realFolder($: EngineInterface, dir: string, seen: Map>): Promise { + const known = seen.get(dir) + if (known) return known + const work = (async () => { + const stat = await $.fs.stat(dir, { resolve: true }).catch(() => null) + if (stat?.realPath) return norm(stat.realPath) + // A deleted or renamed file's folder may be gone too: resolve the nearest folder that exists and keep the rest. + const slash = dir.lastIndexOf('/') + if (slash <= 0) return dir + return `${await realFolder($, dir.slice(0, slash), seen)}${dir.slice(slash)}` + })() + seen.set(dir, work) + return work +} + +/** + * Resolves each file's folder through the engine (a symlinked folder to the one `dbt_health` names a model's files under), + * so the hooks module needs no file-system module of its own. The result is for the files asked about. + */ +export async function canonical($: EngineInterface, files: readonly string[]): Promise { + const seen = new Map>() + const real = new Map() + await Promise.all( + [...new Set(files.filter(Boolean).map(norm))].map(async file => { + const slash = file.lastIndexOf('/') + real.set(file, slash <= 0 ? file : `${await realFolder($, file.slice(0, slash), seen)}${file.slice(slash)}`) + }), + ) + return p => { + const n = norm(p) + return real.get(n) ?? n } } -/** The models whose SQL file or properties file is one of `files`. */ -export function modelsOfFiles(files: readonly string[], insights: readonly HealthInsight[]): Set { - const changed = new Set(files.map(canonical)) +/** The models whose SQL file or properties file is one of `files`; `real` names the paths as `canonical` resolved them. */ +export function modelsOfFiles(files: readonly string[], insights: readonly HealthInsight[], real: RealPath = norm): Set { + const changed = new Set(files.map(real)) const ids = new Set() - for (const i of insights) if ((i.file && changed.has(canonical(i.file))) || (i.properties && changed.has(canonical(i.properties)))) ids.add(i.uniqueId) + for (const i of insights) if ((i.file && changed.has(real(i.file))) || (i.properties && changed.has(real(i.properties)))) ids.add(i.uniqueId) return ids } @@ -494,13 +513,15 @@ async function loop($: EngineInterface, mine: number) { // Not asked for again at once: the next change, /health or push asks. return } + const asked = (await read($, healthAtom))?.touched ?? [] + const real = await canonical($, [...asked, ...run.insights.flatMap(i => [i.file, i.properties])]) await update($, healthAtom, prev => { const base = prev ?? fresh() // A baseline that lands after edits (the first run failed, or the project was opened above the dbt // project) cannot tell the touched models' old issues from the new: all of theirs count as new. - const edited = modelsOfFiles(base.touched, run.insights) + const edited = modelsOfFiles(base.touched, run.insights, real) const baseline = base.baseline ?? run.insights.filter(i => !edited.has(i.uniqueId)).map(pairKey) - const issues = newIssues(run.insights, baseline, modelsOfFiles(base.touched, run.insights)) + const issues = newIssues(run.insights, baseline, modelsOfFiles(base.touched, run.insights, real)) return { ...base, baseline, issues, running: false, problem: null } }) toldProblem = null diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index 2f21bf6f..6f1e5a66 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -1,10 +1,6 @@ -import { mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs' -import { tmpdir } from 'node:os' -import { join } from 'node:path' - import { expect, mock, test } from 'claude-code/testing' -import { bandText, filesWrittenBy, isGitPush, modelsOfFiles, newIssues, pairKey, pushRefusal, reportText, simpleCommands, toolArgs } from './features/health' +import { bandText, canonical, filesWrittenBy, isGitPush, modelsOfFiles, newIssues, pairKey, pushRefusal, reportText, simpleCommands, toolArgs } from './features/health' const ROOT = '/repo' const STG = `${ROOT}/models/staging/stg_orders.sql` @@ -45,6 +41,8 @@ type Options = { hold?: { at: number; until: Promise } /** Folders that hold a dbt_project.yml (default: the repo). */ projects?: string[] + /** Folders the engine resolves to another real folder (a symlink); any other folder is unknown to it. */ + realFolders?: Record } async function start($: any, on: any, options: Options, session: { isInteractive?: boolean } = {}) { @@ -72,6 +70,11 @@ async function start($: any, on: any, options: Options, session: { isInteractive }) const projects = options.projects ?? [ROOT] on('fs.exists', (_: unknown, e: { path: string }) => ({ value: projects.some(p => e.path === `${p}/dbt_project.yml`) })) + on('fs.stat', (_: unknown, e: { path: string }) => { + const real = options.realFolders?.[e.path] + if (real === undefined) throw new Error('ENOENT') + return { value: { kind: 'dir', size: 0, mtimeMs: 0, isLink: true, realPath: real } } + }) on('fs.read', () => ({ value: '' })) on('tool.list', () => ({ value: [] })) on('mcp.connect', () => ({ value: { isConnected: false, reason: 'not_connected', message: 'off' } })) @@ -504,29 +507,53 @@ test('shell syntax: a redirect without spaces, bash -c, cd before the edit, back expect(isGitPush('cat < { - const dir = mkdtempSync(join(tmpdir(), 'health-')) - try { - mkdirSync(join(dir, 'real/models'), { recursive: true }) - writeFileSync(join(dir, 'real/models/x.sql'), 'select 1') - symlinkSync(join(dir, 'real'), join(dir, 'alias')) - const insight = { ...OLD_STG, file: join(realpathSync(dir), 'real/models/x.sql'), properties: '' } - expect([...modelsOfFiles([join(dir, 'alias/models/x.sql')], [insight])]).toEqual([OLD_STG.uniqueId]) - } finally { - rmSync(dir, { recursive: true, force: true }) - } +test('the hooks module loads in the sandbox Claude Code runs it in (no node: import) and runs a baseline', async ($, on) => { + const { calls } = await start($, on, { runs: [[OLD_STG]] }) + expect(calls.map(c => c.tool)).toEqual(['dbt_health']) }) -test('a deleted or renamed model reached through a symlinked folder still matches the real path', () => { - const dir = mkdtempSync(join(tmpdir(), 'health-')) - try { - mkdirSync(join(dir, 'real/models'), { recursive: true }) - symlinkSync(join(dir, 'real'), join(dir, 'alias')) - const insight = { ...OLD_STG, file: join(realpathSync(dir), 'real/models/gone.sql'), properties: '' } - expect([...modelsOfFiles([join(dir, 'alias/models/gone.sql')], [insight])]).toEqual([OLD_STG.uniqueId]) - } finally { - rmSync(dir, { recursive: true, force: true }) +test('canonical resolves a symlinked folder through the engine, and a deleted file through its nearest existing folder', async () => { + const folders: Record = { '/alias': '/private/real', '/alias/models': '/private/real/models' } + const engine = { + fs: { + stat: async (path: string) => { + if (!(path in folders)) throw new Error('ENOENT') + return { realPath: folders[path] } + }, + }, } + const real = await canonical(engine as any, ['/alias/models/x.sql', '/alias/models/sub/gone.sql', '/other/a.sql', '/alias/./models/../models/y.sql']) + expect(real('/alias/models/x.sql')).toBe('/private/real/models/x.sql') + expect(real('/alias/models/sub/gone.sql')).toBe('/private/real/models/sub/gone.sql') + expect(real('/other/a.sql')).toBe('/other/a.sql') + expect(real('/alias/./models/../models/y.sql')).toBe('/private/real/models/y.sql') + // A path it was not asked about keeps its own spelling. + expect(real('/alias/models/z.sql')).toBe('/alias/models/z.sql') +}) + +test('a model edited through a symlinked folder, edited or deleted, shows its new issue in the band', async ($, on) => { + const real = '/private/shop' + const file = `${real}/models/staging/stg_orders.sql` + const stg = { ...NEW_STG, file, properties: '' } + const old = { ...OLD_STG, file, properties: '' } + const { clock, toasts } = await start($, on, { + runs: [[old], [old, stg]], + realFolders: { [`${ROOT}/models/staging`]: `${real}/models/staging` }, + }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + expect(toasts).toEqual(['◆ 1 new health issue · stg_orders: hard coded references · /health']) +}) + +test('a deleted model in a folder that is gone too still matches through its nearest existing folder', async ($, on) => { + const real = '/private/shop' + const file = `${real}/models/gone/stg_orders.sql` + const stg = { ...NEW_STG, file, properties: '' } + const old = { ...OLD_STG, file, properties: '' } + const { clock, toasts } = await start($, on, { runs: [[old], [old, stg]], realFolders: { [`${ROOT}/models`]: `${real}/models` } }) + await $.tool.call(edit(`${ROOT}/models/gone/stg_orders.sql`)) + await endTurn($, clock, 't1') + expect(toasts).toEqual(['◆ 1 new health issue · stg_orders: hard coded references · /health']) }) test('a leading input redirect does not hide git push', () => { From 73660d7db5cb284966e17a5be233ee4da0dad403 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Sun, 15 Nov 2026 21:42:45 +0530 Subject: [PATCH 11/34] fix: health commits a check only once every edited file's folder is resolved, so an edit that lands meanwhile is not folded into the baseline Co-Authored-By: Claude Sonnet 5.5 --- .../altimate-code/hooks/features/health.tsx | 32 ++++++++++++------- plugins/altimate-code/hooks/health.test.tsx | 23 ++++++++++++- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 5627062a..1003c070 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -513,17 +513,27 @@ async function loop($: EngineInterface, mine: number) { // Not asked for again at once: the next change, /health or push asks. return } - const asked = (await read($, healthAtom))?.touched ?? [] - const real = await canonical($, [...asked, ...run.insights.flatMap(i => [i.file, i.properties])]) - await update($, healthAtom, prev => { - const base = prev ?? fresh() - // A baseline that lands after edits (the first run failed, or the project was opened above the dbt - // project) cannot tell the touched models' old issues from the new: all of theirs count as new. - const edited = modelsOfFiles(base.touched, run.insights, real) - const baseline = base.baseline ?? run.insights.filter(i => !edited.has(i.uniqueId)).map(pairKey) - const issues = newIssues(run.insights, baseline, modelsOfFiles(base.touched, run.insights, real)) - return { ...base, baseline, issues, running: false, problem: null } - }) + // Resolving the folders takes engine calls, and an edit can land meanwhile: the result is committed only with + // every touched file resolved, so a late edit's model never counts its old issues as the baseline. + for (let attempt = 0; ; attempt++) { + const asked = new Set((await read($, healthAtom))?.touched ?? []) + const real = await canonical($, [...asked, ...run.insights.flatMap(i => [i.file, i.properties])]) + let late = false + await update($, healthAtom, prev => { + const base = prev ?? fresh() + if (attempt < 3 && base.touched.some(f => !asked.has(f))) { + late = true + return base + } + // A baseline that lands after edits (the first run failed, or the project was opened above the dbt + // project) cannot tell the touched models' old issues from the new: all of theirs count as new. + const edited = modelsOfFiles(base.touched, run.insights, real) + const baseline = base.baseline ?? run.insights.filter(i => !edited.has(i.uniqueId)).map(pairKey) + const issues = newIssues(run.insights, baseline, edited) + return { ...base, baseline, issues, running: false, problem: null } + }) + if (!late) break + } toldProblem = null const issues = (await read($, healthAtom))?.issues ?? [] await drawStatus($, issues) diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index 6f1e5a66..37e52e62 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -43,6 +43,8 @@ type Options = { projects?: string[] /** Folders the engine resolves to another real folder (a symlink); any other folder is unknown to it. */ realFolders?: Record + /** Holds the first folder lookup until released. */ + holdStat?: Promise } async function start($: any, on: any, options: Options, session: { isInteractive?: boolean } = {}) { @@ -70,7 +72,12 @@ async function start($: any, on: any, options: Options, session: { isInteractive }) const projects = options.projects ?? [ROOT] on('fs.exists', (_: unknown, e: { path: string }) => ({ value: projects.some(p => e.path === `${p}/dbt_project.yml`) })) - on('fs.stat', (_: unknown, e: { path: string }) => { + let statHeld = false + on('fs.stat', async (_: unknown, e: { path: string }) => { + if (options.holdStat && !statHeld) { + statHeld = true + await options.holdStat + } const real = options.realFolders?.[e.path] if (real === undefined) throw new Error('ENOENT') return { value: { kind: 'dir', size: 0, mtimeMs: 0, isLink: true, realPath: real } } @@ -545,6 +552,20 @@ test('a model edited through a symlinked folder, edited or deleted, shows its ne expect(toasts).toEqual(['◆ 1 new health issue · stg_orders: hard coded references · /health']) }) +test('an edit that lands while the folders resolve is counted as an edit of its model, not folded into the baseline', async ($, on) => { + let release!: () => void + const holdStat = new Promise(resolve => (release = resolve)) + const real = '/private/shop' + const file = `${real}/models/staging/stg_orders.sql` + const both = [{ ...OLD_STG, file, properties: '' }, { ...NEW_STG, file, properties: '' }] + const { clock, toasts } = await start($, on, { runs: [both], realFolders: { [`${ROOT}/models/staging`]: `${real}/models/staging` }, holdStat }) + await $.tool.call(edit(STG)) + release() + await clock.settle() + await endTurn($, clock, 't1') + expect(toasts).toEqual(['◆ 2 new health issues · stg_orders: missing documentation · /health']) +}) + test('a deleted model in a folder that is gone too still matches through its nearest existing folder', async ($, on) => { const real = '/private/shop' const file = `${real}/models/gone/stg_orders.sql` From 00f366aad2e20a2e309d132216aaa5550d89fc66 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Sun, 15 Nov 2026 21:46:22 +0530 Subject: [PATCH 12/34] fix: a health check from before a session restart writes nothing while its folders resolve Co-Authored-By: Claude Sonnet 5.5 --- .../altimate-code/hooks/features/health.tsx | 4 +++ plugins/altimate-code/hooks/health.test.tsx | 27 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 1003c070..0c61d0e9 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -518,9 +518,12 @@ async function loop($: EngineInterface, mine: number) { for (let attempt = 0; ; attempt++) { const asked = new Set((await read($, healthAtom))?.touched ?? []) const real = await canonical($, [...asked, ...run.insights.flatMap(i => [i.file, i.properties])]) + // A session that restarted meanwhile owns the state now: this check's answer is for the one before. + if (mine !== generation) return let late = false await update($, healthAtom, prev => { const base = prev ?? fresh() + if (mine !== generation) return base if (attempt < 3 && base.touched.some(f => !asked.has(f))) { late = true return base @@ -534,6 +537,7 @@ async function loop($: EngineInterface, mine: number) { }) if (!late) break } + if (mine !== generation) return toldProblem = null const issues = (await read($, healthAtom))?.issues ?? [] await drawStatus($, issues) diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index 37e52e62..2246519e 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -566,6 +566,33 @@ test('an edit that lands while the folders resolve is counted as an edit of its expect(toasts).toEqual(['◆ 2 new health issues · stg_orders: missing documentation · /health']) }) +test('a check of the session before a restart does not write its answer into the new session while the folders resolve', async ($, on) => { + let releaseStat!: () => void + const holdStat = new Promise(resolve => (releaseStat = resolve)) + let releaseRun!: () => void + const until = new Promise(resolve => (releaseRun = resolve)) + const real = '/private/shop' + const file = `${real}/models/staging/stg_orders.sql` + const old = { ...OLD_STG, file, properties: '' } + const added = { ...NEW_STG, file, properties: '' } + const { clock, toasts } = await start($, on, { + // The first answer is for the session before the restart; the new session's baseline is the project as it is. + runs: [[old, added], [old], [old, added]], + realFolders: { [`${ROOT}/models/staging`]: `${real}/models/staging` }, + holdStat, + hold: { at: 1, until }, + }) + await $.session.start({ cwd: ROOT, surface: 'terminal', isInteractive: true }) + releaseStat() + await clock.settle() + releaseRun() + await clock.settle() + // The old session's answer did not become the new baseline: this edit's new issue is new. + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + expect(toasts).toEqual(['◆ 1 new health issue · stg_orders: hard coded references · /health']) +}) + test('a deleted model in a folder that is gone too still matches through its nearest existing folder', async ($, on) => { const real = '/private/shop' const file = `${real}/models/gone/stg_orders.sql` From de85ff66adb57b4eaa568233d75b0437e9942d8c Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Sun, 15 Nov 2026 22:23:21 +0530 Subject: [PATCH 13/34] fix: the health feature clears a status part an earlier process left, keeps one feature's failed registration from skipping the rest, and leaves a check uncommitted when edits keep landing while folders resolve Co-Authored-By: Claude Sonnet 5.5 --- .../altimate-code/hooks/features/health.tsx | 16 +++-- plugins/altimate-code/hooks/features/index.ts | 60 +++++++++++++++---- plugins/altimate-code/hooks/health.test.tsx | 49 ++++++++++++++- 3 files changed, 110 insertions(+), 15 deletions(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 0c61d0e9..7f1dad80 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -515,6 +515,9 @@ async function loop($: EngineInterface, mine: number) { } // Resolving the folders takes engine calls, and an edit can land meanwhile: the result is committed only with // every touched file resolved, so a late edit's model never counts its old issues as the baseline. + // After three tries the answer is not committed (it would count a late edit's model in its unresolved + // spelling): the changes it covered are asked for again, and the loop starts over. + let again = false for (let attempt = 0; ; attempt++) { const asked = new Set((await read($, healthAtom))?.touched ?? []) const real = await canonical($, [...asked, ...run.insights.flatMap(i => [i.file, i.properties])]) @@ -524,9 +527,11 @@ async function loop($: EngineInterface, mine: number) { await update($, healthAtom, prev => { const base = prev ?? fresh() if (mine !== generation) return base - if (attempt < 3 && base.touched.some(f => !asked.has(f))) { + if (base.touched.some(f => !asked.has(f))) { late = true - return base + if (attempt < 3) return base + again = true + return { ...base, pending: [...new Set([...batch, ...base.pending])] } } // A baseline that lands after edits (the first run failed, or the project was opened above the dbt // project) cannot tell the touched models' old issues from the new: all of theirs count as new. @@ -535,8 +540,9 @@ async function loop($: EngineInterface, mine: number) { const issues = newIssues(run.insights, baseline, edited) return { ...base, baseline, issues, running: false, problem: null } }) - if (!late) break + if (!late || again) break } + if (again) continue if (mine !== generation) return toldProblem = null const issues = (await read($, healthAtom))?.issues ?? [] @@ -602,7 +608,9 @@ export async function sessionStart($: EngineInterface, e: Args<'session.start'>) toldProblem = null projects.clear() await update($, healthAtom, () => fresh()) - if (drawn) await drawStatus($, []) + // The status line outlives a process: a part an earlier one left is cleared whether or not this one drew it. + drawn = true + await drawStatus($, []) await $.command.register({ name: COMMAND, description: 'Show the project health issues Claude introduced' }) // The baseline takes a while (the dbt adapter starts): nothing waits for it. A session with nobody at // the prompt checks only when the push gate needs it. diff --git a/plugins/altimate-code/hooks/features/index.ts b/plugins/altimate-code/hooks/features/index.ts index e3149873..8a743e0e 100644 --- a/plugins/altimate-code/hooks/features/index.ts +++ b/plugins/altimate-code/hooks/features/index.ts @@ -153,20 +153,60 @@ function failed($: EngineInterface, id: string, part: string, error: unknown) { } catch {} } -/** Registers every feature's own hooks. */ +/** Registers that threw, logged at the next session start (there is no `$` while registering). */ +const registerFailures: [string, unknown][] = [] + +/** Registers every feature's own hooks; one feature's failure never skips the rest. */ export function registerFeatures(on: On, context: FeatureContext) { - runsRegister(on, context) - deferRegister(on, context) - toModelRegister(on, context) - commentsRegister(on, context) - costRegister(on, context) - healthRegister(on, context) - sqlGraphRegister(on, context) - docsRegister(on, context) - cteProfileRegister(on, context) + try { + runsRegister(on, context) + } catch (error) { + registerFailures.push(['runs', error]) + } + try { + deferRegister(on, context) + } catch (error) { + registerFailures.push(['defer', error]) + } + try { + toModelRegister(on, context) + } catch (error) { + registerFailures.push(['toModel', error]) + } + try { + commentsRegister(on, context) + } catch (error) { + registerFailures.push(['comments', error]) + } + try { + costRegister(on, context) + } catch (error) { + registerFailures.push(['cost', error]) + } + try { + healthRegister(on, context) + } catch (error) { + registerFailures.push(['health', error]) + } + try { + sqlGraphRegister(on, context) + } catch (error) { + registerFailures.push(['sqlGraph', error]) + } + try { + docsRegister(on, context) + } catch (error) { + registerFailures.push(['docs', error]) + } + try { + cteProfileRegister(on, context) + } catch (error) { + registerFailures.push(['cteProfile', error]) + } } export async function featuresSessionStart($: EngineInterface, e: Args<'session.start'>) { + for (const [id, error] of registerFailures.splice(0)) failed($, id, 'register', error) await runsSessionStart($, e).catch(error => failed($, 'runs', 'sessionStart', error)) await deferSessionStart($, e).catch(error => failed($, 'defer', 'sessionStart', error)) await toModelSessionStart($, e).catch(error => failed($, 'toModel', 'sessionStart', error)) diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index 2246519e..9110c12f 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -1,5 +1,6 @@ import { expect, mock, test } from 'claude-code/testing' +import { registerFeatures } from './features' import { bandText, canonical, filesWrittenBy, isGitPush, modelsOfFiles, newIssues, pairKey, pushRefusal, reportText, simpleCommands, toolArgs } from './features/health' const ROOT = '/repo' @@ -32,7 +33,7 @@ const OLD_FCT = insight('fct_orders', FCT, 'Missing tests') const NEW_STG = insight('stg_orders', STG, 'Hard coded references', 'stg_orders reads raw.orders directly', STG_YML) const NEW_FCT = insight('fct_orders', FCT, 'Model fanout', 'fct_orders has 4 direct children') // A model of a dependency package: it has no files in the project. -const DEP = { ...insight('base', '', 'Missing tests'), uniqueId: 'model.dep.base', path: 'models/base.sql' } +const DEP = { ...insight('base', `${ROOT}/dbt_packages/dep/models/base.sql`, 'Missing tests'), uniqueId: 'model.dep.base', path: 'models/base.sql' } type Options = { /** The tool's answer per run (the last repeats). */ @@ -45,6 +46,8 @@ type Options = { realFolders?: Record /** Holds the first folder lookup until released. */ holdStat?: Promise + /** Called with the number of each folder lookup (from 1). */ + onStat?: (n: number) => Promise | void } async function start($: any, on: any, options: Options, session: { isInteractive?: boolean } = {}) { @@ -73,7 +76,9 @@ async function start($: any, on: any, options: Options, session: { isInteractive const projects = options.projects ?? [ROOT] on('fs.exists', (_: unknown, e: { path: string }) => ({ value: projects.some(p => e.path === `${p}/dbt_project.yml`) })) let statHeld = false + let stats = 0 on('fs.stat', async (_: unknown, e: { path: string }) => { + await options.onStat?.(++stats) if (options.holdStat && !statHeld) { statHeld = true await options.holdStat @@ -593,6 +598,48 @@ test('a check of the session before a restart does not write its answer into the expect(toasts).toEqual(['◆ 1 new health issue · stg_orders: hard coded references · /health']) }) +test('an edit that keeps landing while the folders resolve is never folded into the baseline, however many tries it takes', async ($, on) => { + const real = '/private/shop' + const file = `${real}/models/staging/stg_orders.sql` + const both = [{ ...OLD_STG, file, properties: '' }, { ...NEW_STG, file, properties: '' }] + // Each folder lookup waits for the test, which lands an edit during it: three late edits of other files + // use up the tries, and the model's own file lands during the last one. + const held: (() => void)[] = [] + let gate = true + const started = start($, on, { + runs: [both], + realFolders: { [`${ROOT}/models/staging`]: `${real}/models/staging` }, + onStat: () => (gate ? new Promise(resolve => held.push(resolve)) : undefined), + }) + const files = [`${ROOT}/models/staging/late1.sql`, `${ROOT}/models/staging/late2.sql`, `${ROOT}/models/staging/late3.sql`, STG] + for (const file of files) { + while (held.length === 0) await new Promise(resolve => setTimeout(resolve, 5)) + gate = false + await $.tool.call(edit(file)) + gate = true + held.splice(0).forEach(release => release()) + } + gate = false + const { clock, statuses } = await started + await clock.settle() + expect(statuses.at(-1)).toContain('2 health issues') +}) + +test('the status line part a previous process left is cleared at session start, drawn or not', async ($, on) => { + const { clock, statuses } = await start($, on, { runs: [[OLD_STG]] }) + const before = statuses.length + await $.session.start({ cwd: ROOT, surface: 'terminal', isInteractive: true }) + await clock.settle() + expect(statuses.length).toBeGreaterThan(before) +}) + +test('one feature that throws while registering does not stop the others', () => { + const on: any = () => { + throw new Error('every registration fails') + } + expect(() => registerFeatures(on, { options: {} as any })).not.toThrow() +}) + test('a deleted model in a folder that is gone too still matches through its nearest existing folder', async ($, on) => { const real = '/private/shop' const file = `${real}/models/gone/stg_orders.sql` From c40f3b950ce15b2d553bbec6695a3d747e76bb33 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 07:52:19 +0530 Subject: [PATCH 14/34] test: health's feature tests on top of the foundation's registration and lineage tests Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/hooks/features/index.test.ts | 3 ++- plugins/altimate-code/hooks/health.test.tsx | 8 -------- plugins/altimate-code/hooks/task-impact.test.tsx | 2 ++ 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/plugins/altimate-code/hooks/features/index.test.ts b/plugins/altimate-code/hooks/features/index.test.ts index 7c1e206d..9925872b 100644 --- a/plugins/altimate-code/hooks/features/index.test.ts +++ b/plugins/altimate-code/hooks/features/index.test.ts @@ -47,5 +47,6 @@ test('with every feature a stub, each phase runs through the features and logs n expect(await featuresBashAfter($, bash, {} as any)).toEqual([]) expect(await featuresEditAfter($, file, {} as any, { path: '/repo/models/a.sql', before: null })).toEqual([]) expect(await featuresReadOrEditAfter($, file, {} as any, { path: '/repo/models/a.sql' })).toEqual([]) - expect(logs).toEqual([]) + // health is implemented and needs a session's state, which this bare engine has none of; its own tests cover it. + expect(logs.filter(line => !line.startsWith('health · '))).toEqual([]) }) diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index 9110c12f..201a7ccc 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -1,6 +1,5 @@ import { expect, mock, test } from 'claude-code/testing' -import { registerFeatures } from './features' import { bandText, canonical, filesWrittenBy, isGitPush, modelsOfFiles, newIssues, pairKey, pushRefusal, reportText, simpleCommands, toolArgs } from './features/health' const ROOT = '/repo' @@ -633,13 +632,6 @@ test('the status line part a previous process left is cleared at session start, expect(statuses.length).toBeGreaterThan(before) }) -test('one feature that throws while registering does not stop the others', () => { - const on: any = () => { - throw new Error('every registration fails') - } - expect(() => registerFeatures(on, { options: {} as any })).not.toThrow() -}) - test('a deleted model in a folder that is gone too still matches through its nearest existing folder', async ($, on) => { const real = '/private/shop' const file = `${real}/models/gone/stg_orders.sql` diff --git a/plugins/altimate-code/hooks/task-impact.test.tsx b/plugins/altimate-code/hooks/task-impact.test.tsx index bea1a61d..12277285 100644 --- a/plugins/altimate-code/hooks/task-impact.test.tsx +++ b/plugins/altimate-code/hooks/task-impact.test.tsx @@ -144,6 +144,8 @@ async function start($: any, on: any, options: Options = {}) { const ok = (stdout = '') => ({ value: { exitCode: 0, stdout, stderr: '' } }) const [bin] = argv if (bin === 'altimate' && argv[2] === 'report-event') return ok() + // The health feature's own check runs through the same CLI; it is not an impact lookup. + if (bin === 'altimate' && argv[3] === 'dbt_health') return ok(JSON.stringify({ status: 'ok', output: '', metadata: { model_insights: [] } })) if (bin === 'altimate') { const args = JSON.parse(String(argv[argv.indexOf('--args') + 1] ?? '{}')) if (options.cliFails) return { value: { exitCode: 1, stdout: JSON.stringify({ status: 'error', output: 'no', metadata: {} }), stderr: '' } } From 00c91617c1d85c79cb0054b7d80e90e4184c1a5a Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 11:43:55 +0530 Subject: [PATCH 15/34] fix: the health push gate answers inside the tool chain's deadline, and a deleted schema file still counts its models as touched Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/hooks/features/health.tsx | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 7f1dad80..785d6b9c 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -15,6 +15,7 @@ import type { Args, EngineInterface, EventResult, On, PluginOptions, RenderEleme import type { HealthInsight, HealthState } from '../../types' import { later, runTool, within } from '../altimate-cli' import { setStatusPart } from '../status-line' +import { PHASE_TIMEOUT_MS } from '../tool-chain' import type { BashCall, ContextLines, EditedFile, FileCall } from '../tool-chain' import type { BandUi, FeatureContext } from './index' @@ -24,6 +25,8 @@ type HealthRun = { insights: HealthInsight[] } | { problem: string } const COMMAND = 'health' /** A check can take minutes: the dbt adapter starts, then DataPilot reads the manifest. */ const CHECK_MS = 9 * 60_000 +/** How long the push gate waits for a check: inside the tool chain's phase deadline, past which a gate is given up on and the push runs. */ +const GATE_WAIT_MS = PHASE_TIMEOUT_MS - 1_000 /** How long /health waits for a running check before it answers with what it has. */ const COMMAND_WAIT_MS = 3_000 /** The most issues the push refusal and the report list. */ @@ -47,6 +50,8 @@ let enabled = false let eligible = false /** The problem told to the user last, so the same one is not told at every turn. */ let toldProblem: string | null = null +/** Which model each file belonged to in earlier checks: a deleted schema file or model no longer shows in the latest answer. */ +let known: HealthInsight[] = [] /** Whether the status line carries the health part now. */ let drawn = false /** dbt project roots by folder, null for a folder in no project. */ @@ -520,7 +525,8 @@ async function loop($: EngineInterface, mine: number) { let again = false for (let attempt = 0; ; attempt++) { const asked = new Set((await read($, healthAtom))?.touched ?? []) - const real = await canonical($, [...asked, ...run.insights.flatMap(i => [i.file, i.properties])]) + const seen = [...known, ...run.insights] + const real = await canonical($, [...asked, ...seen.flatMap(i => [i.file, i.properties])]) // A session that restarted meanwhile owns the state now: this check's answer is for the one before. if (mine !== generation) return let late = false @@ -535,7 +541,7 @@ async function loop($: EngineInterface, mine: number) { } // A baseline that lands after edits (the first run failed, or the project was opened above the dbt // project) cannot tell the touched models' old issues from the new: all of theirs count as new. - const edited = modelsOfFiles(base.touched, run.insights, real) + const edited = modelsOfFiles(base.touched, seen, real) const baseline = base.baseline ?? run.insights.filter(i => !edited.has(i.uniqueId)).map(pairKey) const issues = newIssues(run.insights, baseline, edited) return { ...base, baseline, issues, running: false, problem: null } @@ -544,6 +550,8 @@ async function loop($: EngineInterface, mine: number) { } if (again) continue if (mine !== generation) return + const keep = new Set(known.map(i => `${i.uniqueId}\0${i.file}\0${i.properties}`)) + for (const i of run.insights) if (!keep.has(`${i.uniqueId}\0${i.file}\0${i.properties}`)) known.push({ ...i, name: '', message: '', recommendation: '', severity: '' }) toldProblem = null const issues = (await read($, healthAtom))?.issues ?? [] await drawStatus($, issues) @@ -606,6 +614,7 @@ export async function sessionStart($: EngineInterface, e: Args<'session.start'>) running = null enabled = false toldProblem = null + known = [] projects.clear() await update($, healthAtom, () => fresh()) // The status line outlives a process: a part an earlier one left is cleared whether or not this one drew it. @@ -670,7 +679,7 @@ export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: for (const name of filesWrittenBy(e.command)) if (await isModelFile($, name.startsWith('/') || /^[A-Za-z]:/.test(name) ? name : `${root}/${name}`)) return hold(`this command changes the model file ${name} and pushes in one go, so the change is not checked first. Run them as separate commands.`) // With nothing changed this session there is nothing to wait for or hold. if (((await read($, healthAtom))?.touched.length ?? 0) === 0) return null - if ((await wantsCheck($, true)) || running?.generation === generation) await within($, CHECK_MS, kick($)) + if ((await wantsCheck($, true)) || running?.generation === generation) await within($, GATE_WAIT_MS, kick($)) const state = await read($, healthAtom) // A check that failed or did not finish leaves the changes unchecked: a gate that fails open is no gate. if (state && enabled && (state.running || state.pending.length > 0 || state.baseline === null)) From f7ad7adf5ad42534738b24f46c33323326c497c1 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 11:50:42 +0530 Subject: [PATCH 16/34] fix: the health push gate counts its earlier work against the chain's deadline, counts every model when a schema file is deleted, and takes the deadline from a module that imports nothing Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/hooks/deadlines.ts | 5 +++++ plugins/altimate-code/hooks/features/health.tsx | 11 +++++++++-- plugins/altimate-code/hooks/tool-chain.ts | 3 ++- 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 plugins/altimate-code/hooks/deadlines.ts diff --git a/plugins/altimate-code/hooks/deadlines.ts b/plugins/altimate-code/hooks/deadlines.ts new file mode 100644 index 00000000..cc4234d5 --- /dev/null +++ b/plugins/altimate-code/hooks/deadlines.ts @@ -0,0 +1,5 @@ +/** + * How long one tool-chain phase may take, in milliseconds. Kept apart from the chain so a feature can + * bound its own work inside it without importing the chain that imports the feature. + */ +export const PHASE_TIMEOUT_MS = 5_000 diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 785d6b9c..006dfb8e 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -15,7 +15,7 @@ import type { Args, EngineInterface, EventResult, On, PluginOptions, RenderEleme import type { HealthInsight, HealthState } from '../../types' import { later, runTool, within } from '../altimate-cli' import { setStatusPart } from '../status-line' -import { PHASE_TIMEOUT_MS } from '../tool-chain' +import { PHASE_TIMEOUT_MS } from '../deadlines' import type { BashCall, ContextLines, EditedFile, FileCall } from '../tool-chain' import type { BandUi, FeatureContext } from './index' @@ -542,6 +542,10 @@ async function loop($: EngineInterface, mine: number) { // A baseline that lands after edits (the first run failed, or the project was opened above the dbt // project) cannot tell the touched models' old issues from the new: all of theirs count as new. const edited = modelsOfFiles(base.touched, seen, real) + // A deleted schema file names no model any more, and a model that had no insight before has no record + // of it: its new issues are told from the baseline, so every model counts. + const matched = new Set(seen.flatMap(i => [i.file, i.properties]).filter(Boolean).map(real)) + if (base.touched.some(f => /\.ya?ml$/i.test(f) && !matched.has(real(f)))) for (const i of run.insights) edited.add(i.uniqueId) const baseline = base.baseline ?? run.insights.filter(i => !edited.has(i.uniqueId)).map(pairKey) const issues = newIssues(run.insights, baseline, edited) return { ...base, baseline, issues, running: false, problem: null } @@ -671,6 +675,7 @@ export function pushRefusal(blocks: unknown, command: string, issues: readonly H */ export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> { if (!(options.healthBlocksPush === true || options.healthBlocksPush === 'true') || !isGitPush(e.command)) return null + const startedAt = await $.clock.now() const hold = (why: string) => ({ deny: `git push is held: ${why}\nRun /${COMMAND} to check again, or turn off "Project health blocks git push" in the plugin's settings.`, }) @@ -679,7 +684,9 @@ export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: for (const name of filesWrittenBy(e.command)) if (await isModelFile($, name.startsWith('/') || /^[A-Za-z]:/.test(name) ? name : `${root}/${name}`)) return hold(`this command changes the model file ${name} and pushes in one go, so the change is not checked first. Run them as separate commands.`) // With nothing changed this session there is nothing to wait for or hold. if (((await read($, healthAtom))?.touched.length ?? 0) === 0) return null - if ((await wantsCheck($, true)) || running?.generation === generation) await within($, GATE_WAIT_MS, kick($)) + // The wait is what is left of the gate's budget: the work above counts against the chain's deadline too. + const left = GATE_WAIT_MS - ((await $.clock.now()) - startedAt) + if (left > 0 && ((await wantsCheck($, true)) || running?.generation === generation)) await within($, left, kick($)) const state = await read($, healthAtom) // A check that failed or did not finish leaves the changes unchecked: a gate that fails open is no gate. if (state && enabled && (state.running || state.pending.length > 0 || state.baseline === null)) diff --git a/plugins/altimate-code/hooks/tool-chain.ts b/plugins/altimate-code/hooks/tool-chain.ts index 45eea4df..23d55447 100644 --- a/plugins/altimate-code/hooks/tool-chain.ts +++ b/plugins/altimate-code/hooks/tool-chain.ts @@ -11,6 +11,7 @@ import type { On, ToolCallInput, ToolCallResult } from 'claude-code' import { within } from './altimate-cli' +import { PHASE_TIMEOUT_MS } from './deadlines' import { EDIT_READS_BEFORE, featuresBashAfter, @@ -44,7 +45,7 @@ export const PHASES: readonly Phase[] = ['bashRewrite', 'bashGate', 'bashAfter', * is given up on and the call runs. A gate that must hold the call therefore bounds its own check well inside * this and returns `{ deny }` when the check runs out of time. */ -export const PHASE_TIMEOUT_MS = 5_000 +export { PHASE_TIMEOUT_MS } const EDIT_TOOLS = new Set(['Edit', 'Write', 'MultiEdit']) From 5d1f513d21c61b83360eb0c0872ca9f0a49e3db6 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 12:45:10 +0530 Subject: [PATCH 17/34] fix: a deleted shared schema file counts every model, those without an earlier insight too Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/hooks/features/health.tsx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 006dfb8e..3411ba09 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -542,9 +542,10 @@ async function loop($: EngineInterface, mine: number) { // A baseline that lands after edits (the first run failed, or the project was opened above the dbt // project) cannot tell the touched models' old issues from the new: all of theirs count as new. const edited = modelsOfFiles(base.touched, seen, real) - // A deleted schema file names no model any more, and a model that had no insight before has no record - // of it: its new issues are told from the baseline, so every model counts. - const matched = new Set(seen.flatMap(i => [i.file, i.properties]).filter(Boolean).map(real)) + // A schema file the latest answer no longer names was deleted (or never mattered): its models, the ones + // that had no insight before included, are not found by their files, and new issues are told from the + // baseline, so every model counts. + const matched = new Set(run.insights.flatMap(i => [i.file, i.properties]).filter(Boolean).map(real)) if (base.touched.some(f => /\.ya?ml$/i.test(f) && !matched.has(real(f)))) for (const i of run.insights) edited.add(i.uniqueId) const baseline = base.baseline ?? run.insights.filter(i => !edited.has(i.uniqueId)).map(pairKey) const issues = newIssues(run.insights, baseline, edited) From 3cce2c058a769e31032d5de9a9ce5245205ec709 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 12:49:46 +0530 Subject: [PATCH 18/34] fix: the health push gate measures its remaining wait after its last state read Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/hooks/features/health.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 3411ba09..be3ffa38 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -686,8 +686,10 @@ export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: // With nothing changed this session there is nothing to wait for or hold. if (((await read($, healthAtom))?.touched.length ?? 0) === 0) return null // The wait is what is left of the gate's budget: the work above counts against the chain's deadline too. - const left = GATE_WAIT_MS - ((await $.clock.now()) - startedAt) - if (left > 0 && ((await wantsCheck($, true)) || running?.generation === generation)) await within($, left, kick($)) + if ((await wantsCheck($, true)) || running?.generation === generation) { + const left = GATE_WAIT_MS - ((await $.clock.now()) - startedAt) + if (left > 0) await within($, left, kick($)) + } const state = await read($, healthAtom) // A check that failed or did not finish leaves the changes unchecked: a gate that fails open is no gate. if (state && enabled && (state.running || state.pending.length > 0 || state.baseline === null)) From cc4d687360e624b5daf361daf1acb6558355f345 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 13:53:02 +0530 Subject: [PATCH 19/34] fix: project health keeps the baseline when no schema file is named, rejects a config path outside the project, tells Claude to report held issues, and documents its write call Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/README.md | 2 +- .../altimate-code/hooks/features/health.tsx | 18 +++++++++++++++--- plugins/altimate-code/hooks/health.test.tsx | 7 +++++-- plugins/altimate-code/hooks/tool-chain.ts | 1 - 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/plugins/altimate-code/README.md b/plugins/altimate-code/README.md index e2594739..621539ca 100644 --- a/plugins/altimate-code/README.md +++ b/plugins/altimate-code/README.md @@ -52,7 +52,7 @@ If altimate-code is not on PATH or not configured, the plugin surfaces the failu 1. A **SessionStart hook** adds a description of the available tools and both modes to the session, and copies the linked workspace's skills (see below). It does not start the MCP server. In a linked project, the [team rules](#team-rules) hooks add the workspace's rules too. 2. **MCP mode.** Claude calls `warehouse_list`, `warehouse_add`, `sql_execute` (`tables` returns columns and value profiles; large results spill to a local file), `sql_materialize` (session-local `scratch` DuckDB for cross-database joins), `answer_commit`, and `impact_analysis`, `dbt_lineage`, `altimate_core_column_lineage`, `schema_detect_pii`, `data_diff`, `altimate_core_equivalence`, `sql_translate`. 3. **CLI mode.** The **skill** has Claude run `altimate tool context` once (connections, schema listing, analyst method), then `altimate tool run --args ''` through Bash and reason over the structured output. `altimate tool list --json` shows every tool and its input schema. A warm process per project starts automatically. -4. Calls are read-only unless you approve `--allow-write` (CLI mode); `DROP DATABASE`, `DROP SCHEMA` and `TRUNCATE` are always blocked. Read-only is enforced by the database on Postgres, SQLite and DuckDB and by SQL classification elsewhere, so use a read-only DB user for the strongest boundary. +4. Calls are read-only unless you approve `--allow-write` (CLI mode), except the project health check: it runs `dbt_health` with write, because the dbt project's integration refreshes `target/`, so it starts only in an interactive session (or with `healthBlocksPush` on) that is inside a dbt project; `DROP DATABASE`, `DROP SCHEMA` and `TRUNCATE` are always blocked. Read-only is enforced by the database on Postgres, SQLite and DuckDB and by SQL classification elsewhere, so use a read-only DB user for the strongest boundary. 5. For long multi-step jobs, or when you invoke **`/altimate-code:altimate `** for work that needs the agent, Claude Code runs `altimate run ""` locally with the `builder` agent (or `analyst` for a data question when MCP is unavailable), which uses your configured LLM provider. 6. Warehouse queries and dbt commands run **locally under your credentials**. If the installed CLI predates the `tool` subcommand, the skill falls back to delegation. diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index be3ffa38..9b66332a 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -416,6 +416,15 @@ export function toolArgs(configPath: unknown): Record { return config ? { config_path: config } : {} } +/** Why a `healthConfigPath` setting can't be used: it must stay inside the dbt project. Null when it can. */ +export function configPathProblem(configPath: unknown): string | null { + const config = typeof configPath === 'string' ? configPath.trim() : '' + if (!config) return null + if (/^([\\/]|[A-Za-z]:)/.test(config) || config.split(/[\\/]/).includes('..')) + return 'Project health: the "Project health check config" setting must be a path inside the dbt project (relative, without ..).' + return null +} + /** The dbt project folder above `dir` (up to 40 levels), cached; null when there is none. */ async function projectAbove($: EngineInterface, dir: string): Promise { const seen: string[] = [] @@ -460,6 +469,8 @@ async function isModelFile($: EngineInterface, file: string): Promise { /** Asks altimate-code's dbt_health for the project's insights. */ async function check($: EngineInterface): Promise { const until = (await $.clock.now()) + CHECK_MS + const badConfig = configPathProblem(options.healthConfigPath) + if (badConfig) return { problem: badConfig } let result: Awaited> try { result = await runTool($, 'dbt_health', toolArgs(options.healthConfigPath), { write: true, until }) @@ -546,7 +557,8 @@ async function loop($: EngineInterface, mine: number) { // that had no insight before included, are not found by their files, and new issues are told from the // baseline, so every model counts. const matched = new Set(run.insights.flatMap(i => [i.file, i.properties]).filter(Boolean).map(real)) - if (base.touched.some(f => /\.ya?ml$/i.test(f) && !matched.has(real(f)))) for (const i of run.insights) edited.add(i.uniqueId) + // Not when there is no baseline yet: counting every model as edited would make the baseline empty, and every existing issue new. + if (base.baseline !== null && base.touched.some(f => /\.ya?ml$/i.test(f) && !matched.has(real(f)))) for (const i of run.insights) edited.add(i.uniqueId) const baseline = base.baseline ?? run.insights.filter(i => !edited.has(i.uniqueId)).map(pairKey) const issues = newIssues(run.insights, baseline, edited) return { ...base, baseline, issues, running: false, problem: null } @@ -666,7 +678,7 @@ export async function bashRewrite($: EngineInterface, e: BashCall): Promise ({ - deny: `git push is held: ${why}\nRun /${COMMAND} to check again, or turn off "Project health blocks git push" in the plugin's settings.`, + deny: `git push is held: ${why}\nRun /${COMMAND} to check again; only the user can turn off "Project health blocks git push" in the plugin's settings.`, }) // An edit in the same command would run before any check of it. const root = await $.session.root().catch(() => '') diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index 201a7ccc..0cc1818c 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -1,6 +1,6 @@ import { expect, mock, test } from 'claude-code/testing' -import { bandText, canonical, filesWrittenBy, isGitPush, modelsOfFiles, newIssues, pairKey, pushRefusal, reportText, simpleCommands, toolArgs } from './features/health' +import { bandText, canonical, configPathProblem, filesWrittenBy, isGitPush, modelsOfFiles, newIssues, pairKey, pushRefusal, reportText, simpleCommands, toolArgs } from './features/health' const ROOT = '/repo' const STG = `${ROOT}/models/staging/stg_orders.sql` @@ -416,6 +416,9 @@ test('the configured config path is passed to the tool, empty means the defaults expect(toolArgs(' datapilot.yml ')).toEqual({ config_path: 'datapilot.yml' }) expect(toolArgs('')).toEqual({}) expect(toolArgs(undefined)).toEqual({}) + expect(configPathProblem('datapilot.yml')).toBeNull() + expect(configPathProblem('')).toBeNull() + for (const bad of ['/etc/hosts', 'C:\\x.yml', '../x.yml', 'a/../../x.yml']) expect(configPathProblem(bad)).toContain('inside the dbt project') }) test('git push is found by its command words, not by its text', () => { @@ -612,7 +615,7 @@ test('an edit that keeps landing while the folders resolve is never folded into }) const files = [`${ROOT}/models/staging/late1.sql`, `${ROOT}/models/staging/late2.sql`, `${ROOT}/models/staging/late3.sql`, STG] for (const file of files) { - while (held.length === 0) await new Promise(resolve => setTimeout(resolve, 5)) + while (held.length === 0) await new Promise(resolve => (globalThis as any).setTimeout(resolve, 5)) gate = false await $.tool.call(edit(file)) gate = true diff --git a/plugins/altimate-code/hooks/tool-chain.ts b/plugins/altimate-code/hooks/tool-chain.ts index bc234577..eed1930b 100644 --- a/plugins/altimate-code/hooks/tool-chain.ts +++ b/plugins/altimate-code/hooks/tool-chain.ts @@ -45,7 +45,6 @@ export const PHASES: readonly Phase[] = ['bashRewrite', 'bashGate', 'bashAfter', * is given up on and the call runs. A gate that must hold the call therefore bounds its own check well inside * this and returns `{ deny }` when the check runs out of time. */ -export { PHASE_TIMEOUT_MS } const EDIT_TOOLS = new Set(['Edit', 'Write', 'MultiEdit']) From 59f84f2c1658e5e193bc06198546b250151d41d5 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 14:11:00 +0530 Subject: [PATCH 20/34] fix: project health treats a result the engine cut at its size limit as a failed check, and counts only edits to the session's own dbt project Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/hooks/features/health.tsx | 10 ++++++++-- plugins/altimate-code/hooks/health.test.tsx | 13 +++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 9b66332a..2b199c05 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -482,6 +482,8 @@ async function check($: EngineInterface): Promise { if (result.ok && result.metadata?.success === true && Array.isArray(insights)) { // Insights read from the last good manifest are not a check of the changes just made. const stale = result.metadata?.stale_manifest as { message?: unknown } | undefined + // A result cut at the engine's size limit leaves the changed models' issues unknown: that is no check. + if (result.metadata?.truncated === true) return { problem: 'Project health: the project has too many health insights for one check to read, so the changes could not be checked.' } if (stale) return { problem: `Project health: ${typeof stale.message === 'string' ? stale.message : 'dbt could not parse the project, so the last good manifest was read.'}` } return { insights: insights as HealthInsight[] } } @@ -718,8 +720,12 @@ export async function bashAfter($: EngineInterface, e: BashCall, result: ToolCal async function markChanged($: EngineInterface, file: string) { const path = norm(file) if (!(await isModelFile($, path))) return - // A session that opened outside a dbt project checks from its first change to one. - if (eligible && !enabled) enabled = true + // The CLI finds its project from the session's folder: a model of another project (a nested one, or one beside + // the session's) is not covered by its check, so it is not counted as checked. + const root = await $.session.root().catch(() => '') + const own = root ? await projectAbove($, norm(root)) : null + const mine = await projectAbove($, path.replace(/\/[^/]*$/, '')) + if (own === null || mine === null || norm(own) !== norm(mine)) return await update($, healthAtom, prev => { const base = prev ?? fresh() return { diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index 0cc1818c..b36a1291 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -36,7 +36,7 @@ const DEP = { ...insight('base', `${ROOT}/dbt_packages/dep/models/base.sql`, 'Mi type Options = { /** The tool's answer per run (the last repeats). */ - runs: (ReturnType[] | { fail: string } | { stale: string })[] + runs: (ReturnType[] | { fail: string } | { stale: string } | { truncated: true })[] /** Holds the run with this index (from 0) until released. */ hold?: { at: number; until: Promise } /** Folders that hold a dbt_project.yml (default: the repo). */ @@ -100,7 +100,7 @@ async function start($: any, on: any, options: Options, session: { isInteractive if (options.hold?.at === index) await options.hold.until const answer = options.runs[Math.min(index, options.runs.length - 1)]! const metadata = - 'fail' in answer ? { success: false, insights: [] } : 'stale' in answer ? { success: true, insights: [OLD_STG], stale_manifest: { message: answer.stale } } : { success: true, insights: answer } + 'fail' in answer ? { success: false, insights: [] } : 'truncated' in answer ? { success: true, insights: [], truncated: true, total: 9999 } : 'stale' in answer ? { success: true, insights: [OLD_STG], stale_manifest: { message: answer.stale } } : { success: true, insights: answer } const output = 'fail' in answer ? answer.fail : '' return { value: { exitCode: 0, stdout: JSON.stringify({ status: 'ok', output, metadata }), stderr: '' } } }) @@ -411,6 +411,15 @@ test('with the push gate on, stale insights (dbt could not parse) are a failed c expect(denied?.deny).toContain('dbt could not parse the project') }) +test('with the push gate on, a result the engine cut at its size limit is a failed check, not a clean one', { options: { healthBlocksPush: true } }, async ($, on) => { + const { clock, toasts } = await start($, on, { runs: [[], { truncated: true }] }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + expect(toasts.at(-1)).toContain('too many health insights') + const denied: any = await $.tool.call(command('git push')) + expect(denied?.deny).toContain('too many health insights') +}) + test('the configured config path is passed to the tool, empty means the defaults', () => { expect(toolArgs('datapilot.yml')).toEqual({ config_path: 'datapilot.yml' }) expect(toolArgs(' datapilot.yml ')).toEqual({ config_path: 'datapilot.yml' }) From f8104bd09384aef104dc7e61ce328db1a4d63469 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 14:15:31 +0530 Subject: [PATCH 21/34] docs: project health states what it cannot see of a shell's working folder and which project it covers Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugins/altimate-code/README.md b/plugins/altimate-code/README.md index 621539ca..6702bdd8 100644 --- a/plugins/altimate-code/README.md +++ b/plugins/altimate-code/README.md @@ -146,6 +146,8 @@ The plugin has five settings, shown when you enable it (`userConfig` in `.claude | `commentsPollSeconds` | 900 | How often the team's comments on shared docs are fetched again (at least 30). | | `commentsInContext` | on | Whether the open comments on a model Claude reads or edits are added to its context. | +Project health follows edits Claude makes with Edit/Write and the files a Bash command names. It reads shell commands heuristically, and a Bash call carries no working folder of its own: a relative path written after an earlier call changed folder (`cd models` in one call, `sed -i x.sql` in the next) is read from the session's folder and can miss the model. It also covers only the dbt project the session's folder is in, and a result too large for the engine to return whole counts as a failed check. + ## Example prompts Once installed, try these in Claude Code (or prefix any of them with `/altimate-code:altimate`): From eb08fe736a26c0f4a6629ae18c16a758bb2eba7c Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 16:21:12 +0530 Subject: [PATCH 22/34] fix: project health settings and README say the check is live, a nested or symlinked project is told apart by its real folder, and the session root is read once per edit Co-Authored-By: Claude Sonnet 5.5 --- .../altimate-code/.claude-plugin/plugin.json | 6 ++--- plugins/altimate-code/README.md | 2 +- .../altimate-code/hooks/features/health.tsx | 12 +++++----- plugins/altimate-code/hooks/health.test.tsx | 22 +++++++++++++++++++ 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/plugins/altimate-code/.claude-plugin/plugin.json b/plugins/altimate-code/.claude-plugin/plugin.json index 5a71365a..b1b6ef8e 100644 --- a/plugins/altimate-code/.claude-plugin/plugin.json +++ b/plugins/altimate-code/.claude-plugin/plugin.json @@ -31,14 +31,14 @@ }, "healthBlocksPush": { "type": "boolean", - "title": "Project health blocks git push (not used yet)", - "description": "Not used yet: the project health check isn't in this version, so git push is never refused. When it ships, on refuses a push while the check finds new issues in the models you changed; off only shows them.", + "title": "Project health blocks git push", + "description": "On refuses a git push while the project health check finds new issues in the models you changed this session; off only shows them.", "default": false }, "healthConfigPath": { "type": "string", "title": "Project health check config", - "description": "Not used yet: the project health check isn't in this version. Path to a DataPilot health check config file, relative to the dbt project. Empty uses the default checks.", + "description": "Path to a DataPilot health check config file, relative to the dbt project. Empty uses the default checks.", "default": "" }, "commentsPollSeconds": { diff --git a/plugins/altimate-code/README.md b/plugins/altimate-code/README.md index 6702bdd8..de031220 100644 --- a/plugins/altimate-code/README.md +++ b/plugins/altimate-code/README.md @@ -136,7 +136,7 @@ Rule drafting uses altimate-code's own model (sign in to the Altimate LLM Gatewa ## Settings -The plugin has five settings, shown when you enable it (`userConfig` in `.claude-plugin/plugin.json`). None is used yet: the features that read them are not in this version, so changing one changes nothing. The table says what each will control. +The plugin has five settings, shown when you enable it (`userConfig` in `.claude-plugin/plugin.json`). `healthBlocksPush` and `healthConfigPath` are used by the project health check. `maxScanBytes` and the comments settings are not used yet: the features that read them are not in this version, so changing one changes nothing. The table says what each controls. | Setting | Default | What it controls | | --- | --- | --- | diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 2b199c05..5d8ada63 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -714,18 +714,20 @@ export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: /** A model file Bash writes (a `sed -i`, a redirect, a `git checkout -- file`) is checked like one Claude edited. */ export async function bashAfter($: EngineInterface, e: BashCall, result: ToolCallResult): Promise { const root = await $.session.root().catch(() => '') - for (const name of filesWrittenBy(e.command)) await markChanged($, name.startsWith('/') || /^[A-Za-z]:/.test(name) ? name : `${root}/${name}`) + for (const name of filesWrittenBy(e.command)) await markChanged($, name.startsWith('/') || /^[A-Za-z]:/.test(name) ? name : `${root}/${name}`, root) } -async function markChanged($: EngineInterface, file: string) { +async function markChanged($: EngineInterface, file: string, root: string) { const path = norm(file) if (!(await isModelFile($, path))) return // The CLI finds its project from the session's folder: a model of another project (a nested one, or one beside // the session's) is not covered by its check, so it is not counted as checked. - const root = await $.session.root().catch(() => '') const own = root ? await projectAbove($, norm(root)) : null const mine = await projectAbove($, path.replace(/\/[^/]*$/, '')) - if (own === null || mine === null || norm(own) !== norm(mine)) return + if (own === null || mine === null) return + // Compared as the system names the folders, the way `canonical` matches an edit to an insight's file. + const real = await canonical($, [`${own}/x`, `${mine}/x`]) + if (real(`${own}/x`) !== real(`${mine}/x`)) return await update($, healthAtom, prev => { const base = prev ?? fresh() return { @@ -741,7 +743,7 @@ async function markChanged($: EngineInterface, file: string) { /** A model file Claude changed is checked at the end of the turn. */ export async function editAfter($: EngineInterface, e: FileCall, result: ToolCallResult, file: EditedFile): Promise { const root = await $.session.root().catch(() => '') - await markChanged($, file.path.startsWith('/') || /^[A-Za-z]:/.test(file.path) ? file.path : `${root}/${file.path}`) + await markChanged($, file.path.startsWith('/') || /^[A-Za-z]:/.test(file.path) ? file.path : `${root}/${file.path}`, root) } export async function readOrEditAfter($: EngineInterface, e: FileCall, result: ToolCallResult, file: { path: string }): Promise {} diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index b36a1291..a3e23dd8 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -152,6 +152,28 @@ test('a model inside an installed package with its own dbt_project.yml is not a expect(calls).toHaveLength(1) }) +test('a model of a project nested in the session\'s own is not counted as checked: no second check runs', async ($, on) => { + const { clock, calls } = await start($, on, { runs: [[OLD_STG], [OLD_STG, NEW_STG]], projects: [ROOT, `${ROOT}/nested`] }) + await $.tool.call(edit(`${ROOT}/nested/models/x.sql`)) + await endTurn($, clock, 't1') + expect(calls).toHaveLength(1) +}) + +test('an edit spelled by the real path of the session\'s symlinked project is still an edit of its own project', async ($, on) => { + const real = '/private/shop' + const file = `${real}/models/staging/stg_orders.sql` + const stg = { ...NEW_STG, file, properties: '' } + const old = { ...OLD_STG, file, properties: '' } + const { clock, toasts } = await start($, on, { + runs: [[old], [old, stg]], + projects: [ROOT, real], + realFolders: { [ROOT]: real, [real]: real }, + }) + await $.tool.call(edit(file)) + await endTurn($, clock, 't1') + expect(toasts).toEqual(['◆ 1 new health issue · stg_orders: hard coded references · /health']) +}) + test('README, dbt_project.yml, profiles, CI and dbt_packages files are not model files: no check runs', async ($, on) => { const { clock, calls, toasts } = await start($, on, { runs: [[OLD_STG], [OLD_STG, NEW_STG]] }) for (const file of [NOTES, `${ROOT}/dbt_project.yml`, `${ROOT}/profiles.yml`, `${ROOT}/.github/workflows/ci.yml`, `${ROOT}/dbt_packages/dep/models/x.sql`, `${ROOT}/target/compiled/x.sql`]) From 4d2b7d5eebe9ec559deed9518627a7411a923c09 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Tue, 17 Nov 2026 21:07:21 +0530 Subject: [PATCH 23/34] Keep to health's own files: shared files match main; health bounds its own push-gate wait Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/.claude-plugin/plugin.json | 6 +++--- plugins/altimate-code/README.md | 6 ++---- plugins/altimate-code/hooks/deadlines.ts | 5 ----- plugins/altimate-code/hooks/features/health.tsx | 4 ++-- plugins/altimate-code/hooks/features/index.test.ts | 3 +-- plugins/altimate-code/hooks/lineage.test.tsx | 2 -- plugins/altimate-code/hooks/task-impact.test.tsx | 2 -- plugins/altimate-code/hooks/tool-chain.ts | 2 +- 8 files changed, 9 insertions(+), 21 deletions(-) delete mode 100644 plugins/altimate-code/hooks/deadlines.ts diff --git a/plugins/altimate-code/.claude-plugin/plugin.json b/plugins/altimate-code/.claude-plugin/plugin.json index b1b6ef8e..5a71365a 100644 --- a/plugins/altimate-code/.claude-plugin/plugin.json +++ b/plugins/altimate-code/.claude-plugin/plugin.json @@ -31,14 +31,14 @@ }, "healthBlocksPush": { "type": "boolean", - "title": "Project health blocks git push", - "description": "On refuses a git push while the project health check finds new issues in the models you changed this session; off only shows them.", + "title": "Project health blocks git push (not used yet)", + "description": "Not used yet: the project health check isn't in this version, so git push is never refused. When it ships, on refuses a push while the check finds new issues in the models you changed; off only shows them.", "default": false }, "healthConfigPath": { "type": "string", "title": "Project health check config", - "description": "Path to a DataPilot health check config file, relative to the dbt project. Empty uses the default checks.", + "description": "Not used yet: the project health check isn't in this version. Path to a DataPilot health check config file, relative to the dbt project. Empty uses the default checks.", "default": "" }, "commentsPollSeconds": { diff --git a/plugins/altimate-code/README.md b/plugins/altimate-code/README.md index de031220..e2594739 100644 --- a/plugins/altimate-code/README.md +++ b/plugins/altimate-code/README.md @@ -52,7 +52,7 @@ If altimate-code is not on PATH or not configured, the plugin surfaces the failu 1. A **SessionStart hook** adds a description of the available tools and both modes to the session, and copies the linked workspace's skills (see below). It does not start the MCP server. In a linked project, the [team rules](#team-rules) hooks add the workspace's rules too. 2. **MCP mode.** Claude calls `warehouse_list`, `warehouse_add`, `sql_execute` (`tables` returns columns and value profiles; large results spill to a local file), `sql_materialize` (session-local `scratch` DuckDB for cross-database joins), `answer_commit`, and `impact_analysis`, `dbt_lineage`, `altimate_core_column_lineage`, `schema_detect_pii`, `data_diff`, `altimate_core_equivalence`, `sql_translate`. 3. **CLI mode.** The **skill** has Claude run `altimate tool context` once (connections, schema listing, analyst method), then `altimate tool run --args ''` through Bash and reason over the structured output. `altimate tool list --json` shows every tool and its input schema. A warm process per project starts automatically. -4. Calls are read-only unless you approve `--allow-write` (CLI mode), except the project health check: it runs `dbt_health` with write, because the dbt project's integration refreshes `target/`, so it starts only in an interactive session (or with `healthBlocksPush` on) that is inside a dbt project; `DROP DATABASE`, `DROP SCHEMA` and `TRUNCATE` are always blocked. Read-only is enforced by the database on Postgres, SQLite and DuckDB and by SQL classification elsewhere, so use a read-only DB user for the strongest boundary. +4. Calls are read-only unless you approve `--allow-write` (CLI mode); `DROP DATABASE`, `DROP SCHEMA` and `TRUNCATE` are always blocked. Read-only is enforced by the database on Postgres, SQLite and DuckDB and by SQL classification elsewhere, so use a read-only DB user for the strongest boundary. 5. For long multi-step jobs, or when you invoke **`/altimate-code:altimate `** for work that needs the agent, Claude Code runs `altimate run ""` locally with the `builder` agent (or `analyst` for a data question when MCP is unavailable), which uses your configured LLM provider. 6. Warehouse queries and dbt commands run **locally under your credentials**. If the installed CLI predates the `tool` subcommand, the skill falls back to delegation. @@ -136,7 +136,7 @@ Rule drafting uses altimate-code's own model (sign in to the Altimate LLM Gatewa ## Settings -The plugin has five settings, shown when you enable it (`userConfig` in `.claude-plugin/plugin.json`). `healthBlocksPush` and `healthConfigPath` are used by the project health check. `maxScanBytes` and the comments settings are not used yet: the features that read them are not in this version, so changing one changes nothing. The table says what each controls. +The plugin has five settings, shown when you enable it (`userConfig` in `.claude-plugin/plugin.json`). None is used yet: the features that read them are not in this version, so changing one changes nothing. The table says what each will control. | Setting | Default | What it controls | | --- | --- | --- | @@ -146,8 +146,6 @@ The plugin has five settings, shown when you enable it (`userConfig` in `.claude | `commentsPollSeconds` | 900 | How often the team's comments on shared docs are fetched again (at least 30). | | `commentsInContext` | on | Whether the open comments on a model Claude reads or edits are added to its context. | -Project health follows edits Claude makes with Edit/Write and the files a Bash command names. It reads shell commands heuristically, and a Bash call carries no working folder of its own: a relative path written after an earlier call changed folder (`cd models` in one call, `sed -i x.sql` in the next) is read from the session's folder and can miss the model. It also covers only the dbt project the session's folder is in, and a result too large for the engine to return whole counts as a failed check. - ## Example prompts Once installed, try these in Claude Code (or prefix any of them with `/altimate-code:altimate`): diff --git a/plugins/altimate-code/hooks/deadlines.ts b/plugins/altimate-code/hooks/deadlines.ts deleted file mode 100644 index cc4234d5..00000000 --- a/plugins/altimate-code/hooks/deadlines.ts +++ /dev/null @@ -1,5 +0,0 @@ -/** - * How long one tool-chain phase may take, in milliseconds. Kept apart from the chain so a feature can - * bound its own work inside it without importing the chain that imports the feature. - */ -export const PHASE_TIMEOUT_MS = 5_000 diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 5d8ada63..464f0c15 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -15,7 +15,6 @@ import type { Args, EngineInterface, EventResult, On, PluginOptions, RenderEleme import type { HealthInsight, HealthState } from '../../types' import { later, runTool, within } from '../altimate-cli' import { setStatusPart } from '../status-line' -import { PHASE_TIMEOUT_MS } from '../deadlines' import type { BashCall, ContextLines, EditedFile, FileCall } from '../tool-chain' import type { BandUi, FeatureContext } from './index' @@ -26,7 +25,8 @@ const COMMAND = 'health' /** A check can take minutes: the dbt adapter starts, then DataPilot reads the manifest. */ const CHECK_MS = 9 * 60_000 /** How long the push gate waits for a check: inside the tool chain's phase deadline, past which a gate is given up on and the push runs. */ -const GATE_WAIT_MS = PHASE_TIMEOUT_MS - 1_000 +// The tool chain gives a phase 5 s; the push gate waits inside that. Kept here, not imported, to avoid an import cycle with the chain. +const GATE_WAIT_MS = 4_000 /** How long /health waits for a running check before it answers with what it has. */ const COMMAND_WAIT_MS = 3_000 /** The most issues the push refusal and the report list. */ diff --git a/plugins/altimate-code/hooks/features/index.test.ts b/plugins/altimate-code/hooks/features/index.test.ts index 22d14a6d..00a494a3 100644 --- a/plugins/altimate-code/hooks/features/index.test.ts +++ b/plugins/altimate-code/hooks/features/index.test.ts @@ -54,6 +54,5 @@ test('with every feature a stub, each phase runs through the features and logs n expect(await featuresBashAfter($, bash, {} as any)).toEqual([]) expect(await featuresEditAfter($, file, {} as any, { path: '/repo/models/a.sql', before: null })).toEqual([]) expect(await featuresReadOrEditAfter($, file, {} as any, { path: '/repo/models/a.sql' })).toEqual([]) - // health is implemented and needs a session's state, which this bare engine has none of; its own tests cover it. - expect(logs.filter(line => !line.startsWith('health · '))).toEqual([]) + expect(logs).toEqual([]) }) diff --git a/plugins/altimate-code/hooks/lineage.test.tsx b/plugins/altimate-code/hooks/lineage.test.tsx index b4a41546..daafc11d 100644 --- a/plugins/altimate-code/hooks/lineage.test.tsx +++ b/plugins/altimate-code/hooks/lineage.test.tsx @@ -194,8 +194,6 @@ async function start($: any, on: any, options: Options = {}) { if (command === 'tool' && tool === 'dbt_lineage' && options.holdCliLineage) await options.holdCliLineage // The integrations half of the plugin asks the CLI for its status at session start. if (command !== 'tool') return { value: { exitCode: 1, stdout: '', stderr: 'not this test' } } - // The project health check the mod starts at session start is not part of these tests. - if (tool === 'dbt_health') return { value: { exitCode: 0, stdout: JSON.stringify({ status: 'ok', output: '', metadata: { success: true, insights: [] } }), stderr: '' } } const args = JSON.parse(String(e.argv[e.argv.indexOf('--args') + 1] ?? '{}')) calls.push(`cli:${tool}${args.include_graph ? ':graph' : ''}${args.column ? `:${args.column}` : ''}`) // A column that fails, or answers late, as the MCP server does in the tests below. diff --git a/plugins/altimate-code/hooks/task-impact.test.tsx b/plugins/altimate-code/hooks/task-impact.test.tsx index 12277285..bea1a61d 100644 --- a/plugins/altimate-code/hooks/task-impact.test.tsx +++ b/plugins/altimate-code/hooks/task-impact.test.tsx @@ -144,8 +144,6 @@ async function start($: any, on: any, options: Options = {}) { const ok = (stdout = '') => ({ value: { exitCode: 0, stdout, stderr: '' } }) const [bin] = argv if (bin === 'altimate' && argv[2] === 'report-event') return ok() - // The health feature's own check runs through the same CLI; it is not an impact lookup. - if (bin === 'altimate' && argv[3] === 'dbt_health') return ok(JSON.stringify({ status: 'ok', output: '', metadata: { model_insights: [] } })) if (bin === 'altimate') { const args = JSON.parse(String(argv[argv.indexOf('--args') + 1] ?? '{}')) if (options.cliFails) return { value: { exitCode: 1, stdout: JSON.stringify({ status: 'error', output: 'no', metadata: {} }), stderr: '' } } diff --git a/plugins/altimate-code/hooks/tool-chain.ts b/plugins/altimate-code/hooks/tool-chain.ts index eed1930b..0f2e9010 100644 --- a/plugins/altimate-code/hooks/tool-chain.ts +++ b/plugins/altimate-code/hooks/tool-chain.ts @@ -11,7 +11,6 @@ import type { On, ToolCallInput, ToolCallResult } from 'claude-code' import { within } from './altimate-cli' -import { PHASE_TIMEOUT_MS } from './deadlines' import { EDIT_READS_BEFORE, featuresBashAfter, @@ -45,6 +44,7 @@ export const PHASES: readonly Phase[] = ['bashRewrite', 'bashGate', 'bashAfter', * is given up on and the call runs. A gate that must hold the call therefore bounds its own check well inside * this and returns `{ deny }` when the check runs out of time. */ +export const PHASE_TIMEOUT_MS = 5_000 const EDIT_TOOLS = new Set(['Edit', 'Write', 'MultiEdit']) From bbac47429f8a90a3bf88f4590dd2865bfd132d54 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 07:03:47 +0530 Subject: [PATCH 24/34] test(health): pin the push gate's wait below the tool chain's phase deadline Co-Authored-By: Claude Opus 5.5 --- plugins/altimate-code/hooks/features/health.tsx | 2 +- plugins/altimate-code/hooks/health.test.tsx | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 464f0c15..4f7cea84 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -26,7 +26,7 @@ const COMMAND = 'health' const CHECK_MS = 9 * 60_000 /** How long the push gate waits for a check: inside the tool chain's phase deadline, past which a gate is given up on and the push runs. */ // The tool chain gives a phase 5 s; the push gate waits inside that. Kept here, not imported, to avoid an import cycle with the chain. -const GATE_WAIT_MS = 4_000 +export const GATE_WAIT_MS = 4_000 /** How long /health waits for a running check before it answers with what it has. */ const COMMAND_WAIT_MS = 3_000 /** The most issues the push refusal and the report list. */ diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index a3e23dd8..e5b3507b 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -1,6 +1,7 @@ import { expect, mock, test } from 'claude-code/testing' -import { bandText, canonical, configPathProblem, filesWrittenBy, isGitPush, modelsOfFiles, newIssues, pairKey, pushRefusal, reportText, simpleCommands, toolArgs } from './features/health' +import { PHASE_TIMEOUT_MS } from './tool-chain' +import { GATE_WAIT_MS, bandText, canonical, configPathProblem, filesWrittenBy, isGitPush, modelsOfFiles, newIssues, pairKey, pushRefusal, reportText, simpleCommands, toolArgs } from './features/health' const ROOT = '/repo' const STG = `${ROOT}/models/staging/stg_orders.sql` @@ -703,3 +704,7 @@ test('the band line and the report', () => { expect(bandText([])).toBe('') expect(reportText([NEW_STG])).toBe('- stg_orders: hard coded references (warning) stg_orders reads raw.orders directly → Fix Hard coded references') }) + +test('the push gate waits for less than the tool chain gives a phase, so a held push is never given up on', () => { + expect(GATE_WAIT_MS).toBeLessThan(PHASE_TIMEOUT_MS) +}) From ce2011c5b4982832fb01e547b2f3fd6c5fc4e89f Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 07:11:15 +0530 Subject: [PATCH 25/34] fix(health): a model dropped from a schema file that still documents others keeps its new issues on the push gate Co-Authored-By: Claude Opus 5.5 --- plugins/altimate-code/hooks/features/health.tsx | 2 ++ plugins/altimate-code/hooks/health.test.tsx | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 4f7cea84..1e57134e 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -561,6 +561,8 @@ async function loop($: EngineInterface, mine: number) { const matched = new Set(run.insights.flatMap(i => [i.file, i.properties]).filter(Boolean).map(real)) // Not when there is no baseline yet: counting every model as edited would make the baseline empty, and every existing issue new. if (base.baseline !== null && base.touched.some(f => /\.ya?ml$/i.test(f) && !matched.has(real(f)))) for (const i of run.insights) edited.add(i.uniqueId) + // A model dropped from a schema file that still documents others shows its new issues with no properties file: they are not found by that file either. + if (base.baseline !== null && base.touched.some(f => /\.ya?ml$/i.test(f))) for (const i of run.insights) if (!i.properties && i.file) edited.add(i.uniqueId) const baseline = base.baseline ?? run.insights.filter(i => !edited.has(i.uniqueId)).map(pairKey) const issues = newIssues(run.insights, baseline, edited) return { ...base, baseline, issues, running: false, problem: null } diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index e5b3507b..2811eff7 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -33,7 +33,7 @@ const OLD_FCT = insight('fct_orders', FCT, 'Missing tests') const NEW_STG = insight('stg_orders', STG, 'Hard coded references', 'stg_orders reads raw.orders directly', STG_YML) const NEW_FCT = insight('fct_orders', FCT, 'Model fanout', 'fct_orders has 4 direct children') // A model of a dependency package: it has no files in the project. -const DEP = { ...insight('base', `${ROOT}/dbt_packages/dep/models/base.sql`, 'Missing tests'), uniqueId: 'model.dep.base', path: 'models/base.sql' } +const DEP = { ...insight('base', '', 'Missing tests'), uniqueId: 'model.dep.base', path: 'models/base.sql' } type Options = { /** The tool's answer per run (the last repeats). */ @@ -205,6 +205,14 @@ test('a changed properties file checks the models it documents, and a dependency expect(toasts).toEqual(['◆ 1 new health issue · stg_orders: hard coded references · /health']) }) +test('a model dropped from a schema file that still documents others is held for its new issues', async ($, on) => { + const dropped = insight('fct_orders', FCT, 'Missing documentation') + const { clock, toasts } = await start($, on, { runs: [[OLD_STG], [OLD_STG, dropped]] }) + await $.tool.call(edit(STG_YML)) + await endTurn($, clock, 't1') + expect(toasts).toEqual(['◆ 1 new health issue · fct_orders: missing documentation · /health']) +}) + test('a model file Bash writes counts like one Claude edited; a command naming no model file starts no check', async ($, on) => { const { clock, calls, toasts } = await start($, on, { runs: [[], [NEW_STG]] }) await $.tool.call(command('ls -la && git status')) From 9962fac86d84768dfc79977981a0530a63963368 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 07:31:08 +0530 Subject: [PATCH 26/34] fix(health): bound the whole push-gate decision, folder lookups included, inside the chain's deadline Co-Authored-By: Claude Opus 5.5 --- .../altimate-code/hooks/features/health.tsx | 17 +++++++++++----- plugins/altimate-code/hooks/health.test.tsx | 20 ++++++++++++++++++- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 1e57134e..e658f131 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -692,15 +692,22 @@ export function pushRefusal(blocks: unknown, command: string, issues: readonly H */ export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> { if (!(options.healthBlocksPush === true || options.healthBlocksPush === 'true') || !isGitPush(e.command)) return null + // The whole decision, folder lookups included, is bounded inside the chain's deadline: past it the gate is given up on and the push runs. + const decided = await within($, GATE_WAIT_MS, decide($, e)) + return decided === null ? hold('the check of the changes could not be read in time.') : decided === 'run' ? null : decided +} + +const hold = (why: string) => ({ + deny: `git push is held: ${why}\nRun /${COMMAND} to check again; only the user can turn off "Project health blocks git push" in the plugin's settings.`, +}) + +async function decide($: EngineInterface, e: BashCall): Promise<{ deny: string } | 'run'> { const startedAt = await $.clock.now() - const hold = (why: string) => ({ - deny: `git push is held: ${why}\nRun /${COMMAND} to check again; only the user can turn off "Project health blocks git push" in the plugin's settings.`, - }) // An edit in the same command would run before any check of it. const root = await $.session.root().catch(() => '') for (const name of filesWrittenBy(e.command)) if (await isModelFile($, name.startsWith('/') || /^[A-Za-z]:/.test(name) ? name : `${root}/${name}`)) return hold(`this command changes the model file ${name} and pushes in one go, so the change is not checked first. Run them as separate commands.`) // With nothing changed this session there is nothing to wait for or hold. - if (((await read($, healthAtom))?.touched.length ?? 0) === 0) return null + if (((await read($, healthAtom))?.touched.length ?? 0) === 0) return 'run' // The wait is what is left of the gate's budget: the work above counts against the chain's deadline too. if ((await wantsCheck($, true)) || running?.generation === generation) { const left = GATE_WAIT_MS - ((await $.clock.now()) - startedAt) @@ -710,7 +717,7 @@ export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: // A check that failed or did not finish leaves the changes unchecked: a gate that fails open is no gate. if (state && enabled && (state.running || state.pending.length > 0 || state.baseline === null)) return hold(`the check of the models changed this session ${state.running ? 'did not finish in time' : `could not run (${state.problem ?? 'no answer'})`}.`) - return pushRefusal(options.healthBlocksPush, e.command, state?.issues ?? []) + return pushRefusal(options.healthBlocksPush, e.command, state?.issues ?? []) ?? 'run' } /** A model file Bash writes (a `sed -i`, a redirect, a `git checkout -- file`) is checked like one Claude edited. */ diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index 2811eff7..e7a7d7df 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -48,6 +48,8 @@ type Options = { holdStat?: Promise /** Called with the number of each folder lookup (from 1). */ onStat?: (n: number) => Promise | void + /** Called at each project lookup; a promise it returns holds the lookup. */ + onExists?: () => Promise | void } async function start($: any, on: any, options: Options, session: { isInteractive?: boolean } = {}) { @@ -74,7 +76,10 @@ async function start($: any, on: any, options: Options, session: { isInteractive return }) const projects = options.projects ?? [ROOT] - on('fs.exists', (_: unknown, e: { path: string }) => ({ value: projects.some(p => e.path === `${p}/dbt_project.yml`) })) + on('fs.exists', async (_: unknown, e: { path: string }) => { + await options.onExists?.() + return { value: projects.some(p => e.path === `${p}/dbt_project.yml`) } + }) let statHeld = false let stats = 0 on('fs.stat', async (_: unknown, e: { path: string }) => { @@ -425,6 +430,19 @@ test('with the push gate on, a push before the baseline lands, with no model cha expect(allowed?.deny).toBeUndefined() }) +test('with the push gate on, a push whose folder lookups outlast the gate is held, not let through', { options: { healthBlocksPush: true } }, async ($, on) => { + let slow = false + const never = new Promise(() => {}) + const { clock } = await start($, on, { runs: [[]], onExists: () => (slow ? never : undefined) }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + slow = true + const pending = $.tool.call(command("sed -i 's/a/b/' fresh/models/x.sql && git push")) + await clock.advance(GATE_WAIT_MS + 100) + const denied: any = await pending + expect(denied?.deny).toContain('could not be read in time') +}) + test('with the push gate on, a command that edits a model and pushes is held: the edit is not checked before it runs', { options: { healthBlocksPush: true } }, async ($, on) => { await start($, on, { runs: [[]] }) const denied: any = await $.tool.call(command(`sed -i 's/a/b/' models/staging/stg_orders.sql && git push`)) From 01378cb3aa33930462a0bced8d62e2dd66cdecaf Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 13:45:48 +0530 Subject: [PATCH 27/34] fix(health): a comment no longer hides a push, a throwing clock drops no batch, the write and baseline limits are documented again, and the guards have tests Co-Authored-By: Claude Sonnet 5.5 --- .../altimate-code/.claude-plugin/plugin.json | 6 +-- plugins/altimate-code/README.md | 6 +-- .../altimate-code/hooks/features/health.tsx | 8 +++- plugins/altimate-code/hooks/health.test.tsx | 38 +++++++++++++++++++ 4 files changed, 50 insertions(+), 8 deletions(-) diff --git a/plugins/altimate-code/.claude-plugin/plugin.json b/plugins/altimate-code/.claude-plugin/plugin.json index 5a71365a..b1b6ef8e 100644 --- a/plugins/altimate-code/.claude-plugin/plugin.json +++ b/plugins/altimate-code/.claude-plugin/plugin.json @@ -31,14 +31,14 @@ }, "healthBlocksPush": { "type": "boolean", - "title": "Project health blocks git push (not used yet)", - "description": "Not used yet: the project health check isn't in this version, so git push is never refused. When it ships, on refuses a push while the check finds new issues in the models you changed; off only shows them.", + "title": "Project health blocks git push", + "description": "On refuses a git push while the project health check finds new issues in the models you changed this session; off only shows them.", "default": false }, "healthConfigPath": { "type": "string", "title": "Project health check config", - "description": "Not used yet: the project health check isn't in this version. Path to a DataPilot health check config file, relative to the dbt project. Empty uses the default checks.", + "description": "Path to a DataPilot health check config file, relative to the dbt project. Empty uses the default checks.", "default": "" }, "commentsPollSeconds": { diff --git a/plugins/altimate-code/README.md b/plugins/altimate-code/README.md index e2594739..0c1e1339 100644 --- a/plugins/altimate-code/README.md +++ b/plugins/altimate-code/README.md @@ -52,7 +52,7 @@ If altimate-code is not on PATH or not configured, the plugin surfaces the failu 1. A **SessionStart hook** adds a description of the available tools and both modes to the session, and copies the linked workspace's skills (see below). It does not start the MCP server. In a linked project, the [team rules](#team-rules) hooks add the workspace's rules too. 2. **MCP mode.** Claude calls `warehouse_list`, `warehouse_add`, `sql_execute` (`tables` returns columns and value profiles; large results spill to a local file), `sql_materialize` (session-local `scratch` DuckDB for cross-database joins), `answer_commit`, and `impact_analysis`, `dbt_lineage`, `altimate_core_column_lineage`, `schema_detect_pii`, `data_diff`, `altimate_core_equivalence`, `sql_translate`. 3. **CLI mode.** The **skill** has Claude run `altimate tool context` once (connections, schema listing, analyst method), then `altimate tool run --args ''` through Bash and reason over the structured output. `altimate tool list --json` shows every tool and its input schema. A warm process per project starts automatically. -4. Calls are read-only unless you approve `--allow-write` (CLI mode); `DROP DATABASE`, `DROP SCHEMA` and `TRUNCATE` are always blocked. Read-only is enforced by the database on Postgres, SQLite and DuckDB and by SQL classification elsewhere, so use a read-only DB user for the strongest boundary. +4. Calls are read-only unless you approve `--allow-write` (CLI mode), except the project health check: it runs `dbt_health` with write, without asking, because the dbt project's integration refreshes `target/` (and runs `dbt deps`), so it starts only in an interactive session (or with `healthBlocksPush` on) that is inside a dbt project, and has no separate off switch; `DROP DATABASE`, `DROP SCHEMA` and `TRUNCATE` are always blocked. Read-only is enforced by the database on Postgres, SQLite and DuckDB and by SQL classification elsewhere, so use a read-only DB user for the strongest boundary. 5. For long multi-step jobs, or when you invoke **`/altimate-code:altimate `** for work that needs the agent, Claude Code runs `altimate run ""` locally with the `builder` agent (or `analyst` for a data question when MCP is unavailable), which uses your configured LLM provider. 6. Warehouse queries and dbt commands run **locally under your credentials**. If the installed CLI predates the `tool` subcommand, the skill falls back to delegation. @@ -136,12 +136,12 @@ Rule drafting uses altimate-code's own model (sign in to the Altimate LLM Gatewa ## Settings -The plugin has five settings, shown when you enable it (`userConfig` in `.claude-plugin/plugin.json`). None is used yet: the features that read them are not in this version, so changing one changes nothing. The table says what each will control. +The plugin has five settings, shown when you enable it (`userConfig` in `.claude-plugin/plugin.json`). `healthBlocksPush` and `healthConfigPath` are used by the project health check. `maxScanBytes` and the comments settings are not used yet: the features that read them are not in this version, so changing one changes nothing. The table says what each controls. | Setting | Default | What it controls | | --- | --- | --- | | `maxScanBytes` | 10 GiB | The largest BigQuery scan a dbt run, build or show may make before it waits for your approval (0 never waits; at least 0). Other warehouses are never held. | -| `healthBlocksPush` | off | When on, `git push` is refused while the project health check finds new issues in the models you changed. Off, the issues are only shown. | +| `healthBlocksPush` | off | When on, `git push` is refused while the project health check finds new issues in the models you changed. Off, the issues are only shown. If the first check of a session lands after you changed a model, that model's existing issues count as new (the baseline then lacks them). | | `healthConfigPath` | empty | The DataPilot health check config file, relative to the dbt project. Empty uses the default checks. | | `commentsPollSeconds` | 900 | How often the team's comments on shared docs are fetched again (at least 30). | | `commentsInContext` | on | Whether the open comments on a model Claude reads or edits are added to its context. | diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index e658f131..0abecff4 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -263,7 +263,11 @@ export function simpleCommands(line: string): string[][] { } for (let i = 0; i < line.length; i++) { const c = line[i]! - if (c === "'") { + if (c === '#' && !inWord) { + // An unquoted `#` at the start of a word is a comment to the end of the line: its quotes and commands are text. + const end = line.indexOf('\n', i) + i = end < 0 ? line.length : end - 1 + } else if (c === "'") { quoted = true const close = line.indexOf("'", i + 1) word += line.slice(i + 1, close < 0 ? line.length : close) @@ -468,11 +472,11 @@ async function isModelFile($: EngineInterface, file: string): Promise { /** Asks altimate-code's dbt_health for the project's insights. */ async function check($: EngineInterface): Promise { - const until = (await $.clock.now()) + CHECK_MS const badConfig = configPathProblem(options.healthConfigPath) if (badConfig) return { problem: badConfig } let result: Awaited> try { + const until = (await $.clock.now()) + CHECK_MS result = await runTool($, 'dbt_health', toolArgs(options.healthConfigPath), { write: true, until }) } catch (error) { return { problem: `Project health could not run: ${String(error).split('\n')[0]}` } diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index e7a7d7df..8fd5484d 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -479,6 +479,44 @@ test('the configured config path is passed to the tool, empty means the defaults for (const bad of ['/etc/hosts', 'C:\\x.yml', '../x.yml', 'a/../../x.yml']) expect(configPathProblem(bad)).toContain('inside the dbt project') }) +test('a comment never hides the push that follows it, and a push inside a comment is not a push', () => { + expect(isGitPush("# Push this branch's commits\ngit push origin HEAD")).toBe(true) + expect(isGitPush("git add -A # don't forget\ngit push")).toBe(true) + expect(isGitPush('echo x # ; git push')).toBe(false) + expect(isGitPush('git status # git push')).toBe(false) + expect(isGitPush('echo a#b && git push')).toBe(true) +}) + +test('an unnamed YAML file edited before the baseline lands does not empty the baseline', async ($, on) => { + let release!: () => void + const until = new Promise(resolve => (release = resolve)) + const { clock, toasts } = await start($, on, { runs: [[OLD_STG, OLD_FCT]], hold: { at: 0, until } }) + await $.tool.call(edit(`${ROOT}/models/_sources.yml`)) + await endTurn($, clock, 't1') + release() + await clock.settle() + expect(toasts).toEqual([]) +}) + +test('a config path outside the project runs no check and says so', { options: { healthConfigPath: '/etc/hosts' } }, async ($, on) => { + const { calls, toasts } = await start($, on, { runs: [[]] }) + expect(calls).toHaveLength(0) + expect(toasts.join('\n')).toContain('inside the dbt project') +}) + +test('with the push gate on, a push while a re-check is still running is held', { options: { healthBlocksPush: true } }, async ($, on) => { + let release!: () => void + const until = new Promise(resolve => (release = resolve)) + const { clock } = await start($, on, { runs: [[], [NEW_STG]], hold: { at: 1, until } }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + const pending = $.tool.call(command('git push')) + await clock.advance(GATE_WAIT_MS + 100) + const denied: any = await pending + expect(denied?.deny).toContain('could not be read in time') + release() +}) + test('git push is found by its command words, not by its text', () => { for (const line of [ 'git push', From 5c13dd1f892fcb3d70207c1a43f62cc754c439a3 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 13:57:03 +0530 Subject: [PATCH 28/34] docs(health): state the late-baseline limit for a model dropped from a schema file Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/altimate-code/README.md b/plugins/altimate-code/README.md index 0c1e1339..4f68167d 100644 --- a/plugins/altimate-code/README.md +++ b/plugins/altimate-code/README.md @@ -141,7 +141,7 @@ The plugin has five settings, shown when you enable it (`userConfig` in `.claude | Setting | Default | What it controls | | --- | --- | --- | | `maxScanBytes` | 10 GiB | The largest BigQuery scan a dbt run, build or show may make before it waits for your approval (0 never waits; at least 0). Other warehouses are never held. | -| `healthBlocksPush` | off | When on, `git push` is refused while the project health check finds new issues in the models you changed. Off, the issues are only shown. If the first check of a session lands after you changed a model, that model's existing issues count as new (the baseline then lacks them). | +| `healthBlocksPush` | off | When on, `git push` is refused while the project health check finds new issues in the models you changed. Off, the issues are only shown. If the first check of a session lands after you changed a model, that model's existing issues count as new (the baseline then lacks them). The reverse also holds: a model you drop from a schema file before that first check lands has its new issues taken as existing, so a push is not held for them. | | `healthConfigPath` | empty | The DataPilot health check config file, relative to the dbt project. Empty uses the default checks. | | `commentsPollSeconds` | 900 | How often the team's comments on shared docs are fetched again (at least 30). | | `commentsInContext` | on | Whether the open comments on a model Claude reads or edits are added to its context. | From eb8d48cc1422ab52dd07e0ec0e21468ea4e155cb Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 14:11:51 +0530 Subject: [PATCH 29/34] fix(health): a relative file a Bash command writes is read from the shell's folder, and the docs test ignores the health check's own tool call Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/hooks/features/docs.test.tsx | 3 +++ plugins/altimate-code/hooks/features/health.tsx | 13 ++++++++++--- plugins/altimate-code/hooks/health.test.tsx | 11 +++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/plugins/altimate-code/hooks/features/docs.test.tsx b/plugins/altimate-code/hooks/features/docs.test.tsx index 5c73f569..19e04996 100644 --- a/plugins/altimate-code/hooks/features/docs.test.tsx +++ b/plugins/altimate-code/hooks/features/docs.test.tsx @@ -2,6 +2,7 @@ import { expect, mock, test } from 'claude-code/testing' import { PHASE_TIMEOUT_MS } from '../tool-chain' import { ROUND_MS, applyPrompt, candidatesFrom, changedDescriptions, columnDescriptions, modelPaths, sentGroups } from './docs' +import { nothingToReport } from '../inert-features' const ROOT = '/repo' const MANIFEST_PATH = `${ROOT}/target/manifest.json` @@ -257,6 +258,8 @@ async function start($: any, on: any, options: Options = {}) { on('process.run', (_: unknown, e: { argv: readonly string[] }) => { const [, command, , tool] = e.argv if (command !== 'tool') return { value: { exitCode: 1, stdout: '', stderr: 'not this test' } } + // The project health check asks for its own tool at session start: not a call this feature makes. + if (tool === 'dbt_health') return { value: nothingToReport(e.argv)! } calls.push(`cli:${tool}`) toolArgs.push(String(e.argv[5])) if (tool === 'dbt_docs_coverage') { diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 0abecff4..e901ba16 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -708,8 +708,8 @@ const hold = (why: string) => ({ async function decide($: EngineInterface, e: BashCall): Promise<{ deny: string } | 'run'> { const startedAt = await $.clock.now() // An edit in the same command would run before any check of it. - const root = await $.session.root().catch(() => '') - for (const name of filesWrittenBy(e.command)) if (await isModelFile($, name.startsWith('/') || /^[A-Za-z]:/.test(name) ? name : `${root}/${name}`)) return hold(`this command changes the model file ${name} and pushes in one go, so the change is not checked first. Run them as separate commands.`) + const here = await shellFolder($) + for (const name of filesWrittenBy(e.command)) if (await isModelFile($, name.startsWith('/') || /^[A-Za-z]:/.test(name) ? name : `${here}/${name}`)) return hold(`this command changes the model file ${name} and pushes in one go, so the change is not checked first. Run them as separate commands.`) // With nothing changed this session there is nothing to wait for or hold. if (((await read($, healthAtom))?.touched.length ?? 0) === 0) return 'run' // The wait is what is left of the gate's budget: the work above counts against the chain's deadline too. @@ -724,10 +724,17 @@ async function decide($: EngineInterface, e: BashCall): Promise<{ deny: string } return pushRefusal(options.healthBlocksPush, e.command, state?.issues ?? []) ?? 'run' } +/** The folder the session's shell is in now; the session's root when the host does not say. */ +async function shellFolder($: EngineInterface): Promise { + return (await $.session.cwd().catch(() => null)) || (await $.session.root().catch(() => '')) +} + /** A model file Bash writes (a `sed -i`, a redirect, a `git checkout -- file`) is checked like one Claude edited. */ export async function bashAfter($: EngineInterface, e: BashCall, result: ToolCallResult): Promise { const root = await $.session.root().catch(() => '') - for (const name of filesWrittenBy(e.command)) await markChanged($, name.startsWith('/') || /^[A-Za-z]:/.test(name) ? name : `${root}/${name}`, root) + // A relative name is relative to the folder the session's shell is in now: an earlier `cd` moved it. + const here = await shellFolder($) + for (const name of filesWrittenBy(e.command)) await markChanged($, name.startsWith('/') || /^[A-Za-z]:/.test(name) ? name : `${here}/${name}`, root) } async function markChanged($: EngineInterface, file: string, root: string) { diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index 8fd5484d..562290a0 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -48,6 +48,8 @@ type Options = { holdStat?: Promise /** Called with the number of each folder lookup (from 1). */ onStat?: (n: number) => Promise | void + /** The folder the session's shell is in (default: the engine does not say). */ + cwd?: string /** Called at each project lookup; a promise it returns holds the lookup. */ onExists?: () => Promise | void } @@ -63,6 +65,7 @@ async function start($: any, on: any, options: Options, session: { isInteractive 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 })) + if (options.cwd) on('session.cwd', () => ({ value: options.cwd })) on('ui.toast', (_: unknown, e: { text: string }) => { toasts.push(e.text) return { value: undefined } @@ -517,6 +520,14 @@ test('with the push gate on, a push while a re-check is still running is held', release() }) +test('a relative file a Bash command writes is read from the shell folder an earlier cd left, not the session root', async ($, on) => { + const { clock, calls, toasts } = await start($, on, { runs: [[], [NEW_STG]], cwd: `${ROOT}/models/staging` }) + await $.tool.call(command(`sed -i 's/a/b/' stg_orders.sql`)) + await endTurn($, clock, 't1') + expect(calls).toHaveLength(2) + expect(toasts).toEqual(['◆ 1 new health issue · stg_orders: hard coded references · /health']) +}) + test('git push is found by its command words, not by its text', () => { for (const line of [ 'git push', From ee73986c2089a226f0a112f452a659515ab82d65 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 14:15:58 +0530 Subject: [PATCH 30/34] fix(health): a cd in the same Bash command is read from the folder the command started in Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/hooks/features/health.tsx | 8 +++++++- plugins/altimate-code/hooks/health.test.tsx | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index e901ba16..2a0435f3 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -695,6 +695,8 @@ export function pushRefusal(blocks: unknown, command: string, issues: readonly H * they have new health issues. A check that cannot run or does not finish holds it too. */ export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> { + // Where the shell is before this command runs: its own `cd`s are read from there, and `bashAfter` sees the folder after them. + if (/\b(cd|pushd)\b/.test(e.command)) shellBefore = await shellFolder($) if (!(options.healthBlocksPush === true || options.healthBlocksPush === 'true') || !isGitPush(e.command)) return null // The whole decision, folder lookups included, is bounded inside the chain's deadline: past it the gate is given up on and the push runs. const decided = await within($, GATE_WAIT_MS, decide($, e)) @@ -724,6 +726,8 @@ async function decide($: EngineInterface, e: BashCall): Promise<{ deny: string } return pushRefusal(options.healthBlocksPush, e.command, state?.issues ?? []) ?? 'run' } +let shellBefore: string | null = null + /** The folder the session's shell is in now; the session's root when the host does not say. */ async function shellFolder($: EngineInterface): Promise { return (await $.session.cwd().catch(() => null)) || (await $.session.root().catch(() => '')) @@ -733,7 +737,9 @@ async function shellFolder($: EngineInterface): Promise { export async function bashAfter($: EngineInterface, e: BashCall, result: ToolCallResult): Promise { const root = await $.session.root().catch(() => '') // A relative name is relative to the folder the session's shell is in now: an earlier `cd` moved it. - const here = await shellFolder($) + // A command with its own `cd` is read from the folder it started in, which the gate noted. + const here = shellBefore ?? (await shellFolder($)) + shellBefore = null for (const name of filesWrittenBy(e.command)) await markChanged($, name.startsWith('/') || /^[A-Za-z]:/.test(name) ? name : `${here}/${name}`, root) } diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index 562290a0..6e8e5684 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -50,6 +50,8 @@ type Options = { onStat?: (n: number) => Promise | void /** The folder the session's shell is in (default: the engine does not say). */ cwd?: string + /** Called when the Bash command itself runs, between the gate and the after-phase. */ + onCall?: () => void /** Called at each project lookup; a promise it returns holds the lookup. */ onExists?: () => Promise | void } @@ -98,7 +100,10 @@ async function start($: any, on: any, options: Options, session: { isInteractive on('fs.read', () => ({ value: '' })) on('tool.list', () => ({ value: [] })) on('mcp.connect', () => ({ value: { isConnected: false, reason: 'not_connected', message: 'off' } })) - on('tool.call', () => ({ result: { ok: true } })) + on('tool.call', () => { + options.onCall?.() + return { result: { ok: true } } + }) let run = 0 on('process.run', async (_: unknown, e: { argv: readonly string[] }) => { const [, command, , tool = ''] = e.argv @@ -528,6 +533,15 @@ test('a relative file a Bash command writes is read from the shell folder an ear expect(toasts).toEqual(['◆ 1 new health issue · stg_orders: hard coded references · /health']) }) +test('a cd in the same Bash command is read from the folder the command started in, not the one it left the shell in', async ($, on) => { + const options: Options = { runs: [[], [NEW_STG]], cwd: ROOT, onCall: () => (options.cwd = `${ROOT}/models/staging`) } + const { clock, calls, toasts } = await start($, on, options) + await $.tool.call(command(`cd models/staging && sed -i 's/a/b/' stg_orders.sql`)) + await endTurn($, clock, 't1') + expect(calls).toHaveLength(2) + expect(toasts).toEqual(['◆ 1 new health issue · stg_orders: hard coded references · /health']) +}) + test('git push is found by its command words, not by its text', () => { for (const line of [ 'git push', From f04ebc740e82dac88fa02aa46a164a31d14af5e0 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 14:17:46 +0530 Subject: [PATCH 31/34] test(health): the push gate reads a relative edit from the shell's folder Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/hooks/health.test.tsx | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index 6e8e5684..1da112b1 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -542,6 +542,12 @@ test('a cd in the same Bash command is read from the folder the command started expect(toasts).toEqual(['◆ 1 new health issue · stg_orders: hard coded references · /health']) }) +test('with the push gate on, an edit-and-push written from the folder an earlier cd left is held', { options: { healthBlocksPush: true } }, async ($, on) => { + await start($, on, { runs: [[]], cwd: `${ROOT}/models/staging` }) + const denied: any = await $.tool.call(command(`sed -i 's/a/b/' stg_orders.sql && git push`)) + expect(denied?.deny).toContain('Run them as separate commands') +}) + test('git push is found by its command words, not by its text', () => { for (const line of [ 'git push', From 9b9b05ae09d1c2a922557a49d5e0a6dcb0a4368c Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 14:18:43 +0530 Subject: [PATCH 32/34] test(health): pin the shell folder the push gate reads a relative edit from Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/hooks/health.test.tsx | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index 1da112b1..ff6a0150 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -548,6 +548,12 @@ test('with the push gate on, an edit-and-push written from the folder an earlier expect(denied?.deny).toContain('Run them as separate commands') }) +test('with the push gate on, a write from a dbt-internal folder the shell is in is no model edit', { options: { healthBlocksPush: true } }, async ($, on) => { + await start($, on, { runs: [[]], cwd: `${ROOT}/target` }) + const allowed: any = await $.tool.call(command(`sed -i 's/a/b/' x.sql && git push`)) + expect(allowed?.deny).toBeUndefined() +}) + test('git push is found by its command words, not by its text', () => { for (const line of [ 'git push', From 1aa54d6f70aeb02401ddaa527cdbf949dbfecd45 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 14:43:37 +0530 Subject: [PATCH 33/34] fix(health): the shell-folder lookup of a push is inside the gate's bounded decision Co-Authored-By: Claude Sonnet 5.5 --- .../altimate-code/hooks/features/health.tsx | 10 ++++++--- plugins/altimate-code/hooks/health.test.tsx | 21 ++++++++++++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 2a0435f3..9b53ce6d 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -695,9 +695,11 @@ export function pushRefusal(blocks: unknown, command: string, issues: readonly H * they have new health issues. A check that cannot run or does not finish holds it too. */ export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> { - // Where the shell is before this command runs: its own `cd`s are read from there, and `bashAfter` sees the folder after them. - if (/\b(cd|pushd)\b/.test(e.command)) shellBefore = await shellFolder($) - if (!(options.healthBlocksPush === true || options.healthBlocksPush === 'true') || !isGitPush(e.command)) return null + if (!(options.healthBlocksPush === true || options.healthBlocksPush === 'true') || !isGitPush(e.command)) { + // Where the shell is before this command runs: its own `cd`s are read from there, and `bashAfter` sees the folder after them. + if (CHANGES_FOLDER.test(e.command)) shellBefore = await within($, GATE_WAIT_MS, shellFolder($)) + return null + } // The whole decision, folder lookups included, is bounded inside the chain's deadline: past it the gate is given up on and the push runs. const decided = await within($, GATE_WAIT_MS, decide($, e)) return decided === null ? hold('the check of the changes could not be read in time.') : decided === 'run' ? null : decided @@ -711,6 +713,7 @@ async function decide($: EngineInterface, e: BashCall): Promise<{ deny: string } const startedAt = await $.clock.now() // An edit in the same command would run before any check of it. const here = await shellFolder($) + if (CHANGES_FOLDER.test(e.command)) shellBefore = here for (const name of filesWrittenBy(e.command)) if (await isModelFile($, name.startsWith('/') || /^[A-Za-z]:/.test(name) ? name : `${here}/${name}`)) return hold(`this command changes the model file ${name} and pushes in one go, so the change is not checked first. Run them as separate commands.`) // With nothing changed this session there is nothing to wait for or hold. if (((await read($, healthAtom))?.touched.length ?? 0) === 0) return 'run' @@ -727,6 +730,7 @@ async function decide($: EngineInterface, e: BashCall): Promise<{ deny: string } } let shellBefore: string | null = null +const CHANGES_FOLDER = /\b(cd|pushd)\b/ /** The folder the session's shell is in now; the session's root when the host does not say. */ async function shellFolder($: EngineInterface): Promise { diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index ff6a0150..3f126ad1 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -50,6 +50,8 @@ type Options = { onStat?: (n: number) => Promise | void /** The folder the session's shell is in (default: the engine does not say). */ cwd?: string + /** Holds the folder lookup while it returns a promise. */ + holdCwd?: () => Promise | undefined /** Called when the Bash command itself runs, between the gate and the after-phase. */ onCall?: () => void /** Called at each project lookup; a promise it returns holds the lookup. */ @@ -67,7 +69,11 @@ async function start($: any, on: any, options: Options, session: { isInteractive 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 })) - if (options.cwd) on('session.cwd', () => ({ value: options.cwd })) + if (options.cwd || options.holdCwd) + on('session.cwd', async () => { + await options.holdCwd?.() + return { value: options.cwd } + }) on('ui.toast', (_: unknown, e: { text: string }) => { toasts.push(e.text) return { value: undefined } @@ -554,6 +560,19 @@ test('with the push gate on, a write from a dbt-internal folder the shell is in expect(allowed?.deny).toBeUndefined() }) +test('with the push gate on, a push whose shell-folder lookup stalls is held, not let through', { options: { healthBlocksPush: true } }, async ($, on) => { + let stall = false + const never = new Promise(() => {}) + const { clock } = await start($, on, { runs: [[], [NEW_STG]], holdCwd: () => (stall ? never : undefined) }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + stall = true + const pending = $.tool.call(command('cd /repo && git push')) + await clock.advance(GATE_WAIT_MS + 100) + const denied: any = await pending + expect(denied?.deny).toContain('could not be read in time') +}) + test('git push is found by its command words, not by its text', () => { for (const line of [ 'git push', From f352d99a5988c61bed231ffa5f72ec5ca64f57d1 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 18 Nov 2026 15:06:17 +0530 Subject: [PATCH 34/34] fix(health): the shell folder noted before a command belongs to that command, and an unknown start reads from the session root Co-Authored-By: Claude Sonnet 5.5 --- plugins/altimate-code/hooks/features/health.tsx | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 9b53ce6d..c318c81e 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -640,6 +640,7 @@ export async function sessionStart($: EngineInterface, e: Args<'session.start'>) enabled = false toldProblem = null known = [] + shellBefore = null projects.clear() await update($, healthAtom, () => fresh()) // The status line outlives a process: a part an earlier one left is cleared whether or not this one drew it. @@ -697,7 +698,7 @@ export function pushRefusal(blocks: unknown, command: string, issues: readonly H export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> { if (!(options.healthBlocksPush === true || options.healthBlocksPush === 'true') || !isGitPush(e.command)) { // Where the shell is before this command runs: its own `cd`s are read from there, and `bashAfter` sees the folder after them. - if (CHANGES_FOLDER.test(e.command)) shellBefore = await within($, GATE_WAIT_MS, shellFolder($)) + if (CHANGES_FOLDER.test(e.command)) shellBefore = { command: e.command, folder: await within($, GATE_WAIT_MS, shellFolder($)) } return null } // The whole decision, folder lookups included, is bounded inside the chain's deadline: past it the gate is given up on and the push runs. @@ -713,7 +714,7 @@ async function decide($: EngineInterface, e: BashCall): Promise<{ deny: string } const startedAt = await $.clock.now() // An edit in the same command would run before any check of it. const here = await shellFolder($) - if (CHANGES_FOLDER.test(e.command)) shellBefore = here + if (CHANGES_FOLDER.test(e.command)) shellBefore = { command: e.command, folder: here } for (const name of filesWrittenBy(e.command)) if (await isModelFile($, name.startsWith('/') || /^[A-Za-z]:/.test(name) ? name : `${here}/${name}`)) return hold(`this command changes the model file ${name} and pushes in one go, so the change is not checked first. Run them as separate commands.`) // With nothing changed this session there is nothing to wait for or hold. if (((await read($, healthAtom))?.touched.length ?? 0) === 0) return 'run' @@ -729,7 +730,8 @@ async function decide($: EngineInterface, e: BashCall): Promise<{ deny: string } return pushRefusal(options.healthBlocksPush, e.command, state?.issues ?? []) ?? 'run' } -let shellBefore: string | null = null +// The folder noted before a command with its own `cd` ran; `folder` is null when the lookup ran out of time. +let shellBefore: { command: string; folder: string | null } | null = null const CHANGES_FOLDER = /\b(cd|pushd)\b/ /** The folder the session's shell is in now; the session's root when the host does not say. */ @@ -742,8 +744,10 @@ export async function bashAfter($: EngineInterface, e: BashCall, result: ToolCal const root = await $.session.root().catch(() => '') // A relative name is relative to the folder the session's shell is in now: an earlier `cd` moved it. // A command with its own `cd` is read from the folder it started in, which the gate noted. - const here = shellBefore ?? (await shellFolder($)) + // A note belongs to its own command; a command with a `cd` whose start is unknown is read from the session's root, where it began. + const noted = shellBefore?.command === e.command ? shellBefore : null shellBefore = null + const here = noted ? (noted.folder ?? root) : CHANGES_FOLDER.test(e.command) ? root : await shellFolder($) for (const name of filesWrittenBy(e.command)) await markChanged($, name.startsWith('/') || /^[A-Za-z]:/.test(name) ? name : `${here}/${name}`, root) }