diff --git a/plugins/altimate-code/hooks/altimate-cli.test.ts b/plugins/altimate-code/hooks/altimate-cli.test.ts index 0c2ab774..5d879bab 100644 --- a/plugins/altimate-code/hooks/altimate-cli.test.ts +++ b/plugins/altimate-code/hooks/altimate-cli.test.ts @@ -51,6 +51,27 @@ test('a write call never asks the MCP server, even when one is connected', async expect(argvs.map(argv => argv.slice(1))[1]).toEqual(['tool', 'run', 'dbt_manifest', '--args', '{}', '--json']) }) +test('a failure with no answer object keeps its first line of text; one with an object keeps the object\'s', async () => { + const failing = (run: () => unknown) => { + const { $ } = standIn(() => null) + $.process.run = async () => run() + return $ + } + const text = async (run: () => unknown) => (await runTool(failing(run), 'dbt_manifest', {})).text + expect(await text(() => ({ exitCode: 1, stdout: 'Error: no such tool\nusage: altimate tool run', stderr: 'ignored' }))).toBe('Error: no such tool') + expect(await text(() => ({ exitCode: 1, stdout: '', stderr: '\nTypeError: crashed\n at main' }))).toBe('TypeError: crashed') + expect(await text(() => ({ exitCode: 1, stdout: 'x'.repeat(900), stderr: '' }))).toBe('x'.repeat(500)) + expect(await text(() => ({ exitCode: 1, stdout: '', stderr: '' }))).toBe('') + // An answer cut off mid-JSON is no message: only stderr, if any, is shown. + expect(await text(() => ({ exitCode: 1, stdout: '{"status":"ok","out', stderr: '' }))).toBe('') + expect(await text(() => ({ exitCode: 1, stdout: '{"status":"ok","out', stderr: 'Killed: out of memory' }))).toBe('Killed: out of memory') + // An answer object speaks for itself: its output, or nothing, never a line of stderr. + expect(await text(() => ({ exitCode: 1, stdout: JSON.stringify({ status: 'error', output: 'boom', metadata: {} }), stderr: 'noise' }))).toBe('boom') + expect(await text(() => ({ exitCode: 1, stdout: JSON.stringify({ status: 'error', metadata: {} }), stderr: 'noise' }))).toBe('') + const result = await runTool(failing(() => ({ exitCode: 2, stdout: 'plain failure' })), 'dbt_manifest', {}) + expect(result).toEqual({ metadata: null, text: 'plain failure', ok: false }) +}) + test('within handles the work\'s rejection before it sets the deadline, so a throwing clock leaves none unhandled', async () => { let handled = false // A work that rejects, recording when a rejection handler is attached. diff --git a/plugins/altimate-code/hooks/altimate-cli.ts b/plugins/altimate-code/hooks/altimate-cli.ts index 6fb0d6b4..7ea5b3e4 100644 --- a/plugins/altimate-code/hooks/altimate-cli.ts +++ b/plugins/altimate-code/hooks/altimate-cli.ts @@ -184,5 +184,9 @@ export async function runTool($: EngineInterface, name: string, args: Record { const logs: string[] = [] + const atoms = new Map() // Enough of the engine for a feature that keeps state, reads files and registers commands; a stub needs none of it. const $ = { - ui: { log: (text: string) => void logs.push(text) }, - fs: { exists: async () => false, read: async () => '' }, - state: { get: async () => ({ value: null }), set: async () => ({ isSet: true }), update: async () => ({ isSet: true }) }, + ui: { log: (text: string) => void logs.push(text), status: async () => {}, toast: () => {}, open: async () => {} }, + fs: { exists: async () => false, read: async () => '', list: async () => [], stat: async () => null }, + state: { + get: async (key: unknown) => ({ value: atoms.get(JSON.stringify(key)) }), + set: async (key: unknown, value: unknown) => (atoms.set(JSON.stringify(key), value), { isSet: true }), + }, command: { register: async () => {} }, + // What the features that are built ask of the session: a project with nothing to report, no CLI beyond the data tools, no clock work run. + process: { run: async (argv: readonly string[]) => nothingToReport(argv) ?? { exitCode: 1, stdout: '', stderr: 'no CLI here' } }, + clock: { now: async () => 0, after: () => {}, every: () => {}, sleep: async () => {} }, + session: { root: async () => '/repo', cwd: async () => '/repo' }, + store: { get: async () => null, set: async () => {} }, + mcp: { connect: async () => ({ isConnected: false, reason: 'not_connected', message: 'off' }) }, } return { logs, $: $ as any } } @@ -40,7 +51,30 @@ test('attempt logs a feature function that throws at once, one that rejects, and expect(logs).toEqual(['runs · sessionStart failed: Error: at once', 'defer · bandLine failed: Error: later']) }) -test('with every feature a stub, each phase runs through the features and logs nothing', async () => { +test('no feature fails in any phase: every failure would be logged, and none is', async () => { + const { $, logs } = engine() + // The engine does record a failure: a feature function that throws is logged through it. + await attempt($, 'runs', 'sessionStart', () => { + throw new Error('broken') + }) + expect(logs.splice(0)).toEqual(['runs · sessionStart failed: Error: broken']) + const bash = { tool: 'Bash', command: 'dbt run -s a' } as any + const file = { tool: 'Edit', file_path: '/repo/models/a.sql' } as any + await featuresSessionStart($, {} as any) + await featuresTurnStart($, {} as any) + await featureBandLines($, {} as any) + await featuresBashRewrite($, bash) + await featuresBashGate($, bash) + await featuresBashAfter($, bash, {} as any) + await featuresEditAfter($, file, {} as any, { path: '/repo/models/a.sql', before: null }) + await featuresReadOrEditAfter($, file, {} as any, { path: '/repo/models/a.sql' }) + await featuresTurnComplete($, {} as any, {} as any) + await featuresSessionEnd($, {} as any) + // A feature may log what it does; what must not appear is a failure, which is logged as ` · failed: …`. + expect(logs.filter(line => line.includes(' failed: '))).toEqual([]) +}) + +test('each phase runs through the features, which find nothing to report and log nothing', async () => { const { $, logs } = engine() const bash = { tool: 'Bash', command: 'dbt run -s a' } as any const file = { tool: 'Edit', file_path: '/repo/models/a.sql' } as any @@ -51,7 +85,8 @@ test('with every feature a stub, each phase runs through the features and logs n expect(await featureBandLines($, {} as any)).toEqual([]) expect(await featuresBashRewrite($, bash)).toBe('dbt run -s a') expect(await featuresBashGate($, bash)).toBeNull() - expect(await featuresBashAfter($, bash, {} as any)).toEqual([]) + // A dbt command may get a note from a feature that watches dbt (what a run would scan); a call no feature watches gets none. + expect(await featuresBashAfter($, { tool: 'Bash', command: 'ls models' } as any, {} as any)).toEqual([]) expect(await featuresEditAfter($, file, {} as any, { path: '/repo/models/a.sql', before: null })).toEqual([]) expect(await featuresReadOrEditAfter($, file, {} as any, { path: '/repo/models/a.sql' })).toEqual([]) expect(logs).toEqual([]) diff --git a/plugins/altimate-code/hooks/features/sql-graph.test.tsx b/plugins/altimate-code/hooks/features/sql-graph.test.tsx index 47fe1c68..12e031de 100644 --- a/plugins/altimate-code/hooks/features/sql-graph.test.tsx +++ b/plugins/altimate-code/hooks/features/sql-graph.test.tsx @@ -1,5 +1,6 @@ import { expect, mock, test } from 'claude-code/testing' +import { nothingToReport } from '../inert-features' import { cteCount, inputsOf, orderNodes } from './sql-graph' const ROOT = '/repo' @@ -52,6 +53,8 @@ async function start($: any, on: any, options: Options = {}) { on('tool.list', () => ({ value: [] })) on('mcp.connect', () => ({ value: { isConnected: false, reason: 'not_connected', message: 'off' } })) on('process.run', async (_: unknown, e: { argv: readonly string[] }) => { + const inert = nothingToReport(e.argv) + if (inert) return { value: inert } const [, command] = e.argv // The integrations half of the plugin asks the CLI for its status at session start. if (command !== 'tool') return { value: { exitCode: 1, stdout: '', stderr: 'not this test' } } diff --git a/plugins/altimate-code/hooks/inert-features.ts b/plugins/altimate-code/hooks/inert-features.ts new file mode 100644 index 00000000..5f598d3c --- /dev/null +++ b/plugins/altimate-code/hooks/inert-features.ts @@ -0,0 +1,20 @@ +// For tests: what each data tool a feature asks for answers when there is nothing to report. A test's +// fake CLI answers these first, so the calls it records are its own and a feature that is built finds a +// project with nothing to say, whichever features are built. +const NOTHING_TO_REPORT: Record> = { + docs_share_comments: { threads: [] }, + dbt_health: { success: true, insights: [] }, + dbt_defer_state: { success: true, enabled: false, mode: 'none' }, + dbt_cost_estimate: { supported: false, adapter: 'none' }, + dbt_run_results: { found: false }, + // The docs feature shows a coverage line for any successful answer and passes over one that isn't, silently. + dbt_docs_coverage: { success: false }, +} + +/** The answer of the `altimate tool run ` in `argv` when `name` is one a feature asks for; null for any other command. */ +export function nothingToReport(argv: readonly string[]): { exitCode: number; stdout: string; stderr: string } | null { + const [, command, sub, name] = argv + const metadata = command === 'tool' && sub === 'run' && name !== undefined ? NOTHING_TO_REPORT[name] : undefined + if (!metadata) return null + return { exitCode: 0, stdout: JSON.stringify({ status: 'ok', output: '', metadata }), stderr: '' } +} diff --git a/plugins/altimate-code/hooks/lineage.test.tsx b/plugins/altimate-code/hooks/lineage.test.tsx index daafc11d..7ec790ab 100644 --- a/plugins/altimate-code/hooks/lineage.test.tsx +++ b/plugins/altimate-code/hooks/lineage.test.tsx @@ -1,7 +1,9 @@ import { expect, mock, test } from 'claude-code/testing' +import { commandDir } from './lineage' import { buildGraph, subgraph } from './lineage-graph' import { MANIFEST } from './lineage-graph.test' +import { nothingToReport } from './inert-features' const ROOT = '/repo' const MANIFEST_PATH = `${ROOT}/target/manifest.json` @@ -189,6 +191,8 @@ async function start($: any, on: any, options: Options = {}) { return { value: { content: [], isError: true } } }) on('process.run', async (_: unknown, e: { argv: readonly string[] }) => { + const inert = nothingToReport(e.argv) + if (inert) return { value: inert } const [, command, , tool] = e.argv // A slow CLI dbt_lineage: it answers once the test lets it. if (command === 'tool' && tool === 'dbt_lineage' && options.holdCliLineage) await options.holdCliLineage @@ -1741,3 +1745,17 @@ test("the pane's MCP column trace that never answers is given up on, and the CLI expect(calls.indexOf('mcp:impact_analysis:revenue')).toBeLessThan(calls.indexOf('cli:impact_analysis:revenue')) expect(logs.some(line => line.includes('impact_analysis(fct_orders, column: revenue) · read by 2 models'))).toBe(true) }) + +test('each cd in a command moves the folder it reads from, after any separator or blank line', () => { + expect(commandDir('cd a && cd "b c"; cd /abs\n cd rel', '/repo')).toBe('/abs/rel') + expect(commandDir('ls;\n\n cd models | cat', '/repo')).toBe('/repo/models') + expect(commandDir('echo cd nowhere', '/repo')).toBe('/repo') +}) + +test('a very long run of blank lines before a command is read at once', () => { + const began = Date.now() + expect(commandDir(`${'\n'.repeat(64000)}cd models`, '/repo')).toBe('/repo/models') + expect(commandDir(`${'\n'.repeat(64000)}dbt run -s orders`, '/repo')).toBe('/repo') + for (const blank of ['\r', '\r\n', '\u2028', '\u2029']) expect(commandDir(`${blank.repeat(32000)}cd models`, '/repo')).toBe('/repo/models') + expect(Date.now() - began).toBeLessThan(500) +}) diff --git a/plugins/altimate-code/hooks/lineage.tsx b/plugins/altimate-code/hooks/lineage.tsx index a6b47628..cbe461eb 100644 --- a/plugins/altimate-code/hooks/lineage.tsx +++ b/plugins/altimate-code/hooks/lineage.tsx @@ -271,9 +271,10 @@ function sqlFilesIn(command: string, cwd: string): string[] { }) } -// Where a shell command reads from: the session folder, moved by each `cd` in the command. -const CD = /(?:^|[;&|(\n]\s*)cd\s+("[^"]+"|'[^']+'|[^\s;&|)]+)/gm -function commandDir(command: string, cwd: string): string { +// Where a shell command reads from: the session folder, moved by each `cd` in the command. Blanks after a +// separator stop at any line break, which starts a line itself, so a long run of blank lines is read once, not once per line. +const CD = /(?:^|[;&|(\n\r\u2028\u2029][^\S\n\r\u2028\u2029]*)cd\s+("[^"]+"|'[^']+'|[^\s;&|)]+)/gm +export function commandDir(command: string, cwd: string): string { let dir = cwd for (const match of command.matchAll(CD)) { const target = match[1]!.replace(/^['"]|['"]$/g, '') diff --git a/plugins/altimate-code/hooks/task-impact.test.tsx b/plugins/altimate-code/hooks/task-impact.test.tsx index bea1a61d..e63222c5 100644 --- a/plugins/altimate-code/hooks/task-impact.test.tsx +++ b/plugins/altimate-code/hooks/task-impact.test.tsx @@ -2,6 +2,7 @@ import { expect, mock, test } from 'claude-code/testing' import { MIN_ALTIMATE_CODE } from './altimate-cli' import { DOWN, UP, embedded } from './impact-report.test' +import { nothingToReport } from './inert-features' const ROOT = '/repo' const MANIFEST = `${ROOT}/target/manifest.json` @@ -140,6 +141,8 @@ async function start($: any, on: any, options: Options = {}) { on('session.turns', () => ({ value: options.turns?.() ?? 1 })) on('process.run', (_: unknown, e: { argv: readonly string[] }) => { const argv = [...e.argv] + const inert = nothingToReport(argv) + if (inert) return { value: inert } runs.push(argv) const ok = (stdout = '') => ({ value: { exitCode: 0, stdout, stderr: '' } }) const [bin] = argv diff --git a/plugins/altimate-code/hooks/tool-chain.test.ts b/plugins/altimate-code/hooks/tool-chain.test.ts index f7514f1e..940fd6ad 100644 --- a/plugins/altimate-code/hooks/tool-chain.test.ts +++ b/plugins/altimate-code/hooks/tool-chain.test.ts @@ -1,9 +1,10 @@ -import { expect, test } from 'claude-code/testing' +import { expect, mock, test } from 'claude-code/testing' import { FEATURE_IDS } from './features' import { register } from './integrations' import { STATUS_ORDER, statusLineOf } from './status-line' import { within } from './altimate-cli' +import { nothingToReport } from './inert-features' import { afterCall, beforeCall, PHASES, type ChainSteps } from './tool-chain' /** Steps that record each phase they run, in order. */ @@ -86,15 +87,26 @@ test('the features are listed in the stated order, and the status line joins its expect(statusLineOf({ integrations: '', runs: '' })).toBeUndefined() }) -test('through the plugin, a Bash call passes unchanged while every feature is a stub', async ($, on) => { +test('through the plugin, a Bash call reaches the tool unchanged and its result comes back with no added context', async ($, on) => { + // The session beneath has nothing to report, so no feature that is built holds a call or adds to one it has no reason to. + mock.clock(on) + on('ui.status', () => ({ value: undefined })) + on('session.root', () => ({ value: '/repo' })) + on('fs.exists', () => ({ value: false })) + on('process.run', (_: unknown, e: { argv: readonly string[] }) => ({ value: { isStdoutTruncated: false, isStderrTruncated: false, ...(nothingToReport(e.argv) ?? { exitCode: 1, stdout: '', stderr: 'no CLI here' }) } })) const calls: any[] = [] on('tool.call', (_: unknown, e: any) => { calls.push(e) return { result: { ok: true } } }) - const bash: any = await $.tool.call({ tool: 'Bash', command: 'dbt run -s orders' }) - expect(calls.map(one => one.command ?? one.tool)).toEqual(['dbt run -s orders']) - expect(bash.context ?? []).toEqual([]) + // A dbt command may get a note from a feature that watches dbt (what a run would scan); a call no feature + // watches gets nothing. Neither is held or changed. + const dbt: any = await $.tool.call({ tool: 'Bash', command: 'dbt run -s orders' }) + const plain: any = await $.tool.call({ tool: 'Bash', command: 'ls models' }) + expect(calls.map(one => one.command ?? one.tool)).toEqual(['dbt run -s orders', 'ls models']) + expect(dbt.deny).toBeUndefined() + expect(plain.deny).toBeUndefined() + expect(plain.context ?? []).toEqual([]) }) test('the plugin registers the chain: one tool.call hook for Bash and the file tools, which runs the call it is given', async () => {