Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/opencode/src/altimate/mcp-server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export async function serverInstructions(): Promise<string> {
// fields are passed, and the whole is capped.
const STRUCTURED_FIELDS: Record<string, readonly string[]> = {
dbt_lineage: ["model_name", "model_unique_id", "confidence", "confidence_factors", "upstream", "columns", "edges", "error"],
dbt_sql_graph: ["model", "nodes", "edges", "error"],
Comment thread
hkaltai marked this conversation as resolved.
Resolved
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
impact_analysis: [
"model",
"column",
Expand Down Expand Up @@ -96,8 +97,14 @@ export function structuredResult(tool: string, metadata: Record<string, unknown>
return Buffer.byteLength(JSON.stringify(full)) <= STRUCTURED_MAX_BYTES ? full : capReport(report, true)
}
const fields = Object.fromEntries((STRUCTURED_FIELDS[tool] ?? []).filter((key) => key in metadata).map((key) => [key, metadata[key]]))
// dbt_lineage's text prints the same mappings and edges as `columns` and `edges`: keep its header.
const head = tool === "dbt_lineage" && "columns" in fields ? report.split(/\n(?:Column Mappings|Lineage Edges):/)[0]!.trim() : report
// dbt_lineage's text prints the same mappings and edges as `columns` and `edges`, and dbt_sql_graph's
// text lists the same nodes as `nodes`: keep only the header.
let head = report
if (tool === "dbt_lineage" && "columns" in fields) {
head = report.split(/\n(?:Column Mappings|Lineage Edges):/)[0]!.trim()
} else if (tool === "dbt_sql_graph" && "nodes" in fields) {
head = report.split("\n")[0]!.trim()
}
const full = { ...fields, report: head }
if (Buffer.byteLength(JSON.stringify(full)) <= STRUCTURED_MAX_BYTES) return full
// The column answer is the largest field: past the cap it goes first, so the model-level fields
Expand Down
56 changes: 47 additions & 9 deletions packages/opencode/src/altimate/validators/validator-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -838,10 +838,15 @@ function collectDeliverableTokens(tokens: string[]): { models: string[]; files:
// ---------------------------------------------------------------------------

/**
* Resolve the project's artifact directory. Honours `DBT_TARGET_PATH` and a
* `target-path:` key in `dbt_project.yml`; defaults to `target`.
*/
export async function resolveDbtTargetPath(dbtRoot: string): Promise<string> {
* Resolve the project's artifact directory. A `targetPath` the caller names (the
* folder a `--target-path` run wrote to) wins; else `DBT_TARGET_PATH` and a
* `target-path:` key in `dbt_project.yml` are honoured; defaults to `target`.
*/
export async function resolveDbtTargetPath(dbtRoot: string, targetPath?: string): Promise<string> {
// A folder the caller names (the one a `--target-path` run wrote to) wins over the environment and the project file.
if (targetPath && targetPath.trim().length > 0) {
return isAbsolutePath(targetPath) ? targetPath : join(dbtRoot, targetPath)
}
const fromEnv = process.env.DBT_TARGET_PATH
if (fromEnv && fromEnv.trim().length > 0) {
return isAbsolutePath(fromEnv) ? fromEnv : join(dbtRoot, fromEnv)
Expand Down Expand Up @@ -1191,6 +1196,8 @@ export interface RunResultNode {
status: string
/** dbt's message for the node, when present. */
message: string | null
/** Seconds dbt spent on the node (`execution_time`), when it wrote a number. */
executionTimeSec: number | null
}

/** A parsed `run_results.json` plus its freshness. */
Expand All @@ -1212,6 +1219,10 @@ export interface RunResultsArtifact {
* not evidence that anything was built.
*/
command: string | null
/** `metadata.invocation_id`, when the file carries one. */
invocationId: string | null
/** The run's `elapsed_time` in seconds, when the file carries one. */
elapsedSec: number | null
}

/**
Expand Down Expand Up @@ -1348,14 +1359,26 @@ export function isFailedRunStatus(status: string): boolean {
* Read and parse `<target>/run_results.json`. Returns null when the artifact
* is absent or unparseable — callers decide what that means for their gate.
*/
export async function readRunResults(dbtRoot: string): Promise<RunResultsArtifact | null> {
const targetPath = await resolveDbtTargetPath(dbtRoot)
export async function readRunResults(dbtRoot: string, targetPathOverride?: string): Promise<RunResultsArtifact | null> {
const targetPath = await resolveDbtTargetPath(dbtRoot, targetPathOverride)
const path = join(targetPath, "run_results.json")
try {
const stat = await fs.stat(path)
const stat = await fs.stat(path, { bigint: true })
if (!stat.isFile()) return null
const raw = await fs.readFile(path, "utf8")
const parsed = JSON.parse(raw) as { results?: unknown; args?: unknown }
// The time keeps the plain stat's own rounding: every other caller compares it with mtimes read the same way, and a different rounding of equal timestamps would order them apart. It is taken between the two bigint stats, so the comparison below proves it describes the same file version as the rows.
const plain = await fs.stat(path)
// A rewrite during the read changes the file's identity, size or times (nanosecond precision): the rows and the time would belong to different runs, so there is no evidence.
const after = await fs.stat(path, { bigint: true })
if (
after.ino !== stat.ino ||
after.size !== stat.size ||
after.mtimeNs !== stat.mtimeNs ||
after.ctimeNs !== stat.ctimeNs
)
return null
const mtimeMs = plain.mtimeMs
const parsed = JSON.parse(raw) as { results?: unknown; args?: unknown; metadata?: unknown; elapsed_time?: unknown }
// A file that parses but carries no `results` array is not a run artifact.
// Returning an empty one would read as "a build happened and recorded
// nothing", which lets an unbuilt model through; null means "no evidence".
Expand Down Expand Up @@ -1383,9 +1406,24 @@ export async function readRunResults(dbtRoot: string): Promise<RunResultsArtifac
version: versioned ? last : null,
status: typeof r["status"] === "string" ? r["status"].toLowerCase() : "",
message: typeof r["message"] === "string" ? r["message"] : null,
executionTimeSec:
typeof r["execution_time"] === "number" && Number.isFinite(r["execution_time"]) ? r["execution_time"] : null,
})
}
return { path, mtimeMs: stat.mtimeMs, results, command }
const meta =
typeof parsed.metadata === "object" && parsed.metadata !== null
? (parsed.metadata as Record<string, unknown>)
: null
const invocation = meta && typeof meta["invocation_id"] === "string" ? meta["invocation_id"] : ""
const elapsed = typeof parsed.elapsed_time === "number" && Number.isFinite(parsed.elapsed_time) ? parsed.elapsed_time : null
return {
path,
mtimeMs,
results,
command,
invocationId: invocation || null,
elapsedSec: elapsed,
}
} catch {
return null
}
Expand Down
16 changes: 16 additions & 0 deletions packages/opencode/test/altimate/column-reach.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,22 @@ describe("structuredError", () => {
.report,
).toBe("Model: x")
})
test("dbt_sql_graph's model, nodes and edges reach the structured fields, with only the report's header", () => {
const nodes = [{ id: "a", name: "a" }]
const report = "SQL graph for orders: 1 nodes, 0 edges\n- a (cte): 1 columns\n columns: id\n sql: select id from x"
expect(structuredResult("dbt_sql_graph", { model: "orders", nodes, edges: [], success: true }, report)).toEqual({
model: "orders",
nodes,
edges: [],
report: "SQL graph for orders: 1 nodes, 0 edges",
})
})
test("a dbt_sql_graph failure keeps its whole report", () => {
expect(structuredResult("dbt_sql_graph", { success: false, error: "no key" }, "Failed: no key\nsee the docs")).toEqual({
error: "no key",
report: "Failed: no key\nsee the docs",
})
})
test("exposures_error reaches the structured fields", () => {
expect(structuredResult("impact_analysis", { exposures_error: "bad json" }, "R")).toEqual({
exposures_error: "bad json",
Expand Down
23 changes: 21 additions & 2 deletions packages/opencode/test/altimate/dbt-adapter-access.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// dbt adapter is replaced through `dbtAdapterInternals`, so these tests need no Python or dbt.
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test } from "bun:test"
import { chmod, mkdtemp, rm, writeFile } from "node:fs/promises"
import { realpathSync } from "node:fs"
import { existsSync, readFileSync, realpathSync } from "node:fs"
import os from "node:os"
import path from "node:path"
import { Effect } from "effect"
Expand Down Expand Up @@ -32,6 +32,12 @@ const MINIMAL_ARGS: Record<string, Record<string, unknown>> = {
}
const FEATURE_TOOLS = Object.keys(MINIMAL_ARGS)

/** The sentence every stub's answer carries; a real tool replaces the whole file, so it no longer has it. */
const PLACEHOLDER_MARKER = "is not implemented in this build of altimate-code yet."
const toolSource = (id: string) => path.join(import.meta.dir, "../../src/altimate/tools", `${id.replaceAll("_", "-")}.ts`)
// Read when a test runs, so a missing source file fails the test that checks for it rather than the import.
const isPlaceholder = (id: string) => readFileSync(toolSource(id), "utf8").includes(PLACEHOLDER_MARKER)

const ctx: Tool.Context = {
sessionID: SessionID.make("ses_dbt_adapter_access"),
messageID: MessageID.make("msg_dbt_adapter_access"),
Expand Down Expand Up @@ -82,8 +88,21 @@ describe("feature tools in the allow-list", () => {
expect(listed).toEqual([...listed].sort())
})

test("each placeholder answers that it isn't implemented, as a failure", async () => {
test("every feature tool has its source file, so the placeholder marker is always read", () => {
for (const id of FEATURE_TOOLS) expect(existsSync(toolSource(id))).toBe(true)
})

test("a tool is classed a placeholder by its source's marker exactly when it answers that it isn't implemented", async () => {
for (const id of FEATURE_TOOLS) {
const tool = await initTool(EXPOSED[id] as any)
expect(tool.id).toBe(id)
const result = await tool.execute(MINIMAL_ARGS[id], ctx)
expect(result.title === `${id}: not implemented`).toBe(isPlaceholder(id))
}
})

test("each placeholder answers that it isn't implemented, as a failure", async () => {
for (const id of FEATURE_TOOLS.filter(isPlaceholder)) {
const tool = await initTool(EXPOSED[id] as any)
expect(tool.id).toBe(id)
const result = await tool.execute(MINIMAL_ARGS[id], ctx)
Expand Down
13 changes: 13 additions & 0 deletions packages/opencode/test/altimate/validators/dbt-build-green.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ describe("DbtBuildGreenValidator — fresh artifact", () => {
expect(r.details!["verdict"]).toBe("fresh-build")
})

test("a model and an artifact stamped with the same instant read as built, not edited after the build", async () => {
await makeProject()
await writeModel("stg_orders")
await writeRunResults([{ id: "model.t.stg_orders", status: "success" }])
// A coarse filesystem clock gives writes in one tick the same nanosecond timestamp.
const t = 1_800_000_000.004123
await fs.utimes(join(dir, "models", "stg_orders.sql"), t, t)
await fs.utimes(join(dir, "target", "run_results.json"), t, t)
const r = await DbtBuildGreenValidator.check(ctx())
expect(r.ok).toBe(true)
expect(r.details!["verdict"]).toBe("fresh-build")
})

test("treats warn as a clean build", async () => {
await makeProject()
await writeModel("stg_orders")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// altimate_change start — tests for the nothing-built inverse completion gate
import { describe, expect, test, beforeEach, afterEach } from "bun:test"
import { describe, expect, test, beforeEach, afterEach, spyOn } from "bun:test"
import { promises as fs } from "fs"
import { tmpdir } from "os"
import { join } from "path"
Expand Down Expand Up @@ -274,6 +274,84 @@ describe("run_results and inventory helpers", () => {
expect(await resolveDbtTargetPath(dir)).toBe(join(dir, "alt_target"))
})

test("resolveDbtTargetPath lets a caller-named folder win over DBT_TARGET_PATH and the project file", async () => {
await makeProject()
await fs.appendFile(join(dir, "dbt_project.yml"), 'target-path: "build_out"\n')
process.env.DBT_TARGET_PATH = "alt_target"
expect(await resolveDbtTargetPath(dir, "chosen")).toBe(join(dir, "chosen"))
expect(await resolveDbtTargetPath(dir, join(dir, "abs"))).toBe(join(dir, "abs"))
expect(await resolveDbtTargetPath(dir, " ")).toBe(join(dir, "alt_target"))
})

test("readRunResults reads a caller-named folder and returns the invocation id, elapsed time and per-node time", async () => {
delete process.env.DBT_TARGET_PATH
await makeProject()
await fs.mkdir(join(dir, "chosen"), { recursive: true })
await fs.writeFile(
join(dir, "chosen", "run_results.json"),
JSON.stringify({
metadata: { invocation_id: "abc-123" },
elapsed_time: 4.5,
args: { which: "build" },
results: [
{ unique_id: "model.t.a", status: "success", execution_time: 1.25 },
{ unique_id: "model.t.b", status: "success" },
],
}),
)
expect(await readRunResults(dir)).toBeNull()
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
const artifact = await readRunResults(dir, "chosen")
expect(artifact?.invocationId).toBe("abc-123")
expect(artifact?.elapsedSec).toBe(4.5)
expect(artifact?.results.map((r) => r.executionTimeSec)).toEqual([1.25, null])
})

test("readRunResults returns null for a file rewritten while it was being read", async () => {
delete process.env.DBT_TARGET_PATH
await makeProject()
await fs.mkdir(join(dir, "target"), { recursive: true })
const file = join(dir, "target", "run_results.json")
await fs.writeFile(file, JSON.stringify({ args: { which: "build" }, results: [] }))
const real = fs.stat
let bigints = 0
const spy = spyOn(fs, "stat").mockImplementation((async (p: any, o?: any) => {
const stat: any = await real(p, o)
if (String(p) === file && o?.bigint && ++bigints === 2) return { ...stat, isFile: () => true, size: stat.size + 1n }
return stat
}) as any)
try {
expect(await readRunResults(dir)).toBeNull()
} finally {
spy.mockRestore()
}
})

test("readRunResults takes the time from between the two identity stats, so it describes the version the rows came from", async () => {
delete process.env.DBT_TARGET_PATH
const dir = await fs.mkdtemp(join(tmpdir(), "rr-order-"))
await fs.mkdir(join(dir, "target"), { recursive: true })
await fs.writeFile(join(dir, "target", "run_results.json"), JSON.stringify({ args: { which: "build" }, results: [] }))
const real = fs.stat
// Only the stat between the two identity checks answers this time, so the time returned is that one.
const between = 1_234_567
const kinds: string[] = []
const spy = spyOn(fs, "stat").mockImplementation((async (p: any, o?: any) => {
const stat: any = await real(p, o)
if (!String(p).endsWith("run_results.json")) return stat
kinds.push(o?.bigint ? "bigint" : "plain")
return kinds.length === 2 && !o?.bigint ? Object.assign(Object.create(Object.getPrototypeOf(stat)), stat, { mtimeMs: between }) : stat
}) as any)
let artifact: Awaited<ReturnType<typeof readRunResults>> = null
try {
artifact = await readRunResults(dir)
} finally {
spy.mockRestore()
await fs.rm(dir, { recursive: true, force: true })
}
expect(kinds).toEqual(["bigint", "plain", "bigint"])
Comment thread
hkaltai marked this conversation as resolved.
Resolved
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
expect(artifact?.mtimeMs).toBe(between)
})

test("readRunResults returns null when the artifact is missing", async () => {
await makeProject()
expect(await readRunResults(dir)).toBeNull()
Expand Down
Loading