From a1dbcf640ec18d322576e0b4ea1f1712e74adc7b Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 23 Nov 2026 08:01:25 -0600 Subject: [PATCH] fix(pm-aggregator): create metadata DB parent directory before opening (#13293) openMetadataStore opened the metadata DB without ensuring its parent directory exists; bun:sqlite creates the file but not its parents, so a clean environment (no ~/.local/state/pm-aggregator yet) crashed server boot with SQLITE_CANTOPEN. Create the parent with mkdirSync recursive before opening (:memory: exempt); genuine open failures still throw. Adds two regression tests: a missing nested parent directory is created and the store opens; an unhealable parent still fails closed. --- pm-stack/aggregator/src/metadata-ingestion.ts | 9 +++++ .../test/metadata-ingestion.test.ts | 33 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/pm-stack/aggregator/src/metadata-ingestion.ts b/pm-stack/aggregator/src/metadata-ingestion.ts index 11eafb1170..9ff74341dd 100644 --- a/pm-stack/aggregator/src/metadata-ingestion.ts +++ b/pm-stack/aggregator/src/metadata-ingestion.ts @@ -25,6 +25,8 @@ * Metadata *queries*; GitHub stays the system of record for the items. */ import type { Database } from "bun:sqlite"; +import { mkdirSync } from "node:fs"; +import { dirname } from "node:path"; import { createPmMetadataStore, parsePmMetadata, @@ -388,6 +390,13 @@ export function storeOutcome(db: Database, outcome: MetadataCheckOutcome): void /** Open (creating) the sweeper/listener database with the store installed. */ export function openMetadataStore(path: string, open: (path: string) => Database): Database { + // bun:sqlite creates the database file but not its parent directories, + // so a clean environment (no state directory yet) crashed the server at + // boot with SQLITE_CANTOPEN (#13293). Create the parent first, like the + // other state stores do on write; genuine open failures (permissions, + // a parent that is not a directory) still throw — only the + // missing-directory case self-heals. + if (path !== ":memory:") mkdirSync(dirname(path), { recursive: true }); const db = open(path); createPmMetadataStore(db); return db; diff --git a/pm-stack/aggregator/test/metadata-ingestion.test.ts b/pm-stack/aggregator/test/metadata-ingestion.test.ts index e441576d7f..7115a3f6cc 100644 --- a/pm-stack/aggregator/test/metadata-ingestion.test.ts +++ b/pm-stack/aggregator/test/metadata-ingestion.test.ts @@ -1,9 +1,14 @@ import { describe, expect, test } from "bun:test"; import { Database } from "bun:sqlite"; +import { existsSync } from "node:fs"; +import { mkdtemp, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; import { artifactIdFor, buildTriagePacket, extractMetadataPayload, + openMetadataStore, renderMetadataBlock, runMetadataCheck, sourceUrlFor, @@ -232,3 +237,31 @@ describe("buildTriagePacket: the broker's required fields, pinned", () => { } }); }); + +describe("openMetadataStore: clean-environment boot (#13293)", () => { + test("creates a missing parent directory before opening", async () => { + const root = await mkdtemp(join(tmpdir(), "pm-metadata-store-")); + try { + const path = join(root, "state", "pm-aggregator", "pm-metadata.sqlite"); + const db = openMetadataStore(path, (p) => new Database(p)); + expect( + db.query("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'pm_metadata_items'").get(), + ).toEqual({ name: "pm_metadata_items" }); + db.close(); + expect(existsSync(path)).toBe(true); + } finally { + await rm(root, { recursive: true, force: true }); + } + }); + + test("an unhealable parent still fails closed", async () => { + const root = await mkdtemp(join(tmpdir(), "pm-metadata-store-")); + try { + const blocker = join(root, "blocker"); + await writeFile(blocker, "not a directory"); + expect(() => openMetadataStore(join(blocker, "pm-metadata.sqlite"), (p) => new Database(p))).toThrow(); + } finally { + await rm(root, { recursive: true, force: true }); + } + }); +});