From a5a57f1734e7d39b0930b934116d75225fe251cb Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 13 Nov 2026 17:02:03 +0530 Subject: [PATCH 01/47] feat(runs): record each dbt run's results and show them in /runs Co-Authored-By: Claude Sonnet 5.5 --- .../hooks/features/runs.test.tsx | 165 ++++++++++++ plugins/altimate-code/hooks/features/runs.tsx | 241 +++++++++++++++++- plugins/altimate-code/types/index.d.ts | 41 ++- 3 files changed, 437 insertions(+), 10 deletions(-) create mode 100644 plugins/altimate-code/hooks/features/runs.test.tsx diff --git a/plugins/altimate-code/hooks/features/runs.test.tsx b/plugins/altimate-code/hooks/features/runs.test.tsx new file mode 100644 index 00000000..2d1712f6 --- /dev/null +++ b/plugins/altimate-code/hooks/features/runs.test.tsx @@ -0,0 +1,165 @@ +import { expect, mock, test } from 'claude-code/testing' + +import type { RunRecord } from '../../types' +import { fitStore, MAX_MESSAGE, MAX_ROWS, MAX_RUNS, resultsInvocation, summaryLine, toRecord, withRun } from './runs' + +const ROOT = '/repo' + +const PANE = { + plugin: 'altimate-code', + component: 'Pane', + requestId: 'altimate-runs', + props: { title: 'dbt runs', isFocused: true, bodyColumns: 90, placement: 'dock', scroll: { offset: 0, bodyRows: 40 }, view: {} }, +} as const + +const view = (invocationId: string | null, rows: [string, string, string | null][] = [['model.shop.stg_orders', 'success', null]], command = 'build') => ({ + found: true, + invocationId, + command, + elapsedSec: 4.2, + completedAt: '2026-11-13T10:00:00.000Z', + results: rows.map(([uniqueId, status, message]) => ({ uniqueId, name: uniqueId.split('.').pop()!, status, executionTimeSec: 1.5, message })), +}) + +/** The session beneath the plugin: a store in memory, and the CLI answering `dbt_run_results` with `answer()`. */ +async function start($: any, on: any, answer: () => unknown) { + const clock = mock.clock(on) + const stored = new Map() + const filled: string[] = [] + const cli: string[] = [] + on('command.register', () => ({ value: { command: 'runs' } })) + on('session.start', (_: unknown, e: { cwd: string }) => ({ cwd: e.cwd })) + on('session.root', () => ({ value: ROOT })) + on('store.get', (_: unknown, e: { key: string }) => ({ value: stored.get(e.key) })) + on('store.set', (_: unknown, e: { key: string; value: unknown }) => { + stored.set(e.key, JSON.parse(JSON.stringify(e.value))) + return { value: undefined } + }) + on('mcp.connect', () => ({ value: { isConnected: false, reason: 'not_connected', message: 'off' } })) + on('process.run', (_: unknown, e: { argv: readonly string[] }) => { + if (e.argv[1] !== 'tool') return { value: { exitCode: 1, stdout: '', stderr: 'not this test' } } + cli.push(e.argv[3]!) + return { value: { exitCode: 0, stdout: JSON.stringify({ status: 'ok', output: '', metadata: answer() }), stderr: '' } } + }) + on('prompt.fill', (_: unknown, e: { text: string }) => { + filled.push(e.text) + return { isFilled: true } + }) + on('ui.open', () => ({ value: {} })) + on('tool.call', () => ({ result: { ok: true } })) + await $.session.start({ cwd: ROOT, surface: 'terminal', isInteractive: true }) + await clock.settle() + return { clock, stored, filled, cli } +} + +test('a dbt build is recorded once; the same unchanged file is ignored; the line says what failed', async ($, on) => { + const next = view('inv-1', [ + ['model.shop.stg_orders', 'success', null], + ['model.shop.fct_sales', 'error', 'Database Error: column "x" does not exist'], + ['test.shop.not_null_orders_id.f3a1c', 'pass', null], + ]) + const { stored, cli } = await start($, on, () => next) + + const first: any = await $.tool.call({ tool: 'Bash', command: 'cd /repo && dbt build -s orders' }) + expect(first.context).toEqual(['build: 2 ok, 1 error: fct_sales']) + const runs = (stored.get('runs') as Record)[ROOT]! + expect(runs.map(run => [run.id, run.commandLine, run.total, run.passed, run.failed])).toEqual([['inv-1', 'dbt build -s orders', 3, 2, 1]]) + expect(runs[0]!.rows[0]!.name).toBe('fct_sales') + + // Run again: dbt wrote nothing new (its invocation id is the one recorded). + const again: any = await $.tool.call({ tool: 'Bash', command: 'dbt build -s orders' }) + expect(again.context ?? []).toEqual([]) + expect((stored.get('runs') as Record)[ROOT]).toHaveLength(1) + expect(cli).toEqual(['dbt_run_results', 'dbt_run_results']) +}) + +test('a Bash call that is no dbt command that writes results reads nothing', async ($, on) => { + const { stored, cli } = await start($, on, () => view('inv-1')) + await $.tool.call({ tool: 'Bash', command: 'ls models' }) + await $.tool.call({ tool: 'Bash', command: 'dbt deps' }) + expect(cli).toEqual([]) + expect(stored.has('runs')).toBe(false) +}) + +test('no run_results.json: nothing is recorded', async ($, on) => { + const { stored } = await start($, on, () => ({ found: false, invocationId: null, command: null, elapsedSec: null, completedAt: null, results: [] })) + const result: any = await $.tool.call({ tool: 'Bash', command: 'dbt run' }) + expect(result.context ?? []).toEqual([]) + expect(stored.has('runs')).toBe(false) +}) + +for (const surface of ['terminal', 'desktop'] as const) { + test(`${surface}: /runs lists the run, opens it with failures first, and Re-run fills the prompt`, async ($, on) => { + const { filled } = await start($, on, () => + view('inv-9', [ + ['model.shop.stg_orders', 'success', null], + ['model.shop.fct_sales', 'error', 'boom'], + ]), + ) + await $.tool.call({ tool: 'Bash', command: 'uv run dbt build -s fct_sales' }) + + const ui = await $.ui.mount({ ...PANE, surface }) + expect(await ui.find({ type: 'Button', text: /build .*1\/2 passed/ })).toBeDefined() + await ui.press({ key: 'run-inv-9' }) + + const rows = await ui.findAll({ type: 'Text', text: /^(error|success) / }) + expect(rows.map(row => row.text)).toEqual(['error fct_sales 1.5s', 'success stg_orders 1.5s']) + + await ui.press({ key: 'runs-rerun' }) + await ui.press({ key: 'runs-retry' }) + expect(filled).toEqual(['Run `uv run dbt build -s fct_sales`', 'Run `uv run dbt retry`']) + await ui.unmount() + }) +} + +test('a run with no failure has no Re-run failed button', async ($, on) => { + await start($, on, () => view('inv-2')) + await $.tool.call({ tool: 'Bash', command: 'dbt run' }) + const ui = await $.ui.mount({ ...PANE, surface: 'terminal' }) + await ui.press({ key: 'run-inv-2' }) + expect(await ui.find({ key: 'runs-rerun' })).toBeDefined() + expect(await ui.find({ key: 'runs-retry' })).toBeUndefined() + await ui.unmount() +}) + +test('which dbt invocation wrote the results, and what to retry', () => { + expect(resultsInvocation('cd a && dbt run -s x | tee log')).toEqual({ commandLine: 'dbt run -s x', retryLine: 'dbt retry' }) + expect(resultsInvocation('DBT_PROFILES_DIR=. dbt build')).toEqual({ commandLine: 'DBT_PROFILES_DIR=. dbt build', retryLine: 'dbt retry' }) + expect(resultsInvocation('dbt deps && dbt test')?.commandLine).toBe('dbt test') + expect(resultsInvocation('dbt deps')).toBeNull() + expect(resultsInvocation('echo "unclosed')).toBeNull() +}) + +test('a record keeps failures first, cuts long messages and rows; at most 50 runs, the same id never twice', () => { + const long = 'x'.repeat(MAX_MESSAGE + 500) + const many = Array.from({ length: MAX_ROWS + 20 }, (_, i): [string, string, string | null] => [`model.shop.m${i}`, i === MAX_ROWS + 10 ? 'error' : 'success', i === MAX_ROWS + 10 ? long : null]) + const run = toRecord(view('a', many), 'dbt run', 'dbt retry')! + expect(run.rows).toHaveLength(MAX_ROWS) + expect(run.total).toBe(MAX_ROWS + 20) + expect(run.rows[0]!.status).toBe('error') + expect(run.rows[0]!.message!.length).toBe(MAX_MESSAGE + 1) + expect(toRecord({ ...view(null), completedAt: null }, 'dbt run', 'dbt retry')).toBeNull() + expect(toRecord(view(null), 'dbt run', 'dbt retry')!.id).toBe('2026-11-13T10:00:00.000Z') + + let runs: RunRecord[] = [] + for (let i = 0; i < MAX_RUNS + 5; i++) runs = withRun(runs, { ...run, id: `r${i}` }) + expect(runs).toHaveLength(MAX_RUNS) + expect(runs[0]!.id).toBe(`r${MAX_RUNS + 4}`) + expect(withRun(runs, { ...run, id: 'r10' }).map(one => one.id).filter(id => id === 'r10')).toHaveLength(1) +}) + +test('the store is cut to its budget, the oldest run of the busiest project first', () => { + const fat = (id: string): RunRecord => ({ ...toRecord(view(id), 'dbt run', 'dbt retry')!, rows: Array.from({ length: 100 }, (_, i) => ({ uniqueId: `u${i}`, name: 'n'.repeat(300), status: 'success', executionTimeSec: 1, message: null })) }) + const held = { '/a': Array.from({ length: 50 }, (_, i) => fat(`a${i}`)), '/b': [fat('b0')] } + const fitted = fitStore(held) + expect(JSON.stringify(fitted).length).toBeLessThanOrEqual(2_500_000) + expect(fitted['/b']).toHaveLength(1) + expect(fitted['/a']![0]!.id).toBe('a0') +}) + +test('the line for Claude names five failures and counts the rest', () => { + const rows = Array.from({ length: 7 }, (_, i): [string, string, string | null] => [`model.shop.m${i}`, 'error', null]) + expect(summaryLine(toRecord(view('a', [...rows, ['model.shop.ok', 'success', null], ['model.shop.s', 'skipped', null]]), 'dbt build', 'dbt retry')!)).toBe( + 'build: 1 ok, 7 errors: m0, m1, m2, m3, m4 and 2 more, 1 skipped', + ) +}) diff --git a/plugins/altimate-code/hooks/features/runs.tsx b/plugins/altimate-code/hooks/features/runs.tsx index f589b5fd..78c717cc 100644 --- a/plugins/altimate-code/hooks/features/runs.tsx +++ b/plugins/altimate-code/hooks/features/runs.tsx @@ -1,20 +1,239 @@ // 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)`. +// After a Bash call that ran dbt, the last run's `dbt_run_results` is read and recorded once per +// invocation (up to 50 runs per project, in `$.store`); `/runs` lists them in a pane, where a run can be +// opened, re-run, or its failures re-run through the prompt. +import { atom, read, update } from 'claude-code' import type { Args, EngineInterface, EventResult, On, RenderElement, ToolCallResult } from 'claude-code' +import type { RunRecord, RunRow, RunsState } from '../../types' +import { runTool } from '../altimate-cli' +import { parseDbtCommand } from '../dbt-command' import type { BashCall, ContextLines, EditedFile, FileCall } from '../tool-chain' import type { BandUi, FeatureContext } from './index' +const PANE = 'altimate-runs' +const PANE_TITLE = 'dbt runs' +const COMMAND = 'runs' +const PANE_SIZE = { columns: 90, rows: 30 } +const STORE_KEY = 'runs' + +/** Runs kept per project. */ +export const MAX_RUNS = 50 +/** Rows kept per run: failures first, then the rest. */ +export const MAX_ROWS = 100 +/** A stored message is cut to this many characters. */ +export const MAX_MESSAGE = 300 +/** The whole store holds at most 4 MiB; the runs stay well under it. */ +export const MAX_STORE_BYTES = 2_500_000 +/** Rows the pane draws for one run. */ +const SHOWN_ROWS = 40 +/** Models named in the line Claude gets. */ +const NAMED = 5 +/** The most the tool call may be held for. */ +const READ_MS = 5000 + +/** The dbt commands that write run_results.json. */ +const WRITES_RESULTS = new Set(['build', 'run', 'test', 'seed', 'snapshot', 'compile', 'retry', 'clone']) +const PASSED = new Set(['success', 'pass', 'warn']) +const SKIPPED = new Set(['skipped', 'no-op']) + +const runsAtom = atom({ plugin: 'altimate-code', key: 'runs' } as const, null as RunsState | null) + +/** What `dbt_run_results` returns, as the plugin reads it. */ +export type RunView = { + found?: boolean + invocationId: string | null + command: string | null + elapsedSec: number | null + completedAt: string | null + results: RunRow[] +} + +/** The store's runs: project folder → newest first. */ +type Held = Record + +const isFailure = (status: string) => !PASSED.has(status) && !SKIPPED.has(status) + +/** The dbt invocation of `command` that wrote the results: the last one with a command that writes them. */ +export function resultsInvocation(command: string) { + const found = parseDbtCommand(command).filter(one => one.subcommand !== null && WRITES_RESULTS.has(one.subcommand)) + const last = found[found.length - 1] + return last ? { commandLine: command.slice(last.start, last.end).trim(), retryLine: `${last.binary} retry` } : null +} + +/** One run as it is kept: counts of every node, the rows failures first, each message cut. */ +export function toRecord(view: RunView, commandLine: string, retryLine: string): RunRecord | null { + const completedAt = view.completedAt ?? '' + const id = view.invocationId ?? (completedAt || null) + if (!id) return null + const rows = [...view.results.filter(row => isFailure(row.status)), ...view.results.filter(row => !isFailure(row.status))] + return { + id, + commandLine, + retryLine, + command: view.command ?? 'dbt', + elapsedSec: view.elapsedSec, + completedAt, + total: view.results.length, + passed: view.results.filter(row => PASSED.has(row.status)).length, + failed: view.results.filter(row => isFailure(row.status)).length, + skipped: view.results.filter(row => SKIPPED.has(row.status)).length, + rows: rows.slice(0, MAX_ROWS).map(row => ({ + ...row, + message: row.message === null ? null : row.message.length > MAX_MESSAGE ? `${row.message.slice(0, MAX_MESSAGE)}…` : row.message, + })), + } +} + +/** `run` first, the older ones after it, at most MAX_RUNS; the same invocation is never kept twice. */ +export function withRun(runs: readonly RunRecord[], run: RunRecord): RunRecord[] { + return [run, ...runs.filter(one => one.id !== run.id)].slice(0, MAX_RUNS) +} + +/** `held` cut to MAX_STORE_BYTES: the oldest run of the project with the most runs goes first. */ +export function fitStore(held: Held): Held { + const next: Held = { ...held } + while (JSON.stringify(next).length > MAX_STORE_BYTES) { + const biggest = Object.keys(next).sort((a, b) => next[b]!.length - next[a]!.length)[0] + if (!biggest) break + next[biggest] = next[biggest]!.slice(0, -1) + if (next[biggest]!.length === 0) delete next[biggest] + } + return next +} + +/** The line Claude gets: "build: 41 ok, 2 errors: stg_orders, fct_sales". */ +export function summaryLine(run: RunRecord): string { + const names = run.rows.filter(row => isFailure(row.status)).slice(0, NAMED).map(row => row.name) + const more = run.failed > names.length ? ` and ${run.failed - names.length} more` : '' + return [ + `${run.command}: ${run.passed} ok`, + run.failed > 0 ? `${run.failed} error${run.failed === 1 ? '' : 's'}: ${names.join(', ')}${more}` : null, + run.skipped > 0 ? `${run.skipped} skipped` : null, + ] + .filter(part => part !== null) + .join(', ') +} + +const seconds = (value: number | null) => (value === null ? '-' : `${value < 10 ? value.toFixed(1) : Math.round(value)}s`) + +async function held($: EngineInterface): Promise { + const value = await $.store.get(STORE_KEY) + return value && typeof value === 'object' && !Array.isArray(value) ? (value as Held) : {} +} + +/** The pane's runs for `root`, from the store. */ +async function load($: EngineInterface, root: string, open: string | null = null) { + const runs = (await held($))[root] ?? [] + await update($, runsAtom, () => ({ root, runs, open })) +} + +/** Reads the last run's results and records it unless it is the one recorded last; the line for Claude, or null. */ +async function record($: EngineInterface, e: BashCall): Promise { + const invocation = resultsInvocation(e.command) + if (!invocation) return null + const root = await $.session.root() + const until = (await $.clock.now()) + READ_MS + const readable = (metadata: unknown): RunView | null => { + const view = metadata as RunView | null + return view && Array.isArray(view.results) ? view : null + } + let result = await runTool($, 'dbt_run_results', {}, { until }) + let view = result.ok ? readable(result.metadata) : null + // An MCP answer without the rows (a build that leaves structured content out): the CLI's carries them. + if (!view && result.ok) { + result = await runTool($, 'dbt_run_results', {}, { until, cliOnly: true }) + view = result.ok ? readable(result.metadata) : null + } + if (!view || view.found === false) return null + const run = toRecord(view, invocation.commandLine, invocation.retryLine) + if (!run) return null + const all = await held($) + const runs = all[root] ?? [] + // The same file as the last recording: this command wrote nothing new (it failed before it started). + if (runs[0]?.id === run.id) return null + const next = fitStore({ ...all, [root]: withRun(runs, run) }) + await $.store.set(STORE_KEY, next) + const shown = await read($, runsAtom) + if (shown?.root === root) await update($, runsAtom, () => ({ root, runs: next[root] ?? [], open: shown.open })) + return summaryLine(run) +} + /** Adds this feature's own hooks. */ -export function register(on: On, context: FeatureContext) {} +export function register(on: On, context: FeatureContext) { + on('command.run', { command: COMMAND }, async $ => { + try { + await load($, await $.session.root()) + await $.ui.open({ id: PANE, title: PANE_TITLE, ...PANE_SIZE, focus: true, closeOnEscape: true }) + } catch (error) { + return { text: `Could not open the dbt runs: ${String(error)}` } + } + return { text: 'The dbt runs are open.' } + }) + + on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => { + const { Box, Button, Text } = $.ui.resolve(e) + const state = await read($, runsAtom) + const runs = state?.runs ?? [] + const open = state?.open ? runs.find(run => run.id === state.open) : undefined + const show = (open: string | null) => update($, runsAtom, prev => (prev ? { ...prev, open } : prev)) + const fill = (text: string) => $.prompt.fill({ text, mode: 'replace' }) -/** After the session started: the place to register commands. */ -export async function sessionStart($: EngineInterface, e: Args<'session.start'>) {} + if (!open) { + return ( + + dbt runs + {runs.length === 0 ? ( + No dbt command has run in this project yet. + ) : ( + runs.map(run => ( +