diff --git a/plugins/altimate-code/.claude-plugin/plugin.json b/plugins/altimate-code/.claude-plugin/plugin.json index 5c5b876..a656d07 100644 --- a/plugins/altimate-code/.claude-plugin/plugin.json +++ b/plugins/altimate-code/.claude-plugin/plugin.json @@ -20,5 +20,37 @@ "databricks", "column-lineage" ], - "types": "./types/index.d.ts" + "types": "./types/index.d.ts", + "userConfig": { + "maxScanBytes": { + "type": "number", + "title": "Largest BigQuery scan without asking", + "description": "A dbt run or build of one named model, or a show of one model or with --inline, that BigQuery estimates would scan more bytes than this is held until you allow it with /allow-scan. A whole-project run, a graph selection or a command with options that change the target, state, vars, project or refresh mode, or one run after a non-dbt command, is not estimated, so it is never held. 0 never holds one. Other warehouses are never held.", + "default": 10737418240 + }, + "healthBlocksPush": { + "type": "boolean", + "title": "Project health blocks git push", + "description": "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.", + "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.", + "default": "" + }, + "commentsPollSeconds": { + "type": "number", + "title": "Team comments refresh (seconds)", + "description": "How often the team's comments on shared docs are fetched again.", + "default": 900 + }, + "commentsInContext": { + "type": "boolean", + "title": "Team comments in Claude's context", + "description": "When on, the open comments on a model Claude reads or edits are added to its context. Off keeps them out of Claude's context.", + "default": true + } + } } diff --git a/plugins/altimate-code/hooks/altimate-cli.ts b/plugins/altimate-code/hooks/altimate-cli.ts index bf1bdf8..e6cda5a 100644 --- a/plugins/altimate-code/hooks/altimate-cli.ts +++ b/plugins/altimate-code/hooks/altimate-cli.ts @@ -47,3 +47,133 @@ export function later($: EngineInterface, work: () => Promise) { }) }) } + +// The plugin's own MCP server, by its key in .mcp.json. +const SERVER_KEY = 'altimate-code' +const TOOL_TIMEOUT_MS = 60_000 + +/** Resolves with the work's value, or null once `ms` pass or it fails. */ +export function within($: EngineInterface, ms: number, work: Promise): Promise { + return new Promise(resolve => { + let settled = false + const settle = (value: T | null) => { + if (settled) return + settled = true + resolve(value) + } + $.clock.after(ms, () => settle(null)) + work.then(settle, () => settle(null)) + }) +} + +let serverName: string | null = null +let connectFailedAt = -Infinity +const CONNECT_RETRY_MS = 60_000 + +/** The plugin's MCP server as $.mcp.call names it, or null when it is not connected (CLI mode); a failure is retried after a minute. */ +async function mcpServer($: EngineInterface): Promise { + if (serverName) return serverName + const now = await $.clock.now() + if (now - connectFailedAt < CONNECT_RETRY_MS) return null + const connected = await $.mcp.connect(SERVER_KEY).catch(() => null) + serverName = connected?.isConnected ? connected.server : null + if (!serverName) connectFailedAt = now + return serverName +} + +export function textOf(content: unknown): string { + if (typeof content === 'string') return content + const blocks = Array.isArray(content) ? content : Array.isArray((content as any)?.content) ? (content as any).content : [] + return blocks + .filter((block: any) => block?.type === 'text' && typeof block.text === 'string') + .map((block: any) => block.text) + .join('\n') +} + +/** + * The file a `` preview says holds the whole answer: null when the text is no such + * preview, undefined when it is one but names no file. + */ +export function persistedPath(text: string): string | null | undefined { + if (!/^\s*(?:<|<)persisted-output(?:>|>)/.test(text)) return null + return /Full output saved to: ([^\n]+?)\s*$/m.exec(text)?.[1] +} + +/** A file:// URI as a filesystem path; a Windows drive path keeps no leading slash. */ +function fileUriPath(uri: string): string { + const path = decodeURIComponent(new URL(uri).pathname) + return /^\/[A-Za-z]:/.test(path) ? path.slice(1) : path +} + +export type RunToolOptions = { + /** Straight to the CLI, whose --json carries the whole metadata. */ + cliOnly?: boolean + /** A deadline on the engine's clock: nothing starts once it has passed, and the CLI is stopped at it. */ + until?: number + /** + * The tool needs write permission (it starts the dbt adapter, or changes something): the call goes + * through the CLI with `--allow-write`, since the plugin's MCP server runs read-only. + */ + write?: boolean +} + +/** + * Runs one altimate-code data tool: through the plugin's MCP server when it is connected (no + * permission prompt, structured content when the build sends it), else `altimate tool run --json`. + * `cliOnly` goes straight to the CLI, whose --json carries the whole metadata; a structured result + * the server left out for size is fetched that way too. + */ +export async function runTool($: EngineInterface, name: string, args: Record, options: RunToolOptions = {}) { + const { until, write = false } = options + const cliOnly = options.cliOnly === true || write + // With a deadline, nothing starts once it has passed, and the CLI is stopped at it. + const left = async () => { + const ms = until === undefined ? TOOL_TIMEOUT_MS : until - (await $.clock.now()) + if (ms <= 0) throw new Error('out of time') + return ms + } + const server = cliOnly ? null : await mcpServer($) + if (server) { + const ms = await left() + try { + // The engine's MCP call has no timeout of its own: one that never answers is given up on, so a + // shared trace does not hold every later edit of that column, and the CLI is tried instead. + const result = await within($, ms, $.mcp.call(server, name, args)) + if (!result) throw new Error('altimate-code did not answer over MCP') + let text = textOf(result.content) + // A long result comes back as a preview plus a link to the full text. + const link = result.content.find((block: any) => block?.type === 'resource_link' && String(block.uri).startsWith('file://')) as + | { uri: string } + | undefined + // Claude Code (2.1.319) swaps an answer of about 49 KB or more for a preview that names the file it + // saved the whole answer to, with no link block: the answer is that file. + const saved = link ? null : persistedPath(text) + if (!link && saved === undefined) throw new Error('a cut-off preview with no file named') + // An unreadable link or file leaves only the preview: fetch the whole answer through the CLI instead. + const full = link ? await $.fs.read(fileUriPath(link.uri)).catch(() => null) : saved ? await $.fs.read(saved).catch(() => null) : text + if (full === null) throw new Error('the full result could not be read') + text = full + // Claude Code hands a tool's structured content over as one JSON text block in place of the + // text; the structured content carries the text report as `report`. + const structured = (result.structuredContent ?? (text.trim().startsWith('{') ? parseJson(text) : null)) as any + if (structured?.truncated !== true) { + return { metadata: structured, text: typeof structured?.report === 'string' ? structured.report : text, ok: !result.isError } + } + } catch { + // Fall through to the CLI: the server may have gone away mid-session, so the next call reconnects. + serverName = null + } + } + const argv = ['tool', 'run', name, '--args', JSON.stringify(args), '--json', ...(write ? ['--allow-write'] : [])] + const result = await runAltimate($, argv, await left()) + if ((result as { isStdoutTruncated?: boolean }).isStdoutTruncated) { + return { metadata: null as any, text: `${name}'s answer was larger than the 4 MiB a plugin can read`, ok: false } + } + const data = parseJson<{ status?: string; output?: string; metadata?: unknown }>(result.stdout) + // Anything but the CLI's answer object (an altimate-code that exits before a pipe drains cuts it off) is a + // failure to report and ask again, not "no such model". + if (result.exitCode === 0 && (!data || typeof data !== 'object' || typeof data.status !== 'string')) { + return { metadata: null as any, text: 'the answer could not be read', ok: false } + } + return { metadata: (data?.metadata ?? null) as any, text: data?.output ?? '', ok: result.exitCode === 0 && data?.status !== 'error' } +} diff --git a/plugins/altimate-code/hooks/dbt-command.test.ts b/plugins/altimate-code/hooks/dbt-command.test.ts new file mode 100644 index 0000000..ffa5f78 --- /dev/null +++ b/plugins/altimate-code/hooks/dbt-command.test.ts @@ -0,0 +1,132 @@ +import { expect, test } from 'claude-code/testing' + +import { parseDbtCommand } from './dbt-command' + +/** The fields a caller decides on, without the offsets. */ +const shape = (line: string) => parseDbtCommand(line).map(({ start, end, subcommandEnd, ...rest }) => rest) + +const base = { select: [], target: null, targetPath: null, hasDefer: false, hasState: false } + +test('a plain dbt command: subcommand, selections, target and target path', () => { + expect(shape('dbt build -s stg_orders --select +fct_orders -t prod --target-path tgt')).toEqual([ + { ...base, binary: 'dbt', subcommand: 'build', select: ['stg_orders', '+fct_orders'], target: 'prod', targetPath: 'tgt' }, + ]) + expect(shape('dbt run --models a b --target=dev')).toEqual([{ ...base, binary: 'dbt', subcommand: 'run', select: ['a', 'b'], target: 'dev' }]) +}) + +test('commands joined with &&, ; and pipes each give their dbt invocation, in order', () => { + expect(shape('cd jaffle && dbt deps && dbt run -s a; dbt test | tee log.txt')).toEqual([ + { ...base, binary: 'dbt', subcommand: 'deps' }, + { ...base, binary: 'dbt', subcommand: 'run', select: ['a'] }, + { ...base, binary: 'dbt', subcommand: 'test' }, + ]) +}) + +test('uv run dbt, poetry run dbt and a path to dbt are dbt launchers', () => { + expect(shape('uv run dbt build -s a')).toEqual([{ ...base, binary: 'uv run dbt', subcommand: 'build', select: ['a'] }]) + expect(shape('poetry run dbt test')).toEqual([{ ...base, binary: 'poetry run dbt', subcommand: 'test' }]) + expect(shape('.venv/bin/dbt compile')).toEqual([{ ...base, binary: '.venv/bin/dbt', subcommand: 'compile' }]) +}) + +test('VAR=x in front of dbt is kept in the invocation; DBT_DEFER and DBT_STATE count as defer and state', () => { + const line = 'DBT_PROFILES_DIR=. dbt run -s a' + const [one] = parseDbtCommand(line) + expect(one).toEqual({ ...base, binary: 'dbt', subcommand: 'run', select: ['a'], start: 0, end: line.length, subcommandEnd: line.indexOf(' -s') }) + expect(shape('DBT_DEFER=1 DBT_STATE=prod dbt run')).toEqual([{ ...base, binary: 'dbt', subcommand: 'run', hasDefer: true, hasState: true }]) +}) + +test('--defer and --state are seen; the offsets cut the invocation out of the line', () => { + const line = 'echo hi && dbt build --defer --state prod-artifacts -s a > out.log' + const [one] = parseDbtCommand(line) + expect(one).toMatchObject({ subcommand: 'build', hasDefer: true, hasState: true }) + expect(line.slice(one!.start, one!.end)).toBe('dbt build --defer --state prod-artifacts -s a') + expect(line.slice(one!.start, one!.subcommandEnd)).toBe('dbt build') +}) + +test('a line without dbt, or one that cannot be read, gives no invocation', () => { + expect(parseDbtCommand('ls -la && git status')).toEqual([]) + expect(parseDbtCommand('echo "dbt run')).toEqual([]) + expect(parseDbtCommand('dbt run $(echo')).toEqual([]) + expect(parseDbtCommand('')).toEqual([]) + expect(parseDbtCommand('grep dbt run.log')).toEqual([]) +}) + +test('a bare dbt has no subcommand; quoted selections are unquoted', () => { + expect(shape('dbt')).toEqual([{ ...base, binary: 'dbt', subcommand: null }]) + expect(shape(`dbt ls -s "tag:nightly" --select 'path:models/staging'`)).toEqual([ + { ...base, binary: 'dbt', subcommand: 'ls', select: ['tag:nightly', 'path:models/staging'] }, + ]) +}) + +test('a heredoc, or an operator or redirection with nothing after it, makes the line unreadable', () => { + expect(parseDbtCommand('cat > run.sh < x.sh\ndbt build\nEOF\nbash x.sh")).toEqual([]) + expect(parseDbtCommand('cat <<-EOF\n\tdbt run\n\tEOF')).toEqual([]) + for (const line of ['dbt run >', 'dbt run &&', 'dbt run |', '&& dbt run', '; dbt run', 'dbt run )', 'dbt run (', '(dbt run', 'dbt run ; ;']) + expect(parseDbtCommand(line)).toEqual([]) + // A here-string is no heredoc; a subshell, a function and a trailing ; or & are fine. + expect(shape('dbt run <<< y')).toEqual([{ ...base, binary: 'dbt', subcommand: 'run' }]) + expect(shape('(cd a && dbt run); f() { dbt test; }; dbt ls &')).toEqual([ + { ...base, binary: 'dbt', subcommand: 'run' }, + { ...base, binary: 'dbt', subcommand: 'test' }, + { ...base, binary: 'dbt', subcommand: 'ls' }, + ]) +}) + +test('parentheses inside a quoted string in a substitution are text', () => { + expect(shape('dbt run $(printf "(") -t dev')).toEqual([{ ...base, binary: 'dbt', subcommand: 'run', target: 'dev' }]) + expect(shape('dbt run "$(printf ")")" -t dev')).toEqual([{ ...base, binary: 'dbt', subcommand: 'run', target: 'dev' }]) +}) + +test('short options joined to their value, and global options that take one before the subcommand', () => { + expect(shape('dbt run -tprod -sorders -musers')).toEqual([{ ...base, binary: 'dbt', subcommand: 'run', select: ['orders', 'users'], target: 'prod' }]) + expect(shape(`dbt --warn-error-options '{"include":"all"}' -r timing.txt run -s a`)).toEqual([ + { ...base, binary: 'dbt', subcommand: 'run', select: ['a'] }, + ]) + expect(shape('dbt build --indirect-selection cautious -s a')).toEqual([{ ...base, binary: 'dbt', subcommand: 'build', select: ['a'] }]) +}) + +test('env options that take a value; DBT_TARGET and DBT_TARGET_PATH in front of dbt, which the flags override', () => { + expect(shape('env -i -u FOO dbt run')).toEqual([{ ...base, binary: 'dbt', subcommand: 'run' }]) + expect(shape('DBT_TARGET=prod DBT_TARGET_PATH=tgt dbt run')).toEqual([{ ...base, binary: 'dbt', subcommand: 'run', target: 'prod', targetPath: 'tgt' }]) + expect(shape('DBT_TARGET=prod dbt run -t dev')).toEqual([{ ...base, binary: 'dbt', subcommand: 'run', target: 'dev' }]) +}) + +test('subshells after a keyword, case patterns, process substitution, arrays and arithmetic are read', () => { + const run = { ...base, binary: 'dbt', subcommand: 'run' } + expect(shape('time (dbt build)')).toEqual([{ ...base, binary: 'dbt', subcommand: 'build' }]) + for (const line of [ + 'if (cd proj && dbt run); then echo ok; fi', + '! (dbt run)', + '{ (dbt run); }', + 'dbt run 2> >(tee err.log)', + 'for ((i=0;i<3;i++)); do dbt run; done', + 'f() ( dbt run )', + 'case $t in\n a|b) dbt run;;\nesac', + ]) + expect(shape(line)).toEqual([run]) + expect(shape('case $t in prod) dbt run -t prod;; *) dbt run;; esac')).toEqual([{ ...run, target: 'prod' }, run]) + expect(shape('case $t in (prod) dbt run -t prod;; esac')).toEqual([{ ...run, target: 'prod' }]) + expect(shape('models=(orders users); for m in "${models[@]}"; do dbt run -s $m; done')).toEqual([{ ...run, select: ['$m'] }]) + const line = 'diff <(dbt ls -s a) <(dbt ls -s b)' + const found = parseDbtCommand(line) + expect(found.map(({ start, end }) => line.slice(start, end))).toEqual(['dbt ls -s a', 'dbt ls -s b']) +}) + +test('a function definition takes one name: `dbt run ()` is unreadable and a function named dbt is no invocation', () => { + expect(parseDbtCommand('dbt run ()')).toEqual([]) + expect(parseDbtCommand('case x in a) dbt run')).toEqual([]) + expect(shape('dbt() { echo hi; }; dbt test')).toEqual([{ ...base, binary: 'dbt', subcommand: 'test' }]) + expect(shape('function f() { dbt run; }')).toEqual([{ ...base, binary: 'dbt', subcommand: 'run' }]) +}) + +test('a case pattern named dbt is no invocation', () => { + const run = { ...base, binary: 'dbt', subcommand: 'run' } + for (const line of ['case $t in a) x;; dbt) dbt run;; esac', 'case $t in a|dbt) dbt run;; esac', 'case $t in (dbt) dbt run;; esac']) + expect(shape(line)).toEqual([run]) +}) + +test("quotes in a heredoc inside a substitution are text, so Claude's commit form stays readable", () => { + const line = `dbt run -s a && git commit -m "$(cat <<'EOF'\nfix "foo it's\nEOF\n)"` + expect(shape(line)).toEqual([{ ...base, binary: 'dbt', subcommand: 'run', select: ['a'] }]) +}) diff --git a/plugins/altimate-code/hooks/dbt-command.ts b/plugins/altimate-code/hooks/dbt-command.ts new file mode 100644 index 0000000..6c77970 --- /dev/null +++ b/plugins/altimate-code/hooks/dbt-command.ts @@ -0,0 +1,535 @@ +// The dbt invocations in a shell command line, as Claude's Bash calls write them: `dbt build -s x`, +// `cd a && dbt run`, `uv run dbt test | tee log`, `DBT_PROFILES_DIR=. dbt run`. Each comes with its +// place in the line, so a caller can rewrite one without touching the rest. A line that can't be read +// (an unclosed quote, substitution or parenthesis, an operator or redirection with nothing after it, a +// heredoc outside a `$(…)` substitution) yields no invocation. + +export type DbtInvocation = { + /** How dbt is launched, as written: `dbt`, `.venv/bin/dbt`, `uv run dbt`, `poetry run dbt`. */ + binary: string + /** The first word after the launcher's options: `build`, `run`, `test`…; null when there is none. */ + subcommand: string | null + /** Every value given to -s, --select, -m, --models or --model, in order. */ + select: string[] + /** The value of -t or --target, else of DBT_TARGET set in front of the command; null when neither. */ + target: string | null + /** The value of --target-path, else of DBT_TARGET_PATH set in front of the command; null when neither. */ + targetPath: string | null + /** --defer or --no-defer is given, or DBT_DEFER is set in front of the command. */ + hasDefer: boolean + /** --state or --defer-state is given, or DBT_STATE / DBT_DEFER_STATE is set in front of the command. */ + hasState: boolean + /** Offset of the invocation's first character (its leading VAR=x assignments included). */ + start: number + /** Offset just past its last argument (redirections and what follows a pipe excluded). */ + end: number + /** Offset just past the subcommand (past `binary` when there is none): where flags can be inserted. */ + subcommandEnd: number +} + +type Token = { kind: 'word'; text: string; start: number; end: number } | { kind: 'op'; text: string; start: number; end: number } + +const OPERATORS = ['&&', '||', ';;', '|&', '|', '&', ';', '(', ')', '\n'] +const ARRAY_ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*(?:\[[^\]]*\])?\+?=$/ +const REDIRECT = /^(?:\d*(?:>>|>&|>\||>|<&|<<<|<<|<>|<)|&>>?)/ + +const HEREDOC = /^<<(-?)[ \t]*(?:'([^'\n]*)'|"([^"\n]*)"|([^\s;&|()<>'"]+))/ + +/** + * Offset of the newline that ends a heredoc's closing delimiter line, where `from` is the newline before + * the body; -1 when the delimiter never comes or nothing follows it. + */ +function heredocBodyEnd(line: string, from: number, delimiter: string, strip: boolean): number { + for (let at = from + 1; ; ) { + const eol = line.indexOf('\n', at) + if (eol < 0) return -1 + const row = line.slice(at, eol) + if ((strip ? row.replace(/^\t+/, '') : row) === delimiter) return eol + at = eol + 1 + } +} + +/** Offset just past the `$(…)` or backtick substitution that opens at `at`; -1 when it is not closed. */ +function substitutionEnd(line: string, at: number): number { + if (line[at] === '`') { + const close = line.indexOf('`', at + 1) + return close < 0 ? -1 : close + 1 + } + // `at` is the opening parenthesis: it counts as the first level. + let depth = 0 + // Heredocs opened on the line being read; their bodies start on the next line. + const heredocs: { delimiter: string; strip: boolean }[] = [] + for (let j = at; j < line.length; j++) { + const c = line[j] + if (c === '\n' && heredocs.length) { + for (const { delimiter, strip } of heredocs.splice(0)) { + j = heredocBodyEnd(line, j, delimiter, strip) + if (j < 0) return -1 + } + } else if (c === '<' && line[j + 1] === '<' && line[j + 2] !== '<') { + // A heredoc's body is text: its quotes and parentheses are not read. + const heredoc = HEREDOC.exec(line.slice(j)) + if (heredoc) { + heredocs.push({ delimiter: heredoc[2] ?? heredoc[3] ?? heredoc[4]!, strip: heredoc[1] === '-' }) + j += heredoc[0].length - 1 + } + } else if (c === '\\') j++ + else if (c === "'") { + j = line.indexOf("'", j + 1) + if (j < 0) return -1 + } else if (c === '"') { + // A double-quoted string: its parentheses are text, but a substitution inside it still nests. + for (j++; j < line.length && line[j] !== '"'; j++) { + if (line[j] === '\\') j++ + else if (line[j] === '$' && line[j + 1] === '(') { + const end = substitutionEnd(line, j + 1) + if (end < 0) return -1 + j = end - 1 + } + } + if (j >= line.length) return -1 + } else if (c === '(') depth++ + else if (c === ')' && --depth === 0) return j + 1 + } + return -1 +} + +/** The words and operators of a command line, with their offsets; null when a quote or substitution is not closed. */ +function lex(line: string): Token[] | null { + const tokens: Token[] = [] + let i = 0 + let text = '' + let start = -1 + const flush = (end: number) => { + if (start >= 0) tokens.push({ kind: 'word', text, start, end }) + text = '' + start = -1 + } + while (i < line.length) { + const c = line[i]! + if (c === '\\' && line[i + 1] === '\n') { + i += 2 + continue + } + if (c === ' ' || c === '\t' || c === '\r') { + flush(i) + i++ + continue + } + if (c === '#' && start < 0) { + while (i < line.length && line[i] !== '\n') i++ + continue + } + // `<(…)` and `>(…)` (process substitution), `((…))` (arithmetic) and the `(…)` of `name=(…)` (an + // array) are one word, up to the matching parenthesis. + const paren = + (c === '<' || c === '>') && line[i + 1] === '(' && start < 0 + ? i + 1 + : c === '(' && (start < 0 ? line[i + 1] === '(' : ARRAY_ASSIGNMENT.test(text)) + ? i + : -1 + if (paren >= 0) { + const end = substitutionEnd(line, paren) + if (end < 0) return null + if (start < 0) start = i + text += line.slice(i, end) + i = end + continue + } + const redirect = REDIRECT.exec(line.slice(i)) + if (redirect && (start < 0 || !/^\d/.test(redirect[0]))) { + flush(i) + tokens.push({ kind: 'op', text: redirect[0], start: i, end: i + redirect[0].length }) + i += redirect[0].length + continue + } + const op = OPERATORS.find(one => line.startsWith(one, i)) + if (op) { + flush(i) + tokens.push({ kind: 'op', text: op, start: i, end: i + op.length }) + i += op.length + continue + } + if (start < 0) start = i + if (c === '\\') { + text += line[i + 1] ?? '' + i += 2 + } else if (c === "'") { + const close = line.indexOf("'", i + 1) + if (close < 0) return null + text += line.slice(i + 1, close) + i = close + 1 + } else if (c === '"') { + let j = i + 1 + while (j < line.length && line[j] !== '"') { + if (line[j] === '\\' && j + 1 < line.length) { + text += line[j + 1] + j += 2 + } else if ((line[j] === '$' && line[j + 1] === '(') || line[j] === '`') { + const end = substitutionEnd(line, line[j] === '$' ? j + 1 : j) + if (end < 0) return null + text += line.slice(j, end) + j = end + } else text += line[j++] + } + if (j >= line.length) return null + i = j + 1 + } else if ((c === '$' && line[i + 1] === '(') || c === '`') { + const end = substitutionEnd(line, c === '$' ? i + 1 : i) + if (end < 0) return null + text += line.slice(i, end) + i = end + } else { + text += c + i++ + } + } + flush(i) + return tokens +} + +const ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/ +// Words that may stand in front of a command without being it. +const PREFIX_WORDS = new Set(['!', '{', 'then', 'do', 'else', 'elif', 'if', 'while', 'until', 'time', 'command', 'exec', 'nohup', 'env']) +// Options of those prefix words that take a value, so the value is not read as the command. +const PREFIX_VALUE_FLAGS = new Set(['-u', '--unset', '-C', '--chdir', '-f', '--format', '-o', '--output', '-a']) +// Launchers whose `run` subcommand runs the command after their own options. +const RUNNERS = new Set(['uv', 'poetry', 'pipenv', 'pdm', 'hatch', 'rye', 'uvx']) +// Launcher options that take a value, so the value is not read as the command. +const RUNNER_VALUE_FLAGS = new Set([ + '--with', + '--with-editable', + '--with-requirements', + '--project', + '--directory', + '--python', + '-p', + '--env-file', + '--extra', + '--group', + '--package', + '--index', + '--index-url', + '--from', + '-e', + '--env', + '-C', +]) +// dbt options that take a value, so the value is not read as the subcommand or as a selector. +const DBT_VALUE_FLAGS = new Set([ + '--log-format', + '--log-format-file', + '--log-level', + '--log-level-file', + '--log-path', + '--profiles-dir', + '--project-dir', + '--profile', + '--target', + '-t', + '--target-path', + '--vars', + '--state', + '--defer-state', + '--threads', + '--printer-width', + '--exclude', + '--selector', + '--resource-type', + '--resource-types', + '--exclude-resource-type', + '--exclude-resource-types', + '--output', + '-o', + '--output-keys', + '--inline', + '--limit', + '--args', + '--macro', + '--warn-error-options', + '--record-timing-info', + '-r', + '--indirect-selection', + '--event-time-start', + '--event-time-end', + '--sample', + '--log-file-max-bytes', + '--add-package', + '--source', +]) +// Short dbt options that take a value, which may also be written joined to it: `-tprod`, `-sorders`. +const SHORT_VALUE_FLAGS = new Set(['-s', '-m', '-t', '-o', '-r']) +const SELECT_FLAGS = new Set(['-s', '--select', '-m', '--models', '--model']) + +const isDbt = (word: string) => word.split('/').pop() === 'dbt' + +/** The dbt invocations in a shell command line, in the order written; [] when there are none or the line can't be read. */ +export function parseDbtCommand(line: string): DbtInvocation[] { + if (typeof line !== 'string' || !/\bdbt\b/.test(line)) return [] + const tokens = lex(line) + const notCommand = tokens && readable(tokens) + if (!tokens || !notCommand) return [] + const found: DbtInvocation[] = [] + let simple: Token[] = [] + const finish = () => { + const invocation = readSimple(simple) + if (invocation) found.push(invocation) + simple = [] + } + for (let k = 0; k < tokens.length; k++) { + const token = tokens[k]! + if (notCommand.has(k)) continue + if (token.kind === 'op' && !/[<>]/.test(token.text)) finish() + else simple.push(token) + // The commands of a process substitution (`<(…)`, `>(…)`) run too: they are read as a line of their own. + if (token.kind === 'word' && /^[<>]\(/.test(line.slice(token.start, token.end))) { + const shift = token.start + 2 + for (const inner of parseDbtCommand(line.slice(shift, token.end - 1))) + found.push({ ...inner, start: inner.start + shift, end: inner.end + shift, subcommandEnd: inner.subcommandEnd + shift }) + } + } + finish() + return found +} + +const isRedirect = (token: Token) => token.kind === 'op' && /[<>]/.test(token.text) + +// Words after which a command still starts, so a `(` there opens a subshell. +const COMMAND_KEYWORDS = new Set(['!', '{', 'then', 'do', 'else', 'elif', 'if', 'while', 'until', 'time']) +const NAME = /^[A-Za-z_][A-Za-z0-9_-]*$/ + +/** + * Which words are not part of a command (`case` patterns, function names) when the tokens make a command + * line the shell would run; null when they don't. A line the shell would run is one where every + * redirection has a target, every `&&`, `||` and pipe has a command on both sides, `;` and `&` follow a command, parentheses pair up + * (subshells, `name()` function definitions, `case` patterns), and a `case` reaches its `esac`. A + * heredoc counts as unreadable, since its body lines are text, not commands (one inside `$(…)` is skipped + * when the substitution is read). + */ +function readable(tokens: Token[]): Set | null { + // Indices of words that are not part of a command: `case` patterns and function names. + const notCommand = new Set() + // Open subshells and `case` statements, innermost last. + const open: ('(' | 'case')[] = [] + // Words of the command being read, its leading keywords not counted; whether an operator still waits + // for a command; whether a `case` pattern is being read, up to its `)`; and how far a `case` head + // (`case WORD in`) has been read. + let words: string[] = [] + let waiting = false + let pattern = false + let caseHead: 'none' | 'case' | 'word' = 'none' + for (let k = 0; k < tokens.length; k++) { + const token = tokens[k]! + if (token.kind === 'word') { + waiting = false + if (caseHead === 'case') caseHead = 'word' + else if (caseHead === 'word') { + if (token.text !== 'in') return false + caseHead = 'none' + pattern = true + } else if (!words.length && token.text === 'esac' && open.at(-1) === 'case') { + open.pop() + pattern = false + words = [token.text] + } else if (pattern) { + words.push(token.text) + notCommand.add(k) + } + else if (!words.length && token.text === 'case') { + open.push('case') + caseHead = 'case' + } else if (words.length || !COMMAND_KEYWORDS.has(token.text)) words.push(token.text) + continue + } + if (isRedirect(token)) { + if (/^\d*<<$/.test(token.text)) return false + if (tokens[k + 1]?.kind !== 'word') return false + // The target is not the command's word, but a redirection alone is a command (`> out`). + k++ + words.push(token.text) + waiting = false + continue + } + if (caseHead !== 'none' && token.text !== '\n') return false + switch (token.text) { + case '\n': + if (!waiting && !pattern) words = [] + continue + case '(': + // A pattern may open with `(`; where a command starts, `(` opens a subshell; after one name it + // defines a function, whose body follows. + if (pattern && !words.length) continue + if (!words.length) open.push('(') + else if ( + tokens[k + 1]?.text === ')' && + NAME.test(words.at(-1)!) && + (words.length === 1 || (words.length === 2 && words[0] === 'function')) + ) { + notCommand.add(k - 1) + if (words.length === 2) notCommand.add(k - 2) + k++ + words = [] + } else return false + continue + case ')': + if (pattern) { + if (!words.length) return false + pattern = false + words = [] + continue + } + if (open.at(-1) !== '(' || waiting) return false + open.pop() + words = [token.text] + continue + case ';;': + if (open.at(-1) !== 'case' || pattern) return false + words = [] + pattern = true + continue + case '|': + // Alternatives in a pattern. + if (pattern) { + if (!words.length) return false + words = [] + continue + } + // falls through + case '&&': + case '||': + case '|&': + if (!words.length || pattern) return false + words = [] + waiting = true + continue + default: + // `;` and `&` end the command before them. + if (!words.length || pattern) return false + words = [] + } + } + return !open.length && !waiting && !pattern && caseHead === 'none' ? notCommand : null +} + +function readSimple(tokens: Token[]): DbtInvocation | null { + // Drop redirections and their targets: what is left is the command's words. + const words: Extract[] = [] + for (let i = 0; i < tokens.length; i++) { + const token = tokens[i]! + if (token.kind === 'op') { + // A redirection takes the next word as its target (`> log`, `2>&1`). + i++ + continue + } + words.push(token) + } + if (words.length === 0) return null + const start = words[0]!.start + let i = 0 + let hasDefer = false + let hasState = false + let envTarget: string | null = null + let envTargetPath: string | null = null + // The prefix word whose options are being read (`env -u X`, `time -p`). + let prefix: string | null = null + for (; i < words.length; i++) { + const word = words[i]!.text + if (ASSIGNMENT.test(word)) { + const name = word.slice(0, word.indexOf('=')) + const assigned = word.slice(word.indexOf('=') + 1) + if (name === 'DBT_DEFER') hasDefer = true + if (name === 'DBT_STATE' || name === 'DBT_DEFER_STATE') hasState = true + if (name === 'DBT_TARGET') envTarget = assigned + if (name === 'DBT_TARGET_PATH') envTargetPath = assigned + prefix = null + continue + } + if (PREFIX_WORDS.has(word)) { + prefix = word + continue + } + if (word.startsWith('-') && prefix !== null) { + if (PREFIX_VALUE_FLAGS.has(word)) i++ + continue + } + break + } + if (i >= words.length) return null + const launcherStart = i + let binaryEnd: number + if (isDbt(words[i]!.text)) { + binaryEnd = i + } else if (RUNNERS.has(words[i]!.text.split('/').pop()!)) { + const runner = words[i]!.text.split('/').pop()! + i++ + if (runner !== 'uvx') { + if (words[i]?.text !== 'run') return null + i++ + } + while (i < words.length && words[i]!.text.startsWith('-')) { + const flag = words[i]!.text + i += RUNNER_VALUE_FLAGS.has(flag) && !flag.includes('=') ? 2 : 1 + } + // `uv run --` ends the launcher's options. + if (words[i]?.text === '--') i++ + if (i >= words.length || !isDbt(words[i]!.text)) return null + binaryEnd = i + } else return null + + const binary = words + .slice(launcherStart, binaryEnd + 1) + .map(one => one.text) + .join(' ') + let subcommand: string | null = null + let subcommandEnd = words[binaryEnd]!.end + const select: string[] = [] + let target: string | null = null + let targetPath: string | null = null + let selecting = false + for (let j = binaryEnd + 1; j < words.length; j++) { + const word = words[j]!.text + if (word.startsWith('-')) { + selecting = false + const eq = word.indexOf('=') + const joined = !word.startsWith('--') && word.length > 2 && SHORT_VALUE_FLAGS.has(word.slice(0, 2)) + const flag = joined ? word.slice(0, 2) : eq > 0 ? word.slice(0, eq) : word + const inline = joined ? word.slice(2) : eq > 0 ? word.slice(eq + 1) : null + const value = () => { + if (inline !== null) return inline + j++ + return words[j]?.text ?? null + } + if (SELECT_FLAGS.has(flag)) { + if (inline !== null) select.push(...inline.split(/\s+/).filter(Boolean)) + selecting = true + } else if (flag === '-t' || flag === '--target') target = value() + else if (flag === '--target-path') targetPath = value() + else if (flag === '--defer' || flag === '--no-defer') hasDefer = true + else if (flag === '--state' || flag === '--defer-state') { + hasState = true + value() + } else if (DBT_VALUE_FLAGS.has(flag) && inline === null) j++ + continue + } + if (selecting) { + select.push(...word.split(/\s+/).filter(Boolean)) + continue + } + if (subcommand === null) { + subcommand = word + subcommandEnd = words[j]!.end + } + } + return { + binary, + subcommand, + select, + target: target ?? envTarget, + targetPath: targetPath ?? envTargetPath, + hasDefer, + hasState, + start, + end: words[words.length - 1]!.end, + subcommandEnd, + } +} diff --git a/plugins/altimate-code/hooks/features/comments.tsx b/plugins/altimate-code/hooks/features/comments.tsx new file mode 100644 index 0000000..ecd7564 --- /dev/null +++ b/plugins/altimate-code/hooks/features/comments.tsx @@ -0,0 +1,53 @@ +// Team comments from shared dbt docs, in Claude's context and in /comments. +// +// 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($, 'comments', text)`. +import type { Args, EngineInterface, EventResult, On, RenderElement, ToolCallResult } from 'claude-code' + +import type { BashCall, ContextLines, EditedFile, FileCall } from '../tool-chain' +import type { BandUi, FeatureContext } from './index' + +/** Adds this feature's own hooks. */ +export function register(on: On, context: FeatureContext) {} + +/** After the session started: the place to register commands. */ +export async function sessionStart($: EngineInterface, e: Args<'session.start'>) {} + +/** 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'>) {} + +export async function sessionEnd($: EngineInterface, e: Args<'session.end'>) {} + +/** 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 +} + +/** 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. 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 }`. + */ +export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> { + return null +} + +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 {} + +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). */ +export const editReadsBefore = false diff --git a/plugins/altimate-code/hooks/features/cost.test.tsx b/plugins/altimate-code/hooks/features/cost.test.tsx new file mode 100644 index 0000000..38588f3 --- /dev/null +++ b/plugins/altimate-code/hooks/features/cost.test.tsx @@ -0,0 +1,387 @@ +import { expect, mock, test } from 'claude-code/testing' + +import { formatBytes, inlineSql } from './cost' + +const ROOT = '/repo' +const GIB = 1024 ** 3 + +type Options = { + /** What dbt_cost_estimate answers for a model or SQL, by model name or the SQL text. */ + answers?: Record | 'unknown-tool'> + /** The adapter's answer for everything else. */ + fallback?: Record +} + +/** The session beneath the plugin, a CLI whose dbt_cost_estimate is scripted, and a Bash tool that records what ran. */ +async function start($: any, on: any, options: Options = {}) { + const clock = mock.clock(on) + const estimates: string[] = [] + const ran: string[] = [] + const statuses: (string | undefined)[] = [] + on('command.register', (_: unknown, e: { name: string }) => ({ value: { command: e.name } })) + 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.log', () => ({ value: undefined })) + on('ui.status', (_: unknown, e: { text?: string }) => { + statuses.push(e.text) + return { value: undefined } + }) + on('ui.render', ($$: any, e: any) => { + const { Box } = $$.ui.resolve(e) + return + }) + on('fs.exists', () => ({ value: false })) + 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' } } + expect(tool).toBe('dbt_cost_estimate') + // The adapter starts the project's dbt: the call must carry write permission. + expect(e.argv).toContain('--allow-write') + const args = JSON.parse(String(e.argv[e.argv.indexOf('--args') + 1] ?? '{}')) + const key = String(args.model ?? args.sql) + estimates.push(key) + const answer = options.answers?.[key] ?? options.fallback + if (answer === 'unknown-tool') { + return { value: { exitCode: 1, stdout: JSON.stringify({ status: 'error', output: `Unknown tool: ${tool}. Run \`altimate-code tool list\` to see the available tools.`, metadata: {} }), stderr: '' } } + } + return { value: { exitCode: 0, stdout: JSON.stringify({ status: 'ok', output: '', metadata: answer ?? { success: false, error: 'no answer scripted' } }), stderr: '' } } + }) + // The Bash tool itself, beneath the plugin. + on('tool.call', (_: unknown, e: { tool: string; command?: string }) => { + if (e.tool === 'Bash') ran.push(String(e.command)) + return { result: { text: 'ok', isError: false } } + }) + await $.session.start({ cwd: ROOT, surface: 'terminal', isInteractive: true }) + await clock.settle() + return { clock, estimates, ran, statuses } +} + +const bq = (bytes: number, display: string, maxBytes = bytes) => ({ success: true, supported: true, adapter: 'bigquery', bytes, maxBytes, display }) +const OVER = bq(20 * GIB, '20.0 GiB') +const UNDER = bq(2 * GIB, '2.0 GiB') + +const bash = (command: string) => ({ tool: 'Bash' as const, command }) + +test('a model over the limit is refused with its estimate, and the command never runs', async ($, on) => { + const { clock, estimates, ran } = await start($, on, { answers: { orders: OVER } }) + const result: any = await $.tool.call(bash('dbt run --select orders')) + await clock.settle() + + expect(estimates).toEqual(['orders']) + expect(ran).toEqual([]) + expect(result.deny).toBe('≈ 20.0 GiB would be scanned, over the 10.0 GiB limit. Narrow the query, or ask the user to run /allow-scan and then run the same command again.') +}) + +test('a model under the limit runs, with the estimate in a line under the row', async ($, on) => { + const { clock, ran } = await start($, on, { answers: { orders: UNDER } }) + const result: any = await $.tool.call(bash('dbt build -s orders')) + await clock.settle() + + expect(ran).toEqual(['dbt build -s orders']) + expect(result.context).toEqual(['cost · orders would scan 2.0 GiB']) +}) + +test('dbt show --inline is estimated from its SQL', async ($, on) => { + const { clock, estimates } = await start($, on, { answers: { 'select * from big': OVER } }) + const result: any = await $.tool.call(bash(`dbt show --inline "select * from big"`)) + await clock.settle() + + expect(estimates).toEqual(['select * from big']) + expect(result.deny).toContain('20.0 GiB') +}) + +test('/allow-scan lets the next held command through once, then the one after is held again', async ($, on) => { + const { clock, estimates, ran } = await start($, on, { answers: { orders: OVER } }) + expect((await $.tool.call(bash('dbt run -s orders')) as any).deny).toBeDefined() + + const allowed = await $.command.run({ command: 'allow-scan', args: '' } as any) + expect(allowed.text).toContain('runs once') + const through: any = await $.tool.call(bash('dbt run -s orders')) + await clock.settle() + expect(through.deny).toBeUndefined() + expect(through.context).toEqual(['cost · orders would scan 20.0 GiB, over the 10.0 GiB limit: allowed once']) + expect(ran).toEqual(['dbt run -s orders']) + + expect(((await $.tool.call(bash('dbt run -s orders'))) as any).deny).toBeDefined() + expect(estimates).toHaveLength(3) +}) + +test('a selection that is not one plain model passes, saying there is no estimate, and asks nothing', async ($, on) => { + const { clock, estimates, ran } = await start($, on) + for (const [command, why] of [ + ['dbt run', 'no model named'], + ['dbt build -s +orders', 'graph, tag, path or package selector'], + ['dbt run -s tag:nightly', 'graph, tag, path or package selector'], + ['dbt run -s a b', 'several selectors'], + ] as const) { + const result: any = await $.tool.call(bash(command)) + expect(result.deny).toBeUndefined() + expect(result.context[0]).toContain('cost · no estimate for this selection') + expect(result.context[0]).toContain(why) + } + await clock.settle() + expect(estimates).toEqual([]) + expect(ran).toHaveLength(4) +}) + +test('commands that scan nothing are not looked at', async ($, on) => { + const { clock, estimates, ran } = await start($, on) + for (const command of ['dbt test -s orders', 'dbt compile -s orders', 'ls', 'git status']) await $.tool.call(bash(command)) + await clock.settle() + expect(estimates).toEqual([]) + expect(ran).toHaveLength(4) +}) + +test('another warehouse is told once, never held, and not asked again', async ($, on) => { + const { clock, estimates, ran } = await start($, on, { fallback: { success: true, supported: false, adapter: 'snowflake' } }) + const first: any = await $.tool.call(bash('dbt run -s orders')) + const second: any = await $.tool.call(bash('dbt run -s customers')) + await clock.settle() + + expect(first.deny).toBeUndefined() + expect(first.context).toEqual(['cost · no estimate on snowflake: only BigQuery has a dry run']) + expect(second.deny).toBeUndefined() + expect(estimates).toEqual(['orders']) + expect(ran).toEqual(['dbt run -s orders', 'dbt run -s customers']) +}) + +test('an altimate-code without the tool shows one line saying what is needed, and the command runs', async ($, on) => { + const { clock, ran } = await start($, on, { answers: { orders: 'unknown-tool' } }) + const result: any = await $.tool.call(bash('dbt run -s orders')) + await clock.settle() + + expect(ran).toEqual(['dbt run -s orders']) + expect(result.context).toHaveLength(1) + expect(result.context[0]).toContain('needs a newer altimate-code') +}) + +test('a dry run that failed passes the command with the reason', async ($, on) => { + const { clock, ran } = await start($, on, { answers: { orders: { success: false, error: 'Access Denied: BigQuery job' } } }) + const result: any = await $.tool.call(bash('dbt run -s orders')) + await clock.settle() + + expect(ran).toEqual(['dbt run -s orders']) + expect(result.context).toEqual(['cost · no estimate for orders: Access Denied: BigQuery job']) +}) + +test('/scan-cost estimates a model on demand and says how it stands against the limit', async ($, on) => { + const { clock, estimates, ran } = await start($, on, { answers: { orders: OVER, customers: UNDER } }) + expect((await $.command.run({ command: 'scan-cost', args: 'orders' } as any)).text).toBe( + 'orders would scan 20.0 GiB, over the 10.0 GiB limit: a dbt run of it is held until /allow-scan.', + ) + expect((await $.command.run({ command: 'scan-cost', args: 'customers' } as any)).text).toBe('customers would scan 2.0 GiB, within the 10.0 GiB limit.') + expect((await $.command.run({ command: 'scan-cost', args: '' } as any)).text).toContain('Last estimate: customers') + await clock.settle() + expect(estimates).toEqual(['orders', 'customers']) + expect(ran).toEqual([]) +}) + +test('a command naming two models over the limit is held whole, and one /allow-scan lets all of it through once', async ($, on) => { + const { clock, ran } = await start($, on, { answers: { orders: OVER, customers: bq(30 * GIB, '30.0 GiB') } }) + const command = 'dbt run -s orders && dbt run -s customers' + expect(((await $.tool.call(bash(command))) as any).deny).toContain('30.0 GiB') + await $.command.run({ command: 'allow-scan', args: '' } as any) + const through: any = await $.tool.call(bash(command)) + await clock.settle() + + expect(through.deny).toBeUndefined() + expect(through.context).toEqual([ + 'cost · orders would scan 20.0 GiB, over the 10.0 GiB limit: allowed once', + 'cost · customers would scan 30.0 GiB, over the 10.0 GiB limit: allowed once', + ]) + expect(ran).toEqual([command]) + expect(((await $.tool.call(bash(command))) as any).deny).toBeDefined() +}) + +test('/allow-scan with nothing held allows nothing', async ($, on) => { + const { clock } = await start($, on, { answers: { orders: OVER } }) + expect((await $.command.run({ command: 'allow-scan', args: '' } as any)).text).toContain('Nothing is held') + expect(((await $.tool.call(bash('dbt run -s orders'))) as any).deny).toBeDefined() + await clock.settle() +}) + +test('the allowance is for the command that was held: another over-limit command is held, and the allowance is gone', async ($, on) => { + const { clock, ran } = await start($, on, { answers: { orders: OVER, customers: OVER } }) + expect(((await $.tool.call(bash('dbt run -s orders'))) as any).deny).toBeDefined() + await $.command.run({ command: 'allow-scan', args: '' } as any) + + expect(((await $.tool.call(bash('dbt run -s customers'))) as any).deny).toBeDefined() + expect(((await $.tool.call(bash('dbt run -s orders'))) as any).deny).toBeDefined() + await clock.settle() + expect(ran).toEqual([]) +}) + +test('an estimate whose rounded size is within the limit but whose upper bound is over it is held', async ($, on) => { + const { clock } = await start($, on, { answers: { orders: bq(10 * GIB, '10.0 GiB', 10 * GIB + 52 * 1024 ** 2) } }) + expect(((await $.tool.call(bash('dbt run -s orders'))) as any).deny).toBe('up to 10.1 GiB (≈ 10.0 GiB estimated) would be scanned, over the 10.0 GiB limit. Narrow the query, or ask the user to run /allow-scan and then run the same command again.') + await clock.settle() +}) + +test('a warehouse found to have no dry run ends a hold from before, and its allowance', async ($, on) => { + const answers: Record = { orders: OVER } + const { clock, ran, statuses } = await start($, on, { answers }) + expect(((await $.tool.call(bash('dbt run -s orders'))) as any).deny).toBeDefined() + await $.command.run({ command: 'allow-scan', args: '' } as any) + answers.orders = { success: true, supported: false, adapter: 'snowflake' } + await $.tool.call(bash('dbt run -s orders')) + await clock.settle() + expect(ran).toEqual(['dbt run -s orders']) + expect(statuses.at(-1) ?? '').not.toContain('held') + expect((await $.command.run({ command: 'allow-scan', args: '' } as any)).text).toContain('Nothing is held') +}) + +test('a scan just over the limit reads as over it, not as equal to it', async ($, on) => { + const { clock } = await start($, on, { answers: { orders: bq(10 * GIB + 1024 ** 2, '10.0 GiB') } }) + expect(((await $.tool.call(bash('dbt run -s orders'))) as any).deny).toBe('just over 10.0 GiB would be scanned, over the 10.0 GiB limit. Narrow the query, or ask the user to run /allow-scan and then run the same command again.') + await clock.settle() +}) + +test('a command that sets its own target, defer state, vars or project is not estimated', async ($, on) => { + const { clock, estimates, ran } = await start($, on, { answers: { orders: OVER } }) + const commands = [ + 'dbt run -s orders --target prod', + 'dbt run -s orders --defer --state prod-run', + `dbt run -s orders --vars '{a: 1}'`, + 'dbt run -s orders --project-dir other', + 'DBT_PROFILES_DIR=ci dbt run -s orders', + 'cd other && dbt run -s orders', + ' cd other && dbt run -s orders', + 'export DBT_TARGET=prod; dbt run -s orders', + 'uv run --directory other dbt run -s orders', + "dbt run -s orders --'vars' x", + 'dbt run -s orders --full-refresh', + 'dbt run -s orders --full-\\\nrefresh', + 'if true; then cd other; fi; dbt run -s orders', + 'dbt --profiles-dir ci run -s orders', + 'FOO=1 dbt run -s orders', + "dbt run -s orders --threads $(printf '4 --target prod')", + 'dbt run -s orders --threads `echo 4 --vars x`', + 'dbt run -s orders 2>&${DBT_TARGET:=1}', + 'dbt --log-path "${DBT_TARGET:=prod}" deps; dbt run -s orders', + ] + for (const command of commands) { + const result: any = await $.tool.call(bash(command)) + expect(result.deny).toBeUndefined() + expect(result.context[0]).toContain('it sets its own target, defer state, vars, project or refresh mode') + } + await clock.settle() + expect(estimates).toEqual([]) + expect(ran).toEqual(commands) +}) + +test('dbt show --inline with its SQL on the next line after a backslash is estimated', async ($, on) => { + const { clock, estimates } = await start($, on, { answers: { 'select * from big': OVER } }) + const result: any = await $.tool.call(bash(`dbt show --inline \\\n 'select * from big'`)) + await clock.settle() + expect(estimates).toEqual(['select * from big']) + expect(result.deny).toContain('20.0 GiB') +}) + +test('letting a later command through clears the hold from the status line', async ($, on) => { + const { clock, statuses } = await start($, on, { answers: { orders: OVER, customers: UNDER } }) + await $.tool.call(bash('dbt run -s orders')) + await $.tool.call(bash('dbt run -s customers')) + await clock.settle() + expect(statuses.filter(Boolean).at(-1)).not.toContain('held') +}) + +test('a variant of the held command that is not estimated leaves the hold in place', async ($, on) => { + const { clock, ran } = await start($, on, { answers: { orders: OVER } }) + expect(((await $.tool.call(bash('dbt run -s orders'))) as any).deny).toBeDefined() + await $.tool.call(bash('dbt run -s +orders')) + await $.tool.call(bash('dbt run -s orders --full-refresh')) + await clock.settle() + await $.command.run({ command: 'allow-scan', args: '' } as any) + expect(((await $.tool.call(bash('dbt run -s orders'))) as any).deny).toBeUndefined() + expect(ran.at(-1)).toBe('dbt run -s orders') +}) + +test('a repeated --inline is estimated from the last one, as dbt reads it', async ($, on) => { + const { clock, estimates } = await start($, on, { answers: { 'select * from big': OVER } }) + const result: any = await $.tool.call(bash(`dbt show --inline 'select 1' --inline 'select * from big'`)) + await clock.settle() + expect(estimates).toEqual(['select * from big']) + expect(result.deny).toContain('20.0 GiB') +}) + +test('the lines under a command are the latest estimate of it, never an earlier one', async ($, on) => { + const answers: Record = { orders: UNDER } + const { clock } = await start($, on, { answers }) + expect(((await $.tool.call(bash('dbt run -s orders'))) as any).context).toEqual(['cost · orders would scan 2.0 GiB']) + answers.orders = bq(3 * GIB, '3.0 GiB') + expect(((await $.tool.call(bash('dbt run -s orders'))) as any).context).toEqual(['cost · orders would scan 3.0 GiB']) + await clock.settle() +}) + +test('/allow-scan allows only the hold it read: a hold replaced meanwhile is not allowed', async ($, on) => { + const { clock, ran } = await start($, on, { answers: { orders: OVER, customers: OVER } }) + expect(((await $.tool.call(bash('dbt run -s orders'))) as any).deny).toBeDefined() + const allowing = $.command.run({ command: 'allow-scan', args: '' } as any) + expect(((await $.tool.call(bash('dbt run -s customers'))) as any).deny).toBeDefined() + await allowing + await clock.settle() + // Whichever order the two steps took, customers (the later hold) is not allowed by a read of orders. + expect(((await $.tool.call(bash('dbt run -s customers'))) as any).deny).toBeDefined() + expect(ran).toEqual([]) +}) + +test('a non-dbt command in between neither clears the hold nor spends the allowance', async ($, on) => { + const { clock, ran } = await start($, on, { answers: { orders: OVER } }) + await $.tool.call(bash('dbt run -s orders')) + await $.command.run({ command: 'allow-scan', args: '' } as any) + await $.tool.call(bash('ls')) + const through: any = await $.tool.call(bash('dbt run -s orders')) + await clock.settle() + expect(through.deny).toBeUndefined() + expect(ran).toEqual(['ls', 'dbt run -s orders']) +}) + +test('inlineSql reads the query as the shell would', () => { + expect(inlineSql(`dbt show --inline "select 'a' as x"`)).toBe(`select 'a' as x`) + expect(inlineSql(`dbt show --inline='select 1'`)).toBe('select 1') + expect(inlineSql(`dbt show --inline select\\ 1 --limit 5`)).toBe('select 1') + expect(inlineSql(`dbt show --inline \\\n 'select 1'`)).toBe('select 1') + expect(inlineSql('dbt show -s orders')).toBeNull() + expect(inlineSql(`dbt show --inline 'select 1' --inline='select 2'`)).toBe('select 2') + // The value of --inline is the query, even when it looks like an option. + expect(inlineSql(`dbt show --inline '--inline=comment select 1'`)).toBe('--inline=comment select 1') +}) + +test('formatBytes writes the units the dry run does', () => { + expect(formatBytes(512)).toBe('512.0 Bytes') + expect(formatBytes(1536)).toBe('1.5 KiB') + expect(formatBytes(10 * GIB)).toBe('10.0 GiB') + expect(formatBytes(1024 ** 5)).toBe('1.0 PiB') +}) + +test('what leaves the query alone is still estimated: other dbt calls before it, thread and limit flags, a launcher', async ($, on) => { + const { clock, estimates } = await start($, on, { answers: { orders: UNDER, 'select 1': UNDER } }) + for (const command of ['dbt deps && dbt run -s orders --threads 4', 'uv run dbt build --select orders --fail-fast', `dbt show --inline "select 1" --limit 5`]) { + expect(((await $.tool.call(bash(command))) as any).context[0]).toContain('would scan 2.0 GiB') + } + await clock.settle() + expect(estimates).toEqual(['orders', 'orders', 'select 1']) +}) + +test('a very long run of blank lines before the command costs no noticeable time', async ($, on) => { + const { clock } = await start($, on, { answers: { orders: UNDER } }) + const began = Date.now() + await $.tool.call(bash(`${'\n'.repeat(64000)}dbt run -s orders`)) + await clock.settle() + expect(Date.now() - began).toBeLessThan(3000) +}) + +test('a line with a great many dbt calls is not read call by call', async ($, on) => { + const { clock, estimates } = await start($, on, { answers: { orders: UNDER } }) + const began = Date.now() + const result: any = await $.tool.call(bash('dbt run -s orders;'.repeat(4000))) + await clock.settle() + expect(estimates).toEqual([]) + expect(result.context.length).toBe(6) + expect(result.context[0]).toContain('cost · no estimate') + expect(result.context[5]).toBe('cost · 3995 more notes not shown') + expect(Date.now() - began).toBeLessThan(3000) +}) diff --git a/plugins/altimate-code/hooks/features/cost.tsx b/plugins/altimate-code/hooks/features/cost.tsx new file mode 100644 index 0000000..e9b873d --- /dev/null +++ b/plugins/altimate-code/hooks/features/cost.tsx @@ -0,0 +1,375 @@ +// Cost check: BigQuery scans over the limit are held until allowed. +// +// Before a `dbt run`, `dbt build` or `dbt show` Claude runs, the gate asks altimate-code's +// dbt_cost_estimate (a BigQuery dry run) what the query would scan. Over `maxScanBytes` the command is +// refused with the estimate until the user runs /allow-scan, which lets that same command through once. +// Other warehouses have no dry run: the first answer says so, and nothing is estimated or held after that. +// Only what names its query is estimated: `dbt show --inline ` and a single plain model name +// (`-s orders`); any other selection, and any command that sets its own target, defer state, vars or +// project, runs as it is, with one line saying so under the Bash row. +import { atom, read, update } from 'claude-code' +import type { Args, EngineInterface, EventResult, On, RenderElement, ToolCallResult } from 'claude-code' + +import { runTool } from '../altimate-cli' +import { parseDbtCommand, type DbtInvocation } from '../dbt-command' +import { setStatusPart } from '../status-line' +import type { BashCall, ContextLines, EditedFile, FileCall } from '../tool-chain' +import type { CostState } from '../../types' +import type { BandUi, FeatureContext } from './index' + +/** Used when `maxScanBytes` is missing from the plugin's options: 10 GiB, as plugin.json's default. */ +const DEFAULT_MAX_SCAN_BYTES = 10 * 1024 ** 3 +/** The adapter may have to start (dbt deps, a parse) for the first estimate. */ +const ESTIMATE_BUDGET_MS = 120_000 +/** The dbt subcommands that run SQL a person pays for. */ +const SCANNING = new Set(['run', 'build', 'show']) +/** A selector that is one plain model name: no graph operator, tag, path, package or wildcard. */ +const PLAIN_MODEL = /^[A-Za-z_][A-Za-z0-9_]*$/ +/** What the CLI answers for a tool it doesn't have (altimate-code's tool-cli runner). */ +const UNKNOWN_TOOL = /Unknown tool/i +/** The altimate-code that has dbt_cost_estimate; changing it is the one edit a release needs. */ +const NEEDED_VERSION = 'a newer altimate-code (the release with dbt_cost_estimate)' + +const costAtom = atom({ plugin: 'altimate-code', key: 'cost' } as const, null as CostState | null) + +const FRESH: CostState = { unsupportedAdapter: null, lastEstimate: null, held: null, allowOnce: false } + +/** The limit in force; set by `register` from the plugin's options. 0 never holds. */ +let limitBytes = DEFAULT_MAX_SCAN_BYTES +/** The lines to show under an estimated command's Bash row, by command; each gate pass replaces the last. Taken by bashAfter. */ +const notes = new Map() + +/** Adds this feature's own hooks. */ +export function register(on: On, context: FeatureContext) { + const configured = Number((context.options as Record | undefined)?.maxScanBytes) + limitBytes = Number.isFinite(configured) && configured >= 0 ? configured : DEFAULT_MAX_SCAN_BYTES + + on('command.run', { command: 'allow-scan' }, async $ => { + // The allowance is set against the hold it was read from, in the same step: a hold replaced meanwhile is not allowed. + const held = (await read($, costAtom))?.held + let allowed = false + await update($, costAtom, prev => { + const current = prev ?? FRESH + allowed = !!held && current.held?.command === held.command + return allowed ? { ...current, allowOnce: true } : current + }) + if (!held) return { text: 'Nothing is held: no dbt command is waiting on its scan size.' } + if (!allowed) return { text: 'The held command changed meanwhile: run /allow-scan again to allow the one now held.' } + return { text: `Allowed: ${held.target} (≈ ${held.display}) runs once when Claude runs the same command again; after that it is held again.` } + }) + + on('command.run', { command: 'scan-cost' }, async ($, e) => { + const target = e.args.trim() + if (!target) { + const last = (await read($, costAtom))?.lastEstimate + return { text: last ? `Last estimate: ${last.target} would scan ${last.display}. Run /scan-cost for another.` : 'Run /scan-cost to estimate the scan of a model.' } + } + const answer = await estimate($, { model: target }, (await $.clock.now()) + ESTIMATE_BUDGET_MS) + if (answer.kind === 'bytes') { + await rememberEstimate($, target, answer.bytes, answer.display) + return { text: `${target} would scan ${answer.display}${verdict(answer.maxBytes)}` } + } + return { text: answer.kind === 'unsupported' ? `No estimate: ${answer.adapter} has no dry run (only BigQuery has).` : `No estimate: ${answer.reason}` } + }) +} + +/** After the session started: the place to register commands. */ +export async function sessionStart($: EngineInterface, e: Args<'session.start'>) { + notes.clear() + await update($, costAtom, () => ({ ...FRESH })) + await setStatusPart($, 'cost', null) + await $.command.register({ name: 'allow-scan', description: 'Let the dbt command held for its BigQuery scan size run once, when run again unchanged' }) + await $.command.register({ name: 'scan-cost', description: 'Estimate the BigQuery scan of a dbt model before running it', argumentHint: '' }) +} + +/** 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'>) {} + +export async function sessionEnd($: EngineInterface, e: Args<'session.end'>) {} + +/** This feature's line in the band: the command held, until it is allowed or a later dbt command is estimated under the limit. */ +export async function bandLine($: EngineInterface, ui: BandUi): Promise { + const held = (await read($, costAtom))?.held + if (!held) return null + return {`⛔ held: ${held.target} would scan ${held.display} (limit ${formatBytes(limitBytes)}) · /allow-scan`} +} + +/** 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. 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 }`. + */ +export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> { + if (limitBytes <= 0) return null + const all = parseDbtCommand(e.command) + const invocations = all.filter(i => i.subcommand !== null && SCANNING.has(i.subcommand)) + if (invocations.length === 0) return null + const state = (await read($, costAtom)) ?? FRESH + // A warehouse with no dry run was found out already: the command is never held, and nothing is asked again. + if (state.unsupportedAdapter) return null + + const lines: string[] = [] + let estimated = false + const over: { label: string; bytes: number; point: number; display: string }[] = [] + // One budget for the whole command, however many queries it names. + const until = (await $.clock.now()) + ESTIMATE_BUDGET_MS + for (const invocation of invocations) { + const target = targetOf(e.command, invocation, all) + if (!target.model && target.sql === undefined) { + lines.push(`cost · no estimate for this selection (${target.why})`) + continue + } + const label = target.model ?? 'the inline query' + const answer = await estimate($, target.model ? { model: target.model } : { sql: target.sql }, until) + if (answer.kind === 'unsupported') { + // Nothing can be held on this warehouse, so a hold from before the verdict goes with it. + await update($, costAtom, prev => ({ ...(prev ?? FRESH), unsupportedAdapter: answer.adapter, held: null, allowOnce: false })) + await setStatusPart($, 'cost', null) + lines.push(`cost · no estimate on ${answer.adapter}: only BigQuery has a dry run`) + over.length = 0 + break + } + if (answer.kind === 'none') { + lines.push(`cost · no estimate for ${label}: ${answer.reason}`) + continue + } + estimated = true + await rememberEstimate($, label, answer.bytes, answer.display) + if (answer.maxBytes <= limitBytes) lines.push(`cost · ${label} would scan ${answer.display}`) + else over.push({ label, bytes: answer.maxBytes, point: answer.bytes, display: answer.display }) + } + + if (over.length > 0) { + const worst = over.reduce((a, b) => (b.bytes > a.bytes ? b : a)) + // The allowance is spent in one step, and only by the command that was held for it. + let granted = false + await update($, costAtom, prev => { + const current = prev ?? FRESH + granted = current.allowOnce && current.held?.command === e.command + return granted + ? { ...current, allowOnce: false, held: null } + : { ...current, allowOnce: false, held: { command: e.command, target: worst.label, bytes: worst.bytes, display: worst.display } } + }) + if (granted) { + for (const one of over) lines.push(`cost · ${one.label} would scan ${one.display}, over the ${formatBytes(limitBytes)} limit: allowed once`) + keepNotes(e.command, lines) + return null + } + notes.delete(e.command) + await setStatusPart($, 'cost', `scan held ≈ ${worst.display}`) + // The hold is decided on the upper bound, and the display is the rounded point estimate: either can read + // as not over the limit when the scan is just past it. + const scanned = + worst.point <= limitBytes + ? `up to ${formatBytes(worst.bytes)} (≈ ${worst.display} estimated)` + : worst.display === formatBytes(limitBytes) + ? `just over ${worst.display}` + : `≈ ${worst.display}` + return { + deny: `${scanned} would be scanned, over the ${formatBytes(limitBytes)} limit. Narrow the query, or ask the user to run /allow-scan and then run the same command again.`, + } + } + // A command that was estimated and is under the limit ends an earlier hold, and its allowance. One that + // was not estimated says nothing, so the hold stays. + if (estimated && (state.held || state.allowOnce)) { + await update($, costAtom, prev => (prev?.held || prev?.allowOnce ? { ...prev, held: null, allowOnce: false } : prev)) + const last = (await read($, costAtom))?.lastEstimate + await setStatusPart($, 'cost', last ? `scan ≈ ${last.display}` : null) + } + keepNotes(e.command, lines) + return null +} + +/** Notes kept per command: a line of many dbt calls would otherwise put one note each into Claude's context. */ +const MAX_NOTES = 5 + +function keepNotes(command: string, lines: string[]) { + if (lines.length > MAX_NOTES) notes.set(command, [...lines.slice(0, MAX_NOTES), `cost · ${lines.length - MAX_NOTES} more notes not shown`]) + else if (lines.length > 0) notes.set(command, lines) + else notes.delete(command) +} + +/** What the gate said about the command, under its Bash row. */ +export async function bashAfter($: EngineInterface, e: BashCall, result: ToolCallResult): Promise { + const lines = notes.get(e.command) + notes.delete(e.command) + return lines +} + +/** `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 {} + +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). */ +export const editReadsBefore = false + +type Target = { model?: string; sql?: string; why: string } + +/** More dbt calls than this on one line are not estimated. */ +const MAX_INVOCATIONS = 20 +/** The dbt launchers whose project is the session's: nothing else on the line can move it. */ +const PLAIN_LAUNCHER = /^((uv|poetry) run )?(\S*\/)?dbt$/ +/** Flags with one value that leave the project, target and compiled SQL as they are. */ +const VALUE_FLAGS = new Set(['--inline', '--limit', '--threads']) +/** Flags that name the models; any number of values follow. */ +const SELECT_FLAGS = new Set(['-s', '--select', '-m', '--models', '--model']) +/** Flags without a value that leave the project, target and compiled SQL as they are. */ +const PLAIN_FLAGS = new Set(['--fail-fast', '-x', '--quiet', '-q']) + +/** + * Whether the estimate would be of what the invocation runs. It asks about the session's project at its + * default target, so it is only so when the line moves neither: the dbt launcher is plain, nothing + * precedes the invocation but other dbt invocations, and every flag is one known to leave the query + * alone, with no expansion on the line. Anything else (another target, defer state, vars, project, a + * full refresh) is not estimated. + */ +function isPlainInvocation(command: string, invocation: DbtInvocation, all: DbtInvocation[]): boolean { + // The shell expands `$` and backticks anywhere on the line (arguments, redirections, an earlier call) into + // flags or environment this check can't see; a line with many calls is not worth reading call by call. + if (/[$`]/.test(command) || all.length > MAX_INVOCATIONS) return false + if (!PLAIN_LAUNCHER.test(invocation.binary)) return false + let before = command.slice(0, invocation.start) + for (const earlier of all.filter(one => one.end <= invocation.start).reverse()) { + before = before.slice(0, earlier.start) + ' ' + before.slice(earlier.end) + } + if (!/^[\s;&]*$/.test(before)) return false + const words = shellWords(command.slice(invocation.start, invocation.end)) + const launcher = shellWords(invocation.binary) + if (launcher.some((word, i) => words[i] !== word) || words[launcher.length] !== invocation.subcommand) return false + let selecting = false + let pending = false + for (const word of words.slice(launcher.length + 1)) { + if (pending) { + pending = false + continue + } + const flag = word.startsWith('-') ? word.split('=')[0]! : null + if (flag === null) { + if (selecting) continue + return false + } + selecting = SELECT_FLAGS.has(flag) + if (selecting || PLAIN_FLAGS.has(flag)) continue + if (VALUE_FLAGS.has(flag)) pending = !word.includes('=') + else return false + } + return true +} + +/** What a scanning invocation names to estimate: its inline SQL, or one plain model; else why not. */ +function targetOf(command: string, invocation: DbtInvocation, all: DbtInvocation[]): Target { + const text = command.slice(invocation.start, invocation.end) + if (!isPlainInvocation(command, invocation, all)) { + return { why: 'it sets its own target, defer state, vars, project or refresh mode, or runs after something that might' } + } + if (invocation.subcommand === 'show') { + const inline = inlineSql(text) + if (inline !== null) return inline.trim() ? { sql: inline, why: '' } : { why: 'empty inline query' } + } + const [only, ...rest] = invocation.select + if (only === undefined) return { why: 'no model named; selection resolves with dbt ls' } + if (rest.length > 0) return { why: 'several selectors' } + if (!PLAIN_MODEL.test(only)) return { why: `${only} is a graph, tag, path or package selector` } + return { model: only, why: '' } +} + +/** The SQL of `--inline` in a dbt command's text, unquoted as the shell would; null when there is none. */ +export function inlineSql(text: string): string | null { + const words = shellWords(text) + // dbt keeps the last of a repeated option. + let found: string | null = null + for (let i = 0; i < words.length; i++) { + const word = words[i]! + if (word === '--inline') found = words[++i] ?? null + else if (word.startsWith('--inline=')) found = word.slice('--inline='.length) + } + return found +} + +/** The words of a command's text with their quotes and backslashes resolved. */ +function shellWords(text: string): string[] { + const words: string[] = [] + let word = '' + let inWord = false + let quote: '"' | "'" | null = null + for (let i = 0; i < text.length; i++) { + const c = text[i]! + // A backslash before a line break joins the lines, in a word or in double quotes. + if (c === '\\' && quote !== "'" && (text[i + 1] === '\n' || (text[i + 1] === '\r' && text[i + 2] === '\n'))) { + i += text[i + 1] === '\n' ? 1 : 2 + continue + } + if (quote) { + if (c === quote) quote = null + else if (c === '\\' && quote === '"' && i + 1 < text.length && '"\\$`'.includes(text[i + 1]!)) word += text[++i] + else word += c + } else if (c === '"' || c === "'") { + quote = c + inWord = true + } else if (c === '\\' && i + 1 < text.length) { + word += text[++i] + inWord = true + } else if (/\s/.test(c)) { + if (inWord) words.push(word) + word = '' + inWord = false + } else { + word += c + inWord = true + } + } + if (inWord) words.push(word) + return words +} + +type Answer = { kind: 'bytes'; bytes: number; maxBytes: number; display: string } | { kind: 'unsupported'; adapter: string } | { kind: 'none'; reason: string } + +/** Asks altimate-code for the scan of a model or SQL; never throws: a failure is `none` with why. */ +async function estimate($: EngineInterface, args: { model?: string; sql?: string }, until: number): Promise { + try { + const run = await runTool($, 'dbt_cost_estimate', args, { write: true, until }) + if (!run.ok && UNKNOWN_TOOL.test(run.text)) return { kind: 'none', reason: `this altimate-code has no cost estimate: it needs ${NEEDED_VERSION}` } + const data = run.metadata as { supported?: unknown; adapter?: unknown; bytes?: unknown; maxBytes?: unknown; display?: unknown; error?: unknown } | null + if (data?.supported === false) return { kind: 'unsupported', adapter: typeof data.adapter === 'string' ? data.adapter : 'this warehouse' } + if (data?.supported === true && typeof data.bytes === 'number' && Number.isFinite(data.bytes)) { + // The tool's upper bound when it gives one: the display is rounded, and the limit is a hard one. + const maxBytes = typeof data.maxBytes === 'number' && Number.isFinite(data.maxBytes) && data.maxBytes >= data.bytes ? data.maxBytes : data.bytes + return { kind: 'bytes', bytes: data.bytes, maxBytes, display: typeof data.display === 'string' ? data.display : formatBytes(data.bytes) } + } + const why = typeof data?.error === 'string' ? data.error : run.text.trim().split('\n')[0] || 'altimate-code gave no estimate' + return { kind: 'none', reason: why } + } catch (error) { + return { kind: 'none', reason: error instanceof Error ? error.message : String(error) } + } +} + +async function rememberEstimate($: EngineInterface, target: string, bytes: number, display: string) { + await update($, costAtom, prev => ({ ...(prev ?? FRESH), lastEstimate: { target, bytes, display } })) + await setStatusPart($, 'cost', `scan ≈ ${display}`) +} + +/** What an estimate means against the limit, as a sentence tail for /scan-cost. */ +function verdict(maxBytes: number): string { + if (limitBytes <= 0) return '.' + return maxBytes > limitBytes ? `, over the ${formatBytes(limitBytes)} limit: a dbt run of it is held until /allow-scan.` : `, within the ${formatBytes(limitBytes)} limit.` +} + +/** Bytes in the units BigQuery's dry run writes them in: "4.2 TiB". */ +export function formatBytes(bytes: number): string { + const units = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'] + let n = bytes + let i = 0 + while (Math.abs(n) >= 1024 && i < units.length - 1) { + n /= 1024 + i++ + } + return `${n.toFixed(1)} ${units[i]}` +} diff --git a/plugins/altimate-code/hooks/features/cte-profile.tsx b/plugins/altimate-code/hooks/features/cte-profile.tsx new file mode 100644 index 0000000..75dd00b --- /dev/null +++ b/plugins/altimate-code/hooks/features/cte-profile.tsx @@ -0,0 +1,53 @@ +// Profile CTEs: /profile-ctes times each CTE. +// +// 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($, 'cteProfile', text)`. +import type { Args, EngineInterface, EventResult, On, RenderElement, ToolCallResult } from 'claude-code' + +import type { BashCall, ContextLines, EditedFile, FileCall } from '../tool-chain' +import type { BandUi, FeatureContext } from './index' + +/** Adds this feature's own hooks. */ +export function register(on: On, context: FeatureContext) {} + +/** After the session started: the place to register commands. */ +export async function sessionStart($: EngineInterface, e: Args<'session.start'>) {} + +/** 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'>) {} + +export async function sessionEnd($: EngineInterface, e: Args<'session.end'>) {} + +/** 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 +} + +/** 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. 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 }`. + */ +export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> { + return null +} + +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 {} + +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). */ +export const editReadsBefore = false diff --git a/plugins/altimate-code/hooks/features/defer.tsx b/plugins/altimate-code/hooks/features/defer.tsx new file mode 100644 index 0000000..c37aa85 --- /dev/null +++ b/plugins/altimate-code/hooks/features/defer.tsx @@ -0,0 +1,53 @@ +// Defer and target: --defer --state on dbt commands, and the target in the status line. +// +// 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($, 'defer', text)`. +import type { Args, EngineInterface, EventResult, On, RenderElement, ToolCallResult } from 'claude-code' + +import type { BashCall, ContextLines, EditedFile, FileCall } from '../tool-chain' +import type { BandUi, FeatureContext } from './index' + +/** Adds this feature's own hooks. */ +export function register(on: On, context: FeatureContext) {} + +/** After the session started: the place to register commands. */ +export async function sessionStart($: EngineInterface, e: Args<'session.start'>) {} + +/** 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'>) {} + +export async function sessionEnd($: EngineInterface, e: Args<'session.end'>) {} + +/** 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 +} + +/** 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. 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 }`. + */ +export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> { + return null +} + +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 {} + +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). */ +export const editReadsBefore = false diff --git a/plugins/altimate-code/hooks/features/docs.tsx b/plugins/altimate-code/hooks/features/docs.tsx new file mode 100644 index 0000000..3fb211e --- /dev/null +++ b/plugins/altimate-code/hooks/features/docs.tsx @@ -0,0 +1,53 @@ +// Docs coverage and description propagation. +// +// 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($, 'docs', text)`. +import type { Args, EngineInterface, EventResult, On, RenderElement, ToolCallResult } from 'claude-code' + +import type { BashCall, ContextLines, EditedFile, FileCall } from '../tool-chain' +import type { BandUi, FeatureContext } from './index' + +/** Adds this feature's own hooks. */ +export function register(on: On, context: FeatureContext) {} + +/** After the session started: the place to register commands. */ +export async function sessionStart($: EngineInterface, e: Args<'session.start'>) {} + +/** 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'>) {} + +export async function sessionEnd($: EngineInterface, e: Args<'session.end'>) {} + +/** 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 +} + +/** 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. 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 }`. + */ +export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> { + return null +} + +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 {} + +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). */ +export const editReadsBefore = false diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx new file mode 100644 index 0000000..dc141a9 --- /dev/null +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -0,0 +1,53 @@ +// 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' + +import type { BashCall, ContextLines, EditedFile, FileCall } from '../tool-chain' +import type { BandUi, FeatureContext } from './index' + +/** Adds this feature's own hooks. */ +export function register(on: On, context: FeatureContext) {} + +/** After the session started: the place to register commands. */ +export async function sessionStart($: EngineInterface, e: Args<'session.start'>) {} + +/** 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'>) {} + +export async function sessionEnd($: EngineInterface, e: Args<'session.end'>) {} + +/** 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 +} + +/** 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. 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 }`. + */ +export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> { + return null +} + +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 {} + +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). */ +export const editReadsBefore = false diff --git a/plugins/altimate-code/hooks/features/index.ts b/plugins/altimate-code/hooks/features/index.ts new file mode 100644 index 0000000..e314987 --- /dev/null +++ b/plugins/altimate-code/hooks/features/index.ts @@ -0,0 +1,321 @@ +// The dbt Power User features, each in its own file here, and the one place that calls them, in a +// fixed order: runs, defer, toModel, comments, cost, health, sqlGraph, docs, cteProfile. +// +// A plugin hooks each shared event once (a second hook on an event without a matcher is refused), and +// the engine refuses `$` handed to anything but a function it can name, so this file calls every +// feature's functions by name instead of looping over a list. A feature fills in its own file only. +// A function that throws is logged and skipped: one feature's failure never stops another or the call. +import type { Args, EngineInterface, EventResult, On, PluginOptions, RenderElement, ToolCallResult } from 'claude-code' + +import type { BashCall, EditedFile, FileCall } from '../tool-chain' +import { + register as runsRegister, + sessionStart as runsSessionStart, + turnStart as runsTurnStart, + turnComplete as runsTurnComplete, + sessionEnd as runsSessionEnd, + bandLine as runsBandLine, + bashRewrite as runsBashRewrite, + bashGate as runsBashGate, + bashAfter as runsBashAfter, + editAfter as runsEditAfter, + readOrEditAfter as runsReadOrEditAfter, + editReadsBefore as runsEditReadsBefore, +} from './runs' +import { + register as deferRegister, + sessionStart as deferSessionStart, + turnStart as deferTurnStart, + turnComplete as deferTurnComplete, + sessionEnd as deferSessionEnd, + bandLine as deferBandLine, + bashRewrite as deferBashRewrite, + bashGate as deferBashGate, + bashAfter as deferBashAfter, + editAfter as deferEditAfter, + readOrEditAfter as deferReadOrEditAfter, + editReadsBefore as deferEditReadsBefore, +} from './defer' +import { + register as toModelRegister, + sessionStart as toModelSessionStart, + turnStart as toModelTurnStart, + turnComplete as toModelTurnComplete, + sessionEnd as toModelSessionEnd, + bandLine as toModelBandLine, + bashRewrite as toModelBashRewrite, + bashGate as toModelBashGate, + bashAfter as toModelBashAfter, + editAfter as toModelEditAfter, + readOrEditAfter as toModelReadOrEditAfter, + editReadsBefore as toModelEditReadsBefore, +} from './to-model' +import { + register as commentsRegister, + sessionStart as commentsSessionStart, + turnStart as commentsTurnStart, + turnComplete as commentsTurnComplete, + sessionEnd as commentsSessionEnd, + bandLine as commentsBandLine, + bashRewrite as commentsBashRewrite, + bashGate as commentsBashGate, + bashAfter as commentsBashAfter, + editAfter as commentsEditAfter, + readOrEditAfter as commentsReadOrEditAfter, + editReadsBefore as commentsEditReadsBefore, +} from './comments' +import { + register as costRegister, + sessionStart as costSessionStart, + turnStart as costTurnStart, + turnComplete as costTurnComplete, + sessionEnd as costSessionEnd, + bandLine as costBandLine, + bashRewrite as costBashRewrite, + bashGate as costBashGate, + bashAfter as costBashAfter, + editAfter as costEditAfter, + readOrEditAfter as costReadOrEditAfter, + editReadsBefore as costEditReadsBefore, +} from './cost' +import { + register as healthRegister, + sessionStart as healthSessionStart, + turnStart as healthTurnStart, + turnComplete as healthTurnComplete, + sessionEnd as healthSessionEnd, + bandLine as healthBandLine, + bashRewrite as healthBashRewrite, + bashGate as healthBashGate, + bashAfter as healthBashAfter, + editAfter as healthEditAfter, + readOrEditAfter as healthReadOrEditAfter, + editReadsBefore as healthEditReadsBefore, +} from './health' +import { + register as sqlGraphRegister, + sessionStart as sqlGraphSessionStart, + turnStart as sqlGraphTurnStart, + turnComplete as sqlGraphTurnComplete, + sessionEnd as sqlGraphSessionEnd, + bandLine as sqlGraphBandLine, + bashRewrite as sqlGraphBashRewrite, + bashGate as sqlGraphBashGate, + bashAfter as sqlGraphBashAfter, + editAfter as sqlGraphEditAfter, + readOrEditAfter as sqlGraphReadOrEditAfter, + editReadsBefore as sqlGraphEditReadsBefore, +} from './sql-graph' +import { + register as docsRegister, + sessionStart as docsSessionStart, + turnStart as docsTurnStart, + turnComplete as docsTurnComplete, + sessionEnd as docsSessionEnd, + bandLine as docsBandLine, + bashRewrite as docsBashRewrite, + bashGate as docsBashGate, + bashAfter as docsBashAfter, + editAfter as docsEditAfter, + readOrEditAfter as docsReadOrEditAfter, + editReadsBefore as docsEditReadsBefore, +} from './docs' +import { + register as cteProfileRegister, + sessionStart as cteProfileSessionStart, + turnStart as cteProfileTurnStart, + turnComplete as cteProfileTurnComplete, + sessionEnd as cteProfileSessionEnd, + bandLine as cteProfileBandLine, + bashRewrite as cteProfileBashRewrite, + bashGate as cteProfileBashGate, + bashAfter as cteProfileBashAfter, + editAfter as cteProfileEditAfter, + readOrEditAfter as cteProfileReadOrEditAfter, + editReadsBefore as cteProfileEditReadsBefore, +} from './cte-profile' + +export type { FeatureId } from '../status-line' +export { FEATURE_IDS } from '../status-line' + +/** The elements a band line draws with: what `$.ui.resolve(e)` gave the band's hook. */ +export type BandUi = { Box: any; Button: any; Text: any; [element: string]: any } + +/** What a feature's `register` gets. */ +export type FeatureContext = { + /** The plugin's `userConfig` values (`.claude-plugin/plugin.json`), defaults filled in. */ + options: PluginOptions +} + +function failed($: EngineInterface, id: string, part: string, error: unknown) { + try { + $.ui.log(`${id} · ${part} failed: ${String(error)}`, { to: 'debug' }) + } catch {} +} + +/** Registers every feature's own hooks. */ +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) +} + +export async function featuresSessionStart($: EngineInterface, e: Args<'session.start'>) { + 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)) + await commentsSessionStart($, e).catch(error => failed($, 'comments', 'sessionStart', error)) + await costSessionStart($, e).catch(error => failed($, 'cost', 'sessionStart', error)) + await healthSessionStart($, e).catch(error => failed($, 'health', 'sessionStart', error)) + await sqlGraphSessionStart($, e).catch(error => failed($, 'sqlGraph', 'sessionStart', error)) + await docsSessionStart($, e).catch(error => failed($, 'docs', 'sessionStart', error)) + await cteProfileSessionStart($, e).catch(error => failed($, 'cteProfile', 'sessionStart', error)) +} + +export async function featuresTurnStart($: EngineInterface, e: Args<'turn.start'>) { + await runsTurnStart($, e).catch(error => failed($, 'runs', 'turnStart', error)) + await deferTurnStart($, e).catch(error => failed($, 'defer', 'turnStart', error)) + await toModelTurnStart($, e).catch(error => failed($, 'toModel', 'turnStart', error)) + await commentsTurnStart($, e).catch(error => failed($, 'comments', 'turnStart', error)) + await costTurnStart($, e).catch(error => failed($, 'cost', 'turnStart', error)) + await healthTurnStart($, e).catch(error => failed($, 'health', 'turnStart', error)) + await sqlGraphTurnStart($, e).catch(error => failed($, 'sqlGraph', 'turnStart', error)) + await docsTurnStart($, e).catch(error => failed($, 'docs', 'turnStart', error)) + await cteProfileTurnStart($, e).catch(error => failed($, 'cteProfile', 'turnStart', error)) +} + +export async function featuresTurnComplete($: EngineInterface, e: Args<'turn.complete'>, out: EventResult<'turn.complete'>) { + await runsTurnComplete($, e, out).catch(error => failed($, 'runs', 'turnComplete', error)) + await deferTurnComplete($, e, out).catch(error => failed($, 'defer', 'turnComplete', error)) + await toModelTurnComplete($, e, out).catch(error => failed($, 'toModel', 'turnComplete', error)) + await commentsTurnComplete($, e, out).catch(error => failed($, 'comments', 'turnComplete', error)) + await costTurnComplete($, e, out).catch(error => failed($, 'cost', 'turnComplete', error)) + await healthTurnComplete($, e, out).catch(error => failed($, 'health', 'turnComplete', error)) + await sqlGraphTurnComplete($, e, out).catch(error => failed($, 'sqlGraph', 'turnComplete', error)) + await docsTurnComplete($, e, out).catch(error => failed($, 'docs', 'turnComplete', error)) + await cteProfileTurnComplete($, e, out).catch(error => failed($, 'cteProfile', 'turnComplete', error)) +} + +export async function featuresSessionEnd($: EngineInterface, e: Args<'session.end'>) { + await runsSessionEnd($, e).catch(error => failed($, 'runs', 'sessionEnd', error)) + await deferSessionEnd($, e).catch(error => failed($, 'defer', 'sessionEnd', error)) + await toModelSessionEnd($, e).catch(error => failed($, 'toModel', 'sessionEnd', error)) + await commentsSessionEnd($, e).catch(error => failed($, 'comments', 'sessionEnd', error)) + await costSessionEnd($, e).catch(error => failed($, 'cost', 'sessionEnd', error)) + await healthSessionEnd($, e).catch(error => failed($, 'health', 'sessionEnd', error)) + await sqlGraphSessionEnd($, e).catch(error => failed($, 'sqlGraph', 'sessionEnd', error)) + await docsSessionEnd($, e).catch(error => failed($, 'docs', 'sessionEnd', error)) + await cteProfileSessionEnd($, e).catch(error => failed($, 'cteProfile', 'sessionEnd', error)) +} + +/** Each feature's band line, in order, with its id; a feature with nothing to say is left out. */ +export async function featureBandLines($: EngineInterface, ui: BandUi): Promise<{ id: string; line: RenderElement }[]> { + const lines: { id: string; line: RenderElement }[] = [] + const add = (id: string, line: RenderElement | null | void) => { + if (line) lines.push({ id, line }) + } + add('runs', await runsBandLine($, ui).catch(error => failed($, 'runs', 'bandLine', error))) + add('defer', await deferBandLine($, ui).catch(error => failed($, 'defer', 'bandLine', error))) + add('toModel', await toModelBandLine($, ui).catch(error => failed($, 'toModel', 'bandLine', error))) + add('comments', await commentsBandLine($, ui).catch(error => failed($, 'comments', 'bandLine', error))) + add('cost', await costBandLine($, ui).catch(error => failed($, 'cost', 'bandLine', error))) + add('health', await healthBandLine($, ui).catch(error => failed($, 'health', 'bandLine', error))) + add('sqlGraph', await sqlGraphBandLine($, ui).catch(error => failed($, 'sqlGraph', 'bandLine', error))) + add('docs', await docsBandLine($, ui).catch(error => failed($, 'docs', 'bandLine', error))) + add('cteProfile', await cteProfileBandLine($, ui).catch(error => failed($, 'cteProfile', 'bandLine', error))) + return lines +} + +/** The command after every feature's rewrite, each seeing the one before's. */ +export async function featuresBashRewrite($: EngineInterface, e: BashCall): Promise { + let command = e.command + const take = (next: string | null | void) => { + if (typeof next === 'string' && next.trim() !== '') command = next + } + take(await runsBashRewrite($, { ...e, command }).catch(error => failed($, 'runs', 'bashRewrite', error))) + take(await deferBashRewrite($, { ...e, command }).catch(error => failed($, 'defer', 'bashRewrite', error))) + take(await toModelBashRewrite($, { ...e, command }).catch(error => failed($, 'toModel', 'bashRewrite', error))) + take(await commentsBashRewrite($, { ...e, command }).catch(error => failed($, 'comments', 'bashRewrite', error))) + take(await costBashRewrite($, { ...e, command }).catch(error => failed($, 'cost', 'bashRewrite', error))) + take(await healthBashRewrite($, { ...e, command }).catch(error => failed($, 'health', 'bashRewrite', error))) + take(await sqlGraphBashRewrite($, { ...e, command }).catch(error => failed($, 'sqlGraph', 'bashRewrite', error))) + take(await docsBashRewrite($, { ...e, command }).catch(error => failed($, 'docs', 'bashRewrite', error))) + take(await cteProfileBashRewrite($, { ...e, command }).catch(error => failed($, 'cteProfile', 'bashRewrite', error))) + return command +} + +/** The first feature's refusal of the Bash call, or null. */ +export async function featuresBashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> { + let gate: { deny: string } | null = null + const take = (next: { deny: string } | null | void) => { + if (!gate && next && typeof next.deny === 'string') gate = { deny: next.deny } + } + if (!gate) take(await runsBashGate($, e).catch(error => failed($, 'runs', 'bashGate', error))) + if (!gate) take(await deferBashGate($, e).catch(error => failed($, 'defer', 'bashGate', error))) + if (!gate) take(await toModelBashGate($, e).catch(error => failed($, 'toModel', 'bashGate', error))) + if (!gate) take(await commentsBashGate($, e).catch(error => failed($, 'comments', 'bashGate', error))) + if (!gate) take(await costBashGate($, e).catch(error => failed($, 'cost', 'bashGate', error))) + if (!gate) take(await healthBashGate($, e).catch(error => failed($, 'health', 'bashGate', error))) + if (!gate) take(await sqlGraphBashGate($, e).catch(error => failed($, 'sqlGraph', 'bashGate', error))) + if (!gate) take(await docsBashGate($, e).catch(error => failed($, 'docs', 'bashGate', error))) + if (!gate) take(await cteProfileBashGate($, e).catch(error => failed($, 'cteProfile', 'bashGate', error))) + return gate +} + +/** The context lines every feature adds after a Bash call ran. */ +export async function featuresBashAfter($: EngineInterface, e: BashCall, result: ToolCallResult): Promise { + const lines: string[] = [] + addLines(lines, await runsBashAfter($, e, result).catch(error => failed($, 'runs', 'bashAfter', error))) + addLines(lines, await deferBashAfter($, e, result).catch(error => failed($, 'defer', 'bashAfter', error))) + addLines(lines, await toModelBashAfter($, e, result).catch(error => failed($, 'toModel', 'bashAfter', error))) + addLines(lines, await commentsBashAfter($, e, result).catch(error => failed($, 'comments', 'bashAfter', error))) + addLines(lines, await costBashAfter($, e, result).catch(error => failed($, 'cost', 'bashAfter', error))) + addLines(lines, await healthBashAfter($, e, result).catch(error => failed($, 'health', 'bashAfter', error))) + addLines(lines, await sqlGraphBashAfter($, e, result).catch(error => failed($, 'sqlGraph', 'bashAfter', error))) + addLines(lines, await docsBashAfter($, e, result).catch(error => failed($, 'docs', 'bashAfter', error))) + addLines(lines, await cteProfileBashAfter($, e, result).catch(error => failed($, 'cteProfile', 'bashAfter', error))) + return lines +} + +/** Whether any feature's `editAfter` needs the file's text from before the edit. */ +export const EDIT_READS_BEFORE: boolean = runsEditReadsBefore || deferEditReadsBefore || toModelEditReadsBefore || commentsEditReadsBefore || costEditReadsBefore || healthEditReadsBefore || sqlGraphEditReadsBefore || docsEditReadsBefore || cteProfileEditReadsBefore + +/** The context lines every feature adds after an edit landed. */ +export async function featuresEditAfter($: EngineInterface, e: FileCall, result: ToolCallResult, file: EditedFile): Promise { + const lines: string[] = [] + addLines(lines, await runsEditAfter($, e, result, runsEditReadsBefore ? file : { path: file.path, before: null }).catch(error => failed($, 'runs', 'editAfter', error))) + addLines(lines, await deferEditAfter($, e, result, deferEditReadsBefore ? file : { path: file.path, before: null }).catch(error => failed($, 'defer', 'editAfter', error))) + addLines(lines, await toModelEditAfter($, e, result, toModelEditReadsBefore ? file : { path: file.path, before: null }).catch(error => failed($, 'toModel', 'editAfter', error))) + addLines(lines, await commentsEditAfter($, e, result, commentsEditReadsBefore ? file : { path: file.path, before: null }).catch(error => failed($, 'comments', 'editAfter', error))) + addLines(lines, await costEditAfter($, e, result, costEditReadsBefore ? file : { path: file.path, before: null }).catch(error => failed($, 'cost', 'editAfter', error))) + addLines(lines, await healthEditAfter($, e, result, healthEditReadsBefore ? file : { path: file.path, before: null }).catch(error => failed($, 'health', 'editAfter', error))) + addLines(lines, await sqlGraphEditAfter($, e, result, sqlGraphEditReadsBefore ? file : { path: file.path, before: null }).catch(error => failed($, 'sqlGraph', 'editAfter', error))) + addLines(lines, await docsEditAfter($, e, result, docsEditReadsBefore ? file : { path: file.path, before: null }).catch(error => failed($, 'docs', 'editAfter', error))) + addLines(lines, await cteProfileEditAfter($, e, result, cteProfileEditReadsBefore ? file : { path: file.path, before: null }).catch(error => failed($, 'cteProfile', 'editAfter', error))) + return lines +} + +/** The context lines every feature adds after a read or an edit succeeded. */ +export async function featuresReadOrEditAfter($: EngineInterface, e: FileCall, result: ToolCallResult, file: { path: string }): Promise { + const lines: string[] = [] + addLines(lines, await runsReadOrEditAfter($, e, result, file).catch(error => failed($, 'runs', 'readOrEditAfter', error))) + addLines(lines, await deferReadOrEditAfter($, e, result, file).catch(error => failed($, 'defer', 'readOrEditAfter', error))) + addLines(lines, await toModelReadOrEditAfter($, e, result, file).catch(error => failed($, 'toModel', 'readOrEditAfter', error))) + addLines(lines, await commentsReadOrEditAfter($, e, result, file).catch(error => failed($, 'comments', 'readOrEditAfter', error))) + addLines(lines, await costReadOrEditAfter($, e, result, file).catch(error => failed($, 'cost', 'readOrEditAfter', error))) + addLines(lines, await healthReadOrEditAfter($, e, result, file).catch(error => failed($, 'health', 'readOrEditAfter', error))) + addLines(lines, await sqlGraphReadOrEditAfter($, e, result, file).catch(error => failed($, 'sqlGraph', 'readOrEditAfter', error))) + addLines(lines, await docsReadOrEditAfter($, e, result, file).catch(error => failed($, 'docs', 'readOrEditAfter', error))) + addLines(lines, await cteProfileReadOrEditAfter($, e, result, file).catch(error => failed($, 'cteProfile', 'readOrEditAfter', error))) + return lines +} + +function addLines(lines: string[], added: readonly unknown[] | null | void) { + if (Array.isArray(added)) for (const line of added) if (typeof line === 'string' && line !== '') lines.push(line) +} diff --git a/plugins/altimate-code/hooks/features/runs.tsx b/plugins/altimate-code/hooks/features/runs.tsx new file mode 100644 index 0000000..40ad8ff --- /dev/null +++ b/plugins/altimate-code/hooks/features/runs.tsx @@ -0,0 +1,53 @@ +// Runs: every dbt command Claude runs, with its results, in /runs. +// +// 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($, 'runs', text)`. +import type { Args, EngineInterface, EventResult, On, RenderElement, ToolCallResult } from 'claude-code' + +import type { BashCall, ContextLines, EditedFile, FileCall } from '../tool-chain' +import type { BandUi, FeatureContext } from './index' + +/** Adds this feature's own hooks. */ +export function register(on: On, context: FeatureContext) {} + +/** After the session started: the place to register commands. */ +export async function sessionStart($: EngineInterface, e: Args<'session.start'>) {} + +/** 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'>) {} + +export async function sessionEnd($: EngineInterface, e: Args<'session.end'>) {} + +/** 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 +} + +/** 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. 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 }`. + */ +export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> { + return null +} + +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 {} + +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). */ +export const editReadsBefore = false diff --git a/plugins/altimate-code/hooks/features/sql-graph.tsx b/plugins/altimate-code/hooks/features/sql-graph.tsx new file mode 100644 index 0000000..756f1bd --- /dev/null +++ b/plugins/altimate-code/hooks/features/sql-graph.tsx @@ -0,0 +1,53 @@ +// SQL graph: /sql-graph lists a model's CTEs, joins and filters. +// +// 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($, 'sqlGraph', text)`. +import type { Args, EngineInterface, EventResult, On, RenderElement, ToolCallResult } from 'claude-code' + +import type { BashCall, ContextLines, EditedFile, FileCall } from '../tool-chain' +import type { BandUi, FeatureContext } from './index' + +/** Adds this feature's own hooks. */ +export function register(on: On, context: FeatureContext) {} + +/** After the session started: the place to register commands. */ +export async function sessionStart($: EngineInterface, e: Args<'session.start'>) {} + +/** 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'>) {} + +export async function sessionEnd($: EngineInterface, e: Args<'session.end'>) {} + +/** 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 +} + +/** 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. 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 }`. + */ +export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> { + return null +} + +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 {} + +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). */ +export const editReadsBefore = false diff --git a/plugins/altimate-code/hooks/features/to-model.tsx b/plugins/altimate-code/hooks/features/to-model.tsx new file mode 100644 index 0000000..4308a3e --- /dev/null +++ b/plugins/altimate-code/hooks/features/to-model.tsx @@ -0,0 +1,53 @@ +// Raw SQL to a dbt model: /to-model. +// +// 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($, 'toModel', text)`. +import type { Args, EngineInterface, EventResult, On, RenderElement, ToolCallResult } from 'claude-code' + +import type { BashCall, ContextLines, EditedFile, FileCall } from '../tool-chain' +import type { BandUi, FeatureContext } from './index' + +/** Adds this feature's own hooks. */ +export function register(on: On, context: FeatureContext) {} + +/** After the session started: the place to register commands. */ +export async function sessionStart($: EngineInterface, e: Args<'session.start'>) {} + +/** 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'>) {} + +export async function sessionEnd($: EngineInterface, e: Args<'session.end'>) {} + +/** 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 +} + +/** 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. 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 }`. + */ +export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> { + return null +} + +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 {} + +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). */ +export const editReadsBefore = false diff --git a/plugins/altimate-code/hooks/integrations.tsx b/plugins/altimate-code/hooks/integrations.tsx index 256131b..8f14ff1 100644 --- a/plugins/altimate-code/hooks/integrations.tsx +++ b/plugins/altimate-code/hooks/integrations.tsx @@ -3,7 +3,10 @@ import type { EngineInterface, Register } from 'claude-code' import type { Busy, Expanded, Form, IntegrationsStatus, Provider, ProviderId, WorkspaceChoice } from '../types' import { MISSING_BINARY, later, parseJson, runAltimate } from './altimate-cli' +import { featureBandLines, featuresSessionStart, registerFeatures } from './features' import { lineageBand, registerLineage, startLineage } from './lineage' +import { setStatusPart } from './status-line' +import { installToolChain } from './tool-chain' const PANE = 'altimate-integrations' const PANE_TITLE = 'Altimate integrations' @@ -102,7 +105,7 @@ async function refresh($: EngineInterface, subcommand: 'status' | 'detect' = 'st const next = parseStatus(result.stdout) if (next) { await update($, status, () => next) - showStatusLine($, next) + await showStatusLine($, next) } else { const failure = parseJson<{ message?: string }>(result.stdout)?.message ?? firstLine(result.stderr.replace(ANSI, '')) await update($, message, () => failure ?? 'altimate returned no integrations status.') @@ -115,7 +118,8 @@ async function refresh($: EngineInterface, subcommand: 'status' | 'detect' = 'st } } -function showStatusLine($: EngineInterface, current: IntegrationsStatus) { +/** The integrations part of the plugin's status line (status-line.ts adds the features' parts). */ +async function showStatusLine($: EngineInterface, current: IntegrationsStatus) { const linked = current.providers.filter(one => one.state === 'connected') const synced = linked.filter(one => one.sync === 'synced').length const waiting = current.providers.filter(one => one.state === 'detected' || one.found || one.state === 'available').length @@ -124,7 +128,7 @@ function showStatusLine($: EngineInterface, current: IntegrationsStatus) { if (attention > 0) parts.push(`${attention} needs attention`) if (synced > 0) parts.push(`${synced} synced`) if (waiting > 0) parts.push(`${waiting} found`) - $.ui.status(parts.join(' · ')) + await setStatusPart($, 'integrations', parts.join(' · ')) } /** The timeout that fits what the command runs: one provider's sync or disconnect. */ @@ -551,7 +555,7 @@ function nameOf(id: ProviderId): string { return { github: 'GitHub', jira: 'Jira', linear: 'Linear' }[id] } -export const register: Register = on => { +export const register: Register = (on, options) => { on('session.start', async ($, e, next) => { const started = await next(e) await $.command.register({ @@ -560,6 +564,8 @@ export const register: Register = on => { }) // The lineage half failing to start must not stop this pane's own setup below. await startLineage($).catch(error => $.ui.log(`lineage-pane · could not start: ${String(error)}`)) + // Each feature's own start (its commands); one failing is logged and the rest go on. + await featuresSessionStart($, e) // A press that was running when an earlier session ended can never finish, so it must not block this one. await update($, busy, () => null) // A `-p` run or the SDK has nobody to show the band to, so it does not scan the machine. @@ -580,10 +586,24 @@ export const register: Register = on => { }) on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => { - // One band per plugin: the lineage impact line rides under the integrations one, or alone. + // One band per plugin: the lineage impact line rides under the integrations one, or alone; each + // feature's line rides under the lineage one. if (e.props.hasSurvey) return next(e) - const lineage = await lineageBand($, $.ui.resolve(e)) - const pass = async () => lineage ?? next(e) + const ui = $.ui.resolve(e) + const lineage = await lineageBand($, ui) + const extra = await featureBandLines($, ui) + const tail = + extra.length === 0 ? ( + lineage + ) : ( + + {lineage} + {extra.map(one => ( + {one.line} + ))} + + ) + const pass = async () => tail ?? next(e) const current = await read($, status) if (!current || (await read($, isBandHidden))) return pass() @@ -639,7 +659,7 @@ export const register: Register = on => {