diff --git a/src/sqlite/SqliteDatabaseInspection.ts b/src/sqlite/SqliteDatabaseInspection.ts index 0384da516d..4f29e33bef 100644 --- a/src/sqlite/SqliteDatabaseInspection.ts +++ b/src/sqlite/SqliteDatabaseInspection.ts @@ -1,3 +1,4 @@ +import { statSync } from "node:fs"; import { DatabaseSync } from "node:sqlite"; import { z } from "zod"; import { Budget } from "../domain/budget.js"; @@ -1068,6 +1069,10 @@ export const inspectSqliteDatabaseSnapshot = ( snapshotPath: string, input: InspectSqliteDatabaseInput, ): Inspection => { + // SQLite's Unix VFS reports a one-byte file as size zero, so the engine would + // open it as an empty database and never reach its own header check. + if (statSync(snapshotPath).size === 1) + throw new SqliteInspectionFailure("format", "file is not a database"); const db = new DatabaseSync(snapshotPath, { readOnly: true, allowExtension: false, diff --git a/tests/acceptance/sqliteDatabaseCli.test.ts b/tests/acceptance/sqliteDatabaseCli.test.ts index afd0e9aa28..29e2ec04c3 100644 --- a/tests/acceptance/sqliteDatabaseCli.test.ts +++ b/tests/acceptance/sqliteDatabaseCli.test.ts @@ -939,6 +939,43 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( }, ); +/** Assert the refusal's facts, not the sentence that joins them. */ +const expectNotADatabaseRefusal = (error: unknown, selected: string): void => { + expect(error).toMatchObject({ + code: "invalid_request", + details: { + issues: [ + { + path: ["path"], + reason: "invalid_format", + message: expect.stringContaining("file is not a database"), + }, + ], + }, + }); + expect(error).toMatchObject({ + details: { issues: [{ message: expect.stringContaining(selected) }] }, + }); +}; + +cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( + "refuses a one-byte file instead of reporting a complete empty schema", + async ({ cli }) => { + const root = await createTestTempDirectory("rea-sqlite-one-byte-"); + const selected = join(root, "truncated.db"); + await writeFile(selected, "S"); + const refused = await cli.run({ + arguments: ["inspect-sqlite-database", selected, "--json"], + environment: { REA_LOG_LEVEL: "silent" }, + }); + expect(refused.exitCode).toBe(1); + expectNotADatabaseRefusal(refused.json, selected); + const call = await parityMcp.session.call(); + const response = await call("inspect_sqlite_database", { path: selected }); + expectNotADatabaseRefusal(parseMcpToolError(response).error, selected); + }, +); + cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( "returns actionable public errors for corrupt input, unknown or ambiguous tables and invalid record choices", async ({ cli }) => { @@ -948,28 +985,16 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( // Distinct malformed selections each name their own selected path. for (const selected of [path, join(root, "other-invalid.db")]) { await writeFile(selected, "not a SQLite database"); - const expectedError = { - code: "invalid_request", - details: { - issues: [ - { - path: ["path"], - reason: "invalid_format", - message: `Selected SQLite database is invalid (file is not a database): ${selected}`, - }, - ], - }, - }; const corrupt = await cli.run({ arguments: ["inspect-sqlite-database", selected, "--json"], environment: { REA_LOG_LEVEL: "silent" }, }); expect(corrupt.exitCode).toBe(1); - expect(corrupt.json).toMatchObject(expectedError); + expectNotADatabaseRefusal(corrupt.json, selected); const response = await call("inspect_sqlite_database", { path: selected, }); - expect(parseMcpToolError(response).error).toMatchObject(expectedError); + expectNotADatabaseRefusal(parseMcpToolError(response).error, selected); } const valid = join(root, "valid.db"); const database = new DatabaseSync(valid);