From dd02dbbf7d973fc3ee82f051f28fd67dec1a3f32 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 20 Nov 2026 00:27:57 +0530 Subject: [PATCH 01/13] fix: /clear keeps comments and health, plugin dbt runs keep the project's manifest, and five smaller demo bugs - /clear: the new conversation (a new session id, with empty state and no session.start) reloads the team comments and keeps project health's baseline, pending changes and issues; the status line keeps its parts. - The dbt runs the plugin starts itself (compile and validate, /dbt-show, Re-run, the health check's parse) write to target/.altimate-plugin, so the project's manifest keeps every model's compiled SQL. - Docs coverage after an SQL edit dbt has not compiled says it is from before the edit, and is read again from the edit check's manifest. - A model the task created is called new in the end-of-task summary, not an old altimate-code. - Inline (narrow) panes replace the no-op Min/Default/Max row with a note. - The flags /defer and /target add are told to Claude and shown in the band. - Resolve on a thread the lagging list still shows is done; a reply to one says it is no longer open. Co-Authored-By: Claude Opus 5.5 --- plugins/altimate-code/CHANGELOG.md | 11 ++ plugins/altimate-code/hooks/altimate-cli.ts | 10 +- .../hooks/clear-state.testkit.ts | 30 +++++ plugins/altimate-code/hooks/comments.test.tsx | 52 +++++++- plugins/altimate-code/hooks/dbt-mods-core.ts | 8 ++ plugins/altimate-code/hooks/dbt-mods.test.tsx | 117 +++++++++++++----- plugins/altimate-code/hooks/dbt-mods.tsx | 67 +++++++--- .../altimate-code/hooks/features/comments.tsx | 32 ++++- .../hooks/features/defer.test.ts | 37 +++++- .../altimate-code/hooks/features/defer.tsx | 65 ++++++++-- .../hooks/features/docs.test.tsx | 46 ++++++- plugins/altimate-code/hooks/features/docs.tsx | 72 ++++++++--- .../altimate-code/hooks/features/health.tsx | 62 +++++++++- plugins/altimate-code/hooks/features/index.ts | 37 ++++++ plugins/altimate-code/hooks/health.test.tsx | 55 +++++++- plugins/altimate-code/hooks/lineage.tsx | 2 +- plugins/altimate-code/hooks/pane-size.tsx | 16 ++- plugins/altimate-code/hooks/status-line.ts | 12 +- .../altimate-code/hooks/task-impact.test.tsx | 14 +++ plugins/altimate-code/hooks/task-impact.tsx | 7 +- plugins/altimate-code/types/index.d.ts | 4 + 21 files changed, 661 insertions(+), 95 deletions(-) create mode 100644 plugins/altimate-code/hooks/clear-state.testkit.ts diff --git a/plugins/altimate-code/CHANGELOG.md b/plugins/altimate-code/CHANGELOG.md index 6031c1a6..6daeab2c 100644 --- a/plugins/altimate-code/CHANGELOG.md +++ b/plugins/altimate-code/CHANGELOG.md @@ -2,6 +2,17 @@ All notable changes to this plugin will be documented in this file. +## [Unreleased] + +### Fixed +- After `/clear`, the new conversation loads the team comments again (the band, and the threads Claude gets when it reads a commented model) instead of waiting for `/comments` or the next poll, and project health keeps its baseline, the changes not yet checked and the issues found (`/health` no longer says the session has no project; a check the `/clear` cut short runs again). The status line keeps its parts. +- The dbt runs the plugin starts by itself (compile and validate's `dbt show` and `dbt ls`, `/dbt-show` and Re-run, and the project health check's parse) write their artifacts to `target/.altimate-plugin/` instead of `target/`, so they no longer replace the project's `manifest.json` (with every model's compiled SQL) or `run_results.json`. The health check runs its CLI call in its own process for that, with the project's `catalog.json` copied beside its manifest. The doctor's **Run dbt parse** still refreshes the project's own manifest. +- After Claude edits a model's SQL that dbt has not compiled since, its documentation coverage says it is from before the edit, and once the edit's compile-and-validate check has run the model, `/propagate` shows the coverage of the model as it is now. +- The end-of-task summary for a model the task created says its columns are new with an unknown reach, not "update altimate-code". +- When the Query results or lineage pane sits above the prompt (a terminal under 110 columns, or not fullscreen), Claude Code gives it the same height whatever size it asks for, so the Min / Default / Max row, which did nothing there, is replaced by a line saying so. +- `--defer --state …`, `--favor-state` and `--target …` that `/defer` and `/target` add to Claude's dbt commands are no longer invisible: Claude's result says what the command ran as, and a line above the prompt shows it until the next prompt. +- **Resolve** on a thread the lagging comment list still shows after it was resolved elsewhere is done (the thread is hidden) instead of answering "not an open thread"; a reply to such a thread says it is no longer open and reads the list again. + ## [1.8.1] — 2026-11-19 ### Fixed diff --git a/plugins/altimate-code/hooks/altimate-cli.ts b/plugins/altimate-code/hooks/altimate-cli.ts index 07bf8955..1c646406 100644 --- a/plugins/altimate-code/hooks/altimate-cli.ts +++ b/plugins/altimate-code/hooks/altimate-cli.ts @@ -18,11 +18,11 @@ export const NOT_FOUND = /ENOENT|not found|no such file/i export const MISSING_BINARY = new RegExp(`${NOT_FOUND.source}|(cannot|could not|failed to) (start|spawn|run)`, 'i') /** Runs the altimate CLI under the first name that is installed; 90 s unless the caller says otherwise. */ -export async function runAltimate($: EngineInterface, args: string[], timeoutMs = 90_000) { +export async function runAltimate($: EngineInterface, args: string[], timeoutMs = 90_000, env?: Record) { let lastError: unknown = null for (const bin of CLI_NAMES) { try { - return await $.process.run([bin, ...args], { timeoutMs }) + return await $.process.run([bin, ...args], { timeoutMs, ...(env && { env }) }) } catch (error) { if (!MISSING_BINARY.test(String(error))) throw error lastError = error @@ -191,6 +191,8 @@ export type RunToolOptions = { * item 4), as the skill does before it adds `--allow-write`. */ write?: boolean + /** Variables set for the CLI process (CLI route only, so a call that sets them never goes over MCP). */ + env?: Record } /** @@ -201,7 +203,7 @@ export type RunToolOptions = { */ export async function runTool($: EngineInterface, name: string, args: Record, options: RunToolOptions = {}) { const { until, write = false } = options - const cliOnly = options.cliOnly === true || write + const cliOnly = options.cliOnly === true || write || options.env !== undefined // 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()) @@ -241,7 +243,7 @@ export async function runTool($: EngineInterface, name: string, args: Record Promise { + let cleared = false + const written = new Set() + const name = (e: Address) => `${e.plugin}\u0000${e.key}\u0000${e.id ?? ''}` + // A $-call's hook answers `{ value }`: here the value is the read, `{ value, version }`. The version stays, so a write still lands. + on('state.get', async (_: unknown, e: Address, next: (e: Address) => Promise<{ value?: { value: unknown; version: number } }>) => { + const stored = await next(e) + return cleared && !written.has(name(e)) && stored.value ? { ...stored, value: { ...stored.value, value: undefined } } : stored + }) + on('state.set', (_: unknown, e: Address, next: (e: Address) => Promise) => { + if (cleared) written.add(name(e)) + return next(e) + }) + on('session.id', () => ({ value: cleared ? 'after-clear' : 'before-clear' })) + return async () => { + await $.session.end({ reason: 'clear', sessionId: 'before-clear' }) + cleared = true + written.clear() + } +} diff --git a/plugins/altimate-code/hooks/comments.test.tsx b/plugins/altimate-code/hooks/comments.test.tsx index 87f197d7..5b611c9e 100644 --- a/plugins/altimate-code/hooks/comments.test.tsx +++ b/plugins/altimate-code/hooks/comments.test.tsx @@ -1,5 +1,6 @@ import { expect, mock, test } from 'claude-code/testing' +import { playsClear } from './clear-state.testkit' import { PHASE_TIMEOUT_MS } from './tool-chain' import { FIRST_READ_WAIT_MS, contextBlock, groupByModel, placeThread, projectName, readOptions, sameText, relativePath, threadsOnFile, toThread } from './features/comments' @@ -795,14 +796,59 @@ test('a folder without a project of its own is looked up again from another proj expect(cli.filter(c => c.args.action === 'list').map(c => c.args.project)).toEqual(['jaffle_shop', 'other_shop', 'jaffle_shop', 'other_shop']) }) -test('a post to a thread a teammate already resolved says nothing was sent and does not lock the other threads', async ($, on) => { - const { clock, cli } = await start($, on, { failPost: true, failPostCode: 'unknown_thread' }) +test('resolving a thread a teammate already resolved, which the lagging list still shows, is done: it leaves the pane and the list is read again', async ($, on) => { + const second = { ...RAW, group_id: 12, file_path: 'models/customers.sql' } + const { clock, cli } = await start($, on, { failPost: true, failPostCode: 'unknown_thread', threads: [RAW, second] }) const ui = await $.ui.mount({ ...PANE, surface: 'terminal' }) await clock.settle() await ui.press({ key: '7:11-resolve' }) for (let i = 0; i < 3; i++) await clock.settle() - await ui.press({ key: '7:11-resolve' }) + expect(await ui.find({ type: 'Text', text: /Already resolved: the list was behind/ })).toBeDefined() + expect(await ui.find({ type: 'Button', key: '7:11-resolve' })).toBeUndefined() + expect(await ui.find({ type: 'Text', text: /Thread 11 is not an open thread/ })).toBeUndefined() + expect(cli.filter(c => c.args.action === 'list')).toHaveLength(2) + // The other thread is not locked. + await ui.press({ key: '7:12-resolve' }) for (let i = 0; i < 3; i++) await clock.settle() expect(cli.filter(c => c.args.action === 'resolve')).toHaveLength(2) await ui.unmount() }) + +test('a reply to a thread that is no longer open says so plainly and reads the list again', async ($, on) => { + const { clock, cli } = await start($, on, { failPost: true, failPostCode: 'unknown_thread' }) + const ui = await $.ui.mount({ ...PANE, surface: 'terminal' }) + await clock.settle() + await ui.input({ key: '7:11-reply', text: 'Yes' }) + for (let i = 0; i < 3; i++) await clock.settle() + expect(cli.filter(c => c.args.action === 'reply')).toHaveLength(1) + expect(await ui.find({ type: 'Text', text: /That thread is no longer open \(it was resolved, or the list was behind\): nothing was sent/ })).toBeDefined() + expect(cli.filter(c => c.args.action === 'list')).toHaveLength(2) + await ui.unmount() +}) + +test('after /clear the new conversation loads the threads again: the pane lists them and a read of the model gets them', async ($, on) => { + const clear = playsClear($, on) + const { clock, cli } = await start($, on) + await clear() + await clock.settle() + expect(cli.filter(c => c.args.action === 'list')).toHaveLength(2) + const ui = await $.ui.mount({ ...PANE, surface: 'terminal' }) + expect(await ui.find({ type: 'Text', text: 'models/orders.sql' })).toBeDefined() + await ui.unmount() + const result: any = await $.tool.call({ tool: 'Read', file_path: FILE }) + expect(result.context?.[0]).toContain('Grace Hopper: No, gross.') +}) + +test('after /clear the first read of a model waits for the new conversation\'s list, as a session\'s first read does', async ($, on) => { + const clear = playsClear($, on) + let release!: () => void + const held = new Promise(resolve => (release = resolve)) + const { clock } = await start($, on, { holdListCall: { n: 1, until: held } }) + await clear() + const reading = $.tool.call({ tool: 'Read', file_path: FILE }) + await clock.advance(10) + release() + await clock.advance(10) + const result: any = await reading + expect(result.context?.[0]).toContain('line 3, model, on “sum(amount)”') +}) diff --git a/plugins/altimate-code/hooks/dbt-mods-core.ts b/plugins/altimate-code/hooks/dbt-mods-core.ts index 301bc0a9..2e982e1a 100644 --- a/plugins/altimate-code/hooks/dbt-mods-core.ts +++ b/plugins/altimate-code/hooks/dbt-mods-core.ts @@ -472,6 +472,14 @@ export function targetPath(yml: string): string { return value && !value.includes('{{') ? value : 'target' } +/** + * The folder, inside the project's artifact folder, where the dbt runs the plugin starts by itself (a check's + * `dbt show`, a health check's parse) write their artifacts. dbt rewrites manifest.json on every run with the + * compiled SQL of only the nodes that run compiled: in the project's own folder that would drop every other + * model's compiled SQL (and replace run_results.json), so those runs go here and are read from here. + */ +export const SCRATCH_TARGET = '.altimate-plugin' + /** * The folders a project's files live in, from dbt_project.yml: its model, seed, snapshot, macro, test * and analysis paths (dbt's defaults when a key is absent). `''` is the project folder itself. diff --git a/plugins/altimate-code/hooks/dbt-mods.test.tsx b/plugins/altimate-code/hooks/dbt-mods.test.tsx index 2c9f9c53..d017951f 100644 --- a/plugins/altimate-code/hooks/dbt-mods.test.tsx +++ b/plugins/altimate-code/hooks/dbt-mods.test.tsx @@ -135,7 +135,7 @@ const DOCTOR = { ...RESULTS, requestId: 'altimate-doctor', props: { ...RESULTS.p type Options = { /** What dbt show answers: rows, or a failure's error lines. */ - show?: { rows?: Record[]; error?: string; node?: string | null; onlyName?: string; mustInclude?: string; raw?: { exitCode: number; stdout: string; stderr?: string }; writes?: string; invocation?: string } + show?: { rows?: Record[]; error?: string; node?: string | null; onlyName?: string; mustInclude?: string; raw?: { exitCode: number; stdout: string; stderr?: string }; writes?: string; claudeWrites?: string; invocation?: string } /** The node names `dbt ls` prints; the default is none. */ listed?: (string | { name: string; resource_type: string })[] /** What stops dbt from running: its spawn failing, or the shell finding no such command. Asked at each run. */ @@ -223,6 +223,7 @@ type Options = { async function start($: any, on: any, options: Options = {}) { const clock = mock.clock(on, { now: options.now ?? 1_000 }) const runs: string[][] = [] + const envs: Record[] = [] const cwds: string[] = [] const toasts: string[] = [] const prompts: string[] = [] @@ -326,7 +327,7 @@ async function start($: any, on: any, options: Options = {}) { if (!exists(e.path)) throw new Error('ENOENT') const isDir = !(rel in files) const mtimeMs = rel === 'target/run_results.json' ? (options.runResultsAt ?? 5_000) : (mtimes[rel] ?? 500) - const size = rel === 'target/manifest.json' && options.manifestSize ? options.manifestSize : rel === 'target/run_results.json' && options.runResultsSize ? options.runResultsSize : (files[rel]?.length ?? 0) + const size = /(^|\/)manifest\.json$/.test(rel) && options.manifestSize ? options.manifestSize : rel === 'target/run_results.json' && options.runResultsSize ? options.runResultsSize : (files[rel]?.length ?? 0) return { value: { kind: isDir ? 'dir' : 'file', size, mtimeMs, isLink: false } } }) on('fs.read', (_: unknown, e: { path: string }) => { @@ -349,8 +350,12 @@ async function start($: any, on: any, options: Options = {}) { }) let held = false let shows = 0 - on('process.run', async (_: unknown, e: { argv: readonly string[]; init?: { cwd?: string; stdin?: string } }) => { + on('process.run', async (_: unknown, e: { argv: readonly string[]; init?: { cwd?: string; stdin?: string; env?: Record } }) => { runs.push([...e.argv]) + envs.push(e.init?.env ?? {}) + // Where this dbt writes its artifacts: DBT_TARGET_PATH when set, else the project's target folder. + const given = e.init?.env?.DBT_TARGET_PATH + const out = given ? (given.startsWith(`${ROOT}/`) ? given.slice(ROOT.length + 1) : given) : (options.target ?? 'target') if (e.argv[2] === PROFILE_REDACT) { redactSent.push({ argv: [...e.argv], stdin: e.init?.stdin }) await options.holdRedact @@ -395,11 +400,14 @@ async function start($: any, on: any, options: Options = {}) { } if (verb === 'show') { const show = options.show ?? { rows: [{ order_id: 1, amount: 10 }] } - // A dbt show writes run_results.json, whoever's invocation is in it by the time the process exits. - if (show.writes) files['target/run_results.json'] = JSON.stringify({ ...RUN_RESULTS, metadata: { invocation_id: show.writes, generated_at: 'x' } }) - if (options.manifestAfterShow) { - files['target/manifest.json'] = JSON.stringify(options.manifestAfterShow) - mtimes['target/manifest.json'] = (mtimes['target/manifest.json'] ?? 1_000) + 1 + // A dbt show writes run_results.json to its own artifact folder; a run of Claude's that ends meanwhile writes the project's. + if (show.writes) files[`${out}/run_results.json`] = JSON.stringify({ ...RUN_RESULTS, metadata: { invocation_id: show.writes, generated_at: 'x' } }) + if (show.claudeWrites) files[`${options.target ?? 'target'}/run_results.json`] = JSON.stringify({ ...RUN_RESULTS, metadata: { invocation_id: show.claudeWrites, generated_at: 'x' } }) + // dbt parses the project and writes the manifest to its artifact folder, every time. + const parsed = options.manifestAfterShow ? JSON.stringify(options.manifestAfterShow) : files[`${options.target ?? 'target'}/manifest.json`] + if (parsed) { + files[`${out}/manifest.json`] = parsed + mtimes[`${out}/manifest.json`] = (mtimes[`${out}/manifest.json`] ?? 1_000) + 1 } // dbt selects a model by its name: the one the test's manifest gives the file, if it gave one. const asked = e.argv[e.argv.indexOf('--select') + 1] @@ -434,7 +442,7 @@ async function start($: any, on: any, options: Options = {}) { }) await $.session.start({ cwd: ROOT, surface: 'terminal', isInteractive: options.interactive ?? false }) await clock.settle() - return { redactSent, clock, runs, cwds, toasts, prompts, store, registered, storeGets, mtimes, opened, sizes, files, setBash: (next: typeof bash) => void (bash = next) } + return { redactSent, clock, runs, envs, cwds, toasts, prompts, store, registered, storeGets, mtimes, opened, sizes, files, setBash: (next: typeof bash) => void (bash = next) } } // ─── What the helpers read from dbt ───────────────────────────────────────────────────────────── @@ -2682,10 +2690,27 @@ test('a docked pane reports the transcript\'s width, which never sets the next d await clock.settle() expect(sizes.at(-1)).toMatchObject({ columns: 90 }) const inline = await $.ui.mount({ ...RESULTS, surface: 'terminal', props: { ...RESULTS.props, placement: 'inline' }, viewport: { columns: 100, rows: 50, isFullscreen: false } }) - await inline.press({ key: 'results-size-max' }) + await inline.unmount() + // The inline drawing gave the terminal's width: the next dock is sized from it. + const again = await $.ui.mount({ ...RESULTS, surface: 'terminal', viewport: { columns: 55, rows: 50, isFullscreen: true } }) + await again.press({ key: 'results-size-max' }) await clock.settle() expect(sizes.at(-1)).toMatchObject({ columns: 45, rows: 60 }) + await again.unmount() +}) + +test('seated inline above the prompt, where Claude Code gives the pane the same height whatever it asks for, the pane has no Min / Default / Max that would do nothing, and says why', async ($, on) => { + const { clock } = await start($, on) + await $.command.run({ command: 'dbt-show', args: 'orders' }) + await clock.settle() + const inline = await $.ui.mount({ ...RESULTS, surface: 'terminal', props: { ...RESULTS.props, placement: 'inline' }, viewport: { columns: 100, rows: 42, isFullscreen: false } }) + for (const size of ['min', 'default', 'max']) expect(await inline.find({ type: 'Button', key: `results-size-${size}` })).toBeUndefined() + expect(await inline.find({ type: 'Text', text: /Above the prompt the pane is as tall as Claude Code lets it be/ })).toBeDefined() await inline.unmount() + // Docked, the three are there. + const docked = await $.ui.mount({ ...RESULTS, surface: 'terminal' }) + expect(await docked.find({ type: 'Button', key: 'results-size-max' })).toBeDefined() + await docked.unmount() }) test('Max asks for the width Default asks for, Min for less, and the first open asks as Default does', async ($, on) => { @@ -3308,36 +3333,34 @@ test('a run_results.json over 4 MiB that is the one seen before the command is n await band.unmount() }) -for (const [what, show] of [ - ['one that changed across the plugin\'s process', { writes: 'plugin-show' }], -] as const) { - test(`${what}: the artifact the plugin's own dbt show wrote while Claude's command ran is not that command's result`, async ($, on) => { - let ctx: any - ctx = await start($, on, { - runResultsAt: 9_000_000, - show: { rows: [{ order_id: 1 }], ...show }, - // The plugin's check finishes while Claude's command is running. - duringBash: async () => { - await editOrders($) - await ctx.clock.settle() - }, - }) - ctx.setBash({ stdout: CUSTOMERS_FAILED, isError: true }) - await $.tool.call({ tool: 'Bash', command: 'dbt run -s customers' }) - await ctx.clock.settle() - const band = await $.ui.mount({ ...BAND, surface: 'terminal' }) - expect(await band.find({ type: 'Text', text: /customers/ })).toBeDefined() - expect(await band.find({ type: 'Text', text: /not_null_orders_amount/ })).toBeUndefined() - await band.unmount() +test('the plugin\'s own dbt show, run while Claude\'s command runs, writes its run results to its own folder: the project\'s are left to Claude\'s command', async ($, on) => { + let ctx: any + ctx = await start($, on, { + runResultsAt: 9_000_000, + show: { rows: [{ order_id: 1 }], writes: 'plugin-show' }, + // The plugin's check finishes while Claude's command is running. + duringBash: async () => { + await editOrders($) + await ctx.clock.settle() + }, }) -} + const projectResults = ctx.files['target/run_results.json'] + ctx.setBash({ stdout: CUSTOMERS_FAILED, isError: true }) + await $.tool.call({ tool: 'Bash', command: 'dbt run -s customers' }) + await ctx.clock.settle() + expect(ctx.files['target/run_results.json']).toBe(projectResults) + expect(ctx.files['target/.altimate-plugin/run_results.json']).toContain('plugin-show') + const band = await $.ui.mount({ ...BAND, surface: 'terminal' }) + expect(await band.find({ type: 'Text', text: /customers/ })).toBeDefined() + await band.unmount() +}) test('Claude\'s run that wrote run_results.json after the plugin\'s own dbt show, before the plugin read it, is still recorded', async ($, on) => { let ctx: any // The plugin's dbt printed its own id; by the time the process exited Claude's run had written the file. ctx = await start($, on, { runResultsAt: 9_000_000, - show: { rows: [{ order_id: 1 }], writes: 'claude-run', invocation: 'plugin-show' }, + show: { rows: [{ order_id: 1 }], claudeWrites: 'claude-run', invocation: 'plugin-show' }, duringBash: async () => { await editOrders($) await ctx.clock.settle() @@ -3888,3 +3911,31 @@ kitTest('dbt debug\'s error is scrubbed whole before it is cut, so a value that clearRedactions() } }) + +test('the plugin\'s own dbt runs write their artifacts to its own folder: the project\'s manifest, with every model\'s compiled SQL, is left as it was', async ($, on) => { + const { clock, runs, envs, files, mtimes } = await start($, on, { manifestAfterShow: { ...MANIFEST, metadata: { invocation_id: 'plugin-show' } } }) + const kept = files['target/manifest.json'] + const keptAt = mtimes['target/manifest.json'] + // The check of an edit, then /dbt-show. + const result: any = await $.tool.call({ tool: 'Edit', file_path: `${ROOT}/models/orders.sql`, old_string: 'a', new_string: 'b' }) + await clock.settle() + await $.command.run({ command: 'dbt-show', args: 'orders' }) + await clock.settle() + const shows = runs.map((argv, i) => ({ argv, env: envs[i]! })).filter(one => one.argv[0] === DBT && one.argv.includes('show')) + expect(shows).toHaveLength(2) + for (const one of shows) expect(one.env.DBT_TARGET_PATH).toBe(`${ROOT}/target/.altimate-plugin`) + expect(files['target/manifest.json']).toBe(kept) + expect(mtimes['target/manifest.json']).toBe(keptAt) + // The check reads the manifest its own dbt show wrote: the model as it is now. + expect(files['target/.altimate-plugin/manifest.json']).toContain('plugin-show') + expect(result.context?.find((one: string) => one.startsWith('Altimate compile-and-validate'))).toBeDefined() +}) + +test('the doctor\'s dbt parse, which the person asks for to refresh a stale manifest, still writes the project\'s own', async ($, on) => { + const { clock, runs, envs } = await start($, on) + await $.command.run({ command: 'dbt-doctor', args: '' }) + await clock.settle() + const parse = runs.findIndex(argv => argv[0] === DBT && argv[1] === 'parse') + if (parse >= 0) expect(envs[parse]!.DBT_TARGET_PATH).toBeUndefined() + for (const [i, argv] of runs.entries()) if (argv[0] === DBT && (argv[1] === '--version' || argv[1] === 'debug')) expect(envs[i]!.DBT_TARGET_PATH).toBeUndefined() +}) diff --git a/plugins/altimate-code/hooks/dbt-mods.tsx b/plugins/altimate-code/hooks/dbt-mods.tsx index 430fae07..de742aa3 100644 --- a/plugins/altimate-code/hooks/dbt-mods.tsx +++ b/plugins/altimate-code/hooks/dbt-mods.tsx @@ -45,6 +45,7 @@ import { dbtVerdict, RUN_VERBS, targetPath, + SCRATCH_TARGET, showPreview, stripAnsi, textTable, @@ -192,7 +193,7 @@ const SKIPPED = { exitCode: -2, stdout: '', stderr: 'skipped: a newer check repl * Runs a command in a project, after any dbt this plugin already started there. `wanted` is asked * when its turn comes: a check a newer edit replaced runs no warehouse query. */ -function runQueued($: EngineInterface, root: string, argv: () => Promise, options: { cwd?: string; timeoutMs?: number; wanted?: () => boolean } = {}) { +function runQueued($: EngineInterface, root: string, argv: () => Promise, options: { cwd?: string; timeoutMs?: number; wanted?: () => boolean; scratch?: boolean } = {}) { const before = queues.get(root) ?? Promise.resolve() const run = before .catch(() => {}) @@ -202,10 +203,15 @@ function runQueued($: EngineInterface, root: string, argv: () => Promise word === 'show' || word === 'ls') + // own only when it changed across the process, and when dbt printed its own id, only that one is. A run in + // the plugin's own folder leaves the project's artifact alone, unless the command names a folder itself. + const ownFolder = options.scratch && !command.some(word => word === '--target-path' || word.startsWith('--target-path=')) + const tracked = !ownFolder && command.some(word => word === 'show' || word === 'ls') const before = tracked ? await seenInvocation($, root).catch(() => null) : null - const done = await $.process.run(command, { cwd: options.cwd ?? root, timeoutMs: options.timeoutMs ?? DBT_TIMEOUT_MS }) + // A run the plugin starts by itself writes its artifacts to its own folder (a --target-path the command + // names still wins): the project's manifest keeps every model's compiled SQL. + const env = options.scratch ? { DBT_TARGET_PATH: await scratchOf($, root) } : undefined + const done = await $.process.run(command, { cwd: options.cwd ?? root, timeoutMs: options.timeoutMs ?? DBT_TIMEOUT_MS, ...(env && { env }) }) if (tracked) { const after = await seenInvocation($, root).catch(() => null) const own = printedInvocation(done.stdout) ?? (after !== null && after !== before ? after : null) @@ -250,8 +256,8 @@ function cannotRun(run: { exitCode: number; stdout: string; stderr: string }): b } /** Runs the project's dbt in the project, after any dbt this plugin already started there. */ -function runDbt($: EngineInterface, root: string, args: string[], timeoutMs = DBT_TIMEOUT_MS, wanted?: () => boolean) { - return runQueued($, root, async () => [await dbtBinary($, root), ...args], { timeoutMs, wanted }).then( +function runDbt($: EngineInterface, root: string, args: string[], timeoutMs = DBT_TIMEOUT_MS, wanted?: () => boolean, scratch = false) { + return runQueued($, root, async () => [await dbtBinary($, root), ...args], { timeoutMs, wanted, scratch }).then( done => { if (done !== SKIPPED) { if (cannotRun(done)) unrunnable.add(root) @@ -354,6 +360,11 @@ async function targetOf($: EngineInterface, root: string): Promise { return normalize(path.startsWith('/') ? path : `${root}/${path}`) } +/** Where the plugin's own dbt runs in the project write their artifacts (SCRATCH_TARGET): never the project's own. */ +async function scratchOf($: EngineInterface, root: string): Promise { + return `${await targetOf($, root)}/${SCRATCH_TARGET}` +} + /** The interpreter dbt's own launcher names on its first line (`#!/x/bin/python`); undefined for a native binary or an `env` launcher. */ async function dbtShebang($: EngineInterface, dbt: string): Promise { const launcher = dbt === 'dbt' ? await $.process.run(['which', 'dbt'], { timeoutMs: 5_000 }).then(one => one.stdout.trim()).catch(() => '') : dbt @@ -379,8 +390,8 @@ async function pythonFor($: EngineInterface, root: string): Promise { * manifest over 4 MiB (more than $.fs.read takes) is read for one file's node only, `rel`, through the * project's Python, as the lineage half reads one through altimate-code. */ -async function readManifest($: EngineInterface, root: string, rel?: string): Promise<{ manifest: any; mtimeMs: number } | null> { - const path = `${await targetOf($, root)}/manifest.json` +async function readManifest($: EngineInterface, root: string, rel?: string, dir?: string): Promise<{ manifest: any; mtimeMs: number } | null> { + const path = `${dir ?? (await targetOf($, root))}/manifest.json` const stat = await $.fs.stat(path).catch(() => null) if (!stat || stat.kind !== 'file') return null try { @@ -468,7 +479,7 @@ async function compileAndValidate($: EngineInterface, root: string, file: string // name (`defined_in`) is still previewed, and the path keeps it to that one version. const pkg = await projectName($, root) const selectorFor = (name: string | null) => [`path:${rel}`, ...(pkg ? [`package:${pkg}`] : []), ...(name ? [name] : [])].join(',') - const run1 = (args: string[]) => runDbt($, root, args, DBT_TIMEOUT_MS, current).catch(error => ({ exitCode: -1, stdout: '', stderr: String(error) })) + const run1 = (args: string[]) => runDbt($, root, args, DBT_TIMEOUT_MS, current, true).catch(error => ({ exitCode: -1, stdout: '', stderr: String(error) })) const show = (select: string) => run1(['show', '--select', select, '--indirect-selection', 'empty', '--limit', '1', '--output', 'json', '--log-format', 'json']) // A newer edit replaced this check before its turn: nothing ran, nothing to report. const replaced = () => notChecked('a newer check replaced this one').then(one => ({ ...one, note: '' })) @@ -487,8 +498,9 @@ async function compileAndValidate($: EngineInterface, root: string, file: string let run = await show(selector) if (run === SKIPPED) return replaced() if (cannotRun(run)) return notChecked(NO_DBT) - // dbt show re-parsed the project: the manifest now holds the model and schema.yml as they are. - let after = await readManifest($, root, rel) + // dbt show re-parsed the project: the manifest it wrote (in the plugin's own folder) holds the model and schema.yml as they are. + const scratch = await scratchOf($, root) + let after = await readManifest($, root, rel, scratch) let node = after ? nodeForFile(after.manifest, rel, await projectName($, root)) : null let named = typeof node?.node?.name === 'string' ? node.node.name : null let preview = run.exitCode === 0 ? showPreview(run.stdout, node?.id ?? known?.id) : null @@ -499,7 +511,7 @@ async function compileAndValidate($: EngineInterface, root: string, file: string const again = await show(selectorFor(named)) if (again === SKIPPED) return replaced() run = again - after = await readManifest($, root, rel) + after = await readManifest($, root, rel, scratch) node = after ? nodeForFile(after.manifest, rel, await projectName($, root)) : null named = typeof node?.node?.name === 'string' ? node.node.name : null preview = run.exitCode === 0 ? showPreview(run.stdout, node?.id ?? known?.id) : null @@ -511,7 +523,7 @@ async function compileAndValidate($: EngineInterface, root: string, file: string // list (a model just disabled, not a model at all) was not checked. const nothingShown = run.exitCode === 0 && !preview // A manifest that is there but could not be read (too big to slice, no interpreter) says nothing about this file. - const manifestUnread = !after && (await $.fs.stat(`${await targetOf($, root)}/manifest.json`).catch(() => null))?.kind === 'file' + const manifestUnread = !after && (await $.fs.stat(`${scratch}/manifest.json`).catch(() => null))?.kind === 'file' if (nothingShown && node) errors.push(`dbt show selected nothing for ${rel} (the model ${named ?? model}), so nothing was compiled or run. ${scrubDbt(stripAnsi(run.stdout)).split('\n').map(line => line.trim()).filter(Boolean).slice(-1)[0] ?? ''}`.trim()) const documentedColumns = node ? Object.values(node.node.columns ?? {}) : [] const documented = documentedColumns.map(column => String(column.name)) @@ -534,6 +546,7 @@ async function compileAndValidate($: EngineInterface, root: string, file: string const status: CheckView['status'] = errors.length ? 'error' : reason ? 'partial' : drift.missing.length || drift.undocumented.length ? 'warn' : 'ok' const view: CheckView = { ...blank, model: named ?? model, schema: node ? schemaOf(node.node) : blank.schema, status, reason, ran: !errors.length && !nothingShown, errors: errors.map(one => one.slice(0, 1500)), missing: drift.missing, undocumented: drift.undocumented, columns: returned ? returned.length : null, returned: returned ?? [], documented, ms, at: started } await setCheck($, view, newest) + if (current()) checkEnded(`${root}/${rel}`, view.ran && after ? { root, file: `${root}/${rel}`, manifest: `${scratch}/manifest.json` } : null) // An edit of a schema file or macro that came while this ran checks it again now. if (forcedWhileBusy && current() && !rechecking) { const waiting = forcedWhileBusy @@ -545,6 +558,28 @@ async function compileAndValidate($: EngineInterface, root: string, file: string return { view, note: checkNote(view), current, newest } } +/** A check that ran its model: the model's file, and the manifest its dbt show wrote (in the plugin's own folder). */ +export type CheckLanded = { root: string; file: string; manifest: string } +// Who waits for the next check of a file, by the file's normalized path. +const landedWaiters = new Map void)[]>() + +/** + * Resolves when the newest check of `file` ends: with its manifest when it compiled and ran the model (that + * manifest holds the model's SQL as it is now, where the project's own may hold it as it was at the last dbt + * compile), else null. Never rejects; a caller bounds the wait itself. + */ +export function nextCheckOf(file: string): Promise { + const key = normalize(file) + return new Promise(resolve => landedWaiters.set(key, [...(landedWaiters.get(key) ?? []), resolve])) +} + +function checkEnded(file: string, landed: CheckLanded | null) { + const key = normalize(file) + const waiting = landedWaiters.get(key) ?? [] + landedWaiters.delete(key) + for (const resolve of waiting) resolve(landed) +} + async function setCheck($: EngineInterface, view: CheckView, current: () => boolean) { if (!current()) return await update($, checkAtom, () => view) @@ -802,7 +837,7 @@ async function runShowWith($: EngineInterface, base: Omit runDbt($, root, args), asked) + return runShowWith($, { id: queryId(root, select, inline, []), title: titleOf(select, inline), root, select, inline, options: [], argv: null, cwd: null, limit, by: 'you' }, () => runDbt($, root, args, DBT_TIMEOUT_MS, undefined, true), asked) } /** @@ -820,7 +855,7 @@ async function rerun($: EngineInterface, query: QueryResult, asked = generation) const executable = written && (await $.fs.exists(written).catch(() => false)) ? written : await dbtBinary($, query.root) // The profile the replayed command reads is the one its own flags name, from the folder it runs in. await ensureRedactions($, query.root, { dir: cwd, profilesDir: profilesDirOf(query.argv), profile: flagOf(query.argv, '--profile') }) - return runShowWith($, query, () => runQueued($, query.root, async () => [executable, ...args], { cwd }), asked) + return runShowWith($, query, () => runQueued($, query.root, async () => [executable, ...args], { cwd, scratch: true }), asked) } // ─── 12 · Model context, before Claude guesses ─────────────────────────────────────────────────────── @@ -1814,7 +1849,7 @@ export function registerDbtMods(on: On, context: { options: PluginOptions }) { const recent = history.filter(one => !one.bookmarked && one.id !== result?.id).slice(0, HISTORY_SHOWN) // Clear history is offered while any query that is not bookmarked is kept, the one shown included. const clearable = history.some(one => !one.bookmarked) - const sizes = await sizeControls($, { Box, Button, Text }, { id: RESULTS_PANE, title: RESULTS_TITLE, spec: RESULTS_SIZES, keyPrefix: 'results', terminal: paneTerminalColumns(e) }, work => later($, work)) + const sizes = await sizeControls($, { Box, Button, Text }, { id: RESULTS_PANE, title: RESULTS_TITLE, spec: RESULTS_SIZES, keyPrefix: 'results', terminal: paneTerminalColumns(e), placement: (e.props as { placement?: string } | undefined)?.placement }, work => later($, work)) const table = (() => { if (!result) return Run /dbt-show <model>, or ask Claude to `dbt show` a model. diff --git a/plugins/altimate-code/hooks/features/comments.tsx b/plugins/altimate-code/hooks/features/comments.tsx index 24b73f90..11fb5ce5 100644 --- a/plugins/altimate-code/hooks/features/comments.tsx +++ b/plugins/altimate-code/hooks/features/comments.tsx @@ -37,6 +37,8 @@ const MODEL_FILE = /\.(sql|ya?ml|py|md|csv)$/i const PARTIAL_NOTE = 'Not every open thread is listed: the service sent only part of the shares, or the list was cut for size.' const UNCONFIRMED_NOTE = 'An earlier post is not confirmed yet: press Refresh first.' const LOCKED_NOTE = ' The thread stays locked until the list shows the post or a few minutes have passed (the list can lag): press Refresh later.' +const NOT_OPEN_NOTE = 'That thread is no longer open (it was resolved, or the list was behind): nothing was sent. The list is read again.' +const ALREADY_RESOLVED_NOTE = 'Already resolved: the list was behind. It is hidden now.' const REFUSALS = new Set(['read_only', 'missing_text', 'missing_ids', 'unknown_thread', 'incomplete_list']) const MIN_POLL_SECONDS = 30 const MAX_POLL_SECONDS = 86_400 @@ -214,6 +216,20 @@ export async function sessionStart($: EngineInterface, e: Args<'session.start'>) await attach($, cwd) } +/** + * After a /clear: the new conversation's `$.state` is empty, so the project is found and its threads are + * loaded again now, and its first read of a model waits for them as a session's first does. The poll and a + * post's lock carry on (sessionEnd keeps them: they describe the shared docs, not the conversation). + */ +export async function clearStart($: EngineInterface) { + firstWaitSpent = false + pendingFor = null + searched.clear() + within.clear() + const cwd = (await $.session.cwd().catch(() => null)) ?? (await $.session.root().catch(() => null)) + if (cwd) await attach($, cwd) +} + /** Also called for a subagent's turns, which carry `e.agentId`. */ export async function turnStart($: EngineInterface, e: Args<'turn.start'>) {} @@ -524,14 +540,22 @@ async function post($: EngineInterface, thread: CommentsThread, args: Record (mine !== generation ? current : { ...current, posting: null, note })) + // A thread that is no longer open: the list is read again, so it shows what the service holds now. + if (gone) { + release() + await load($) + } return } // Any other failure, a timeout included, may have landed: posting stays locked until a list of this @@ -557,7 +581,7 @@ async function post($: EngineInterface, thread: CommentsThread, args: Record one.shareId !== thread.shareId || one.groupId !== thread.groupId) : current.threads, }, diff --git a/plugins/altimate-code/hooks/features/defer.test.ts b/plugins/altimate-code/hooks/features/defer.test.ts index e48bab2f..687b9199 100644 --- a/plugins/altimate-code/hooks/features/defer.test.ts +++ b/plugins/altimate-code/hooks/features/defer.test.ts @@ -1,6 +1,6 @@ import { expect, mock, test } from 'claude-code/testing' -import { effective, rewriteDeferCommand, runsHere, runsInProject, statusText } from './defer' +import { effective, rewriteDefer, rewriteDeferCommand, runsHere, runsInProject, statusText } from './defer' const on = { stateDir: '/work/prod-state' } @@ -411,3 +411,38 @@ test('a flag that takes no value before --project-dir leaves it a real option, a expect(rewriteDeferCommand('DBT_FAVOR_STATE=false env dbt run', options)).toBe('DBT_FAVOR_STATE=false env dbt run --defer --state /work/prod-state') expect(rewriteDeferCommand('echo hi; dbt run', options)).toBe('echo hi; dbt run --defer --state /work/prod-state --favor-state') }) + +test('the flags the plugin adds are told: rewriteDefer names each invocation\'s, the state folder relative to the project', () => { + const { command, added } = rewriteDefer('dbt build -s x && dbt test', { stateDir: `${ROOT}/prod`, favorState: true, target: 'dev' }, ROOT) + expect(command).toBe(`dbt build --defer --state ${ROOT}/prod --favor-state --target dev -s x && dbt test --defer --state ${ROOT}/prod --favor-state --target dev`) + expect(added).toEqual(['dbt build --defer --state prod --favor-state --target dev', 'dbt test --defer --state prod --favor-state --target dev']) + expect(rewriteDefer('ls', { stateDir: `${ROOT}/prod` }, ROOT).added).toEqual([]) +}) + +const BAND = { + plugin: 'altimate-code', + component: 'AbovePrompt', + props: { hasSurvey: false, isCollapsed: false, isWorking: false, maxRows: 10, bodyColumns: 120, scroll: { offset: 0, bodyRows: 10 }, view: {} }, +} as const + +test('Claude is told in the result, and the person in the band until the next prompt, the flags its dbt command ran with', async ($, on) => { + start($, on, DEFER) + on('ui.render', ($$: any, e: any) => { + const { Box } = $$.ui.resolve(e) + return h(Box, { key: 'engine' }) as any + }) + on('turn.start', (_: unknown, e: { turnId: string }) => ({ turnId: e.turnId })) + await $.session.start({ cwd: ROOT, surface: 'terminal', isInteractive: true }) + const ran: any = await $.tool.call({ tool: 'Bash', command: 'dbt build -s x' }) + expect(ran.context).toContain('◆ dbt · this command ran as dbt build --defer --state /work/prod-state --favor-state: /defer and /target added those flags (the command shown is as written), so its results are from that target and state') + const band = await $.ui.mount({ ...BAND, surface: 'terminal' }) + expect(await band.find({ type: 'Text', text: 'dbt build --defer --state /work/prod-state --favor-state' })).toBeDefined() + await band.unmount() + // A command the plugin left as written says nothing. + const own: any = await $.tool.call({ tool: 'Bash', command: 'dbt build --state ./mine' }) + expect((own.context ?? []).some((line: string) => line.startsWith('◆ dbt · this command ran as'))).toBe(false) + await $.turn.start({ turnId: 't2', prompt: 'next' } as any) + const after = await $.ui.mount({ ...BAND, surface: 'terminal' }) + expect(await after.find({ type: 'Text', text: /ran as/ })).toBeUndefined() + await after.unmount() +}) diff --git a/plugins/altimate-code/hooks/features/defer.tsx b/plugins/altimate-code/hooks/features/defer.tsx index f913afb1..ef7db2e8 100644 --- a/plugins/altimate-code/hooks/features/defer.tsx +++ b/plugins/altimate-code/hooks/features/defer.tsx @@ -16,6 +16,10 @@ import type { DeferState } from '../../types' import type { BandUi, FeatureContext } from './index' const deferAtom = atom({ plugin: 'altimate-code', key: 'defer' } as const, null as DeferState | null) +// What /defer and /target added to Claude's last dbt command, for the band, until the next prompt. +const ranAtom = atom({ plugin: 'altimate-code', key: 'deferRan' } as const, null as string | null) +// The flags added to each rewritten command, by the command as it ran (the after phase sees the rewrite), for its result. +const addedTo = new Map() // The commands that take `--defer` in every dbt version this reads state for (older dbt refuses the option on // `ls`, `parse`, `seed`, `run-operation` and others, and `retry` reads the previous run's results from `--state`). @@ -45,8 +49,17 @@ export type RewriteOptions = { * when nothing needs to change. An invocation with a word the parser cannot read (`$FLAGS`, `~/x`, a glob) is left as written. */ export function rewriteDeferCommand(command: string, options: RewriteOptions): string { + return rewriteDefer(command, options).command +} + +/** + * rewriteDeferCommand's command, with the flags it added to each dbt invocation, first invocation first, as the + * person reads them: the state folder named relative to `base` when it is inside it. + */ +export function rewriteDefer(command: string, options: RewriteOptions, base?: string): { command: string; added: string[] } { const { stateDir, favorState = false, target } = options - if (!stateDir && !target) return command + const added: string[] = [] + if (!stateDir && !target) return { command, added } let out = command // DBT_STATE or DBT_TARGET exported in the line is the user's own choice: none of its dbt commands gets the flag // (a line with an `export` and the variable anywhere in it counts, whatever the quoting). @@ -68,8 +81,17 @@ export function rewriteDeferCommand(command: string, options: RewriteOptions): s if (target && !NO_TARGET.has(one.subcommand) && one.target === null && !exportedTarget && !/\bDBT_TARGET=/.test(ownText)) flags.push('--target', quote(target)) if (flags.length === 0) continue out = `${out.slice(0, one.subcommandEnd)} ${flags.join(' ')}${out.slice(one.subcommandEnd)}` + const shown = flags.map(flag => (stateDir && flag === quote(stateDir) ? quote(relativeTo(base, stateDir)) : flag)) + added.unshift(`dbt ${one.subcommand} ${shown.join(' ')}`) } - return out + return { command: out, added } +} + +/** `path` relative to `base` when it is inside it, else as it is. */ +function relativeTo(base: string | undefined, path: string): string { + if (!base) return path + const root = base.replace(/[\\/]+$/, '') + return path.startsWith(`${root}/`) ? path.slice(root.length + 1) : path } type DeferAnswer = { @@ -411,13 +433,18 @@ export async function sessionStart($: EngineInterface, e: Args<'session.start'>) // A new session starts from the project's own settings, not from the last one's overrides. generation++ loading = null + addedTo.clear() await update($, deferAtom, () => null) + await update($, ranAtom, () => null) // Clearing the line is cosmetic: an engine that cannot draw it does not stop the session. await setStatusPart($, 'defer', null).catch(() => {}) } /** Also called for a subagent's turns, which carry `e.agentId`. */ -export async function turnStart($: EngineInterface, e: Args<'turn.start'>) {} +export async function turnStart($: EngineInterface, e: Args<'turn.start'>) { + // A new prompt: the band's line was about the last one's commands. A subagent's turn is part of the prompt's. + if (!(e as { agentId?: string }).agentId && (await read($, ranAtom))) await update($, ranAtom, () => null) +} /** 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'>) {} @@ -426,7 +453,16 @@ 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 + const ran = await read($, ranAtom) + if (!ran) return null + const { Text } = ui + return ( + + ↪ ran as + {ran} + · /defer, /target + + ) } /** The command to run in place of `e.command`; null keeps it. */ @@ -439,8 +475,12 @@ export async function bashRewrite($: EngineInterface, e: BashCall): Promise null)) ?? (await $.session.root().catch(() => null)) if (!root) return null if (!(await runsHere($, e.command, root, state.projectDir))) return null - const command = rewriteDeferCommand(e.command, effective(state)) - return command === e.command ? null : command + const { command, added } = rewriteDefer(e.command, effective(state), state.projectDir) + if (command === e.command) return null + // Kept for the call's after phase, which tells Claude what ran; a call that never gets there leaves at most a few. + if (addedTo.size >= 20) addedTo.delete(addedTo.keys().next().value!) + addedTo.set(command, added) + return command } /** @@ -451,7 +491,18 @@ export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: return null } -export async function bashAfter($: EngineInterface, e: BashCall, result: ToolCallResult): Promise {} +/** + * The flags the plugin added to a dbt command: the transcript shows the command as Claude wrote it, so Claude is + * told in the result, and the person in the band, what really ran. + */ +export async function bashAfter($: EngineInterface, e: BashCall, result: ToolCallResult): Promise { + const added = addedTo.get(e.command) + if (!added) return + addedTo.delete(e.command) + const text = added.join('; ') + await update($, ranAtom, () => text) + return [`◆ dbt · this command ran as ${text}: /defer and /target added those flags (the command shown is as written), so its results are from that target and state`] +} /** `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 {} diff --git a/plugins/altimate-code/hooks/features/docs.test.tsx b/plugins/altimate-code/hooks/features/docs.test.tsx index a393dd41..f40791ae 100644 --- a/plugins/altimate-code/hooks/features/docs.test.tsx +++ b/plugins/altimate-code/hooks/features/docs.test.tsx @@ -200,7 +200,21 @@ const DOWNSTREAM_AFTER = `models: description: "The customer's key, as the shop issues it" ` -type Options = { refuse?: boolean; downstream?: string; reach?: Record; coverage?: Record | null; coverageError?: string; noManifest?: boolean; stale?: boolean; newFile?: boolean; manifest?: Record } +type Options = { + refuse?: boolean + downstream?: string + reach?: Record + coverage?: Record | null + coverageError?: string + noManifest?: boolean + stale?: boolean + newFile?: boolean + manifest?: Record + /** A model file written after the project's manifest: dbt has not run since the edit. */ + editNewer?: boolean + /** The edit's check (the dbt helpers): what its dbt show previews, the manifest it writes, and the coverage read from that manifest. */ + check?: { rows: Record[]; node: string; manifest: Record; coverage: Record } +} /** A dbt project at /repo with the plugin's MCP server connected, and the session beneath the plugin. */ async function start($: any, on: any, options: Options = {}) { @@ -234,10 +248,11 @@ async function start($: any, on: any, options: Options = {}) { return { text: e.text } }) on('fs.exists', (_: unknown, e: { path: string }) => ({ value: e.path === `${ROOT}/dbt_project.yml` || ((e.path === SCHEMA && !options.newFile) || e.path === DOWNSTREAM) || (!options.noManifest && e.path === MANIFEST_PATH) })) - on('fs.stat', () => ({ value: { kind: 'file', size: 100, mtimeMs: 1, isLink: false } })) + on('fs.stat', (_: unknown, e: { path: string }) => ({ value: { kind: 'file', size: 100, mtimeMs: options.editNewer && e.path.endsWith('.sql') ? 2 : 1, isLink: false } })) on('fs.read', (_: unknown, e: { path: string }) => { if (e.path === `${ROOT}/dbt_project.yml`) return { value: "name: 'shop'\n" } if (e.path === MANIFEST_PATH && options.manifest) return { value: JSON.stringify({ nodes: options.manifest }) } + if (e.path === `${ROOT}/target/.altimate-plugin/manifest.json` && options.check) return { value: JSON.stringify({ nodes: options.check.manifest }) } // The edit's text from before is read first, then the file as the edit left it. // Each edit reads the file three times: before it, after it, and when the traces are back. if (e.path === SCHEMA) { @@ -261,6 +276,11 @@ async function start($: any, on: any, options: Options = {}) { }) on('process.run', (_: unknown, e: { argv: readonly string[] }) => { const [, command, , tool] = e.argv + // The edit's check: dbt show previews the model as the edit left it. + if (e.argv[0] === 'dbt' && command === 'show' && options.check) { + const preview = { data: { is_inline: false, node_name: options.check.node, output_format: 'json', preview: JSON.stringify(options.check.rows), unique_id: `model.shop.${options.check.node}` }, info: { code: 'Q041', level: 'info', msg: '…' } } + return { value: { exitCode: 0, stdout: JSON.stringify(preview), stderr: '' } } + } if (command !== 'tool' || tool === 'docs_share_comments') return { value: { exitCode: 1, stdout: '', stderr: 'not this test' } } // The project health check asks for its own tool at session start: not a call this feature makes. if (tool === 'dbt_health') return { value: nothingToReport(e.argv)! } @@ -268,7 +288,8 @@ async function start($: any, on: any, options: Options = {}) { toolArgs.push(String(e.argv[5])) if (tool === 'dbt_docs_coverage') { if (options.coverageError) return { value: { exitCode: 1, stdout: JSON.stringify({ status: 'error', output: options.coverageError, metadata: { success: false } }), stderr: '' } } - const body = options.coverage === null ? { status: 'error', output: 'Unknown tool: dbt_docs_coverage', metadata: {} } : { status: 'ok', output: 'coverage', metadata: options.coverage } + const fromCheck = options.check && String(e.argv[5]).includes('.altimate-plugin') + const body = options.coverage === null ? { status: 'error', output: 'Unknown tool: dbt_docs_coverage', metadata: {} } : { status: 'ok', output: 'coverage', metadata: fromCheck ? options.check!.coverage : options.coverage } return { value: { exitCode: options.coverage === null ? 1 : 0, stdout: JSON.stringify(body), stderr: '' } } } return { value: { exitCode: 0, stdout: JSON.stringify({ status: 'ok', output: '', metadata: options.reach ?? REACH }), stderr: '' } } @@ -622,3 +643,22 @@ test('a plain description with a blank line in it is read to its last paragraph, ['orders.status', 'Plain'], ]) }) + +const COVERAGE_PANE = { plugin: 'altimate-code', component: 'Pane', requestId: 'altimate-docs', props: { title: 'Altimate docs', isFocused: false, bodyColumns: 120, placement: 'dock', scroll: { offset: 0, bodyRows: 30 }, view: {} } } as const + +kitTest('a SQL edit dbt has not compiled yet: the coverage says it is from before the edit, and the edit\'s check replaces it with the model as it is now', { options: { dbt_mods: true } }, async ($, on) => { + const node = (columns: string[]) => ({ 'model.shop.orders': { resource_type: 'model', name: 'orders', package_name: 'shop', original_file_path: 'models/marts/orders.sql', columns: Object.fromEntries(columns.map(name => [name, { name, description: 'd' }])) } }) + // The project's manifest is from before the rename: every column it knows is documented. + const before = { success: true, model: 'orders', has_description: true, output_columns: ['order_id', 'payment_count'], documented: ['order_id', 'payment_count'], missing: [], percent: 100 } + // The check's manifest has the model as the edit left it: payment_count is now payments, which nothing documents. + const after = { success: true, model: 'orders', has_description: true, output_columns: ['order_id', 'payments'], documented: ['order_id'], missing: ['payments'], percent: 50 } + const { clock } = await start($, on, { editNewer: true, manifest: node(['order_id', 'payment_count']), coverage: before, check: { rows: [{ order_id: 1, payments: 2 }], node: 'orders', manifest: node(['order_id', 'payment_count']), coverage: after } }) + const result: any = await $.tool.call({ tool: 'Edit', file_path: `${ROOT}/models/marts/orders.sql`, old_string: 'payment_count', new_string: 'payments' }) + const line = result.context?.find((one: string) => one.startsWith('◆ docs')) + expect(line).toContain('as of the last dbt compile, before this edit') + await clock.settle() + const ui = await $.ui.mount({ ...COVERAGE_PANE, surface: 'terminal' }) + expect(await ui.find({ type: 'Text', text: /orders: 1 of 2 columns documented \(50%\) · no description: payments/ })).toBeDefined() + expect(await ui.find({ type: 'Text', text: /2 of 2 columns documented/ })).toBeUndefined() + await ui.unmount() +}) diff --git a/plugins/altimate-code/hooks/features/docs.tsx b/plugins/altimate-code/hooks/features/docs.tsx index 4d7c8413..63971348 100644 --- a/plugins/altimate-code/hooks/features/docs.tsx +++ b/plugins/altimate-code/hooks/features/docs.tsx @@ -9,7 +9,8 @@ import { atom, read, update } from 'claude-code' import type { Args, EngineInterface, EventResult, On, RenderElement, ToolCallResult } from 'claude-code' import type { DocsCandidate, DocsCoverageView, DocsGroup, DocsState } from '../../types' -import { later, runTool } from '../altimate-cli' +import { later, runTool, within } from '../altimate-cli' +import { nextCheckOf } from '../dbt-mods' import { setStatusPart } from '../status-line' import type { BashCall, ContextLines, EditedFile, FileCall } from '../tool-chain' import type { BandUi, FeatureContext } from './index' @@ -482,21 +483,17 @@ async function schemaEdited($: EngineInterface, file: EditedFile, created: boole return lines } -async function sqlEdited($: EngineInterface, file: EditedFile): Promise { - const project = await projectOf($, file.path) - if (!project) return [] - const model = normal(file.path).split('/').pop()!.replace(/\.sql$/i, '') - if (!(await $.fs.exists(project.manifest).catch(() => false))) return [] - const until = (await $.clock.now()) + ROUND_MS - const result = await runTool($, 'dbt_docs_coverage', { model: await modelRef($, project, model, true), manifest_path: project.manifest }, { until, cliOnly: true }).catch(() => null) - if (!result) return [] +/** The model's documentation coverage as `manifest` has it; a line for Claude when the tool is missing; null when it could not be read. */ +async function coverageOf($: EngineInterface, project: Project, model: string, manifest: string, until: number): Promise<{ view: DocsCoverageView; star: boolean } | { line: string } | null> { + const result = await runTool($, 'dbt_docs_coverage', { model: await modelRef($, project, model, true), manifest_path: manifest }, { until, cliOnly: true }).catch(() => null) + if (!result) return null const meta = result.metadata if (!result.ok || !meta || meta.success !== true) { if (MISSING_TOOL.test(result.text) && !saidOldCoverage) { saidOldCoverage = true - return ['◆ docs · this altimate-code has no dbt_docs_coverage: npm i -g @altimateai/altimate-code'] + return { line: '◆ docs · this altimate-code has no dbt_docs_coverage: npm i -g @altimateai/altimate-code' } } - return [] + return null } const view: DocsCoverageView = { model: typeof meta.model === 'string' && meta.model ? meta.model : model, @@ -505,17 +502,60 @@ async function sqlEdited($: EngineInterface, file: EditedFile): Promise { + const project = await projectOf($, file.path) + if (!project) return [] + const model = normal(file.path).split('/').pop()!.replace(/\.sql$/i, '') + const manifest = await $.fs.stat(project.manifest).catch(() => null) + if (!manifest || manifest.kind !== 'file') return [] + // dbt wrote the manifest before this edit: its compiled SQL is the model as it was, not as it is now. + const edited = await $.fs.stat(file.path).catch(() => null) + const stale = !!edited && manifest.mtimeMs < edited.mtimeMs + const asked = stale ? { file: normal(file.path), model, project } : null + waiting = asked + // Asked for before the check starts (the dbt helpers run after this feature in the edit phase), so it is not missed. + if (asked) { + const check = nextCheckOf(file.path) + later($, () => coverageAfterCheck($, asked, check)) + } + const read = await coverageOf($, project, model, project.manifest, (await $.clock.now()) + ROUND_MS) + if (!read) return [] + if ('line' in read) return [read.line] + const view = { ...read.view, stale } await update($, docsAtom, prev => ({ ...(prev ?? empty()), coverage: view })) await showStatus($) const gaps = [...(view.hasDescription ? [] : ['the model itself']), ...view.missing] // A select list that is not plain column names (a `*`, an unaliased expression, a UNION) is counted by the manifest's columns only. - const star = meta.source === 'manifest_columns' ? " · the model's select list is not plain column names (a `*`, an unaliased expression or a UNION), so only the columns the manifest lists are counted" : '' - if (!gaps.length && !star) return [] + const star = read.star ? " · the model's select list is not plain column names (a `*`, an unaliased expression or a UNION), so only the columns the manifest lists are counted" : '' + if (!gaps.length && !star && !stale) return [] // Names come from the project and this line goes to Claude, so each is a quoted string. const shown = gaps.slice(0, MAX_NAMES).map(one => JSON.stringify(one)).join(', ') + (gaps.length > MAX_NAMES ? `, +${gaps.length - MAX_NAMES} more` : '') - // The manifest's compiled SQL is from the last dbt compile, not from this edit. - return [`◆ docs · ${JSON.stringify(view.model)}: ${view.documented} of ${view.total} columns documented (${view.percent}%)${gaps.length ? ` · no description for: ${shown}` : ''}${star} · as of the last dbt compile`] + // The manifest's compiled SQL is from the last dbt compile: from before this edit when dbt has not run since. + const asOf = stale ? ' · as of the last dbt compile, before this edit: columns this edit added, renamed or dropped are not counted yet' : ' · as of the last dbt compile' + return [`◆ docs · ${JSON.stringify(view.model)}: ${view.documented} of ${view.total} columns documented (${view.percent}%)${gaps.length ? ` · no description for: ${shown}` : ''}${star}${asOf}`] +} + +/** Once the edit's check ran its model, the coverage shown from before that edit is read again from the check's manifest. */ +async function coverageAfterCheck($: EngineInterface, asked: { file: string; model: string; project: Project }, check: Promise<{ manifest: string } | null>) { + const landed = await within($, CHECK_WAIT_MS, check) + if (!landed || waiting !== asked) return + const read = await coverageOf($, asked.project, asked.model, landed.manifest, (await $.clock.now()) + RECHECK_MS) + // A newer edit asks for its own coverage: this one is no longer shown. + if (waiting !== asked || !read || 'line' in read) return + waiting = null + await update($, docsAtom, prev => ({ ...(prev ?? empty()), coverage: read.view })) } async function showStatus($: EngineInterface) { @@ -559,6 +599,7 @@ export function register(on: On, context: FeatureContext) { {coverage.model}: {coverage.documented} of {coverage.total} columns documented ({coverage.percent}%) {coverage.missing.length ? ` · no description: ${coverage.missing.slice(0, MAX_NAMES).join(', ')}` : ''} + {coverage.stale ? ' · from the manifest of before the last edit (the edit\'s check reads it again)' : ''} ) : null} {groups.length === 0 ? No description to copy: when Claude writes a column description, the downstream columns that carry the same value are listed here. : null} @@ -653,6 +694,7 @@ 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'>) { + waiting = null saidOldReach = false saidOldCoverage = false applied.clear() diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 030f5588..e38ea1c4 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -15,6 +15,7 @@ import type { Args, EngineInterface, EventResult, On, PluginOptions, RenderEleme import type { HealthInsight, HealthState } from '../../types' import { NOT_FOUND, later, runTool, within } from '../altimate-cli' import { setStatusPart } from '../status-line' +import { SCRATCH_TARGET, targetPath } from '../dbt-mods-core' import type { BashCall, ContextLines, EditedFile, FileCall } from '../tool-chain' import type { BandUi, FeatureContext } from './index' @@ -51,6 +52,10 @@ let enabled = false let eligible = false /** The problem told to the user last, so the same one is not told at every turn. */ let toldProblem: string | null = null +/** The files the running check was asked about: a check a /clear cuts short has them asked for again. */ +let inFlight: string[] = [] +/** What the conversation a /clear ended knew (its baseline, the changes not yet checked, the issues): the next one keeps it. */ +let carried: HealthState | null = null /** Which model each file belonged to in earlier checks: a deleted schema file or model no longer shows in the latest answer. */ let known: HealthInsight[] = [] /** Whether the status line carries the health part now. */ @@ -471,6 +476,27 @@ async function isModelFile($: EngineInterface, file: string): Promise { return project !== null && !(ROOT_FILES.has(path.split('/').pop() ?? '') && norm(project) === dir) } +/** + * What the check's CLI process is given so its parse never rewrites the project's manifest (which would drop + * every model's compiled SQL): dbt's artifact folder is the plugin's own (SCRATCH_TARGET), with the project's + * catalog copied in (DataPilot reads it beside the manifest), and the CLI runs the tool in its own process, since + * a running tool daemon keeps the environment it started with. + */ +async function checkEnv($: EngineInterface): Promise | undefined> { + const root = await $.session.root().catch(() => '') + const project = root ? await projectAbove($, root) : null + if (!project) return undefined + const path = targetPath(await $.fs.read(`${project}/dbt_project.yml`).catch(() => '')) + const target = path.startsWith('/') ? path : `${project}/${path}` + const scratch = `${target}/${SCRATCH_TARGET}` + if (await $.fs.exists(`${target}/catalog.json`).catch(() => false)) { + // Best effort: without the copy the check reads no catalog, as with a project that has none. + await $.process.run(['mkdir', '-p', scratch], { timeoutMs: 10_000 }).catch(() => null) + await $.process.run(['cp', '-p', `${target}/catalog.json`, `${scratch}/catalog.json`], { timeoutMs: 60_000 }).catch(() => null) + } + return { DBT_TARGET_PATH: scratch, ALTIMATE_TOOL_DAEMON: '0' } +} + /** Asks altimate-code's dbt_health for the project's insights. */ async function check($: EngineInterface): Promise { const badConfig = configPathProblem(options.healthConfigPath) @@ -478,7 +504,8 @@ async function check($: EngineInterface): Promise { let result: Awaited> try { const until = (await $.clock.now()) + CHECK_MS - result = await runTool($, 'dbt_health', toolArgs(options.healthConfigPath), { write: true, until }) + const env = await checkEnv($) + result = await runTool($, 'dbt_health', toolArgs(options.healthConfigPath), { write: true, until, ...(env && { env }) }) } catch (error) { // Only a binary that is not there: one that is there and refused to start keeps its own words. if (NOT_FOUND.test(String(error))) return { problem: NO_CLI } @@ -529,6 +556,7 @@ async function loop($: EngineInterface, mine: number) { if (!needsBaseline && before.pending.length === 0) return // The baseline run leaves `pending` alone: edits made while it ran are checked by the next run. const batch = needsBaseline ? [] : before.pending + inFlight = batch await update($, healthAtom, prev => ({ ...(prev ?? fresh()), pending: needsBaseline ? (prev?.pending ?? []) : (prev?.pending ?? []).filter(f => !batch.includes(f)), running: true })) const run = await check($) if (mine !== generation) return @@ -645,6 +673,8 @@ export async function sessionStart($: EngineInterface, e: Args<'session.start'>) generation += 1 running = null enabled = false + carried = null + inFlight = [] toldProblem = null known = [] shellBefore = null @@ -657,12 +687,39 @@ export async function sessionStart($: EngineInterface, e: Args<'session.start'>) // The baseline takes a while (the dbt adapter starts): nothing waits for it. A session with nobody at // the prompt checks only when the push gate needs it. eligible = e.isInteractive || options.healthBlocksPush === true || options.healthBlocksPush === 'true' + await enable($) +} + +/** Turns the checks on when the session's folder is in a dbt project, and starts the baseline. */ +async function enable($: EngineInterface) { const root = await $.session.root().catch(() => '') if (!eligible || !root || !(await projectAbove($, root))) return enabled = true later($, () => kick($)) } +/** + * After a /clear: the new conversation's `$.state` is empty. The baseline, the changes not yet checked and + * the issues of the conversation that ended are the project's, so they are kept (a change made before the + * /clear still holds a push); with none kept, the baseline is taken as at a session's start. + */ +export async function clearStart($: EngineInterface) { + const kept = carried + carried = null + generation += 1 + running = null + if (!kept) { + await update($, healthAtom, () => fresh()) + await enable($) + return + } + enabled = true + await update($, healthAtom, () => ({ ...kept, running: false })) + drawn = true + await drawStatus($, kept.issues) + if (await wantsCheck($)) later($, () => kick($)) +} + /** Also called for a subagent's turns, which carry `e.agentId`. */ export async function turnStart($: EngineInterface, e: Args<'turn.start'>) {} @@ -673,6 +730,9 @@ export async function turnComplete($: EngineInterface, e: Args<'turn.complete'>, } export async function sessionEnd($: EngineInterface, e: Args<'session.end'>) { + // A /clear: what this conversation knows is handed to the next one. A check it cuts short is asked for again there. + const state = e.reason === 'clear' && enabled ? await read($, healthAtom).catch(() => null) : null + carried = state ? { ...state, pending: state.running ? [...new Set([...inFlight, ...state.pending])] : state.pending, running: false } : null generation += 1 running = null enabled = false diff --git a/plugins/altimate-code/hooks/features/index.ts b/plugins/altimate-code/hooks/features/index.ts index 89652ec1..98d4b362 100644 --- a/plugins/altimate-code/hooks/features/index.ts +++ b/plugins/altimate-code/hooks/features/index.ts @@ -9,6 +9,8 @@ // A function that throws, at once or as a rejection, is logged and skipped: one feature's failure never stops another or the call. A `register` failure is silent (`$` is not available at load). import type { Args, EngineInterface, EventResult, On, PluginOptions, RenderElement, ToolCallInput, ToolCallResult } from 'claude-code' +import { later } from '../altimate-cli' +import { restoreStatusParts } from '../status-line' import type { BashCall, EditedFile, FileCall } from '../tool-chain' import { CHECK_HOLD_MS as dbtModsCheckHoldMs, @@ -68,6 +70,7 @@ import { import { register as commentsRegister, sessionStart as commentsSessionStart, + clearStart as commentsClearStart, turnStart as commentsTurnStart, turnComplete as commentsTurnComplete, sessionEnd as commentsSessionEnd, @@ -96,6 +99,7 @@ import { import { register as healthRegister, sessionStart as healthSessionStart, + clearStart as healthClearStart, turnStart as healthTurnStart, turnComplete as healthTurnComplete, sessionEnd as healthSessionEnd, @@ -228,6 +232,37 @@ export async function featuresSessionStart($: EngineInterface, e: Args<'session. await attempt($, 'dbtMods', 'sessionStart', () => sessionStartDbtMods($, e)) } +/** + * A conversation that a /clear started: no `session.start` comes for it, and its `$.state` starts empty, so the + * features whose state describes the project rather than the conversation fill it again. + */ +export async function featuresClearStart($: EngineInterface) { + await attempt($, 'statusLine', 'clearStart', () => restoreStatusParts($)) + await attempt($, 'comments', 'clearStart', () => commentsClearStart($)) + await attempt($, 'health', 'clearStart', () => healthClearStart($)) +} + +// How often, and how many times, the session's id is read for the one a /clear starts. +const CLEAR_POLL_MS = 200 +const CLEAR_POLLS = 50 + +/** + * After a /clear: the conversation that follows runs under a new session id, and its state is that id's. Once + * the id has changed, featuresClearStart fills it; an engine that cannot say its id gets nothing. + */ +async function whenCleared($: EngineInterface, ended: string) { + for (let i = 0; i < CLEAR_POLLS; i++) { + let id: string + try { + id = await $.session.id() + } catch { + return + } + if (id !== ended) return featuresClearStart($) + await $.clock.sleep(CLEAR_POLL_MS) + } +} + export async function featuresTurnStart($: EngineInterface, e: Args<'turn.start'>) { await attempt($, 'runs', 'turnStart', () => runsTurnStart($, e)) await attempt($, 'defer', 'turnStart', () => deferTurnStart($, e)) @@ -254,6 +289,8 @@ export async function featuresTurnComplete($: EngineInterface, e: Args<'turn.com } export async function featuresSessionEnd($: EngineInterface, e: Args<'session.end'>) { + // Taken before the features end: a /clear goes on under another id once they have. + if (e.reason === 'clear' && typeof e.sessionId === 'string') later($, () => whenCleared($, e.sessionId)) await attempt($, 'runs', 'sessionEnd', () => runsSessionEnd($, e)) await attempt($, 'defer', 'sessionEnd', () => deferSessionEnd($, e)) await attempt($, 'toModel', 'sessionEnd', () => toModelSessionEnd($, e)) diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index 4be2ac8e..a1b7edf8 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -1,5 +1,6 @@ import { expect, mock, test as engineTest } from 'claude-code/testing' +import { playsClear } from './clear-state.testkit' import { PHASE_TIMEOUT_MS } from './tool-chain' import { GATE_WAIT_MS, bandText, canonical, configPathProblem, filesWrittenBy, isGitPush, modelsOfFiles, newIssues, pairKey, pushRefusal, reportText, simpleCommands, toolArgs } from './features/health' @@ -68,7 +69,7 @@ type Options = { async function start($: any, on: any, options: Options, session: { isInteractive?: boolean } = {}) { const clock = mock.clock(on) - const calls: { tool: string; args: Record; argv: readonly string[] }[] = [] + const calls: { tool: string; args: Record; argv: readonly string[]; env?: Record }[] = [] const toasts: string[] = [] const statuses: string[] = [] on('command.register', () => ({ value: { command: 'health' } })) @@ -124,7 +125,7 @@ async function start($: any, on: any, options: Options, session: { isInteractive if (command !== 'tool') return { value: { exitCode: 1, stdout: '', stderr: 'not this test' } } const args = JSON.parse(String(e.argv[e.argv.indexOf('--args') + 1] ?? '{}')) const index = run++ - calls.push({ tool, args, argv: e.argv }) + calls.push({ tool, args, argv: e.argv, env: (e as { init?: { env?: Record } }).init?.env }) if (options.hold?.at === index) await options.hold.until const answer = options.runs[Math.min(index, options.runs.length - 1)]! if ('missing' in answer) return { deny: 'spawn ENOENT' } @@ -165,6 +166,56 @@ test('a baseline run at session start; a changed model with a new insight shows await band.unmount() }) +test('after /clear the new conversation keeps the baseline and the issues: the band, /health and the push gate still have them', async ($, on) => { + const clear = playsClear($, on) + const { clock, calls, statuses } = await start($, on, { runs: [[OLD_STG, OLD_FCT], [OLD_STG, OLD_FCT, NEW_STG]] }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + expect(calls).toHaveLength(2) + await clear() + await clock.settle() + expect(statuses.at(-1)).toContain('1 health issue') + // No second baseline: the one from the session's start still stands. + expect(calls).toHaveLength(2) + const band = await $.ui.mount({ ...BAND, surface: 'terminal' }) + expect(await band.find({ type: 'Text', text: '1 new health issue · stg_orders: hard coded references · /health' })).toBeDefined() + await band.unmount() + const shown: any = await $.command.run({ command: 'health' } as any) + expect(shown.text).toContain('1 new health issue · stg_orders: hard coded references') +}) + +test('a /clear while a check runs has the next conversation check those changes again', async ($, on) => { + const clear = playsClear($, on) + let release!: () => void + const { clock, calls } = await start($, on, { runs: [[OLD_STG], [OLD_STG], [OLD_STG, NEW_STG]], hold: { at: 1, until: new Promise(resolve => (release = resolve)) } }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + expect(calls).toHaveLength(2) + await clear() + release() + await clock.settle() + expect(calls).toHaveLength(3) + const shown: any = await $.command.run({ command: 'health' } as any) + expect(shown.text).toContain('1 new health issue · stg_orders') +}) + +test('after /clear with no baseline yet (the first check failed), /health in the new conversation takes it', async ($, on) => { + const clear = playsClear($, on) + const { clock, calls } = await start($, on, { runs: [{ fail: 'boom' }, [OLD_STG]] }) + expect(calls).toHaveLength(1) + await clear() + await clock.settle() + const shown: any = await $.command.run({ command: 'health' } as any) + expect(shown.text).toBe('No new health issues in the models changed this session.') + expect(calls).toHaveLength(2) +}) + +test('the check\'s parse writes to the plugin\'s own artifact folder, in a CLI process of its own: the project\'s manifest keeps its compiled SQL', async ($, on) => { + const { calls } = await start($, on, { runs: [[OLD_STG]] }) + expect(calls).toHaveLength(1) + expect(calls[0]!.env).toEqual({ DBT_TARGET_PATH: `${ROOT}/target/.altimate-plugin`, ALTIMATE_TOOL_DAEMON: '0' }) +}) + test('two changed models give "2 new health issues" with the first model named', async ($, on) => { const { clock } = await start($, on, { runs: [[OLD_STG], [OLD_STG, NEW_STG, NEW_FCT]] }) await $.tool.call(edit(STG)) diff --git a/plugins/altimate-code/hooks/lineage.tsx b/plugins/altimate-code/hooks/lineage.tsx index 5aab355c..08a0811d 100644 --- a/plugins/altimate-code/hooks/lineage.tsx +++ b/plugins/altimate-code/hooks/lineage.tsx @@ -1538,7 +1538,7 @@ export function registerLineage(on: On, options: Record = {}) { ) : null const relationNames = new Map(Object.values(graph.nodes).filter(one => one.relation).map(one => [one.relation!.toLowerCase(), one])) - const sizes = await sizeControls($, { Box, Button, Text }, { id: PANE, title: PANE_TITLE, spec: PANE_SIZES, keyPrefix: 'lineage', terminal: paneTerminalColumns(e) }, work => later($, work)) + const sizes = await sizeControls($, { Box, Button, Text }, { id: PANE, title: PANE_TITLE, spec: PANE_SIZES, keyPrefix: 'lineage', terminal: paneTerminalColumns(e), placement: (e.props as { placement?: string } | undefined)?.placement }, work => later($, work)) return ( diff --git a/plugins/altimate-code/hooks/pane-size.tsx b/plugins/altimate-code/hooks/pane-size.tsx index d8b2387a..fc1ab432 100644 --- a/plugins/altimate-code/hooks/pane-size.tsx +++ b/plugins/altimate-code/hooks/pane-size.tsx @@ -12,6 +12,7 @@ export type PaneSizes = Record export const SIZE_ORDER: PaneSize[] = ['min', 'default', 'max'] const SIZE_LABEL: Record = { min: 'Min', default: 'Default', max: 'Max' } const SIZES_KEY = 'pane-sizes' +export const INLINE_SIZE_NOTE = 'Above the prompt the pane is as tall as Claude Code lets it be; Min, Default and Max size it when it docks beside a fullscreen chat.' const sizeAtom = atom({ plugin: 'altimate-code', key: 'paneSizes' } as const, {} as Record) @@ -91,10 +92,21 @@ async function choose($: EngineInterface, id: string, title: string, spec: PaneS await $.ui.open({ id, title, ...paneSizes(spec)[size], closeOnEscape: true }) } -/** The Min / Default / Max row of a pane, the pane's current size marked. */ -export async function sizeControls($: EngineInterface, ui: { Box: any; Button: any; Text: any }, pane: { id: string; title: string; spec: PaneSpec; keyPrefix: string; terminal?: number }, later: (work: () => Promise) => void) { +/** + * The Min / Default / Max row of a pane, the pane's current size marked. Seated inline above the prompt (a + * terminal under 110 columns, or not fullscreen), the pane is as tall as the layout spares whatever it asks + * for, so the three would look the same: a line says so in their place. + */ +export async function sizeControls($: EngineInterface, ui: { Box: any; Button: any; Text: any }, pane: { id: string; title: string; spec: PaneSpec; keyPrefix: string; terminal?: number; placement?: string }, later: (work: () => Promise) => void) { const { Box, Button, Text } = ui noteTerminal(pane.terminal) + if (pane.placement === 'inline') { + return ( + + {INLINE_SIZE_NOTE} + + ) + } const current = (await read($, sizeAtom))[pane.id] ?? 'default' return ( diff --git a/plugins/altimate-code/hooks/status-line.ts b/plugins/altimate-code/hooks/status-line.ts index b9d1e4e2..27c744cc 100644 --- a/plugins/altimate-code/hooks/status-line.ts +++ b/plugins/altimate-code/hooks/status-line.ts @@ -22,6 +22,9 @@ export function statusLineOf(current: Record): string | undefine return shown.join(' · ') } +// The parts as last drawn: a /clear empties the session's state, and the line is the process's, so they are put back. +let drawnParts: Record = {} + /** Sets one part (null or empty removes it) and draws the status line again. */ export async function setStatusPart($: EngineInterface, id: StatusPartId, text: string | null) { await update($, parts, prev => { @@ -30,7 +33,13 @@ export async function setStatusPart($: EngineInterface, id: StatusPartId, text: else delete next[id] return next }) - $.ui.status(statusLineOf(await read($, parts))) + drawnParts = await read($, parts) + $.ui.status(statusLineOf(drawnParts)) +} + +/** After a /clear: the parts drawn before it, under any the new conversation has set already. */ +export async function restoreStatusParts($: EngineInterface) { + await update($, parts, prev => ({ ...drawnParts, ...prev })) } /** @@ -38,6 +47,7 @@ export async function setStatusPart($: EngineInterface, id: StatusPartId, text: * set is not drawn again before its owner has set it in this one. */ export async function clearStatusParts($: EngineInterface) { + drawnParts = {} await update($, parts, () => ({})) $.ui.status(undefined) } diff --git a/plugins/altimate-code/hooks/task-impact.test.tsx b/plugins/altimate-code/hooks/task-impact.test.tsx index d72d04f5..07220f64 100644 --- a/plugins/altimate-code/hooks/task-impact.test.tsx +++ b/plugins/altimate-code/hooks/task-impact.test.tsx @@ -919,3 +919,17 @@ test('a column the SQL from before the task did not name but the manifest has (a expect(line).toContain('amount reaches 2 columns in 2 models') expect(line).not.toMatch(/not in the manifest from before the task/) }) + +test('a model the task created, which the manifest from before it lacks, has its columns called new with an unknown reach, not an altimate-code to update', async ($, on) => { + const NEW_MODEL = `${ROOT}/models/marts/region_revenue.sql` + const { turn, logs } = await start($, on, { + answer: args => (args.model === 'region_revenue' ? { status: 'ok', output: 'Model "region_revenue" not found in manifest. Available models: fct_orders, int_orders', metadata: { success: false, truncated: false } } : undefined), + }) + await turn('t1', async () => { + await $.tool.call({ tool: 'Write', file_path: NEW_MODEL, content: 'select\n region,\n sum(revenue) as revenue\nfrom {{ ref("fct_orders") }}\ngroup by 1\n' }) + }) + const line = logs.find(one => one.startsWith('◆ Lineage after this task'))! + expect(line).toContain('region_revenue') + expect(line).toContain('region not in the manifest from before the task (new in it?): its reach is unknown, not zero') + expect(line).not.toContain('update altimate-code') +}) diff --git a/plugins/altimate-code/hooks/task-impact.tsx b/plugins/altimate-code/hooks/task-impact.tsx index 51284221..fdcca9a9 100644 --- a/plugins/altimate-code/hooks/task-impact.tsx +++ b/plugins/altimate-code/hooks/task-impact.tsx @@ -226,11 +226,14 @@ async function traceDown($: EngineInterface, done: Finished): Promise one.hop === 0) ? answer : null - const lacks = typeof (meta as any)?.column_answer_error === 'string' || (!!answer && !down) + // The manifest from before the task has no such model (one the task created): impact_analysis says so with + // success false, and no column answer to give. + const noModel = (meta as any)?.success === false + const lacks = noModel || typeof (meta as any)?.column_answer_error === 'string' || (!!answer && !down) // Only a column the SQL from before the task did not name, which the manifest lacks too, is called new. const unchecked = col.before === null && !!meta && lacks // An altimate-code from before the column answer gives neither the answer nor its error. - const stale = !answer && !!meta && (meta as any).column_answer === undefined && (meta as any).column_answer_error === undefined + const stale = !noModel && !answer && !!meta && (meta as any).column_answer === undefined && (meta as any).column_answer_error === undefined return { model: change.model, column: col.name, down, up: null, modelLevel: modelLevelOf(meta), failed: !down && !stale, stale, unchecked, ...unknownOf(down), consumers: consumersOf(meta) } }) } diff --git a/plugins/altimate-code/types/index.d.ts b/plugins/altimate-code/types/index.d.ts index 3b9c021a..666a8a5b 100644 --- a/plugins/altimate-code/types/index.d.ts +++ b/plugins/altimate-code/types/index.d.ts @@ -592,6 +592,8 @@ export interface DocsCoverageView { /** Output columns without a description. */ missing: string[] hasDescription: boolean + /** Read from a manifest dbt wrote before the model's last edit; the edit's check reads it again. */ + stale: boolean } export interface DocsState { @@ -695,6 +697,8 @@ declare module 'claude-code' { runs: Shaped /** hooks/features/: DeferState. */ defer: DeferState | null + /** hooks/features/: the flags /defer and /target added to Claude's last dbt command, until the next prompt; null with none. */ + deferRan: string | null /** hooks/features/: ToModelState. */ toModel: ToModelState | null /** hooks/features/: CommentsState. */ From 82cff5a50834c164e0ec24b998e6586a51bcfd08 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 20 Nov 2026 00:42:19 +0530 Subject: [PATCH 02/13] =?UTF-8?q?fix:=20review=20round=201=20=E2=80=94=20r?= =?UTF-8?q?eplays=20drop=20--target-path,=20health=20parses=20in=20its=20o?= =?UTF-8?q?wn=20folder,=20coverage=20waits=20for=20its=20own=20check?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Re-run of Claude's dbt show drops a --target-path Claude named, so the replay writes to the plugin's folder. - The health check's parse writes to target/.altimate-plugin/health: it runs outside the plugin's dbt queue, so it never rewrites a check's manifest. - An edit's coverage is answered only by a check that started after it, and is dropped when the check's manifest was rewritten while it was read; the coverage id is resolved from the manifest being read; a wait that times out or a session that ends leaves no waiter behind. - Changes recorded right after /clear are merged into the kept health state. - Two identical rewritten dbt commands each get their flags line. - The doctor test presses Run dbt parse. Co-Authored-By: Claude Opus 5.5 --- plugins/altimate-code/hooks/dbt-mods-core.ts | 7 +- plugins/altimate-code/hooks/dbt-mods.test.tsx | 31 ++++++-- plugins/altimate-code/hooks/dbt-mods.tsx | 44 +++++++---- .../altimate-code/hooks/features/defer.tsx | 23 ++++-- .../hooks/features/docs.test.tsx | 76 ++++++++++++++++++- plugins/altimate-code/hooks/features/docs.tsx | 21 +++-- .../altimate-code/hooks/features/health.tsx | 17 ++++- plugins/altimate-code/hooks/health.test.tsx | 15 +++- 8 files changed, 191 insertions(+), 43 deletions(-) diff --git a/plugins/altimate-code/hooks/dbt-mods-core.ts b/plugins/altimate-code/hooks/dbt-mods-core.ts index 2e982e1a..996c1a90 100644 --- a/plugins/altimate-code/hooks/dbt-mods-core.ts +++ b/plugins/altimate-code/hooks/dbt-mods-core.ts @@ -250,8 +250,9 @@ const CONTEXT_FLAGS: Record = { '--target': '--target', '-t': '- const SELECT_FLAGS: Record = { '--select': '--select', '-s': '--select', '--models': '--select', '-m': '--select', '--exclude': '--exclude', '--selector': '--selector' } // Flags that change how dbt reports or how fast it runs, never what it runs. const DISPLAY_FLAGS = new Set(['--log-format', '--log-format-file', '--log-level', '--log-level-file', '--log-path', '--quiet', '-q', '--debug', '-d', '--no-print', '--print', '--use-colors', '--no-use-colors', '--printer-width', '--threads', '--no-version-check', '--version-check', '--write-json', '--no-write-json', '--send-anonymous-usage-stats', '--no-send-anonymous-usage-stats', '--log-cache-events', '--record-timing-info']) -// Flags a re-run of Claude's dbt show replaces: the pane reads JSON, and keeps the limit itself. -const OUTPUT_FLAGS = new Set(['--output', '-o', '--log-format', '--limit', '--log-level']) +// Flags a re-run of Claude's dbt show replaces: the pane reads JSON, and keeps the limit itself. Its artifact +// folder is the plugin's own (SCRATCH_TARGET), so a folder Claude named is dropped too. +const OUTPUT_FLAGS = new Set(['--output', '-o', '--log-format', '--limit', '--log-level', '--target-path']) const BARE_OUTPUT_FLAGS = new Set(['--quiet', '-q']) /** The last value given to `--flag value` or `--flag=value` in an argv; null when none, or when it holds an expansion the shell would make. */ @@ -479,6 +480,8 @@ export function targetPath(yml: string): string { * model's compiled SQL (and replace run_results.json), so those runs go here and are read from here. */ export const SCRATCH_TARGET = '.altimate-plugin' +/** The health check's own folder inside SCRATCH_TARGET: its parse runs outside the plugin's dbt queue, so it never writes the manifest a check is reading. */ +export const HEALTH_SCRATCH = 'health' /** * The folders a project's files live in, from dbt_project.yml: its model, seed, snapshot, macro, test diff --git a/plugins/altimate-code/hooks/dbt-mods.test.tsx b/plugins/altimate-code/hooks/dbt-mods.test.tsx index d017951f..4f087555 100644 --- a/plugins/altimate-code/hooks/dbt-mods.test.tsx +++ b/plugins/altimate-code/hooks/dbt-mods.test.tsx @@ -3931,11 +3931,32 @@ test('the plugin\'s own dbt runs write their artifacts to its own folder: the pr expect(result.context?.find((one: string) => one.startsWith('Altimate compile-and-validate'))).toBeDefined() }) -test('the doctor\'s dbt parse, which the person asks for to refresh a stale manifest, still writes the project\'s own', async ($, on) => { - const { clock, runs, envs } = await start($, on) - await $.command.run({ command: 'dbt-doctor', args: '' }) +test('the doctor\'s Run dbt parse, which the person presses to refresh a stale manifest, writes the project\'s own manifest', async ($, on) => { + const { clock, runs, envs } = await start($, on, { interactive: true, now: 200_000, mtimes: { 'models/orders.sql': 5_000 } }) + const band = await $.ui.mount({ ...BAND, surface: 'terminal' }) + await band.press({ key: 'dbt-doctor-parse' }) await clock.settle() + await band.unmount() const parse = runs.findIndex(argv => argv[0] === DBT && argv[1] === 'parse') - if (parse >= 0) expect(envs[parse]!.DBT_TARGET_PATH).toBeUndefined() - for (const [i, argv] of runs.entries()) if (argv[0] === DBT && (argv[1] === '--version' || argv[1] === 'debug')) expect(envs[i]!.DBT_TARGET_PATH).toBeUndefined() + expect(parse).toBeGreaterThanOrEqual(0) + expect(envs[parse]!.DBT_TARGET_PATH).toBeUndefined() +}) + +test('Re-run of Claude\'s dbt show drops the artifact folder Claude named: the replay writes to the plugin\'s own', async ($, on) => { + expect(replayArgv(['dbt', 'show', '-s', 'orders', '--target-path', 'target'], 5)).toEqual(['dbt', 'show', '-s', 'orders', '--limit', '5', '--output', 'json', '--log-format', 'json']) + expect(replayArgv(['dbt', 'show', '--target-path=target', '-s', 'orders'], 5)).toEqual(['dbt', 'show', '-s', 'orders', '--limit', '5', '--output', 'json', '--log-format', 'json']) + const { clock, runs, envs, setBash, files } = await start($, on) + const kept = files['target/manifest.json'] + setBash({ stdout: showLog([{ order_id: 1 }], 'orders') }) + await $.tool.call({ tool: 'Bash', command: 'dbt show -s orders --target-path target' }) + await clock.settle() + const ui = await $.ui.mount({ ...RESULTS, surface: 'terminal' }) + await ui.press({ key: 'results-rerun' }) + await clock.settle() + await ui.unmount() + const replay = runs.findIndex(argv => argv[0] === DBT && argv[1] === 'show') + expect(replay).toBeGreaterThanOrEqual(0) + expect(runs[replay]).not.toContain('--target-path') + expect(envs[replay]!.DBT_TARGET_PATH).toBe(`${ROOT}/target/.altimate-plugin`) + expect(files['target/manifest.json']).toBe(kept) }) diff --git a/plugins/altimate-code/hooks/dbt-mods.tsx b/plugins/altimate-code/hooks/dbt-mods.tsx index de742aa3..023204bc 100644 --- a/plugins/altimate-code/hooks/dbt-mods.tsx +++ b/plugins/altimate-code/hooks/dbt-mods.tsx @@ -546,7 +546,7 @@ async function compileAndValidate($: EngineInterface, root: string, file: string const status: CheckView['status'] = errors.length ? 'error' : reason ? 'partial' : drift.missing.length || drift.undocumented.length ? 'warn' : 'ok' const view: CheckView = { ...blank, model: named ?? model, schema: node ? schemaOf(node.node) : blank.schema, status, reason, ran: !errors.length && !nothingShown, errors: errors.map(one => one.slice(0, 1500)), missing: drift.missing, undocumented: drift.undocumented, columns: returned ? returned.length : null, returned: returned ?? [], documented, ms, at: started } await setCheck($, view, newest) - if (current()) checkEnded(`${root}/${rel}`, view.ran && after ? { root, file: `${root}/${rel}`, manifest: `${scratch}/manifest.json` } : null) + if (current()) checkEnded(`${root}/${rel}`, mine, view.ran && after ? { root, file: `${root}/${rel}`, manifest: `${scratch}/manifest.json`, mtimeMs: after.mtimeMs } : null) // An edit of a schema file or macro that came while this ran checks it again now. if (forcedWhileBusy && current() && !rechecking) { const waiting = forcedWhileBusy @@ -558,26 +558,41 @@ async function compileAndValidate($: EngineInterface, root: string, file: string return { view, note: checkNote(view), current, newest } } -/** A check that ran its model: the model's file, and the manifest its dbt show wrote (in the plugin's own folder). */ -export type CheckLanded = { root: string; file: string; manifest: string } -// Who waits for the next check of a file, by the file's normalized path. -const landedWaiters = new Map void)[]>() +/** A check that ran its model: the model's file, and the manifest its dbt show wrote (in the plugin's own folder) with its time. */ +export type CheckLanded = { root: string; file: string; manifest: string; mtimeMs: number } +type Waiter = { after: number; resolve: (landed: CheckLanded | null) => void } +// Who waits for a check of a file, by the file's normalized path, with the last check started when they asked. +const landedWaiters = new Map() /** - * Resolves when the newest check of `file` ends: with its manifest when it compiled and ran the model (that - * manifest holds the model's SQL as it is now, where the project's own may hold it as it was at the last dbt - * compile), else null. Never rejects; a caller bounds the wait itself. + * Resolves when the newest check of `file` that starts after this call ends: with its manifest when it compiled + * and ran the model (that manifest holds the model's SQL as it is now, where the project's own may hold it as it + * was at the last dbt compile), else null. A check already running when this is called is from before the edit, + * and does not answer it. Never rejects; `cancel` stops the wait (the caller's own deadline). */ -export function nextCheckOf(file: string): Promise { +export function nextCheckOf(file: string): { landed: Promise; cancel: () => void } { const key = normalize(file) - return new Promise(resolve => landedWaiters.set(key, [...(landedWaiters.get(key) ?? []), resolve])) + let waiter!: Waiter + const landed = new Promise(resolve => (waiter = { after: seq, resolve })) + landedWaiters.set(key, [...(landedWaiters.get(key) ?? []), waiter]) + const cancel = () => { + const left = (landedWaiters.get(key) ?? []).filter(one => one !== waiter) + if (left.length) landedWaiters.set(key, left) + else landedWaiters.delete(key) + waiter.resolve(null) + } + return { landed, cancel } } -function checkEnded(file: string, landed: CheckLanded | null) { +/** The check numbered `mine` of `file` ended: the waiters that asked before it started get its answer. */ +function checkEnded(file: string, mine: number, landed: CheckLanded | null) { const key = normalize(file) const waiting = landedWaiters.get(key) ?? [] - landedWaiters.delete(key) - for (const resolve of waiting) resolve(landed) + const answered = waiting.filter(one => one.after < mine) + const left = waiting.filter(one => one.after >= mine) + if (left.length) landedWaiters.set(key, left) + else landedWaiters.delete(key) + for (const one of answered) one.resolve(landed) } async function setCheck($: EngineInterface, view: CheckView, current: () => boolean) { @@ -1759,6 +1774,9 @@ export async function dbtModsReadOrEditAfter($: EngineInterface, e: { tool: stri */ export async function endDbtMods($: EngineInterface) { generation++ + // A check of the conversation that ended answers nobody in the next. + for (const waiting of landedWaiters.values()) for (const one of waiting) one.resolve(null) + landedWaiters.clear() forgetRedactions() slices.clear() parsed = null diff --git a/plugins/altimate-code/hooks/features/defer.tsx b/plugins/altimate-code/hooks/features/defer.tsx index ef7db2e8..f2a92194 100644 --- a/plugins/altimate-code/hooks/features/defer.tsx +++ b/plugins/altimate-code/hooks/features/defer.tsx @@ -18,8 +18,10 @@ import type { BandUi, FeatureContext } from './index' const deferAtom = atom({ plugin: 'altimate-code', key: 'defer' } as const, null as DeferState | null) // What /defer and /target added to Claude's last dbt command, for the band, until the next prompt. const ranAtom = atom({ plugin: 'altimate-code', key: 'deferRan' } as const, null as string | null) -// The flags added to each rewritten command, by the command as it ran (the after phase sees the rewrite), for its result. -const addedTo = new Map() +// The flags added to each rewritten command, by the command as it ran (the after phase sees the rewrite), for its +// result: one entry per call, oldest first, so two calls of the same command each get theirs. +const addedTo = new Map() +let addedCount = 0 // The commands that take `--defer` in every dbt version this reads state for (older dbt refuses the option on // `ls`, `parse`, `seed`, `run-operation` and others, and `retry` reads the previous run's results from `--state`). @@ -434,6 +436,7 @@ export async function sessionStart($: EngineInterface, e: Args<'session.start'>) generation++ loading = null addedTo.clear() + addedCount = 0 await update($, deferAtom, () => null) await update($, ranAtom, () => null) // Clearing the line is cosmetic: an engine that cannot draw it does not stop the session. @@ -477,9 +480,13 @@ export async function bashRewrite($: EngineInterface, e: BashCall): Promise= 20) addedTo.delete(addedTo.keys().next().value!) - addedTo.set(command, added) + // Kept for the call's after phase, which tells Claude what ran; calls that never get there leave at most a few. + if (addedCount >= 20) { + addedTo.clear() + addedCount = 0 + } + addedTo.set(command, [...(addedTo.get(command) ?? []), added]) + addedCount++ return command } @@ -496,9 +503,11 @@ export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: * told in the result, and the person in the band, what really ran. */ export async function bashAfter($: EngineInterface, e: BashCall, result: ToolCallResult): Promise { - const added = addedTo.get(e.command) + const queued = addedTo.get(e.command) + const added = queued?.shift() if (!added) return - addedTo.delete(e.command) + if (!queued!.length) addedTo.delete(e.command) + addedCount-- const text = added.join('; ') await update($, ranAtom, () => text) return [`◆ dbt · this command ran as ${text}: /defer and /target added those flags (the command shown is as written), so its results are from that target and state`] diff --git a/plugins/altimate-code/hooks/features/docs.test.tsx b/plugins/altimate-code/hooks/features/docs.test.tsx index f40791ae..b819b2c0 100644 --- a/plugins/altimate-code/hooks/features/docs.test.tsx +++ b/plugins/altimate-code/hooks/features/docs.test.tsx @@ -213,7 +213,20 @@ type Options = { /** A model file written after the project's manifest: dbt has not run since the edit. */ editNewer?: boolean /** The edit's check (the dbt helpers): what its dbt show previews, the manifest it writes, and the coverage read from that manifest. */ - check?: { rows: Record[]; node: string; manifest: Record; coverage: Record } + check?: { + rows: Record[] + node: string + manifest: Record + coverage: Record + /** The coverage read from the check's manifest, by how many dbt shows had finished when it was read. */ + coverageAfter?: (shows: number) => Record + /** The first dbt show waits for this. */ + holdFirst?: Promise + /** The coverage read from the project's manifest for the second edit waits for this. */ + holdSecondEdit?: Promise + /** The check's manifest is rewritten (by another plugin run) while its coverage is read. */ + rewrittenWhileRead?: boolean + } } /** A dbt project at /repo with the plugin's MCP server connected, and the session beneath the plugin. */ @@ -248,7 +261,8 @@ async function start($: any, on: any, options: Options = {}) { return { text: e.text } }) on('fs.exists', (_: unknown, e: { path: string }) => ({ value: e.path === `${ROOT}/dbt_project.yml` || ((e.path === SCHEMA && !options.newFile) || e.path === DOWNSTREAM) || (!options.noManifest && e.path === MANIFEST_PATH) })) - on('fs.stat', (_: unknown, e: { path: string }) => ({ value: { kind: 'file', size: 100, mtimeMs: options.editNewer && e.path.endsWith('.sql') ? 2 : 1, isLink: false } })) + let checkManifestAt = 1 + on('fs.stat', (_: unknown, e: { path: string }) => ({ value: { kind: 'file', size: 100, mtimeMs: options.editNewer && e.path.endsWith('.sql') ? 2 : e.path.includes('.altimate-plugin') ? checkManifestAt : 1, isLink: false } })) on('fs.read', (_: unknown, e: { path: string }) => { if (e.path === `${ROOT}/dbt_project.yml`) return { value: "name: 'shop'\n" } if (e.path === MANIFEST_PATH && options.manifest) return { value: JSON.stringify({ nodes: options.manifest }) } @@ -274,10 +288,18 @@ async function start($: any, on: any, options: Options = {}) { if (e.tool === 'impact_analysis') return { value: { content: [{ type: 'text', text: JSON.stringify({ ...(options.reach ?? REACH), report: 'Impact' }) }], isError: false } } return { value: { content: [], isError: true } } }) + let shows = 0 + let plainReads = 0 + const previewOf = () => ({ data: { is_inline: false, node_name: options.check!.node, output_format: 'json', preview: JSON.stringify(options.check!.rows), unique_id: `model.shop.${options.check!.node}` }, info: { code: 'Q041', level: 'info', msg: '…' } }) on('process.run', (_: unknown, e: { argv: readonly string[] }) => { const [, command, , tool] = e.argv // The edit's check: dbt show previews the model as the edit left it. if (e.argv[0] === 'dbt' && command === 'show' && options.check) { + if (shows === 0 && options.check.holdFirst) { + shows++ + return options.check.holdFirst.then(() => ({ value: { exitCode: 0, stdout: JSON.stringify(previewOf()), stderr: '' } })) + } + shows++ const preview = { data: { is_inline: false, node_name: options.check.node, output_format: 'json', preview: JSON.stringify(options.check.rows), unique_id: `model.shop.${options.check.node}` }, info: { code: 'Q041', level: 'info', msg: '…' } } return { value: { exitCode: 0, stdout: JSON.stringify(preview), stderr: '' } } } @@ -289,7 +311,13 @@ async function start($: any, on: any, options: Options = {}) { if (tool === 'dbt_docs_coverage') { if (options.coverageError) return { value: { exitCode: 1, stdout: JSON.stringify({ status: 'error', output: options.coverageError, metadata: { success: false } }), stderr: '' } } const fromCheck = options.check && String(e.argv[5]).includes('.altimate-plugin') - const body = options.coverage === null ? { status: 'error', output: 'Unknown tool: dbt_docs_coverage', metadata: {} } : { status: 'ok', output: 'coverage', metadata: fromCheck ? options.check!.coverage : options.coverage } + if (!fromCheck && options.check?.holdSecondEdit && ++plainReads === 2) { + const answer = { value: { exitCode: 0, stdout: JSON.stringify({ status: 'ok', output: 'coverage', metadata: options.coverage }), stderr: '' } } + return options.check.holdSecondEdit.then(() => answer) + } + if (fromCheck && options.check!.rewrittenWhileRead) checkManifestAt++ + const checked = fromCheck ? (options.check!.coverageAfter?.(shows) ?? options.check!.coverage) : null + const body = options.coverage === null ? { status: 'error', output: 'Unknown tool: dbt_docs_coverage', metadata: {} } : { status: 'ok', output: 'coverage', metadata: checked ?? options.coverage } return { value: { exitCode: options.coverage === null ? 1 : 0, stdout: JSON.stringify(body), stderr: '' } } } return { value: { exitCode: 0, stdout: JSON.stringify({ status: 'ok', output: '', metadata: options.reach ?? REACH }), stderr: '' } } @@ -662,3 +690,45 @@ kitTest('a SQL edit dbt has not compiled yet: the coverage says it is from befor expect(await ui.find({ type: 'Text', text: /2 of 2 columns documented/ })).toBeUndefined() await ui.unmount() }) + +kitTest('the coverage of an edit is read only from its own check: a check still running from an earlier edit does not answer it', { options: { dbt_mods: true } }, async ($, on) => { + const node = { 'model.shop.orders': { resource_type: 'model', name: 'orders', package_name: 'shop', original_file_path: 'models/marts/orders.sql', columns: {} } } + const before = { success: true, model: 'orders', has_description: true, output_columns: ['order_id'], documented: ['order_id'], missing: [], percent: 100 } + let release!: () => void + const holdFirst = new Promise(resolve => (release = resolve)) + let releaseB!: () => void + const holdSecondEdit = new Promise(resolve => (releaseB = resolve)) + // The coverage read from the check's manifest says which dbt show it followed. + const coverageAfter = (shows: number) => ({ success: true, model: 'orders', has_description: true, output_columns: ['order_id', `col_${shows}`], documented: ['order_id'], missing: [`col_${shows}`], percent: 50 }) + const { clock } = await start($, on, { editNewer: true, manifest: node, coverage: before, check: { rows: [{ order_id: 1 }], node: 'orders', manifest: node, coverage: before, coverageAfter, holdFirst, holdSecondEdit } }) + const file = `${ROOT}/models/marts/orders.sql` + // Edit A's check is held; edit B comes while it runs, and A's check ends before B's own check has started. + const a = $.tool.call({ tool: 'Edit', file_path: file, old_string: 'a', new_string: 'b' }) + await clock.advance(10) + const b = $.tool.call({ tool: 'Edit', file_path: file, old_string: 'b', new_string: 'c' }) + await clock.advance(10) + release() + await a + await clock.advance(10) + releaseB() + await b + await clock.settle() + const ui = await $.ui.mount({ ...COVERAGE_PANE, surface: 'terminal' }) + // B's coverage comes from the second show (B's own check), never the first (A's). + expect(await ui.find({ type: 'Text', text: /no description: col_2/ })).toBeDefined() + expect(await ui.find({ type: 'Text', text: /col_1/ })).toBeUndefined() + await ui.unmount() +}) + +kitTest('a check manifest another plugin run rewrote while its coverage was read is not used: the coverage stays marked as from before the edit', { options: { dbt_mods: true } }, async ($, on) => { + const node = { 'model.shop.orders': { resource_type: 'model', name: 'orders', package_name: 'shop', original_file_path: 'models/marts/orders.sql', columns: {} } } + const before = { success: true, model: 'orders', has_description: true, output_columns: ['order_id'], documented: ['order_id'], missing: [], percent: 100 } + const after = { success: true, model: 'orders', has_description: true, output_columns: ['order_id', 'payments'], documented: ['order_id'], missing: ['payments'], percent: 50 } + const { clock } = await start($, on, { editNewer: true, manifest: node, coverage: before, check: { rows: [{ order_id: 1 }], node: 'orders', manifest: node, coverage: after, rewrittenWhileRead: true } }) + await $.tool.call({ tool: 'Edit', file_path: `${ROOT}/models/marts/orders.sql`, old_string: 'a', new_string: 'b' }) + await clock.settle() + const ui = await $.ui.mount({ ...COVERAGE_PANE, surface: 'terminal' }) + expect(await ui.find({ type: 'Text', text: /from the manifest of before the last edit/ })).toBeDefined() + expect(await ui.find({ type: 'Text', text: /no description: payments/ })).toBeUndefined() + await ui.unmount() +}) diff --git a/plugins/altimate-code/hooks/features/docs.tsx b/plugins/altimate-code/hooks/features/docs.tsx index 63971348..062ea28c 100644 --- a/plugins/altimate-code/hooks/features/docs.tsx +++ b/plugins/altimate-code/hooks/features/docs.tsx @@ -10,7 +10,7 @@ import type { Args, EngineInterface, EventResult, On, RenderElement, ToolCallRes import type { DocsCandidate, DocsCoverageView, DocsGroup, DocsState } from '../../types' import { later, runTool, within } from '../altimate-cli' -import { nextCheckOf } from '../dbt-mods' +import { nextCheckOf, type CheckLanded } from '../dbt-mods' import { setStatusPart } from '../status-line' import type { BashCall, ContextLines, EditedFile, FileCall } from '../tool-chain' import type { BandUi, FeatureContext } from './index' @@ -351,11 +351,11 @@ type ManifestModel = { resource_type?: string; package_name?: string; name?: str * versioned models its latest version stands for them. A model the manifest does not list, or a manifest * that cannot be read, gets the id dbt would give it, which finds nothing rather than another package's model. */ -async function modelRef($: EngineInterface, project: Project, model: string, byFile = false): Promise { +async function modelRef($: EngineInterface, project: Project, model: string, byFile = false, manifest = project.manifest): Promise { if (!project.package) return model const built = `model.${project.package}.${model}` try { - const nodes = (JSON.parse((await $.fs.read(project.manifest)) ?? '') as { nodes?: Record }).nodes ?? {} + const nodes = (JSON.parse((await $.fs.read(manifest)) ?? '') as { nodes?: Record }).nodes ?? {} const own = Object.entries(nodes).filter(([, node]) => node?.resource_type === 'model' && node.package_name === project.package) const stem = (node: ManifestModel) => (node.original_file_path ?? '').replace(/\.sql$/i, '').split('/').pop() if (byFile) { @@ -485,7 +485,7 @@ async function schemaEdited($: EngineInterface, file: EditedFile, created: boole /** The model's documentation coverage as `manifest` has it; a line for Claude when the tool is missing; null when it could not be read. */ async function coverageOf($: EngineInterface, project: Project, model: string, manifest: string, until: number): Promise<{ view: DocsCoverageView; star: boolean } | { line: string } | null> { - const result = await runTool($, 'dbt_docs_coverage', { model: await modelRef($, project, model, true), manifest_path: manifest }, { until, cliOnly: true }).catch(() => null) + const result = await runTool($, 'dbt_docs_coverage', { model: await modelRef($, project, model, true, manifest), manifest_path: manifest }, { until, cliOnly: true }).catch(() => null) if (!result) return null const meta = result.metadata if (!result.ok || !meta || meta.success !== true) { @@ -527,8 +527,8 @@ async function sqlEdited($: EngineInterface, file: EditedFile): Promise coverageAfterCheck($, asked, check)) + const { landed, cancel } = nextCheckOf(file.path) + later($, () => coverageAfterCheck($, asked, landed, cancel)) } const read = await coverageOf($, project, model, project.manifest, (await $.clock.now()) + ROUND_MS) if (!read) return [] @@ -548,10 +548,15 @@ async function sqlEdited($: EngineInterface, file: EditedFile): Promise) { +async function coverageAfterCheck($: EngineInterface, asked: { file: string; model: string; project: Project }, check: Promise, cancel: () => void) { const landed = await within($, CHECK_WAIT_MS, check) - if (!landed || waiting !== asked) return + // A wait that ran out leaves nothing behind. + if (!landed) return cancel() + if (waiting !== asked) return const read = await coverageOf($, asked.project, asked.model, landed.manifest, (await $.clock.now()) + RECHECK_MS) + // The plugin's next dbt run rewrites that manifest: an answer read from another run's is not this edit's. + const now = await $.fs.stat(landed.manifest).catch(() => null) + if (now?.mtimeMs !== landed.mtimeMs) return // A newer edit asks for its own coverage: this one is no longer shown. if (waiting !== asked || !read || 'line' in read) return waiting = null diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index e38ea1c4..adba45fe 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -15,7 +15,7 @@ import type { Args, EngineInterface, EventResult, On, PluginOptions, RenderEleme import type { HealthInsight, HealthState } from '../../types' import { NOT_FOUND, later, runTool, within } from '../altimate-cli' import { setStatusPart } from '../status-line' -import { SCRATCH_TARGET, targetPath } from '../dbt-mods-core' +import { HEALTH_SCRATCH, SCRATCH_TARGET, targetPath } from '../dbt-mods-core' import type { BashCall, ContextLines, EditedFile, FileCall } from '../tool-chain' import type { BandUi, FeatureContext } from './index' @@ -488,7 +488,7 @@ async function checkEnv($: EngineInterface): Promise | un if (!project) return undefined const path = targetPath(await $.fs.read(`${project}/dbt_project.yml`).catch(() => '')) const target = path.startsWith('/') ? path : `${project}/${path}` - const scratch = `${target}/${SCRATCH_TARGET}` + const scratch = `${target}/${SCRATCH_TARGET}/${HEALTH_SCRATCH}` if (await $.fs.exists(`${target}/catalog.json`).catch(() => false)) { // Best effort: without the copy the check reads no catalog, as with a project that has none. await $.process.run(['mkdir', '-p', scratch], { timeoutMs: 10_000 }).catch(() => null) @@ -714,7 +714,15 @@ export async function clearStart($: EngineInterface) { return } enabled = true - await update($, healthAtom, () => ({ ...kept, running: false })) + // Changes the new conversation recorded before this ran are added to the kept ones, not replaced by them. + await update($, healthAtom, prev => ({ + ...kept, + running: false, + pending: [...new Set([...kept.pending, ...(prev?.pending ?? [])])], + touched: [...new Set([...kept.touched, ...(prev?.touched ?? [])])], + // A new change asks for a check again after a failed one, as markChanged does. + ...(prev?.pending.length ? { problem: null } : {}), + })) drawn = true await drawStatus($, kept.issues) if (await wantsCheck($)) later($, () => kick($)) @@ -735,7 +743,8 @@ export async function sessionEnd($: EngineInterface, e: Args<'session.end'>) { carried = state ? { ...state, pending: state.running ? [...new Set([...inFlight, ...state.pending])] : state.pending, running: false } : null generation += 1 running = null - enabled = false + // Kept on through a /clear that carries the state: a change made before clearStart restores it is still recorded, and merged. + if (!carried) enabled = false } /** This feature's line in the band, under the lineage line; null when there is nothing to say. */ diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index a1b7edf8..3fcf3b7c 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -213,7 +213,20 @@ test('after /clear with no baseline yet (the first check failed), /health in the test('the check\'s parse writes to the plugin\'s own artifact folder, in a CLI process of its own: the project\'s manifest keeps its compiled SQL', async ($, on) => { const { calls } = await start($, on, { runs: [[OLD_STG]] }) expect(calls).toHaveLength(1) - expect(calls[0]!.env).toEqual({ DBT_TARGET_PATH: `${ROOT}/target/.altimate-plugin`, ALTIMATE_TOOL_DAEMON: '0' }) + expect(calls[0]!.env).toEqual({ DBT_TARGET_PATH: `${ROOT}/target/.altimate-plugin/health`, ALTIMATE_TOOL_DAEMON: '0' }) +}) + +test('a change made right after /clear, before the kept state is put back, is merged into it and checked', async ($, on) => { + const clear = playsClear($, on) + const { clock, calls } = await start($, on, { runs: [[OLD_STG, OLD_FCT], [OLD_STG, OLD_FCT, NEW_FCT]] }) + await clear() + // The new conversation's first edit lands before the kept state is restored. + await $.tool.call(edit(FCT)) + await clock.settle() + await endTurn($, clock, 't1') + expect(calls).toHaveLength(2) + const shown: any = await $.command.run({ command: 'health' } as any) + expect(shown.text).toContain('1 new health issue · fct_orders: model fanout') }) test('two changed models give "2 new health issues" with the first model named', async ($, on) => { From bddefe584f4fe295dfe34337ae77a97d2c4f4696 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 20 Nov 2026 01:04:26 +0530 Subject: [PATCH 03/13] =?UTF-8?q?fix:=20review=20round=202=20=E2=80=94=20c?= =?UTF-8?q?heck=20manifest=20read=20inside=20the=20dbt=20queue,=20/clear?= =?UTF-8?q?=20restore=20before=20a=20push,=20coverage=20writes=20in=20edit?= =?UTF-8?q?=20order?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - A check reads the manifest its dbt show wrote while the plugin's dbt queue is still held, so a queued run cannot replace it first. - Health's /clear restore runs once, at the session id's change or the new conversation's first push gate, turn end or /health, whichever is first; a second /clear before it keeps what the first carried. - An edit's first coverage read that lands late does not replace newer coverage; a read running at session end writes nothing. - The status line parts are kept at /clear, apart from later updates. - Defer flag notices are keyed by tool call and let go when a call is refused. - The health check removes its catalog copy before each run. Co-Authored-By: Claude Opus 5.5 --- .../hooks/clear-state.testkit.ts | 6 ++- plugins/altimate-code/hooks/dbt-mods.test.tsx | 29 ++++++++++++- plugins/altimate-code/hooks/dbt-mods.tsx | 24 +++++++---- .../hooks/features/defer.test.ts | 10 +++++ .../altimate-code/hooks/features/defer.tsx | 29 ++++++------- .../hooks/features/docs.test.tsx | 28 +++++++++++++ plugins/altimate-code/hooks/features/docs.tsx | 16 ++++++-- .../altimate-code/hooks/features/health.tsx | 41 ++++++++++++++----- plugins/altimate-code/hooks/features/index.ts | 9 +++- plugins/altimate-code/hooks/health.test.tsx | 25 +++++++++++ plugins/altimate-code/hooks/status-line.ts | 16 +++++++- 11 files changed, 189 insertions(+), 44 deletions(-) diff --git a/plugins/altimate-code/hooks/clear-state.testkit.ts b/plugins/altimate-code/hooks/clear-state.testkit.ts index f3c53b4c..e488544d 100644 --- a/plugins/altimate-code/hooks/clear-state.testkit.ts +++ b/plugins/altimate-code/hooks/clear-state.testkit.ts @@ -21,9 +21,11 @@ export function playsClear($: any, on: any): () => Promise { if (cleared) written.add(name(e)) return next(e) }) - on('session.id', () => ({ value: cleared ? 'after-clear' : 'before-clear' })) + let clears = 0 + on('session.id', () => ({ value: `session-${clears}` })) return async () => { - await $.session.end({ reason: 'clear', sessionId: 'before-clear' }) + await $.session.end({ reason: 'clear', sessionId: `session-${clears}` }) + clears++ cleared = true written.clear() } diff --git a/plugins/altimate-code/hooks/dbt-mods.test.tsx b/plugins/altimate-code/hooks/dbt-mods.test.tsx index 4f087555..c5ba3339 100644 --- a/plugins/altimate-code/hooks/dbt-mods.test.tsx +++ b/plugins/altimate-code/hooks/dbt-mods.test.tsx @@ -224,6 +224,8 @@ async function start($: any, on: any, options: Options = {}) { const clock = mock.clock(on, { now: options.now ?? 1_000 }) const runs: string[][] = [] const envs: Record[] = [] + // dbt processes and manifest reads, in the order they happened. + const order: string[] = [] const cwds: string[] = [] const toasts: string[] = [] const prompts: string[] = [] @@ -333,6 +335,7 @@ async function start($: any, on: any, options: Options = {}) { on('fs.read', (_: unknown, e: { path: string }) => { if (options.pathDbtPython && e.path === '/x/bin/dbt') return { value: `#!${options.pathDbtPython}\nimport sys\n` } const rel = e.path.slice(ROOT.length + 1) + if (rel.endsWith('manifest.json')) order.push(`read ${rel}`) if (!(rel in files)) throw new Error('ENOENT') if (rel === 'target/run_results.json' && (options.runResultsSize ?? 0) > 4 * 1024 * 1024) throw new Error('file is larger than 4 MiB') return { value: files[rel] } @@ -353,6 +356,7 @@ async function start($: any, on: any, options: Options = {}) { on('process.run', async (_: unknown, e: { argv: readonly string[]; init?: { cwd?: string; stdin?: string; env?: Record } }) => { runs.push([...e.argv]) envs.push(e.init?.env ?? {}) + if (e.argv[0] === DBT) order.push(`dbt ${e.argv[1]}`) // Where this dbt writes its artifacts: DBT_TARGET_PATH when set, else the project's target folder. const given = e.init?.env?.DBT_TARGET_PATH const out = given ? (given.startsWith(`${ROOT}/`) ? given.slice(ROOT.length + 1) : given) : (options.target ?? 'target') @@ -442,7 +446,7 @@ async function start($: any, on: any, options: Options = {}) { }) await $.session.start({ cwd: ROOT, surface: 'terminal', isInteractive: options.interactive ?? false }) await clock.settle() - return { redactSent, clock, runs, envs, cwds, toasts, prompts, store, registered, storeGets, mtimes, opened, sizes, files, setBash: (next: typeof bash) => void (bash = next) } + return { redactSent, clock, runs, envs, order, cwds, toasts, prompts, store, registered, storeGets, mtimes, opened, sizes, files, setBash: (next: typeof bash) => void (bash = next) } } // ─── What the helpers read from dbt ───────────────────────────────────────────────────────────── @@ -3960,3 +3964,26 @@ test('Re-run of Claude\'s dbt show drops the artifact folder Claude named: the r expect(envs[replay]!.DBT_TARGET_PATH).toBe(`${ROOT}/target/.altimate-plugin`) expect(files['target/manifest.json']).toBe(kept) }) + +test('a check reads the manifest its dbt show wrote before the plugin\'s next queued dbt run can write over it', async ($, on) => { + let release!: () => void + let readable!: () => void + // The check's read of its manifest is slow: the queued run would start meanwhile if the queue were not held. + const statGate = { path: 'target/.altimate-plugin/manifest.json', until: new Promise(resolve => (readable = resolve)) } + const { clock, order } = await start($, on, { holdShow: new Promise(resolve => (release = resolve)), statGate }) + const edit = $.tool.call({ tool: 'Edit', file_path: `${ROOT}/models/orders.sql`, old_string: 'a', new_string: 'b' }) + await clock.advance(10) + // A /dbt-show queued behind the check's own show. + const shown = $.command.run({ command: 'dbt-show', args: 'orders' }) + await clock.advance(10) + release() + for (let i = 0; i < 5; i++) await clock.advance(10) + readable() + await Promise.all([edit, shown]) + await clock.settle() + const shows = order.map((one, i) => (one === 'dbt show' ? i : -1)).filter(i => i >= 0) + expect(shows.length).toBeGreaterThanOrEqual(2) + const read = order.findIndex((one, i) => i > shows[0]! && one === 'read target/.altimate-plugin/manifest.json') + expect(read).toBeGreaterThan(shows[0]!) + expect(read).toBeLessThan(shows[1]!) +}) diff --git a/plugins/altimate-code/hooks/dbt-mods.tsx b/plugins/altimate-code/hooks/dbt-mods.tsx index f86f1cf5..c838bd8e 100644 --- a/plugins/altimate-code/hooks/dbt-mods.tsx +++ b/plugins/altimate-code/hooks/dbt-mods.tsx @@ -187,6 +187,10 @@ async function dbtBinary($: EngineInterface, root: string): Promise { return 'dbt' } +// A run's process result, with what `read` gave of its artifacts while the queue was held (null when that failed). +type Artifact = { manifest: any; mtimeMs: number } | null +type ProcessDone = { exitCode: number; stdout: string; stderr: string; isStdoutTruncated?: boolean; artifact?: Artifact } + // What a run that was no longer wanted when its turn came answers: it never started. const SKIPPED = { exitCode: -2, stdout: '', stderr: 'skipped: a newer check replaced it' } @@ -194,7 +198,7 @@ const SKIPPED = { exitCode: -2, stdout: '', stderr: 'skipped: a newer check repl * Runs a command in a project, after any dbt this plugin already started there. `wanted` is asked * when its turn comes: a check a newer edit replaced runs no warehouse query. */ -function runQueued($: EngineInterface, root: string, argv: () => Promise, options: { cwd?: string; timeoutMs?: number; wanted?: () => boolean; scratch?: boolean } = {}) { +function runQueued($: EngineInterface, root: string, argv: () => Promise, options: { cwd?: string; timeoutMs?: number; wanted?: () => boolean; scratch?: boolean; read?: () => Promise } = {}): Promise { const before = queues.get(root) ?? Promise.resolve() const run = before .catch(() => {}) @@ -218,7 +222,8 @@ function runQueued($: EngineInterface, root: string, argv: () => Promise null) } : done }) queues.set(root, run.catch(() => {})) return run @@ -257,8 +262,8 @@ function cannotRun(run: { exitCode: number; stdout: string; stderr: string }): b } /** Runs the project's dbt in the project, after any dbt this plugin already started there. */ -function runDbt($: EngineInterface, root: string, args: string[], timeoutMs = DBT_TIMEOUT_MS, wanted?: () => boolean, scratch = false) { - return runQueued($, root, async () => [await dbtBinary($, root), ...args], { timeoutMs, wanted, scratch }).then( +function runDbt($: EngineInterface, root: string, args: string[], timeoutMs = DBT_TIMEOUT_MS, wanted?: () => boolean, scratch = false, read?: () => Promise) { + return runQueued($, root, async () => [await dbtBinary($, root), ...args], { timeoutMs, wanted, scratch, read }).then( done => { if (done !== SKIPPED) { if (cannotRun(done)) unrunnable.add(root) @@ -480,8 +485,10 @@ async function compileAndValidate($: EngineInterface, root: string, file: string // name (`defined_in`) is still previewed, and the path keeps it to that one version. const pkg = await projectName($, root) const selectorFor = (name: string | null) => [`path:${rel}`, ...(pkg ? [`package:${pkg}`] : []), ...(name ? [name] : [])].join(',') - const run1 = (args: string[]) => runDbt($, root, args, DBT_TIMEOUT_MS, current, true).catch(error => ({ exitCode: -1, stdout: '', stderr: String(error) })) - const show = (select: string) => run1(['show', '--select', select, '--indirect-selection', 'empty', '--limit', '1', '--output', 'json', '--log-format', 'json']) + const scratch = await scratchOf($, root) + const run1 = (args: string[], read?: () => Promise): Promise => runDbt($, root, args, DBT_TIMEOUT_MS, current, true, read).catch(error => ({ exitCode: -1, stdout: '', stderr: String(error) })) + // The manifest the show wrote is read while the plugin's dbt queue is still held, so no other run of it replaces it first. + const show = (select: string) => run1(['show', '--select', select, '--indirect-selection', 'empty', '--limit', '1', '--output', 'json', '--log-format', 'json'], () => readManifest($, root, rel, scratch)) // A newer edit replaced this check before its turn: nothing ran, nothing to report. const replaced = () => notChecked('a newer check replaced this one').then(one => ({ ...one, note: '' })) const knownName = typeof known?.node?.name === 'string' ? known.node.name : null @@ -500,8 +507,7 @@ async function compileAndValidate($: EngineInterface, root: string, file: string if (run === SKIPPED) return replaced() if (cannotRun(run)) return notChecked(NO_DBT) // dbt show re-parsed the project: the manifest it wrote (in the plugin's own folder) holds the model and schema.yml as they are. - const scratch = await scratchOf($, root) - let after = await readManifest($, root, rel, scratch) + let after = run.artifact ?? null let node = after ? nodeForFile(after.manifest, rel, await projectName($, root)) : null let named = typeof node?.node?.name === 'string' ? node.node.name : null let preview = run.exitCode === 0 ? showPreview(run.stdout, node?.id ?? known?.id) : null @@ -512,7 +518,7 @@ async function compileAndValidate($: EngineInterface, root: string, file: string const again = await show(selectorFor(named)) if (again === SKIPPED) return replaced() run = again - after = await readManifest($, root, rel, scratch) + after = run.artifact ?? null node = after ? nodeForFile(after.manifest, rel, await projectName($, root)) : null named = typeof node?.node?.name === 'string' ? node.node.name : null preview = run.exitCode === 0 ? showPreview(run.stdout, node?.id ?? known?.id) : null diff --git a/plugins/altimate-code/hooks/features/defer.test.ts b/plugins/altimate-code/hooks/features/defer.test.ts index 687b9199..b8eace85 100644 --- a/plugins/altimate-code/hooks/features/defer.test.ts +++ b/plugins/altimate-code/hooks/features/defer.test.ts @@ -446,3 +446,13 @@ test('Claude is told in the result, and the person in the band until the next pr expect(await after.find({ type: 'Text', text: /ran as/ })).toBeUndefined() await after.unmount() }) + +test('each call gets the flags added to it: a concurrent call that wrote the same flags itself takes none of them', async ($, on) => { + start($, on, DEFER) + await $.session.start({ cwd: ROOT, surface: 'terminal', isInteractive: true }) + // Claude's own command already names the flags the rewrite would add, so it is not rewritten. + const own: any = await $.tool.call({ tool: 'Bash', command: 'dbt build --defer --state /work/prod-state --favor-state -s x', tool_use_id: 'own' }) + expect((own.context ?? []).some((line: string) => line.startsWith('◆ dbt · this command ran as'))).toBe(false) + const ran: any = await $.tool.call({ tool: 'Bash', command: 'dbt build -s x', tool_use_id: 'rewritten' }) + expect(ran.context).toContain('◆ dbt · this command ran as dbt build --defer --state /work/prod-state --favor-state: /defer and /target added those flags (the command shown is as written), so its results are from that target and state') +}) diff --git a/plugins/altimate-code/hooks/features/defer.tsx b/plugins/altimate-code/hooks/features/defer.tsx index f2a92194..a872e919 100644 --- a/plugins/altimate-code/hooks/features/defer.tsx +++ b/plugins/altimate-code/hooks/features/defer.tsx @@ -18,10 +18,9 @@ import type { BandUi, FeatureContext } from './index' const deferAtom = atom({ plugin: 'altimate-code', key: 'defer' } as const, null as DeferState | null) // What /defer and /target added to Claude's last dbt command, for the band, until the next prompt. const ranAtom = atom({ plugin: 'altimate-code', key: 'deferRan' } as const, null as string | null) -// The flags added to each rewritten command, by the command as it ran (the after phase sees the rewrite), for its -// result: one entry per call, oldest first, so two calls of the same command each get theirs. -const addedTo = new Map() -let addedCount = 0 +// The flags added to each rewritten call, by the call's tool_use_id (the command as it ran when there is none), for its result. +const addedTo = new Map() +const callKey = (e: BashCall) => (typeof e.tool_use_id === 'string' && e.tool_use_id ? e.tool_use_id : `command:${e.command}`) // The commands that take `--defer` in every dbt version this reads state for (older dbt refuses the option on // `ls`, `parse`, `seed`, `run-operation` and others, and `retry` reads the previous run's results from `--state`). @@ -436,7 +435,6 @@ export async function sessionStart($: EngineInterface, e: Args<'session.start'>) generation++ loading = null addedTo.clear() - addedCount = 0 await update($, deferAtom, () => null) await update($, ranAtom, () => null) // Clearing the line is cosmetic: an engine that cannot draw it does not stop the session. @@ -480,13 +478,9 @@ export async function bashRewrite($: EngineInterface, e: BashCall): Promise= 20) { - addedTo.clear() - addedCount = 0 - } - addedTo.set(command, [...(addedTo.get(command) ?? []), added]) - addedCount++ + // Kept for the call's after phase, which tells Claude what ran (a refused call lets go in bashDropped); at most a few are held. + if (addedTo.size >= 20) addedTo.delete(addedTo.keys().next().value!) + addedTo.set(callKey({ ...e, command }), added) return command } @@ -503,16 +497,19 @@ export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: * told in the result, and the person in the band, what really ran. */ export async function bashAfter($: EngineInterface, e: BashCall, result: ToolCallResult): Promise { - const queued = addedTo.get(e.command) - const added = queued?.shift() + const added = addedTo.get(callKey(e)) if (!added) return - if (!queued!.length) addedTo.delete(e.command) - addedCount-- + addedTo.delete(callKey(e)) const text = added.join('; ') await update($, ranAtom, () => text) return [`◆ dbt · this command ran as ${text}: /defer and /target added those flags (the command shown is as written), so its results are from that target and state`] } +/** A call a gate refused never reaches bashAfter: its flags are let go. */ +export async function bashDropped($: EngineInterface, e: BashCall): Promise { + addedTo.delete(callKey(e)) +} + /** `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 {} diff --git a/plugins/altimate-code/hooks/features/docs.test.tsx b/plugins/altimate-code/hooks/features/docs.test.tsx index b819b2c0..26212f80 100644 --- a/plugins/altimate-code/hooks/features/docs.test.tsx +++ b/plugins/altimate-code/hooks/features/docs.test.tsx @@ -224,6 +224,8 @@ type Options = { holdFirst?: Promise /** The coverage read from the project's manifest for the second edit waits for this. */ holdSecondEdit?: Promise + /** The coverage read from the project's manifest for the first edit waits for this. */ + holdFirstEdit?: Promise /** The check's manifest is rewritten (by another plugin run) while its coverage is read. */ rewrittenWhileRead?: boolean } @@ -290,6 +292,7 @@ async function start($: any, on: any, options: Options = {}) { }) let shows = 0 let plainReads = 0 + let firstReads = 0 const previewOf = () => ({ data: { is_inline: false, node_name: options.check!.node, output_format: 'json', preview: JSON.stringify(options.check!.rows), unique_id: `model.shop.${options.check!.node}` }, info: { code: 'Q041', level: 'info', msg: '…' } }) on('process.run', (_: unknown, e: { argv: readonly string[] }) => { const [, command, , tool] = e.argv @@ -311,6 +314,10 @@ async function start($: any, on: any, options: Options = {}) { if (tool === 'dbt_docs_coverage') { if (options.coverageError) return { value: { exitCode: 1, stdout: JSON.stringify({ status: 'error', output: options.coverageError, metadata: { success: false } }), stderr: '' } } const fromCheck = options.check && String(e.argv[5]).includes('.altimate-plugin') + if (!fromCheck && options.check?.holdFirstEdit && ++firstReads === 1) { + const answer = { value: { exitCode: 0, stdout: JSON.stringify({ status: 'ok', output: 'coverage', metadata: options.coverage }), stderr: '' } } + return options.check.holdFirstEdit.then(() => answer) + } if (!fromCheck && options.check?.holdSecondEdit && ++plainReads === 2) { const answer = { value: { exitCode: 0, stdout: JSON.stringify({ status: 'ok', output: 'coverage', metadata: options.coverage }), stderr: '' } } return options.check.holdSecondEdit.then(() => answer) @@ -732,3 +739,24 @@ kitTest('a check manifest another plugin run rewrote while its coverage was read expect(await ui.find({ type: 'Text', text: /no description: payments/ })).toBeUndefined() await ui.unmount() }) + +kitTest('an edit\'s first coverage read that comes back late does not replace the coverage a later edit\'s check already gave', { options: { dbt_mods: true } }, async ($, on) => { + const node = { 'model.shop.orders': { resource_type: 'model', name: 'orders', package_name: 'shop', original_file_path: 'models/marts/orders.sql', columns: {} } } + const before = { success: true, model: 'orders', has_description: true, output_columns: ['order_id'], documented: ['order_id'], missing: [], percent: 100 } + const after = { success: true, model: 'orders', has_description: true, output_columns: ['order_id', 'payments'], documented: ['order_id'], missing: ['payments'], percent: 50 } + let releaseA!: () => void + const holdFirstEdit = new Promise(resolve => (releaseA = resolve)) + const { clock } = await start($, on, { editNewer: true, manifest: node, coverage: before, check: { rows: [{ order_id: 1 }], node: 'orders', manifest: node, coverage: after, holdFirstEdit } }) + const file = `${ROOT}/models/marts/orders.sql` + const a = $.tool.call({ tool: 'Edit', file_path: file, old_string: 'a', new_string: 'b' }) + await clock.advance(10) + await $.tool.call({ tool: 'Edit', file_path: file, old_string: 'b', new_string: 'c' }) + await clock.settle() + releaseA() + await a + await clock.settle() + const ui = await $.ui.mount({ ...COVERAGE_PANE, surface: 'terminal' }) + expect(await ui.find({ type: 'Text', text: /orders: 1 of 2 columns documented \(50%\) · no description: payments/ })).toBeDefined() + expect(await ui.find({ type: 'Text', text: /1 of 1 columns documented/ })).toBeUndefined() + await ui.unmount() +}) diff --git a/plugins/altimate-code/hooks/features/docs.tsx b/plugins/altimate-code/hooks/features/docs.tsx index 062ea28c..9ce07340 100644 --- a/plugins/altimate-code/hooks/features/docs.tsx +++ b/plugins/altimate-code/hooks/features/docs.tsx @@ -510,6 +510,8 @@ async function coverageOf($: EngineInterface, project: Project, model: string, m // The coverage shown came from a manifest older than the model's last edit: the check of that edit parses the // project again, and the coverage is read once more from the manifest it wrote. Null when nothing waits on one. let waiting: { file: string; model: string; project: Project } | null = null +// Counts the SQL edits whose coverage is read, and the sessions: only the newest edit's first read may set the coverage shown. +let coverageSeq = 0 // How long the coverage waits for the edit's check (a dbt show against the warehouse), then for its second read. const CHECK_WAIT_MS = 180_000 const RECHECK_MS = 30_000 @@ -523,6 +525,7 @@ async function sqlEdited($: EngineInterface, file: EditedFile): Promise null) const stale = !!edited && manifest.mtimeMs < edited.mtimeMs + const mine = ++coverageSeq const asked = stale ? { file: normal(file.path), model, project } : null waiting = asked // Asked for before the check starts (the dbt helpers run after this feature in the edit phase), so it is not missed. @@ -534,8 +537,11 @@ async function sqlEdited($: EngineInterface, file: EditedFile): Promise ({ ...(prev ?? empty()), coverage: view })) - await showStatus($) + // A later edit (or its check's coverage) has set the coverage shown since this read began: it is not replaced. + if (mine === coverageSeq && waiting === asked) { + await update($, docsAtom, prev => ({ ...(prev ?? empty()), coverage: view })) + await showStatus($) + } const gaps = [...(view.hasDescription ? [] : ['the model itself']), ...view.missing] // A select list that is not plain column names (a `*`, an unaliased expression, a UNION) is counted by the manifest's columns only. const star = read.star ? " · the model's select list is not plain column names (a `*`, an unaliased expression or a UNION), so only the columns the manifest lists are counted" : '' @@ -716,7 +722,11 @@ 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'>) {} +export async function sessionEnd($: EngineInterface, e: Args<'session.end'>) { + // A coverage read still running belongs to the conversation that ended (a /clear has no session start). + waiting = null + coverageSeq++ +} /** 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 { diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index c719d4ce..59695de1 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -57,6 +57,8 @@ let toldProblem: string | null = null let inFlight: string[] = [] /** What the conversation a /clear ended knew (its baseline, the changes not yet checked, the issues): the next one keeps it. */ let carried: HealthState | null = null +/** A /clear whose conversation has not had clearStart yet. */ +let clearPending = false /** Which model each file belonged to in earlier checks: a deleted schema file or model no longer shows in the latest answer. */ let known: HealthInsight[] = [] /** Whether the status line carries the health part now. */ @@ -490,8 +492,10 @@ async function checkEnv($: EngineInterface): Promise | un const path = targetPath(await $.fs.read(`${project}/dbt_project.yml`).catch(() => '')) const target = path.startsWith('/') ? path : `${project}/${path}` const scratch = `${target}/${SCRATCH_TARGET}/${HEALTH_SCRATCH}` + // Best effort: without the copy the check reads no catalog, as with a project that has none; an earlier copy is + // removed first, so a catalog the project no longer has, or one that failed to copy, is never read. + await $.process.run(['rm', '-f', `${scratch}/catalog.json`], { timeoutMs: 10_000 }).catch(() => null) if (await $.fs.exists(`${target}/catalog.json`).catch(() => false)) { - // Best effort: without the copy the check reads no catalog, as with a project that has none. await $.process.run(['mkdir', '-p', scratch], { timeoutMs: 10_000 }).catch(() => null) await $.process.run(['cp', '-p', `${target}/catalog.json`, `${scratch}/catalog.json`], { timeoutMs: 60_000 }).catch(() => null) } @@ -656,6 +660,7 @@ export function register(on: On, context: FeatureContext) { options = context.options on('command.run', { command: COMMAND }, async $ => { featureUsed($, 'health', 'opened') + await clearStart($) const state = await read($, healthAtom) if (!state || !enabled) return { text: 'Project health checks dbt projects: this session has none open yet.' } // Asking again retries a first check that failed, and checks changes not yet checked. @@ -676,6 +681,7 @@ export async function sessionStart($: EngineInterface, e: Args<'session.start'>) running = null enabled = false carried = null + clearPending = false inFlight = [] toldProblem = null known = [] @@ -706,6 +712,9 @@ async function enable($: EngineInterface) { * /clear still holds a push); with none kept, the baseline is taken as at a session's start. */ export async function clearStart($: EngineInterface) { + // Once per /clear, whichever comes first: the session id's change, or a hook of the new conversation. + if (!clearPending) return + clearPending = false const kept = carried carried = null generation += 1 @@ -717,32 +726,42 @@ export async function clearStart($: EngineInterface) { } enabled = true // Changes the new conversation recorded before this ran are added to the kept ones, not replaced by them. - await update($, healthAtom, prev => ({ - ...kept, - running: false, - pending: [...new Set([...kept.pending, ...(prev?.pending ?? [])])], - touched: [...new Set([...kept.touched, ...(prev?.touched ?? [])])], - // A new change asks for a check again after a failed one, as markChanged does. - ...(prev?.pending.length ? { problem: null } : {}), - })) + await update($, healthAtom, prev => (prev ? mergeKept(kept, prev) : { ...kept, running: false })) drawn = true await drawStatus($, kept.issues) if (await wantsCheck($)) later($, () => kick($)) } +/** The state a /clear carries, with the changes a conversation recorded on top of it. */ +function mergeKept(kept: HealthState, later: HealthState): HealthState { + return { + ...kept, + baseline: kept.baseline ?? later.baseline, + running: false, + pending: [...new Set([...kept.pending, ...later.pending])], + touched: [...new Set([...kept.touched, ...later.touched])], + // A new change asks for a check again after a failed one, as markChanged does. + ...(later.pending.length ? { problem: null } : {}), + } +} + /** 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: when model files changed, check again. */ export async function turnComplete($: EngineInterface, e: Args<'turn.complete'>, out: EventResult<'turn.complete'>) { if ((e as { agentId?: string }).agentId) return + await clearStart($) if (await wantsCheck($)) later($, () => kick($)) } export async function sessionEnd($: EngineInterface, e: Args<'session.end'>) { // A /clear: what this conversation knows is handed to the next one. A check it cuts short is asked for again there. const state = e.reason === 'clear' && enabled ? await read($, healthAtom).catch(() => null) : null - carried = state ? { ...state, pending: state.running ? [...new Set([...inFlight, ...state.pending])] : state.pending, running: false } : null + const now = state ? { ...state, pending: state.running ? [...new Set([...inFlight, ...state.pending])] : state.pending, running: false } : null + // A second /clear before the first was restored: what that one carried and what this conversation recorded are both kept. + carried = e.reason !== 'clear' ? null : carried && now ? mergeKept(carried, now) : (now ?? carried) + clearPending = e.reason === 'clear' generation += 1 running = null // Kept on through a /clear that carries the state: a change made before clearStart restores it is still recorded, and merged. @@ -774,6 +793,8 @@ export function pushRefusal(blocks: unknown, command: string, issues: readonly H * they have new health issues. A check that cannot run or does not finish holds it too. */ export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> { + // Right after a /clear the kept issues may not be back yet: a push waits for them. + await clearStart($) if (!(options.healthBlocksPush === true || options.healthBlocksPush === 'true') || !isGitPush(e.command)) { // Where the shell is before this command runs: its own `cd`s are read from there, and `bashAfter` sees the folder after them. if (CHANGES_FOLDER.test(e.command)) shellBefore = { command: e.command, folder: await within($, GATE_WAIT_MS, shellFolder($)) } diff --git a/plugins/altimate-code/hooks/features/index.ts b/plugins/altimate-code/hooks/features/index.ts index 98d4b362..28f1b4bb 100644 --- a/plugins/altimate-code/hooks/features/index.ts +++ b/plugins/altimate-code/hooks/features/index.ts @@ -10,7 +10,7 @@ import type { Args, EngineInterface, EventResult, On, PluginOptions, RenderElement, ToolCallInput, ToolCallResult } from 'claude-code' import { later } from '../altimate-cli' -import { restoreStatusParts } from '../status-line' +import { keepStatusParts, restoreStatusParts } from '../status-line' import type { BashCall, EditedFile, FileCall } from '../tool-chain' import { CHECK_HOLD_MS as dbtModsCheckHoldMs, @@ -49,6 +49,7 @@ import { bashRewrite as deferBashRewrite, bashGate as deferBashGate, bashAfter as deferBashAfter, + bashDropped as deferBashDropped, editAfter as deferEditAfter, readOrEditAfter as deferReadOrEditAfter, editReadsBefore as deferEditReadsBefore, @@ -290,7 +291,10 @@ export async function featuresTurnComplete($: EngineInterface, e: Args<'turn.com export async function featuresSessionEnd($: EngineInterface, e: Args<'session.end'>) { // Taken before the features end: a /clear goes on under another id once they have. - if (e.reason === 'clear' && typeof e.sessionId === 'string') later($, () => whenCleared($, e.sessionId)) + if (e.reason === 'clear' && typeof e.sessionId === 'string') { + keepStatusParts() + later($, () => whenCleared($, e.sessionId)) + } await attempt($, 'runs', 'sessionEnd', () => runsSessionEnd($, e)) await attempt($, 'defer', 'sessionEnd', () => deferSessionEnd($, e)) await attempt($, 'toModel', 'sessionEnd', () => toModelSessionEnd($, e)) @@ -369,6 +373,7 @@ export async function featuresCallStart($: EngineInterface, e: ToolCallInput): P /** A Bash call that a gate refused, or whose result is a refusal, never reaches bashAfter: the features that kept it let go. */ export async function featuresBashDropped($: EngineInterface, e: BashCall): Promise { await attempt($, 'runs', 'bashDropped', () => runsBashDropped($, e)) + await attempt($, 'defer', 'bashDropped', () => deferBashDropped($, e)) } /** The context lines every feature adds after a Bash call ran. */ diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index 8447eb6c..5b186d01 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -242,6 +242,31 @@ test('a change made right after /clear, before the kept state is put back, is me expect(shown.text).toContain('1 new health issue · fct_orders: model fanout') }) +test('a git push right after /clear, before the kept issues are back, is still held by them', { options: { healthBlocksPush: true } }, async ($, on) => { + const clear = playsClear($, on) + const { clock, calls } = await start($, on, { runs: [[OLD_STG, OLD_FCT], [OLD_STG, OLD_FCT, NEW_STG]] }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + await clear() + // No settling: the push is the new conversation's first call. + const denied: any = await $.tool.call(command('git push')) + expect(denied?.deny).toContain('1 new project health issue') + expect(calls).toHaveLength(2) +}) + +test('a second /clear before the first one\'s state was restored keeps what the first carried', async ($, on) => { + const clear = playsClear($, on) + const { clock, calls } = await start($, on, { runs: [[OLD_STG, OLD_FCT], [OLD_STG, OLD_FCT, NEW_STG]] }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + await clear() + await clear() + await clock.settle() + expect(calls).toHaveLength(2) + const shown: any = await $.command.run({ command: 'health' } as any) + expect(shown.text).toContain('1 new health issue · stg_orders: hard coded references') +}) + test('two changed models give "2 new health issues" with the first model named', async ($, on) => { const { clock } = await start($, on, { runs: [[OLD_STG], [OLD_STG, NEW_STG, NEW_FCT]] }) await $.tool.call(edit(STG)) diff --git a/plugins/altimate-code/hooks/status-line.ts b/plugins/altimate-code/hooks/status-line.ts index 27c744cc..9e27ccbd 100644 --- a/plugins/altimate-code/hooks/status-line.ts +++ b/plugins/altimate-code/hooks/status-line.ts @@ -37,9 +37,22 @@ export async function setStatusPart($: EngineInterface, id: StatusPartId, text: $.ui.status(statusLineOf(drawnParts)) } +// The parts drawn when a /clear ended the conversation, until the next one has them back. +let beforeClear: Record | null = null + +/** At a /clear: keeps the parts drawn now, which a part set before restoreStatusParts must not replace. */ +export function keepStatusParts() { + beforeClear = { ...(beforeClear ?? {}), ...drawnParts } +} + /** After a /clear: the parts drawn before it, under any the new conversation has set already. */ export async function restoreStatusParts($: EngineInterface) { - await update($, parts, prev => ({ ...drawnParts, ...prev })) + const kept = beforeClear + beforeClear = null + if (!kept) return + await update($, parts, prev => ({ ...kept, ...prev })) + drawnParts = await read($, parts) + $.ui.status(statusLineOf(drawnParts)) } /** @@ -48,6 +61,7 @@ export async function restoreStatusParts($: EngineInterface) { */ export async function clearStatusParts($: EngineInterface) { drawnParts = {} + beforeClear = null await update($, parts, () => ({})) $.ui.status(undefined) } From e83245a1615ceb958f283b809d7e92172d75e075 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 20 Nov 2026 01:14:15 +0530 Subject: [PATCH 04/13] =?UTF-8?q?fix:=20review=20round=203=20=E2=80=94=20c?= =?UTF-8?q?allers=20wait=20for=20a=20running=20/clear=20restore,=20coverag?= =?UTF-8?q?e=20order=20taken=20before=20the=20first=20wait,=20removed=20st?= =?UTF-8?q?atus=20parts=20stay=20removed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- .../altimate-code/hooks/clear-state.testkit.ts | 13 ++++++++++--- plugins/altimate-code/hooks/features/docs.tsx | 4 +++- .../altimate-code/hooks/features/health.tsx | 17 ++++++++++++++++- plugins/altimate-code/hooks/health.test.tsx | 18 ++++++++++++++++++ plugins/altimate-code/hooks/status-line.ts | 7 ++++--- 5 files changed, 51 insertions(+), 8 deletions(-) diff --git a/plugins/altimate-code/hooks/clear-state.testkit.ts b/plugins/altimate-code/hooks/clear-state.testkit.ts index e488544d..bfd73ec9 100644 --- a/plugins/altimate-code/hooks/clear-state.testkit.ts +++ b/plugins/altimate-code/hooks/clear-state.testkit.ts @@ -6,10 +6,12 @@ type Address = { plugin: string; key: string; id?: string } /** * Registers the state and session id hooks (before the test's first call on `$`); the function it returns plays - * the /clear: the session's end, then the new id with its empty state. + * the /clear: the session's end, then the new id with its empty state. `holdWriteOf` holds the new conversation's + * first write of one value until released. */ -export function playsClear($: any, on: any): () => Promise { +export function playsClear($: any, on: any, options: { holdWriteOf?: { key: string; until: Promise } } = {}): () => Promise { let cleared = false + let held = false const written = new Set() const name = (e: Address) => `${e.plugin}\u0000${e.key}\u0000${e.id ?? ''}` // A $-call's hook answers `{ value }`: here the value is the read, `{ value, version }`. The version stays, so a write still lands. @@ -17,8 +19,13 @@ export function playsClear($: any, on: any): () => Promise { const stored = await next(e) return cleared && !written.has(name(e)) && stored.value ? { ...stored, value: { ...stored.value, value: undefined } } : stored }) - on('state.set', (_: unknown, e: Address, next: (e: Address) => Promise) => { + on('state.set', async (_: unknown, e: Address, next: (e: Address) => Promise) => { if (cleared) written.add(name(e)) + // The new conversation's first write of that value waits, so a test can act while it is under way. + if (cleared && !held && options.holdWriteOf?.key === e.key) { + held = true + await options.holdWriteOf.until + } return next(e) }) let clears = 0 diff --git a/plugins/altimate-code/hooks/features/docs.tsx b/plugins/altimate-code/hooks/features/docs.tsx index 9ce07340..8f32461a 100644 --- a/plugins/altimate-code/hooks/features/docs.tsx +++ b/plugins/altimate-code/hooks/features/docs.tsx @@ -517,6 +517,8 @@ const CHECK_WAIT_MS = 180_000 const RECHECK_MS = 30_000 async function sqlEdited($: EngineInterface, file: EditedFile): Promise { + // Taken before the first wait: an edit that comes later, or a session's end, makes this one's coverage old. + const mine = ++coverageSeq const project = await projectOf($, file.path) if (!project) return [] const model = normal(file.path).split('/').pop()!.replace(/\.sql$/i, '') @@ -525,7 +527,7 @@ async function sqlEdited($: EngineInterface, file: EditedFile): Promise null) const stale = !!edited && manifest.mtimeMs < edited.mtimeMs - const mine = ++coverageSeq + if (mine !== coverageSeq) return [] const asked = stale ? { file: normal(file.path), model, project } : null waiting = asked // Asked for before the check starts (the dbt helpers run after this feature in the edit phase), so it is not missed. diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 59695de1..ba7723b9 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -59,6 +59,8 @@ let inFlight: string[] = [] let carried: HealthState | null = null /** A /clear whose conversation has not had clearStart yet. */ let clearPending = false +/** clearStart's restore while it runs. */ +let restoring: Promise | null = null /** Which model each file belonged to in earlier checks: a deleted schema file or model no longer shows in the latest answer. */ let known: HealthInsight[] = [] /** Whether the status line carries the health part now. */ @@ -682,6 +684,7 @@ export async function sessionStart($: EngineInterface, e: Args<'session.start'>) enabled = false carried = null clearPending = false + restoring = null inFlight = [] toldProblem = null known = [] @@ -712,9 +715,21 @@ async function enable($: EngineInterface) { * /clear still holds a push); with none kept, the baseline is taken as at a session's start. */ export async function clearStart($: EngineInterface) { - // Once per /clear, whichever comes first: the session id's change, or a hook of the new conversation. + // Once per /clear, whichever comes first: the session id's change, or a hook of the new conversation. A caller + // that comes while it runs waits for it, so a push is never decided on the empty state. + if (restoring) return restoring if (!clearPending) return clearPending = false + const run = restore($) + restoring = run + try { + await run + } finally { + if (restoring === run) restoring = null + } +} + +async function restore($: EngineInterface) { const kept = carried carried = null generation += 1 diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index 5b186d01..ce2b9a2f 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -254,6 +254,24 @@ test('a git push right after /clear, before the kept issues are back, is still h expect(calls).toHaveLength(2) }) +test('a git push that comes while the /clear restore is running waits for it and is held', { options: { healthBlocksPush: true } }, async ($, on) => { + let release!: () => void + const clear = playsClear($, on, { holdWriteOf: { key: 'health', until: new Promise(resolve => (release = resolve)) } }) + const { clock } = await start($, on, { runs: [[OLD_STG, OLD_FCT], [OLD_STG, OLD_FCT, NEW_STG]] }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + await clear() + // /health starts the restore, whose write of the kept state is held; the push and a second /health come meanwhile. + const asked = $.command.run({ command: 'health' } as any) + const pushed = $.tool.call(command('git push')) + const again = $.command.run({ command: 'health' } as any) + await clock.advance(10) + release() + const [first, denied, second]: any[] = await Promise.all([asked, pushed, again]) + expect(denied?.deny).toContain('1 new project health issue') + for (const shown of [first, second]) expect(shown.text).toContain('1 new health issue · stg_orders') +}) + test('a second /clear before the first one\'s state was restored keeps what the first carried', async ($, on) => { const clear = playsClear($, on) const { clock, calls } = await start($, on, { runs: [[OLD_STG, OLD_FCT], [OLD_STG, OLD_FCT, NEW_STG]] }) diff --git a/plugins/altimate-code/hooks/status-line.ts b/plugins/altimate-code/hooks/status-line.ts index 9e27ccbd..be8e1763 100644 --- a/plugins/altimate-code/hooks/status-line.ts +++ b/plugins/altimate-code/hooks/status-line.ts @@ -24,9 +24,13 @@ export function statusLineOf(current: Record): string | undefine // The parts as last drawn: a /clear empties the session's state, and the line is the process's, so they are put back. let drawnParts: Record = {} +// The parts drawn when a /clear ended the conversation, until the next one has them back. +let beforeClear: Record | null = null /** Sets one part (null or empty removes it) and draws the status line again. */ export async function setStatusPart($: EngineInterface, id: StatusPartId, text: string | null) { + // A part its owner removes after a /clear is not brought back with the parts kept from before it. + if (beforeClear && !text) delete beforeClear[id] await update($, parts, prev => { const next = { ...prev } if (text) next[id] = text @@ -37,9 +41,6 @@ export async function setStatusPart($: EngineInterface, id: StatusPartId, text: $.ui.status(statusLineOf(drawnParts)) } -// The parts drawn when a /clear ended the conversation, until the next one has them back. -let beforeClear: Record | null = null - /** At a /clear: keeps the parts drawn now, which a part set before restoreStatusParts must not replace. */ export function keepStatusParts() { beforeClear = { ...(beforeClear ?? {}), ...drawnParts } From 0e64040540b507070a764a634b10be7b37ef43c9 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 20 Nov 2026 01:21:16 +0530 Subject: [PATCH 05/13] =?UTF-8?q?fix:=20review=20round=204=20=E2=80=94=20t?= =?UTF-8?q?he=20/clear=20restore=20counts=20inside=20the=20push=20gate's?= =?UTF-8?q?=20wait;=20a=20status=20part=20removed=20during=20the=20restore?= =?UTF-8?q?=20stays=20removed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- plugins/altimate-code/hooks/features/health.tsx | 5 ++--- plugins/altimate-code/hooks/health.test.tsx | 12 ++++++++++++ plugins/altimate-code/hooks/status-line.ts | 6 +++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index ba7723b9..140964b9 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -808,15 +808,14 @@ export function pushRefusal(blocks: unknown, command: string, issues: readonly H * they have new health issues. A check that cannot run or does not finish holds it too. */ export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: string } | null> { - // Right after a /clear the kept issues may not be back yet: a push waits for them. - await clearStart($) if (!(options.healthBlocksPush === true || options.healthBlocksPush === 'true') || !isGitPush(e.command)) { // Where the shell is before this command runs: its own `cd`s are read from there, and `bashAfter` sees the folder after them. if (CHANGES_FOLDER.test(e.command)) shellBefore = { command: e.command, folder: await within($, GATE_WAIT_MS, shellFolder($)) } return null } // The whole decision, folder lookups included, is bounded inside the chain's deadline: past it the gate is given up on and the push runs. - const decided = await within($, GATE_WAIT_MS, decide($, e)) + // Right after a /clear the kept issues may not be back yet: the push waits for them, inside the same deadline. + const decided = await within($, GATE_WAIT_MS, clearStart($).then(() => decide($, e))) const verdict = decided === null ? hold('the check of the changes could not be read in time.') : decided === 'run' ? null : decided featureUsed($, 'health_push_gate', verdict ? 'held' : 'allowed') return verdict diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index ce2b9a2f..9a2112c1 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -272,6 +272,18 @@ test('a git push that comes while the /clear restore is running waits for it and for (const shown of [first, second]) expect(shown.text).toContain('1 new health issue · stg_orders') }) +test('a git push whose /clear restore does not finish within the gate\'s wait is held, not let through', { options: { healthBlocksPush: true } }, async ($, on) => { + const clear = playsClear($, on, { holdWriteOf: { key: 'health', until: new Promise(() => {}) } }) + const { clock } = await start($, on, { runs: [[OLD_STG, OLD_FCT], [OLD_STG, OLD_FCT, NEW_STG]] }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + await clear() + const pushed = $.tool.call(command('git push')) + await clock.advance(GATE_WAIT_MS + 10) + const denied: any = await pushed + expect(denied?.deny).toContain('git push is held') +}) + test('a second /clear before the first one\'s state was restored keeps what the first carried', async ($, on) => { const clear = playsClear($, on) const { clock, calls } = await start($, on, { runs: [[OLD_STG, OLD_FCT], [OLD_STG, OLD_FCT, NEW_STG]] }) diff --git a/plugins/altimate-code/hooks/status-line.ts b/plugins/altimate-code/hooks/status-line.ts index be8e1763..5da4ac19 100644 --- a/plugins/altimate-code/hooks/status-line.ts +++ b/plugins/altimate-code/hooks/status-line.ts @@ -48,10 +48,10 @@ export function keepStatusParts() { /** After a /clear: the parts drawn before it, under any the new conversation has set already. */ export async function restoreStatusParts($: EngineInterface) { - const kept = beforeClear + if (!beforeClear) return + // Read when the write applies, so a part removed while it waits stays removed; let go only once written. + await update($, parts, prev => ({ ...(beforeClear ?? {}), ...prev })) beforeClear = null - if (!kept) return - await update($, parts, prev => ({ ...kept, ...prev })) drawnParts = await read($, parts) $.ui.status(statusLineOf(drawnParts)) } From c2ceebb7e47e55db4e9b0f12cff7ae84bffff82a Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 20 Nov 2026 01:33:23 +0530 Subject: [PATCH 06/13] fix: only the integrations and altimate-code status parts are kept across /clear; rename a parameter that shadowed `later` Co-Authored-By: Claude Opus 5.5 --- plugins/altimate-code/CHANGELOG.md | 2 +- plugins/altimate-code/hooks/features/health.tsx | 10 +++++----- plugins/altimate-code/hooks/status-line.ts | 9 +++++++-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/plugins/altimate-code/CHANGELOG.md b/plugins/altimate-code/CHANGELOG.md index 46a28535..6ccf06f8 100644 --- a/plugins/altimate-code/CHANGELOG.md +++ b/plugins/altimate-code/CHANGELOG.md @@ -5,7 +5,7 @@ All notable changes to this plugin will be documented in this file. ## [Unreleased] ### Fixed -- After `/clear`, the new conversation loads the team comments again (the band, and the threads Claude gets when it reads a commented model) instead of waiting for `/comments` or the next poll, and project health keeps its baseline, the changes not yet checked and the issues found (`/health` no longer says the session has no project; a check the `/clear` cut short runs again). The status line keeps its parts. +- After `/clear`, the new conversation loads the team comments again (the band, and the threads Claude gets when it reads a commented model) instead of waiting for `/comments` or the next poll, and project health keeps its baseline, the changes not yet checked and the issues found (`/health` no longer says the session has no project; a check the `/clear` cut short runs again). The status line keeps the integrations and altimate-code parts; health draws its own again. - The dbt runs the plugin starts by itself (compile and validate's `dbt show` and `dbt ls`, `/dbt-show` and Re-run, and the project health check's parse) write their artifacts to `target/.altimate-plugin/` instead of `target/`, so they no longer replace the project's `manifest.json` (with every model's compiled SQL) or `run_results.json`. The health check runs its CLI call in its own process for that, with the project's `catalog.json` copied beside its manifest. The doctor's **Run dbt parse** still refreshes the project's own manifest. - After Claude edits a model's SQL that dbt has not compiled since, its documentation coverage says it is from before the edit, and once the edit's compile-and-validate check has run the model, `/propagate` shows the coverage of the model as it is now. - The end-of-task summary for a model the task created says its columns are new with an unknown reach, not "update altimate-code". diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 140964b9..87b3ec4d 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -748,15 +748,15 @@ async function restore($: EngineInterface) { } /** The state a /clear carries, with the changes a conversation recorded on top of it. */ -function mergeKept(kept: HealthState, later: HealthState): HealthState { +function mergeKept(kept: HealthState, recorded: HealthState): HealthState { return { ...kept, - baseline: kept.baseline ?? later.baseline, + baseline: kept.baseline ?? recorded.baseline, running: false, - pending: [...new Set([...kept.pending, ...later.pending])], - touched: [...new Set([...kept.touched, ...later.touched])], + pending: [...new Set([...kept.pending, ...recorded.pending])], + touched: [...new Set([...kept.touched, ...recorded.touched])], // A new change asks for a check again after a failed one, as markChanged does. - ...(later.pending.length ? { problem: null } : {}), + ...(recorded.pending.length ? { problem: null } : {}), } } diff --git a/plugins/altimate-code/hooks/status-line.ts b/plugins/altimate-code/hooks/status-line.ts index 5da4ac19..3d5b3d4b 100644 --- a/plugins/altimate-code/hooks/status-line.ts +++ b/plugins/altimate-code/hooks/status-line.ts @@ -41,9 +41,14 @@ export async function setStatusPart($: EngineInterface, id: StatusPartId, text: $.ui.status(statusLineOf(drawnParts)) } -/** At a /clear: keeps the parts drawn now, which a part set before restoreStatusParts must not replace. */ +// The parts that describe the machine rather than the conversation: they hold after a /clear. A feature's part +// comes from its session state, which a /clear empties; the feature draws it again when it has it back. +const KEPT_ACROSS_CLEAR: readonly StatusPartId[] = ['integrations', 'altimateCode'] + +/** At a /clear: keeps the machine's parts drawn now, which a part set before restoreStatusParts must not replace. */ export function keepStatusParts() { - beforeClear = { ...(beforeClear ?? {}), ...drawnParts } + const kept = Object.fromEntries(KEPT_ACROSS_CLEAR.filter(id => drawnParts[id]).map(id => [id, drawnParts[id]!])) + beforeClear = { ...(beforeClear ?? {}), ...kept } } /** After a /clear: the parts drawn before it, under any the new conversation has set already. */ From d83f1a999283bee87df93f23f233d7eb33cb3dd2 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 20 Nov 2026 01:44:50 +0530 Subject: [PATCH 07/13] =?UTF-8?q?fix:=20review=20round=205=20=E2=80=94=20a?= =?UTF-8?q?=20/clear=20while=20the=20previous=20restore=20is=20writing=20k?= =?UTF-8?q?eeps=20the=20health=20snapshot;=20the=20test=20/clear=20reads?= =?UTF-8?q?=20a=20value=20as=20written=20only=20once=20it=20is?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- .../altimate-code/hooks/clear-state.testkit.ts | 7 +++++-- .../altimate-code/hooks/features/health.tsx | 6 ++++-- plugins/altimate-code/hooks/health.test.tsx | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/plugins/altimate-code/hooks/clear-state.testkit.ts b/plugins/altimate-code/hooks/clear-state.testkit.ts index bfd73ec9..77be94a1 100644 --- a/plugins/altimate-code/hooks/clear-state.testkit.ts +++ b/plugins/altimate-code/hooks/clear-state.testkit.ts @@ -20,13 +20,16 @@ export function playsClear($: any, on: any, options: { holdWriteOf?: { key: stri return cleared && !written.has(name(e)) && stored.value ? { ...stored, value: { ...stored.value, value: undefined } } : stored }) on('state.set', async (_: unknown, e: Address, next: (e: Address) => Promise) => { - if (cleared) written.add(name(e)) + const asked = clears // The new conversation's first write of that value waits, so a test can act while it is under way. if (cleared && !held && options.holdWriteOf?.key === e.key) { held = true await options.holdWriteOf.until } - return next(e) + const done = await next(e) + // Read as written only once it is, and only in the conversation it was written in. + if (cleared && asked === clears) written.add(name(e)) + return done }) let clears = 0 on('session.id', () => ({ value: `session-${clears}` })) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 87b3ec4d..f305e759 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -730,8 +730,8 @@ export async function clearStart($: EngineInterface) { } async function restore($: EngineInterface) { + // The snapshot is let go only once it is written: a /clear while the write is under way carries it on. const kept = carried - carried = null generation += 1 running = null if (!kept) { @@ -742,6 +742,7 @@ async function restore($: EngineInterface) { enabled = true // Changes the new conversation recorded before this ran are added to the kept ones, not replaced by them. await update($, healthAtom, prev => (prev ? mergeKept(kept, prev) : { ...kept, running: false })) + if (carried === kept) carried = null drawn = true await drawStatus($, kept.issues) if (await wantsCheck($)) later($, () => kick($)) @@ -775,7 +776,8 @@ export async function sessionEnd($: EngineInterface, e: Args<'session.end'>) { const state = e.reason === 'clear' && enabled ? await read($, healthAtom).catch(() => null) : null const now = state ? { ...state, pending: state.running ? [...new Set([...inFlight, ...state.pending])] : state.pending, running: false } : null // A second /clear before the first was restored: what that one carried and what this conversation recorded are both kept. - carried = e.reason !== 'clear' ? null : carried && now ? mergeKept(carried, now) : (now ?? carried) + // A fresh object either way, so a restore still writing the earlier snapshot does not let this one go. + carried = e.reason !== 'clear' ? null : carried ? (now ? mergeKept(carried, now) : { ...carried }) : now clearPending = e.reason === 'clear' generation += 1 running = null diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index 9a2112c1..554eb2be 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -284,6 +284,24 @@ test('a git push whose /clear restore does not finish within the gate\'s wait is expect(denied?.deny).toContain('git push is held') }) +test('a /clear that comes while the previous one\'s restore is still writing keeps the state, and the push stays held', { options: { healthBlocksPush: true } }, async ($, on) => { + let release!: () => void + const clear = playsClear($, on, { holdWriteOf: { key: 'health', until: new Promise(resolve => (release = resolve)) } }) + const { clock, calls } = await start($, on, { runs: [[OLD_STG, OLD_FCT], [OLD_STG, OLD_FCT, NEW_STG]] }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + await clear() + const asked = $.command.run({ command: 'health' } as any) + await clock.advance(10) + await clear() + release() + await asked + await clock.settle() + expect(calls).toHaveLength(2) + const denied: any = await $.tool.call(command('git push')) + expect(denied?.deny).toContain('1 new project health issue') +}) + test('a second /clear before the first one\'s state was restored keeps what the first carried', async ($, on) => { const clear = playsClear($, on) const { clock, calls } = await start($, on, { runs: [[OLD_STG, OLD_FCT], [OLD_STG, OLD_FCT, NEW_STG]] }) From 5b0d7d2a38c87156dd1621cf097f59735840f7ad Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 20 Nov 2026 01:45:46 +0530 Subject: [PATCH 08/13] test: /clear drops a feature's status part whose state it empties Co-Authored-By: Claude Opus 5.5 --- .../hooks/features/docs.test.tsx | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/plugins/altimate-code/hooks/features/docs.test.tsx b/plugins/altimate-code/hooks/features/docs.test.tsx index 26212f80..0ea317f3 100644 --- a/plugins/altimate-code/hooks/features/docs.test.tsx +++ b/plugins/altimate-code/hooks/features/docs.test.tsx @@ -7,6 +7,7 @@ const test = (name: string, body: Parameters[1]) => kitTest(name import { PHASE_TIMEOUT_MS } from '../tool-chain' import { ROUND_MS, applyPrompt, candidatesFrom, changedDescriptions, columnDescriptions, modelPaths, sentGroups } from './docs' import { nothingToReport } from '../inert-features' +import { playsClear } from '../clear-state.testkit' const ROOT = '/repo' const MANIFEST_PATH = `${ROOT}/target/manifest.json` @@ -252,7 +253,11 @@ async function start($: any, on: any, options: Options = {}) { on('ui.open', () => ({ value: {} })) on('ui.log', () => ({ value: undefined })) on('ui.toast', () => ({ value: undefined })) - on('ui.status', () => ({ value: undefined })) + const statuses: (string | 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 @@ -337,6 +342,7 @@ async function start($: any, on: any, options: Options = {}) { calls, toolArgs, submitted, + statuses, /** The next schema edit removes the description instead of writing it. */ clear: () => { clearing = true @@ -760,3 +766,14 @@ kitTest('an edit\'s first coverage read that comes back late does not replace th expect(await ui.find({ type: 'Text', text: /1 of 1 columns documented/ })).toBeUndefined() await ui.unmount() }) + +test('/clear drops the "docs N to copy" status part with the offers it counted, which the new conversation does not have', async ($, on) => { + const clear = playsClear($, on) + const { clock, statuses } = await start($, on) + await $.tool.call(EDIT) + await clock.settle() + expect(statuses.at(-1)).toContain('docs 2 to copy') + await clear() + await clock.settle() + expect(statuses.at(-1) ?? '').not.toContain('docs') +}) From 47187ac3a67b822b36c3fdc2bd199c8526c8dc21 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 20 Nov 2026 01:52:37 +0530 Subject: [PATCH 09/13] =?UTF-8?q?fix:=20review=20round=206=20=E2=80=94=20a?= =?UTF-8?q?=20caller=20that=20joins=20a=20running=20/clear=20restore=20run?= =?UTF-8?q?s=20the=20one=20a=20later=20/clear=20asked=20for;=20a=20status?= =?UTF-8?q?=20restore=20lets=20go=20only=20of=20its=20own=20snapshot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- .../altimate-code/hooks/features/health.tsx | 24 ++++++++++++------- plugins/altimate-code/hooks/health.test.tsx | 20 ++++++++++++++++ plugins/altimate-code/hooks/status-line.ts | 10 ++++---- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index f305e759..80f7a13c 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -717,15 +717,21 @@ async function enable($: EngineInterface) { export async function clearStart($: EngineInterface) { // Once per /clear, whichever comes first: the session id's change, or a hook of the new conversation. A caller // that comes while it runs waits for it, so a push is never decided on the empty state. - if (restoring) return restoring - if (!clearPending) return - clearPending = false - const run = restore($) - restoring = run - try { - await run - } finally { - if (restoring === run) restoring = null + // Another /clear may come while a restore runs: once it ends, the one that /clear asked for runs too. + for (;;) { + if (restoring) { + await restoring + continue + } + if (!clearPending) return + clearPending = false + const run = restore($) + restoring = run + try { + await run + } finally { + if (restoring === run) restoring = null + } } } diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index 554eb2be..be62b04a 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -302,6 +302,26 @@ test('a /clear that comes while the previous one\'s restore is still writing kee expect(denied?.deny).toContain('1 new project health issue') }) +test('a git push after a second /clear that came while the first restore was writing waits for both restores and is held', { options: { healthBlocksPush: true } }, async ($, on) => { + let release!: () => void + const clear = playsClear($, on, { holdWriteOf: { key: 'health', until: new Promise(resolve => (release = resolve)) } }) + const { clock } = await start($, on, { runs: [[OLD_STG, OLD_FCT], [OLD_STG, OLD_FCT, NEW_STG]] }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + await clear() + const asked = $.command.run({ command: 'health' } as any) + await clock.advance(10) + await clear() + const pushed = $.tool.call(command('git push')) + const again = $.command.run({ command: 'health' } as any) + await clock.advance(10) + release() + const [denied, shown]: any[] = await Promise.all([pushed, again]) + await asked + expect(denied?.deny).toContain('1 new project health issue') + expect(shown.text).toContain('1 new health issue · stg_orders') +}) + test('a second /clear before the first one\'s state was restored keeps what the first carried', async ($, on) => { const clear = playsClear($, on) const { clock, calls } = await start($, on, { runs: [[OLD_STG, OLD_FCT], [OLD_STG, OLD_FCT, NEW_STG]] }) diff --git a/plugins/altimate-code/hooks/status-line.ts b/plugins/altimate-code/hooks/status-line.ts index 3d5b3d4b..92eaced2 100644 --- a/plugins/altimate-code/hooks/status-line.ts +++ b/plugins/altimate-code/hooks/status-line.ts @@ -53,10 +53,12 @@ export function keepStatusParts() { /** After a /clear: the parts drawn before it, under any the new conversation has set already. */ export async function restoreStatusParts($: EngineInterface) { - if (!beforeClear) return - // Read when the write applies, so a part removed while it waits stays removed; let go only once written. - await update($, parts, prev => ({ ...(beforeClear ?? {}), ...prev })) - beforeClear = null + const kept = beforeClear + if (!kept) return + // Read when the write applies, so a part removed while it waits stays removed; let go only once written, and only + // if no later /clear kept a snapshot of its own meanwhile. + await update($, parts, prev => ({ ...kept, ...prev })) + if (beforeClear === kept) beforeClear = null drawnParts = await read($, parts) $.ui.status(statusLineOf(drawnParts)) } From 4c8f2fe0d5740b0fef027a2d2a962b4b84b052d6 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 20 Nov 2026 02:10:29 +0530 Subject: [PATCH 10/13] =?UTF-8?q?fix:=20review=20round=207=20=E2=80=94=20t?= =?UTF-8?q?he=20first=20read=20after=20/clear=20waits=20for=20the=20new=20?= =?UTF-8?q?conversation's=20comments=20even=20before=20clearStart=20runs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- .../hooks/clear-state.testkit.ts | 9 ++++--- plugins/altimate-code/hooks/comments.test.tsx | 24 +++++++++++++++++++ .../altimate-code/hooks/features/comments.tsx | 8 ++++++- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/plugins/altimate-code/hooks/clear-state.testkit.ts b/plugins/altimate-code/hooks/clear-state.testkit.ts index 77be94a1..8fb16d1e 100644 --- a/plugins/altimate-code/hooks/clear-state.testkit.ts +++ b/plugins/altimate-code/hooks/clear-state.testkit.ts @@ -7,9 +7,9 @@ type Address = { plugin: string; key: string; id?: string } /** * Registers the state and session id hooks (before the test's first call on `$`); the function it returns plays * the /clear: the session's end, then the new id with its empty state. `holdWriteOf` holds the new conversation's - * first write of one value until released. + * first write of one value until released; `idLagsUntil` keeps the old id readable until it settles. */ -export function playsClear($: any, on: any, options: { holdWriteOf?: { key: string; until: Promise } } = {}): () => Promise { +export function playsClear($: any, on: any, options: { holdWriteOf?: { key: string; until: Promise }; idLagsUntil?: Promise } = {}): () => Promise { let cleared = false let held = false const written = new Set() @@ -32,7 +32,10 @@ export function playsClear($: any, on: any, options: { holdWriteOf?: { key: stri return done }) let clears = 0 - on('session.id', () => ({ value: `session-${clears}` })) + // The engine's id read can lag the /clear: until `idLagsUntil` settles it still names the session that ended. + let lagging = !!options.idLagsUntil + options.idLagsUntil?.then(() => (lagging = false)) + on('session.id', () => ({ value: `session-${lagging ? Math.max(0, clears - 1) : clears}` })) return async () => { await $.session.end({ reason: 'clear', sessionId: `session-${clears}` }) clears++ diff --git a/plugins/altimate-code/hooks/comments.test.tsx b/plugins/altimate-code/hooks/comments.test.tsx index 5b611c9e..eb8e5140 100644 --- a/plugins/altimate-code/hooks/comments.test.tsx +++ b/plugins/altimate-code/hooks/comments.test.tsx @@ -852,3 +852,27 @@ test('after /clear the first read of a model waits for the new conversation\'s l const result: any = await reading expect(result.context?.[0]).toContain('line 3, model, on “sum(amount)”') }) + +test('a read right after /clear, before the new conversation\'s comments are loaded, waits for them even when the last conversation spent its first-read wait', async ($, on) => { + let newId!: () => void + const clear = playsClear($, on, { idLagsUntil: new Promise(resolve => (newId = resolve)) }) + let first!: () => void + let second!: () => void + const holdLists = { 0: new Promise(resolve => (first = resolve)), 1: new Promise(resolve => (second = resolve)) } + const { clock } = await start($, on, { settle: false, holdLists }) + // The last conversation's first read comes before its list: it spends its wait. + const early = $.tool.call({ tool: 'Read', file_path: FILE }) + await clock.advance(FIRST_READ_WAIT_MS + 10) + await early + first() + await clock.settle() + await clear() + // No settling: the read comes before the session id watcher has seen the new id. + const reading = $.tool.call({ tool: 'Read', file_path: FILE }) + await clock.advance(10) + second() + await clock.advance(10) + const result: any = await reading + newId() + expect(result.context?.[0]).toContain('Grace Hopper: No, gross.') +}) diff --git a/plugins/altimate-code/hooks/features/comments.tsx b/plugins/altimate-code/hooks/features/comments.tsx index aafc1549..18ed3dc1 100644 --- a/plugins/altimate-code/hooks/features/comments.tsx +++ b/plugins/altimate-code/hooks/features/comments.tsx @@ -240,7 +240,13 @@ export async function turnComplete($: EngineInterface, e: Args<'turn.complete'>, export async function sessionEnd($: EngineInterface, e: Args<'session.end'>) { // /clear ends a session without starting another: the pane keeps its threads and drafts, so the poll goes on // and a post still running or unconfirmed keeps its lock (it describes the shared docs, not the conversation). - if (e.reason === 'clear') return + if (e.reason === 'clear') { + // The next conversation starts with empty state: its first read of a model waits for its list, as a session's + // first read does, even when it comes before clearStart. + firstWaitSpent = false + pendingFor = null + return + } poll?.cancel() poll = null generation++ From 73a117b60bba944a2e35775d40969f2247fde583 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 20 Nov 2026 02:20:50 +0530 Subject: [PATCH 11/13] =?UTF-8?q?fix:=20review=20round=208=20=E2=80=94=20o?= =?UTF-8?q?ne=20health=20CLI=20call=20at=20a=20time=20across=20/clear;=20a?= =?UTF-8?q?=20project=20a=20read=20found=20before=20clearStart=20stays?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- plugins/altimate-code/hooks/comments.test.tsx | 15 +++++++++++++++ .../altimate-code/hooks/features/comments.tsx | 6 +++--- .../altimate-code/hooks/features/health.tsx | 18 +++++++++++++++++- plugins/altimate-code/hooks/health.test.tsx | 16 ++++++++++++++++ 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/plugins/altimate-code/hooks/comments.test.tsx b/plugins/altimate-code/hooks/comments.test.tsx index eb8e5140..e84a5d21 100644 --- a/plugins/altimate-code/hooks/comments.test.tsx +++ b/plugins/altimate-code/hooks/comments.test.tsx @@ -876,3 +876,18 @@ test('a read right after /clear, before the new conversation\'s comments are loa newId() expect(result.context?.[0]).toContain('Grace Hopper: No, gross.') }) + +test('a read of another project right after /clear, before the new conversation\'s comments are loaded, keeps that project', async ($, on) => { + let newId!: () => void + const clear = playsClear($, on, { idLagsUntil: new Promise(resolve => (newId = resolve)) }) + const other = '/other' + const { clock, cli } = await start($, on, { otherProject: other, files: { [FILE]: SQL, [`${other}/models/orders.sql`]: SQL } }) + await clear() + await $.tool.call({ tool: 'Read', file_path: `${other}/models/orders.sql` }) + newId() + await clock.settle() + const ui = await $.ui.mount({ ...PANE, surface: 'terminal' }) + expect(await ui.find({ type: 'Text', text: /Team comments · other_shop/ })).toBeDefined() + await ui.unmount() + expect(cli.filter(c => c.args.action === 'list').at(-1)?.args.project).toBe('other_shop') +}) diff --git a/plugins/altimate-code/hooks/features/comments.tsx b/plugins/altimate-code/hooks/features/comments.tsx index 18ed3dc1..466bad07 100644 --- a/plugins/altimate-code/hooks/features/comments.tsx +++ b/plugins/altimate-code/hooks/features/comments.tsx @@ -219,12 +219,12 @@ export async function sessionStart($: EngineInterface, e: Args<'session.start'>) /** * After a /clear: the new conversation's `$.state` is empty, so the project is found and its threads are - * loaded again now, and its first read of a model waits for them as a session's first does. The poll and a + * loaded again now (sessionEnd has reset the first-read wait, so its first read of a model waits for them). The poll and a * post's lock carry on (sessionEnd keeps them: they describe the shared docs, not the conversation). */ export async function clearStart($: EngineInterface) { - firstWaitSpent = false - pendingFor = null + // A read in the new conversation that came first has found its project already: that one stays. + if ((await get($)).root) return searched.clear() within.clear() const cwd = (await $.session.cwd().catch(() => null)) ?? (await $.session.root().catch(() => null)) diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 80f7a13c..087eb0e6 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -61,6 +61,8 @@ let carried: HealthState | null = null let clearPending = false /** clearStart's restore while it runs. */ let restoring: Promise | null = null +/** The dbt_health CLI call running now, settled either way; the next waits for it. */ +let cliCall: Promise | null = null /** Which model each file belonged to in earlier checks: a deleted schema file or model no longer shows in the latest answer. */ let known: HealthInsight[] = [] /** Whether the status line carries the health part now. */ @@ -511,8 +513,22 @@ async function check($: EngineInterface): Promise { let result: Awaited> try { const until = (await $.clock.now()) + CHECK_MS + // One CLI call at a time, across a /clear or a new session too: a call whose loop was cut short still runs, and + // the next one would share its artifact folder, so it waits for it. + const before = cliCall + if (before) await within($, Math.max(0, until - (await $.clock.now())), before) const env = await checkEnv($) - result = await runTool($, 'dbt_health', toolArgs(options.healthConfigPath), { write: true, until, ...(env && { env }) }) + const call = runTool($, 'dbt_health', toolArgs(options.healthConfigPath), { write: true, until, ...(env && { env }) }) + const settled = call.then( + () => {}, + () => {}, + ) + cliCall = settled + try { + result = await call + } finally { + if (cliCall === settled) cliCall = null + } } catch (error) { // Only a binary that is not there: one that is there and refused to start keeps its own words. if (NOT_FOUND.test(String(error))) return { problem: NO_CLI } diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index be62b04a..ac213c3a 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -322,6 +322,22 @@ test('a git push after a second /clear that came while the first restore was wri expect(shown.text).toContain('1 new health issue · stg_orders') }) +test('the next conversation\'s check waits for a CLI call the /clear cut short, so the two never share the artifact folder', async ($, on) => { + const clear = playsClear($, on) + let release!: () => void + const { clock, calls } = await start($, on, { runs: [[OLD_STG], [OLD_STG], [OLD_STG, NEW_STG]], hold: { at: 1, until: new Promise(resolve => (release = resolve)) } }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + expect(calls).toHaveLength(2) + await clear() + await clock.settle() + // The cut-short call is still running: the restored conversation's check has not started. + expect(calls).toHaveLength(2) + release() + await clock.settle() + expect(calls).toHaveLength(3) +}) + test('a second /clear before the first one\'s state was restored keeps what the first carried', async ($, on) => { const clear = playsClear($, on) const { clock, calls } = await start($, on, { runs: [[OLD_STG, OLD_FCT], [OLD_STG, OLD_FCT, NEW_STG]] }) From d50a1ebf1cb997ce56ca9ef96097c10e779d0b70 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 20 Nov 2026 02:29:21 +0530 Subject: [PATCH 12/13] =?UTF-8?q?fix:=20review=20round=209=20=E2=80=94=20h?= =?UTF-8?q?ealth=20CLI=20calls=20take=20a=20queue=20place=20before=20any?= =?UTF-8?q?=20wait,=20folder=20preparation=20included;=20one=20reset=20for?= =?UTF-8?q?=20a=20new=20conversation's=20comments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- .../altimate-code/hooks/features/comments.tsx | 12 ++++--- .../altimate-code/hooks/features/health.tsx | 36 +++++++++---------- plugins/altimate-code/hooks/health.test.tsx | 23 +++++++++++- 3 files changed, 48 insertions(+), 23 deletions(-) diff --git a/plugins/altimate-code/hooks/features/comments.tsx b/plugins/altimate-code/hooks/features/comments.tsx index 466bad07..b0a016f7 100644 --- a/plugins/altimate-code/hooks/features/comments.tsx +++ b/plugins/altimate-code/hooks/features/comments.tsx @@ -194,6 +194,12 @@ export function register(on: On, context: FeatureContext) { }) } +/** What a conversation keeps of its own, reset when one starts (a session's start, or a /clear's end). */ +function newConversation() { + firstWaitSpent = false + pendingFor = null +} + /** After the session started: the command, a first load, and the poll. */ export async function sessionStart($: EngineInterface, e: Args<'session.start'>) { await $.command.register({ name: COMMAND, description: 'Open comment threads from your team’s shared dbt docs; reply or resolve' }) @@ -201,8 +207,7 @@ export async function sessionStart($: EngineInterface, e: Args<'session.start'>) poll = null loadAgain = false generation++ - firstWaitSpent = false - pendingFor = null + newConversation() uncertain = null resolvedAt.clear() // Work still running when an earlier session ended can never finish, so it must not block this one. @@ -243,8 +248,7 @@ export async function sessionEnd($: EngineInterface, e: Args<'session.end'>) { if (e.reason === 'clear') { // The next conversation starts with empty state: its first read of a model waits for its list, as a session's // first read does, even when it comes before clearStart. - firstWaitSpent = false - pendingFor = null + newConversation() return } poll?.cancel() diff --git a/plugins/altimate-code/hooks/features/health.tsx b/plugins/altimate-code/hooks/features/health.tsx index 087eb0e6..640e1a5f 100644 --- a/plugins/altimate-code/hooks/features/health.tsx +++ b/plugins/altimate-code/hooks/features/health.tsx @@ -61,8 +61,18 @@ let carried: HealthState | null = null let clearPending = false /** clearStart's restore while it runs. */ let restoring: Promise | null = null -/** The dbt_health CLI call running now, settled either way; the next waits for it. */ -let cliCall: Promise | null = null +/** The last health CLI call queued, settled either way: the next one starts after it. */ +let cliQueue: Promise = Promise.resolve() + +/** Runs `job` after every one queued before it; its place is taken at once, before anything is awaited. */ +function oneAtATime(job: () => Promise): Promise { + const run = cliQueue.then(job, job) + cliQueue = run.then( + () => {}, + () => {}, + ) + return run +} /** Which model each file belonged to in earlier checks: a deleted schema file or model no longer shows in the latest answer. */ let known: HealthInsight[] = [] /** Whether the status line carries the health part now. */ @@ -513,22 +523,12 @@ async function check($: EngineInterface): Promise { let result: Awaited> try { const until = (await $.clock.now()) + CHECK_MS - // One CLI call at a time, across a /clear or a new session too: a call whose loop was cut short still runs, and - // the next one would share its artifact folder, so it waits for it. - const before = cliCall - if (before) await within($, Math.max(0, until - (await $.clock.now())), before) - const env = await checkEnv($) - const call = runTool($, 'dbt_health', toolArgs(options.healthConfigPath), { write: true, until, ...(env && { env }) }) - const settled = call.then( - () => {}, - () => {}, - ) - cliCall = settled - try { - result = await call - } finally { - if (cliCall === settled) cliCall = null - } + // One CLI call at a time, its folder's preparation included, across a /clear or a new session too: a call whose + // loop was cut short still runs, and the next one would share its artifact folder, so it takes its turn after it. + result = await oneAtATime(async () => { + const env = await checkEnv($) + return runTool($, 'dbt_health', toolArgs(options.healthConfigPath), { write: true, until, ...(env && { env }) }) + }) } catch (error) { // Only a binary that is not there: one that is there and refused to start keeps its own words. if (NOT_FOUND.test(String(error))) return { problem: NO_CLI } diff --git a/plugins/altimate-code/hooks/health.test.tsx b/plugins/altimate-code/hooks/health.test.tsx index ac213c3a..a35802a9 100644 --- a/plugins/altimate-code/hooks/health.test.tsx +++ b/plugins/altimate-code/hooks/health.test.tsx @@ -74,6 +74,7 @@ async function start($: any, on: any, options: Options, session: { isInteractive const calls: { tool: string; args: Record; argv: readonly string[]; env?: Record }[] = [] const toasts: string[] = [] const statuses: string[] = [] + const concurrent = { now: 0, most: 0 } /** The usage events sent: feature and outcome. */ const usage: string[][] = [] if (options.env) { @@ -139,7 +140,10 @@ async function start($: any, on: any, options: Options, session: { isInteractive const args = JSON.parse(String(e.argv[e.argv.indexOf('--args') + 1] ?? '{}')) const index = run++ calls.push({ tool, args, argv: e.argv, env: (e as { init?: { env?: Record } }).init?.env }) + // How many tool calls run at once: a held one counts until it is released. + concurrent.most = Math.max(concurrent.most, ++concurrent.now) if (options.hold?.at === index) await options.hold.until + concurrent.now-- const answer = options.runs[Math.min(index, options.runs.length - 1)]! if ('missing' in answer) return { deny: 'spawn ENOENT' } if ('refused' in answer) return { deny: 'failed to spawn: operation not permitted' } @@ -150,7 +154,7 @@ async function start($: any, on: any, options: Options, session: { isInteractive }) await $.session.start({ cwd: ROOT, surface: 'terminal', isInteractive: session.isInteractive ?? true }) await clock.settle() - return { clock, calls, toasts, statuses, usage } + return { clock, calls, toasts, statuses, usage, concurrent } } async function endTurn($: any, clock: any, id: string) { @@ -338,6 +342,23 @@ test('the next conversation\'s check waits for a CLI call the /clear cut short, expect(calls).toHaveLength(3) }) +test('two /clears while a cut-short CLI call runs still start their checks one after another', async ($, on) => { + const clear = playsClear($, on) + let release!: () => void + const { clock, calls, concurrent } = await start($, on, { runs: [[OLD_STG], [OLD_STG], [OLD_STG, NEW_STG]], hold: { at: 1, until: new Promise(resolve => (release = resolve)) } }) + await $.tool.call(edit(STG)) + await endTurn($, clock, 't1') + await clear() + await clock.settle() + await $.tool.call(edit(STG)) + await clear() + await clock.settle() + release() + await clock.settle() + expect(calls.length).toBeGreaterThanOrEqual(3) + expect(concurrent.most).toBe(1) +}) + test('a second /clear before the first one\'s state was restored keeps what the first carried', async ($, on) => { const clear = playsClear($, on) const { clock, calls } = await start($, on, { runs: [[OLD_STG, OLD_FCT], [OLD_STG, OLD_FCT, NEW_STG]] }) From 4c6f857a0ebf780c5c2e69ac56cf7da250d9d35f Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Fri, 20 Nov 2026 02:37:37 +0530 Subject: [PATCH 13/13] =?UTF-8?q?fix:=20review=20round=2010=20=E2=80=94=20?= =?UTF-8?q?the=20flags=20line=20is=20told=20only=20when=20the=20command=20?= =?UTF-8?q?that=20ran=20is=20the=20rewritten=20one?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- .../hooks/features/defer.test.ts | 30 +++++++++++++++++-- .../altimate-code/hooks/features/defer.tsx | 12 ++++---- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/plugins/altimate-code/hooks/features/defer.test.ts b/plugins/altimate-code/hooks/features/defer.test.ts index b8eace85..86f13753 100644 --- a/plugins/altimate-code/hooks/features/defer.test.ts +++ b/plugins/altimate-code/hooks/features/defer.test.ts @@ -1,5 +1,6 @@ import { expect, mock, test } from 'claude-code/testing' +import { PHASE_TIMEOUT_MS } from '../tool-chain' import { effective, rewriteDefer, rewriteDeferCommand, runsHere, runsInProject, statusText } from './defer' const on = { stateDir: '/work/prod-state' } @@ -224,6 +225,9 @@ test('the override decides, and the status text shows target and defer', () => { const ROOT = '/repo' /** The session's folder, answered by the fake host. */ let cwd = ROOT +/** Held while set: the project's defer setting read, and the Bash command itself. */ +let holdRead: Promise | null = null +let holdBash: Promise | null = null /** A dbt project at /repo whose defer setting the CLI reports as `answer`; Bash commands are recorded. */ function start($: any, on: any, answer: Record, fail: { times: number } = { times: 0 }, projects: string[] = [ROOT]) { @@ -235,11 +239,14 @@ function start($: any, on: any, answer: Record, fail: { times: on('session.start', (_: unknown, e: { cwd: string }) => ({ cwd: e.cwd })) on('session.root', () => ({ value: ROOT })) cwd = ROOT + holdRead = null + holdBash = null on('session.cwd', () => ({ value: cwd })) on('fs.exists', (_: unknown, e: { path: string }) => ({ value: projects.some(dir => e.path === `${dir}/dbt_project.yml`) })) - on('process.run', (_: unknown, e: { readonly argv: readonly string[] }) => { + on('process.run', async (_: unknown, e: { readonly argv: readonly string[] }) => { if (!e.argv.includes('dbt_defer_state')) return { value: { exitCode: 0, stdout: '{}', stderr: '' } } reads.push(e.argv.join(' ')) + await holdRead if (fail.times-- > 0) return { value: { exitCode: 1, stdout: '', stderr: 'not ready' } } return { value: { exitCode: 0, stdout: JSON.stringify({ status: 'success', output: '', metadata: { success: true, ...answer } }), stderr: '' }, @@ -249,8 +256,9 @@ function start($: any, on: any, answer: Record, fail: { times: statuses.push(e.text) return { value: undefined } }) - on('tool.call', (_: unknown, e: any) => { + on('tool.call', async (_: unknown, e: any) => { bash.push(e.command) + await holdBash return { result: { ok: true } } }) return { bash, statuses, reads, clock } @@ -456,3 +464,21 @@ test('each call gets the flags added to it: a concurrent call that wrote the sam const ran: any = await $.tool.call({ tool: 'Bash', command: 'dbt build -s x', tool_use_id: 'rewritten' }) expect(ran.context).toContain('◆ dbt · this command ran as dbt build --defer --state /work/prod-state --favor-state: /defer and /target added those flags (the command shown is as written), so its results are from that target and state') }) + +test('a rewrite the chain gave up on, which finishes while the command runs as written, tells Claude nothing about flags it never added', async ($, on) => { + const seen = start($, on, DEFER) + await $.session.start({ cwd: ROOT, surface: 'terminal', isInteractive: true }) + let releaseRead!: () => void + let releaseBash!: () => void + holdRead = new Promise(resolve => (releaseRead = resolve)) + holdBash = new Promise(resolve => (releaseBash = resolve)) + const call = $.tool.call({ tool: 'Bash', command: 'dbt build -s x', tool_use_id: 'late' }) + // Past the phase deadline the command runs as written; the settings read then finishes while it runs. + await seen.clock.advance(PHASE_TIMEOUT_MS + 100) + releaseRead() + await seen.clock.advance(10) + releaseBash() + const ran: any = await call + expect(seen.bash.at(-1)).toBe('dbt build -s x') + expect((ran.context ?? []).some((line: string) => line.startsWith('◆ dbt · this command ran as'))).toBe(false) +}) diff --git a/plugins/altimate-code/hooks/features/defer.tsx b/plugins/altimate-code/hooks/features/defer.tsx index a872e919..888485b6 100644 --- a/plugins/altimate-code/hooks/features/defer.tsx +++ b/plugins/altimate-code/hooks/features/defer.tsx @@ -19,7 +19,7 @@ const deferAtom = atom({ plugin: 'altimate-code', key: 'defer' } as const, null // What /defer and /target added to Claude's last dbt command, for the band, until the next prompt. const ranAtom = atom({ plugin: 'altimate-code', key: 'deferRan' } as const, null as string | null) // The flags added to each rewritten call, by the call's tool_use_id (the command as it ran when there is none), for its result. -const addedTo = new Map() +const addedTo = new Map() const callKey = (e: BashCall) => (typeof e.tool_use_id === 'string' && e.tool_use_id ? e.tool_use_id : `command:${e.command}`) // The commands that take `--defer` in every dbt version this reads state for (older dbt refuses the option on @@ -480,7 +480,7 @@ export async function bashRewrite($: EngineInterface, e: BashCall): Promise= 20) addedTo.delete(addedTo.keys().next().value!) - addedTo.set(callKey({ ...e, command }), added) + addedTo.set(callKey({ ...e, command }), { command, added }) return command } @@ -497,10 +497,12 @@ export async function bashGate($: EngineInterface, e: BashCall): Promise<{ deny: * told in the result, and the person in the band, what really ran. */ export async function bashAfter($: EngineInterface, e: BashCall, result: ToolCallResult): Promise { - const added = addedTo.get(callKey(e)) - if (!added) return + const kept = addedTo.get(callKey(e)) + if (!kept) return addedTo.delete(callKey(e)) - const text = added.join('; ') + // A rewrite the chain gave up on (its deadline passed) never ran: the command that ran says which did. + if (kept.command !== e.command) return + const text = kept.added.join('; ') await update($, ranAtom, () => text) return [`◆ dbt · this command ran as ${text}: /defer and /target added those flags (the command shown is as written), so its results are from that target and state`] }