|
| 1 | +/** |
| 2 | + * A warehouse failure must never reach the agent as an empty result. |
| 3 | + * |
| 4 | + * Field report against the real rig: a compiled binary returned "no tables" |
| 5 | + * with no error and no file created anywhere on disk. The cause was not the |
| 6 | + * silent-creation path — it is that a failure was shaped like success all the |
| 7 | + * way to the model: |
| 8 | + * |
| 9 | + * 1. `packages/drivers/src/duckdb.ts` read `(config.path as string) ?? ":memory:"`, |
| 10 | + * so a config that arrived without a `path` became an in-memory database. |
| 11 | + * Nothing is created, nothing is on disk, every query returns no rows. |
| 12 | + * 2. `sql.execute` never throws. It catches every connection and query error |
| 13 | + * and returns `{ columns: [], rows: [], row_count: 0, error }` |
| 14 | + * (native/connections/register.ts). |
| 15 | + * 3. `formatResult()` rendered that as the literal string `"(0 rows)"` and |
| 16 | + * never read `error` — so the agent saw a healthy empty table. |
| 17 | + * |
| 18 | + * The layers are tested where each one breaks: the guard against the real |
| 19 | + * registry, and the rendering against a stubbed `sql.execute` that returns the |
| 20 | + * error-carrying shape the real handler produces. The stub is deliberate — |
| 21 | + * `Dispatcher.reset()` leaks between test files in Bun, and the true |
| 22 | + * end-to-end through the live dispatcher is covered by the compiled-binary |
| 23 | + * test in store-path-resolution.test.ts, which runs in its own process. |
| 24 | + */ |
| 25 | + |
| 26 | +import { afterEach, beforeEach, describe, expect, test } from "bun:test" |
| 27 | +import { Database } from "bun:sqlite" |
| 28 | +import * as fs from "fs" |
| 29 | +import * as os from "os" |
| 30 | +import * as path from "path" |
| 31 | +import { initTool } from "./tool-fixture" |
| 32 | +import * as Dispatcher from "../../src/altimate/native/dispatcher" |
| 33 | +import * as Registry from "../../src/altimate/native/connections/registry" |
| 34 | +import { SqlExecuteTool } from "../../src/altimate/tools/sql-execute" |
| 35 | + |
| 36 | +/** Unguessable, so a pass cannot come from anything but the real store. */ |
| 37 | +const CANARY_TABLE = "zorbulax_ledger" |
| 38 | + |
| 39 | +const ctx = { |
| 40 | + sessionID: "test-session", |
| 41 | + messageID: "test-message", |
| 42 | + callID: "test-call", |
| 43 | + agent: "test", |
| 44 | + abort: new AbortController().signal, |
| 45 | + messages: [], |
| 46 | + metadata: () => {}, |
| 47 | +} |
| 48 | + |
| 49 | +const tmpDirs: string[] = [] |
| 50 | +function tmp(): string { |
| 51 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "warehouse-failure-")) |
| 52 | + tmpDirs.push(dir) |
| 53 | + return dir |
| 54 | +} |
| 55 | + |
| 56 | +beforeEach(() => { |
| 57 | + process.env.ALTIMATE_TELEMETRY_DISABLED = "true" |
| 58 | + Registry.reset() |
| 59 | +}) |
| 60 | + |
| 61 | +afterEach(() => { |
| 62 | + Registry.reset() |
| 63 | + while (tmpDirs.length) fs.rmSync(tmpDirs.pop()!, { recursive: true, force: true }) |
| 64 | +}) |
| 65 | + |
| 66 | +const LIST_TABLES = `SELECT name FROM sqlite_master WHERE type = 'table'` |
| 67 | + |
| 68 | +describe("a file-backed connection that cannot be resolved fails loudly", () => { |
| 69 | + test("a config with no path is rejected instead of becoming an in-memory database", async () => { |
| 70 | + // The reported shape: nothing on disk, no error, every query empty. A |
| 71 | + // `?? ":memory:"` default turns any failure to carry a path into a |
| 72 | + // successful query against nothing. |
| 73 | + Registry.setConfigs({ wh: { type: "sqlite" } }) |
| 74 | + |
| 75 | + await expect(Registry.get("wh")).rejects.toThrow(/path/) |
| 76 | + }) |
| 77 | + |
| 78 | + test("a missing store file is rejected, and no store is conjured", async () => { |
| 79 | + const missing = path.join(tmp(), "definitely-absent.db") |
| 80 | + Registry.setConfigs({ wh: { type: "sqlite", path: missing } }) |
| 81 | + |
| 82 | + await expect(Registry.get("wh")).rejects.toThrow(/not found/) |
| 83 | + expect(fs.existsSync(missing)).toBe(false) |
| 84 | + }) |
| 85 | + |
| 86 | + test("an explicit :memory: is still honoured — it just has to be asked for", async () => { |
| 87 | + Registry.setConfigs({ wh: { type: "sqlite", path: ":memory:" } }) |
| 88 | + |
| 89 | + const connector = await Registry.get("wh") |
| 90 | + const result = await connector.execute("SELECT 1 AS n") |
| 91 | + expect(result.rows[0][0]).toBe(1) |
| 92 | + }) |
| 93 | + |
| 94 | + test("a populated store still opens and returns its real tables", async () => { |
| 95 | + const store = path.join(tmp(), "warehouse.db") |
| 96 | + const seed = new Database(store, { create: true }) |
| 97 | + seed.exec(`CREATE TABLE ${CANARY_TABLE}(id INTEGER)`) |
| 98 | + seed.close() |
| 99 | + Registry.setConfigs({ wh: { type: "sqlite", path: store } }) |
| 100 | + |
| 101 | + const connector = await Registry.get("wh") |
| 102 | + const result = await connector.execute(LIST_TABLES) |
| 103 | + expect(result.rows.map((r) => r[0])).toContain(CANARY_TABLE) |
| 104 | + }) |
| 105 | +}) |
| 106 | + |
| 107 | +describe("sql_execute renders a warehouse failure as a failure", () => { |
| 108 | + // Reset first, then register: `call()` runs the lazy registration hook |
| 109 | + // before dispatching, and that would re-import the real handler and clobber |
| 110 | + // the stub. This is the same order tool-response-normalization.test.ts uses. |
| 111 | + beforeEach(() => { |
| 112 | + Dispatcher.reset() |
| 113 | + }) |
| 114 | + |
| 115 | + /** The exact shape sql.execute returns for any connection or query error. */ |
| 116 | + function stubFailure(message: string) { |
| 117 | + Dispatcher.register("sql.execute" as any, async () => ({ |
| 118 | + columns: [], |
| 119 | + rows: [], |
| 120 | + row_count: 0, |
| 121 | + truncated: false, |
| 122 | + error: message, |
| 123 | + })) |
| 124 | + } |
| 125 | + |
| 126 | + test("an error-carrying result is reported, not printed as (0 rows)", async () => { |
| 127 | + // This is the step that turned a broken connection into "no tables": |
| 128 | + // formatResult() returned "(0 rows)" for row_count === 0 and never looked |
| 129 | + // at `error`, so the model was told the warehouse was simply empty. |
| 130 | + stubFailure('SQLite database file not found: "/nowhere/absent.db".') |
| 131 | + |
| 132 | + const tool = await initTool(SqlExecuteTool) |
| 133 | + const result = await tool.execute({ query: LIST_TABLES }, ctx as any) |
| 134 | + |
| 135 | + expect(result.output).not.toContain("(0 rows)") |
| 136 | + expect(result.title).toContain("ERROR") |
| 137 | + expect(result.output).toContain("not found") |
| 138 | + expect(result.metadata.error).toBeDefined() |
| 139 | + }) |
| 140 | + |
| 141 | + test("a genuinely empty result set is still reported as empty, not as an error", async () => { |
| 142 | + // The distinction that matters: zero rows from a healthy warehouse is not |
| 143 | + // a failure, and must not start reporting as one. |
| 144 | + Dispatcher.register("sql.execute" as any, async () => ({ |
| 145 | + columns: ["name"], |
| 146 | + rows: [], |
| 147 | + row_count: 0, |
| 148 | + truncated: false, |
| 149 | + })) |
| 150 | + |
| 151 | + const tool = await initTool(SqlExecuteTool) |
| 152 | + const result = await tool.execute({ query: LIST_TABLES }, ctx as any) |
| 153 | + |
| 154 | + expect(result.title).not.toContain("ERROR") |
| 155 | + expect(result.output).toContain("(0 rows)") |
| 156 | + expect(result.metadata.error).toBeUndefined() |
| 157 | + }) |
| 158 | +}) |
0 commit comments