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..4f68167d 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). 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. | diff --git a/plugins/altimate-code/hooks/features/docs.test.tsx b/plugins/altimate-code/hooks/features/docs.test.tsx index 98c5598b..315e162d 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' || tool === 'docs_share_comments') 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 dc141a90..c318c81e 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -1,51 +1,784 @@ // 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. 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 { 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 } +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. */ +// 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. +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. */ +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 fresh = (): HealthState => ({ baseline: null, pending: [], touched: [], issues: [], problem: null, running: false }) + +let options: PluginOptions = {} +/** 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 +/** 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. */ +const projects = new Map() + +export const pairKey = (i: Pick) => `${i.uniqueId}\u0000${i.name}` + +/** 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('/') +} + +/** 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`; `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(real(i.file))) || (i.properties && changed.has(real(i.properties)))) ids.add(i.uniqueId) + return ids +} + +/** 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 (+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 => 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') +} + +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']) + +/** 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++ + 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] + } + // 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 } +} + +/** 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).replace(/\r$/, '') === 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] === '(') { + const j = substitutionEnd(text, i + 1) + 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, 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[][] = [] + const heredocs: { delimiter: string; quoted: boolean; strip: boolean; owner: 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) + words = [] + } + for (let i = 0; i < line.length; i++) { + const c = line[i]! + 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) + 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] === '`') { + // A substitution inside quotes still runs: its commands are commands of the line. + let j = i + if (line[i] === '`') { + j = line.indexOf('`', i + 1) + if (j < 0) j = line.length + } else j = substitutionEnd(line, i + 1) + nested.push(...substitutionsIn(line.slice(i, j + 1)).flatMap(simpleCommands)) + 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 + quoted = true + } else if (c === '$' && line[i + 1] === '(') { + endCommand() + i++ + } else if (c === '<' && heredocAt(line, i)) { + const doc = heredocAt(line, i)! + // 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. + for (const doc of heredocs.splice(0)) { + const { body, next } = heredocBody(line, at, doc) + at = next - 1 + 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 + } else if (c === '<' && line[i + 1] !== '(') { + // An input redirect (`<`, `<<<`) is its own word before its target, as an output one is. + if (!bareDescriptor()) 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 (!bareDescriptor()) 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') { + 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 (/^\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 + } + 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 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`; 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 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]!) + 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 (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)) + } 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)) + } + return out +} + +/** 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 } : {} +} + +/** 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[] = [] + 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)) 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 + // 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) +} + +/** Asks altimate-code's dbt_health for the project's insights. */ +async function check($: EngineInterface): Promise { + 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]}` } + } + // 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)) { + // 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[] } + } + if (/unknown tool/i.test(result.text)) return { problem: NEEDS_CLI } + return { problem: `Project health: ${(result.text || 'no answer').split('\n')[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) +} + +/** 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 }) +} + +/** + * 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 + } + // 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 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 + await update($, healthAtom, prev => { + const base = prev ?? fresh() + if (mine !== generation) return base + if (base.touched.some(f => !asked.has(f))) { + late = true + 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. + const edited = modelsOfFiles(base.touched, seen, 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)) + // 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 } + }) + if (!late || again) break + } + 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) + if (!needsBaseline && issues.length) $.ui.toast(`◆ ${bandText(issues)}`, { timeoutMs: 8000 }) + } +} + +/** 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` (/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) + if (!enabled || !state) return false + return (retry || !state.problem) && (state.pending.length > 0 || state.baseline === null) +} + /** 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 || !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.' } + }) +} -/** After the session started: the place to register commands. */ -export async function sessionStart($: EngineInterface, e: Args<'session.start'>) {} +export async function sessionStart($: EngineInterface, e: Args<'session.start'>) { + generation += 1 + running = null + 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. + 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. + 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($)) +} /** Also called for a subagent's turns, which carry `e.agentId`. */ export async function turnStart($: EngineInterface, e: Args<'turn.start'>) {} -/** After the turn's answer; `out` is what the turn ended with. Also called for a subagent's turns (`e.agentId`). */ -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 + if (await wantsCheck($)) later($, () => kick($)) +} -export async function sessionEnd($: EngineInterface, e: Args<'session.end'>) {} +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. */ 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 } +/** 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)}\nTell the user about them and stop; only the user can turn off "Project health blocks git push" in the plugin's settings.`, + } +} + /** - * `{ deny }` refuses the Bash call; null lets it run. A gate that throws is logged and the call runs: - * a gate that must hold the call when its own check fails catches that failure and returns `{ deny }`. + * 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 or does not finish holds it too. */ export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> { - 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 = { 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. + 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() + // An edit in the same command would run before any check of it. + const here = await shellFolder($) + 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' + // 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) + 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)) + 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 ?? []) ?? 'run' } -export async function bashAfter($: EngineInterface, e: BashCall, result: ToolCallResult): Promise {} +// 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. */ +async function shellFolder($: EngineInterface): Promise { + return (await $.session.cwd().catch(() => null)) || (await $.session.root().catch(() => '')) +} -/** `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 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(() => '') + // 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. + // 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) +} + +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 own = root ? await projectAbove($, norm(root)) : null + const mine = await projectAbove($, path.replace(/\/[^/]*$/, '')) + 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 { + ...base, + // 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}`, 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 new file mode 100644 index 00000000..3f126ad1 --- /dev/null +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -0,0 +1,830 @@ +import { expect, mock, test } from 'claude-code/testing' + +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` +const STG_YML = `${ROOT}/models/staging/_staging.yml` +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, file: string, name: string, message = `${name} found`, properties = '') => ({ + uniqueId: `model.shop.${id}`, + 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', 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', 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 answer per run (the last repeats). */ + 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). */ + 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 + /** 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 + /** 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. */ + onExists?: () => Promise | void +} + +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[] = [] + 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 })) + 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 } + }) + 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 + }) + const projects = options.projects ?? [ROOT] + 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 }) => { + await options.onStat?.(++stats) + 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 } } + }) + on('fs.read', () => ({ value: '' })) + on('tool.list', () => ({ value: [] })) + on('mcp.connect', () => ({ value: { isConnected: false, reason: 'not_connected', message: 'off' } })) + 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 + 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.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: [] } : '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: '' } } + }) + await $.session.start({ cwd: ROOT, surface: 'terminal', isInteractive: session.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' }) +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]] }) + 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 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('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`]) + await $.tool.call(edit(file)) + await endTurn($, clock, 't1') + expect(calls).toHaveLength(1) + expect(toasts).toEqual([]) +}) + +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) => { + 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 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 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')) + 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 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('◆ 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) => { + 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)) + 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(command('git push origin main')) + 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('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 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 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`)) + 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('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' }) + 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('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('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('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('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('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('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', + '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('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('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('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) + expect(isGitPush('cat < { + const { calls } = await start($, on, { runs: [[OLD_STG]] }) + expect(calls.map(c => c.tool)).toEqual(['dbt_health']) +}) + +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('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 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('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 => (globalThis as any).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('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', () => { + 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" { + 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(newIssues([OLD_STG, NEW_STG, NEW_FCT], [pairKey(OLD_STG)], new Set(['model.shop.stg_orders']))).toEqual([NEW_STG]) +}) + +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') +}) + +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) +}) diff --git a/plugins/altimate-code/types/index.d.ts b/plugins/altimate-code/types/index.d.ts index be6e2a19..eb269eff 100644 --- a/plugins/altimate-code/types/index.d.ts +++ b/plugins/altimate-code/types/index.d.ts @@ -376,7 +376,32 @@ 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 {} +/** 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 + /** 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: HealthInsight[] + /** Why the check can't run, shown once per distinct reason. */ + problem: string | null + running: boolean +} // --------------------------------------------------------------------------------------------------- // SQL graph: the edited model's SQL graph.